All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gemstone.gemfire.management.internal.cli.shell.GfshExecutionStrategyJUnitTest Maven / Gradle / Ivy

There is a newer version: 2.0-BETA
Show newest version
/*
 * Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You
 * may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License. See accompanying
 * LICENSE file.
 */
package com.gemstone.gemfire.management.internal.cli.shell;

import java.util.List;


import junit.framework.TestCase;

import org.springframework.shell.core.CommandMarker;
import org.springframework.shell.core.annotation.CliCommand;
import org.springframework.shell.core.annotation.CliOption;
import org.springframework.shell.event.ParseResult;

import com.gemstone.gemfire.management.cli.Result;
import com.gemstone.gemfire.management.internal.cli.CommandManager;
import com.gemstone.gemfire.management.internal.cli.GfshParser;
import com.gemstone.gemfire.management.internal.cli.annotation.CliArgument;
import com.gemstone.gemfire.management.cli.CliMetaData;
import com.gemstone.gemfire.management.cli.ConverterHint;
import com.gemstone.gemfire.management.internal.cli.i18n.CliStrings;
import com.gemstone.gemfire.management.internal.cli.result.ResultBuilder;
import com.gemstone.gemfire.management.internal.cli.shell.Gfsh;
import com.gemstone.gemfire.management.internal.cli.shell.GfshConfig;
import com.gemstone.gemfire.management.internal.cli.shell.GfshExecutionStrategy;

/**
 * GfshExecutionStrategyTest - Includes tests to for GfshExecutionStrategyTest
 * 
 * @author apande
 * 
 */
public class GfshExecutionStrategyJUnitTest extends TestCase {
  private static final String COMMAND1_NAME = "command1";
  private static final String COMMAND1_NAME_ALIAS = "command1_alias";
  private static final String COMMAND2_NAME = "command2";
  private static final String COMMAND1_SUCESS = "Command1 Executed successfully";
  private static final String COMMAND2_SUCESS = "Command2 Executed successfully";
  private static final String COMMAND1_HELP = "help for " + COMMAND1_NAME;

  
  //tests execute method by executing dummy method command1
  public void testGfshExecutionStartegyExecute() throws Exception {

    CommandManager commandManager = null;
    try {
      commandManager = CommandManager.getInstance();
      assertNotNull("CommandManager should not be null.", commandManager);      
      commandManager.add(Commands.class.newInstance());
      GfshParser parser = new GfshParser(commandManager);
      String[] command1Names = ((CliCommand) Commands.class.getMethod(
          COMMAND1_NAME).getAnnotation(CliCommand.class)).value();
      String input =command1Names[0];
      ParseResult parseResult = null;   
      parseResult = parser.parse(input);  
      String[] args = new String[] {command1Names[0]  };
      Gfsh gfsh = Gfsh.getInstance(false, args, new GfshConfig());      
      GfshExecutionStrategy gfshExecutionStrategy = new GfshExecutionStrategy(gfsh);
      Result resultObject = (Result)gfshExecutionStrategy.execute(parseResult);     
      String str  = resultObject.nextLine();      
      assertTrue(str.trim().equals(COMMAND1_SUCESS));      
    } catch (Exception e) {   
      throw e;
    }
  }
  
  //tests isReadyForCommnads method by executing dummy method command1.
  //TODO: this method is hard coded in source which may change in future. So this 
  //test should also be accordingly changed
  
  public void testGfshExecutionStartegyIsReadyForCommands() throws Exception {

    CommandManager commandManager = null;
    try {
      commandManager = CommandManager.getInstance();
      assertNotNull("CommandManager should not be null.", commandManager);
      commandManager.add(Commands.class.newInstance());      
      String[] command1Names = ((CliCommand) Commands.class.getMethod(
          COMMAND1_NAME).getAnnotation(CliCommand.class)).value();       
      String[] args = new String[] {command1Names[0]  };
      Gfsh gfsh = Gfsh.getInstance(false, args, new GfshConfig());      
      GfshExecutionStrategy gfshExecutionStrategy = new GfshExecutionStrategy(gfsh);
      boolean ready = gfshExecutionStrategy.isReadyForCommands();      
      assertTrue(ready);      
    } catch (Exception e) {   
      throw e;
    }
  }

  //represents class for dummy methods
  public static class Commands implements CommandMarker {

    @CliCommand(value = { COMMAND1_NAME, COMMAND1_NAME_ALIAS }, help = COMMAND1_HELP)
    @CliMetaData(shellOnly = true )
    public static Result command1() {     
      return ResultBuilder.createInfoResult(COMMAND1_SUCESS);      
    }

    @CliCommand(value = { COMMAND2_NAME })
    @CliMetaData(shellOnly = false )
    public static Result command2() {
      return ResultBuilder.createInfoResult(COMMAND2_SUCESS);      
    }

    @CliCommand(value = { "testParamConcat" })
    public static Result testParamConcat(
        @CliOption(key = { "string" })
        String string,
        @CliOption(key = { "stringArray" })
        @CliMetaData(valueSeparator = ",")
        String[] stringArray,
        @CliOption(key = { "stringList" }, optionContext = ConverterHint.STRING_LIST)
        @CliMetaData(valueSeparator = ",")
        List stringList, @CliOption(key = { "integer" })
        Integer integer, @CliOption(key = { "colonArray" })
        @CliMetaData(valueSeparator = ":")
        String[] colonArray) {
      return null;
    }

    @CliCommand(value = { "testMultiWordArg" })
    public static Result testMultiWordArg(@CliArgument(name = "arg1")
    String arg1, @CliArgument(name = "arg2")
    String arg2) {
      return null;
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy