Path: blob/master/test/jdk/java/util/Properties/SaveLoadBasher.java
41152 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 4077980 4011163 4096786 407595526* @summary Test properties 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 properties41* using character encoding42*/43public class SaveLoadBasher {4445private static String keyValueSeparators = "=: \t\r\n\f#!\\";4647public static void main(String[] args) throws Exception {4849Properties originalProps = new Properties();50Properties loadedProps = new Properties();5152// Generate a unicode key and value53Random generator = new Random();54int achar=0;55StringBuffer aKeyBuffer = new StringBuffer(120);56StringBuffer aValueBuffer = new StringBuffer(120);57String aKey;58String aValue;59for (int x=0; x<300; x++) {60for(int y=0; y<100; y++) {61achar = generator.nextInt();62char test;63if(achar < 99999) {64test = (char)(achar);65}66else {67int zz = achar % 10;68test = keyValueSeparators.charAt(zz);69}70aKeyBuffer.append(test);71}72aKey = aKeyBuffer.toString();73for(int y=0; y<100; y++) {74achar = generator.nextInt();75char test = (char)(achar);76aValueBuffer.append(test);77}78aValue = aValueBuffer.toString();7980// Attempt to add to original81try {82originalProps.put(aKey, aValue);83}84catch (IllegalArgumentException e) {85System.err.println("disallowing...");86}87aKeyBuffer.setLength(0);88aValueBuffer.setLength(0);89}9091// Destroy old test file if it exists92File oldTestFile = new File("props3");93oldTestFile.delete();9495// Save original96System.out.println("Saving...");97OutputStream out = new FileOutputStream("props3");98originalProps.store(out, "test properties");99out.close();100101// Load in the set102System.out.println("Loading...");103InputStream in = new FileInputStream("props3");104try {105loadedProps.load(in);106} finally {107in.close();108}109110// Compare results111if (!originalProps.equals(loadedProps))112throw new RuntimeException("Properties load and save failed");113114}115116}117118119