From 0c330e334c17f9d3832f03554e6c0cc2aec56f66 Mon Sep 17 00:00:00 2001 From: Heongseok Heo Date: Fri, 1 Nov 2013 16:47:12 +0900 Subject: [PATCH] CLI: Fix bug with no argument 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 --- org.tizen.ncli.ide/.classpath | 2 +- .../src/org/tizen/core/ide/BuildWebAppModule.java | 63 +++++++++++----------- .../src/org/tizen/ncli/ide/shell/AbstractCLI.java | 11 ---- .../src/org/tizen/ncli/ide/shell/Main.java | 29 +++++----- .../org/tizen/ncli/ide/shell/BuildWebCLITest.java | 45 ++++++++++++++++ .../org/tizen/ncli/ide/shell/ConfigCLITest.java | 17 ++++-- 6 files changed, 106 insertions(+), 61 deletions(-) create mode 100644 org.tizen.ncli.ide/test/src/org/tizen/ncli/ide/shell/BuildWebCLITest.java diff --git a/org.tizen.ncli.ide/.classpath b/org.tizen.ncli.ide/.classpath index 0643ce9..4520449 100644 --- a/org.tizen.ncli.ide/.classpath +++ b/org.tizen.ncli.ide/.classpath @@ -5,6 +5,6 @@ - + diff --git a/org.tizen.ncli.ide/src/org/tizen/core/ide/BuildWebAppModule.java b/org.tizen.ncli.ide/src/org/tizen/core/ide/BuildWebAppModule.java index e51f7d6..46d338e 100644 --- a/org.tizen.ncli.ide/src/org/tizen/core/ide/BuildWebAppModule.java +++ b/org.tizen.ncli.ide/src/org/tizen/core/ide/BuildWebAppModule.java @@ -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 filterFactory = new WildCardFilterFactory(); public List excludes = new ArrayList(); - 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(); - - if( isPrerequisiteSatisfy()) { + + if (isPrerequisiteSatisfy()) { // Set output - if( null != 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 resources = new ArrayList(); // 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 excludes) { -// filter.clearExcludes(); + // filter.clearExcludes(); if (null != excludes) { for (final String exclude : excludes) { filter.addExcludes(filterFactory.create(exclude)); diff --git a/org.tizen.ncli.ide/src/org/tizen/ncli/ide/shell/AbstractCLI.java b/org.tizen.ncli.ide/src/org/tizen/ncli/ide/shell/AbstractCLI.java index 749c8a1..1f793b1 100644 --- a/org.tizen.ncli.ide/src/org/tizen/ncli/ide/shell/AbstractCLI.java +++ b/org.tizen.ncli.ide/src/org/tizen/ncli/ide/shell/AbstractCLI.java @@ -47,16 +47,5 @@ public abstract class AbstractCLI { protected PrintWriter output = new PrintWriter(System.out);; - public final void execute(String[] args) { - parseArgument(args); - execute(); - } - - /** - * @param args - */ - private void parseArgument(String[] args) { - log.trace("Could not use parsing argument again!"); - } public abstract void execute(); } diff --git a/org.tizen.ncli.ide/src/org/tizen/ncli/ide/shell/Main.java b/org.tizen.ncli.ide/src/org/tizen/ncli/ide/shell/Main.java index 6447db4..3877b78 100644 --- a/org.tizen.ncli.ide/src/org/tizen/ncli/ide/shell/Main.java +++ b/org.tizen.ncli.ide/src/org/tizen/ncli/ide/shell/Main.java @@ -3,11 +3,13 @@ */ package org.tizen.ncli.ide.shell; +import java.io.PrintWriter; import java.util.List; import org.kohsuke.args4j.Argument; import org.kohsuke.args4j.CmdLineException; import org.kohsuke.args4j.CmdLineParser; +import org.kohsuke.args4j.OptionHandlerFilter; import org.kohsuke.args4j.spi.OptionHandler; import org.kohsuke.args4j.spi.SubCommand; import org.kohsuke.args4j.spi.SubCommandHandler; @@ -15,10 +17,9 @@ import org.kohsuke.args4j.spi.SubCommands; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - /** - * This class is entry point of Tizen New Command Line Interface. All the command - * line argument would be parsed and then proper Command class is called. + * This class is entry point of Tizen New Command Line Interface. All the command line argument would be parsed and then + * proper Command class is called. * * @author Harry Hyeongseok Heo{@literal } (S-core) * @@ -27,12 +28,13 @@ public class Main { private Logger log = LoggerFactory.getLogger(Main.class); - @Argument(index = 0, required = true, handler = SubCommandHandler.class, usage = "Command is needed!") + @Argument(index = 0, required = true, handler = SubCommandHandler.class, + usage = "Sub command is needed!" , + metaVar="[create | build-native | build-web | cli-config | sign]") @SubCommands({ @SubCommand(name = "create", impl = CreateCLI.class), @SubCommand(name = "build-native", impl = BuildNativeCLI.class), @SubCommand(name = "build-web", impl = BuildWebCLI.class), - @SubCommand(name = "sign", impl = SignCLI.class), - @SubCommand(name = "cli-config", impl = ConfigCLI.class) }) + @SubCommand(name = "sign", impl = SignCLI.class), @SubCommand(name = "cli-config", impl = ConfigCLI.class) }) private AbstractCLI tizenCLI; /** @@ -46,22 +48,23 @@ public class Main { private void run(String[] args) { CmdLineParser cmdParser = new CmdLineParser(this); - log.trace("Start running Tizen CLI Main class..."); try { - //Argument parsing - make metadata from annotation. + // Argument parsing - make metadata from annotation. cmdParser.parseArgument(args); - if( null != this.tizenCLI) { + if (null != this.tizenCLI) { this.tizenCLI.execute(); } - + } catch (CmdLineException e) { // TODO Handle CLI exception + cmdParser.printUsage(System.out); e.printStackTrace(); } catch (Exception e) { - // TODO: handle exception - e.printStackTrace(); - } + // TODO: handle exception + cmdParser.printUsage(System.out); + e.printStackTrace(); + } } 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 index 0000000..32e9b96 --- /dev/null +++ b/org.tizen.ncli.ide/test/src/org/tizen/ncli/ide/shell/BuildWebCLITest.java @@ -0,0 +1,45 @@ +/* + * IDE + * + * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: + * Hyeongseok Heo + * Kangho Kim + * + * 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 } (S-core) + * + * + */ +public class BuildWebCLITest extends Args4JTestBase { + + + @Override + public BuildWebCLI getTestObject() { + // TODO Auto-generated method stub + return new BuildWebCLI(); + } + +} diff --git a/org.tizen.ncli.ide/test/src/org/tizen/ncli/ide/shell/ConfigCLITest.java b/org.tizen.ncli.ide/test/src/org/tizen/ncli/ide/shell/ConfigCLITest.java index d0fb964..5f81ddd 100644 --- a/org.tizen.ncli.ide/test/src/org/tizen/ncli/ide/shell/ConfigCLITest.java +++ b/org.tizen.ncli.ide/test/src/org/tizen/ncli/ide/shell/ConfigCLITest.java @@ -57,12 +57,21 @@ public class ConfigCLITest extends Args4JTestBase{ try { parser.parseArgument(args); } catch (CmdLineException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - throw new Exception(); + fail(e.getMessage()); } testObject.execute(); - //TODO assertion needed + } + + @Test + public void test_no_option() throws Exception{ + String [] args = {""}; + + try { + parser.parseArgument(args); + } catch (CmdLineException e) { + e.printStackTrace(); + assertNotNull(e); + } } -- 2.7.4