KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > pmti > v1 > autodoc > v1 > xml > XMLFileParserUTest


1 /*
2  * @(#)XMLFileParserUTest.java
3  *
4  * Copyright (C) 2002-2003 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  */

26
27 package net.sourceforge.groboutils.pmti.v1.autodoc.v1.xml;
28
29 import net.sourceforge.groboutils.pmti.v1.itf.parser.*;
30
31 import java.io.*;
32 import org.easymock.EasyMock;
33 import org.easymock.MockControl;
34 import junit.framework.Test;
35 import junit.framework.TestCase;
36 import junit.framework.TestSuite;
37 import net.sourceforge.groboutils.autodoc.v1.AutoDoc;
38 import net.sourceforge.groboutils.junit.v1.iftc.*;
39 import junit.framework.AssertionFailedError;
40
41 import javax.xml.parsers.SAXParserFactory JavaDoc;
42 import org.xml.sax.Parser JavaDoc;
43
44
45 /**
46  * Tests the XMLFileParser class.
47  *
48  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
49  * @version $Date: 2003/05/29 13:05:47 $
50  * @since July 14, 2002
51  */

52 public class XMLFileParserUTest extends TestCase
53 {
54     //-------------------------------------------------------------------------
55
// Standard JUnit Class-specific declarations
56

57     private static final Class JavaDoc THIS_CLASS = XMLFileParserUTest.class;
58     private static final AutoDoc DOC = new AutoDoc( THIS_CLASS );
59     
60     public XMLFileParserUTest( String JavaDoc name )
61     {
62         super( name );
63     }
64
65     
66     //-------------------------------------------------------------------------
67
// setup
68

69     /**
70      *
71      * @exception Exception thrown under any exceptional condition.
72      */

73     protected void setUp() throws Exception JavaDoc
74     {
75         super.setUp();
76         
77         // set ourself up
78
}
79
80
81     //-------------------------------------------------------------------------
82
// Tests
83

84     
85     public void testConstructor1()
86     {
87         try
88         {
89             new XMLFileParser( null, null );
90         }
91         catch (IllegalArgumentException JavaDoc e)
92         {
93             // test exception ???
94
}
95     }
96
97     
98     //-------------------------------------------------------------------------
99
// Helpers
100

101     
102     protected static File createSample1()
103     {
104         File f = new File( "test-" + System.currentTimeMillis() + ".xml" );
105         PrintWriter pw = null;
106         try
107         {
108             FileWriter fw = new FileWriter( f );
109             pw = new PrintWriter( fw );
110             
111             pw.println( "<?xml version=\"1.0\"?>" );
112             
113             pw.println( "<testresult " );
114             pw.println( " suite=\"sample1\"" );
115             pw.println( " test=\"test1\"" );
116             pw.println( " tests=\"1\"" );
117             pw.println( " time=\"0.08\"" );
118             pw.println( " failures=\"0\"" );
119             pw.println( " errors=\"0\" >" );
120             
121             pw.println( "</testresult>" );
122         }
123         catch (IOException e)
124         {
125             e.printStackTrace();
126             if (f.exists())
127             {
128                 f.delete();
129             }
130             throw new IllegalStateException JavaDoc("unexpected exception: "+
131                 e.getMessage() );
132         }
133         finally
134         {
135             if (pw != null)
136             {
137                 pw.close();
138             }
139         }
140         return f;
141     }
142     
143     
144     protected static File createSample2()
145     {
146         File f = new File( "test-" + System.currentTimeMillis() + ".xml" );
147         PrintWriter pw = null;
148         try
149         {
150             FileWriter fw = new FileWriter( f );
151             pw = new PrintWriter( fw );
152             
153             pw.println( "<?xml version=\"1.0\"?>" );
154             
155             pw.println( "<testresult>" );
156             pw.println( "</testresult>" );
157         }
158         catch (IOException e)
159         {
160             e.printStackTrace();
161             if (f.exists())
162             {
163                 f.delete();
164             }
165             throw new IllegalStateException JavaDoc("unexpected exception: "+
166                 e.getMessage() );
167         }
168         finally
169         {
170             if (pw != null)
171             {
172                 pw.close();
173             }
174         }
175         return f;
176     }
177     
178     
179     protected static Parser JavaDoc createParser()
180     {
181         try
182         {
183             return SAXParserFactory.newInstance().newSAXParser().getParser();
184         }
185         catch (Exception JavaDoc e)
186         {
187             DOC.getLog().debug( "Creating SAX parser caused exception.", e );
188             throw new IllegalStateException JavaDoc("Could not create a SAX parser: "+
189                 e.getMessage() );
190         }
191     }
192
193     
194     
195     //-------------------------------------------------------------------------
196
// Standard JUnit declarations
197

198     
199     public static Test suite()
200     {
201         InterfaceTestSuite suite = IParserUTestI.suite();
202         suite.addTestSuite( THIS_CLASS );
203         suite.addFactory( new CxFactory( "A" ) {
204             public Object JavaDoc createImplObject() {
205                 return new XMLFileParser( createSample1(), createParser() );
206             }
207         } );
208         suite.addFactory( new CxFactory( "B" ) {
209             public Object JavaDoc createImplObject() {
210                 return new XMLFileParser( createSample2(), createParser() );
211             }
212         } );
213         
214         return suite;
215     }
216     
217     public static void main( String JavaDoc[] args )
218     {
219         String JavaDoc[] name = { THIS_CLASS.getName() };
220         
221         // junit.textui.TestRunner.main( name );
222
// junit.swingui.TestRunner.main( name );
223

224         junit.textui.TestRunner.main( name );
225     }
226     
227     
228     /**
229      *
230      * @exception Exception thrown under any exceptional condition.
231      */

232     protected void tearDown() throws Exception JavaDoc
233     {
234         // tear ourself down
235

236         
237         super.tearDown();
238     }
239 }
240
241
Popular Tags