1 /* 2 * Copyright (c) 2016, 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 package common; 25 26 import java.io.ByteArrayOutputStream; 27 import java.io.PrintStream; 28 import java.util.concurrent.CyclicBarrier; 29 import java.util.concurrent.ExecutorService; 30 import java.util.concurrent.Executors; 31 import java.util.concurrent.TimeUnit; 32 import java.util.regex.Matcher; 33 import java.util.regex.Pattern; 34 import javax.xml.XMLConstants; 35 import org.testng.Assert; 36 37 /* 38 * This class helps to test suppression of unsupported parser properties 39 * messages printed to standard error output. 40 * It launches THREADS_COUNT tasks. Each task does ITERATIONS_PER_THREAD 41 * sequential calls to doOneIteration method implemented by specific test class. 42 */ 43 public abstract class WarningsTestBase { 44 45 /* 46 * Abstract method that should be implemented by test class. 47 * It is repeatedly called by each TestWorker task. 48 */ 49 abstract void doOneTestIteration() throws Exception; 50 51 /* 52 * Launches parallel test tasks and check the output for the number of 53 * generated warning messages. There should be no more than one message of 54 * each type. 55 */ 56 void startTest() throws Exception { 57 //Save standard error stream 58 PrintStream defStdErr = System.err; 59 //Set new byte array stream as standard error stream 60 ByteArrayOutputStream byteStream = new ByteArrayOutputStream(5000); 61 System.setErr(new PrintStream(byteStream)); 62 //Execute multiple TestWorker tasks 63 for (int id = 0; id < THREADS_COUNT; id++) { 64 EXECUTOR.execute(new TestWorker(id)); 65 } 66 //Initiate shutdown of previously submitted task 67 EXECUTOR.shutdown(); 68 //Wait for termination of submitted tasks 69 if (!EXECUTOR.awaitTermination(THREADS_COUNT, TimeUnit.SECONDS)) { 70 //If not all tasks terminates during the time out force them to shutdown 71 EXECUTOR.shutdownNow(); 72 } 73 //Restore default standard error stream 74 System.setErr(defStdErr); 75 //Print tasks stderr output 76 String errContent = byteStream.toString(); 77 System.out.println("Standard error output content:"); 78 System.out.println(errContent); 79 //Check tasks stderr output for quatity of warning messages 80 Assert.assertTrue(warningPrintedOnce(XMLConstants.ACCESS_EXTERNAL_DTD, errContent)); 81 Assert.assertTrue(warningPrintedOnce(ENT_EXP_PROPERTY, errContent)); 82 Assert.assertTrue(warningPrintedOnce(XMLConstants.FEATURE_SECURE_PROCESSING, errContent)); 83 } 84 85 // Count occurences of warning messages in standard error and check if warning is printed 86 // not more than once 87 private boolean warningPrintedOnce(String propertyName, String testOutput) { 88 //Count for property name in test output 89 Pattern p = Pattern.compile(propertyName); 90 Matcher m = p.matcher(testOutput); 91 int count = 0; 92 while (m.find()) { 93 count += 1; 94 } 95 System.out.println("'" + propertyName + "' print count: " + count); 96 //If count is more than 1 then consider test failed 97 return count <= 1; 98 } 99 100 //TestWorker task that sequentially calls test method 101 private class TestWorker implements Runnable { 102 // Task id 103 private final int id; 104 105 TestWorker(int id) { 106 this.id = id; 107 } 108 109 @Override 110 public void run() { 111 try { 112 System.out.printf("%d: waiting for barrier%n", id); 113 //Synchronize startup of all tasks 114 BARRIER.await(); 115 System.out.printf("%d: starting iterations%n", id); 116 //Call test method multiple times 117 for (int i = 0; i < ITERATIONS_PER_THREAD; i++) { 118 doOneTestIteration(); 119 } 120 } catch (Exception ex) { 121 throw new RuntimeException("TestWorker id:" + id + " failed", ex); 122 } 123 } 124 } 125 126 //Entity expansion limit property name 127 private static final String ENT_EXP_PROPERTY = "http://www.oracle.com/xml/jaxp/properties/entityExpansionLimit"; 128 //Number of simultaneous test threads 129 private static final int THREADS_COUNT = 10; 130 //Number of iterations per one thread 131 private static final int ITERATIONS_PER_THREAD = 4; 132 //Test thread pool 133 private static final ExecutorService EXECUTOR = Executors.newCachedThreadPool(); 134 //Cyclic barrier for threads startup synchronisation 135 private static final CyclicBarrier BARRIER = new CyclicBarrier(THREADS_COUNT); 136 }