FileDocCategorySizeDatePackage
S1ASCommandTest.javaAPI DocGlassfish v2 API16330Fri May 04 22:25:18 BST 2007com.sun.enterprise.cli.commands

S1ASCommandTest

public class S1ASCommandTest extends TestCase
Execute these tests using gmake (and Ant) by: cd gmake ANT_TARGETS=CommandTest.java

Fields Summary
S1ASCommand
testCommand
Constructors Summary
public S1ASCommandTest(String name)

        super(name);
    
Methods Summary
public static voidmain(java.lang.String[] args)

        final TestRunner runner= new TestRunner();
        final TestResult result = runner.doRun(S1ASCommandTest.suite(), false);
        System.exit(result.errorCount() + result.failureCount());
    
private voidnyi()

        fail("Not Yet Implemented");
    
protected voidsetUp()


        
        //Properties systemProperties = new java.util.Propertis();
        //systemProperties.put("com.sun.aas.configRoot",)
        String configProperty = SystemPropertyConstants.CONFIG_ROOT_PROPERTY;
        //System.out.println(configProperty + " = " + System.getProperty(configProperty));
        final CLIDescriptorsReader cliDescriptorsReader = CLIDescriptorsReader.getInstance();
        ValidCommand validCommand = cliDescriptorsReader.getCommand(null);
        LocalStringsManagerFactory.setCommandLocalStringsManagerProperties(
            CLIDescriptorsReader.getInstance().getProperties());
        testCommand = new S1ASCommand() {
                public void runCommand()
                    throws CommandException, CommandValidationException
                {
                    setProperties(new Properties());
                }
                public boolean validateOptions() throws CommandValidationException
                {
                    return true;
                }
            };
        testCommand.setName("sampleCommand");
        testCommand.runCommand();
    
public static junit.framework.Testsuite()

        TestSuite suite = new TestSuite(S1ASCommandTest.class);
        return suite;
    
protected voidtearDown()

    
public voidtestCheckForFileExistence()

        final File f = File.createTempFile("S1ASCommandTest_testCheckForFileExistence", ".tmp");
        f.deleteOnExit();
        String tempdir = System.getProperty("java.io.tmpdir");
        assertTrue(testCommand.checkForFileExistence(tempdir, f.getName()).exists());
    
public voidtestCheckForFileExistenceWhenNoFile()

        String tempdir = System.getProperty("java.io.tmpdir");
        try{
            File f = testCommand.checkForFileExistence(tempdir, "FileDoesNotExist");
        }catch (Exception e){
            assertEquals(e.getMessage(), "CLI146 FileDoesNotExist does not exist in the file system or read permission denied.");
        }
    
public voidtestCreatePropertiesParam()

        String propStr = "name=value";
        Properties props = testCommand.createPropertiesParam(propStr);
        assertEquals(props.getProperty("name"),"value");
    
public voidtestCreatePropertiesParamInvalid()

        String propStr = "name1=value1:value2";
        try{
            Properties props = testCommand.createPropertiesParam(propStr);
        }catch (Exception e){
            assertEquals(e.getMessage(), "CLI131 Invalid property syntax.");
        }
    
public voidtestCreatePropertiesParamWithEscapes()

        String propStr = "user=dbuser:password=dbpassword:DatabaseName=jdbc\\:pointbase:server=http\\://localhost\\:9292";
        Properties props = testCommand.createPropertiesParam(propStr);
        assertEquals(props.getProperty("user"),"dbuser");
        assertEquals(props.getProperty("password"), "dbpassword");
        assertEquals(props.getProperty("DatabaseName"), "jdbc:pointbase");
        assertEquals(props.getProperty("server"), "http://localhost:9292");
    
public voidtestCreateStringArrayParam()

        String paramStr = "value1:value2";
        String[] params = testCommand.createStringArrayParam(paramStr);
        assertEquals(params[0],"value1");
        assertEquals(params[1],"value2");
    
public voidtestCreateStringArrayParamWithEscapes()

        String paramStr = "-XX\\:NewRatio=2";
        String[] params = testCommand.createStringArrayParam(paramStr);
        assertEquals(params[0],"-XX:NewRatio=2");
    
