001: /*
002: * @(#)XMLFileParserUTest.java
003: *
004: * Copyright (C) 2002-2003 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a
009: * copy of this software and associated documentation files (the "Software"),
010: * to deal in the Software without restriction, including without limitation
011: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
012: * and/or sell copies of the Software, and to permit persons to whom the
013: * Software is furnished to do so, subject to the following conditions:
014: *
015: * The above copyright notice and this permission notice shall be included in
016: * all copies or substantial portions of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
021: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
023: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
024: * DEALINGS IN THE SOFTWARE.
025: */
026:
027: package net.sourceforge.groboutils.pmti.v1.autodoc.v1.xml;
028:
029: import net.sourceforge.groboutils.pmti.v1.itf.parser.*;
030:
031: import java.io.*;
032: import org.easymock.EasyMock;
033: import org.easymock.MockControl;
034: import junit.framework.Test;
035: import junit.framework.TestCase;
036: import junit.framework.TestSuite;
037: import net.sourceforge.groboutils.autodoc.v1.AutoDoc;
038: import net.sourceforge.groboutils.junit.v1.iftc.*;
039: import junit.framework.AssertionFailedError;
040:
041: import javax.xml.parsers.SAXParserFactory;
042: import org.xml.sax.Parser;
043:
044: /**
045: * Tests the XMLFileParser class.
046: *
047: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
048: * @version $Date: 2003/05/29 13:05:47 $
049: * @since July 14, 2002
050: */
051: public class XMLFileParserUTest extends TestCase {
052: //-------------------------------------------------------------------------
053: // Standard JUnit Class-specific declarations
054:
055: private static final Class THIS_CLASS = XMLFileParserUTest.class;
056: private static final AutoDoc DOC = new AutoDoc(THIS_CLASS);
057:
058: public XMLFileParserUTest(String name) {
059: super (name);
060: }
061:
062: //-------------------------------------------------------------------------
063: // setup
064:
065: /**
066: *
067: * @exception Exception thrown under any exceptional condition.
068: */
069: protected void setUp() throws Exception {
070: super .setUp();
071:
072: // set ourself up
073: }
074:
075: //-------------------------------------------------------------------------
076: // Tests
077:
078: public void testConstructor1() {
079: try {
080: new XMLFileParser(null, null);
081: } catch (IllegalArgumentException e) {
082: // test exception ???
083: }
084: }
085:
086: //-------------------------------------------------------------------------
087: // Helpers
088:
089: protected static File createSample1() {
090: File f = new File("test-" + System.currentTimeMillis() + ".xml");
091: PrintWriter pw = null;
092: try {
093: FileWriter fw = new FileWriter(f);
094: pw = new PrintWriter(fw);
095:
096: pw.println("<?xml version=\"1.0\"?>");
097:
098: pw.println("<testresult ");
099: pw.println(" suite=\"sample1\"");
100: pw.println(" test=\"test1\"");
101: pw.println(" tests=\"1\"");
102: pw.println(" time=\"0.08\"");
103: pw.println(" failures=\"0\"");
104: pw.println(" errors=\"0\" >");
105:
106: pw.println("</testresult>");
107: } catch (IOException e) {
108: e.printStackTrace();
109: if (f.exists()) {
110: f.delete();
111: }
112: throw new IllegalStateException("unexpected exception: "
113: + e.getMessage());
114: } finally {
115: if (pw != null) {
116: pw.close();
117: }
118: }
119: return f;
120: }
121:
122: protected static File createSample2() {
123: File f = new File("test-" + System.currentTimeMillis() + ".xml");
124: PrintWriter pw = null;
125: try {
126: FileWriter fw = new FileWriter(f);
127: pw = new PrintWriter(fw);
128:
129: pw.println("<?xml version=\"1.0\"?>");
130:
131: pw.println("<testresult>");
132: pw.println("</testresult>");
133: } catch (IOException e) {
134: e.printStackTrace();
135: if (f.exists()) {
136: f.delete();
137: }
138: throw new IllegalStateException("unexpected exception: "
139: + e.getMessage());
140: } finally {
141: if (pw != null) {
142: pw.close();
143: }
144: }
145: return f;
146: }
147:
148: protected static Parser createParser() {
149: try {
150: return SAXParserFactory.newInstance().newSAXParser()
151: .getParser();
152: } catch (Exception e) {
153: DOC.getLog().debug("Creating SAX parser caused exception.",
154: e);
155: throw new IllegalStateException(
156: "Could not create a SAX parser: " + e.getMessage());
157: }
158: }
159:
160: //-------------------------------------------------------------------------
161: // Standard JUnit declarations
162:
163: public static Test suite() {
164: InterfaceTestSuite suite = IParserUTestI.suite();
165: suite.addTestSuite(THIS_CLASS);
166: suite.addFactory(new CxFactory("A") {
167: public Object createImplObject() {
168: return new XMLFileParser(createSample1(),
169: createParser());
170: }
171: });
172: suite.addFactory(new CxFactory("B") {
173: public Object createImplObject() {
174: return new XMLFileParser(createSample2(),
175: createParser());
176: }
177: });
178:
179: return suite;
180: }
181:
182: public static void main(String[] args) {
183: String[] name = { THIS_CLASS.getName() };
184:
185: // junit.textui.TestRunner.main( name );
186: // junit.swingui.TestRunner.main( name );
187:
188: junit.textui.TestRunner.main(name);
189: }
190:
191: /**
192: *
193: * @exception Exception thrown under any exceptional condition.
194: */
195: protected void tearDown() throws Exception {
196: // tear ourself down
197:
198: super.tearDown();
199: }
200: }
|