1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  */
   4 /*
   5  * Licensed to the Apache Software Foundation (ASF) under one or more
   6  * contributor license agreements.  See the NOTICE file distributed with
   7  * this work for additional information regarding copyright ownership.
   8  * The ASF licenses this file to You under the Apache License, Version 2.0
   9  * (the "License"); you may not use this file except in compliance with
  10  * the License.  You may obtain a copy of the License at
  11  *
  12  *      http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 package org.apache.qetest.trax;
  21 
  22 import com.sun.org.apache.xml.internal.utils.DefaultErrorHandler;
  23 import javax.xml.transform.TransformerException;
  24 import jaxp.library.JAXPBaseTest;
  25 import static jaxp.library.JAXPTestUtilities.failUnexpected;
  26 import static org.testng.Assert.assertNotNull;
  27 import org.testng.annotations.BeforeMethod;
  28 import org.testng.annotations.Test;
  29 
  30 /**
  31  * API Coverage test for ErrorListener.
  32  */
  33 public class ErrorListenerAPITest extends JAXPBaseTest {
  34     /**
  35      * All test method need access internal package 
  36      * com.sun.org.apache.xml.internal.utils and 
  37      * com.sun.org.apache.xpath.internal.objects.
  38      */
  39     @BeforeMethod
  40     public void inititalize() {
  41         setPermissions(new RuntimePermission("accessClassInPackage.com.sun.org.apache.xml.internal.utils"),
  42                 new RuntimePermission("accessClassInPackage.com.sun.org.apache.xpath.internal.objects"));
  43     }
  44     /**
  45      * Root exception wrap message.
  46      */
  47     private final String EXP_MSG = "Exception-message-here";
  48     
  49     /**
  50      * Transformer exception wrap message.
  51      */
  52     private final String TRANSFORMER_EXP_MSG = "TransformerException-message-here";
  53 
  54     /**
  55      * Default behavior of DefaultErrorHandler.warning is doing nothing.
  56      */
  57     @Test
  58     public void testWarning() {
  59         DefaultErrorHandler deh = new DefaultErrorHandler();
  60         assertNotNull(deh);
  61         
  62         Exception ex = new Exception(EXP_MSG);
  63         TransformerException tex = new TransformerException(TRANSFORMER_EXP_MSG, ex);
  64         try {
  65             deh.warning(tex);
  66         } catch (TransformerException ex1) {
  67             failUnexpected(ex);
  68         }
  69     }
  70     
  71     /**
  72      * Default behavior of DefaultErrorHandler.error is throwing out the 
  73      * TransformerException.
  74      * 
  75      * @throws TransformerException Any SAX exception, possibly wrapping another
  76      *         exception. 
  77      */
  78     @Test(expectedExceptions = TransformerException.class)
  79     public void testError() throws TransformerException {
  80         DefaultErrorHandler deh = new DefaultErrorHandler();
  81         assertNotNull(deh);
  82         
  83         Exception ex = new Exception(EXP_MSG);
  84         TransformerException tex = new TransformerException(TRANSFORMER_EXP_MSG, ex);
  85         deh.error(tex);
  86     }
  87     
  88     /**
  89      * Default behavior of DefaultErrorHandler.fatalError is throwing out the 
  90      * TransformerException.
  91      * 
  92      * @throws TransformerException Any SAX exception, possibly wrapping another
  93      *         exception.
  94      */
  95     @Test(expectedExceptions = TransformerException.class)
  96     public void testFatalError() throws TransformerException {
  97         DefaultErrorHandler deh = new DefaultErrorHandler();
  98         assertNotNull(deh);
  99         
 100         Exception ex = new Exception(EXP_MSG);
 101         TransformerException tex = new TransformerException(TRANSFORMER_EXP_MSG, ex);
 102         deh.fatalError(tex);
 103     }
 104 }