Path: blob/master/test/jdk/java/util/Properties/StringPropertyNames.java
41149 views
/*1* Copyright (c) 2018, 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 6253413 805936126* @summary Test for Properties.stringPropertyNames() if the system27* properties contain another list of properties as the defaults.28* @author Mandy Chung29*30* @run build StringPropertyNames31* @run main StringPropertyNames32*/3334import java.util.Properties;35import java.util.Enumeration;36import java.util.Iterator;37import java.util.Set;3839public class StringPropertyNames {40private static int NUM_SHARE_PROPS = 2;41private static int NUM_PROPS1 = 3;42private static int NUM_PROPS2 = 5;43private static String KEY = "good.property.";44private static String VALUE = "good.value.";45public static void main(String[] argv) throws Exception {46Properties props1 = new Properties();47Properties props2 = new Properties(props1);4849// add several new properties50for (int i = 0; i < NUM_PROPS1; i++) {51props1.put(KEY + "1." + i, VALUE + "1." + i);52}53for (int i = 0; i < NUM_PROPS2; i++) {54props2.put(KEY + "2." + i, VALUE + "2." + i);55}5657// add the same properties in both props1 and props258for (int i = 0; i < NUM_SHARE_PROPS; i++) {59props1.put(KEY + i, VALUE + "1." + i);60props2.put(KEY + i, VALUE + "2." + i);61}62checkProperties(props1,63NUM_PROPS1 + NUM_SHARE_PROPS, // size of props164NUM_PROPS1 + NUM_SHARE_PROPS, // num of string keys65NUM_PROPS1 + NUM_SHARE_PROPS, // num of keys in propertyName(),66false);67checkProperties(props2,68NUM_PROPS2 + NUM_SHARE_PROPS, // size of props269NUM_PROPS1 + NUM_PROPS2 + NUM_SHARE_PROPS, // num of string keys70NUM_PROPS1 + NUM_PROPS2 + NUM_SHARE_PROPS, // num of keys in propertyName(),71false);7273// Add non-String value74props1.put(KEY + "9", new Integer(4));75checkProperties(props1,76NUM_PROPS1 + NUM_SHARE_PROPS + 1, // size of props177NUM_PROPS1 + NUM_SHARE_PROPS, // num of string keys78NUM_PROPS1 + NUM_SHARE_PROPS + 1, // num of keys in propertyName(),79false);80checkProperties(props2,81NUM_PROPS2 + NUM_SHARE_PROPS, // size of props282NUM_PROPS1 + NUM_PROPS2 + NUM_SHARE_PROPS, // num of string keys83NUM_PROPS1 + NUM_PROPS2 + NUM_SHARE_PROPS + 1, // num of keys in propertyName(),84false);85Object v = props1.remove(KEY + "9");86if (v == null) {87throw new RuntimeException("Test Failed: " +88"Key " + KEY + "9" + " not found");89}9091// Add a non-String key92props1.put(new Integer(5), "good.value.5");93props2.put(new Object(), new Object());94checkProperties(props1,95NUM_PROPS1 + NUM_SHARE_PROPS + 1, // size of props196NUM_PROPS1 + NUM_SHARE_PROPS, // num of string keys97NUM_PROPS1 + NUM_SHARE_PROPS + 1, // num of keys in propertyName(),98true);99checkProperties(props2,100NUM_PROPS2 + NUM_SHARE_PROPS + 1, // size of props2101NUM_PROPS1 + NUM_PROPS2 + NUM_SHARE_PROPS, // num of string keys102NUM_PROPS1 + NUM_PROPS2 + NUM_SHARE_PROPS + 2, // num of keys in propertyName(),103true);104System.out.println("Test passed.");105}106107private static void checkProperties(Properties props,108int propSize,109int numStringKeys,110int enumerateSize,111boolean hasNonStringKeys) {112// check the size of the properties113if (props.size() != propSize) {114throw new RuntimeException("Test Failed: " +115"Expected number of properties = " +116propSize + " but found = " + props.size());117}118119// check the number of properties whose key and value120// are both strings121Set<String> keys = props.stringPropertyNames();122if (keys.size() != numStringKeys) {123throw new RuntimeException("Test Failed: " +124"Expected number of String keys = " +125numStringKeys + " but found = " + keys.size());126}127boolean cceThrown = false;128try {129// check the number of properties whose key are strings130// but its value can be anything in the current impl131int count = 0;132Enumeration<?> e = props.propertyNames();133for (;e.hasMoreElements(); e.nextElement()) {134count++;135}136if (count != enumerateSize) {137throw new RuntimeException("Test Failed: " +138"Expected number of enumerated keys = " +139enumerateSize + " but found = " + count);140}141} catch (ClassCastException e) {142if (!hasNonStringKeys) {143RuntimeException re = new RuntimeException("Test Failed: " +144"ClassCastException is expected not to be thrown");145re.initCause(e);146throw re;147}148cceThrown = true;149}150151if ((hasNonStringKeys && !cceThrown)) {152throw new RuntimeException("Test Failed: " +153"ClassCastException is expected to be thrown");154}155156// make sure the set cannot be modified157try {158keys.add("xyzzy");159throw new RuntimeException("Test Failed: " +160"add() should have thrown UnsupportedOperationException");161} catch (UnsupportedOperationException ignore) { }162163Iterator<String> it = keys.iterator();164if (it.hasNext()) {165try {166keys.remove(it.next());167throw new RuntimeException("Test Failed: " +168"remove() should have thrown UnsupportedOperationException");169} catch (UnsupportedOperationException ignore) { }170}171}172}173174175