1 9 10 package org.nanocontainer; 11 12 import java.io.File ; 13 import java.io.IOException ; 14 import java.net.URL ; 15 16 import junit.framework.TestCase; 17 18 import org.apache.commons.cli.CommandLine; 19 20 21 24 public class StandaloneTestCase extends TestCase { 25 26 public void testShouldBeAbleToInvokeMainMethodWithScriptFromFile() throws IOException , ClassNotFoundException { 27 File absoluteScriptPath = getAbsoluteScriptPath(); 28 Standalone.main(new String [] { 29 "-c", 30 absoluteScriptPath.getAbsolutePath(), 31 "-n" 32 }); 33 } 34 35 public void testShouldBeAbleToInvokeMainMethodWithScriptFromClasspathWithXmlIncludes() throws IOException , ClassNotFoundException { 36 Standalone.main(new String [] { 37 "-r", 38 "/org/nanocontainer/nanocontainer-with-include.xml", 39 "-n" 40 }); 41 } 42 43 private File getAbsoluteScriptPath() { 44 String className = getClass().getName(); 45 String relativeClassPath = "/" + className.replace('.', '/') + ".class"; 46 URL classURL = Standalone.class.getResource(relativeClassPath); 47 String absoluteClassPath = classURL.getFile(); 48 File absoluteDirPath = new File (absoluteClassPath).getParentFile(); 49 File absoluteScriptPath = new File (absoluteDirPath, "nanocontainer.xml"); 50 return absoluteScriptPath; 51 } 52 53 public void testCommandLineWithHelp() throws Exception { 54 CommandLine cl = Standalone.getCommandLine(new String []{"-h"}, Standalone.createOptions()); 55 assertTrue(cl.hasOption('h')); 56 assertFalse(cl.hasOption('v')); 57 assertNull(cl.getOptionValue('c')); 58 assertFalse(cl.hasOption('q')); 59 assertFalse(cl.hasOption('n')); 60 } 61 62 public void testCommandLineWithVersion() throws Exception { 63 CommandLine cl = Standalone.getCommandLine(new String []{"-v"}, Standalone.createOptions()); 64 assertFalse(cl.hasOption('h')); 65 assertTrue(cl.hasOption('v')); 66 assertNull(cl.getOptionValue('c')); 67 assertFalse(cl.hasOption('q')); 68 assertFalse(cl.hasOption('n')); 69 } 70 71 public void testCommandLineWithCompostion() throws Exception { 72 CommandLine cl = Standalone.getCommandLine(new String []{"-cpath"}, Standalone.createOptions()); 73 assertFalse(cl.hasOption('h')); 74 assertFalse(cl.hasOption('v')); 75 assertEquals("path", cl.getOptionValue('c')); 76 assertFalse(cl.hasOption('q')); 77 assertFalse(cl.hasOption('n')); 78 } 79 80 public void testCommandLineWithCompositionAndQuiet() throws Exception { 81 CommandLine cl = Standalone.getCommandLine(new String []{"-cpath", "-q"}, Standalone.createOptions()); 82 assertFalse(cl.hasOption('h')); 83 assertFalse(cl.hasOption('v')); 84 assertEquals("path", cl.getOptionValue('c')); 85 assertTrue(cl.hasOption('q')); 86 assertFalse(cl.hasOption('n')); 87 } 88 89 public void testCommandLineWithCompositionAndQuietAndNowait() throws Exception { 90 CommandLine cl = Standalone.getCommandLine(new String []{"-cpath", "-q", "-n"}, Standalone.createOptions()); 91 assertFalse(cl.hasOption('h')); 92 assertFalse(cl.hasOption('v')); 93 assertEquals("path", cl.getOptionValue('c')); 94 assertTrue(cl.hasOption('q')); 95 assertTrue(cl.hasOption('n')); 96 } 97 98 } 99 | Popular Tags |