From: Bon-Yong Lee Date: Wed, 5 Sep 2012 06:03:10 +0000 (+0900) Subject: [Title] Add help detail for web-signing X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ff9555b20fb7acea7fc0b42ea4e07f2eeb10a3be;p=sdk%2Ftools%2Fcli.git [Title] Add help detail for web-signing [Type] Enhancement [Module] Sub [Priority] Minor [CQ#] // CQ Issue Number [Redmine#] // Redmine Isuue Number [Problem] // Problem Description [Cause] // Cause Description [Solution] // Solution Description [TestCase] // Executed the test-target (How to) --- diff --git a/org.tizen.cli/.settings/org.eclipse.core.resources.prefs b/org.tizen.cli/.settings/org.eclipse.core.resources.prefs new file mode 100755 index 0000000..898ab72 --- /dev/null +++ b/org.tizen.cli/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=utf8 diff --git a/org.tizen.cli/doc/install/doc/schema/profiles.xsd b/org.tizen.cli/doc/install/doc/schema/profiles.xsd new file mode 100755 index 0000000..220980c --- /dev/null +++ b/org.tizen.cli/doc/install/doc/schema/profiles.xsd @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/org.tizen.cli/src/org/tizen/cli/exec/AbstractLauncher.java b/org.tizen.cli/src/org/tizen/cli/exec/AbstractLauncher.java old mode 100644 new mode 100755 index 9096f5a..d8fa49f --- a/org.tizen.cli/src/org/tizen/cli/exec/AbstractLauncher.java +++ b/org.tizen.cli/src/org/tizen/cli/exec/AbstractLauncher.java @@ -24,19 +24,18 @@ */ package org.tizen.cli.exec; -import static org.tizen.common.util.StringUtil.nvl; - import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintStream; -import java.io.PrintWriter; -import java.io.StringWriter; +import java.net.URL; +import java.nio.charset.Charset; +import java.util.Enumeration; +import java.util.Properties; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.GnuParser; -import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.OptionBuilder; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; @@ -50,7 +49,9 @@ import org.tizen.common.core.command.policy.PolicyRegistry; import org.tizen.common.file.FileHandler; import org.tizen.common.file.FileHandler.Attribute; import org.tizen.common.file.StandardFileHandler; +import org.tizen.common.util.IOUtil; import org.tizen.common.util.MapUtil; +import org.tizen.common.util.PropertyUtil; /** *

@@ -66,10 +67,6 @@ import org.tizen.common.util.MapUtil; abstract public class AbstractLauncher { - /** - * Program name property key - */ - protected static final String PROP_PRG = "cli.name"; /** *

@@ -134,6 +131,14 @@ AbstractLauncher */ protected static final String PROP_PARSER = "tizen.cli.parser"; + + /** + *

+ * suffix for help properties + *

+ */ + protected static final String SUFFIX_CONFIG = ".properties"; + /** *

* logger for this object @@ -143,6 +148,13 @@ AbstractLauncher /** *

+ * object for usage help + *

+ */ + protected Help help; + + /** + *

* optional flag to be specified by user * * flag is formatted in POSIX style in default @@ -182,7 +194,7 @@ AbstractLauncher */ protected String getSyntax() { - return nvl( System.getProperty( PROP_PRG ), "java " + getClass().getName() ); + return getHelp().getSyntax(); } /** @@ -205,6 +217,58 @@ AbstractLauncher } /** + *

+ * Return {@link Help} in this cli instance + *

+ * @return {@link Help} + */ + synchronized protected + Help + getHelp() + { + if ( null == help ) + { + this.help = new Help( getOptions() ); + final ClassLoader cl = getClass().getClassLoader(); + final String resourceName = getClass().getName().replace( '.', '/' ) + SUFFIX_CONFIG; + try + { + final Enumeration iter = cl.getResources( resourceName ); + while ( iter.hasMoreElements() ) + { + final URL url = iter.nextElement(); + final Properties properties = PropertyUtil.loadProperties( url.openStream() ); + + for ( final Object key : properties.keySet() ) + { + final String value = properties.getProperty( (String) key ); + byte[] contents = IOUtil.getBytes( cl.getResourceAsStream( value ), true ); + this.help.addHelpDetail( (String) key, new String( contents, Charset.forName( "utf8" ) ) ); + } + } + } + catch ( final IOException e ) + { + logger.warn( "Can't load help", e ); + } + } + + return this.help; + } + + /** + * Print out sample usage + * + * @return sample usage string + */ + protected + String + printHelpDetail( String option ) + { + return getHelp().getHelpDetail( option ); + } + + /** * Print out usage * * @return usage string @@ -213,29 +277,9 @@ AbstractLauncher String printHelp() { - final HelpFormatter formatter = new HelpFormatter(); - - final StringWriter writer = new StringWriter(); - - final PrintWriter stream = new PrintWriter( writer ); - - formatter.printHelp( - stream, - formatter.getWidth(), - getSyntax(), - null, - getOptions(), - formatter.getLeftPadding(), - formatter.getDescPadding(), - null, - false - ); - - final String help = writer.toString(); - - getPrompter().error( help ); - - return help; + String helpStr = getHelp().getHelp(); + getPrompter().error( helpStr ); + return helpStr; } @@ -279,9 +323,14 @@ AbstractLauncher if ( cmd.hasOption( OPT_HELP ) ) { printHelp(); + + for ( String keyword : cmd.getArgs() ) + { + printHelpDetail( keyword ); + } return ; } - + execute( cmd ); } catch ( final ParseException e ) diff --git a/org.tizen.cli/src/org/tizen/cli/exec/Help.java b/org.tizen.cli/src/org/tizen/cli/exec/Help.java new file mode 100755 index 0000000..16d5712 --- /dev/null +++ b/org.tizen.cli/src/org/tizen/cli/exec/Help.java @@ -0,0 +1,92 @@ +package org.tizen.cli.exec; + +import static org.tizen.common.util.StringUtil.EMPTY_STRING; +import static org.tizen.common.util.StringUtil.nvl; + +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.HashMap; + +import org.apache.commons.cli.HelpFormatter; +import org.apache.commons.cli.Options; + +public class Help +{ + /** + * Program name property key + */ + protected static final String PROP_PRG = "cli.name"; + + protected final Options opts; + + protected HashMap keyword2help = new HashMap(); + + public Help( Options opts ) + { + this.opts = opts; + } + + /** + * {@link Options} for usage + * + * @return defined {@link Options} + */ + protected + Options + getOptions() + { + return opts; + } + + public void addHelpDetail( String keyword, String help ) + { + keyword2help.put( keyword.toLowerCase(), help ); + } + + /** + * return execution command for usage + * + * @return execution command + */ + protected String getSyntax() + { + return nvl( System.getProperty( PROP_PRG ), "java " + getClass().getName() ); + } + + + public String getHelp() + { + final HelpFormatter formatter = new HelpFormatter(); + + final StringWriter writer = new StringWriter(); + + final PrintWriter stream = new PrintWriter( writer ); + + formatter.printHelp( + stream, + formatter.getWidth(), + getSyntax(), + null, + getOptions(), + formatter.getLeftPadding(), + formatter.getDescPadding(), + null, + false + ); + + final String help = writer.toString(); + + return help; + } + + public String + getHelpDetail( final String keyword ) + { + if ( null == keyword ) + { + return EMPTY_STRING; + } + return nvl( keyword2help.get( keyword.toLowerCase() ) ); + } + +} diff --git a/org.tizen.cli/src/org/tizen/cli/exec/sign/Main.java b/org.tizen.cli/src/org/tizen/cli/exec/sign/Main.java index d5119db..1b38000 100755 --- a/org.tizen.cli/src/org/tizen/cli/exec/sign/Main.java +++ b/org.tizen.cli/src/org/tizen/cli/exec/sign/Main.java @@ -25,6 +25,7 @@ package org.tizen.cli.exec.sign; import static org.tizen.common.util.FilenameUtil.addTailingPath; +import static org.tizen.common.util.StringUtil.nvl; import java.io.IOException; import java.text.MessageFormat; @@ -41,6 +42,7 @@ import org.tizen.common.core.command.prompter.Option; import org.tizen.common.file.FileHandler; import org.tizen.common.file.FileHandler.Attribute; import org.tizen.common.util.CollectionUtil; +import org.tizen.common.util.FilenameUtil; import org.tizen.web.sign.command.ReadSigningProfileFileCommand; import org.tizen.web.sign.command.SignCommand; @@ -88,6 +90,28 @@ implements ILaunchOptions */ protected static final int SEPARATOR_PROFILE = ':'; + + /** + *

+ * JVM property key for default profile path + *

+ */ + protected static final String PROP_CLI_HOME = "cli.home"; + + /** + *

+ * OS environment variable key for default profile path + *

+ */ + protected static final String ENV_CLI_HOME = "CLI_HOME"; + + /** + *

+ * default profile file's name + *

+ */ + protected static final String DEFAULT_PROFILE_NAME = "profiles.xml"; + /** * Entry point for cli main * @@ -189,7 +213,7 @@ implements ILaunchOptions final int index = profileName.indexOf( SEPARATOR_PROFILE ); if ( index < 0 ) { - profilesFilePath = System.getProperty( "user.home" ) + "/.tizen/profiles.xml"; + profilesFilePath = getDefaultProfilesFilePath(); } else { @@ -199,6 +223,11 @@ implements ILaunchOptions logger.debug( "Profile name :{}", profileName ); logger.debug( "Profiles file path :{}", profilesFilePath ); + if ( null == profilesFilePath ) + { + getExecutionContext().getPrompter().error( "Profiles file not found. Try to specify profiles path using -p option or environment variable cli.home" ); + return ; + } final ReadSigningProfileFileCommand readProfile = new ReadSigningProfileFileCommand( profilesFilePath, profileName ); getExecutor().execute( readProfile ); @@ -217,6 +246,30 @@ implements ILaunchOptions } /** + * Return default profiles.xml's path + * + * default profile path is specified by jvm propertyes naming "cli.home" + * + * or os environment variable "CLI_HOME" + * + * @return default profiles path + */ + protected + String + getDefaultProfilesFilePath() + { + final String cliHome = + nvl( System.getProperty( PROP_CLI_HOME ), System.getenv( ENV_CLI_HOME ) ); + + if ( null == cliHome ) + { + return null; + } + + return FilenameUtil.addTailingPath( cliHome, "conf/" + DEFAULT_PROFILE_NAME ); + } + + /** * Check if path is Tizen web project root * * @param path directory path to check diff --git a/org.tizen.cli/src/org/tizen/cli/exec/sign/Main.properties b/org.tizen.cli/src/org/tizen/cli/exec/sign/Main.properties new file mode 100755 index 0000000..17f6922 --- /dev/null +++ b/org.tizen.cli/src/org/tizen/cli/exec/sign/Main.properties @@ -0,0 +1 @@ +profile=org/tizen/cli/exec/sign/profile.help \ No newline at end of file diff --git a/org.tizen.cli/src/org/tizen/cli/exec/sign/profile.help b/org.tizen.cli/src/org/tizen/cli/exec/sign/profile.help new file mode 100755 index 0000000..460a0d3 --- /dev/null +++ b/org.tizen.cli/src/org/tizen/cli/exec/sign/profile.help @@ -0,0 +1,14 @@ +Specify profile( profile name and profiles file path ) + +* profile name + Profiles file can contain multiple profiles. You must specify profile to use in signing. + +* profiles file path + Path of profiles file containing profiles. Default value is "$CLI_HOME/conf/profiles.xml". You can change it using java virtual machine properties( -D ). + + Ex.) + web-signing -p develop:/home/tizen/profiles.xml + +* profiles file format + It is xml-formatted file. Its schema( xml schema ) exists in $CLI_HOME/doc/schema/profiles.xsd + You don't have to specify schema in profiles file \ No newline at end of file diff --git a/org.tizen.cli/test/src/org/tizen/cli/exec/AbstractLauncherTest.java b/org.tizen.cli/test/src/org/tizen/cli/exec/AbstractLauncherTest.java old mode 100644 new mode 100755 index 0cbeb1e..373a85f --- a/org.tizen.cli/test/src/org/tizen/cli/exec/AbstractLauncherTest.java +++ b/org.tizen.cli/test/src/org/tizen/cli/exec/AbstractLauncherTest.java @@ -24,6 +24,7 @@ */ package org.tizen.cli.exec; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; @@ -37,6 +38,7 @@ import org.apache.commons.cli.PosixParser; import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.tizen.cli.exec.sign.Main; import org.tizen.common.core.command.Executor; import org.tizen.common.core.command.prompter.NopPrompter; @@ -70,6 +72,7 @@ AbstractLauncherTest */ public LauncherSpi() { executor = new CommandLineExecutor( new NopPrompter() ); + getHelp().addHelpDetail( "hello", "World" ); } /* (non-Javadoc) @@ -193,6 +196,15 @@ AbstractLauncherTest assertNotNull( launcher.printHelp() ); } + @Test + public + void + test_printHelpDetail() + throws Exception + { + assertEquals( "World", launcher.printHelpDetail( "Hello" ) ); + } + /** * Test {@link AbstractLauncher#run(String...)} * @@ -278,4 +290,26 @@ AbstractLauncherTest assertNotNull( executor ); } + /** + * Test {@link AbstractLauncher#getHelp()} + * + * @throws Exception in case of failure in test + * + * @see AbstractLauncher#getHelp() + */ + @Test + public + void + test_getHelp() + throws Exception + { + final Main testTarget = new Main(); + + final Help help = testTarget.getHelp(); + assertNotNull( help ); + + assertTrue( help.getHelpDetail( "profile" ).contains( "profiles.xsd" ) ); + + } + } diff --git a/org.tizen.cli/test/src/org/tizen/cli/exec/HelpTest.java b/org.tizen.cli/test/src/org/tizen/cli/exec/HelpTest.java new file mode 100755 index 0000000..d557276 --- /dev/null +++ b/org.tizen.cli/test/src/org/tizen/cli/exec/HelpTest.java @@ -0,0 +1,82 @@ +/* +* Command Line Interface +* +* Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. +* +* Contact: +* BonYong Lee +* +* 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.cli.exec; + +import static org.junit.Assert.*; + +import org.apache.commons.cli.OptionBuilder; +import org.apache.commons.cli.Options; +import org.junit.Test; + +/** + * HelpTest + * + * Test case for {@link Help} + * + * @author BonYong Lee{@literal } (S-Core) + * + * @see Help + */ +public class HelpTest +{ + + /** + * Test {@link Help#getHelp()} + * + * @throws Exception in case of failure in test + * + * @see Help#getHelp() + */ + @SuppressWarnings("static-access") + @Test + public void test_getHelp() + { + + final Help h = new Help( new Options().addOption( OptionBuilder.withLongOpt( "help" ).create( "h" ) ) ); + + assertNotNull( h.getHelp() ); + assertTrue( h.getHelp().contains( "-h,--help" ) ); + } + + /** + * Test {@link Help#getHelpDetail(String)} + * + * @throws Exception in case of failure in test + * + * @see Help#getHelpDetail(String) + */ + @SuppressWarnings("static-access") + public void test_getHelpDetail() throws Exception + { + final Help h = new Help( new Options().addOption( OptionBuilder.withLongOpt( "help" ).create( "h" ) ) ); + h.addHelpDetail( "command", "command is not supported" ); + + + assertEquals( "command is not supported", h.getHelpDetail( "command" ) ); + + + } + +} diff --git a/org.tizen.cli/test/src/org/tizen/cli/exec/TargetDeviceSelectorTest.java b/org.tizen.cli/test/src/org/tizen/cli/exec/TargetDeviceSelectorTest.java old mode 100644 new mode 100755 index 6819b59..bc26a9a --- a/org.tizen.cli/test/src/org/tizen/cli/exec/TargetDeviceSelectorTest.java +++ b/org.tizen.cli/test/src/org/tizen/cli/exec/TargetDeviceSelectorTest.java @@ -25,19 +25,23 @@ */ package org.tizen.cli.exec; -import static org.junit.Assert.*; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import org.junit.Before; import org.junit.Test; +import org.mockito.Mockito; 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; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - /** * TargetDeviceSelectorTest * @@ -227,11 +231,10 @@ TargetDeviceSelectorTest ) ).thenReturn( no ); ChoiceOption cancel = new ChoiceOption( "Cancel", true ); - GenericOption num = new GenericOption( "Num" ); - when( mockPropter.interact( "select device or cancel process ", - cancel, - num + when( mockPropter.interact( Mockito.eq( "select device or cancel process " ), + Mockito.any(), + Mockito.any() ) ).thenReturn( cancel ); assertEquals( null, testClass.selectDevice() ); diff --git a/org.tizen.cli/test/src/org/tizen/cli/exec/sign/MainTest.java b/org.tizen.cli/test/src/org/tizen/cli/exec/sign/MainTest.java index 400e8f0..aa8b49c 100755 --- a/org.tizen.cli/test/src/org/tizen/cli/exec/sign/MainTest.java +++ b/org.tizen.cli/test/src/org/tizen/cli/exec/sign/MainTest.java @@ -148,4 +148,5 @@ extends AbstractMainTest } + }