PageRenderTime 62ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/java-api/test/de/zib/scalaris/ErlangValueTest.java

http://scalaris.googlecode.com/
Java | 1034 lines | 805 code | 85 blank | 144 comment | 71 complexity | 673e180062e82c03c523d9d369d1db70 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause, Apache-2.0
  1. /**
  2. * Copyright 2011 Zuse Institute Berlin
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package de.zib.scalaris;
  17. import static org.junit.Assert.*;
  18. import java.math.BigInteger;
  19. import java.util.ArrayList;
  20. import java.util.LinkedHashMap;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.Map.Entry;
  24. import java.util.Random;
  25. import java.util.LinkedList;
  26. import org.junit.Test;
  27. import com.ericsson.otp.erlang.OtpErlangBinary;
  28. import com.ericsson.otp.erlang.OtpErlangBoolean;
  29. import com.ericsson.otp.erlang.OtpErlangDouble;
  30. import com.ericsson.otp.erlang.OtpErlangInt;
  31. import com.ericsson.otp.erlang.OtpErlangList;
  32. import com.ericsson.otp.erlang.OtpErlangLong;
  33. import com.ericsson.otp.erlang.OtpErlangObject;
  34. import com.ericsson.otp.erlang.OtpErlangString;
  35. /**
  36. * Unit tests for {@link ErlangValue}.
  37. *
  38. * TODO: implement tests verifying that the expected exceptions are thrown for
  39. * unsupported conversions
  40. *
  41. * @author Nico Kruber, kruber@zib.de
  42. */
  43. public class ErlangValueTest {
  44. private static BigInteger getRandomBigInt(final Random random) {
  45. return BigInteger.valueOf(random.nextLong()).multiply(BigInteger.valueOf(random.nextLong()));
  46. }
  47. private static byte[] getRandomBytes(final Random random, final int size) {
  48. final byte[] bytes = new byte[size];
  49. random.nextBytes(bytes);
  50. return bytes;
  51. }
  52. private static String getRandomString(final Random random, final int length, final boolean onlyChars) {
  53. if (onlyChars) {
  54. return getRandomCharString(random, length);
  55. } else {
  56. return new String(getRandomBytes(random, length));
  57. }
  58. }
  59. private static final char[] chars = new char[26];
  60. private static final char[] digits = new char[10];
  61. private static final char[] symbols = new char[36];
  62. static {
  63. for (int i = 0; i < 26; ++i) {
  64. chars[i] = (char) ('a' + i);
  65. symbols[i] = (char) ('a' + i);
  66. }
  67. for (int i = 0; i < 10; ++i) {
  68. digits[i] = (char) ('0' + i);
  69. symbols[i + 26] = (char) ('0' + i);
  70. }
  71. }
  72. private static String getRandomCharString(final Random random, final int length) {
  73. if(length > 0) {
  74. final char[] result = new char[length];
  75. // lets always start with a character:
  76. result[0] = chars[random.nextInt(chars.length)];
  77. for (int i = 1; i < result.length; ++i) {
  78. result[0] = symbols[random.nextInt(symbols.length)];
  79. }
  80. return new String(result);
  81. }
  82. return "";
  83. }
  84. private static List<Object> getRandomList(final Random random, final int capacity) {
  85. // note: we do not generate (recursive) maps -> so there won't be keys to worry about
  86. return getRandomListRecursive(random, capacity, 0, false);
  87. }
  88. private static List<Object> getRandomListRecursive(final Random random, final int capacity, final int maxDepth, final boolean mapKeyOnlyChars) {
  89. List<Object> currentList = null;
  90. currentList = new ArrayList<Object>(capacity);
  91. final int curMaxDepth = maxDepth == 0 ? 0 : random.nextInt(maxDepth);
  92. final int maxType = curMaxDepth == 0 ? 6 : 8;
  93. switch (random.nextInt(maxType)) {
  94. case 0: // bool
  95. currentList.add(random.nextBoolean());
  96. break;
  97. case 1: // int
  98. currentList.add(random.nextInt());
  99. break;
  100. case 2: // long
  101. currentList.add(random.nextLong());
  102. break;
  103. case 3: // BigInteger
  104. currentList.add(getRandomBigInt(random));
  105. break;
  106. case 4: // double
  107. currentList.add(random.nextDouble());
  108. break;
  109. case 5: // String
  110. currentList.add(getRandomString(random, random.nextInt(10), false));
  111. break;
  112. case 6: // List
  113. currentList.add(getRandomListRecursive(random, capacity, curMaxDepth, mapKeyOnlyChars));
  114. break;
  115. case 7: // Map
  116. currentList.add(getRandomMapRecursive(random, capacity, curMaxDepth, mapKeyOnlyChars));
  117. break;
  118. default:
  119. throw new RuntimeException("unexpected random number");
  120. }
  121. return currentList;
  122. }
  123. private static Map<String, Object> getRandomMapRecursive(final Random random, final int capacity, final int maxDepth, final boolean keyOnlyChars) {
  124. Map<String, Object> currentMap = null;
  125. currentMap = new LinkedHashMap<String, Object>(capacity);
  126. for (int i = 0; i < capacity; ++i) {
  127. // key:
  128. final String key = getRandomString(random, random.nextInt(10), keyOnlyChars);
  129. // value:
  130. final int curMaxDepth = maxDepth == 0 ? 0 : random.nextInt(maxDepth);
  131. final int maxType = curMaxDepth == 0 ? 6 : 8;
  132. switch (random.nextInt(maxType)) {
  133. case 0: // bool
  134. currentMap.put(key, random.nextBoolean());
  135. break;
  136. case 1: // int
  137. currentMap.put(key, random.nextInt());
  138. break;
  139. case 2: // long
  140. currentMap.put(key, random.nextLong());
  141. break;
  142. case 3: // BigInteger
  143. currentMap.put(key, getRandomBigInt(random));
  144. break;
  145. case 4: // double
  146. currentMap.put(key, random.nextDouble());
  147. break;
  148. case 5: // String
  149. currentMap.put(key, getRandomString(random, random.nextInt(10), false));
  150. break;
  151. case 6: // List
  152. currentMap.put(key, getRandomListRecursive(random, capacity, curMaxDepth, keyOnlyChars));
  153. break;
  154. case 7: // Map
  155. currentMap.put(key, getRandomMapRecursive(random, capacity, curMaxDepth, keyOnlyChars));
  156. break;
  157. default:
  158. throw new RuntimeException("unexpected random number");
  159. }
  160. }
  161. return currentMap;
  162. }
  163. /**
  164. * Test method for {@link de.zib.scalaris.ErlangValue#boolValue()}.
  165. */
  166. @Test
  167. public final void testBoolValue() {
  168. final ErlangValue trueVal = new ErlangValue(true);
  169. final ErlangValue trueValOtp = new ErlangValue(new OtpErlangBoolean(true));
  170. final ErlangValue falseVal = new ErlangValue(false);
  171. final ErlangValue falseValOtp = new ErlangValue(new OtpErlangBoolean(false));
  172. assertEquals(true, trueVal.boolValue());
  173. assertEquals(true, trueValOtp.boolValue());
  174. assertEquals(false, falseVal.boolValue());
  175. assertEquals(false, falseValOtp.boolValue());
  176. assertTrue(trueVal.equals(trueValOtp));
  177. assertTrue(trueValOtp.equals(trueVal));
  178. assertTrue(falseVal.equals(falseValOtp));
  179. assertTrue(falseValOtp.equals(falseVal));
  180. }
  181. /**
  182. * Test method for {@link de.zib.scalaris.ErlangValue#intValue()}.
  183. *
  184. * @throws Exception if a test with a random integer failed
  185. */
  186. @Test
  187. public final void testIntValue() throws Exception {
  188. final Random random = new Random();
  189. for (int i = 0; i < 10000; ++i) {
  190. Integer currentInt = null;
  191. try {
  192. currentInt = random.nextInt();
  193. testIntValue(currentInt);
  194. } catch (final ClassCastException e) {
  195. throw new Exception("testIntValue(" + currentInt + ") failed", e);
  196. }
  197. }
  198. }
  199. private final void testIntValue(final int value) {
  200. final ErlangValue eVal = new ErlangValue(value);
  201. final ErlangValue eValOtp = new ErlangValue(new OtpErlangInt(value));
  202. assertEquals(value, eVal.intValue());
  203. assertEquals(value, eValOtp.intValue());
  204. assertTrue(eVal.equals(eValOtp));
  205. assertTrue(eValOtp.equals(eVal));
  206. }
  207. /**
  208. * Test method for {@link de.zib.scalaris.ErlangValue#longValue()}.
  209. *
  210. * @throws Exception if a test with a random long failed
  211. */
  212. @Test
  213. public final void testLongValue() throws Exception {
  214. final Random random = new Random();
  215. for (int i = 0; i < 10000; ++i) {
  216. Long currentLong = null;
  217. try {
  218. currentLong = random.nextLong();
  219. testLongValue(currentLong);
  220. } catch (final ClassCastException e) {
  221. throw new Exception("testLongValue(" + currentLong + ") failed", e);
  222. }
  223. }
  224. }
  225. private final void testLongValue(final long value) {
  226. final ErlangValue eVal = new ErlangValue(value);
  227. final ErlangValue eValOtp = new ErlangValue(new OtpErlangLong(value));
  228. assertEquals(value, eVal.longValue());
  229. assertEquals(value, eValOtp.longValue());
  230. assertTrue(eVal.equals(eValOtp));
  231. assertTrue(eValOtp.equals(eVal));
  232. }
  233. /**
  234. * Test method for {@link de.zib.scalaris.ErlangValue#bigIntValue()}.
  235. *
  236. * @throws Exception if a test with a random big integer failed
  237. */
  238. @Test
  239. public final void testBigIntValue() throws Exception {
  240. final Random random = new Random();
  241. for (int i = 0; i < 10000; ++i) {
  242. BigInteger currentBigInt = null;
  243. try {
  244. currentBigInt = getRandomBigInt(random);
  245. testBigIntValue(currentBigInt);
  246. } catch (final ClassCastException e) {
  247. throw new Exception("testBigIntValue(" + currentBigInt + ") failed", e);
  248. }
  249. }
  250. }
  251. private final void testBigIntValue(final BigInteger value) {
  252. final ErlangValue eVal = new ErlangValue(value);
  253. final ErlangValue eValOtp = new ErlangValue(new OtpErlangLong(value));
  254. assertEquals(value, eVal.bigIntValue());
  255. assertEquals(value, eValOtp.bigIntValue());
  256. assertTrue(eVal.equals(eValOtp));
  257. assertTrue(eValOtp.equals(eVal));
  258. }
  259. /**
  260. * Test method for {@link de.zib.scalaris.ErlangValue#doubleValue()}.
  261. *
  262. * @throws Exception if a test with a random double failed
  263. */
  264. @Test
  265. public final void testDoubleValue() throws Exception {
  266. final Random random = new Random();
  267. for (int i = 0; i < 10000; ++i) {
  268. Double currentDouble = null;
  269. try {
  270. currentDouble = random.nextDouble();
  271. testDoubleValue(currentDouble);
  272. } catch (final ClassCastException e) {
  273. throw new Exception("testDoubleValue(" + currentDouble + ") failed", e);
  274. }
  275. }
  276. }
  277. private final void testDoubleValue(final double value) {
  278. final ErlangValue eVal = new ErlangValue(value);
  279. final ErlangValue eValOtp = new ErlangValue(new OtpErlangDouble(value));
  280. assertEquals(value, eVal.doubleValue(), 0.0);
  281. assertEquals(value, eValOtp.doubleValue(), 0.0);
  282. assertTrue(eVal.equals(eValOtp));
  283. assertTrue(eValOtp.equals(eVal));
  284. }
  285. /**
  286. * Test method for {@link de.zib.scalaris.ErlangValue#stringValue()}.
  287. *
  288. * @throws Exception if a test with a random double failed
  289. */
  290. @Test
  291. public final void testStringValue() throws Exception {
  292. final Random random = new Random();
  293. for (int i = 0; i < 10000; ++i) {
  294. String currentString = null;
  295. try {
  296. currentString = getRandomString(random, random.nextInt(1000), false);
  297. testStringValue(currentString);
  298. } catch (final ClassCastException e) {
  299. throw new Exception("testStringValue(" + currentString + ") failed", e);
  300. }
  301. }
  302. }
  303. private final void testStringValue(final String value) {
  304. final ErlangValue eVal = new ErlangValue(value);
  305. final ErlangValue eValOtp = new ErlangValue(new OtpErlangString(value));
  306. assertEquals(value, eVal.stringValue());
  307. assertEquals(value, eValOtp.stringValue());
  308. assertTrue(eVal.equals(eValOtp));
  309. assertTrue(eValOtp.equals(eVal));
  310. }
  311. /**
  312. * Test method for {@link de.zib.scalaris.ErlangValue#binaryValue()}.
  313. *
  314. * @throws Exception if a test with a random byte array failed
  315. */
  316. @Test
  317. public final void testBinaryValue() throws Exception {
  318. final Random random = new Random();
  319. for (int i = 0; i < 5000; ++i) {
  320. try {
  321. testBinaryValue(getRandomBytes(random, random.nextInt(1000)));
  322. } catch (final ClassCastException e) {
  323. // do not print generated bytes (probably not useful)
  324. throw new Exception("testBinaryValue(...) failed", e);
  325. }
  326. }
  327. }
  328. private final void testBinaryValue(final byte[] value) {
  329. final ErlangValue eVal = new ErlangValue(value);
  330. final ErlangValue eValOtp = new ErlangValue(new OtpErlangBinary(value));
  331. assertArrayEquals(value, eVal.binaryValue());
  332. assertArrayEquals(value, eValOtp.binaryValue());
  333. assertTrue(eVal.equals(eValOtp));
  334. assertTrue(eValOtp.equals(eVal));
  335. }
  336. /**
  337. * Test method for {@link de.zib.scalaris.ErlangValue#listValue()}.
  338. *
  339. * @throws Exception
  340. * if a test with a random list of mixed objects (bool, int,
  341. * long, BigInteger, double, String) failed
  342. */
  343. @Test
  344. public final void testListValue() throws Exception {
  345. final Random random = new Random();
  346. for (int i = 0; i < 10000; ++i) {
  347. List<Object> currentList = null;
  348. try {
  349. final int capacity = random.nextInt(1000);
  350. currentList = getRandomList(random, capacity);
  351. testListValue(currentList);
  352. } catch (final ClassCastException e) {
  353. throw new Exception("testListValue(" + currentList + ") failed", e);
  354. }
  355. }
  356. }
  357. private final void testListValue(final List<Object> value) {
  358. final ErlangValue eVal = new ErlangValue(value);
  359. final OtpErlangObject[] valueOtp = new OtpErlangObject[value.size()];
  360. int i = 0;
  361. for (final Object value_i : value) {
  362. OtpErlangObject valueOtp_i;
  363. if (value_i instanceof Boolean) {
  364. valueOtp_i = new OtpErlangBoolean((Boolean) value_i);
  365. } else if (value_i instanceof Integer) {
  366. valueOtp_i = new OtpErlangInt((Integer) value_i);
  367. } else if (value_i instanceof Long) {
  368. valueOtp_i = new OtpErlangLong((Long) value_i);
  369. } else if (value_i instanceof BigInteger) {
  370. valueOtp_i = new OtpErlangLong((BigInteger) value_i);
  371. } else if (value_i instanceof Double) {
  372. valueOtp_i = new OtpErlangDouble((Double) value_i);
  373. } else if (value_i instanceof String) {
  374. valueOtp_i = new OtpErlangString((String) value_i);
  375. } else {
  376. fail("unsupported (expected) value: " + value_i);
  377. return; // so the Java-compiler does not complain about the following assignment
  378. }
  379. valueOtp[i++] = valueOtp_i;
  380. }
  381. final ErlangValue eValOtp = new ErlangValue(new OtpErlangList(valueOtp));
  382. compareList(value, eVal.listValue());
  383. compareList(value, eValOtp.listValue());
  384. assertTrue(eVal.equals(eValOtp));
  385. assertTrue(eValOtp.equals(eVal));
  386. }
  387. private static void compareList(final List<Object> expected, final List<ErlangValue> actual) {
  388. assertEquals(expected.size(), actual.size());
  389. for (int j = 0; j < actual.size(); ++j) {
  390. final Object expected_j = expected.get(j);
  391. final ErlangValue actual_j = actual.get(j);
  392. if (expected_j instanceof Boolean) {
  393. assertEquals(expected_j, actual_j.boolValue());
  394. } else if (expected_j instanceof Integer) {
  395. assertEquals(expected_j, new Integer(actual_j.intValue()));
  396. } else if (expected_j instanceof Long) {
  397. assertEquals(expected_j, new Long(actual_j.longValue()));
  398. } else if (expected_j instanceof BigInteger) {
  399. assertEquals(expected_j, actual_j.bigIntValue());
  400. } else if (expected_j instanceof Double) {
  401. assertEquals(expected_j, new Double(actual_j.doubleValue()));
  402. } else if (expected_j instanceof String) {
  403. assertEquals(expected_j, actual_j.stringValue());
  404. } else {
  405. fail("unsupported (expected) value: " + expected_j);
  406. }
  407. }
  408. }
  409. /**
  410. * Test method for {@link de.zib.scalaris.ErlangValue#longListValue()}.
  411. *
  412. * @throws Exception if a test with a random list of longs failed
  413. */
  414. @Test
  415. public final void testLongListValue() throws Exception {
  416. final Random random = new Random();
  417. for (int i = 0; i < 10000; ++i) {
  418. List<Long> currentList = null;
  419. try {
  420. final int capacity = random.nextInt(1000);
  421. currentList = new ArrayList<Long>(capacity);
  422. for (int j = 0; j < capacity; ++j) {
  423. currentList.add(random.nextLong());
  424. }
  425. testLongListValue(currentList);
  426. } catch (final ClassCastException e) {
  427. throw new Exception("testLongListValue(" + currentList + ") failed", e);
  428. }
  429. }
  430. }
  431. private final void testLongListValue(final List<Long> value) {
  432. final ErlangValue eVal = new ErlangValue(value);
  433. final OtpErlangLong[] valueOtp = new OtpErlangLong[value.size()];
  434. int i = 0;
  435. for (final Long long2 : value) {
  436. final Long long1 = long2;
  437. valueOtp[i++] = new OtpErlangLong(long1);
  438. }
  439. final ErlangValue eValOtp = new ErlangValue(new OtpErlangList(valueOtp));
  440. assertEquals(value, eVal.longListValue());
  441. assertEquals(value, eValOtp.longListValue());
  442. assertTrue(eVal.equals(eValOtp));
  443. assertTrue(eValOtp.equals(eVal));
  444. }
  445. /**
  446. * Test method for {@link de.zib.scalaris.ErlangValue#doubleListValue()}.
  447. *
  448. * @throws Exception if a test with a random list of doubles failed
  449. */
  450. @Test
  451. public final void testDoubleListValue() throws Exception {
  452. final Random random = new Random();
  453. for (int i = 0; i < 10000; ++i) {
  454. List<Double> currentList = null;
  455. try {
  456. final int capacity = random.nextInt(1000);
  457. currentList = new ArrayList<Double>(capacity);
  458. for (int j = 0; j < capacity; ++j) {
  459. currentList.add(random.nextDouble());
  460. }
  461. testDoubleListValue(currentList);
  462. } catch (final ClassCastException e) {
  463. throw new Exception("testDoubleListValue(" + currentList + ") failed", e);
  464. }
  465. }
  466. }
  467. private final void testDoubleListValue(final List<Double> value) {
  468. final ErlangValue eVal = new ErlangValue(value);
  469. final OtpErlangDouble[] valueOtp = new OtpErlangDouble[value.size()];
  470. int i = 0;
  471. for (final Double double2 : value) {
  472. final Double double1 = double2;
  473. valueOtp[i++] = new OtpErlangDouble(double1);
  474. }
  475. final ErlangValue eValOtp = new ErlangValue(new OtpErlangList(valueOtp));
  476. assertEquals(value, eVal.doubleListValue());
  477. assertEquals(value, eValOtp.doubleListValue());
  478. assertTrue(eVal.equals(eValOtp));
  479. assertTrue(eValOtp.equals(eVal));
  480. }
  481. /**
  482. * Test method for {@link de.zib.scalaris.ErlangValue#stringListValue()}.
  483. *
  484. * @throws Exception if a test with a random list of strings failed
  485. */
  486. @Test
  487. public final void testStringListValue() throws Exception {
  488. final Random random = new Random();
  489. for (int i = 0; i < 10000; ++i) {
  490. List<String> currentList = null;
  491. try {
  492. final int capacity = random.nextInt(10);
  493. currentList = new ArrayList<String>(capacity);
  494. for (int j = 0; j < capacity; ++j) {
  495. currentList.add(getRandomString(random, random.nextInt(1000), false));
  496. }
  497. testStringListValue(currentList);
  498. } catch (final ClassCastException e) {
  499. throw new Exception("testStringListValue(" + currentList + ") failed", e);
  500. }
  501. }
  502. }
  503. private final void testStringListValue(final List<String> value) {
  504. final ErlangValue eVal = new ErlangValue(value);
  505. final OtpErlangString[] valueOtp = new OtpErlangString[value.size()];
  506. int i = 0;
  507. for (final String string : value) {
  508. final String string1 = string;
  509. valueOtp[i++] = new OtpErlangString(string1);
  510. }
  511. final ErlangValue eValOtp = new ErlangValue(new OtpErlangList(valueOtp));
  512. assertEquals(value, eVal.stringListValue());
  513. assertEquals(value, eValOtp.stringListValue());
  514. assertTrue(eVal.equals(eValOtp));
  515. assertTrue(eValOtp.equals(eVal));
  516. }
  517. /**
  518. * Test method for {@link de.zib.scalaris.ErlangValue#binaryListValue()}.
  519. *
  520. * @throws Exception if a test with a random list of binaries failed
  521. */
  522. @Test
  523. public final void testBinaryListValue() throws Exception {
  524. final Random random = new Random();
  525. for (int i = 0; i < 1000; ++i) {
  526. try {
  527. final int capacity = random.nextInt(10);
  528. final List<byte[]> currentList = new ArrayList<byte[]>(capacity);
  529. for (int j = 0; j < capacity; ++j) {
  530. currentList.add(getRandomBytes(random, random.nextInt(1000)));
  531. }
  532. testBinaryListValue(currentList);
  533. } catch (final ClassCastException e) {
  534. // do not print generated bytes (probably not useful)
  535. throw new Exception("testBinaryListValue(...) failed", e);
  536. }
  537. }
  538. }
  539. private final void testBinaryListValue(final List<byte[]> value) {
  540. final ErlangValue eVal = new ErlangValue(value);
  541. final OtpErlangBinary[] valueOtp = new OtpErlangBinary[value.size()];
  542. int i = 0;
  543. for (final byte[] b : value) {
  544. final byte[] binary1 = b;
  545. valueOtp[i++] = new OtpErlangBinary(binary1);
  546. }
  547. final ErlangValue eValOtp = new ErlangValue(new OtpErlangList(valueOtp));
  548. final List<byte[]> actual = eVal.binaryListValue();
  549. final List<byte[]> actualOtp = eValOtp.binaryListValue();
  550. assertEquals(value.size(), actual.size());
  551. assertEquals(value.size(), actualOtp.size());
  552. for (int j = 0; j < actual.size(); ++j) {
  553. assertArrayEquals(value.get(j), actual.get(j));
  554. assertArrayEquals(value.get(j), actualOtp.get(j));
  555. }
  556. assertTrue(eVal.equals(eValOtp));
  557. assertTrue(eValOtp.equals(eVal));
  558. }
  559. /**
  560. * Test method for {@link de.zib.scalaris.ErlangValue#jsonValue()}.
  561. *
  562. * @throws Exception
  563. * if a test with a random list of mixed objects (bool, int,
  564. * long, BigInteger, double, String) failed
  565. */
  566. @Test
  567. public final void testJsonValue() throws Exception {
  568. final Random random = new Random();
  569. for (int i = 0; i < 10000; ++i) {
  570. Map<String, Object> currentMap = null;
  571. try {
  572. currentMap = getRandomMapRecursive(random, random.nextInt(10), 3, false);
  573. testJsonValue(currentMap);
  574. } catch (final ClassCastException e) {
  575. throw new Exception("testJsonValue(" + currentMap + ") failed", e);
  576. }
  577. }
  578. }
  579. private final void testJsonValue(final Map<String, Object> value) {
  580. final ErlangValue eVal = new ErlangValue(value);
  581. final Map<String, Object> actual = eVal.jsonValue();
  582. compareMap(value, actual);
  583. final ErlangValue eVal2 = new ErlangValue(value);
  584. assertTrue(eVal.equals(eVal2));
  585. assertTrue(eVal2.equals(eVal));
  586. }
  587. private final void compareMap(final Map<String, Object> expected, final Map<String, Object> actual) {
  588. assertEquals(expected.size(), actual.size());
  589. for (final Entry<String, Object> entry : expected.entrySet()) {
  590. final String expected_key_i = entry.getKey();
  591. final Object expected_value_i = entry.getValue();
  592. assertTrue(actual.containsKey(expected_key_i));
  593. final Object actual_value_i = actual.get(expected_key_i);
  594. assertEquals(expected_value_i, actual_value_i);
  595. }
  596. }
  597. private static class JSONBeanTest1 {
  598. private boolean a = true;
  599. private int b = 0;
  600. private long c = 0;
  601. private BigInteger d = new BigInteger("0");
  602. private double e = 0.0;
  603. private String f = "";
  604. public JSONBeanTest1() {}
  605. public boolean getA() { return a; }
  606. public int getB() { return b; }
  607. public long getC() { return c; }
  608. public BigInteger getD() { return d; }
  609. public double getE() { return e; }
  610. public String getF() { return f; }
  611. public void setA(final boolean a_) { this.a = a_; }
  612. public void setB(final int b_) { this.b = b_; }
  613. public void setC(final long c_) { this.c = c_; }
  614. public void setD(final BigInteger d_) { this.d = d_; }
  615. public void setE(final double e_) { this.e = e_; }
  616. public void setF(final String f_) { this.f = f_; }
  617. }
  618. /**
  619. * Test method for {@link de.zib.scalaris.ErlangValue#jsonValue(Class)}
  620. * writing a {@link Map} and reading a {@link JSONBeanTest1}.
  621. *
  622. * @throws Exception
  623. * if a test with a random list of mixed objects (bool, int,
  624. * long, BigInteger, double, String) failed
  625. */
  626. @Test
  627. public final void testJsonValueBean1a() throws Exception {
  628. final Random random = new Random();
  629. for (int i = 0; i < 5000; ++i) {
  630. final Map<String, Object> map = new LinkedHashMap<String, Object>(6);
  631. map.put("a", random.nextBoolean());
  632. map.put("b", random.nextInt());
  633. map.put("c", random.nextLong());
  634. map.put("d", getRandomBigInt(random));
  635. map.put("e", random.nextDouble());
  636. map.put("f", getRandomString(random, random.nextInt(100), false));
  637. final ErlangValue value = new ErlangValue(map);
  638. final JSONBeanTest1 actual = value.jsonValue(JSONBeanTest1.class);
  639. assertEquals(map.get("a"), actual.getA());
  640. assertEquals(map.get("b"), actual.getB());
  641. assertEquals(map.get("c"), actual.getC());
  642. assertEquals(map.get("d"), actual.getD());
  643. assertEquals(map.get("e"), actual.getE());
  644. assertEquals(map.get("f"), actual.getF());
  645. final ErlangValue value2 = new ErlangValue(actual);
  646. assertTrue(value.equals(value2));
  647. assertTrue(value2.equals(value));
  648. }
  649. }
  650. /**
  651. * Test method for {@link de.zib.scalaris.ErlangValue#jsonValue(Class)}
  652. * writing a {@link JSONBeanTest1} and reading a {@link JSONBeanTest1}.
  653. *
  654. * @throws Exception
  655. * if a test with a random list of mixed objects (bool, int,
  656. * long, BigInteger, double, String) failed
  657. */
  658. @Test
  659. public final void testJsonValueBean1b() throws Exception {
  660. final Random random = new Random();
  661. for (int i = 0; i < 5000; ++i) {
  662. final JSONBeanTest1 bean1 = new JSONBeanTest1();
  663. bean1.setA(random.nextBoolean());
  664. bean1.setB(random.nextInt());
  665. bean1.setC(random.nextLong());
  666. bean1.setD(getRandomBigInt(random));
  667. bean1.setE(random.nextDouble());
  668. bean1.setF(getRandomString(random, random.nextInt(100), false));
  669. final ErlangValue value = new ErlangValue(bean1);
  670. final JSONBeanTest1 actual = value.jsonValue(JSONBeanTest1.class);
  671. assertEquals(bean1.getA(), actual.getA());
  672. assertEquals(bean1.getB(), actual.getB());
  673. assertEquals(bean1.getC(), actual.getC());
  674. assertEquals(bean1.getD(), actual.getD());
  675. assertEquals(bean1.getE(), actual.getE(), 0.0);
  676. assertEquals(bean1.getF(), actual.getF());
  677. final ErlangValue value2 = new ErlangValue(actual);
  678. assertTrue(value.equals(value2));
  679. assertTrue(value2.equals(value));
  680. }
  681. }
  682. private static class JSONBeanTest2 {
  683. private boolean a2 = true;
  684. private int b2 = 0;
  685. private long c2 = 0;
  686. private BigInteger d2 = new BigInteger("0");
  687. private double e2 = 0.0;
  688. private String f2 = "";
  689. private List<Object> g2 = new ArrayList<Object>();
  690. private JSONBeanTest1 h2 = new JSONBeanTest1();
  691. private Map<String, Object> i2 = new LinkedHashMap<String, Object>();
  692. public JSONBeanTest2() {}
  693. public boolean getA2() { return a2; }
  694. public int getB2() { return b2; }
  695. public long getC2() { return c2; }
  696. public BigInteger getD2() { return d2; }
  697. public double getE2() { return e2; }
  698. public String getF2() { return f2; }
  699. public List<Object> getG2() { return g2; }
  700. public JSONBeanTest1 getH2() { return h2; }
  701. public Map<String, Object> getI2() { return i2; }
  702. public void setA2(final boolean a_) { this.a2 = a_; }
  703. public void setB2(final int b_) { this.b2 = b_; }
  704. public void setC2(final long c_) { this.c2 = c_; }
  705. public void setD2(final BigInteger d_) { this.d2 = d_; }
  706. public void setE2(final double e_) { this.e2 = e_; }
  707. public void setF2(final String f_) { this.f2 = f_; }
  708. public void setG2(final List<Object> g_) { this.g2 = g_; }
  709. public void setH2(final JSONBeanTest1 h_) { this.h2 = h_; }
  710. public void setI2(final Map<String, Object> i_) { this.i2 = i_; }
  711. }
  712. /**
  713. * Test method for {@link de.zib.scalaris.ErlangValue#jsonValue(Class)}
  714. * writing a {@link Map} and reading a {@link JSONBeanTest2}.
  715. *
  716. * @throws Exception
  717. * if a test with a random list of mixed objects (bool, int,
  718. * long, BigInteger, double, String) failed
  719. */
  720. @Test
  721. public final void testJsonValueBean2a() throws Exception {
  722. final Random random = new Random();
  723. for (int i = 0; i < 10000; ++i) {
  724. final Map<String, Object> map = new LinkedHashMap<String, Object>(7);
  725. map.put("a2", random.nextBoolean());
  726. map.put("b2", random.nextInt());
  727. map.put("c2", random.nextLong());
  728. map.put("d2", getRandomBigInt(random));
  729. map.put("e2", random.nextDouble());
  730. map.put("f2", getRandomString(random, random.nextInt(100), false));
  731. map.put("g2", getRandomList(random, 100));
  732. final Map<String, Object> bean1 = new LinkedHashMap<String, Object>(6);
  733. bean1.put("a", random.nextBoolean());
  734. bean1.put("b", random.nextInt());
  735. bean1.put("c", random.nextLong());
  736. bean1.put("d", getRandomBigInt(random));
  737. bean1.put("e", random.nextDouble());
  738. bean1.put("f", getRandomString(random, 10, false));
  739. map.put("h2", bean1);
  740. final Map<String, Object> map2 = getRandomMapRecursive(random, 10, 3, true);
  741. map.put("i2", map2);
  742. final ErlangValue value = new ErlangValue(map);
  743. final JSONBeanTest2 actual = value.jsonValue(JSONBeanTest2.class);
  744. assertEquals(map.get("a2"), actual.getA2());
  745. assertEquals(map.get("b2"), actual.getB2());
  746. assertEquals(map.get("c2"), actual.getC2());
  747. assertEquals(map.get("d2"), actual.getD2());
  748. assertEquals(map.get("e2"), actual.getE2());
  749. assertEquals(map.get("f2"), actual.getF2());
  750. assertEquals(map.get("g2"), actual.getG2());
  751. final JSONBeanTest1 bean1_act = actual.getH2();
  752. assertEquals(bean1.get("a"), bean1_act.getA());
  753. assertEquals(bean1.get("b"), bean1_act.getB());
  754. assertEquals(bean1.get("c"), bean1_act.getC());
  755. assertEquals(bean1.get("d"), bean1_act.getD());
  756. assertEquals(bean1.get("e"), bean1_act.getE());
  757. assertEquals(bean1.get("f"), bean1_act.getF());
  758. compareMap(map2, actual.getI2());
  759. final ErlangValue value2 = new ErlangValue(actual);
  760. assertTrue(value.equals(value2));
  761. assertTrue(value2.equals(value));
  762. }
  763. }
  764. /**
  765. * Test method for {@link de.zib.scalaris.ErlangValue#jsonValue(Class)}
  766. * writing a {@link JSONBeanTest2} and reading a {@link JSONBeanTest2}.
  767. *
  768. * @throws Exception
  769. * if a test with a random list of mixed objects (bool, int,
  770. * long, BigInteger, double, String) failed
  771. */
  772. @Test
  773. public final void testJsonValueBean2b() throws Exception {
  774. final Random random = new Random();
  775. for (int i = 0; i < 10000; ++i) {
  776. final JSONBeanTest2 bean2 = new JSONBeanTest2();
  777. bean2.setA2(random.nextBoolean());
  778. bean2.setB2(random.nextInt());
  779. bean2.setC2(random.nextLong());
  780. bean2.setD2(getRandomBigInt(random));
  781. bean2.setE2(random.nextDouble());
  782. bean2.setF2(getRandomString(random, random.nextInt(100), false));
  783. bean2.setG2(getRandomList(random, 100));
  784. final JSONBeanTest1 bean1 = new JSONBeanTest1();
  785. bean1.setA(random.nextBoolean());
  786. bean1.setB(random.nextInt());
  787. bean1.setC(random.nextLong());
  788. bean1.setD(getRandomBigInt(random));
  789. bean1.setE(random.nextDouble());
  790. bean1.setF(getRandomString(random, 10, false));
  791. bean2.setH2(bean1);
  792. final Map<String, Object> map2 = getRandomMapRecursive(random, 10, 3, true);
  793. bean2.setI2(map2);
  794. final ErlangValue value = new ErlangValue(bean2);
  795. final JSONBeanTest2 actual = value.jsonValue(JSONBeanTest2.class);
  796. assertEquals(bean2.getA2(), actual.getA2());
  797. assertEquals(bean2.getB2(), actual.getB2());
  798. assertEquals(bean2.getC2(), actual.getC2());
  799. assertEquals(bean2.getD2(), actual.getD2());
  800. assertEquals(bean2.getE2(), actual.getE2(), 0.0);
  801. assertEquals(bean2.getF2(), actual.getF2());
  802. assertEquals(bean2.getG2(), actual.getG2());
  803. final JSONBeanTest1 bean1_act = actual.getH2();
  804. assertEquals(bean1.getA(), bean1_act.getA());
  805. assertEquals(bean1.getB(), bean1_act.getB());
  806. assertEquals(bean1.getC(), bean1_act.getC());
  807. assertEquals(bean1.getD(), bean1_act.getD());
  808. assertEquals(bean1.getE(), bean1_act.getE(), 0.0);
  809. assertEquals(bean1.getF(), bean1_act.getF());
  810. compareMap(map2, actual.getI2());
  811. final ErlangValue value2 = new ErlangValue(actual);
  812. assertTrue(value.equals(value2));
  813. assertTrue(value2.equals(value));
  814. }
  815. }
  816. private static class JSONBeanTest3 {
  817. private List<JSONBeanTest1> a3 = new ArrayList<JSONBeanTest1>();
  818. private Map<String, JSONBeanTest1> b3 = new LinkedHashMap<String, JSONBeanTest1>();
  819. public JSONBeanTest3() {}
  820. public List<JSONBeanTest1> getA3() { return a3; }
  821. public Map<String, JSONBeanTest1> getB3() { return b3; }
  822. public void setA3(final List<JSONBeanTest1> a_) { this.a3 = a_; }
  823. public void setB3(final Map<String, JSONBeanTest1> b_) { this.b3 = b_; }
  824. }
  825. /**
  826. * Test method for {@link de.zib.scalaris.ErlangValue#jsonValue(Class)}
  827. * writing a {@link Map} and reading a {@link JSONBeanTest3}.
  828. *
  829. * @throws Exception
  830. * if a test with a random list of mixed objects (bool, int,
  831. * long, BigInteger, double, String) failed
  832. */
  833. @Test
  834. public final void testJsonValueBean3a() throws Exception {
  835. final Random random = new Random();
  836. for (int i = 0; i < 10000; ++i) {
  837. final Map<String, Object> map = new LinkedHashMap<String, Object>(7);
  838. final Map<String, Object> bean1a = new LinkedHashMap<String, Object>(6);
  839. bean1a.put("a", random.nextBoolean());
  840. bean1a.put("b", random.nextInt());
  841. bean1a.put("c", random.nextLong());
  842. bean1a.put("d", getRandomBigInt(random));
  843. bean1a.put("e", random.nextDouble());
  844. bean1a.put("f", getRandomString(random, 10, false));
  845. final Map<String, Object> bean1b = new LinkedHashMap<String, Object>(6);
  846. bean1b.put("a", random.nextBoolean());
  847. bean1b.put("b", random.nextInt());
  848. bean1b.put("c", random.nextLong());
  849. bean1b.put("d", getRandomBigInt(random));
  850. bean1b.put("e", random.nextDouble());
  851. bean1b.put("f", getRandomString(random, 10, false));
  852. final List<Map<String, Object>> list1 = new LinkedList<Map<String,Object>>();
  853. list1.add(bean1a);
  854. list1.add(bean1b);
  855. map.put("a3", list1);
  856. final Map<String, Map<String, Object>> map1 = new LinkedHashMap<String, Map<String,Object>>();
  857. map1.put("a4", bean1a);
  858. map1.put("b4", bean1b);
  859. map.put("b3", map1);
  860. final ErlangValue value = new ErlangValue(map);
  861. final JSONBeanTest3 actual = value.jsonValue(JSONBeanTest3.class);
  862. final List<JSONBeanTest1> actual_a3 = actual.getA3();
  863. assertEquals(list1.size(), actual_a3.size());
  864. for (int j = 0; j < list1.size(); ++j) {
  865. assertEquals(list1.get(j).get("a"), actual_a3.get(j).getA());
  866. assertEquals(list1.get(j).get("b"), actual_a3.get(j).getB());
  867. assertEquals(list1.get(j).get("c"), actual_a3.get(j).getC());
  868. assertEquals(list1.get(j).get("d"), actual_a3.get(j).getD());
  869. assertEquals(list1.get(j).get("e"), actual_a3.get(j).getE());
  870. assertEquals(list1.get(j).get("f"), actual_a3.get(j).getF());
  871. }
  872. final Map<String, JSONBeanTest1> actual_b3 = actual.getB3();
  873. assertEquals(map1.size(), actual_b3.size());
  874. for (final String key : map1.keySet()) {
  875. assertEquals(map1.get(key).get("a"), actual_b3.get(key).getA());
  876. assertEquals(map1.get(key).get("b"), actual_b3.get(key).getB());
  877. assertEquals(map1.get(key).get("c"), actual_b3.get(key).getC());
  878. assertEquals(map1.get(key).get("d"), actual_b3.get(key).getD());
  879. assertEquals(map1.get(key).get("e"), actual_b3.get(key).getE());
  880. assertEquals(map1.get(key).get("f"), actual_b3.get(key).getF());
  881. }
  882. final ErlangValue value2 = new ErlangValue(actual);
  883. assertTrue(value.equals(value2));
  884. assertTrue(value2.equals(value));
  885. }
  886. }
  887. /**
  888. * Test method for {@link de.zib.scalaris.ErlangValue#jsonValue(Class)}
  889. * writing a {@link JSONBeanTest2} and reading a {@link JSONBeanTest2}.
  890. *
  891. * @throws Exception
  892. * if a test with a random list of mixed objects (bool, int,
  893. * long, BigInteger, double, String) failed
  894. */
  895. @Test
  896. public final void testJsonValueBean3b() throws Exception {
  897. final Random random = new Random();
  898. for (int i = 0; i < 10000; ++i) {
  899. final JSONBeanTest3 bean3 = new JSONBeanTest3();
  900. final JSONBeanTest1 bean1a = new JSONBeanTest1();
  901. bean1a.setA(random.nextBoolean());
  902. bean1a.setB(random.nextInt());
  903. bean1a.setC(random.nextLong());
  904. bean1a.setD(getRandomBigInt(random));
  905. bean1a.setE(random.nextDouble());
  906. bean1a.setF(getRandomString(random, 10, false));
  907. final JSONBeanTest1 bean1b = new JSONBeanTest1();
  908. bean1b.setA(random.nextBoolean());
  909. bean1b.setB(random.nextInt());
  910. bean1b.setC(random.nextLong());
  911. bean1b.setD(getRandomBigInt(random));
  912. bean1b.setE(random.nextDouble());
  913. bean1b.setF(getRandomString(random, 10, false));
  914. final List<JSONBeanTest1> list1 = new LinkedList<JSONBeanTest1>();
  915. list1.add(bean1a);
  916. list1.add(bean1b);
  917. bean3.setA3(list1);
  918. final Map<String, JSONBeanTest1> map1 = new LinkedHashMap<String, JSONBeanTest1>();
  919. map1.put("a4", bean1a);
  920. map1.put("b4", bean1b);
  921. bean3.setB3(map1);
  922. final ErlangValue value = new ErlangValue(bean3);
  923. final JSONBeanTest3 actual = value.jsonValue(JSONBeanTest3.class);
  924. final List<JSONBeanTest1> actual_a3 = actual.getA3();
  925. assertEquals(list1.size(), actual_a3.size());
  926. for (int j = 0; j < list1.size(); ++j) {
  927. assertEquals(list1.get(j).getA(), actual_a3.get(j).getA());
  928. assertEquals(list1.get(j).getB(), actual_a3.get(j).getB());
  929. assertEquals(list1.get(j).getC(), actual_a3.get(j).getC());
  930. assertEquals(list1.get(j).getD(), actual_a3.get(j).getD());
  931. assertEquals(list1.get(j).getE(), actual_a3.get(j).getE(), 0.0);
  932. assertEquals(list1.get(j).getF(), actual_a3.get(j).getF());
  933. }
  934. final Map<String, JSONBeanTest1> actual_b3 = actual.getB3();
  935. assertEquals(map1.size(), actual_b3.size());
  936. for (final String key : map1.keySet()) {
  937. assertEquals(map1.get(key).getA(), actual_b3.get(key).getA());
  938. assertEquals(map1.get(key).getB(), actual_b3.get(key).getB());
  939. assertEquals(map1.get(key).getC(), actual_b3.get(key).getC());
  940. assertEquals(map1.get(key).getD(), actual_b3.get(key).getD());
  941. assertEquals(map1.get(key).getE(), actual_b3.get(key).getE(), 0.0);
  942. assertEquals(map1.get(key).getF(), actual_b3.get(key).getF());
  943. }
  944. final ErlangValue value2 = new ErlangValue(actual);
  945. assertTrue(value.equals(value2));
  946. assertTrue(value2.equals(value));
  947. }
  948. }
  949. }