CLI: Fix bug with no argument 60/11660/1
authorHeongseok Heo <hyeongseok.heo@samsung.com>
Fri, 1 Nov 2013 07:47:12 +0000 (16:47 +0900)
committerHeongseok Heo <hyeongseok.heo@samsung.com>
Fri, 1 Nov 2013 07:47:12 +0000 (16:47 +0900)
Add null check when there is no argument
Modify test case for no option in config cli.

Change-Id: If80fdb43d75609660d098787a908c7d13d088fa8
Signed-off-by: Heongseok Heo <hyeongseok.heo@samsung.com>
org.tizen.ncli.ide/.classpath
org.tizen.ncli.ide/src/org/tizen/core/ide/BuildWebAppModule.java
org.tizen.ncli.ide/src/org/tizen/ncli/ide/shell/AbstractCLI.java
org.tizen.ncli.ide/src/org/tizen/ncli/ide/shell/Main.java
org.tizen.ncli.ide/test/src/org/tizen/ncli/ide/shell/BuildWebCLITest.java [new file with mode: 0644]
org.tizen.ncli.ide/test/src/org/tizen/ncli/ide/shell/ConfigCLITest.java

index 0643ce9..4520449 100644 (file)
@@ -5,6 +5,6 @@
        <classpathentry kind="src" path="src"/>
        <classpathentry kind="src" path="test/src"/>
        <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
-       <classpathentry kind="lib" path="lib/args4j-2.0.25.jar"/>
+       <classpathentry kind="lib" path="lib/args4j-2.0.25.jar" sourcepath="/args4j/args4j/src"/>
        <classpathentry kind="output" path="bin"/>
 </classpath>
