1 /* 2 * Copyright (c) 2018, 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 4077980 4011163 4096786 4075955 27 * @summary Test properties save and load methods 28 * @key randomness 29 */ 30 31 package test.java.util.Properties; 32 33 import java.io.File; 34 import java.io.FileInputStream; 35 import java.io.FileOutputStream; 36 import java.io.InputStream; 37 import java.io.OutputStream; 38 import java.util.Properties; 39 import java.util.Random; 40 41 /** 42 * This class tests to see if a properties object 43 * can successfully save and load properties 44 * using character encoding 45 */ 46 public class SaveLoadBasher { 47 48 private static String keyValueSeparators = "=: \t\r\n\f#!\\"; 49 main(String[] args)50 public static void main(String[] args) throws Exception { 51 52 Properties originalProps = new Properties(); 53 Properties loadedProps = new Properties(); 54 55 // Generate a unicode key and value 56 Random generator = new Random(); 57 int achar=0; 58 StringBuffer aKeyBuffer = new StringBuffer(120); 59 StringBuffer aValueBuffer = new StringBuffer(120); 60 String aKey; 61 String aValue; 62 for (int x=0; x<300; x++) { 63 for(int y=0; y<100; y++) { 64 achar = generator.nextInt(); 65 char test; 66 if(achar < 99999) { 67 test = (char)(achar); 68 } 69 else { 70 int zz = achar % 10; 71 test = keyValueSeparators.charAt(zz); 72 } 73 aKeyBuffer.append(test); 74 } 75 aKey = aKeyBuffer.toString(); 76 for(int y=0; y<100; y++) { 77 achar = generator.nextInt(); 78 char test = (char)(achar); 79 aValueBuffer.append(test); 80 } 81 aValue = aValueBuffer.toString(); 82 83 // Attempt to add to original 84 try { 85 originalProps.put(aKey, aValue); 86 } 87 catch (IllegalArgumentException e) { 88 System.err.println("disallowing..."); 89 } 90 aKeyBuffer.setLength(0); 91 aValueBuffer.setLength(0); 92 } 93 94 // Destroy old test file if it exists 95 // Android-changed: read/write from/to a temp file. 96 // File oldTestFile = new File("props3"); 97 File oldTestFile = File.createTempFile("props3", "properties"); 98 oldTestFile.delete(); 99 100 // Save original 101 System.out.println("Saving..."); 102 // Android-changed: read/write from/to a temp file. 103 // OutputStream out = new FileOutputStream("props3"); 104 OutputStream out = new FileOutputStream(oldTestFile); 105 originalProps.store(out, "test properties"); 106 out.close(); 107 108 // Load in the set 109 System.out.println("Loading..."); 110 // Android-changed: read/write from/to a temp file. 111 // InputStream in = new FileInputStream("props3"); 112 InputStream in = new FileInputStream(oldTestFile); 113 try { 114 loadedProps.load(in); 115 } finally { 116 in.close(); 117 } 118 119 // Compare results 120 if (!originalProps.equals(loadedProps)) 121 throw new RuntimeException("Properties load and save failed"); 122 123 } 124 125 } 126