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 import org.testng.annotations.Test; 33 34 import java.util.Properties; 35 36 import static org.testng.Assert.assertEquals; 37 import static org.testng.Assert.assertFalse; 38 import static org.testng.Assert.assertThrows; 39 import static org.testng.Assert.assertTrue; 40 41 public class PropertiesEntrySetTest { main(String[] argv)42 43 @Test 44 public void testEquals() { 45 Properties a = new Properties(); 46 var aEntrySet = a.entrySet(); 47 assertFalse(aEntrySet.equals(null)); 48 assertTrue(aEntrySet.equals(aEntrySet)); 49 50 Properties b = new Properties(); 51 var bEntrySet = b.entrySet(); 52 assertTrue(bEntrySet.equals(aEntrySet)); 53 assertTrue(bEntrySet.hashCode() == aEntrySet.hashCode()); 54 55 a.setProperty("p1", "1"); 56 assertFalse(bEntrySet.equals(aEntrySet)); 57 assertFalse(bEntrySet.hashCode() == aEntrySet.hashCode()); 58 59 b.setProperty("p1", "1"); 60 assertTrue(aEntrySet.equals(bEntrySet)); 61 assertTrue(bEntrySet.hashCode() == aEntrySet.hashCode()); 62 63 Properties c = new Properties(); 64 c.setProperty("p1", "2"); 65 var cEntrySet = c.entrySet(); 66 assertFalse(cEntrySet.equals(bEntrySet)); 67 assertFalse(bEntrySet.hashCode() == cEntrySet.hashCode()); 68 assertFalse(cEntrySet.equals(aEntrySet)); 69 assertFalse(aEntrySet.hashCode() == cEntrySet.hashCode()); 70 71 a.setProperty("p2", "2"); 72 Properties d = new Properties(); 73 d.setProperty("p2", "2"); 74 d.setProperty("p1", "1"); 75 var dEntrySet = d.entrySet(); 76 assertTrue(dEntrySet.equals(aEntrySet)); 77 assertTrue(aEntrySet.hashCode() == dEntrySet.hashCode()); 78 79 a.remove("p1"); 80 assertFalse(aEntrySet.equals(dEntrySet)); 81 assertFalse(aEntrySet.hashCode() == dEntrySet.hashCode()); 82 83 d.remove("p1", "1"); 84 assertTrue(dEntrySet.equals(aEntrySet)); 85 assertTrue(aEntrySet.hashCode() == dEntrySet.hashCode()); 86 87 a.clear(); 88 assertFalse(aEntrySet.equals(dEntrySet)); 89 assertFalse(aEntrySet.hashCode() == dEntrySet.hashCode()); 90 assertTrue(aEntrySet.isEmpty()); 91 92 d.clear(); 93 assertTrue(dEntrySet.equals(aEntrySet)); 94 assertTrue(aEntrySet.hashCode() == dEntrySet.hashCode()); 95 assertTrue(dEntrySet.isEmpty()); 96 } 97 98 @Test 99 public void testToString() { 100 Properties a = new Properties(); 101 var aEntrySet = a.entrySet(); 102 assertEquals(aEntrySet.toString(), "[]"); 103 104 a.setProperty("p1", "1"); 105 assertEquals(aEntrySet.toString(), "[p1=1]"); 106 107 a.setProperty("p2", "2"); 108 assertEquals(aEntrySet.size(), 2); 109 assertTrue(aEntrySet.toString().trim().startsWith("[")); 110 assertTrue(aEntrySet.toString().contains("p1=1")); 111 assertTrue(aEntrySet.toString().contains("p2=2")); 112 assertTrue(aEntrySet.toString().trim().endsWith("]")); 113 114 Properties b = new Properties(); 115 b.setProperty("p2", "2"); 116 b.setProperty("p1", "1"); 117 var bEntrySet = b.entrySet(); 118 assertEquals(bEntrySet.size(), 2); 119 assertTrue(bEntrySet.toString().trim().startsWith("[")); 120 assertTrue(bEntrySet.toString().contains("p1=1")); 121 assertTrue(bEntrySet.toString().contains("p2=2")); 122 assertTrue(bEntrySet.toString().trim().endsWith("]")); 123 124 b.setProperty("p0", "0"); 125 assertEquals(bEntrySet.size(), 3); 126 assertTrue(bEntrySet.toString().contains("p0=0")); 127 128 b.remove("p1"); 129 assertEquals(bEntrySet.size(), 2); 130 assertFalse(bEntrySet.toString().contains("p1=1")); 131 assertTrue(bEntrySet.toString().trim().startsWith("[")); 132 assertTrue(bEntrySet.toString().contains("p0=0")); 133 assertTrue(bEntrySet.toString().contains("p2=2")); 134 assertTrue(bEntrySet.toString().trim().endsWith("]")); 135 136 b.remove("p0", "0"); 137 assertEquals(bEntrySet.size(), 1); 138 assertFalse(bEntrySet.toString().contains("p0=0")); 139 assertTrue(bEntrySet.toString().trim().startsWith("[")); 140 assertTrue(bEntrySet.toString().contains("p2=2")); 141 assertTrue(bEntrySet.toString().trim().endsWith("]")); 142 143 b.clear(); 144 assertTrue(bEntrySet.isEmpty()); 145 assertTrue(bEntrySet.toString().equals("[]")); 146 } 147 148 @Test 149 public void testEntrySetWithoutException() { 150 Properties a = new Properties(); 151 a.setProperty("p1", "1"); 152 a.setProperty("p2", "2"); 153 var aEntrySet = a.entrySet(); 154 assertEquals(aEntrySet.size(), 2); 155 156 var i = aEntrySet.iterator(); 157 var e1 = i.next(); 158 i.remove(); 159 assertFalse(aEntrySet.contains(e1)); 160 assertEquals(aEntrySet.size(), 1); 161 162 var e2 = i.next(); 163 aEntrySet.remove(e2); 164 assertFalse(aEntrySet.contains(e2)); 165 assertTrue(aEntrySet.isEmpty()); 166 167 a.setProperty("p1", "1"); 168 a.setProperty("p3", "3"); 169 Properties b = new Properties(); 170 b.setProperty("p2", "2"); 171 b.setProperty("p1", "1"); 172 var bEntrySet = b.entrySet(); 173 174 assertFalse(bEntrySet.containsAll(aEntrySet)); 175 assertEquals(bEntrySet.size(), 2); 176 177 assertTrue(bEntrySet.removeAll(aEntrySet)); 178 assertEquals(bEntrySet.size(), 1); 179 180 assertTrue(bEntrySet.retainAll(aEntrySet)); 181 assertTrue(bEntrySet.isEmpty()); 182 assertEquals(aEntrySet.size(), 2); 183 184 aEntrySet.clear(); 185 assertTrue(aEntrySet.isEmpty()); 186 } 187 188 @Test 189 public void testEntrySetExceptionWhenAdd() { 190 Properties a = new Properties(); 191 a.setProperty("p1", "1"); 192 var aEntrySet = a.entrySet(); 193 194 Properties b = new Properties(); 195 b.setProperty("p2", "2"); 196 var bEntrySet = b.entrySet(); 197 198 assertThrows(UnsupportedOperationException.class, () -> aEntrySet.addAll(bEntrySet)); 199 assertThrows(UnsupportedOperationException.class, () -> aEntrySet.add(bEntrySet.iterator().next())); 200 } 201 } 202