index e51f7d6..46d338e 100644 (file)
@@ -66,20 +66,16 @@ public class BuildWebAppModule {
     private Logger log = LoggerFactory.getLogger(getClass());
     private BuildWebParameter data;
     private String cwd;
-    private String output = ".buildResult"; 
+    private String output = ".buildResult";
     private SimpleFileFilter filter = new SimpleFileFilter(true);
     public FactoryWithArgument<Filter, String> filterFactory = new WildCardFilterFactory();
     public List<String> excludes = new ArrayList<String>();
-    private String[] DEFAULT_EXCLUDES = { 
-            ".build/*",
-            ".project",
-            ".settings/*",
-            ".sdk_delta.info", "*.wgt" 
-            };
+    private String[] DEFAULT_EXCLUDES = { ".build/*", ".project", ".settings/*", ".sdk_delta.info", "*.wgt" };
 
     public BuildWebAppModule() {
         // TODO Auto-generated constructor stub
     }
+
     /**
      * @param cwd
      *            the cwd to set
@@ -89,7 +85,8 @@ public class BuildWebAppModule {
     }
 
     /**
-     * @param data the data to set
+     * @param data
+     *            the data to set
      */
     public void setData(BuildWebParameter data) {
         this.data = data;
@@ -105,23 +102,21 @@ public class BuildWebAppModule {
         this.excludes = excludes;
     }
 
-    
-
     public void buildResources() throws IOException, BuildException, Exception {
         BuildProcess buildProcess = new BuildProcess();
-        
-        ifisPrerequisiteSatisfy()) {
+
+        if (isPrerequisiteSatisfy()) {
             // Set output
-            ifnull != data && null != data.getOutputName()) {
-                this.output = cwd+File.separator+data.getOutputName();
-            }else {
-                this.output = cwd+File.separator+this.output;
+            if (null != data && null != data.getOutputName()) {
+                this.output = cwd + File.separator + data.getOutputName();
+            } else {
+                this.output = cwd + File.separator + this.output;
             }
             // If output path exist, the path will be recreated.
             deleteOutput();
             // Set default exclude resources
             addDefaultExclude();
-            
+
             // Set start layer
             FileHandler fh = new StandardFileHandler();
             fh.setCurrentWorkingDirectory(cwd);
@@ -136,7 +131,7 @@ public class BuildWebAppModule {
                 log.trace("Total resource count: {}", resources.length);
                 // Generate web builders to build process
             }
-            
+
             generateAllBuilders(buildProcess, startLayer);
 
             if (buildProcess.getLastBuilder() != null) {
@@ -146,32 +141,35 @@ public class BuildWebAppModule {
                 // check succeeding status.
                 checkHybridStructure(buildProcess);
             }
-        }else {
+        } else {
             throw new BuildException(null);
         }
-        Assert.notNull(cwd,"Current Working Directory should not be NULL.");
+        Assert.notNull(cwd, "Current Working Directory should not be NULL.");
     }
 
     private void deleteOutput() {
-        if(FileUtil.isExist(output)) {
+        if (FileUtil.isExist(output)) {
             FileUtil.recursiveDelete(new File(output));
         }
     }
+
     private void addDefaultExclude() {
-        if( null != data && null != data.getExcludeList()) {
-            for (String defaultExclude : DEFAULT_EXCLUDES) {
-                log.trace("add exclude:{}",defaultExclude);
-                data.getExcludeList().add(defaultExclude);
-            }
+        if (null == data) {
+            data = new BuildWebParameter();
+        }
+        for (String defaultExclude : DEFAULT_EXCLUDES) {
+            log.trace("add exclude:{}", defaultExclude);
+            data.getExcludeList().add(defaultExclude);
         }
     }
+
     /**
      * Check pre-requisite for executing build
-     *        
+     * 
      * @return
      */
     private boolean isPrerequisiteSatisfy() {
-        return null != cwd && !"".equals(cwd.trim()) ;
+        return null != cwd && !"".equals(cwd.trim());
     }
 
     /**
@@ -225,8 +223,8 @@ public class BuildWebAppModule {
     private Resource[] getBuildResources(ResourceLayer layer) throws IOException {
         List<Resource> resources = new ArrayList<Resource>();
         // setIncludes(includes);
-         setExcludes(data.getExcludeList());
-        log.trace("CWD:{}",cwd);
+        setExcludes(data.getExcludeList());
+        log.trace("CWD:{}", cwd);
         String cwd = layer.getFileHandler().getCurrentWorkingDirectory();
         resources.addAll(getResources(cwd, FilenameUtil.getRelativePath(cwd, cwd), layer));
 
@@ -380,13 +378,14 @@ public class BuildWebAppModule {
 
         // TODO Notification ?? getPrompter().notify( MessageFormat.format( SUCCEED_MSG, this.output ) );
     }
-    
+
     /**
      * Set excluding resource list
+     * 
      * @param excludes
      */
     private void setExcludes(List<String> excludes) {
-//        filter.clearExcludes();
+        // filter.clearExcludes();
         if (null != excludes) {
             for (final String exclude : excludes) {
                 filter.addExcludes(filterFactory.create(exclude));
index 749c8a1..1f793b1 100644 (file)
@@ -47,16 +47,5 @@ public abstract class AbstractCLI {
 \r
     protected PrintWriter output = new PrintWriter(System.out);;\r
 \r
-    public final void execute(String[] args) {\r
-        parseArgument(args);\r
-        execute();\r
-    }\r
-\r
-    /**\r
-     * @param args\r
-     */\r
-    private void parseArgument(String[] args) {\r
-        log.trace("Could not use parsing argument again!");\r
-    }\r
     public abstract void execute();\r
 }\r
index 6447db4..3877b78 100644 (file)
@@ -3,11 +3,13 @@
  */\r
 package org.tizen.ncli.ide.shell;\r
 \r
+import java.io.PrintWriter;\r
 import java.util.List;\r
 \r
 import org.kohsuke.args4j.Argument;\r
 import org.kohsuke.args4j.CmdLineException;\r
 import org.kohsuke.args4j.CmdLineParser;\r
+import org.kohsuke.args4j.OptionHandlerFilter;\r
 import org.kohsuke.args4j.spi.OptionHandler;\r
 import org.kohsuke.args4j.spi.SubCommand;\r
 import org.kohsuke.args4j.spi.SubCommandHandler;\r
@@ -15,10 +17,9 @@ import org.kohsuke.args4j.spi.SubCommands;
 import org.slf4j.Logger;\r
 import org.slf4j.LoggerFactory;\r
 \r
-\r
 /**\r
- * This class is entry point of Tizen New Command Line Interface. All the command\r
- * line argument would be parsed and then proper Command class is called.\r
+ * This class is entry point of Tizen New Command Line Interface. All the command line argument would be parsed and then\r
+ * proper Command class is called.\r
  * \r
  * @author Harry Hyeongseok Heo{@literal <hyeongseok.heo@samsung.com>} (S-core)\r
  * \r
@@ -27,12 +28,13 @@ public class Main {
 \r
     private Logger log = LoggerFactory.getLogger(Main.class);\r
 \r
-    @Argument(index = 0, required = true, handler = SubCommandHandler.class, usage = "Command is needed!")\r
+    @Argument(index = 0, required = true, handler = SubCommandHandler.class, \r
+            usage = "Sub command is needed!" , \r
+            metaVar="[create | build-native | build-web | cli-config | sign]")\r
     @SubCommands({ @SubCommand(name = "create", impl = CreateCLI.class),\r
             @SubCommand(name = "build-native", impl = BuildNativeCLI.class),\r
             @SubCommand(name = "build-web", impl = BuildWebCLI.class),\r
-            @SubCommand(name = "sign", impl = SignCLI.class),\r
-            @SubCommand(name = "cli-config", impl = ConfigCLI.class) })\r
+            @SubCommand(name = "sign", impl = SignCLI.class), @SubCommand(name = "cli-config", impl = ConfigCLI.class) })\r
     private AbstractCLI tizenCLI;\r
 \r
     /**\r
@@ -46,22 +48,23 @@ public class Main {
 \r
     private void run(String[] args) {\r
         CmdLineParser cmdParser = new CmdLineParser(this);\r
-\r
         log.trace("Start running Tizen CLI Main class...");\r
         try {\r
-            //Argument parsing - make metadata from annotation.\r
+            // Argument parsing - make metadata from annotation.\r
             cmdParser.parseArgument(args);\r
-            ifnull != this.tizenCLI) {\r
+            if (null != this.tizenCLI) {\r
                 this.tizenCLI.execute();\r
             }\r
-            \r
+\r
         } catch (CmdLineException e) {\r
             // TODO Handle CLI exception\r
+            cmdParser.printUsage(System.out);\r
             e.printStackTrace();\r
         } catch (Exception e) {\r
-                       // TODO: handle exception\r
-               e.printStackTrace();\r
-               }\r
+            // TODO: handle exception\r
+            cmdParser.printUsage(System.out);\r
+            e.printStackTrace();\r
+        }\r
 \r
     }\r
 \r
diff --git a/org.tizen.ncli.ide/test/src/org/tizen/ncli/ide/shell/BuildWebCLITest.java b/org.tizen.ncli.ide/test/src/org/tizen/ncli/ide/shell/BuildWebCLITest.java
new file mode 100644 (file)
index 0000000..32e9b96
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * IDE
+ *
+ * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * Hyeongseok Heo <hyeongseok.heo@samsung.com>
+ * Kangho Kim <kh5325.kim@samsung.com>
+ * 
+ * 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.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ */
+package org.tizen.ncli.ide.shell;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+/**
+ * @author Harry Hyeongseok Heo{@literal <hyeongseok.heo@samsung.com>} (S-core)
+ *
+ * 
+ */
+public class BuildWebCLITest extends Args4JTestBase<BuildWebCLI> {
+
+
+    @Override
+    public BuildWebCLI getTestObject() {
+        // TODO Auto-generated method stub
+        return new BuildWebCLI();
+    }
+
+}
index d0fb964..5f81ddd 100644 (file)
@@ -57,12 +57,21 @@ public class ConfigCLITest extends Args4JTestBase<ConfigCLI>{
         try {\r
             parser.parseArgument(args);\r
         } catch (CmdLineException e) {\r
-            // TODO Auto-generated catch block\r
-            e.printStackTrace();\r
-            throw new Exception();\r
+            fail(e.getMessage());\r
         }\r
         testObject.execute();\r
-        //TODO assertion needed\r
+    }\r
+    \r
+    @Test\r
+    public void test_no_option() throws Exception{\r
+        String [] args = {""};\r
+        \r
+        try {\r
+            parser.parseArgument(args);\r
+        } catch (CmdLineException e) {\r
+            e.printStackTrace();\r
+            assertNotNull(e);\r
+        }\r
     }\r
 \r
 \r