1 /* 2 * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 8245694 27 * @summary tests the entrySet() method of Properties class 28 * @author Yu Li 29 * @run testng PropertiesEntrySetTest 30 */ 31 32 package test.java.util.Properties; 33 34 import org.testng.annotations.Test; 35 36 import java.util.Properties; 37 38 import static org.testng.Assert.assertEquals; 39 import static org.testng.Assert.assertFalse; 40 import static org.testng.Assert.assertThrows; 41 import static org.testng.Assert.assertTrue; 42 43 public class PropertiesEntrySetTest { 44 45 @Test testEquals()46 public void testEquals() { 47 Properties a = new Properties(); 48 var aEntrySet = a.entrySet(); 49 assertFalse(aEntrySet.equals(null)); 50 assertTrue(aEntrySet.equals(aEntrySet)); 51 52 Properties b = new Properties(); 53 var bEntrySet = b.entrySet(); 54 assertTrue(bEntrySet.equals(aEntrySet)); 55 assertTrue(bEntrySet.hashCode() == aEntrySet.hashCode()); 56 57 a.setProperty("p1", "1"); 58 assertFalse(bEntrySet.equals(aEntrySet)); 59 assertFalse(bEntrySet.hashCode() == aEntrySet.hashCode()); 60 61 b.setProperty("p1", "1"); 62 assertTrue(aEntrySet.equals(bEntrySet)); 63 assertTrue(bEntrySet.hashCode() == aEntrySet.hashCode()); 64 65 Properties c = new Properties(); 66 c.setProperty("p1", "2"); 67 var cEntrySet = c.entrySet(); 68 assertFalse(cEntrySet.equals(bEntrySet)); 69 assertFalse(bEntrySet.hashCode() == cEntrySet.hashCode()); 70 assertFalse(cEntrySet.equals(aEntrySet)); 71 assertFalse(aEntrySet.hashCode() == cEntrySet.hashCode()); 72 73 a.setProperty("p2", "2"); 74 Properties d = new Properties(); 75 d.setProperty("p2", "2"); 76 d.setProperty("p1", "1"); 77 var dEntrySet = d.entrySet(); 78 assertTrue(dEntrySet.equals(aEntrySet)); 79 assertTrue(aEntrySet.hashCode() == dEntrySet.hashCode()); 80 81 a.remove("p1"); 82 assertFalse(aEntrySet.equals(dEntrySet)); 83 assertFalse(aEntrySet.hashCode() == dEntrySet.hashCode()); 84 85 d.remove("p1", "1"); 86 assertTrue(dEntrySet.equals(aEntrySet)); 87 assertTrue(aEntrySet.hashCode() == dEntrySet.hashCode()); 88 89 a.clear(); 90 assertFalse(aEntrySet.equals(dEntrySet)); 91 assertFalse(aEntrySet.hashCode() == dEntrySet.hashCode()); 92 assertTrue(aEntrySet.isEmpty()); 93 94 d.clear(); 95 assertTrue(dEntrySet.equals(aEntrySet)); 96 assertTrue(aEntrySet.hashCode() == dEntrySet.hashCode()); 97 assertTrue(dEntrySet.isEmpty()); 98 } 99 100 @Test testToString()101 public void testToString() { 102 Properties a = new Properties(); 103 var aEntrySet = a.entrySet(); 104 assertEquals(aEntrySet.toString(), "[]"); 105 106 a.setProperty("p1", "1"); 107 assertEquals(aEntrySet.toString(), "[p1=1]"); 108 109 a.setProperty("p2", "2"); 110 assertEquals(aEntrySet.size(), 2); 111 assertTrue(aEntrySet.toString().trim().startsWith("[")); 112 assertTrue(aEntrySet.toString().contains("p1=1")); 113 assertTrue(aEntrySet.toString().contains("p2=2")); 114 assertTrue(aEntrySet.toString().trim().endsWith("]")); 115 116 Properties b = new Properties(); 117 b.setProperty("p2", "2"); 118 b.setProperty("p1", "1"); 119 var bEntrySet = b.entrySet(); 120 assertEquals(bEntrySet.size(), 2); 121 assertTrue(bEntrySet.toString().trim().startsWith("[")); 122 assertTrue(bEntrySet.toString().contains("p1=1")); 123 assertTrue(bEntrySet.toString().contains("p2=2")); 124 assertTrue(bEntrySet.toString().trim().endsWith("]")); 125 126 b.setProperty("p0", "0"); 127 assertEquals(bEntrySet.size(), 3); 128 assertTrue(bEntrySet.toString().contains("p0=0")); 129 130 b.remove("p1"); 131 assertEquals(bEntrySet.size(), 2); 132 assertFalse(bEntrySet.toString().contains("p1=1")); 133 assertTrue(bEntrySet.toString().trim().startsWith("[")); 134 assertTrue(bEntrySet.toString().contains("p0=0")); 135 assertTrue(bEntrySet.toString().contains("p2=2")); 136 assertTrue(bEntrySet.toString().trim().endsWith("]")); 137 138 b.remove("p0", "0"); 139 assertEquals(bEntrySet.size(), 1); 140 assertFalse(bEntrySet.toString().contains("p0=0")); 141 assertTrue(bEntrySet.toString().trim().startsWith("[")); 142 assertTrue(bEntrySet.toString().contains("p2=2")); 143 assertTrue(bEntrySet.toString().trim().endsWith("]")); 144 145 b.clear(); 146 assertTrue(bEntrySet.isEmpty()); 147 assertTrue(bEntrySet.toString().equals("[]")); 148 } 149 150 @Test testEntrySetWithoutException()151 public void testEntrySetWithoutException() { 152 Properties a = new Properties(); 153 a.setProperty("p1", "1"); 154 a.setProperty("p2", "2"); 155 var aEntrySet = a.entrySet(); 156 assertEquals(aEntrySet.size(), 2); 157 158 var i = aEntrySet.iterator(); 159 var e1 = i.next(); 160 i.remove(); 161 assertFalse(aEntrySet.contains(e1)); 162 assertEquals(aEntrySet.size(), 1); 163 164 var e2 = i.next(); 165 aEntrySet.remove(e2); 166 assertFalse(aEntrySet.contains(e2)); 167 assertTrue(aEntrySet.isEmpty()); 168 169 a.setProperty("p1", "1"); 170 a.setProperty("p3", "3"); 171 Properties b = new Properties(); 172 b.setProperty("p2", "2"); 173 b.setProperty("p1", "1"); 174 var bEntrySet = b.entrySet(); 175 176 assertFalse(bEntrySet.containsAll(aEntrySet)); 177 assertEquals(bEntrySet.size(), 2); 178 179 assertTrue(bEntrySet.removeAll(aEntrySet)); 180 assertEquals(bEntrySet.size(), 1); 181 182 assertTrue(bEntrySet.retainAll(aEntrySet)); 183 assertTrue(bEntrySet.isEmpty()); 184 assertEquals(aEntrySet.size(), 2); 185 186 aEntrySet.clear(); 187 assertTrue(aEntrySet.isEmpty()); 188 } 189 190 @Test testEntrySetExceptionWhenAdd()191 public void testEntrySetExceptionWhenAdd() { 192 Properties a = new Properties(); 193 a.setProperty("p1", "1"); 194 var aEntrySet = a.entrySet(); 195 196 Properties b = new Properties(); 197 b.setProperty("p2", "2"); 198 var bEntrySet = b.entrySet(); 199 200 assertThrows(UnsupportedOperationException.class, () -> aEntrySet.addAll(bEntrySet)); 201 assertThrows(UnsupportedOperationException.class, () -> aEntrySet.add(bEntrySet.iterator().next())); 202 } 203 } 204