001: /*****************************************************************************
002: * Copyright (C) NanoContainer Organization. All rights reserved. *
003: * ------------------------------------------------------------------------- *
004: * The software in this package is published under the terms of the BSD *
005: * style license a copy of which has been included with this distribution in *
006: * the LICENSE.txt file. *
007: * *
008: *****************************************************************************/package org.nanocontainer;
009:
010: import java.io.File;
011: import java.io.IOException;
012: import java.net.URL;
013:
014: import junit.framework.TestCase;
015:
016: import org.apache.commons.cli.CommandLine;
017:
018: /**
019: * @author Mauro Talevi
020: */
021: public class StandaloneTestCase extends TestCase {
022:
023: public void testShouldBeAbleToInvokeMainMethodWithScriptFromFile()
024: throws IOException, ClassNotFoundException {
025: File absoluteScriptPath = getAbsoluteScriptPath();
026: Standalone.main(new String[] { "-c",
027: absoluteScriptPath.getAbsolutePath(), "-n" });
028: }
029:
030: public void testShouldBeAbleToInvokeMainMethodWithScriptFromClasspathWithXmlIncludes()
031: throws IOException, ClassNotFoundException {
032: Standalone.main(new String[] { "-r",
033: "/org/nanocontainer/nanocontainer-with-include.xml",
034: "-n" });
035: }
036:
037: private File getAbsoluteScriptPath() {
038: String className = getClass().getName();
039: String relativeClassPath = "/" + className.replace('.', '/')
040: + ".class";
041: URL classURL = Standalone.class.getResource(relativeClassPath);
042: String absoluteClassPath = classURL.getFile();
043: File absoluteDirPath = new File(absoluteClassPath)
044: .getParentFile();
045: File absoluteScriptPath = new File(absoluteDirPath,
046: "nanocontainer.xml");
047: return absoluteScriptPath;
048: }
049:
050: public void testCommandLineWithHelp() throws Exception {
051: CommandLine cl = Standalone.getCommandLine(
052: new String[] { "-h" }, Standalone.createOptions());
053: assertTrue(cl.hasOption('h'));
054: assertFalse(cl.hasOption('v'));
055: assertNull(cl.getOptionValue('c'));
056: assertFalse(cl.hasOption('q'));
057: assertFalse(cl.hasOption('n'));
058: }
059:
060: public void testCommandLineWithVersion() throws Exception {
061: CommandLine cl = Standalone.getCommandLine(
062: new String[] { "-v" }, Standalone.createOptions());
063: assertFalse(cl.hasOption('h'));
064: assertTrue(cl.hasOption('v'));
065: assertNull(cl.getOptionValue('c'));
066: assertFalse(cl.hasOption('q'));
067: assertFalse(cl.hasOption('n'));
068: }
069:
070: public void testCommandLineWithCompostion() throws Exception {
071: CommandLine cl = Standalone.getCommandLine(
072: new String[] { "-cpath" }, Standalone.createOptions());
073: assertFalse(cl.hasOption('h'));
074: assertFalse(cl.hasOption('v'));
075: assertEquals("path", cl.getOptionValue('c'));
076: assertFalse(cl.hasOption('q'));
077: assertFalse(cl.hasOption('n'));
078: }
079:
080: public void testCommandLineWithCompositionAndQuiet()
081: throws Exception {
082: CommandLine cl = Standalone.getCommandLine(new String[] {
083: "-cpath", "-q" }, Standalone.createOptions());
084: assertFalse(cl.hasOption('h'));
085: assertFalse(cl.hasOption('v'));
086: assertEquals("path", cl.getOptionValue('c'));
087: assertTrue(cl.hasOption('q'));
088: assertFalse(cl.hasOption('n'));
089: }
090:
091: public void testCommandLineWithCompositionAndQuietAndNowait()
092: throws Exception {
093: CommandLine cl = Standalone.getCommandLine(new String[] {
094: "-cpath", "-q", "-n" }, Standalone.createOptions());
095: assertFalse(cl.hasOption('h'));
096: assertFalse(cl.hasOption('v'));
097: assertEquals("path", cl.getOptionValue('c'));
098: assertTrue(cl.hasOption('q'));
099: assertTrue(cl.hasOption('n'));
100: }
101:
102: }
|