[Title] Added multiple device select logic And Fixed run/debug run command
authorgyeongseok.seo <gyeongseok.seo@samsung.com>
Wed, 18 Jul 2012 11:28:42 +0000 (20:28 +0900)
committergyeongseok.seo <gyeongseok.seo@samsung.com>
Wed, 18 Jul 2012 11:28:42 +0000 (20:28 +0900)
[Type] Enhancement
[Module] cli
[Priority] Major
[Jira#]
[Redmine#] 5831
[Problem]
[Cause]
[Solution]
[TestCase]

Change-Id: I6345ac79982a768d23a2ebbe32b02a28d2dc4fba

org.tizen.cli/src/org/tizen/cli/exec/TargetDeviceSelector.java
org.tizen.cli/src/org/tizen/cli/exec/debug/Main.java
org.tizen.cli/src/org/tizen/cli/exec/run/Main.java
org.tizen.cli/test/src/org/tizen/cli/exec/TargetDeviceSelectorTest.java

index d50bf87..c9d450b 100644 (file)
@@ -29,9 +29,11 @@ import java.text.MessageFormat;
 
 import org.tizen.common.core.command.Prompter;
 import org.tizen.common.core.command.prompter.ChoiceOption;
+import org.tizen.common.core.command.prompter.GenericOption;
 import org.tizen.common.core.command.prompter.Option;
 import org.tizen.common.core.command.sdb.SmartDevelopmentBridgeManager;
 import org.tizen.common.util.ArrayUtil;
+import org.tizen.common.util.StringUtil;
 import org.tizen.sdblib.IDevice;
 
 
@@ -66,11 +68,11 @@ TargetDeviceSelector
     }
 
     protected void printDeviceList( IDevice[] devices ) {
-        prompter.notify( "-----------------------------\n" );
-        prompter.notify( "currnet connected target List\n" );
-        prompter.notify( "-----------------------------\n" );
+        prompter.notify( "-----------------------------" );
+        prompter.notify( "currnet connected target List" );
+        prompter.notify( "-----------------------------" );
         for( int i = 0; i < devices.length; i++ ) {
-            prompter.notify( (i+1) + ") " + devices[i].toString() +"\n" );
+            prompter.notify( (i+1) + ") " + devices[i].toString() );
         }
     }
 
@@ -143,7 +145,7 @@ TargetDeviceSelector
             final Option option = prompter.interact(
                 MessageFormat.format( "device is not selected\n" +
                                       "[{0}] device select and\n" +
-                                      "Do you want to be continue?", devices[0].toString() ),
+                                      "Do you want to be continue ?", devices[0].toString() ),
                 yes,
                 no
             );
@@ -153,22 +155,42 @@ TargetDeviceSelector
             } else if ( no.equals( option ) ) {
                 printDeviceList( devices );
 
-                ChoiceOption num = new ChoiceOption( "Num" );
+                ChoiceOption cancel = new ChoiceOption( "Cancel", true );
+                GenericOption num = new GenericOption( "Num" );
 
                 final Option op = prompter.interact(
-                    "select device or cancel process\n",
-                    num,
-                    no
+                    "select device or cancel process ",
+                    cancel,
+                    num
                 );
 
-                if( no.equals( op ) ) {
+                if( cancel.equals( op ) ) {
                     prompter.notify( "Process is canceled, please selected device" );
-
                 } else {
-                    int index = Integer.parseInt( op.getName() );
-                    prompter.notify( "select : " + op.getName() );
-                    selectDevice = ArrayUtil.get( devices, index-1 );
-                    // TODO : Needed implement - select device using option value
+                    String answer = ((GenericOption)op).getAnswer();
+                    if ( !StringUtil.isEmpty( answer ) ) {
+                        boolean isCorrect = false;
+                        int index = 0;
+                        try {
+                            index = Integer.parseInt( answer );
+
+                            // answer check
+                            for ( int i = 1; i <= devices.length; i++ ) {
+                                if ( index == i ) {
+                                    isCorrect = true;
+                                    break;
+                                }
+                            }
+                        } catch (NumberFormatException e) {
+                        } finally {
+                            if ( isCorrect ) {
+                                selectDevice = ArrayUtil.get( devices, index-1 );
+                                prompter.notify( "Select Device is : " + selectDevice.toString() );
+                            } else {
+                                prompter.notify( "Process is canceled, please selected correct device" );
+                            }
+                        }
+                    }
                 }
             }
         }
