Index: trunk/modules/luni/src/main/java/java/util/AbstractMap.java =================================================================== --- trunk/modules/luni/src/main/java/java/util/AbstractMap.java (revision 531723) +++ trunk/modules/luni/src/main/java/java/util/AbstractMap.java (working copy) @@ -139,10 +139,15 @@ Set objectSet = map.entrySet(); Iterator> it = entrySet().iterator(); - while (it.hasNext()) { - if (!objectSet.contains(it.next())) { - return false; + + try { + while (it.hasNext()) { + if (!objectSet.contains(it.next())) { + return false; + } } + } catch (ClassCastException cce) { + return false; } return true; } Index: trunk/modules/luni/src/main/java/java/util/AbstractSet.java =================================================================== --- trunk/modules/luni/src/main/java/java/util/AbstractSet.java (revision 531723) +++ trunk/modules/luni/src/main/java/java/util/AbstractSet.java (working copy) @@ -52,7 +52,12 @@ } if (object instanceof Set) { Set s = (Set) object; - return size() == s.size() && containsAll(s); + + try { + return size() == s.size() && containsAll(s); + } catch (ClassCastException cce) { + return false; + } } return false; } Index: trunk/modules/luni/src/test/java/tests/api/java/util/TreeMapTest.java =================================================================== --- trunk/modules/luni/src/test/java/tests/api/java/util/TreeMapTest.java (revision 531723) +++ trunk/modules/luni/src/test/java/tests/api/java/util/TreeMapTest.java (working copy) @@ -523,6 +523,39 @@ } /** + * Tests equals() method. + * Tests that no ClassCastException will be thrown in all cases. + * Regression test for HARMONY-1639. + */ + public void test_equals() throws Exception { + // comparing TreeMaps with different object types + Map m1 = new TreeMap(); + Map m2 = new TreeMap(); + m1.put("key1", "val1"); + m1.put("key2", "val2"); + m2.put(new Integer(1), "val1"); + m2.put(new Integer(2), "val2"); + assertFalse("Maps should not be equal 1", m1.equals(m2)); + assertFalse("Maps should not be equal 2", m2.equals(m1)); + + // comparing TreeMap with HashMap + m1 = new TreeMap(); + m2 = new HashMap(); + m1.put("key", "val"); + m2.put(new Object(), "val"); + assertFalse("Maps should not be equal 3", m1.equals(m2)); + assertFalse("Maps should not be equal 4", m2.equals(m1)); + + // comparing TreeMaps with not-comparable objects inside + m1 = new TreeMap(); + m2 = new TreeMap(); + m1.put(new Object(), "val1"); + m2.put(new Object(), "val1"); + assertFalse("Maps should not be equal 5", m1.equals(m2)); + assertFalse("Maps should not be equal 6", m2.equals(m1)); + } + + /** * Sets up the fixture, for example, open a network connection. This method * is called before a test is executed. */ Index: trunk/modules/luni/src/test/java/tests/api/java/util/TreeSetTest.java =================================================================== --- trunk/modules/luni/src/test/java/tests/api/java/util/TreeSetTest.java (revision 531723) +++ trunk/modules/luni/src/test/java/tests/api/java/util/TreeSetTest.java (working copy) @@ -296,6 +296,39 @@ assertTrue("Returned incorrect set", s.contains(objArray[i])); } + /** + * Tests equals() method. + * Tests that no ClassCastException will be thrown in all cases. + * Regression test for HARMONY-1639. + */ + public void test_equals() throws Exception { + // comparing TreeSets with different object types + Set s1 = new TreeSet(); + Set s2 = new TreeSet(); + s1.add("key1"); + s1.add("key2"); + s2.add(new Integer(1)); + s2.add(new Integer(2)); + assertFalse("Sets should not be equal 1", s1.equals(s2)); + assertFalse("Sets should not be equal 2", s2.equals(s1)); + + // comparing TreeSet with HashSet + s1 = new TreeSet(); + s2 = new HashSet(); + s1.add("key"); + s2.add(new Object()); + assertFalse("Sets should not be equal 3", s1.equals(s2)); + assertFalse("Sets should not be equal 4", s2.equals(s1)); + + // comparing TreeSets with not-comparable objects inside + s1 = new TreeSet(); + s2 = new TreeSet(); + s1.add(new Object()); + s2.add(new Object()); + assertFalse("Sets should not be equal 5", s1.equals(s2)); + assertFalse("Sets should not be equal 6", s2.equals(s1)); + } + /** * Sets up the fixture, for example, open a network connection. This method * is called before a test is executed.