jdk/test/javax/xml/jaxp/transform/8062923/XslSubstringTest.java
changeset 29721 8fe81a803610
parent 28659 5ddbb06fc4d4
child 29738 ac0aa9ac7043
equal deleted inserted replaced
29720:374070bf0fdf 29721:8fe81a803610
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /**
    24 /**
    25  * @test
    25  * @test
    26  * @bug 8062923 8062924
    26  * @bug 8062923 8062924 8074297
    27  * @run testng XslSubstringTest
    27  * @run testng XslSubstringTest
    28  * @summary Test xsl substring function with negative, Inf and
    28  * @summary Test xsl substring function with negative, Inf and
    29  * NaN length and few other use cases
    29  * NaN length and few other use cases. Also test proper
       
    30  * processing of supplementary characters by substring function.
    30  */
    31  */
    31 
    32 
    32 import java.io.StringReader;
    33 import java.io.StringReader;
    33 import java.io.StringWriter;
    34 import java.io.StringWriter;
    34 import javax.xml.transform.Source;
    35 import javax.xml.transform.Source;
    37 import javax.xml.transform.TransformerFactory;
    38 import javax.xml.transform.TransformerFactory;
    38 import javax.xml.transform.stream.StreamResult;
    39 import javax.xml.transform.stream.StreamResult;
    39 import javax.xml.transform.stream.StreamSource;
    40 import javax.xml.transform.stream.StreamSource;
    40 
    41 
    41 import static org.testng.Assert.assertEquals;
    42 import static org.testng.Assert.assertEquals;
       
    43 import org.testng.annotations.DataProvider;
    42 import org.testng.annotations.Test;
    44 import org.testng.annotations.Test;
    43 
    45 
    44 public class XslSubstringTest {
    46 public class XslSubstringTest {
    45 
    47 
    46     final String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><test></test>";
    48     final String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><test></test>";
    47     final String xslPre = "<xsl:stylesheet version='1.0'"
    49     final String xslPre = "<xsl:stylesheet version='1.0'"
    48             + " xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>"
    50             + " xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>"
    49             + "<xsl:output method='xml' indent='yes' omit-xml-declaration='yes'/>"
    51             + "<xsl:output method='xml' indent='yes' omit-xml-declaration='yes'/>"
    50             + "<xsl:template match='/'><t>";
    52             + "<xsl:template match='/'><t>";
    51     final String xslPost = "</t></xsl:template></xsl:stylesheet>";
    53     final String xslPost = "</t></xsl:template></xsl:stylesheet>";
       
    54 
       
    55     @DataProvider(name = "GeneralTestsData")
       
    56     private Object[][] xmls() {
       
    57         return new Object[][] {
       
    58             { "|<xsl:value-of select=\"substring('asdf',2, 1)\"/>|", "<t>|s|</t>"},
       
    59             { "|<xsl:value-of select=\"substring('asdf',2, 1 div 0)\"/>|", "<t>|sdf|</t>"},
       
    60             { "|<xsl:value-of select=\"substring('asdf',2, -0 div 0)\"/>|", "<t>||</t>" },
       
    61             { "|<xsl:value-of select=\"substring('asdf',2, 1 div 0)\"/>|", "<t>|sdf|</t>" },
       
    62         };
       
    63     }
       
    64 
       
    65     @DataProvider(name = "SupplementaryCharactersTestData")
       
    66     private Object[][] bug8074297() {
       
    67         return new Object[][] {
       
    68             { "|<xsl:value-of select=\"substring('&#131083;ABC', 3)\"/>|",    "<t>|BC|</t>"},
       
    69             { "|<xsl:value-of select=\"substring('&#131083;ABC', 3, 1)\"/>|", "<t>|B|</t>" },
       
    70             { "|<xsl:value-of select=\"substring('&#131083;ABC', 2, 2)\"/>|", "<t>|AB|</t>"},
       
    71             { "|<xsl:value-of select=\"substring('&#131083;ABC', 3, 2)\"/>|", "<t>|BC|</t>"},
       
    72             { "|<xsl:value-of select=\"substring('&#131083;ABC', 3, 4)\"/>|", "<t>|BC|</t>"},
       
    73             { "|<xsl:value-of select=\"substring('&#131083;ABC', 1, 1)\"/>|", "<t>|&#131083;|</t>"},
       
    74             { "|<xsl:value-of select=\"substring('&#131083;ABC', 2, 1)\"/>|", "<t>|A|</t>"},
       
    75             { "|<xsl:value-of select=\"substring('&#131083;ABC', 1, 1 div 0)\"/>|", "<t>|&#131083;ABC|</t>"},
       
    76             { "|<xsl:value-of select=\"substring('&#131083;ABC', -10, 1 div 0)\"/>|", "<t>|&#131083;ABC|</t>"},
       
    77         };
       
    78     }
    52 
    79 
    53     private String testTransform(String xsl) throws Exception {
    80     private String testTransform(String xsl) throws Exception {
    54         //Prepare sources for transormation
    81         //Prepare sources for transormation
    55         Source src = new StreamSource(new StringReader(xml));
    82         Source src = new StreamSource(new StringReader(xml));
    56         Source xslsrc = new StreamSource(new StringReader(xslPre + xsl + xslPost));
    83         Source xslsrc = new StreamSource(new StringReader(xslPre + xsl + xslPost));
    76     public void test8062924() throws Exception {
   103     public void test8062924() throws Exception {
    77         assertEquals(testTransform("|<xsl:value-of select=\"substring('asdf',2,-1 div 0)\"/>|"),
   104         assertEquals(testTransform("|<xsl:value-of select=\"substring('asdf',2,-1 div 0)\"/>|"),
    78                 "<t>||</t>");
   105                 "<t>||</t>");
    79     }
   106     }
    80 
   107 
    81     @Test
   108     @Test(dataProvider = "GeneralTestsData")
    82     public void testGeneral1() throws Exception {
   109     public void testGeneralAll(String xsl, String result) throws Exception {
    83         assertEquals(testTransform("|<xsl:value-of select=\"substring('asdf',2, 1)\"/>|"),
   110         assertEquals(testTransform(xsl), result);
    84                 "<t>|s|</t>");
       
    85     }
   111     }
    86 
   112 
    87     @Test
   113     @Test(dataProvider = "SupplementaryCharactersTestData")
    88     public void testGeneral2() throws Exception {
   114     public void testSupplementCharacters(String xsl, String result) throws Exception {
    89         assertEquals(testTransform("|<xsl:value-of select=\"substring('asdf',2, 1 div 0)\"/>|"),
   115         assertEquals(testTransform(xsl), result);
    90                 "<t>|sdf|</t>");
       
    91     }
   116     }
    92 
   117 
    93     @Test
       
    94     public void testGeneral3() throws Exception {
       
    95         assertEquals(testTransform("|<xsl:value-of select=\"substring('asdf',2, -0 div 0)\"/>|"),
       
    96                 "<t>||</t>");
       
    97     }
       
    98 
       
    99     @Test
       
   100     public void testGeneral4() throws Exception {
       
   101         assertEquals(testTransform("|<xsl:value-of select=\"substring('asdf',2, 0 div 0)\"/>|"),
       
   102                 "<t>||</t>");
       
   103     }
       
   104 }
   118 }