Coverage Report - org.webslinger.containers.TestContainer
 
Classes in this File Line Coverage Branch Coverage Complexity
TestContainer
49%
36/74
75%
9/12
0
TestContainer$1
0%
0/1
N/A
0
TestContainer$TestContainerTest
83%
20/24
100%
1/1
0
TestContainer$TestContainerTestCase
100%
8/8
N/A
0
 
 1  
 package org.webslinger.containers;
 2  
 
 3  
 import java.io.File;
 4  
 import java.io.IOException;
 5  
 import java.io.PrintStream;
 6  
 import java.net.URL;
 7  
 import java.util.ArrayList;
 8  
 import java.util.Collection;
 9  
 import java.util.Enumeration;
 10  
 import java.util.HashMap;
 11  
 import java.util.Iterator;
 12  
 import java.util.List;
 13  
 import javax.imageio.spi.ServiceRegistry;
 14  
 
 15  
 import gnu.getopt.LongOpt;
 16  
 import gnu.getopt.Getopt;
 17  
 
 18  
 import junit.framework.AssertionFailedError;
 19  
 import junit.framework.Test;
 20  
 import junit.framework.TestResult;
 21  
 import junit.framework.TestSuite;
 22  
 import junit.textui.TestRunner;
 23  
 
 24  
 import org.codehaus.janino.DebuggingInformation;
 25  
 import org.codehaus.janino.JavaSourceClassLoader;
 26  
 
 27  
 import org.webslinger.junit.TestBuilder;
 28  
 import org.webslinger.launcher.AbstractGnuoptContainer;
 29  
 import org.webslinger.launcher.Container;
 30  
 import org.webslinger.launcher.Main;
 31  
 import org.webslinger.util.GenericTestCase;
 32  
 
 33  26
 public final class TestContainer extends AbstractGnuoptContainer {
 34  1
     protected static final HashMap<ClassLoader, File> locationsByLoader = new HashMap<ClassLoader, File>();
 35  1
     private static ThreadLocal<TestContainer> tl = new ThreadLocal<TestContainer>();
 36  
 
 37  
     private String[] testPaths;
 38  1
     private boolean doStandardTests = true;
 39  1
     private ArrayList<Test> tests = new ArrayList<Test>();
 40  
 
 41  1
     public TestContainer() {
 42  1
     }
 43  
 
 44  
     public void addTests(TestSuite suite) {
 45  0
         Enumeration en = suite.tests();
 46  0
         while (en.hasMoreElements()) tests.add((Test) en.nextElement());
 47  0
     }
 48  
 
 49  
     public void addTests(Class<?> clz) {
 50  0
         addTests(new TestSuite(clz));
 51  0
     }
 52  
 
 53  
     public void addTests(TestBuilder builder) {
 54  0
         builder.addTests(tests);
 55  0
     }
 56  
 
 57  
     public void addTest(Test test) {
 58  0
         tests.add(test);
 59  0
     }
 60  
 
 61  
     public void setTestPaths(String[] testPaths) {
 62  1
         this.testPaths = testPaths;
 63  1
     }
 64  
 
 65  
     public String[] getTestPaths() {
 66  0
         return testPaths;
 67  
     }
 68  
 
 69  
     public File getModuleRoot() throws IOException {
 70  26
         return main.getModuleRoot();
 71  
     }
 72  
 
 73  
     public void setDoStandardTests(boolean doStandardTests) {
 74  0
         this.doStandardTests = doStandardTests;
 75  0
     }
 76  
 
 77  
     public boolean getDoStandardTests() {
 78  1
         return doStandardTests;
 79  
     }
 80  
 
 81  
     public void showHelp(PrintStream out) throws IOException {
 82  0
         out.println("test help");
 83  0
     }
 84  
 
 85  
     protected Getopt getGetopt(String[] args) {
 86  1
         LongOpt[] longOpts = new LongOpt[] {
 87  
             new LongOpt("standard-tests", LongOpt.NO_ARGUMENT, null, 0),
 88  
             new LongOpt("no-standard-tests", LongOpt.NO_ARGUMENT, null, 0),
 89  
             new LongOpt("add-builder", LongOpt.REQUIRED_ARGUMENT, null, 0),
 90  
             new LongOpt("add-suite", LongOpt.REQUIRED_ARGUMENT, null, 0),
 91  
             new LongOpt("add-class", LongOpt.REQUIRED_ARGUMENT, null, 0),
 92  
         };
 93  1
         return new Getopt("webslinger=test", args, "", longOpts);
 94  
     }
 95  
 
 96  
     protected int processOneArg(int c, Getopt g) throws Exception {
 97  0
         switch (c) {
 98  
             case 0:
 99  0
                 switch (g.getLongind()) {
 100  
                     case 0:
 101  0
                         setDoStandardTests(true);
 102  0
                         break;
 103  
                     case 1:
 104  0
                         setDoStandardTests(false);
 105  0
                         break;
 106  
                     case 2:
 107  0
                         addTests((TestBuilder) getClass().getClassLoader().loadClass(g.getOptarg()).newInstance());
 108  0
                         return 0;
 109  
                     case 3:
 110  0
                         addTests((TestSuite) getClass().getClassLoader().loadClass(g.getOptarg()).newInstance());
 111  0
                         return 0;
 112  
                     case 4:
 113  0
                         addTests(getClass().getClassLoader().loadClass(g.getOptarg()));
 114  0
                         return 0;
 115  
                 }
 116  0
                 break;
 117  
             default:
 118  
         }
 119  0
         return 0;
 120  
     }
 121  
 
 122  
     protected boolean processRest(List<String> rest) {
 123  1
         setTestPaths(rest.toArray(new String[rest.size()]));
 124  1
         return true;
 125  
     }
 126  
 
 127  
     public void modifyClassPath(Main main, Collection<URL> urls) throws Exception {
 128  0
         switch (main.getLayout()) {
 129  
             case SVN:
 130  0
                 urls.add(Main.getFile(main.getRoot(), "build/lib/webslinger-tests.jar").toURL());
 131  0
                 break;
 132  
             case UNIX:
 133  0
                 urls.add(Main.getFile(main.getRoot(), "lib/webslinger/webslinger-tests.jar").toURL());
 134  
                 break;
 135  
         }
 136  0
     }
 137  
 
 138  
     public void init() throws Exception {
 139  1
     }
 140  
 
 141  
     public void run() throws Exception {
 142  1
         TestSuite suite = new TestSuite();
 143  1
         ArrayList<Test> tests = new ArrayList<Test>();
 144  1
         if (getDoStandardTests()) {
 145  1
             Iterator<TestBuilder> it = ServiceRegistry.lookupProviders(TestBuilder.class, getClass().getClassLoader());
 146  7
             while (it.hasNext()) {
 147  
                 try {
 148  6
                     it.next().addTests(tests);
 149  0
                 } catch (RuntimeException e) {
 150  0
                 } catch (UnsupportedClassVersionError e) {
 151  6
                 }
 152  
             }
 153  
         }
 154  1
         for (Test test: tests) suite.addTest(test);
 155  1
         for (Test test: this.tests) suite.addTest(test);
 156  11
         for (String testPath: testPaths) {
 157  10
             File location = new File(testPath).getAbsoluteFile();
 158  10
             File[] children = location.listFiles();
 159  59
             if (children != null) for (File child: children) {
 160  49
                 String name = child.getName();
 161  49
                 if (!name.endsWith(".java")) continue;
 162  15
                 if (!name.startsWith("Test")) continue;
 163  13
                 suite.addTest(new TestContainerTest(location, name.substring(0, name.length() - 5)));
 164  
             }
 165  
         }
 166  1
         TestRunner runner = new TestRunner();
 167  1
         tl.set(this);
 168  
         try {
 169  1
             if (!runner.doRun(suite, false).wasSuccessful()) System.exit(1);
 170  
         } finally {
 171  0
             tl.set(null);
 172  0
         }
 173  0
     }
 174  
 
 175  
     public static class TestContainerTestCase extends GenericTestCase {
 176  
         private File base;
 177  
 
 178  
         protected TestContainerTestCase(String name) {
 179  55
             super(name);
 180  55
         }
 181  
 
 182  
         protected void setUp() throws Exception {
 183  50
             super.setUp();
 184  50
             base = new File(getTestLocation(), getName());
 185  50
         }
 186  
 
 187  
         protected File getTestLocation() {
 188  101
             return locationsByLoader.get(getClass().getClassLoader());
 189  
         }
 190  
 
 191  
         protected File getBase() {
 192  26
             return base;
 193  
         }
 194  
 
 195  
         protected File getModuleRoot() throws IOException {
 196  26
             return tl.get().getModuleRoot();
 197  
         }
 198  
     }
 199  
 
 200  
     private static final class TestContainerTest implements Test {
 201  
         private final File location;
 202  
         private final String className;
 203  
 
 204  13
         protected TestContainerTest(File location, String className) {
 205  13
             this.location = location;
 206  13
             this.className = className;
 207  13
         }
 208  
 
 209  
         public int countTestCases() {
 210  13
             return 1;
 211  
         }
 212  
 
 213  
         public void run(TestResult result) {
 214  13
             result.startTest(this);
 215  
             try {
 216  
                 Exception exception;
 217  
                 Class clz;
 218  13
                 ClassLoader loader = null;
 219  
                 try {
 220  13
                     loader = new JavaSourceClassLoader(
 221  
                         getClass().getClassLoader(),
 222  
                         new File[] {location},
 223  
                         "UTF-8",
 224  
                         DebuggingInformation.ALL
 225  
                     );
 226  13
                     locationsByLoader.put(loader, location);
 227  
                     try {
 228  13
                         clz = loader.loadClass(className);
 229  13
                         exception = null;
 230  0
                     } catch (ClassNotFoundException e) {
 231  0
                         exception = e;
 232  0
                         clz = null;
 233  13
                     }
 234  13
                     if (clz == null) {
 235  0
                         result.addFailure(this, (AssertionFailedError) new AssertionFailedError("Couldn't find " + className + " at " + location).initCause(exception));
 236  
                     } else {
 237  13
                         new TestSuite(clz).run(result);
 238  
                     }
 239  
                 } finally {
 240  13
                     locationsByLoader.remove(loader);
 241  13
                     loader = null;
 242  13
                 }
 243  
             } finally {
 244  13
                 result.endTest(this);
 245  13
             }
 246  13
         }
 247  
     }
 248  
 }