• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the  "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 /*
19  * $Id$
20  */
21 
22 /*
23  *
24  * ErrorListenerAPITest.java
25  *
26  */
27 package org.apache.qetest.trax;
28 
29 import android.platform.test.annotations.FlakyTest;
30 import java.util.Properties;
31 
32 import javax.xml.transform.ErrorListener;
33 import javax.xml.transform.TransformerException;
34 
35 import org.apache.qetest.FileBasedTest;
36 import org.junit.Test;
37 
38 //-------------------------------------------------------------------------
39 
40 /**
41  * API Coverage test for ErrorListener; defaults to Xalan impl.
42  * Only very basic API coverage.
43  * @author shane_curcuru@lotus.com
44  * @version $Id$
45  */
46 public class ErrorListenerAPITest extends FileBasedTest
47 {
48     /** FQCN for Xalan-J 2.x impl.  */
49     public static final String XALAN_ERRORLISTENER_IMPL = "org.apache.xml.utils.DefaultErrorHandler";
50 
51     /** FQCN for the Logging* impl that the tests provide.  */
52     public static final String QETEST_ERRORLISTENER_IMPL = "org.apache.qetest.trax.LoggingErrorHandler";
53 
54     /** Name of ErrorListener implementation we're going to test.  */
55     public String errorListenerClassname = XALAN_ERRORLISTENER_IMPL;
56 
57     /** Just initialize test name, comment, numTestCases. */
ErrorListenerAPITest()58     public ErrorListenerAPITest()
59     {
60         numTestCases = 1;  // REPLACE_num
61         testName = "ErrorListenerAPITest";
62         testComment = "API Coverage test for ErrorListener; defaults to Xalan impl";
63     }
64 
65 
66     /**
67      * Initialize this test - Set names of xml/xsl test files,
68      * //@todo read in name of alternate ErrorListener class!
69      *
70      * @param p Properties to initialize from (if needed)
71      * @return false if we should abort the test; true otherwise
72      */
doTestFileInit(Properties p)73     public boolean doTestFileInit(Properties p)
74     {
75         reporter.logInfoMsg("//@todo allow user to change name of ErrorListener implementation used");
76         return true;
77     }
78 
79 
80     /**
81      * API Coverage of ErrorListener class, using Xalan-J 2.x impl.
82      *
83      * @return false if we should abort the test; true otherwise
84      */
testCase1()85     public boolean testCase1()
86     {
87         reporter.testCaseInit("API Coverage of ErrorListener class, using Xalan-J 2.x impl");
88         Class elClass = null;
89         ErrorListener errorListener = null;
90         try
91         {
92             elClass = Class.forName(errorListenerClassname);
93             errorListener = (ErrorListener)elClass.newInstance();
94         }
95         catch (Exception e)
96         {
97             reporter.checkErr("Loading errorListener implementation " + errorListenerClassname
98                               + " threw: " + e.toString());
99             reporter.testCaseClose();
100             return true;
101         }
102 
103         Exception ex = new Exception("Exception-message-here");
104         TransformerException tex = new TransformerException("TransformerException-message-here", ex);
105 
106         try
107         {
108             errorListener.warning(tex);
109             reporter.checkPass("warning did not throw any exception");
110             reporter.logTraceMsg("//@todo also validate System.err stream!");
111         }
112         catch (TransformerException te)
113         {
114             reporter.checkFail("warning threw TransformerException, threw: " + te.toString());
115         }
116         catch (Throwable t)
117         {
118             reporter.checkFail("warning threw non-TransformerException, threw: " + t.toString());
119         }
120 
121         try
122         {
123             // Default error impl in Xalan throws exception
124             errorListener.error(tex);
125             reporter.checkFail("error did not throw any exception");
126             reporter.logTraceMsg("//@todo also validate System.err stream!");
127         }
128         catch (TransformerException te)
129         {
130             reporter.checkPass("error expectedly threw TransformerException, threw: " + te.toString());
131             reporter.check((te.toString().indexOf("TransformerException-message-here") > -1),
132                            true, "error's exception includes proper text");
133         }
134         catch (Throwable t)
135         {
136             reporter.checkFail("error threw non-TransformerException, threw: " + t.toString());
137         }
138 
139         try
140         {
141             // Default fatalError impl in Xalan throws exception
142             errorListener.error(tex);
143             reporter.checkFail("fatalError did not throw any exception");
144             reporter.logTraceMsg("//@todo also validate System.err stream!");
145         }
146         catch (TransformerException te)
147         {
148             reporter.checkPass("fatalError expectedly threw TransformerException, threw: " + te.toString());
149             reporter.check((te.toString().indexOf("TransformerException-message-here") > -1),
150                            true, "fatalError's exception includes proper text");
151         }
152         catch (Throwable t)
153         {
154             reporter.checkFail("fatalError threw non-TransformerException, threw: " + t.toString());
155         }
156         reporter.testCaseClose();
157         return true;
158     }
159 
160 
161     /**
162      * Convenience method to print out usage information - update if needed.
163      * @return String denoting usage of this test class
164      */
usage()165     public String usage()
166     {
167         return ("Common [optional] options supported by ErrorListenerAPITest:\n"
168                 + "(Note: assumes inputDir=.\\tests\\api)\n"
169                 + super.usage());   // Grab our parent classes usage as well
170     }
171 
172 
173     /**
174      * Main method to run test from the command line - can be left alone.
175      * @param args command line argument array
176      */
main(String[] args)177     public static void main(String[] args)
178     {
179         ErrorListenerAPITest app = new ErrorListenerAPITest();
180         app.doMain(args);
181     }
182 
183     // Android-added: Run main method as a JUnit test case.
184     @FlakyTest(bugId = 292520220)
185     @Test
main()186     public void main() {
187         main(new String[0]);
188     }
189 }
190