index 74c6f0c..b4a2f11 100644 (file)
@@ -25,8 +25,6 @@
 */
 package org.tizen.cli.exec.debug;
 
-import java.text.MessageFormat;
-
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.OptionBuilder;
 import org.apache.commons.cli.Options;
@@ -36,10 +34,13 @@ import org.tizen.common.core.command.Prompter;
 import org.tizen.common.core.command.sdb.PushSdbCommand;
 import org.tizen.common.file.FileHandler;
 import org.tizen.common.file.FileHandler.Attribute;
+import org.tizen.common.util.FilenameUtil;
+import org.tizen.common.util.StringUtil;
 import org.tizen.web.launch.cli.command.CheckInstallCommand;
 import org.tizen.web.launch.cli.command.DebugCommand;
 import org.tizen.web.launch.cli.command.InstallCommand;
-import org.tizen.web.launch.cli.command.RunCommand;
+import org.tizen.web.launch.cli.command.KillCommand;
+import org.tizen.web.launch.cli.command.ModeCommnad;
 
 /**
  * Command Line Interface for debug run widget in target
@@ -124,43 +125,77 @@ extends WRTLauncher
 
         // device not connect then exit
         if ( getDevice() == null ) {
-            prompter.notify( "Process is stoped, please select connect device" );
+            endSDB();
             return ;
         }
 
         // local widget file path control
-        String localWidgetFileName = cmdLine.getOptionValue( OPT_WIDGETFILE );
-        String localWidgetFilePath = (String)fileHandler.get( localWidgetFileName, Attribute.PATH );
+        String inputName = cmdLine.getOptionValue( OPT_WIDGETFILE );
+        logger.trace("input file name : {} ", inputName);
+        if ( StringUtil.isEmpty( inputName ) ) {
+            prompter.notify( "widget file not selected." );
+            printHelp();
+            endSDB();
+        }
+        String widgetId = cmdLine.getOptionValue( OPT_WIDGETID );
+
+        String filePath = (String)fileHandler.get( inputName, Attribute.PATH );
+        logger.trace( "local widget file path : {}", filePath );
+
+        String fileName = FilenameUtil.getFilename( inputName );
+        logger.trace( "local widget file name : {}", fileName );
 
         // core operation
         InstallCommand install_command = new InstallCommand();
+        install_command.setWidget( fileName );
         install_command.setDevice( getDevice() );
-        String remotePath = install_command.getRemotePath();
+        String remotePath = install_command.getRemotePath() + fileName;
+        logger.trace( "remote widget file path : {}", remotePath );
 
-        PushSdbCommand push_command = new PushSdbCommand( localWidgetFilePath, remotePath );
+        PushSdbCommand push_command = new PushSdbCommand( filePath, remotePath );
         push_command.setDevice( getDevice() );
 
+        ModeCommnad mode_command = new ModeCommnad();
+        mode_command.setDevice( getDevice() );
 
         // widget push
+        logger.trace( "push command");
         exec.execute( push_command );
+        if ( "failed".equals( push_command.getResult() ) ) {
+            endSDB();
+        }
 
-        // widget install
-        exec.execute( install_command );
+        // widget mode
+        logger.trace( "mode command");
+        exec.execute( mode_command );
+        if ( mode_command.getResult().contains( "failed" ) ) {
+            endSDB();
+        }
 
-        // install check
-        String widgetID = cmdLine.getOptionValue( OPT_WIDGETID );
-        CheckInstallCommand install_check_command = new CheckInstallCommand( widgetID );
-        exec.execute( install_check_command );
-        if ( !install_check_command.isInstalled() ) {
-            prompter.notify(
-                    MessageFormat.format( "Process is stoped, [{0}] widget is not installed", widgetID )
-                    );
-            return ;
+        if ( !StringUtil.isEmpty( widgetId ) ) {
+            logger.trace( "same widget's pre-install check and kill process" );
+            CheckInstallCommand check_install_cmd = new CheckInstallCommand( widgetId );
+            check_install_cmd.setDevice( getDevice() );
+            check_install_cmd.setPrintOption( false );
+            exec.execute( check_install_cmd );
+            if ( check_install_cmd.isInstalled() ) {
+                KillCommand kill_cmd = new KillCommand( widgetId );
+                kill_cmd.setPrintOption( false );
+                kill_cmd.setDevice( getDevice() );
+                exec.execute( kill_cmd );
+            }
         }
 
+        // widget install
+        logger.trace( "install command");
+        exec.execute( install_command );
+
         // widget debug run
-        DebugCommand run_command = new DebugCommand( widgetID );
+        DebugCommand run_command = new DebugCommand( widgetId );
+        run_command.setDevice( getDevice() );
         exec.execute( run_command );
+
+        endSDB();
     }
 
     /* (non-Javadoc)
index 17a0e50..eca825c 100644 (file)
@@ -25,8 +25,6 @@
 */
 package org.tizen.cli.exec.run;
 
