KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > latka > junit > TestJUnitTestAdapter


1 /*
2  * Copyright 1999-2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.latka.junit;
18
19 import java.io.File JavaDoc;
20 import java.net.SocketException JavaDoc;
21
22 import junit.framework.Test;
23 import junit.framework.TestCase;
24 import junit.framework.TestSuite;
25
26 import org.apache.commons.latka.Suite;
27 import org.xml.sax.InputSource JavaDoc;
28
29 /**
30  * @author Chuck Burdick
31  * @version $Id: TestJUnitTestAdapter.java 155424 2005-02-26 13:09:29Z dirkv $
32  */

33 public class TestJUnitTestAdapter extends TestCase {
34    private String JavaDoc _xmlFileName = "src/distribution/tests/samples/TestCommonsWebsite.xml";
35    private File JavaDoc _xmlFile = null;
36    private JUnitTestAdapter _adapter = null;
37    private Suite _suite = null;
38
39    public TestJUnitTestAdapter(String JavaDoc testName) {
40       super(testName);
41    }
42
43    public static Test suite() {
44       return new TestSuite(TestJUnitTestAdapter.class);
45    }
46
47    public static void main(String JavaDoc args[]) {
48       String JavaDoc[] testCaseName = { TestJUnitTestAdapter.class.getName() };
49       junit.textui.TestRunner.main(testCaseName);
50    }
51
52    public void setUp() throws Exception JavaDoc {
53        super.setUp();
54        String JavaDoc baseDir = System.getProperty("basedir");
55        assertNotNull("The system property basedir was not defined.", baseDir);
56        String JavaDoc fs = System.getProperty("file.separator");
57        assertNotNull("The system property file.separator was not defined.", fs);
58        _xmlFileName = baseDir + fs + _xmlFileName;
59       _xmlFile = new File JavaDoc(_xmlFileName);
60       _suite = new Suite(_xmlFile.toURL());
61       _adapter = new JUnitTestAdapter(_suite, 9);
62    }
63
64    public void testParser() throws Throwable JavaDoc {
65        try {
66             assertEquals("Should parse the xml and count the request objects",
67                 9,
68                 JUnitTestAdapter.parse(new InputSource JavaDoc(_xmlFile.toURL().toString())));
69        } catch (SocketException JavaDoc se) {
70            // allow offline tests
71
if (se.getMessage().indexOf("unreachable") == -1) {
72                fail("SocketException: " + se.toString());
73            }
74        }
75    }
76
77    public void testCount() {
78       assertEquals("Should get 1 TestCase for each request",
79                    9, _adapter.countTestCases());
80    }
81
82    public void testCreateNullAdapterFromResource() {
83       Test myTest = JUnitTestAdapter.createTestFromResource("bogus");
84       assertNull("Should get null object back", myTest);
85    }
86
87    public void testCreateAdapterFromFile() {
88       Test myTest = JUnitTestAdapter.createTestFromFile(_xmlFile);
89       assertNotNull("Should get object back from file", myTest);
90       assertEquals("Should get 1 TestCase for each request",
91                    9, myTest.countTestCases());
92    }
93
94    public void testCreateAdapterFromFileName() {
95       Test myTest = JUnitTestAdapter.createTestFromFile(_xmlFileName);
96       assertNotNull("Should get object back from file name", myTest);
97       assertEquals("Should get 1 TestCase for each request",
98                    9, myTest.countTestCases());
99    }
100
101    public void testCreateNullAdapterFromURL() {
102       String JavaDoc url = "http://www.latka-example-dummy-url.com/";
103       Test myTest = JUnitTestAdapter.createTestFromURL(url);
104       assertNull("Should get null object back from bad url", myTest);
105    }
106 }
107
Popular Tags