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