-import java.text.MessageFormat;
-
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.OptionBuilder;
 import org.apache.commons.cli.Options;
@@ -36,8 +34,12 @@ import org.tizen.common.core.command.Prompter;
 import org.tizen.common.core.command.sdb.PushSdbCommand;
 import org.tizen.common.file.FileHandler;
 import org.tizen.common.file.FileHandler.Attribute;
+import org.tizen.common.util.FilenameUtil;
+import org.tizen.common.util.StringUtil;
 import org.tizen.web.launch.cli.command.CheckInstallCommand;
 import org.tizen.web.launch.cli.command.InstallCommand;
+import org.tizen.web.launch.cli.command.KillCommand;
+import org.tizen.web.launch.cli.command.ModeCommnad;
 import org.tizen.web.launch.cli.command.RunCommand;
 
 /**
@@ -123,43 +125,77 @@ extends WRTLauncher
 
         // device not connect then exit
         if ( getDevice() == null ) {
-            prompter.notify( "Process is stoped, please select connect device" );
+            endSDB();
             return ;
         }
 
         // local widget file path control
-        String localWidgetFileName = cmdLine.getOptionValue( OPT_WIDGETFILE );
-        String localWidgetFilePath = (String)fileHandler.get( localWidgetFileName, Attribute.PATH );
+        String inputName = cmdLine.getOptionValue( OPT_WIDGETFILE );
+        logger.trace("input file name : {} ", inputName);
+        if ( StringUtil.isEmpty( inputName ) ) {
+            prompter.notify( "widget file not selected." );
+            printHelp();
+            endSDB();
+        }
+        String widgetId = cmdLine.getOptionValue( OPT_WIDGETID );
+
+        String filePath = (String)fileHandler.get( inputName, Attribute.PATH );
+        logger.trace( "local widget file path : {}", filePath );
+
+        String fileName = FilenameUtil.getFilename( inputName );
+        logger.trace( "local widget file name : {}", fileName );
 
         // core operation
         InstallCommand install_command = new InstallCommand();
+        install_command.setWidget( fileName );
         install_command.setDevice( getDevice() );
-        String remotePath = install_command.getRemotePath();
+        String remotePath = install_command.getRemotePath() + fileName;
+        logger.trace( "remote widget file path : {}", remotePath );
 
-        PushSdbCommand push_command = new PushSdbCommand( localWidgetFilePath, remotePath );
+        PushSdbCommand push_command = new PushSdbCommand( filePath, remotePath );
         push_command.setDevice( getDevice() );
 
+        ModeCommnad mode_command = new ModeCommnad();
+        mode_command.setDevice( getDevice() );
 
         // widget push
+        logger.trace( "push command");
         exec.execute( push_command );
+        if ( "failed".equals( push_command.getResult() ) ) {
+            endSDB();
+        }
 
-        // widget install
-        exec.execute( install_command );
+        // widget mode
+        logger.trace( "mode command");
+        exec.execute( mode_command );
+        if ( mode_command.getResult().contains( "failed" ) ) {
+            endSDB();
+        }
 
-        // install check
-        String widgetID = cmdLine.getOptionValue( OPT_WIDGETID );
-        CheckInstallCommand install_check_command = new CheckInstallCommand( widgetID );
-        exec.execute( install_check_command );
-        if ( !install_check_command.isInstalled() ) {
-            prompter.notify(
-                    MessageFormat.format( "Process is stoped, [{0}] widget is not installed", widgetID )
-                    );
-            return ;
+        if ( !StringUtil.isEmpty( widgetId ) ) {
+            logger.trace( "same widget's pre-install check and kill process" );
+            CheckInstallCommand check_install_cmd = new CheckInstallCommand( widgetId );
+            check_install_cmd.setDevice( getDevice() );
+            check_install_cmd.setPrintOption( false );
+            exec.execute( check_install_cmd );
+            if ( check_install_cmd.isInstalled() ) {
+                KillCommand kill_cmd = new KillCommand( widgetId );
+                kill_cmd.setPrintOption( false );
+                kill_cmd.setDevice( getDevice() );
+                exec.execute( kill_cmd );
+            }
         }
 
+        // widget install
+        logger.trace( "install command");
+        exec.execute( install_command );
+
         // widget run
-        RunCommand run_command = new RunCommand( widgetID );
+        RunCommand run_command = new RunCommand( widgetId );
+        run_command.setDevice( getDevice() );
         exec.execute( run_command );
+
+        endSDB();
     }
 
     /* (non-Javadoc)
index 2e8e660..6819b59 100644 (file)
@@ -31,6 +31,7 @@ import org.junit.Before;
 import org.junit.Test;
 import org.tizen.common.core.command.Prompter;
 import org.tizen.common.core.command.prompter.ChoiceOption;
+import org.tizen.common.core.command.prompter.GenericOption;
 import org.tizen.sdblib.IDevice;
 
 import static org.mockito.Mockito.mock;
@@ -113,18 +114,18 @@ TargetDeviceSelectorTest
     {
         try {
             testClass.printDeviceList( mockDevices );
-            verify( mockPropter ).notify( "currnet connected target List\n" );
-            verify( mockPropter ).notify( "1) test1\n" );
-            verify( mockPropter ).notify( "2) test2\n" );
+            verify( mockPropter ).notify( "currnet connected target List" );
+            verify( mockPropter ).notify( "1) test1" );
+            verify( mockPropter ).notify( "2) test2" );
         } catch (Exception e) {
             fail( "devices is not null then Not throw exception" );
         }
 
         try {
             testClass.printDeviceList( null );
-            verify( mockPropter ).notify( "-----------------------------\n" );
-            verify( mockPropter ).notify( "currnet connected target List\n" );
-            verify( mockPropter ).notify( "-----------------------------\n" );
+            verify( mockPropter ).notify( "-----------------------------" );
+            verify( mockPropter ).notify( "currnet connected target List" );
+            verify( mockPropter ).notify( "-----------------------------" );
             fail( "devices is null then throw exception" );
         } catch (Exception e) {}
     }
@@ -211,7 +212,7 @@ TargetDeviceSelectorTest
         ChoiceOption no = new ChoiceOption( "No", true );
         when( mockPropter.interact( "device is not selected\n" +
                                     "[test1] device select and\n" +
-                                    "Do you want to be continue?",
+                                    "Do you want to be continue ?",
                                     yes,
                                     no
                                     )
@@ -220,18 +221,19 @@ TargetDeviceSelectorTest
 
         when( mockPropter.interact( "device is not selected\n" +
                 "[test1] device select and\n" +
-                "Do you want to be continue?",
+                "Do you want to be continue ?",
                 yes,
                 no
                 )
             ).thenReturn( no );
-        ChoiceOption num = new ChoiceOption( "Num" );
+        ChoiceOption cancel = new ChoiceOption( "Cancel", true );
+        GenericOption num = new GenericOption( "Num" );
 
-        when( mockPropter.interact( "select device or cancel process\n",
-                num,
-                no
+        when( mockPropter.interact( "select device or cancel process ",
+                cancel,
+                num
                 )
-            ).thenReturn( no );
+            ).thenReturn( cancel );
         assertEquals( null, testClass.selectDevice() );
         verify( mockPropter ).notify( "Process is canceled, please selected device" );
     }