1 /*
   2  * Copyright (c) 2015, 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 /**
  25  * @test
  26  * @bug 8062923 8062924
  27  * @run testng XslSubstringTest
  28  * @summary Test xsl substring function with negative, Inf and
  29  * NaN length and few other use cases
  30  */
  31 
  32 import java.io.StringReader;
  33 import java.io.StringWriter;
  34 import javax.xml.transform.Source;
  35 import javax.xml.transform.Templates;
  36 import javax.xml.transform.Transformer;
  37 import javax.xml.transform.TransformerFactory;
  38 import javax.xml.transform.stream.StreamResult;
  39 import javax.xml.transform.stream.StreamSource;
  40 
  41 import static org.testng.Assert.assertEquals;
  42 import org.testng.annotations.Test;
  43 
  44 public class XslSubstringTest {
  45 
  46     final String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><test></test>";
  47     final String xslPre = "<xsl:stylesheet version='1.0'"
  48             + " xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>"
  49             + "<xsl:output method='xml' indent='yes' omit-xml-declaration='yes'/>"
  50             + "<xsl:template match='/'><t>";
  51     final String xslPost = "</t></xsl:template></xsl:stylesheet>";
  52 
  53     private String testTransform(String xsl) throws Exception {
  54         //Prepare sources for transormation
  55         Source src = new StreamSource(new StringReader(xml));
  56         Source xslsrc = new StreamSource(new StringReader(xslPre + xsl + xslPost));
  57         //Create factory, template and transformer
  58         TransformerFactory tf = TransformerFactory.newInstance();
  59         Templates tmpl = tf.newTemplates(xslsrc);
  60         Transformer t = tmpl.newTransformer();
  61         //Prepare output stream
  62         StringWriter xmlResultString = new StringWriter();
  63         StreamResult xmlResultStream = new StreamResult(xmlResultString);
  64         //Transform
  65         t.transform(src, xmlResultStream);
  66         return xmlResultString.toString().trim();
  67     }
  68 
  69     @Test
  70     public void test8062923() throws Exception {
  71         assertEquals(testTransform("|<xsl:value-of select=\"substring('asdf',2,-1)\"/>|"),
  72                 "<t>||</t>");
  73     }
  74 
  75     @Test
  76     public void test8062924() throws Exception {
  77         assertEquals(testTransform("|<xsl:value-of select=\"substring('asdf',2,-1 div 0)\"/>|"),
  78                 "<t>||</t>");
  79     }
  80 
  81     @Test
  82     public void testGeneral1() throws Exception {
  83         assertEquals(testTransform("|<xsl:value-of select=\"substring('asdf',2, 1)\"/>|"),
  84                 "<t>|s|</t>");
  85     }
  86 
  87     @Test
  88     public void testGeneral2() throws Exception {
  89         assertEquals(testTransform("|<xsl:value-of select=\"substring('asdf',2, 1 div 0)\"/>|"),
  90                 "<t>|sdf|</t>");
  91     }
  92 
  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 }