< prev index next >

test/javax/xml/jaxp/functional/javax/xml/transform/ptests/TransformerExcpTest.java

Print this page


   1 /*
   2  * Copyright (c) 2003, 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 package javax.xml.transform.ptests;
  24 






  25 import java.io.File;
  26 import java.io.FilePermission;
  27 import javax.xml.transform.Transformer;
  28 import javax.xml.transform.TransformerException;
  29 import javax.xml.transform.TransformerFactory;
  30 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
  31 import javax.xml.transform.sax.SAXResult;
  32 import javax.xml.transform.stream.StreamSource;
  33 import jaxp.library.JAXPBaseTest;
  34 import static org.testng.Assert.assertEquals;
  35 import static org.testng.Assert.assertNotNull;
  36 import static org.testng.Assert.assertNull;
  37 import static org.testng.Assert.fail;
  38 import org.testng.annotations.Test;
  39 
  40 /**
  41  *  Basic test for TransformerException specification.
  42  */
  43 public class TransformerExcpTest extends JAXPBaseTest {

  44     /**
  45      * Transform an unformatted style-sheet file. TransformerException is thrown.
  46      */
  47     @Test
  48     public void tfexception() {
  49         try {
  50             setPermissions(new FilePermission(XML_DIR  + "-", "read"));
  51             // invalid.xsl has well-formedness error. Therefore transform throws
  52             // TransformerException
  53             StreamSource streamSource
  54                     = new StreamSource(new File(XML_DIR + "invalid.xsl"));
  55             TransformerFactory tFactory = TransformerFactory.newInstance();
  56             Transformer transformer = tFactory.newTransformer(streamSource);
  57             transformer.transform(
  58                     new StreamSource(new File(XML_DIR + "cities.xml")),
  59                     new SAXResult());
  60             fail("TransformerException is not thrown as expected");
  61         } catch (TransformerException e) {
  62             assertNotNull(e.getCause());
  63             assertNotNull(e.getException());
  64             assertNull(e.getLocationAsString());
  65             assertEquals(e.getMessageAndLocation(),e.getMessage());
  66         } finally {
  67             setPermissions();
  68         }
  69     }
  70 
  71 
  72     /**
  73      * Spec says, "if the throwable was created with
  74      * TransformerException(Throwable), initCause should throw
  75      * IllegalStateException
  76      */
  77     @Test(expectedExceptions = IllegalStateException.class)
  78     public void tfexception06() {
  79         TransformerException te = new TransformerException(new Throwable());
  80         te.initCause(null);
  81     }
  82 
  83     /**
  84      * Spec says, "if the throwable was created with TransformerException(String,
  85      * Throwable), initCause should throw IllegalStateException
  86      */
  87     @Test(expectedExceptions = IllegalStateException.class)
   1 /*
   2  * Copyright (c) 2003, 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 package javax.xml.transform.ptests;
  24 
  25 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
  26 import static org.testng.Assert.assertEquals;
  27 import static org.testng.Assert.assertNotNull;
  28 import static org.testng.Assert.assertNull;
  29 import static org.testng.Assert.fail;
  30 
  31 import java.io.File;
  32 
  33 import javax.xml.transform.Transformer;
  34 import javax.xml.transform.TransformerException;
  35 import javax.xml.transform.TransformerFactory;

  36 import javax.xml.transform.sax.SAXResult;
  37 import javax.xml.transform.stream.StreamSource;
  38 
  39 import org.testng.annotations.Listeners;



  40 import org.testng.annotations.Test;
  41 
  42 /**
  43  *  Basic test for TransformerException specification.
  44  */
  45 @Listeners({jaxp.library.FilePolicy.class})
  46 public class TransformerExcpTest {
  47     /**
  48      * Transform an unformatted style-sheet file. TransformerException is thrown.
  49      */
  50     @Test
  51     public void tfexception() {
  52         try {

  53             // invalid.xsl has well-formedness error. Therefore transform throws
  54             // TransformerException
  55             StreamSource streamSource
  56                     = new StreamSource(new File(XML_DIR + "invalid.xsl"));
  57             TransformerFactory tFactory = TransformerFactory.newInstance();
  58             Transformer transformer = tFactory.newTransformer(streamSource);
  59             transformer.transform(
  60                     new StreamSource(new File(XML_DIR + "cities.xml")),
  61                     new SAXResult());
  62             fail("TransformerException is not thrown as expected");
  63         } catch (TransformerException e) {
  64             assertNotNull(e.getCause());
  65             assertNotNull(e.getException());
  66             assertNull(e.getLocationAsString());
  67             assertEquals(e.getMessageAndLocation(),e.getMessage());


  68         }
  69     }
  70 
  71 
  72     /**
  73      * Spec says, "if the throwable was created with
  74      * TransformerException(Throwable), initCause should throw
  75      * IllegalStateException
  76      */
  77     @Test(expectedExceptions = IllegalStateException.class)
  78     public void tfexception06() {
  79         TransformerException te = new TransformerException(new Throwable());
  80         te.initCause(null);
  81     }
  82 
  83     /**
  84      * Spec says, "if the throwable was created with TransformerException(String,
  85      * Throwable), initCause should throw IllegalStateException
  86      */
  87     @Test(expectedExceptions = IllegalStateException.class)
< prev index next >