public voidtestDisplayExceptionMessage()

        NullPointerException npe = new NullPointerException("a null pointer");
        try{
            testCommand.displayExceptionMessage(npe);
        }
        catch (CommandException ce){
            assertEquals(npe, ExceptionUtil.getRootCause(ce));
        }
    
public voidtestgetHost()

        testCommand.setOption("host", "localhost");
        assertEquals(testCommand.getHost(),"localhost");
    
public voidtestgetInteractiveOptionWhenFalse()

        final CLIDescriptorsReader cliDescriptorsReader = CLIDescriptorsReader.getInstance();
        ValidCommand validCommand = cliDescriptorsReader.getCommand(null);
        LocalStringsManagerFactory.setCommandLocalStringsManagerProperties(
            CLIDescriptorsReader.getInstance().getProperties());
        
        testCommand.setOption("interactive", "false");
        try{
        testCommand.getInteractiveOption("test", "test");
        }
        catch (CommandValidationException cve){
            assertEquals(cve.getMessage(), "CLI152 test is a required option.");
        }
    
public voidtestgetObjectName()

        Vector propertyValues = new Vector();
        propertyValues.add("test_objectname");
        testCommand.setProperty(testCommand.OBJECT_NAME, propertyValues);
        assertEquals(testCommand.getObjectName(), "test_objectname");
    
public voidtestgetOperationName()

        Vector propertyValues = new Vector();
        propertyValues.add("testMethod");
        testCommand.setProperty(testCommand.OPERATION, propertyValues);
        assertEquals(testCommand.getOperationName(), "testMethod");
    
public voidtestgetParamsInfo()

        
        Vector typesValues = new Vector();
        for (int i=0; i<3; i++){
            typesValues.add("java.lang.String");
        }
        testCommand.setProperty(testCommand.PARAM_TYPES, typesValues);
        
        Vector paramValues = new Vector();
        for (int i=0; i<3; i++){
            paramValues.add("x"+i);
        }
        testCommand.setProperty(testCommand.PARAMS, paramValues);
        Object[] params = testCommand.getParamsInfo();
        for (int i=0; i<params.length; i++)
            assertEquals((String)params[i], "x"+i);
    
public voidtestgetParamsInfoInvalid()

        //#of param typess is not equal to #of param values
        Vector typesValues = new Vector();
        typesValues.add("java.lang.String");
        typesValues.add("java.lang.String");
        testCommand.setProperty(testCommand.PARAM_TYPES, typesValues);
        
        Vector paramValues = new Vector();
        for (int i=0; i<3; i++){
            paramValues.add("x"+i);
        }
        testCommand.setProperty(testCommand.PARAMS, paramValues);
        try{
            Object[] params = testCommand.getParamsInfo();
        }catch (CommandException ce){
            assertEquals(ce.getMessage(), 
                    "CLI174 Error in CLIDescriptor.xml -- The number of params doesn''t match the number of param-types.  Solution: edit this command in CLIDescriptor.xml.");
        }
    
public voidtestgetPasswordFromCommandLine()

        testCommand.setOption("password", "test_password");
        String pwd = testCommand.getPassword(testCommand.PASSWORD, null, null, 
                                    true, false, false, false, null, null, false, 
                                    false, true, false);
        assertEquals(pwd, "test_password");
    
public voidtestgetPasswordFromPrefsFile()

        final String enc = "ISO-8859-1";
        final File f = new File(System.getProperty("java.io.tmpdir"),
                                testCommand.ASADMINPREFS);
        f.deleteOnExit();
        final PrintWriter pw = new PrintWriter (
                new OutputStreamWriter(new FileOutputStream(f), enc));
        pw.println("AS_ADMIN_PASSWORD=test_password");
        pw.close();
        System.setProperty("user.home", System.getProperty("java.io.tmpdir"));
        //set the readPrefsFile & readPasswordOptionFromPrefs to true
        String pwd = testCommand.getPassword(testCommand.PASSWORD, null, null, 
                                    false, true, true, false, null, null, false, 
                                    false, true, false);
        assertEquals(pwd, "test_password");
    
