1 19 20 package com.sslexplorer.httpunit; 21 22 import java.io.File ; 23 import java.io.FileFilter ; 24 import java.io.FileNotFoundException ; 25 import java.io.IOException ; 26 import java.util.Collection ; 27 import java.util.HashSet ; 28 import java.util.StringTokenizer ; 29 30 import junit.framework.Test; 31 import junit.framework.TestCase; 32 import junit.framework.TestSuite; 33 34 36 public class JUnitHttpTestRunner { 37 40 public static Test suite() { 41 try { 42 43 Collection <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 e) { 52 e.printStackTrace(); 53 return new TestSuite(); 54 } 55 } 56 57 private static String [] getTestFiles() throws FileNotFoundException { 58 String location = System.getProperty("test.location", ""); 59 if (location.length() == 0) { 60 throw new FileNotFoundException ("No Test Files Found"); 61 } 62 63 Collection <String > fileNames = new HashSet <String >(); 64 for (StringTokenizer tokenizer = new StringTokenizer (location, ";"); tokenizer.hasMoreTokens();) { 65 fileNames.addAll(getTestFiles(tokenizer.nextToken())); 66 } 67 return fileNames.toArray(new String [fileNames.size()]); 68 } 69 70 private static Collection <String > getTestFiles(String location) { 71 File locationDir = new File (location); 72 File [] files = locationDir.listFiles(new FileFilter () { 73 public boolean accept(File pathname) { 74 return pathname.getName().toLowerCase().endsWith("-tests.xml"); 75 } 76 }); 77 78 Collection <String > fileNames = new HashSet <String >(); 79 for (File 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 { 89 runner.setUp(); 90 } 91 92 @Override 93 protected void tearDown() throws Exception { 94 runner.tearDown(); 95 } 96 97 @Override 98 protected void runTest() throws Throwable { 99 runner.runTest(); 100 } 101 }; 102 } 103 } | Popular Tags |