PageRenderTime 62ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/luni/src/test/java/tests/api/java/util/TreeMapTest.java

https://bitbucket.org/cyanogenmod/android_libcore
Java | 891 lines | 638 code | 87 blank | 166 comment | 43 complexity | 92b3fc833cc65a0c1438a0698cf9ccc0 MD5 | raw file
  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 tests.api.java.util;
  18. import dalvik.annotation.KnownFailure;
  19. import dalvik.annotation.TestTargetNew;
  20. import dalvik.annotation.TestTargets;
  21. import dalvik.annotation.TestLevel;
  22. import dalvik.annotation.TestTargetClass;
  23. import dalvik.annotation.AndroidOnly;
  24. import java.io.Serializable;
  25. import java.text.CollationKey;
  26. import java.text.Collator;
  27. import java.util.AbstractMap;
  28. import java.util.Collection;
  29. import java.util.Comparator;
  30. import java.util.HashMap;
  31. import java.util.Iterator;
  32. import java.util.Map;
  33. import java.util.NoSuchElementException;
  34. import java.util.Set;
  35. import java.util.SortedMap;
  36. import java.util.TreeMap;
  37. import org.apache.harmony.testframework.serialization.SerializationTest;
  38. import tests.support.Support_MapTest2;
  39. import tests.support.Support_UnmodifiableCollectionTest;
  40. @TestTargetClass(TreeMap.class)
  41. public class TreeMapTest extends junit.framework.TestCase {
  42. public static class ReversedComparator implements Comparator {
  43. public int compare(Object o1, Object o2) {
  44. return -(((Comparable) o1).compareTo(o2));
  45. }
  46. public boolean equals(Object o1, Object o2) {
  47. return (((Comparable) o1).compareTo(o2)) == 0;
  48. }
  49. }
  50. // Regression for Harmony-1026
  51. public static class MockComparator<T extends Comparable<T>> implements
  52. Comparator<T>, Serializable {
  53. public int compare(T o1, T o2) {
  54. if (o1 == o2) {
  55. return 0;
  56. }
  57. if (null == o1 || null == o2) {
  58. return -1;
  59. }
  60. T c1 = o1;
  61. T c2 = o2;
  62. return c1.compareTo(c2);
  63. }
  64. }
  65. // Regression for Harmony-1161
  66. class MockComparatorNullTolerable implements Comparator<String> {
  67. public int compare(String o1, String o2) {
  68. if (o1 == o2) {
  69. return 0;
  70. }
  71. if (null == o1) {
  72. return -1;
  73. }
  74. if (null == o2) {
  75. return 1;
  76. }
  77. return o1.compareTo(o2);
  78. }
  79. }
  80. TreeMap tm;
  81. Object objArray[] = new Object[1000];
  82. /**
  83. * @tests java.util.TreeMap#TreeMap()
  84. */
  85. @TestTargetNew(
  86. level = TestLevel.COMPLETE,
  87. notes = "",
  88. method = "TreeMap",
  89. args = {}
  90. )
  91. public void test_Constructor() {
  92. // Test for method java.util.TreeMap()
  93. new Support_MapTest2(new TreeMap()).runTest();
  94. assertTrue("New treeMap non-empty", new TreeMap().isEmpty());
  95. }
  96. /**
  97. * @tests java.util.TreeMap#TreeMap(java.util.Comparator)
  98. */
  99. @TestTargetNew(
  100. level = TestLevel.COMPLETE,
  101. notes = "",
  102. method = "TreeMap",
  103. args = {java.util.Comparator.class}
  104. )
  105. public void test_ConstructorLjava_util_Comparator() {
  106. // Test for method java.util.TreeMap(java.util.Comparator)
  107. Comparator comp = new ReversedComparator();
  108. TreeMap reversedTreeMap = new TreeMap(comp);
  109. assertTrue("TreeMap answered incorrect comparator", reversedTreeMap
  110. .comparator() == comp);
  111. reversedTreeMap.put(new Integer(1).toString(), new Integer(1));
  112. reversedTreeMap.put(new Integer(2).toString(), new Integer(2));
  113. assertTrue("TreeMap does not use comparator (firstKey was incorrect)",
  114. reversedTreeMap.firstKey().equals(new Integer(2).toString()));
  115. assertTrue("TreeMap does not use comparator (lastKey was incorrect)",
  116. reversedTreeMap.lastKey().equals(new Integer(1).toString()));
  117. }
  118. /**
  119. * @tests java.util.TreeMap#TreeMap(java.util.Map)
  120. */
  121. @TestTargetNew(
  122. level = TestLevel.COMPLETE,
  123. notes = "",
  124. method = "TreeMap",
  125. args = {java.util.Map.class}
  126. )
  127. public void test_ConstructorLjava_util_Map() {
  128. // Test for method java.util.TreeMap(java.util.Map)
  129. TreeMap myTreeMap = new TreeMap(new HashMap(tm));
  130. assertTrue("Map is incorrect size", myTreeMap.size() == objArray.length);
  131. for (Object element : objArray) {
  132. assertTrue("Map has incorrect mappings", myTreeMap.get(
  133. element.toString()).equals(element));
  134. }
  135. HashMap hm = new HashMap();
  136. hm.put(new Integer(1), "one");
  137. hm.put("one", new Integer(1));
  138. try {
  139. new TreeMap(hm);
  140. fail("ClassCastException expected");
  141. } catch (ClassCastException e) {
  142. //expected
  143. }
  144. try {
  145. new TreeMap((Map)null);
  146. fail("NullPointerException expected");
  147. } catch (NullPointerException e) {
  148. //expected
  149. }
  150. }
  151. /**
  152. * @tests java.util.TreeMap#TreeMap(java.util.SortedMap)
  153. */
  154. @TestTargetNew(
  155. level = TestLevel.COMPLETE,
  156. notes = "",
  157. method = "TreeMap",
  158. args = {java.util.SortedMap.class}
  159. )
  160. public void test_ConstructorLjava_util_SortedMap() {
  161. // Test for method java.util.TreeMap(java.util.SortedMap)
  162. Comparator comp = new ReversedComparator();
  163. TreeMap reversedTreeMap = new TreeMap(comp);
  164. reversedTreeMap.put(new Integer(1).toString(), new Integer(1));
  165. reversedTreeMap.put(new Integer(2).toString(), new Integer(2));
  166. TreeMap anotherTreeMap = new TreeMap(reversedTreeMap);
  167. assertTrue("New tree map does not answer correct comparator",
  168. anotherTreeMap.comparator() == comp);
  169. assertTrue("TreeMap does not use comparator (firstKey was incorrect)",
  170. anotherTreeMap.firstKey().equals(new Integer(2).toString()));
  171. assertTrue("TreeMap does not use comparator (lastKey was incorrect)",
  172. anotherTreeMap.lastKey().equals(new Integer(1).toString()));
  173. try {
  174. new TreeMap((SortedMap)null);
  175. fail("NullPointerException expected");
  176. } catch (NullPointerException e) {
  177. //expected
  178. }
  179. }
  180. /**
  181. * @tests java.util.TreeMap#clear()
  182. */
  183. @TestTargetNew(
  184. level = TestLevel.COMPLETE,
  185. notes = "",
  186. method = "clear",
  187. args = {}
  188. )
  189. public void test_clear() {
  190. // Test for method void java.util.TreeMap.clear()
  191. tm.clear();
  192. assertEquals("Cleared map returned non-zero size", 0, tm.size());
  193. }
  194. /**
  195. * @tests java.util.TreeMap#clone()
  196. */
  197. @TestTargetNew(
  198. level = TestLevel.COMPLETE,
  199. notes = "",
  200. method = "clone",
  201. args = {}
  202. )
  203. public void test_clone() {
  204. // Test for method java.lang.Object java.util.TreeMap.clone()
  205. TreeMap clonedMap = (TreeMap) tm.clone();
  206. assertTrue("Cloned map does not equal the original map", clonedMap
  207. .equals(tm));
  208. assertTrue("Cloned map is the same reference as the original map",
  209. clonedMap != tm);
  210. for (Object element : objArray) {
  211. assertTrue("Cloned map contains incorrect elements", clonedMap
  212. .get(element.toString()) == tm.get(element.toString()));
  213. }
  214. TreeMap map = new TreeMap();
  215. map.put("key", "value");
  216. // get the keySet() and values() on the original Map
  217. Set keys = map.keySet();
  218. Collection values = map.values();
  219. assertEquals("values() does not work", "value", values.iterator()
  220. .next());
  221. assertEquals("keySet() does not work", "key", keys.iterator().next());
  222. AbstractMap map2 = (AbstractMap) map.clone();
  223. map2.put("key", "value2");
  224. Collection values2 = map2.values();
  225. assertTrue("values() is identical", values2 != values);
  226. // values() and keySet() on the cloned() map should be different
  227. assertEquals("values() was not cloned", "value2", values2.iterator()
  228. .next());
  229. map2.clear();
  230. map2.put("key2", "value3");
  231. Set key2 = map2.keySet();
  232. assertTrue("keySet() is identical", key2 != keys);
  233. assertEquals("keySet() was not cloned", "key2", key2.iterator().next());
  234. }
  235. /**
  236. * @tests java.util.TreeMap#comparator()
  237. */
  238. @TestTargetNew(
  239. level = TestLevel.COMPLETE,
  240. notes = "",
  241. method = "comparator",
  242. args = {}
  243. )
  244. public void test_comparator() {
  245. // Test for method java.util.Comparator java.util.TreeMap.comparator()\
  246. Comparator comp = new ReversedComparator();
  247. TreeMap reversedTreeMap = new TreeMap(comp);
  248. assertTrue("TreeMap answered incorrect comparator", reversedTreeMap
  249. .comparator() == comp);
  250. reversedTreeMap.put(new Integer(1).toString(), new Integer(1));
  251. reversedTreeMap.put(new Integer(2).toString(), new Integer(2));
  252. assertTrue("TreeMap does not use comparator (firstKey was incorrect)",
  253. reversedTreeMap.firstKey().equals(new Integer(2).toString()));
  254. assertTrue("TreeMap does not use comparator (lastKey was incorrect)",
  255. reversedTreeMap.lastKey().equals(new Integer(1).toString()));
  256. }
  257. /**
  258. * @tests java.util.TreeMap#containsKey(java.lang.Object)
  259. */
  260. @TestTargetNew(
  261. level = TestLevel.COMPLETE,
  262. notes = "",
  263. method = "containsKey",
  264. args = {java.lang.Object.class}
  265. )
  266. public void test_containsKeyLjava_lang_Object() {
  267. // Test for method boolean
  268. // java.util.TreeMap.containsKey(java.lang.Object)
  269. assertTrue("Returned false for valid key", tm.containsKey("95"));
  270. assertTrue("Returned true for invalid key", !tm.containsKey("XXXXX"));
  271. try {
  272. tm.containsKey(new Double(3.14));
  273. fail("ClassCastException expected");
  274. } catch (ClassCastException e) {
  275. //expected
  276. }
  277. try {
  278. tm.containsKey(null);
  279. fail("NullPointerException expected");
  280. } catch (NullPointerException e) {
  281. //expected
  282. }
  283. }
  284. /**
  285. * @tests java.util.TreeMap#containsValue(java.lang.Object)
  286. */
  287. @TestTargetNew(
  288. level = TestLevel.COMPLETE,
  289. notes = "",
  290. method = "containsValue",
  291. args = {java.lang.Object.class}
  292. )
  293. public void test_containsValueLjava_lang_Object() {
  294. // Test for method boolean
  295. // java.util.TreeMap.containsValue(java.lang.Object)
  296. assertTrue("Returned false for valid value", tm
  297. .containsValue(objArray[986]));
  298. assertTrue("Returned true for invalid value", !tm
  299. .containsValue(new Object()));
  300. }
  301. /**
  302. * @tests java.util.TreeMap#entrySet()
  303. */
  304. @TestTargetNew(
  305. level = TestLevel.COMPLETE,
  306. notes = "",
  307. method = "entrySet",
  308. args = {}
  309. )
  310. public void test_entrySet() {
  311. // Test for method java.util.Set java.util.TreeMap.entrySet()
  312. Set anEntrySet = tm.entrySet();
  313. Iterator entrySetIterator = anEntrySet.iterator();
  314. assertTrue("EntrySet is incorrect size",
  315. anEntrySet.size() == objArray.length);
  316. Map.Entry entry;
  317. while (entrySetIterator.hasNext()) {
  318. entry = (Map.Entry) entrySetIterator.next();
  319. assertTrue("EntrySet does not contain correct mappings", tm
  320. .get(entry.getKey()) == entry.getValue());
  321. }
  322. }
  323. /**
  324. * @tests java.util.TreeMap#firstKey()
  325. */
  326. @TestTargetNew(
  327. level = TestLevel.COMPLETE,
  328. notes = "",
  329. method = "firstKey",
  330. args = {}
  331. )
  332. public void test_firstKey() {
  333. // Test for method java.lang.Object java.util.TreeMap.firstKey()
  334. assertEquals("Returned incorrect first key", "0", tm.firstKey());
  335. tm = new TreeMap();
  336. try {
  337. tm.firstKey();
  338. fail("NoSuchElementException expected");
  339. } catch (NoSuchElementException e) {
  340. //expected
  341. }
  342. }
  343. /**
  344. * @tests java.util.TreeMap#get(java.lang.Object)
  345. */
  346. @TestTargetNew(
  347. level = TestLevel.COMPLETE,
  348. notes = "",
  349. method = "get",
  350. args = {java.lang.Object.class}
  351. )
  352. public void test_getLjava_lang_Object() {
  353. // Test for method java.lang.Object
  354. // java.util.TreeMap.get(java.lang.Object)
  355. Object o = new Object();
  356. tm.put("Hello", o);
  357. assertTrue("Failed to get mapping", tm.get("Hello") == o);
  358. try {
  359. tm.get(new Double(3.14));
  360. fail("ClassCastException expected");
  361. } catch (ClassCastException e) {
  362. //expected
  363. }
  364. try {
  365. tm.get(null);
  366. fail("NullPointerException expected");
  367. } catch (NullPointerException e) {
  368. //expected
  369. }
  370. }
  371. /**
  372. * @tests java.util.TreeMap#headMap(java.lang.Object)
  373. */
  374. @TestTargetNew(
  375. level = TestLevel.COMPLETE,
  376. notes = "",
  377. method = "headMap",
  378. args = {java.lang.Object.class}
  379. )
  380. public void test_headMapLjava_lang_Object() {
  381. // Test for method java.util.SortedMap
  382. // java.util.TreeMap.headMap(java.lang.Object)
  383. Map head = tm.headMap("100");
  384. assertEquals("Returned map of incorrect size", 3, head.size());
  385. assertTrue("Returned incorrect elements", head.containsKey("0")
  386. && head.containsValue(new Integer("1"))
  387. && head.containsKey("10"));
  388. SortedMap sort = tm.headMap("100");
  389. try {
  390. sort.headMap("50");
  391. fail("IllegalArgumentException expected");
  392. } catch (IllegalArgumentException e) {
  393. //expected
  394. }
  395. try {
  396. tm.headMap(this);
  397. fail("ClassCastException expected");
  398. } catch (ClassCastException e) {
  399. //expected
  400. }
  401. try {
  402. tm.headMap(null);
  403. fail("NullPointerException expected");
  404. } catch (NullPointerException e) {
  405. //expected
  406. }
  407. // Regression for Harmony-1026
  408. TreeMap<Integer, Double> map = new TreeMap<Integer, Double>(
  409. new MockComparator());
  410. map.put(1, 2.1);
  411. map.put(2, 3.1);
  412. map.put(3, 4.5);
  413. map.put(7, 21.3);
  414. map.put(null, null);
  415. SortedMap<Integer, Double> smap = map.headMap(null);
  416. assertEquals(0, smap.size());
  417. Set<Integer> keySet = smap.keySet();
  418. assertEquals(0, keySet.size());
  419. Set<Map.Entry<Integer, Double>> entrySet = smap.entrySet();
  420. assertEquals(0, entrySet.size());
  421. Collection<Double> valueCollection = smap.values();
  422. assertEquals(0, valueCollection.size());
  423. // Regression for Harmony-1066
  424. assertTrue(head instanceof Serializable);
  425. // Regression for ill-behaved collator
  426. Collator c = new Collator() {
  427. @Override
  428. public int compare(String o1, String o2) {
  429. if (o1 == null) {
  430. return 0;
  431. }
  432. return o1.compareTo(o2);
  433. }
  434. @Override
  435. public CollationKey getCollationKey(String string) {
  436. return null;
  437. }
  438. @Override
  439. public int hashCode() {
  440. return 0;
  441. }
  442. };
  443. TreeMap<String, String> treemap = new TreeMap<String, String>(c);
  444. assertEquals(0, treemap.headMap(null).size());
  445. }
  446. /**
  447. * @tests java.util.TreeMap#keySet()
  448. */
  449. @TestTargetNew(
  450. level = TestLevel.COMPLETE,
  451. notes = "",
  452. method = "keySet",
  453. args = {}
  454. )
  455. public void test_keySet() {
  456. // Test for method java.util.Set java.util.TreeMap.keySet()
  457. Set ks = tm.keySet();
  458. assertTrue("Returned set of incorrect size",
  459. ks.size() == objArray.length);
  460. for (int i = 0; i < tm.size(); i++) {
  461. assertTrue("Returned set is missing keys", ks.contains(new Integer(
  462. i).toString()));
  463. }
  464. }
  465. /**
  466. * @tests java.util.TreeMap#lastKey()
  467. */
  468. @TestTargetNew(
  469. level = TestLevel.COMPLETE,
  470. notes = "",
  471. method = "lastKey",
  472. args = {}
  473. )
  474. public void test_lastKey() {
  475. // Test for method java.lang.Object java.util.TreeMap.lastKey()
  476. assertTrue("Returned incorrect last key", tm.lastKey().equals(
  477. objArray[objArray.length - 1].toString()));
  478. tm = new TreeMap();
  479. try {
  480. tm.lastKey();
  481. fail("NoSuchElementException expected");
  482. } catch (NoSuchElementException e) {
  483. //expected
  484. }
  485. }
  486. /**
  487. * @tests java.util.TreeMap#put(java.lang.Object, java.lang.Object)
  488. */
  489. @TestTargetNew(
  490. level = TestLevel.COMPLETE,
  491. notes = "",
  492. method = "put",
  493. args = {java.lang.Object.class, java.lang.Object.class}
  494. )
  495. public void test_putLjava_lang_ObjectLjava_lang_Object() {
  496. // Test for method java.lang.Object
  497. // java.util.TreeMap.put(java.lang.Object, java.lang.Object)
  498. Object o = new Object();
  499. tm.put("Hello", o);
  500. assertTrue("Failed to put mapping", tm.get("Hello") == o);
  501. try {
  502. tm.put(null, "null");
  503. fail("NullPointerException expected");
  504. } catch (NullPointerException e) {
  505. //expected
  506. }
  507. // regression for Harmony-780
  508. tm = new TreeMap();
  509. assertNull(tm.put(new Object(), new Object()));
  510. try {
  511. tm.put(new Integer(1), new Object());
  512. fail("should throw ClassCastException");
  513. } catch (ClassCastException e) {
  514. // expected
  515. }
  516. tm = new TreeMap();
  517. assertNull(tm.put(new Integer(1), new Object()));
  518. // regression for Harmony-2474
  519. tm = new TreeMap();
  520. tm.remove(o);
  521. }
  522. /**
  523. * @tests java.util.TreeMap#putAll(java.util.Map)
  524. */
  525. @TestTargetNew(
  526. level = TestLevel.COMPLETE,
  527. notes = "",
  528. method = "putAll",
  529. args = {java.util.Map.class}
  530. )
  531. public void test_putAllLjava_util_Map() {
  532. // Test for method void java.util.TreeMap.putAll(java.util.Map)
  533. TreeMap x = new TreeMap();
  534. x.putAll(tm);
  535. assertTrue("Map incorrect size after put", x.size() == tm.size());
  536. for (Object element : objArray) {
  537. assertTrue("Failed to put all elements", x.get(element.toString())
  538. .equals(element));
  539. }
  540. x = new TreeMap();
  541. x.put(new Integer(1), "one");
  542. x.put(new Integer(2), "two");
  543. try {
  544. tm.putAll(x);
  545. fail("ClassCastException expected");
  546. } catch (ClassCastException e) {
  547. //expected
  548. }
  549. try {
  550. tm.putAll(null);
  551. fail("NullPointerException expected");
  552. } catch (NullPointerException e) {
  553. //expected
  554. }
  555. }
  556. /**
  557. * @tests java.util.TreeMap#remove(java.lang.Object)
  558. */
  559. @TestTargetNew(
  560. level = TestLevel.COMPLETE,
  561. notes = "",
  562. method = "remove",
  563. args = {java.lang.Object.class}
  564. )
  565. public void test_removeLjava_lang_Object() {
  566. // Test for method java.lang.Object
  567. // java.util.TreeMap.remove(java.lang.Object)
  568. tm.remove("990");
  569. assertTrue("Failed to remove mapping", !tm.containsKey("990"));
  570. try {
  571. tm.remove(new Double(3.14));
  572. fail("ClassCastException expected");
  573. } catch (ClassCastException e) {
  574. //expected
  575. }
  576. try {
  577. tm.remove(null);
  578. fail("NullPointerException expected");
  579. } catch (NullPointerException e) {
  580. //expected
  581. }
  582. }
  583. /**
  584. * @tests java.util.TreeMap#size()
  585. */
  586. @TestTargetNew(
  587. level = TestLevel.COMPLETE,
  588. notes = "",
  589. method = "size",
  590. args = {}
  591. )
  592. public void test_size() {
  593. // Test for method int java.util.TreeMap.size()
  594. assertEquals("Returned incorrect size", 1000, tm.size());
  595. }
  596. /**
  597. * @tests java.util.TreeMap#subMap(java.lang.Object, java.lang.Object)
  598. */
  599. @TestTargetNew(
  600. level = TestLevel.COMPLETE,
  601. notes = "",
  602. method = "subMap",
  603. args = {java.lang.Object.class, java.lang.Object.class}
  604. )
  605. public void test_subMapLjava_lang_ObjectLjava_lang_Object() {
  606. // Test for method java.util.SortedMap
  607. // java.util.TreeMap.subMap(java.lang.Object, java.lang.Object)
  608. SortedMap subMap = tm.subMap(objArray[100].toString(), objArray[109]
  609. .toString());
  610. assertEquals("subMap is of incorrect size", 9, subMap.size());
  611. for (int counter = 100; counter < 109; counter++) {
  612. assertTrue("SubMap contains incorrect elements", subMap.get(
  613. objArray[counter].toString()).equals(objArray[counter]));
  614. }
  615. try {
  616. tm.subMap(objArray[9].toString(), objArray[1].toString());
  617. fail("end key less than start key should throw IllegalArgumentException");
  618. } catch (IllegalArgumentException e) {
  619. // Expected
  620. }
  621. // Regression for Harmony-1161
  622. TreeMap<String, String> treeMapWithNull = new TreeMap<String, String>(
  623. new MockComparatorNullTolerable());
  624. treeMapWithNull.put("key1", "value1"); //$NON-NLS-1$ //$NON-NLS-2$
  625. treeMapWithNull.put(null, "value2"); //$NON-NLS-1$
  626. SortedMap<String, String> subMapWithNull = treeMapWithNull.subMap(null,
  627. "key1"); //$NON-NLS-1$
  628. assertEquals("Size of subMap should be 1:", 1, subMapWithNull.size()); //$NON-NLS-1$
  629. // Regression test for typo in lastKey method
  630. SortedMap<String, String> map = new TreeMap<String, String>();
  631. map.put("1", "one"); //$NON-NLS-1$ //$NON-NLS-2$
  632. map.put("2", "two"); //$NON-NLS-1$ //$NON-NLS-2$
  633. map.put("3", "three"); //$NON-NLS-1$ //$NON-NLS-2$
  634. assertEquals("3", map.lastKey());
  635. SortedMap<String, String> sub = map.subMap("1", "3"); //$NON-NLS-1$ //$NON-NLS-2$
  636. assertEquals("2", sub.lastKey()); //$NON-NLS-1$
  637. try {
  638. tm.subMap(this, this);
  639. fail("ClassCastException expected");
  640. } catch (ClassCastException e) {
  641. //expected
  642. }
  643. try {
  644. tm.subMap(objArray[9].toString(), null);
  645. fail("NullPointerException expected");
  646. } catch (NullPointerException e) {
  647. //expected
  648. }
  649. try {
  650. tm.subMap(null, objArray[9].toString());
  651. fail("NullPointerException expected");
  652. } catch (NullPointerException e) {
  653. //expected
  654. }
  655. }
  656. /**
  657. * @tests java.util.TreeMap#tailMap(java.lang.Object)
  658. */
  659. @TestTargetNew(
  660. level = TestLevel.COMPLETE,
  661. notes = "",
  662. method = "tailMap",
  663. args = {java.lang.Object.class}
  664. )
  665. public void test_tailMapLjava_lang_Object() {
  666. // Test for method java.util.SortedMap
  667. // java.util.TreeMap.tailMap(java.lang.Object)
  668. Map tail = tm.tailMap(objArray[900].toString());
  669. assertTrue("Returned map of incorrect size : " + tail.size(), tail
  670. .size() == (objArray.length - 900) + 9);
  671. for (int i = 900; i < objArray.length; i++) {
  672. assertTrue("Map contains incorrect entries", tail
  673. .containsValue(objArray[i]));
  674. }
  675. SortedMap sort = tm.tailMap("99");
  676. try {
  677. sort.tailMap("101");
  678. fail("IllegalArgumentException expected");
  679. } catch (IllegalArgumentException e) {
  680. //expected
  681. }
  682. try {
  683. tm.tailMap(this);
  684. fail("ClassCastException expected");
  685. } catch (ClassCastException e) {
  686. //expected
  687. }
  688. try {
  689. tm.tailMap(null);
  690. fail("NullPointerException expected");
  691. } catch (NullPointerException e) {
  692. //expected
  693. }
  694. // Regression for Harmony-1066
  695. assertTrue(tail instanceof Serializable);
  696. }
  697. /**
  698. * @tests java.util.TreeMap#values()
  699. */
  700. @TestTargetNew(
  701. level = TestLevel.COMPLETE,
  702. notes = "",
  703. method = "values",
  704. args = {}
  705. )
  706. public void test_values() {
  707. // Test for method java.util.Collection java.util.TreeMap.values()
  708. Collection vals = tm.values();
  709. vals.iterator();
  710. assertTrue("Returned collection of incorrect size",
  711. vals.size() == objArray.length);
  712. for (Object element : objArray) {
  713. assertTrue("Collection contains incorrect elements", vals
  714. .contains(element));
  715. }
  716. TreeMap myTreeMap = new TreeMap();
  717. for (int i = 0; i < 100; i++) {
  718. myTreeMap.put(objArray[i], objArray[i]);
  719. }
  720. Collection values = myTreeMap.values();
  721. new Support_UnmodifiableCollectionTest(
  722. "Test Returned Collection From TreeMap.values()", values)
  723. .runTest();
  724. values.remove(new Integer(0));
  725. assertTrue(
  726. "Removing from the values collection should remove from the original map",
  727. !myTreeMap.containsValue(new Integer(0)));
  728. }
  729. /**
  730. * @tests java.util.TreeMap#SerializationTest()
  731. */
  732. @TestTargetNew(
  733. level = TestLevel.COMPLETE,
  734. notes = "Verifies serialization/deserialization.",
  735. method = "!SerializationSelf",
  736. args = {}
  737. )
  738. @AndroidOnly("fail on RI. See comment below")
  739. // Regression for Harmony-1066
  740. public void test_SubMap_Serializable() throws Exception {
  741. TreeMap<Integer, Double> map = new TreeMap<Integer, Double>();
  742. map.put(1, 2.1);
  743. map.put(2, 3.1);
  744. map.put(3, 4.5);
  745. map.put(7, 21.3);
  746. SortedMap<Integer, Double> headMap = map.headMap(3);
  747. assertTrue(headMap instanceof Serializable);
  748. assertFalse(headMap instanceof TreeMap);
  749. assertTrue(headMap instanceof SortedMap);
  750. assertFalse(headMap.entrySet() instanceof Serializable);
  751. assertFalse(headMap.keySet() instanceof Serializable);
  752. assertFalse(headMap.values() instanceof Serializable);
  753. // This assertion will fail on RI. This is a bug of RI.
  754. SerializationTest.verifySelf(headMap);
  755. }
  756. /**
  757. * Tests equals() method.
  758. * Tests that no ClassCastException will be thrown in all cases.
  759. * Regression test for HARMONY-1639.
  760. */
  761. @TestTargetNew(
  762. level = TestLevel.COMPLETE,
  763. notes = "",
  764. method = "equals",
  765. args = {java.lang.Object.class}
  766. )
  767. public void test_equals() throws Exception {
  768. // comparing TreeMaps with different object types
  769. Map m1 = new TreeMap();
  770. Map m2 = new TreeMap();
  771. m1.put("key1", "val1");
  772. m1.put("key2", "val2");
  773. m2.put(new Integer(1), "val1");
  774. m2.put(new Integer(2), "val2");
  775. assertFalse("Maps should not be equal 1", m1.equals(m2));
  776. assertFalse("Maps should not be equal 2", m2.equals(m1));
  777. // comparing TreeMap with HashMap
  778. m1 = new TreeMap();
  779. m2 = new HashMap();
  780. m1.put("key", "val");
  781. m2.put(new Object(), "val");
  782. assertFalse("Maps should not be equal 3", m1.equals(m2));
  783. assertFalse("Maps should not be equal 4", m2.equals(m1));
  784. // comparing TreeMaps with not-comparable objects inside
  785. m1 = new TreeMap();
  786. m2 = new TreeMap();
  787. m1.put(new Object(), "val1");
  788. m2.put(new Object(), "val1");
  789. assertFalse("Maps should not be equal 5", m1.equals(m2));
  790. assertFalse("Maps should not be equal 6", m2.equals(m1));
  791. }
  792. /**
  793. * Sets up the fixture, for example, open a network connection. This method
  794. * is called before a test is executed.
  795. */
  796. @Override
  797. protected void setUp() {
  798. tm = new TreeMap();
  799. for (int i = 0; i < objArray.length; i++) {
  800. Object x = objArray[i] = new Integer(i);
  801. tm.put(x.toString(), x);
  802. }
  803. }
  804. }