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     *****************************************************************************/
009    
010    package org.nanocontainer;
011    
012    import java.io.File;
013    import java.io.IOException;
014    import java.net.URL;
015    
016    import junit.framework.TestCase;
017    
018    import org.apache.commons.cli.CommandLine;
019    
020    
021    /**
022     * @author Mauro Talevi
023     */
024    public class StandaloneTestCase extends TestCase {
025    
026        public void testShouldBeAbleToInvokeMainMethodWithScriptFromFile() throws IOException, ClassNotFoundException {
027            File absoluteScriptPath = getAbsoluteScriptPath();
028            Standalone.main(new String[] {
029                "-c",
030                absoluteScriptPath.getAbsolutePath(),
031                "-n"
032            });
033        }
034    
035        public void testShouldBeAbleToInvokeMainMethodWithScriptFromClasspathWithXmlIncludes() throws IOException, ClassNotFoundException {
036            Standalone.main(new String[] {
037                "-r",
038                "/org/nanocontainer/nanocontainer-with-include.xml", 
039                "-n"
040            });
041        }
042    
043        private File getAbsoluteScriptPath() {
044            String className = getClass().getName();
045            String relativeClassPath = "/" + className.replace('.', '/') + ".class";
046            URL classURL = Standalone.class.getResource(relativeClassPath);
047            String absoluteClassPath = classURL.getFile();
048            File absoluteDirPath = new File(absoluteClassPath).getParentFile();
049            File absoluteScriptPath = new File(absoluteDirPath, "nanocontainer.xml");
050            return absoluteScriptPath;
051        }
052    
053        public void testCommandLineWithHelp() throws Exception {
054            CommandLine cl = Standalone.getCommandLine(new String[]{"-h"}, Standalone.createOptions());
055            assertTrue(cl.hasOption('h'));
056            assertFalse(cl.hasOption('v'));
057            assertNull(cl.getOptionValue('c'));
058            assertFalse(cl.hasOption('q'));
059            assertFalse(cl.hasOption('n'));
060        }
061    
062        public void testCommandLineWithVersion() throws Exception {
063            CommandLine cl = Standalone.getCommandLine(new String[]{"-v"}, Standalone.createOptions());
064            assertFalse(cl.hasOption('h'));
065            assertTrue(cl.hasOption('v'));
066            assertNull(cl.getOptionValue('c'));
067            assertFalse(cl.hasOption('q'));
068            assertFalse(cl.hasOption('n'));
069        }
070    
071        public void testCommandLineWithCompostion() throws Exception {
072            CommandLine cl = Standalone.getCommandLine(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() throws Exception {
081            CommandLine cl = Standalone.getCommandLine(new String[]{"-cpath", "-q"}, Standalone.createOptions());
082            assertFalse(cl.hasOption('h'));
083            assertFalse(cl.hasOption('v'));
084            assertEquals("path", cl.getOptionValue('c'));
085            assertTrue(cl.hasOption('q'));
086            assertFalse(cl.hasOption('n'));
087        }
088    
089        public void testCommandLineWithCompositionAndQuietAndNowait() throws Exception {
090            CommandLine cl = Standalone.getCommandLine(new String[]{"-cpath", "-q", "-n"}, Standalone.createOptions());
091            assertFalse(cl.hasOption('h'));
092            assertFalse(cl.hasOption('v'));
093            assertEquals("path", cl.getOptionValue('c'));
094            assertTrue(cl.hasOption('q'));
095            assertTrue(cl.hasOption('n'));
096        }
097    
098    }