1 /*
   2  * Copyright (c) 2014, 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 javax.xml.transform.Transformer;
  27 import javax.xml.transform.TransformerException;
  28 import javax.xml.transform.TransformerFactory;
  29 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
  30 import javax.xml.transform.sax.SAXResult;
  31 import javax.xml.transform.stream.StreamSource;
  32 import static org.testng.Assert.assertEquals;
  33 import static org.testng.Assert.assertNotNull;
  34 import static org.testng.Assert.assertNull;
  35 import static org.testng.Assert.fail;
  36 import org.testng.annotations.Test;
  37 
  38 /**
  39  *  Basic test for TransformerException specification.
  40  */
  41 public class TransformerExcpTest {
  42     /**
  43      * Transform an unformatted xslt file. TransformerException is thrown.
  44      */
  45     @Test
  46     public void tfexception() {
  47         try {
  48             // invalid.xsl has well-formedness error. Therefore transform throws
  49             // TransformerException
  50             StreamSource streamSource
  51                     = new StreamSource(new File(XML_DIR + "invalid.xsl"));
  52             TransformerFactory tFactory = TransformerFactory.newInstance();
  53             Transformer transformer = tFactory.newTransformer(streamSource);
  54             transformer.transform(
  55                     new StreamSource(new File(XML_DIR + "cities.xml")),
  56                     new SAXResult());
  57             fail("TransformerException is not thrown as expected");
  58         } catch (TransformerException e) {
  59             assertNotNull(e.getCause());
  60             assertNotNull(e.getException());
  61             assertNull(e.getLocationAsString());
  62             assertEquals(e.getMessageAndLocation(),e.getMessage());
  63         }
  64     }
  65 
  66 
  67     /**
  68      * Spec says, "if the throwable was created with
  69      * TransformerException(Throwable), initCause should throw
  70      * IllegalStateException
  71      */
  72     @Test(expectedExceptions = IllegalStateException.class)
  73     public void tfexception06() {
  74         TransformerException te = new TransformerException(new Throwable());
  75         te.initCause(null);
  76     }
  77 
  78     /**
  79      * Spec says, "if the throwable was created with TransformerException(String,
  80      * Throwable), initCause should throw IllegalStateException
  81      */
  82     @Test(expectedExceptions = IllegalStateException.class)
  83     public void tfexception07() {
  84         TransformerException te = new TransformerException("MyMessage", new Throwable());
  85         te.initCause(null);
  86     }
  87 
  88     /**
  89      * Tests if initCause(null) is allowed in other case.
  90      */
  91     @Test
  92     public void tfexception08() {
  93         TransformerException te = new TransformerException("My Message");
  94         assertNotNull(te.initCause(null));
  95     }
  96 }