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/transform/8167179/NamespacePrefixTest.java
38859 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.StringReader;
25
import java.io.StringWriter;
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.concurrent.atomic.AtomicBoolean;
31
32
import javax.xml.transform.Source;
33
import javax.xml.transform.Templates;
34
import javax.xml.transform.Transformer;
35
import javax.xml.transform.TransformerFactory;
36
import javax.xml.transform.stream.StreamResult;
37
import javax.xml.transform.stream.StreamSource;
38
39
import org.testng.annotations.Test;
40
import static org.testng.Assert.assertTrue;
41
42
/*
43
* @test
44
* @bug 8167179
45
* @run testng/othervm NamespacePrefixTest
46
* @summary This class tests the generation of namespace prefixes
47
*/
48
public class NamespacePrefixTest {
49
50
@Test
51
public void testReuseTemplates() throws Exception {
52
final TransformerFactory tf = TransformerFactory.newInstance();
53
final Source xslsrc = new StreamSource(new StringReader(XSL));
54
final Templates tmpl = tf.newTemplates(xslsrc);
55
for (int i = 0; i < TRANSF_COUNT; i++) {
56
checkResult(doTransformation(tmpl.newTransformer()));
57
}
58
}
59
60
@Test
61
public void testReuseTransformer() throws Exception {
62
final TransformerFactory tf = TransformerFactory.newInstance();
63
final Source xslsrc = new StreamSource(new StringReader(XSL));
64
final Transformer t = tf.newTransformer(xslsrc);
65
for (int i = 0; i < TRANSF_COUNT; i++) {
66
checkResult(doTransformation(t));
67
}
68
}
69
70
@Test
71
public void testConcurrentTransformations() throws Exception {
72
final TransformerFactory tf = TransformerFactory.newInstance();
73
final Source xslsrc = new StreamSource(new StringReader(XSL));
74
final Templates tmpl = tf.newTemplates(xslsrc);
75
concurrentTestPassed.set(true);
76
77
// Execute multiple TestWorker tasks
78
for (int id = 0; id < THREADS_COUNT; id++) {
79
EXECUTOR.execute(new TransformerThread(tmpl.newTransformer(), id));
80
}
81
// Initiate shutdown of previously submitted task
82
EXECUTOR.shutdown();
83
// Wait for termination of submitted tasks
84
if (!EXECUTOR.awaitTermination(THREADS_COUNT, TimeUnit.SECONDS)) {
85
// If not all tasks terminates during the time out force them to shutdown
86
EXECUTOR.shutdownNow();
87
}
88
// Check if all transformation threads generated the correct namespace prefix
89
assertTrue(concurrentTestPassed.get());
90
}
91
92
// Do one transformation with the provided transformer
93
private static String doTransformation(Transformer t) throws Exception {
94
StringWriter resWriter = new StringWriter();
95
Source xmlSrc = new StreamSource(new StringReader(XML));
96
t.transform(xmlSrc, new StreamResult(resWriter));
97
return resWriter.toString();
98
}
99
100
// Check if the transformation result string contains the
101
// element with the exact namespace prefix generated.
102
private static void checkResult(String result) {
103
// Check prefix of 'Element2' element, it should always be the same
104
assertTrue(result.contains(EXPECTED_CONTENT));
105
}
106
107
// Check if the transformation result string contains the element with
108
// the exact namespace prefix generated by current thread.
109
// If the expected prefix is not found and there was no failures observed by
110
// other test threads then mark concurrent test as failed.
111
private static void checkThreadResult(String result, int id) {
112
boolean res = result.contains(EXPECTED_CONTENT);
113
System.out.printf("%d: transformation result: %s%n", id, res ? "Pass" : "Fail");
114
if (!res) {
115
System.out.printf("%d result:%s%n", id, result);
116
}
117
concurrentTestPassed.compareAndSet(true, res);
118
}
119
120
// TransformerThread task that does the transformation similar
121
// to testReuseTransformer test method
122
private class TransformerThread implements Runnable {
123
124
private final Transformer transformer;
125
private final int id;
126
127
TransformerThread(Transformer transformer, int id) {
128
this.transformer = transformer;
129
this.id = id;
130
}
131
132
@Override
133
public void run() {
134
try {
135
System.out.printf("%d: waiting for barrier%n", id);
136
//Synchronize startup of all tasks
137
BARRIER.await();
138
System.out.printf("%d: starting transformation%n", id);
139
checkThreadResult(doTransformation(transformer), id);
140
} catch (Exception ex) {
141
throw new RuntimeException("TransformerThread " + id + " failed", ex);
142
}
143
}
144
}
145
146
// Number of subsequent transformations
147
private static final int TRANSF_COUNT = 10;
148
149
// Number of transformer threads running concurently
150
private static final int THREADS_COUNT = 10;
151
152
// Variable for storing the concurrent transformation test result. It is
153
// updated by transformer threads
154
private static final AtomicBoolean concurrentTestPassed = new AtomicBoolean(true);
155
156
// Cyclic barrier for threads startup synchronization
157
private static final CyclicBarrier BARRIER = new CyclicBarrier(THREADS_COUNT);
158
159
// Thread pool
160
private static final ExecutorService EXECUTOR = Executors.newCachedThreadPool();
161
162
// XSL that transforms XML and produces unique namespace prefixes for each element
163
private final static String XSL = "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">\n"
164
+ " <xsl:template match=\"node()|@*\" priority=\"1\">\n"
165
+ " <xsl:copy>\n"
166
+ " <xsl:apply-templates select=\"node()|@*\"/>\n"
167
+ " </xsl:copy>\n"
168
+ " </xsl:template>\n"
169
+ " <xsl:template match=\"*\" priority=\"2\">\n"
170
+ " <xsl:element name=\"{name()}\" namespace=\"{namespace-uri()}\">\n"
171
+ " <xsl:apply-templates select=\"node()|@*\"/>\n"
172
+ " </xsl:element>\n"
173
+ " </xsl:template>\n"
174
+ "</xsl:stylesheet>";
175
176
// Simple XML content with root and two child elements
177
private final static String XML = "<TestRoot xmlns=\"test.xmlns\">\n"
178
+ " <Element1 xmlns=\"test.xmlns\">\n"
179
+ " </Element1>\n"
180
+ " <Element2 xmlns=\"test.xmlns\">\n"
181
+ " </Element2>\n"
182
+ "</TestRoot>";
183
184
// With thread local namespace prefix index each transformation result should
185
// be the same and contain the same prefix for Element2
186
private final static String EXPECTED_CONTENT = "</ns2:Element2>";
187
188
}
189
190