KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > httpunit > JUnitHttpTestRunner


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.httpunit;
21
22 import java.io.File JavaDoc;
23 import java.io.FileFilter JavaDoc;
24 import java.io.FileNotFoundException JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.StringTokenizer JavaDoc;
29
30 import junit.framework.Test;
31 import junit.framework.TestCase;
32 import junit.framework.TestSuite;
33
34 /**
35  */

36 public class JUnitHttpTestRunner {
37     /**
38      * @return Test
39      */

40     public static Test suite() {
41         try {
42
43             Collection JavaDoc<HttpTestContainer> containers = HttpTestParser.generateTests(getTestFiles());
44             TestSuite suite = new TestSuite();
45             for (HttpTestContainer container : containers) {
46                 for (HttpTestEntry entry : container.getEntries()) {
47                     suite.addTest(createTestCase(container, entry));
48                 }
49             }
50             return suite;
51         } catch (IOException JavaDoc e) {
52             e.printStackTrace();
53             return new TestSuite();
54         }
55     }
56
57     private static String JavaDoc[] getTestFiles() throws FileNotFoundException JavaDoc {
58         String JavaDoc location = System.getProperty("test.location", "");
59         if (location.length() == 0) {
60             throw new FileNotFoundException JavaDoc("No Test Files Found");
61         }
62
63         Collection JavaDoc<String JavaDoc> fileNames = new HashSet JavaDoc<String JavaDoc>();
64         for (StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(location, ";"); tokenizer.hasMoreTokens();) {
65             fileNames.addAll(getTestFiles(tokenizer.nextToken()));
66         }
67         return fileNames.toArray(new String JavaDoc[fileNames.size()]);
68     }
69
70     private static Collection JavaDoc<String JavaDoc> getTestFiles(String JavaDoc location) {
71         File JavaDoc locationDir = new File JavaDoc(location);
72         File JavaDoc[] files = locationDir.listFiles(new FileFilter JavaDoc() {
73             public boolean accept(File JavaDoc pathname) {
74                 return pathname.getName().toLowerCase().endsWith("-tests.xml");
75             }
76         });
77
78         Collection JavaDoc<String JavaDoc> fileNames = new HashSet JavaDoc<String JavaDoc>();
79         for (File JavaDoc file : files) {
80             fileNames.add(file.getAbsolutePath());
81         }
82         return fileNames;
83     }
84
85     private static TestCase createTestCase(HttpTestContainer container, HttpTestEntry entry) {
86         final HttpTestRunner runner = new HttpTestRunner(container, entry);
87         return new TestCase(entry.getName()) {
88             protected void setUp() throws Exception JavaDoc {
89                 runner.setUp();
90             }
91
92             @Override JavaDoc
93             protected void tearDown() throws Exception JavaDoc {
94                 runner.tearDown();
95             }
96
97             @Override JavaDoc
98             protected void runTest() throws Throwable JavaDoc {
99                 runner.runTest();
100             }
101         };
102     }
103 }
Popular Tags