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