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