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