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)
  88     public void tfexception07() {
  89         TransformerException te = new TransformerException("MyMessage", new Throwable());
  90         te.initCause(null);
  91     }
  92 
  93     /**
  94      * Tests if initCause(null) is allowed in other case.
  95      */
  96     @Test
  97     public void tfexception08() {
  98         TransformerException te = new TransformerException("My Message");
  99         assertNotNull(te.initCause(null));
 100     }
 101 }