PageRenderTime 60ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/jre_emul/apache_harmony/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/TreeMapTest.java

https://bitbucket.org/AKoz/j2objc
Java | 705 lines | 472 code | 87 blank | 146 comment | 50 complexity | 46511eba4c15a362d2f3c00580d5839d MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.harmony.luni.tests.java.util;
  18. import tests.support.Support_MapTest2;
  19. import tests.support.Support_UnmodifiableCollectionTest;
  20. import java.io.Serializable;
  21. import java.text.CollationKey;
  22. import java.text.Collator;
  23. import java.util.AbstractMap;
  24. import java.util.Collection;
  25. import java.util.Comparator;
  26. import java.util.HashMap;
  27. import java.util.Iterator;
  28. import java.util.Map;
  29. import java.util.Set;
  30. import java.util.SortedMap;
  31. import java.util.TreeMap;
  32. public class TreeMapTest extends junit.framework.TestCase {
  33. public static class ReversedComparator implements Comparator {
  34. public int compare(Object o1, Object o2) {
  35. return -(((Comparable) o1).compareTo(o2));
  36. }
  37. public boolean equals(Object o1, Object o2) {
  38. return (((Comparable) o1).compareTo(o2)) == 0;
  39. }
  40. }
  41. // Regression for Harmony-1026
  42. public static class MockComparator<T extends Comparable<T>> implements
  43. Comparator<T>, Serializable {
  44. public int compare(T o1, T o2) {
  45. if (o1 == o2) {
  46. return 0;
  47. }
  48. if (null == o1 || null == o2) {
  49. return -1;
  50. }
  51. T c1 = o1;
  52. T c2 = o2;
  53. return c1.compareTo(c2);
  54. }
  55. }
  56. // Regression for Harmony-1161
  57. class MockComparatorNullTolerable implements Comparator<String> {
  58. public int compare(String o1, String o2) {
  59. if (o1 == o2) {
  60. return 0;
  61. }
  62. if (null == o1) {
  63. return -1;
  64. }
  65. if (null == o2) { // comparator should be symmetric
  66. return 1;
  67. }
  68. return o1.compareTo(o2);
  69. }
  70. }
  71. TreeMap tm;
  72. Object objArray[] = new Object[1000];
  73. /**
  74. * @tests java.util.TreeMap#TreeMap()
  75. */
  76. public void test_Constructor() {
  77. // Test for method java.util.TreeMap()
  78. new Support_MapTest2(new TreeMap()).runTest();
  79. assertTrue("New treeMap non-empty", new TreeMap().isEmpty());
  80. }
  81. /**
  82. * @tests java.util.TreeMap#TreeMap(java.util.Comparator)
  83. */
  84. public void test_ConstructorLjava_util_Comparator() {
  85. // Test for method java.util.TreeMap(java.util.Comparator)
  86. Comparator comp = new ReversedComparator();
  87. TreeMap reversedTreeMap = new TreeMap(comp);
  88. assertTrue("TreeMap answered incorrect comparator", reversedTreeMap
  89. .comparator() == comp);
  90. reversedTreeMap.put(new Integer(1).toString(), new Integer(1));
  91. reversedTreeMap.put(new Integer(2).toString(), new Integer(2));
  92. assertTrue("TreeMap does not use comparator (firstKey was incorrect)",
  93. reversedTreeMap.firstKey().equals(new Integer(2).toString()));
  94. assertTrue("TreeMap does not use comparator (lastKey was incorrect)",
  95. reversedTreeMap.lastKey().equals(new Integer(1).toString()));
  96. }
  97. /**
  98. * @tests java.util.TreeMap#TreeMap(java.util.Map)
  99. */
  100. public void test_ConstructorLjava_util_Map() {
  101. // Test for method java.util.TreeMap(java.util.Map)
  102. TreeMap myTreeMap = new TreeMap(new HashMap(tm));
  103. assertTrue("Map is incorrect size", myTreeMap.size() == objArray.length);
  104. for (Object element : objArray) {
  105. assertTrue("Map has incorrect mappings", myTreeMap.get(
  106. element.toString()).equals(element));
  107. }
  108. }
  109. /**
  110. * @tests java.util.TreeMap#TreeMap(java.util.SortedMap)
  111. */
  112. public void test_ConstructorLjava_util_SortedMap() {
  113. // Test for method java.util.TreeMap(java.util.SortedMap)
  114. Comparator comp = new ReversedComparator();
  115. TreeMap reversedTreeMap = new TreeMap(comp);
  116. reversedTreeMap.put(new Integer(1).toString(), new Integer(1));
  117. reversedTreeMap.put(new Integer(2).toString(), new Integer(2));
  118. TreeMap anotherTreeMap = new TreeMap(reversedTreeMap);
  119. assertTrue("New tree map does not answer correct comparator",
  120. anotherTreeMap.comparator() == comp);
  121. assertTrue("TreeMap does not use comparator (firstKey was incorrect)",
  122. anotherTreeMap.firstKey().equals(new Integer(2).toString()));
  123. assertTrue("TreeMap does not use comparator (lastKey was incorrect)",
  124. anotherTreeMap.lastKey().equals(new Integer(1).toString()));
  125. }
  126. /**
  127. * @tests java.util.TreeMap#clear()
  128. */
  129. public void test_clear() {
  130. // Test for method void java.util.TreeMap.clear()
  131. tm.clear();
  132. assertEquals("Cleared map returned non-zero size", 0, tm.size());
  133. }
  134. /**
  135. * @tests java.util.TreeMap#clone()
  136. */
  137. public void test_clone() {
  138. // Test for method java.lang.Object java.util.TreeMap.clone()
  139. TreeMap clonedMap = (TreeMap) tm.clone();
  140. assertTrue("Cloned map does not equal the original map", clonedMap
  141. .equals(tm));
  142. assertTrue("Cloned map is the same reference as the original map",
  143. clonedMap != tm);
  144. for (Object element : objArray) {
  145. assertTrue("Cloned map contains incorrect elements", clonedMap
  146. .get(element.toString()) == tm.get(element.toString()));
  147. }
  148. TreeMap map = new TreeMap();
  149. map.put("key", "value");
  150. // get the keySet() and values() on the original Map
  151. Set keys = map.keySet();
  152. Collection values = map.values();
  153. assertEquals("values() does not work", "value", values.iterator()
  154. .next());
  155. assertEquals("keySet() does not work", "key", keys.iterator().next());
  156. AbstractMap map2 = (AbstractMap) map.clone();
  157. map2.put("key", "value2");
  158. Collection values2 = map2.values();
  159. assertTrue("values() is identical", values2 != values);
  160. // values() and keySet() on the cloned() map should be different
  161. assertEquals("values() was not cloned", "value2", values2.iterator()
  162. .next());
  163. map2.clear();
  164. map2.put("key2", "value3");
  165. Set key2 = map2.keySet();
  166. assertTrue("keySet() is identical", key2 != keys);
  167. assertEquals("keySet() was not cloned", "key2", key2.iterator().next());
  168. }
  169. /**
  170. * @tests java.util.TreeMap#comparator()
  171. */
  172. public void test_comparator() {
  173. // Test for method java.util.Comparator java.util.TreeMap.comparator()\
  174. Comparator comp = new ReversedComparator();
  175. TreeMap reversedTreeMap = new TreeMap(comp);
  176. assertTrue("TreeMap answered incorrect comparator", reversedTreeMap
  177. .comparator() == comp);
  178. reversedTreeMap.put(new Integer(1).toString(), new Integer(1));
  179. reversedTreeMap.put(new Integer(2).toString(), new Integer(2));
  180. assertTrue("TreeMap does not use comparator (firstKey was incorrect)",
  181. reversedTreeMap.firstKey().equals(new Integer(2).toString()));
  182. assertTrue("TreeMap does not use comparator (lastKey was incorrect)",
  183. reversedTreeMap.lastKey().equals(new Integer(1).toString()));
  184. }
  185. /**
  186. * @tests java.util.TreeMap#containsKey(java.lang.Object)
  187. */
  188. public void test_containsKeyLjava_lang_Object() {
  189. // Test for method boolean
  190. // java.util.TreeMap.containsKey(java.lang.Object)
  191. assertTrue("Returned false for valid key", tm.containsKey("95"));
  192. assertTrue("Returned true for invalid key", !tm.containsKey("XXXXX"));
  193. }
  194. /**
  195. * @tests java.util.TreeMap#containsValue(java.lang.Object)
  196. */
  197. public void test_containsValueLjava_lang_Object() {
  198. // Test for method boolean
  199. // java.util.TreeMap.containsValue(java.lang.Object)
  200. assertTrue("Returned false for valid value", tm
  201. .containsValue(objArray[986]));
  202. assertTrue("Returned true for invalid value", !tm
  203. .containsValue(new Object()));
  204. }
  205. /**
  206. * @tests java.util.TreeMap#entrySet()
  207. */
  208. public void test_entrySet() {
  209. // Test for method java.util.Set java.util.TreeMap.entrySet()
  210. Set anEntrySet = tm.entrySet();
  211. Iterator entrySetIterator = anEntrySet.iterator();
  212. assertTrue("EntrySet is incorrect size",
  213. anEntrySet.size() == objArray.length);
  214. Map.Entry entry;
  215. while (entrySetIterator.hasNext()) {
  216. entry = (Map.Entry) entrySetIterator.next();
  217. assertTrue("EntrySet does not contain correct mappings", tm
  218. .get(entry.getKey()) == entry.getValue());
  219. }
  220. }
  221. /**
  222. * @tests java.util.TreeMap#firstKey()
  223. */
  224. public void test_firstKey() {
  225. // Test for method java.lang.Object java.util.TreeMap.firstKey()
  226. assertEquals("Returned incorrect first key", "0", tm.firstKey());
  227. }
  228. /**
  229. * @tests java.util.TreeMap#get(java.lang.Object)
  230. */
  231. public void test_getLjava_lang_Object() {
  232. // Test for method java.lang.Object
  233. // java.util.TreeMap.get(java.lang.Object)
  234. Object o = new Object();
  235. tm.put("Hello", o);
  236. assertTrue("Failed to get mapping", tm.get("Hello") == o);
  237. }
  238. /**
  239. * @tests java.util.TreeMap#headMap(java.lang.Object)
  240. */
  241. public void test_headMapLjava_lang_Object() {
  242. // Test for method java.util.SortedMap
  243. // java.util.TreeMap.headMap(java.lang.Object)
  244. Map head = tm.headMap("100");
  245. assertEquals("Returned map of incorrect size", 3, head.size());
  246. assertTrue("Returned incorrect elements", head.containsKey("0")
  247. && head.containsValue(new Integer("1"))
  248. && head.containsKey("10"));
  249. // Regression for Harmony-1026
  250. TreeMap<Integer, Double> map = new TreeMap<Integer, Double>(
  251. new MockComparator());
  252. map.put(1, 2.1);
  253. map.put(2, 3.1);
  254. map.put(3, 4.5);
  255. map.put(7, 21.3);
  256. map.put(null, null);
  257. SortedMap<Integer, Double> smap = map.headMap(null);
  258. assertEquals(0, smap.size());
  259. Set<Integer> keySet = smap.keySet();
  260. assertEquals(0, keySet.size());
  261. Set<Map.Entry<Integer, Double>> entrySet = smap.entrySet();
  262. assertEquals(0, entrySet.size());
  263. Collection<Double> valueCollection = smap.values();
  264. assertEquals(0, valueCollection.size());
  265. // Regression for Harmony-1066
  266. assertTrue(head instanceof Serializable);
  267. // Regression for ill-behaved collator
  268. Collator c = new Collator() {
  269. @Override
  270. public int compare(String o1, String o2) {
  271. if (o1 == null) {
  272. return 0;
  273. }
  274. return o1.compareTo(o2);
  275. }
  276. @Override
  277. public CollationKey getCollationKey(String string) {
  278. return null;
  279. }
  280. @Override
  281. public int hashCode() {
  282. return 0;
  283. }
  284. };
  285. TreeMap<String, String> treemap = new TreeMap<String, String>(c);
  286. assertEquals(0, treemap.headMap(null).size());
  287. treemap = new TreeMap();
  288. SortedMap<String, String> headMap = treemap.headMap("100");
  289. headMap.headMap("100");
  290. SortedMap<Integer,Integer> intMap,sub;
  291. int size = 16;
  292. intMap = new TreeMap<Integer,Integer>();
  293. for(int i=0; i<size; i++) {
  294. intMap.put(i,i);
  295. }
  296. sub = intMap.headMap(-1);
  297. assertEquals("size should be zero",sub.size(),0);
  298. assertTrue("submap should be empty",sub.isEmpty());
  299. try{
  300. sub.firstKey();
  301. fail("java.util.NoSuchElementException should be thrown");
  302. } catch(java.util.NoSuchElementException e) {
  303. }
  304. try{
  305. sub.lastKey();
  306. fail("java.util.NoSuchElementException should be thrown");
  307. } catch(java.util.NoSuchElementException e) {
  308. }
  309. size = 256;
  310. intMap = new TreeMap<Integer,Integer>();
  311. for(int i=0; i<size; i++) {
  312. intMap.put(i,i);
  313. }
  314. sub = intMap.headMap(-1);
  315. assertEquals("size should be zero",sub.size(),0);
  316. assertTrue("submap should be empty",sub.isEmpty());
  317. try{
  318. sub.firstKey();
  319. fail("java.util.NoSuchElementException should be thrown");
  320. } catch(java.util.NoSuchElementException e) {
  321. }
  322. try{
  323. sub.lastKey();
  324. fail("java.util.NoSuchElementException should be thrown");
  325. } catch(java.util.NoSuchElementException e) {
  326. }
  327. }
  328. /**
  329. * @tests java.util.TreeMap#keySet()
  330. */
  331. public void test_keySet() {
  332. // Test for method java.util.Set java.util.TreeMap.keySet()
  333. Set ks = tm.keySet();
  334. assertTrue("Returned set of incorrect size",
  335. ks.size() == objArray.length);
  336. for (int i = 0; i < tm.size(); i++) {
  337. assertTrue("Returned set is missing keys", ks.contains(new Integer(
  338. i).toString()));
  339. }
  340. }
  341. /**
  342. * @tests java.util.TreeMap#lastKey()
  343. */
  344. public void test_lastKey() {
  345. // Test for method java.lang.Object java.util.TreeMap.lastKey()
  346. assertTrue("Returned incorrect last key", tm.lastKey().equals(
  347. objArray[objArray.length - 1].toString()));
  348. }
  349. /**
  350. * @tests java.util.TreeMap#put(java.lang.Object, java.lang.Object)
  351. */
  352. public void test_putLjava_lang_ObjectLjava_lang_Object() {
  353. // Test for method java.lang.Object
  354. // java.util.TreeMap.put(java.lang.Object, java.lang.Object)
  355. Object o = new Object();
  356. tm.put("Hello", o);
  357. assertTrue("Failed to put mapping", tm.get("Hello") == o);
  358. // regression for Harmony-780
  359. tm = new TreeMap();
  360. assertNull(tm.put(new Object(), new Object()));
  361. try {
  362. tm.put(new Integer(1), new Object());
  363. fail("should throw ClassCastException");
  364. } catch (ClassCastException e) {
  365. // expected
  366. }
  367. tm = new TreeMap();
  368. assertNull(tm.put(new Integer(1), new Object()));
  369. // regression for Harmony-2474
  370. tm = new TreeMap();
  371. tm.remove(o);
  372. }
  373. /**
  374. * @tests java.util.TreeMap#putAll(java.util.Map)
  375. */
  376. public void test_putAllLjava_util_Map() {
  377. // Test for method void java.util.TreeMap.putAll(java.util.Map)
  378. TreeMap x = new TreeMap();
  379. x.putAll(tm);
  380. assertTrue("Map incorrect size after put", x.size() == tm.size());
  381. for (Object element : objArray) {
  382. assertTrue("Failed to put all elements", x.get(element.toString())
  383. .equals(element));
  384. }
  385. }
  386. /**
  387. * @tests java.util.TreeMap#remove(java.lang.Object)
  388. */
  389. public void test_removeLjava_lang_Object() {
  390. // Test for method java.lang.Object
  391. // java.util.TreeMap.remove(java.lang.Object)
  392. tm.remove("990");
  393. assertTrue("Failed to remove mapping", !tm.containsKey("990"));
  394. }
  395. /**
  396. * @tests java.util.TreeMap#size()
  397. */
  398. public void test_size() {
  399. // Test for method int java.util.TreeMap.size()
  400. assertEquals("Returned incorrect size", 1000, tm.size());
  401. }
  402. /**
  403. * @tests java.util.TreeMap#subMap(java.lang.Object, java.lang.Object)
  404. */
  405. public void test_subMapLjava_lang_ObjectLjava_lang_Object() {
  406. // Test for method java.util.SortedMap
  407. // java.util.TreeMap.subMap(java.lang.Object, java.lang.Object)
  408. SortedMap subMap = tm.subMap(objArray[100].toString(), objArray[109]
  409. .toString());
  410. assertEquals("subMap is of incorrect size", 9, subMap.size());
  411. for (int counter = 100; counter < 109; counter++) {
  412. assertTrue("SubMap contains incorrect elements", subMap.get(
  413. objArray[counter].toString()).equals(objArray[counter]));
  414. }
  415. try {
  416. tm.subMap(objArray[9].toString(), objArray[1].toString());
  417. fail("end key less than start key should throw IllegalArgumentException");
  418. } catch (IllegalArgumentException e) {
  419. // Expected
  420. }
  421. // Regression for Harmony-1161
  422. TreeMap<String, String> treeMapWithNull = new TreeMap<String, String>(
  423. new MockComparatorNullTolerable());
  424. treeMapWithNull.put("key1", "value1"); //$NON-NLS-1$ //$NON-NLS-2$
  425. treeMapWithNull.put(null, "value2"); //$NON-NLS-1$
  426. SortedMap<String, String> subMapWithNull = treeMapWithNull.subMap(null,
  427. "key1"); //$NON-NLS-1$
  428. assertEquals("Size of subMap should be 1:", 1, subMapWithNull.size()); //$NON-NLS-1$
  429. // Regression test for typo in lastKey method
  430. SortedMap<String, String> map = new TreeMap<String, String>();
  431. map.put("1", "one"); //$NON-NLS-1$ //$NON-NLS-2$
  432. map.put("2", "two"); //$NON-NLS-1$ //$NON-NLS-2$
  433. map.put("3", "three"); //$NON-NLS-1$ //$NON-NLS-2$
  434. assertEquals("3", map.lastKey());
  435. SortedMap<String, String> sub = map.subMap("1", "3"); //$NON-NLS-1$ //$NON-NLS-2$
  436. assertEquals("2", sub.lastKey()); //$NON-NLS-1$
  437. }
  438. /**
  439. * @tests java.util.TreeMap#subMap(java.lang.Object, java.lang.Object)
  440. */
  441. public void test_subMap_Iterator() {
  442. TreeMap<String, String> map = new TreeMap<String, String>();
  443. String[] keys = { "1", "2", "3" };
  444. String[] values = { "one", "two", "three" };
  445. for (int i = 0; i < keys.length; i++) {
  446. map.put(keys[i], values[i]);
  447. }
  448. assertEquals(3, map.size());
  449. Map subMap = map.subMap("", "test");
  450. assertEquals(3, subMap.size());
  451. Set entrySet = subMap.entrySet();
  452. Iterator iter = entrySet.iterator();
  453. int size = 0;
  454. while (iter.hasNext()) {
  455. Map.Entry<String, String> entry = (Map.Entry<String, String>) iter
  456. .next();
  457. assertTrue(map.containsKey(entry.getKey()));
  458. assertTrue(map.containsValue(entry.getValue()));
  459. size++;
  460. }
  461. assertEquals(map.size(), size);
  462. Set<String> keySet = subMap.keySet();
  463. iter = keySet.iterator();
  464. size = 0;
  465. while (iter.hasNext()) {
  466. String key = (String) iter.next();
  467. assertTrue(map.containsKey(key));
  468. size++;
  469. }
  470. assertEquals(map.size(), size);
  471. }
  472. /**
  473. * @tests java.util.TreeMap#tailMap(java.lang.Object)
  474. */
  475. public void test_tailMapLjava_lang_Object() {
  476. // Test for method java.util.SortedMap
  477. // java.util.TreeMap.tailMap(java.lang.Object)
  478. Map tail = tm.tailMap(objArray[900].toString());
  479. assertTrue("Returned map of incorrect size : " + tail.size(), tail
  480. .size() == (objArray.length - 900) + 9);
  481. for (int i = 900; i < objArray.length; i++) {
  482. assertTrue("Map contains incorrect entries", tail
  483. .containsValue(objArray[i]));
  484. }
  485. // Regression for Harmony-1066
  486. assertTrue(tail instanceof Serializable);
  487. SortedMap<Integer,Integer> intMap,sub;
  488. int size = 16;
  489. intMap = new TreeMap<Integer,Integer>();
  490. for(int i=0; i<size; i++) {
  491. intMap.put(i,i);
  492. }
  493. sub = intMap.tailMap(size);
  494. assertEquals("size should be zero",sub.size(),0);
  495. assertTrue("submap should be empty",sub.isEmpty());
  496. try{
  497. sub.firstKey();
  498. fail("java.util.NoSuchElementException should be thrown");
  499. } catch(java.util.NoSuchElementException e) {
  500. }
  501. try{
  502. sub.lastKey();
  503. fail("java.util.NoSuchElementException should be thrown");
  504. } catch(java.util.NoSuchElementException e) {
  505. }
  506. size = 256;
  507. intMap = new TreeMap<Integer,Integer>();
  508. for(int i=0; i<size; i++) {
  509. intMap.put(i,i);
  510. }
  511. sub = intMap.tailMap(size);
  512. assertEquals("size should be zero",sub.size(),0);
  513. assertTrue("submap should be empty",sub.isEmpty());
  514. try{
  515. sub.firstKey();
  516. fail("java.util.NoSuchElementException should be thrown");
  517. } catch(java.util.NoSuchElementException e) {
  518. }
  519. try{
  520. sub.lastKey();
  521. fail("java.util.NoSuchElementException should be thrown");
  522. } catch(java.util.NoSuchElementException e) {
  523. }
  524. }
  525. /**
  526. * @tests java.util.TreeMap#values()
  527. */
  528. public void test_values() {
  529. // Test for method java.util.Collection java.util.TreeMap.values()
  530. Collection vals = tm.values();
  531. vals.iterator();
  532. assertTrue("Returned collection of incorrect size",
  533. vals.size() == objArray.length);
  534. for (Object element : objArray) {
  535. assertTrue("Collection contains incorrect elements", vals
  536. .contains(element));
  537. }
  538. TreeMap myTreeMap = new TreeMap();
  539. for (int i = 0; i < 100; i++) {
  540. myTreeMap.put(objArray[i], objArray[i]);
  541. }
  542. Collection values = myTreeMap.values();
  543. new Support_UnmodifiableCollectionTest(
  544. "Test Returned Collection From TreeMap.values()", values)
  545. .runTest();
  546. values.remove(new Integer(0));
  547. assertTrue(
  548. "Removing from the values collection should remove from the original map",
  549. !myTreeMap.containsValue(new Integer(0)));
  550. }
  551. /**
  552. * Tests equals() method.
  553. * Tests that no ClassCastException will be thrown in all cases.
  554. * Regression test for HARMONY-1639.
  555. */
  556. public void test_equals() throws Exception {
  557. // comparing TreeMaps with different object types
  558. Map m1 = new TreeMap();
  559. Map m2 = new TreeMap();
  560. m1.put("key1", "val1");
  561. m1.put("key2", "val2");
  562. m2.put(new Integer(1), "val1");
  563. m2.put(new Integer(2), "val2");
  564. assertFalse("Maps should not be equal 1", m1.equals(m2));
  565. assertFalse("Maps should not be equal 2", m2.equals(m1));
  566. // comparing TreeMap with HashMap
  567. m1 = new TreeMap();
  568. m2 = new HashMap();
  569. m1.put("key", "val");
  570. m2.put(new Object(), "val");
  571. assertFalse("Maps should not be equal 3", m1.equals(m2));
  572. assertFalse("Maps should not be equal 4", m2.equals(m1));
  573. // comparing TreeMaps with not-comparable objects inside
  574. m1 = new TreeMap();
  575. m2 = new TreeMap();
  576. m1.put(new Object(), "val1");
  577. m2.put(new Object(), "val1");
  578. assertFalse("Maps should not be equal 5", m1.equals(m2));
  579. assertFalse("Maps should not be equal 6", m2.equals(m1));
  580. }
  581. /**
  582. * Tests entrySet().contains() method behaviour with respect to entries
  583. * with null values.
  584. * Regression test for HARMONY-5788.
  585. */
  586. public void test_entrySet_contains() throws Exception {
  587. TreeMap master = new TreeMap<String, String>();
  588. TreeMap test_map = new TreeMap<String, String>();
  589. master.put("null", null);
  590. Object[] entry = master.entrySet().toArray();
  591. assertFalse("Empty map should not contain the null-valued entry",
  592. test_map.entrySet().contains(entry[0]));
  593. Map<String, String> submap = test_map.subMap("a","z");
  594. entry = master.entrySet().toArray();
  595. assertFalse("Empty submap should not contain the null-valued entry",
  596. submap.entrySet().contains(entry[0]));
  597. test_map.put("null", null);
  598. assertTrue("entrySet().containsAll(...) should work with null values",
  599. test_map.entrySet().containsAll(master.entrySet()));
  600. master.clear();
  601. master.put("null", '0');
  602. entry = master.entrySet().toArray();
  603. assertFalse("Null-valued entry should not equal non-null-valued entry",
  604. test_map.entrySet().contains(entry[0]));
  605. }
  606. /**
  607. * Sets up the fixture, for example, open a network connection. This method
  608. * is called before a test is executed.
  609. */
  610. @Override
  611. protected void setUp() {
  612. tm = new TreeMap();
  613. for (int i = 0; i < objArray.length; i++) {
  614. Object x = objArray[i] = new Integer(i);
  615. tm.put(x.toString(), x);
  616. }
  617. }
  618. }