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 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 } | 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 */ 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 */ 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 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 112 this.id = id; 113 } 114 115 @Override 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 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 } |