KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nanocontainer > StandaloneTestCase


1 /*****************************************************************************
2  * Copyright (C) NanoContainer Organization. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  * *
8  *****************************************************************************/

9
10 package org.nanocontainer;
11
12 import java.io.File JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.net.URL JavaDoc;
15
16 import junit.framework.TestCase;
17
18 import org.apache.commons.cli.CommandLine;
19
20
21 /**
22  * @author Mauro Talevi
23  */

24 public class StandaloneTestCase extends TestCase {
25
26     public void testShouldBeAbleToInvokeMainMethodWithScriptFromFile() throws IOException JavaDoc, ClassNotFoundException JavaDoc {
27         File JavaDoc absoluteScriptPath = getAbsoluteScriptPath();
28         Standalone.main(new String JavaDoc[] {
29             "-c",
30             absoluteScriptPath.getAbsolutePath(),
31             "-n"
32         });
33     }
34
35     public void testShouldBeAbleToInvokeMainMethodWithScriptFromClasspathWithXmlIncludes() throws IOException JavaDoc, ClassNotFoundException JavaDoc {
36         Standalone.main(new String JavaDoc[] {
37             "-r",
38             "/org/nanocontainer/nanocontainer-with-include.xml",
39             "-n"
40         });
41     }
42
43     private File JavaDoc getAbsoluteScriptPath() {
44         String JavaDoc className = getClass().getName();
45         String JavaDoc relativeClassPath = "/" + className.replace('.', '/') + ".class";
46         URL JavaDoc classURL = Standalone.class.getResource(relativeClassPath);
47         String JavaDoc absoluteClassPath = classURL.getFile();
48         File JavaDoc absoluteDirPath = new File JavaDoc(absoluteClassPath).getParentFile();
49         File JavaDoc absoluteScriptPath = new File JavaDoc(absoluteDirPath, "nanocontainer.xml");
50         return absoluteScriptPath;
51     }
52
53     public void testCommandLineWithHelp() throws Exception JavaDoc {
54         CommandLine cl = Standalone.getCommandLine(new String JavaDoc[]{"-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 JavaDoc {
63         CommandLine cl = Standalone.getCommandLine(new String JavaDoc[]{"-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 JavaDoc {
72         CommandLine cl = Standalone.getCommandLine(new String JavaDoc[]{"-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 JavaDoc {
81         CommandLine cl = Standalone.getCommandLine(new String JavaDoc[]{"-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 JavaDoc {
90         CommandLine cl = Standalone.getCommandLine(new String JavaDoc[]{"-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