Path: blob/master/test/jdk/java/util/Properties/XMLSaveLoadBasher.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 4749531 5015114 505573826* @summary Test properties XML save and load methods27* @key randomness28*/2930import java.io.File;31import java.io.FileInputStream;32import java.io.FileOutputStream;33import java.io.InputStream;34import java.io.OutputStream;35import java.util.Properties;36import java.util.Random;3738/**39* This class tests to see if a properties object40* can successfully save and load properties in XML41*/42public class XMLSaveLoadBasher {4344private static final int MAX_KEY_SIZE = 120;45private static final int MIN_KEY_SIZE = 1;46private static final int MAX_VALUE_SIZE = 120;47private static final int MIN_VALUE_SIZE = 0;4849public static void main(String[] args) throws Exception {50testSaveLoad("UTF-8", "test save");51testSaveLoad("UTF-8", null);52testSaveLoad("ISO-8859-1", "test save");53testSaveLoad("KOI8-R", "test save");54}5556private static void testSaveLoad(String encoding, String comment)57throws Exception58{59Properties originalProps = new Properties();60Properties loadedProps = new Properties();6162// Generate a unicode key and value63Random generator = new Random();6465for (int x=0; x<10; x++) {66String aKey;67String aValue;6869// Assumes MAX_KEY_SIZE >> MIN_KEY_SIZE70int keyLen = generator.nextInt(MAX_KEY_SIZE - MIN_KEY_SIZE + 1) +71MIN_KEY_SIZE;72int valLen = generator.nextInt(73MAX_VALUE_SIZE - MIN_VALUE_SIZE + 1) + MIN_VALUE_SIZE;7475StringBuffer aKeyBuffer = new StringBuffer(keyLen);76StringBuffer aValueBuffer = new StringBuffer(valLen);7778for(int y=0; y<keyLen; y++) {79char test = (char)(generator.nextInt(6527) + 32);80aKeyBuffer.append(test);81}82aKey = aKeyBuffer.toString();8384for(int y=0; y<valLen; y++) {85char test = (char)(generator.nextInt(6527) + 32);86aValueBuffer.append(test);87}88aValue = aValueBuffer.toString();8990// Attempt to add to original91try {92originalProps.setProperty(aKey, aValue);93} catch (IllegalArgumentException e) {94System.err.println("disallowing...");95}96}9798//originalProps.put("squid", "kraken");99//originalProps.put("demon", "furnace");100101// Destroy old test file if it exists102File oldTestFile = new File("props3");103oldTestFile.delete();104105// Save original106System.err.println("Saving...");107try (OutputStream out = new FileOutputStream("props3")) {108originalProps.storeToXML(out, comment, encoding);109}110111// Load in the set112System.err.println("Loading...");113try (InputStream in = new FileInputStream("props3")) {114loadedProps.loadFromXML(in);115}116117// Compare results118if (!originalProps.equals(loadedProps))119throw new RuntimeException("Properties load and save failed");120121}122}123124125