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