Path: blob/master/test/jdk/java/util/Properties/PropertiesEntrySetTest.java
41149 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 824569426* @summary tests the entrySet() method of Properties class27* @author Yu Li28* @run testng PropertiesEntrySetTest29*/3031import org.testng.annotations.Test;3233import java.util.Properties;3435import static org.testng.Assert.assertEquals;36import static org.testng.Assert.assertFalse;37import static org.testng.Assert.assertThrows;38import static org.testng.Assert.assertTrue;3940public class PropertiesEntrySetTest {4142@Test43public void testEquals() {44Properties a = new Properties();45var aEntrySet = a.entrySet();46assertFalse(aEntrySet.equals(null));47assertTrue(aEntrySet.equals(aEntrySet));4849Properties b = new Properties();50var bEntrySet = b.entrySet();51assertTrue(bEntrySet.equals(aEntrySet));52assertTrue(bEntrySet.hashCode() == aEntrySet.hashCode());5354a.setProperty("p1", "1");55assertFalse(bEntrySet.equals(aEntrySet));56assertFalse(bEntrySet.hashCode() == aEntrySet.hashCode());5758b.setProperty("p1", "1");59assertTrue(aEntrySet.equals(bEntrySet));60assertTrue(bEntrySet.hashCode() == aEntrySet.hashCode());6162Properties c = new Properties();63c.setProperty("p1", "2");64var cEntrySet = c.entrySet();65assertFalse(cEntrySet.equals(bEntrySet));66assertFalse(bEntrySet.hashCode() == cEntrySet.hashCode());67assertFalse(cEntrySet.equals(aEntrySet));68assertFalse(aEntrySet.hashCode() == cEntrySet.hashCode());6970a.setProperty("p2", "2");71Properties d = new Properties();72d.setProperty("p2", "2");73d.setProperty("p1", "1");74var dEntrySet = d.entrySet();75assertTrue(dEntrySet.equals(aEntrySet));76assertTrue(aEntrySet.hashCode() == dEntrySet.hashCode());7778a.remove("p1");79assertFalse(aEntrySet.equals(dEntrySet));80assertFalse(aEntrySet.hashCode() == dEntrySet.hashCode());8182d.remove("p1", "1");83assertTrue(dEntrySet.equals(aEntrySet));84assertTrue(aEntrySet.hashCode() == dEntrySet.hashCode());8586a.clear();87assertFalse(aEntrySet.equals(dEntrySet));88assertFalse(aEntrySet.hashCode() == dEntrySet.hashCode());89assertTrue(aEntrySet.isEmpty());9091d.clear();92assertTrue(dEntrySet.equals(aEntrySet));93assertTrue(aEntrySet.hashCode() == dEntrySet.hashCode());94assertTrue(dEntrySet.isEmpty());95}9697@Test98public void testToString() {99Properties a = new Properties();100var aEntrySet = a.entrySet();101assertEquals(aEntrySet.toString(), "[]");102103a.setProperty("p1", "1");104assertEquals(aEntrySet.toString(), "[p1=1]");105106a.setProperty("p2", "2");107assertEquals(aEntrySet.size(), 2);108assertTrue(aEntrySet.toString().trim().startsWith("["));109assertTrue(aEntrySet.toString().contains("p1=1"));110assertTrue(aEntrySet.toString().contains("p2=2"));111assertTrue(aEntrySet.toString().trim().endsWith("]"));112113Properties b = new Properties();114b.setProperty("p2", "2");115b.setProperty("p1", "1");116var bEntrySet = b.entrySet();117assertEquals(bEntrySet.size(), 2);118assertTrue(bEntrySet.toString().trim().startsWith("["));119assertTrue(bEntrySet.toString().contains("p1=1"));120assertTrue(bEntrySet.toString().contains("p2=2"));121assertTrue(bEntrySet.toString().trim().endsWith("]"));122123b.setProperty("p0", "0");124assertEquals(bEntrySet.size(), 3);125assertTrue(bEntrySet.toString().contains("p0=0"));126127b.remove("p1");128assertEquals(bEntrySet.size(), 2);129assertFalse(bEntrySet.toString().contains("p1=1"));130assertTrue(bEntrySet.toString().trim().startsWith("["));131assertTrue(bEntrySet.toString().contains("p0=0"));132assertTrue(bEntrySet.toString().contains("p2=2"));133assertTrue(bEntrySet.toString().trim().endsWith("]"));134135b.remove("p0", "0");136assertEquals(bEntrySet.size(), 1);137assertFalse(bEntrySet.toString().contains("p0=0"));138assertTrue(bEntrySet.toString().trim().startsWith("["));139assertTrue(bEntrySet.toString().contains("p2=2"));140assertTrue(bEntrySet.toString().trim().endsWith("]"));141142b.clear();143assertTrue(bEntrySet.isEmpty());144assertTrue(bEntrySet.toString().equals("[]"));145}146147@Test148public void testEntrySetWithoutException() {149Properties a = new Properties();150a.setProperty("p1", "1");151a.setProperty("p2", "2");152var aEntrySet = a.entrySet();153assertEquals(aEntrySet.size(), 2);154155var i = aEntrySet.iterator();156var e1 = i.next();157i.remove();158assertFalse(aEntrySet.contains(e1));159assertEquals(aEntrySet.size(), 1);160161var e2 = i.next();162aEntrySet.remove(e2);163assertFalse(aEntrySet.contains(e2));164assertTrue(aEntrySet.isEmpty());165166a.setProperty("p1", "1");167a.setProperty("p3", "3");168Properties b = new Properties();169b.setProperty("p2", "2");170b.setProperty("p1", "1");171var bEntrySet = b.entrySet();172173assertFalse(bEntrySet.containsAll(aEntrySet));174assertEquals(bEntrySet.size(), 2);175176assertTrue(bEntrySet.removeAll(aEntrySet));177assertEquals(bEntrySet.size(), 1);178179assertTrue(bEntrySet.retainAll(aEntrySet));180assertTrue(bEntrySet.isEmpty());181assertEquals(aEntrySet.size(), 2);182183aEntrySet.clear();184assertTrue(aEntrySet.isEmpty());185}186187@Test188public void testEntrySetExceptionWhenAdd() {189Properties a = new Properties();190a.setProperty("p1", "1");191var aEntrySet = a.entrySet();192193Properties b = new Properties();194b.setProperty("p2", "2");195var bEntrySet = b.entrySet();196197assertThrows(UnsupportedOperationException.class, () -> aEntrySet.addAll(bEntrySet));198assertThrows(UnsupportedOperationException.class, () -> aEntrySet.add(bEntrySet.iterator().next()));199}200}201202203