View Javadoc

1   /*
2    * $Id: XMLHammerTest.java,v 1.8 2008/01/23 22:17:28 edankert Exp $
3    *
4    * The contents of this file are subject to the Mozilla Public License 
5    * Version 1.1 (the "License"); you may not use this file except in 
6    * compliance with the License. You may obtain a copy of the License at 
7    * http://www.mozilla.org/MPL/ 
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis, 
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
11   * for the specific language governing rights and limitations under the License.
12   *
13   * The Original Code is XML Hammer code. (org.xmlhammer.*)
14   *
15   * The Initial Developer of the Original Code is Edwin Dankert. Portions created 
16   * by the Initial Developer are Copyright (C) 2005 - 2006 the Initial Developer. 
17   * All Rights Reserved.
18   *
19   * Contributor(s): Edwin Dankert <edankert@gmail.com>
20   */
21  package org.xmlhammer.cli;
22  
23  import java.io.ByteArrayOutputStream;
24  import java.io.PrintStream;
25  
26  import org.xmlhammer.Identity;
27  import org.xmlhammer.Runner;
28  
29  import junit.framework.Test;
30  import junit.framework.TestCase;
31  import junit.framework.TestSuite;
32  
33  
34  /***
35   * Put comment...
36   * 
37   * @version $Revision: 1.8 $, $Date: 2008/01/23 22:17:28 $
38   * @author Edwin Dankert <edankert@gmail.com>
39   */
40  public class XMLHammerTest extends TestCase {
41      private static final String ERROR_MESSAGE = 
42          "Usage: java -jar xmlhammer-"+Identity.getInstance().getVersion()+".jar -cli [options] ${project-file}"+System.getProperty("line.separator")+
43          "Options:"+System.getProperty("line.separator")+
44          "  -cli          Run the command line interface (required)"+System.getProperty("line.separator")+
45          "  -settings     Show the project specific settings"+System.getProperty("line.separator")+
46          "  -default      Use the default preferences for better interoperability"+System.getProperty("line.separator");
47      
48      private static final String[] VALID_MESSAGE_LINES = new String[] {
49          "src/test/resources/input/validator/contacts-invalid.xml",
50          "[error] [5:12] cvc-complex-type.2.4.a: Invalid content was found starting with element 'title'. One of '{",
51          "firstname}' is expected.",
52          "[error] [8:26] cvc-complex-type.3.2.2: Attribute 'type' is not allowed to appear in element 'address'.",
53          "[error] [13:26] cvc-complex-type.3.2.2: Attribute 'type' is not allowed to appear in element 'address'."
54      };
55      
56      private static final String[] VALID_SETTINGS_MESSAGE_LINES = new String[] {
57          "info: javax.xml.parsers.SAXParserFactory",
58          "[class] com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl",
59          "[setting] namespace-aware = true",
60          "[setting] xinclude-aware = false",
61          "[setting] validating = false",
62          "[feature] http://xml.org/sax/features/external-general-entities = true",
63          "[feature] http://xml.org/sax/features/external-parameter-entities = true",
64          "[feature] http://xml.org/sax/features/namespace-prefixes = false",
65          "[feature] http://xml.org/sax/features/string-interning = true",
66          "[feature] http://xml.org/sax/features/lexical-handler/parameter-entities = true",
67          "[feature] http://xml.org/sax/features/unicode-normalization-checking = false",
68          "[feature] http://xml.org/sax/features/allow-dtd-events-after-endDTD = false",
69          "info: javax.xml.validation.SchemaFactory:http://www.w3.org/2001/XMLSchema",
70          "[class] com.sun.org.apache.xerces.internal.jaxp.validation",
71          "SchemaFactory",
72          "xmlhammer/src/test/resources/schema/validator/contacts.xsd" 
73      };
74  	
75      protected XMLHammerTest(String test) {
76          super(test);
77      }
78  
79      public void testWithoutArguments() throws Exception {
80          ByteArrayOutputStream stream = new ByteArrayOutputStream();
81          System.setErr(new PrintStream(stream));
82          
83          try {
84              Runner.main(new String[] { "-cli" });
85          } catch (Exception e) {
86              fail(e.getLocalizedMessage());
87          }
88          
89          assertEquals(ERROR_MESSAGE, stream.toString());
90      }
91      
92      public void testWithSettingsArgument() throws Exception {
93          ByteArrayOutputStream stream = new ByteArrayOutputStream();
94          System.setErr(new PrintStream(stream));
95          
96          try {
97              Runner.main(new String[] { "-cli", "-settings" });
98          } catch (Exception e) {
99              fail(e.getLocalizedMessage());
100         }
101         
102         assertEquals(ERROR_MESSAGE, stream.toString());
103     }
104 
105     public void testWithDefaultArgument() throws Exception {
106         ByteArrayOutputStream stream = new ByteArrayOutputStream();
107         System.setErr(new PrintStream(stream));
108         
109         try {
110             Runner.main(new String[] { "-cli", "-default" });
111         } catch (Exception e) {
112             fail(e.getLocalizedMessage());
113         }
114         
115         assertEquals(ERROR_MESSAGE, stream.toString());
116     }
117 
118     public void testWithBothArguments() throws Exception {
119         ByteArrayOutputStream stream = new ByteArrayOutputStream();
120         System.setErr(new PrintStream(stream));
121         
122         try {
123             Runner.main(new String[] { "-cli", "-default", "-settings" });
124         } catch (Exception e) {
125             fail(e.getLocalizedMessage());
126         }
127         
128         assertEquals(ERROR_MESSAGE, stream.toString());
129     }
130 
131     public void testWithFile() throws Exception {
132         ByteArrayOutputStream stream = new ByteArrayOutputStream();
133         System.setOut(new PrintStream(stream));
134         
135         try {
136             Runner.main(new String[] { "-cli", "src/test/resources/projects/validator/contact-xsd-invalid-sax.xhp", "-default"});
137         } catch (Exception e) {
138             fail(e.getLocalizedMessage());
139         }
140 
141         for (String line : VALID_MESSAGE_LINES) {
142             assertTrue("Could not find: "+line, stream.toString().indexOf(line) != -1);
143         }
144     }
145 
146     public void testWithFileAndShowSettings() throws Exception {
147         ByteArrayOutputStream stream = new ByteArrayOutputStream();
148         System.setOut(new PrintStream(stream));
149         
150         try {
151             Runner.main(new String[] { "-cli", "src/test/resources/projects/validator/contact-xsd-invalid-sax.xhp", "-default", "-settings" });
152         } catch (Exception e) {
153             fail(e.getLocalizedMessage());
154         }
155         
156         for (String line : VALID_SETTINGS_MESSAGE_LINES) {
157             assertTrue(stream.toString().indexOf(line) != -1);
158         }
159     }
160 
161     public static Test suite() {
162         TestSuite suite = new TestSuite("XMLHammer Command Line Test");
163         
164         suite.addTest(new XMLHammerTest("testWithoutArguments"));
165         suite.addTest(new XMLHammerTest("testWithSettingsArgument"));
166         suite.addTest(new XMLHammerTest("testWithDefaultArgument"));
167         suite.addTest(new XMLHammerTest("testWithBothArguments"));
168         suite.addTest(new XMLHammerTest("testWithFile"));
169         suite.addTest(new XMLHammerTest("testWithFileAndShowSettings"));
170 
171         return suite;
172     }
173 }