Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/xml/jaxp/common/8144593/WarningsTestBase.java
38860 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.ByteArrayOutputStream;24import java.io.PrintStream;25import java.util.concurrent.CyclicBarrier;26import java.util.concurrent.ExecutorService;27import java.util.concurrent.Executors;28import java.util.concurrent.TimeUnit;29import java.util.regex.Matcher;30import java.util.regex.Pattern;31import javax.xml.XMLConstants;32import org.testng.Assert;3334/*35* This class helps to test suppression of unsupported parser properties36* messages printed to standard error output.37* It launches THREADS_COUNT tasks. Each task does ITERATIONS_PER_THREAD38* sequential calls to doOneIteration method implemented by specific test class.39*/40public abstract class WarningsTestBase {4142/*43* Abstract method that should be implemented by test class.44* It is repeatedly called by each TestWorker task.45*/46abstract void doOneTestIteration() throws Exception;4748/*49* Launches parallel test tasks and check the output for the number of50* generated warning messages. There should be no more than one message of51* each type.52*/53void startTest() throws Exception {54//Save standard error stream55PrintStream defStdErr = System.err;56//Set new byte array stream as standard error stream57ByteArrayOutputStream byteStream = new ByteArrayOutputStream(5000);58System.setErr(new PrintStream(byteStream));59//Execute multiple TestWorker tasks60for (int id = 0; id < THREADS_COUNT; id++) {61EXECUTOR.execute(new TestWorker(id));62}63//Initiate shutdown of previously submitted task64EXECUTOR.shutdown();65//Wait for termination of submitted tasks66if (!EXECUTOR.awaitTermination(THREADS_COUNT, TimeUnit.SECONDS)) {67//If not all tasks terminates during the time out force them to shutdown68EXECUTOR.shutdownNow();69}70//Restore default standard error stream71System.setErr(defStdErr);72//Print tasks stderr output73String errContent = byteStream.toString();74System.out.println("Standard error output content:");75System.out.println(errContent);76//Check tasks stderr output for quatity of warning messages77Assert.assertTrue(warningPrintedOnce(XMLConstants.ACCESS_EXTERNAL_DTD, errContent));78Assert.assertTrue(warningPrintedOnce(ENT_EXP_PROPERTY, errContent));79Assert.assertTrue(warningPrintedOnce(XMLConstants.FEATURE_SECURE_PROCESSING, errContent));80}8182// Count occurences of warning messages in standard error and check if warning is printed83// not more than once84private boolean warningPrintedOnce(String propertyName, String testOutput) {85//Count for property name in test output86Pattern p = Pattern.compile(propertyName);87Matcher m = p.matcher(testOutput);88int count = 0;89while (m.find()) {90count += 1;91}92System.out.println("'" + propertyName + "' print count: " + count);93//If count is more than 1 then consider test failed94return count <= 1;95}9697//TestWorker task that sequentially calls test method98private class TestWorker implements Runnable {99// Task id100private final int id;101102TestWorker(int id) {103this.id = id;104}105106@Override107public void run() {108try {109System.out.printf("%d: waiting for barrier%n", id);110//Synchronize startup of all tasks111BARRIER.await();112System.out.printf("%d: starting iterations%n", id);113//Call test method multiple times114for (int i = 0; i < ITERATIONS_PER_THREAD; i++) {115doOneTestIteration();116}117} catch (Exception ex) {118throw new RuntimeException("TestWorker id:" + id + " failed", ex);119}120}121}122123//Entity expansion limit property name124private static final String ENT_EXP_PROPERTY = "http://www.oracle.com/xml/jaxp/properties/entityExpansionLimit";125//Number of simultaneous test threads126private static final int THREADS_COUNT = 10;127//Number of iterations per one thread128private static final int ITERATIONS_PER_THREAD = 4;129//Test thread pool130private static final ExecutorService EXECUTOR = Executors.newCachedThreadPool();131//Cyclic barrier for threads startup synchronisation132private static final CyclicBarrier BARRIER = new CyclicBarrier(THREADS_COUNT);133}134135136