Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/xml/jaxp/transform/8167179/NamespacePrefixTest.java
38859 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.StringReader;24import java.io.StringWriter;25import java.util.concurrent.CyclicBarrier;26import java.util.concurrent.ExecutorService;27import java.util.concurrent.Executors;28import java.util.concurrent.TimeUnit;29import java.util.concurrent.atomic.AtomicBoolean;3031import javax.xml.transform.Source;32import javax.xml.transform.Templates;33import javax.xml.transform.Transformer;34import javax.xml.transform.TransformerFactory;35import javax.xml.transform.stream.StreamResult;36import javax.xml.transform.stream.StreamSource;3738import org.testng.annotations.Test;39import static org.testng.Assert.assertTrue;4041/*42* @test43* @bug 816717944* @run testng/othervm NamespacePrefixTest45* @summary This class tests the generation of namespace prefixes46*/47public class NamespacePrefixTest {4849@Test50public void testReuseTemplates() throws Exception {51final TransformerFactory tf = TransformerFactory.newInstance();52final Source xslsrc = new StreamSource(new StringReader(XSL));53final Templates tmpl = tf.newTemplates(xslsrc);54for (int i = 0; i < TRANSF_COUNT; i++) {55checkResult(doTransformation(tmpl.newTransformer()));56}57}5859@Test60public void testReuseTransformer() throws Exception {61final TransformerFactory tf = TransformerFactory.newInstance();62final Source xslsrc = new StreamSource(new StringReader(XSL));63final Transformer t = tf.newTransformer(xslsrc);64for (int i = 0; i < TRANSF_COUNT; i++) {65checkResult(doTransformation(t));66}67}6869@Test70public void testConcurrentTransformations() throws Exception {71final TransformerFactory tf = TransformerFactory.newInstance();72final Source xslsrc = new StreamSource(new StringReader(XSL));73final Templates tmpl = tf.newTemplates(xslsrc);74concurrentTestPassed.set(true);7576// Execute multiple TestWorker tasks77for (int id = 0; id < THREADS_COUNT; id++) {78EXECUTOR.execute(new TransformerThread(tmpl.newTransformer(), id));79}80// Initiate shutdown of previously submitted task81EXECUTOR.shutdown();82// Wait for termination of submitted tasks83if (!EXECUTOR.awaitTermination(THREADS_COUNT, TimeUnit.SECONDS)) {84// If not all tasks terminates during the time out force them to shutdown85EXECUTOR.shutdownNow();86}87// Check if all transformation threads generated the correct namespace prefix88assertTrue(concurrentTestPassed.get());89}9091// Do one transformation with the provided transformer92private static String doTransformation(Transformer t) throws Exception {93StringWriter resWriter = new StringWriter();94Source xmlSrc = new StreamSource(new StringReader(XML));95t.transform(xmlSrc, new StreamResult(resWriter));96return resWriter.toString();97}9899// Check if the transformation result string contains the100// element with the exact namespace prefix generated.101private static void checkResult(String result) {102// Check prefix of 'Element2' element, it should always be the same103assertTrue(result.contains(EXPECTED_CONTENT));104}105106// Check if the transformation result string contains the element with107// the exact namespace prefix generated by current thread.108// If the expected prefix is not found and there was no failures observed by109// other test threads then mark concurrent test as failed.110private static void checkThreadResult(String result, int id) {111boolean res = result.contains(EXPECTED_CONTENT);112System.out.printf("%d: transformation result: %s%n", id, res ? "Pass" : "Fail");113if (!res) {114System.out.printf("%d result:%s%n", id, result);115}116concurrentTestPassed.compareAndSet(true, res);117}118119// TransformerThread task that does the transformation similar120// to testReuseTransformer test method121private class TransformerThread implements Runnable {122123private final Transformer transformer;124private final int id;125126TransformerThread(Transformer transformer, int id) {127this.transformer = transformer;128this.id = id;129}130131@Override132public void run() {133try {134System.out.printf("%d: waiting for barrier%n", id);135//Synchronize startup of all tasks136BARRIER.await();137System.out.printf("%d: starting transformation%n", id);138checkThreadResult(doTransformation(transformer), id);139} catch (Exception ex) {140throw new RuntimeException("TransformerThread " + id + " failed", ex);141}142}143}144145// Number of subsequent transformations146private static final int TRANSF_COUNT = 10;147148// Number of transformer threads running concurently149private static final int THREADS_COUNT = 10;150151// Variable for storing the concurrent transformation test result. It is152// updated by transformer threads153private static final AtomicBoolean concurrentTestPassed = new AtomicBoolean(true);154155// Cyclic barrier for threads startup synchronization156private static final CyclicBarrier BARRIER = new CyclicBarrier(THREADS_COUNT);157158// Thread pool159private static final ExecutorService EXECUTOR = Executors.newCachedThreadPool();160161// XSL that transforms XML and produces unique namespace prefixes for each element162private final static String XSL = "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">\n"163+ " <xsl:template match=\"node()|@*\" priority=\"1\">\n"164+ " <xsl:copy>\n"165+ " <xsl:apply-templates select=\"node()|@*\"/>\n"166+ " </xsl:copy>\n"167+ " </xsl:template>\n"168+ " <xsl:template match=\"*\" priority=\"2\">\n"169+ " <xsl:element name=\"{name()}\" namespace=\"{namespace-uri()}\">\n"170+ " <xsl:apply-templates select=\"node()|@*\"/>\n"171+ " </xsl:element>\n"172+ " </xsl:template>\n"173+ "</xsl:stylesheet>";174175// Simple XML content with root and two child elements176private final static String XML = "<TestRoot xmlns=\"test.xmlns\">\n"177+ " <Element1 xmlns=\"test.xmlns\">\n"178+ " </Element1>\n"179+ " <Element2 xmlns=\"test.xmlns\">\n"180+ " </Element2>\n"181+ "</TestRoot>";182183// With thread local namespace prefix index each transformation result should184// be the same and contain the same prefix for Element2185private final static String EXPECTED_CONTENT = "</ns2:Element2>";186187}188189190