Path: blob/master/test/jdk/javax/xml/jaxp/transform/8062923/XslSubstringTest.java
41154 views
/*1* Copyright (c) 2015, 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*/2223/**24* @test25* @bug 8062923 8062924 8074297 807629026* @run testng XslSubstringTest27* @summary Test xsl substring function with negative, Inf and28* NaN length and few other use cases. Also test proper29* processing of supplementary characters by substring function.30*/3132import java.io.StringReader;33import java.io.StringWriter;34import javax.xml.transform.Source;35import javax.xml.transform.Templates;36import javax.xml.transform.Transformer;37import javax.xml.transform.TransformerFactory;38import javax.xml.transform.stream.StreamResult;39import javax.xml.transform.stream.StreamSource;4041import static org.testng.Assert.assertEquals;42import org.testng.annotations.DataProvider;43import org.testng.annotations.Test;4445public class XslSubstringTest {4647final String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><test></test>";48final String xslPre = "<xsl:stylesheet version='1.0'"49+ " xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>"50+ "<xsl:output method='xml' indent='yes' omit-xml-declaration='yes'/>"51+ "<xsl:template match='/'><t>";52final String xslPost = "</t></xsl:template></xsl:stylesheet>";5354@DataProvider(name = "GeneralTestsData")55private Object[][] xmls() {56return new Object[][] {57{ "|<xsl:value-of select=\"substring('asdf',2, 1)\"/>|", "<t>|s|</t>"},58{ "|<xsl:value-of select=\"substring('asdf',2, 1 div 0)\"/>|", "<t>|sdf|</t>"},59{ "|<xsl:value-of select=\"substring('asdf',2, -0 div 0)\"/>|", "<t>||</t>" },60{ "|<xsl:value-of select=\"substring('asdf',2, 1 div 0)\"/>|", "<t>|sdf|</t>" },61// 8076290 bug test case62{ "|<xsl:value-of select=\"substring('123', 0, 3)\"/>|", "<t>|12|</t>"},63};64}6566@DataProvider(name = "SupplementaryCharactersTestData")67private Object[][] dataSupplementaryCharacters() {68return new Object[][] {69// 8074297 bug test cases70{ "|<xsl:value-of select=\"substring('𠀋ABC', 3)\"/>|", "<t>|BC|</t>"},71{ "|<xsl:value-of select=\"substring('𠀋ABC', 3, 1)\"/>|", "<t>|B|</t>" },72{ "|<xsl:value-of select=\"substring('𠀋ABC', 2, 2)\"/>|", "<t>|AB|</t>"},73{ "|<xsl:value-of select=\"substring('𠀋ABC', 3, 2)\"/>|", "<t>|BC|</t>"},74{ "|<xsl:value-of select=\"substring('𠀋ABC', 3, 4)\"/>|", "<t>|BC|</t>"},75{ "|<xsl:value-of select=\"substring('𠀋ABC', 1, 1)\"/>|", "<t>|𠀋|</t>"},76{ "|<xsl:value-of select=\"substring('𠀋ABC', 2, 1)\"/>|", "<t>|A|</t>"},77{ "|<xsl:value-of select=\"substring('𠀋ABC', 1, 1 div 0)\"/>|", "<t>|𠀋ABC|</t>"},78{ "|<xsl:value-of select=\"substring('𠀋ABC', -10, 1 div 0)\"/>|", "<t>|𠀋ABC|</t>"},79// 8076290 bug test case80{ "|<xsl:value-of select=\"substring('𠀋ABC', 0, 2)\"/>|", "<t>|𠀋|</t>"},81};82}8384private String testTransform(String xsl) throws Exception {85//Prepare sources for transormation86Source src = new StreamSource(new StringReader(xml));87Source xslsrc = new StreamSource(new StringReader(xslPre + xsl + xslPost));88//Create factory, template and transformer89TransformerFactory tf = TransformerFactory.newInstance();90Templates tmpl = tf.newTemplates(xslsrc);91Transformer t = tmpl.newTransformer();92//Prepare output stream93StringWriter xmlResultString = new StringWriter();94StreamResult xmlResultStream = new StreamResult(xmlResultString);95//Transform96t.transform(src, xmlResultStream);97return xmlResultString.toString().trim();98}99100@Test101public void test8062923() throws Exception {102assertEquals(testTransform("|<xsl:value-of select=\"substring('asdf',2,-1)\"/>|"),103"<t>||</t>");104}105106@Test107public void test8062924() throws Exception {108assertEquals(testTransform("|<xsl:value-of select=\"substring('asdf',2,-1 div 0)\"/>|"),109"<t>||</t>");110}111112@Test(dataProvider = "GeneralTestsData")113public void testGeneralAll(String xsl, String result) throws Exception {114assertEquals(testTransform(xsl), result);115}116117@Test(dataProvider = "SupplementaryCharactersTestData")118public void testSupplementCharacters(String xsl, String result) throws Exception {119assertEquals(testTransform(xsl), result);120}121122}123124125