public voidtestgetPasswordWhenNotSet()

        String pwd = testCommand.getPassword(testCommand.PASSWORD, null, null, 
                                    false, false, false, false, null, null, false, 
                                    false, false, false);
        assertEquals(pwd, null);
    
public voidtestgetPort()

        testCommand.setOption(testCommand.PORT, "4848");
        assertEquals(testCommand.getPort(), 4848);
    
public voidtestgetPortInvalid()

        try{
            testCommand.setOption(testCommand.PORT, "xyz");
            testCommand.getPort();
        }catch (Exception e){
            assertEquals(e.getMessage(), "CLI136 Port xyz should be a numeric value.");
        }
    
public voidtestgetReturnType()

        Vector propertyValues = new Vector();
        propertyValues.add("java.lang.String");
        testCommand.setProperty(testCommand.RETURN_TYPE, propertyValues);
        assertEquals(testCommand.getReturnType(), "java.lang.String");
    
public voidtestgetTypesInfo()

        Vector propertyValues = new Vector();
        for (int i=0; i<=3; i++){
            propertyValues.add("java.lang.String");
        }
        testCommand.setProperty(testCommand.PARAM_TYPES, propertyValues);
        String[] types = testCommand.getTypesInfo();
        for (int i=0; i<types.length; i++)
            assertEquals(types[i], "java.lang.String");
    
public voidtestgetUserFromCommandLine()

        testCommand.setOption("user", "admin");
        String user = testCommand.getUser();
        assertEquals(user, "admin");
    
public voidtestgetUserFromPrefsFile()

        final String enc = "ISO-8859-1";
        final File f = new File(System.getProperty("java.io.tmpdir"),
                                testCommand.ASADMINPREFS);
        f.deleteOnExit();
        final PrintWriter pw = new PrintWriter (
                new OutputStreamWriter(new FileOutputStream(f), enc));
        pw.println("AS_ADMIN_USER=admin");
        pw.close();
        System.setProperty("user.home", System.getProperty("java.io.tmpdir"));
        //set the readPrefsFile & readPasswordOptionFromPrefs to true
        String user = testCommand.getUser();
        assertEquals(user, "admin");
    
public voidtestgetUserWhenNotSet()

        try{
            String adminUser = testCommand.getUser();
        }catch(Exception e){
            assertEquals(e.getMessage(), "CLI152 user is a required option.");
        }
    
public voidtestgetValuesFromASADMINPREFS()

        final String enc = "ISO-8859-1";
        final File f = new File(System.getProperty("java.io.tmpdir"),
                                testCommand.ASADMINPREFS);
        f.deleteOnExit();
        final PrintWriter pw = new PrintWriter (
                new OutputStreamWriter(new FileOutputStream(f), enc));
        pw.println("AS_ADMIN_PASSWORD=test_password");
        pw.close();
        System.setProperty("user.home", System.getProperty("java.io.tmpdir"));
        assertEquals(testCommand.getValuesFromASADMINPREFS(testCommand.PASSWORD), 
                        "test_password");
    
public voidtestisPasswordValid()

        boolean isValid = testCommand.isPasswordValid("eightOrMoreCharacters");
        assertEquals(isValid, true);
        isValid = testCommand.isPasswordValid("7chars");
        assertEquals(isValid, false);
    
public voidtestreplacePattern()

        
        Vector typesValues = new Vector();
        typesValues.add("java.lang.String");
        testCommand.setProperty(testCommand.PARAM_TYPES, typesValues);
        testCommand.setOption("testOption", "test");
        Vector paramValues = new Vector();
        paramValues.add("{$testOption}");
        testCommand.setProperty(testCommand.PARAMS, paramValues);
        Object[] params = testCommand.getParamsInfo();
        assertEquals((String)params[0], "test");
    
public voidtestsetLoggerLevel()

        testCommand.setOption(testCommand.TERSE, "false");
        testCommand.setLoggerLevel();
        assertEquals(CLILogger.getInstance().getOutputLevel(),java.util.logging.Level.FINE);
        testCommand.setOption(testCommand.TERSE, "true");
        testCommand.setLoggerLevel();
        assertEquals(CLILogger.getInstance().getOutputLevel(),java.util.logging.Level.INFO);