1 /* 2 * Copyright (c) 2012, 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 /* @test 25 * @bug 8005281 26 * @summary Test that the Properties storeToXML and loadFromXML methods are 27 * thread safe 28 * @key randomness 29 */ 30 31 import java.io.*; 32 import java.util.Properties; 33 import java.util.Random; 34 import java.util.concurrent.Callable; 35 import java.util.concurrent.Executors; 36 import java.util.concurrent.ExecutorService; 37 import java.util.concurrent.Future; 38 39 public class ConcurrentLoadAndStoreXML { 40 41 static final Random RAND = new Random(); 42 43 static volatile boolean done; 44 45 /** 46 * Simple task that bashes on storeToXML and loadFromXML until the "done" 47 * flag is set. 48 */ 49 static class Basher implements Callable<Void> { 50 final Properties props; 51 52 Basher(Properties props) { 53 this.props = props; 54 } 55 56 public Void call() throws IOException { 57 while (!done) { 58 59 // store as XML format 60 ByteArrayOutputStream out = new ByteArrayOutputStream(); 61 props.storeToXML(out, null, "UTF-8"); 62 63 // load from XML format 64 Properties p = new Properties(); 65 ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); 66 p.loadFromXML(in); 67 68 // check that the properties are as expected 69 if (!p.equals(props)) 70 throw new RuntimeException("Properties not equal"); 71 } 72 return null; 73 } 74 } 75 76 public static void main(String[] args) throws Exception { 77 final int NTASKS = 4 + RAND.nextInt(4); 78 79 // create Bashers with Properties of random keys and values 80 Basher[] basher = new Basher[NTASKS]; 81 for (int i=0; i<NTASKS; i++) { 82 Properties props = new Properties(); 83 for (int j=0; j<RAND.nextInt(100); j++) { 84 String key = "k" + RAND.nextInt(1000); 85 String value = "v" + RAND.nextInt(1000); 86 props.put(key, value); 87 } 88 basher[i] = new Basher(props); 89 } 90 91 ExecutorService pool = Executors.newFixedThreadPool(NTASKS); 92 try { 93 // kick off the bashers 94 Future<Void>[] task = new Future[NTASKS]; 95 for (int i=0; i<NTASKS; i++) { 96 task[i] = pool.submit(basher[i]); 97 } 98 99 // give them time to interfere with each each 100 Thread.sleep(2000); 101 done = true; 102 103 // check the result 104 for (int i=0; i<NTASKS; i++) { 105 task[i].get(); 106 } 107 } finally { 108 done = true; 109 pool.shutdown(); 110 } 111 112 } 113 }