[Lutinutil-commits] r1261 - in commandline/trunk: . maven-commandline-plugin maven-commandline-plugin/src/main/java/org/codelutin/option/generate maven-commandline-plugin/src/main/java/org/codelutin/option/generate/util maven-commandline-plugin/src/test/java/org/codelutin/option/def maven-commandline-plugin/src/test/java/org/codelutin/option/generate
Author: chemit Date: 2008-11-30 16:25:11 +0000 (Sun, 30 Nov 2008) New Revision: 1261 Added: commandline/trunk/maven-commandline-plugin/ commandline/trunk/maven-commandline-plugin/pom.xml commandline/trunk/maven-commandline-plugin/src/main/java/org/codelutin/option/generate/JavaGeneratorGoal.java commandline/trunk/maven-commandline-plugin/src/main/java/org/codelutin/option/generate/util/AbstractGeneratorGoal.java commandline/trunk/maven-commandline-plugin/src/test/java/org/codelutin/option/def/DefinitionParserBadOptionTest.java commandline/trunk/maven-commandline-plugin/src/test/java/org/codelutin/option/def/DefinitionParserTest.java commandline/trunk/maven-commandline-plugin/src/test/java/org/codelutin/option/def/ParserTest.java commandline/trunk/maven-commandline-plugin/src/test/java/org/codelutin/option/def/ParserUtilForTest.java commandline/trunk/maven-commandline-plugin/src/test/java/org/codelutin/option/def/PropertiesLoaderTest.java commandline/trunk/maven-commandline-plugin/src/test/java/org/codelutin/option/generate/GenerateJavaTest.java Removed: commandline/trunk/maven-commandline-plugin/THIRD-PARTY.txt commandline/trunk/maven-commandline-plugin/pom.xml commandline/trunk/maven-commandline-plugin/src/main/java/org/codelutin/option/generate/JavaGeneratorGoal.java commandline/trunk/maven-commandline-plugin/src/main/java/org/codelutin/option/generate/util/AbstractGeneratorGoal.java commandline/trunk/maven-commandline-plugin/src/test/java/org/codelutin/option/def/DefinitionParserBadOptionTest.java commandline/trunk/maven-commandline-plugin/src/test/java/org/codelutin/option/def/DefinitionParserTest.java commandline/trunk/maven-commandline-plugin/src/test/java/org/codelutin/option/def/ParserTest.java commandline/trunk/maven-commandline-plugin/src/test/java/org/codelutin/option/def/ParserUtilForTest.java commandline/trunk/maven-commandline-plugin/src/test/java/org/codelutin/option/def/PropertiesLoaderTest.java commandline/trunk/maven-commandline-plugin/src/test/java/org/codelutin/option/generate/GenerateJavaTest.java commandline/trunk/maven-plugin/ Modified: commandline/trunk/pom.xml Log: move plugin to mavben-commandline-plugin Copied: commandline/trunk/maven-commandline-plugin (from rev 1129, commandline/trunk/maven-plugin) Deleted: commandline/trunk/maven-commandline-plugin/THIRD-PARTY.txt =================================================================== --- commandline/trunk/maven-plugin/THIRD-PARTY.txt 2008-09-24 19:26:58 UTC (rev 1129) +++ commandline/trunk/maven-commandline-plugin/THIRD-PARTY.txt 2008-11-30 16:25:11 UTC (rev 1261) @@ -1,32 +0,0 @@ -List of third-party dependencies grouped by their license type. - -Common Public License Version 1.0 : - * JUnit (junit:junit:3.8.1 - http://junit.org) - -LGPL : - * Lutin utilities library (org.codelutin:lutinutil:0.30-SNAPSHOT - http://labs.libre-entreprise.org/projects/lutinutil) - * core (org.codelutin.commandline:commandline-core:0.9 - http://labs.libre-entreprise.org/projects/lutinutil/commandline-core) - -The Apache Software License, Version 2.0 : - * Commons Collections (commons-collections:commons-collections:3.2.1 - http://commons.apache.org/collections/) - * Commons Logging (commons-logging:commons-logging:1.1.1 - http://commons.apache.org/logging) - * Lang (commons-lang:commons-lang:2.1 - http://jakarta.apache.org/commons/${pom.artifactId.substring(8)}/) - * Log4j (log4j:log4j:1.2.14 - http://logging.apache.org/log4j/docs/) - * Maven Artifact (org.apache.maven:maven-artifact:2.0.4 - http://maven.apache.org/maven-artifact) - * Maven Artifact Manager (org.apache.maven:maven-artifact-manager:2.0.4 - http://maven.apache.org/maven-artifact-manager) - * Maven Local Settings Model (org.apache.maven:maven-settings:2.0.4 - http://maven.apache.org/maven-settings) - * Maven Model (org.apache.maven:maven-model:2.0.4 - http://maven.apache.org/maven-model) - * Maven Plugin API (org.apache.maven:maven-plugin-api:2.0.4 - http://maven.apache.org/maven-plugin-api) - * Maven Profile Model (org.apache.maven:maven-profile:2.0.4 - http://maven.apache.org/maven-profile) - * Maven Project Builder (org.apache.maven:maven-project:2.0.4 - http://maven.apache.org/maven-project) - * Maven Repository Metadata Model (org.apache.maven:maven-repository-metadata:2.0.4 - http://maven.apache.org/maven-repository-metadata) - * Maven Wagon API (org.apache.maven.wagon:wagon-provider-api:1.0-alpha-6 - no url defined) - -Unknown license : - * Default Plexus Container (org.codehaus.plexus:plexus-container-default:1.0-alpha-9 - no url defined) - * Logging (commons-logging:commons-logging:1.0.3 - http://jakarta.apache.org/commons/logging/) - * Plexus Common Utilities (org.codehaus.plexus:plexus-utils:1.0.4 - no url defined) - * Plexus Common Utilities (org.codehaus.plexus:plexus-utils:1.1 - no url defined) - * Unnamed - commons-beanutils:commons-beanutils:jar:1.7.0 (commons-beanutils:commons-beanutils:1.7.0 - no url defined) - * Unnamed - commons-primitives:commons-primitives:jar:1.0 (commons-primitives:commons-primitives:1.0 - no url defined) - * classworlds (classworlds:classworlds:1.1-alpha-2 - http://classworlds.codehaus.org/) \ No newline at end of file Deleted: commandline/trunk/maven-commandline-plugin/pom.xml =================================================================== --- commandline/trunk/maven-plugin/pom.xml 2008-09-24 19:26:58 UTC (rev 1129) +++ commandline/trunk/maven-commandline-plugin/pom.xml 2008-11-30 16:25:11 UTC (rev 1261) @@ -1,104 +0,0 @@ -<?xml version="1.0" encoding="ISO-8859-1"?> -<project xmlns="http://maven.apache.org/POM/4.0.0" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> - - <modelVersion>4.0.0</modelVersion> - - <!-- ************************************************************* --> - <!-- *** POM Relationships *************************************** --> - <!-- ************************************************************* --> - - <parent> - <groupId>org.codelutin.commandline</groupId> - <artifactId>commandline-pom</artifactId> - <version>0.9</version> - </parent> - - <!--groupId>org.apache.maven.plugins</groupId--> - <artifactId>maven-commandline-plugin</artifactId> - <name>maven-plugin</name> - - <dependencies> - <dependency> - <groupId>org.codelutin.commandline</groupId> - <artifactId>commandline-core</artifactId> - <scope>compile</scope> - </dependency> - - <dependency> - <groupId>commons-lang</groupId> - <artifactId>commons-lang</artifactId> - <scope>compile</scope> - </dependency> - - <!-- maven plugin project dependencies --> - <dependency> - <groupId>org.apache.maven</groupId> - <artifactId>maven-plugin-api</artifactId> - <scope>compile</scope> - </dependency> - - <dependency> - <groupId>org.apache.maven</groupId> - <artifactId>maven-project</artifactId> - <scope>compile</scope> - </dependency> - - </dependencies> - - <!-- ************************************************************* --> - <!-- *** Project Information ************************************* --> - <!-- ************************************************************* --> - - <version>0.9</version> - - <description> - Plugin maven 2 pour la generation du parser d'options, des - configurations et du context d'application en se basant sur - la librairie commandline. - </description> - - <inceptionYear>2008</inceptionYear> - - <!-- ************************************************************* --> - <!-- *** Build Settings ****************************************** --> - <!-- ************************************************************* --> - - <packaging>maven-plugin</packaging> - <build> - <plugins> - <!-- i18n --> - <plugin> - <groupId>org.codelutin.plugin</groupId> - <artifactId>maven-i18n-plugin</artifactId> - <executions> - <execution> - <goals> - <goal>parserJava</goal> - <goal>gen</goal> - </goals> - </execution> - </executions> - </plugin> - - <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-plugin-plugin</artifactId> - </plugin> - - </plugins> - </build> - - <!-- ************************************************************* --> - <!-- *** Build Environment ************************************** --> - <!-- ************************************************************* --> - - <!-- the plugin is a module in a labs project (buix), so we have to override - this property (see in the parent pom for more explanation) - --> - <scm> - <url>${maven.scm.url}</url> - </scm> - -</project> Copied: commandline/trunk/maven-commandline-plugin/pom.xml (from rev 1260, commandline/trunk/maven-plugin/pom.xml) =================================================================== --- commandline/trunk/maven-commandline-plugin/pom.xml (rev 0) +++ commandline/trunk/maven-commandline-plugin/pom.xml 2008-11-30 16:25:11 UTC (rev 1261) @@ -0,0 +1,98 @@ +<?xml version="1.0" encoding="ISO-8859-1"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + + <modelVersion>4.0.0</modelVersion> + + <!-- ************************************************************* --> + <!-- *** POM Relationships *************************************** --> + <!-- ************************************************************* --> + + <parent> + <groupId>org.codelutin</groupId> + <artifactId>commandline</artifactId> + <version>1.0-SNAPSHOT</version> + </parent> + + <groupId>org.codelutin.commandline</groupId> + <artifactId>maven-commandline-plugin</artifactId> + <name>maven-plugin</name> + + <dependencies> + <dependency> + <groupId>org.codelutin.commandline</groupId> + <artifactId>commandline-core</artifactId> + <scope>compile</scope> + </dependency> + + <dependency> + <groupId>commons-lang</groupId> + <artifactId>commons-lang</artifactId> + <scope>compile</scope> + </dependency> + + <!-- maven plugin project dependencies --> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-plugin-api</artifactId> + <scope>compile</scope> + </dependency> + + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-project</artifactId> + <scope>compile</scope> + </dependency> + + </dependencies> + + <!-- ************************************************************* --> + <!-- *** Project Information ************************************* --> + <!-- ************************************************************* --> + + <description> + Plugin maven 2 pour la generation du parser d'options, des + configurations et du context d'application en se basant sur + la librairie commandline. + </description> + + <!-- ************************************************************* --> + <!-- *** Build Settings ****************************************** --> + <!-- ************************************************************* --> + + <packaging>maven-plugin</packaging> + <build> + <plugins> + <!-- i18n --> + <plugin> + <groupId>org.codelutin</groupId> + <artifactId>maven-i18n-plugin</artifactId> + <executions> + <execution> + <goals> + <goal>parserJava</goal> + <goal>gen</goal> + </goals> + </execution> + </executions> + </plugin> + + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-plugin-plugin</artifactId> + </plugin> + + </plugins> + </build> + + <!-- ************************************************************* --> + <!-- *** Build Environment ************************************** --> + <!-- ************************************************************* --> + <scm> + <connection>${maven.scm.connection.son}</connection> + <developerConnection>${maven.scm.developerConnection.son}</developerConnection> + <url>${maven.scm.url.son}</url> + </scm> + +</project> Deleted: commandline/trunk/maven-commandline-plugin/src/main/java/org/codelutin/option/generate/JavaGeneratorGoal.java =================================================================== --- commandline/trunk/maven-plugin/src/main/java/org/codelutin/option/generate/JavaGeneratorGoal.java 2008-09-24 19:26:58 UTC (rev 1129) +++ commandline/trunk/maven-commandline-plugin/src/main/java/org/codelutin/option/generate/JavaGeneratorGoal.java 2008-11-30 16:25:11 UTC (rev 1261) @@ -1,422 +0,0 @@ -/* -* *##% maven-plugin - * Copyright (C) 2008 CodeLutin - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%* */ -package org.codelutin.option.generate; - -import org.apache.commons.lang.StringUtils; -import org.codelutin.option.Config; -import org.codelutin.option.OptionAction; -import org.codelutin.option.def.DefinitionParserContexts.ConfigContext; -import org.codelutin.option.def.DefinitionParserContexts.OptionContext; -import org.codelutin.option.generate.java.AbstractConfigJavaGenerator; -import org.codelutin.option.generate.java.AbstractContextJavaGenerator; -import org.codelutin.option.generate.java.AbstractOptionActionJavaGenerator; -import org.codelutin.option.generate.java.ConfigJavaGenerator; -import org.codelutin.option.generate.java.OptionActionJavaGenerator; -import org.codelutin.option.generate.java.OptionJavaGenerator; -import org.codelutin.option.generate.java.OptionKeyJavaGenerator; -import org.codelutin.option.generate.java.OptionParserJavaGenerator; -import org.codelutin.option.generate.util.AbstractGeneratorGoal; -import org.codelutin.util.FileUtil; - -import java.io.File; -import java.io.IOException; -import java.util.Map; -import java.util.TreeMap; - -/** - * Permet de generer la factory de definitions d'options de ligne de commande, - * pour un fichier de propriete contenant la definition formelle des options. - * - * @author chemit - * @goal genJava - * @phase process-sources - */ -public class JavaGeneratorGoal extends AbstractGeneratorGoal<JavaGeneratorContext> { - - /** - * chemin du repertoire de generation des resources. - * - * @parameter expression="${jaxx.outResource}" default-value="${basedir}/target/generated-sources/resources" - */ - protected File outResource; - - /** - * chemin du repertoire de compilation des resources. - * - * @parameter expression="${jaxx.outClass}" default-value="${basedir}/target/classes" - */ - protected File outClass; - - /** - * flag to copy generated resource files to outClass - * - * @parameter expression="${jaxx.copyToCP}" default-value="true" - */ - protected boolean copyToCP; - - /** - * fichier de proprietes contenant les definitions d'options a utiliser. - * - * @parameter expression="${commandline.source}" - * @required - */ - protected File source; - - /** - * Repertoire de sortie. - * - * @parameter expression="${commandline.out}" default-value="${maven.src.dir}/java" - * @required - */ - protected File out; - - /** - * le FQN de la classe concrete du Context - * - * @parameter expression="${commandline.concreteContextFQN}" - * @required - */ - protected String concreteContextFQN; - - /** - * le parser a utiliser - * - * @parameter expression="${commandline.parserFQN}" default-value="org.codelutin.option.def.DefinitionParserFromProperties" - */ - protected String parserFQN; - - - /** - * flag to show errors of parsing. - * - * @parameter expression="${commandLine.showError}" default-value="true" - */ - protected boolean showErrors; - - /** - * fqn de la super classe du parseur a generer - * - * @parameter expression="${commandline.parserSuperClass}" default-value="org.codelutin.option.OptionParser" - */ - protected String parserSuperClass; - - /** - * fqn de la super classe des options a generer - * - * @parameter expression="${commandline.optionSuperClass}" default-value="org.codelutin.option.Option" - */ - protected String optionSuperClass; - - /** - * fqn de la super classe des configs a generer - * - * @parameter expression="${commandline.configSuperClass}" default-value="org.codelutin.option.Config" - */ - protected String configSuperClass; - - /** - * fqn de la super classe des actions a generer - * - * @parameter expression="${commandline.actionSuperClass}" default-value="org.codelutin.option.OptionAction" - */ - protected String actionSuperClass; - - /** - * fqn de la super classe des actions a generer - * - * @parameter expression="${commandline.contextSuperClass}" default-value="org.codelutin.option.AbstractContext" - */ - protected String contextSuperClass; - - /** - * flag pour indique si les configs generes sont concrete ou abstraites - * - * @parameter expression="${commandline.concreteConfig}" default-value="false" - */ - protected boolean concreteConfig; - - /** - * le nom du fichier de configuration dans la configuration genere - * - * @parameter expression="${commandline._configFileName}" default-value=".${project.artifactId}" - * @required - */ - protected String _configFileName; - - /** - * le nom du projet - * - * @parameter expression="${commandline._projectName}" default-value="${project.artifactId}" - * @required - */ - protected String _projectName; - - /** - * l'url de l'application - * - * @parameter expression="${commandline._projectURL}" - * @required - */ - protected String _projectURL; - - /** - * le nom du projet - * - * @parameter expression="${commandline._organisationName}" - * @required - */ - protected String _organisationName; - - /** - * l'url de l'application - * - * @parameter expression="${commandline._organisationURL}" - * @required - */ - protected String _organisationURL; - - /** - * l'encoding par default dans la configuration genere - * - * @parameter expression="${commandline._encoding}" default-value="UTF-8" - * @required - */ - protected String _encoding; - /** - * la locale par default dans la configuration genere - * - * @parameter expression="${commandline._locale}" default-value="en_GB" - * @required - */ - protected String _locale; - /** - * la version dans la configuration genere - * - * @parameter expression="${commandline._version}" default-value="${project.version}" - * @required - */ - protected String _version; - /** - * la version dans la configuration genere - * - * @parameter expression="${commandline._copyright}" - * @required - */ - protected String _copyright; - - - public JavaGeneratorGoal() { - super(JavaGeneratorContext.class); - } - - public void generate() throws Exception { - - - Map<String, String> map = new TreeMap<String, String>(); - // build a dictonary of options implementations name - for (OptionContext context : getContext().getOptionContexts()) { - String suffix = StringUtils.capitalize(context.getKey()); - String optionClassName = getContext().getOptionSimpleName() + suffix; - map.put(context.getKey(), optionClassName); - } - - // generate specialized abstract OptionAction for this parser - if (needSpecializedAction()) { - - AbstractOptionActionJavaGenerator.doGenerate(getContext()); - - setActionSuperClass(getContext().getActionSimpleName()); - } - - boolean isConcreteConfig = getContext().isConcreteConfig(); - - getContext().setConfigSimpleName(getContext().getPrefix() + (isConcreteConfig ? "" : "Abstract") + "Config"); - - // generate specialized abstract SimpleConfigImpl for this parser - if (needSpecializedAbstractConfig()) { - AbstractConfigJavaGenerator.doGenerate(getContext(), getPrefix() + "AbstractConfig"); - - // the super classes of generatred configs is the one generated here - setConfigSuperClass(getContext().getPrefix() + "AbstractConfig"); - } - - // generate specialized OptionKey for this parser - OptionKeyJavaGenerator.doGenerate(getContext()); - - // generate OptionParser implementation - OptionParserJavaGenerator.doGenerate(getContext()); - - // generate AbstractContext implementation - AbstractContextJavaGenerator.doGenerate(getContext()); - - // generate Config implementations - for (ConfigContext context : getContext().getConfigContexts()) { - String suffix = StringUtils.capitalize(context.getCategory()); - ConfigJavaGenerator.doGenerate(getContext(), context, - getContext().getConfigSimpleName() + suffix, - isConcreteConfig - ); - } - - // generate Option implementations - for (OptionContext context : getContext().getOptionContexts()) { - OptionJavaGenerator.doGenerate(getContext(), context, map.get(context.getKey())); - } - - // generate OptionAction implementations - for (OptionContext context : getContext().getOptionContexts()) { - String suffix = StringUtils.capitalize(context.getKey()); - String optionClassName = map.get(context.getKey()); - OptionActionJavaGenerator.doGenerate(getContext(), context, - getContext().getActionSimpleName() + suffix, - getContext().getActionSuperClass() + "<" + optionClassName + ">", - optionClassName - ); - } - map.clear(); - - // generate ConcreteContext serivce declaration - generateConcreteContextDeclaration(); - } - - public File getOut() { - return out; - } - - private boolean needSpecializedAction() { - return getContext().getActionSuperClass().equals(OptionAction.class.getName()); - } - - private boolean needSpecializedAbstractConfig() { - return getContext().getConfigSuperClass().equals(Config.class.getName()); - } - - public String getActionSuperClass() { - return actionSuperClass; - } - - public boolean isConcreteConfig() { - return concreteConfig; - } - - public String getConfigSuperClass() { - return configSuperClass; - } - - public String getContextSuperClass() { - return contextSuperClass; - } - - public String getOptionSuperClass() { - return optionSuperClass; - } - - public String getParserSuperClass() { - return parserSuperClass; - } - - public boolean isShowErrors() { - return showErrors; - } - - public File getSource() { - return source; - } - - public String getParserFQN() { - return parserFQN; - } - - public String getConcreteContextFQN() { - return concreteContextFQN; - } - - public void setConfigSuperClass(String configSuperClass) { - this.configSuperClass = configSuperClass; - } - - public void setActionSuperClass(String actionSuperClass) { - this.actionSuperClass = actionSuperClass; - } - - public void setShowErrors(boolean showErrors) { - this.showErrors = showErrors; - } - - public void setConcreteConfig(boolean concreteConfig) { - this.concreteConfig = concreteConfig; - } - - public void setContextSuperClass(String contextSuperClass) { - this.contextSuperClass = contextSuperClass; - } - - public void setOptionSuperClass(String optionSuperClass) { - this.optionSuperClass = optionSuperClass; - } - - public void setParserSuperClass(String parserSuperClass) { - this.parserSuperClass = parserSuperClass; - } - - public void setSource(File source) { - this.source = source; - } - - public void setOut(File out) { - this.out = out; - } - - public void setParserFQN(String parserFQN) { - this.parserFQN = parserFQN; - } - - public void setConcreteContextFQN(String concreteContextFQN) { - this.concreteContextFQN = concreteContextFQN; - } - - protected void generateConcreteContextDeclaration() throws IOException { - File generatedProviderDeclaration = new File(outResource, "META-INF/services/" + org.codelutin.option.Context.class.getName()); - File parent = generatedProviderDeclaration.getParentFile(); - if (!parent.exists()) { - parent.mkdirs(); - } - - // just add the fqn inside the file :) - FileUtil.writeString(generatedProviderDeclaration, concreteContextFQN); - - if (copyToCP) { - // save it also in classes (since we are in process-resources phase and resources has already been copied) - copyResourceToCP(generatedProviderDeclaration); - } - } - - protected void copyResourceToCP(File file) throws IOException { - - String path = file.getAbsolutePath().substring(outResource.getAbsolutePath().length() + 1); - File compiledFile = new File(outClass, path); - if (verbose) { - getLog().info("copy to classapth generated file " + compiledFile); - } - File parent = compiledFile.getParentFile(); - if (!parent.exists()) { - parent.mkdirs(); - } - FileUtil.copy(file, compiledFile); - } -} Copied: commandline/trunk/maven-commandline-plugin/src/main/java/org/codelutin/option/generate/JavaGeneratorGoal.java (from rev 1260, commandline/trunk/maven-plugin/src/main/java/org/codelutin/option/generate/JavaGeneratorGoal.java) =================================================================== --- commandline/trunk/maven-commandline-plugin/src/main/java/org/codelutin/option/generate/JavaGeneratorGoal.java (rev 0) +++ commandline/trunk/maven-commandline-plugin/src/main/java/org/codelutin/option/generate/JavaGeneratorGoal.java 2008-11-30 16:25:11 UTC (rev 1261) @@ -0,0 +1,426 @@ +/* +* *##% maven-plugin + * Copyright (C) 2008 CodeLutin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%* */ +package org.codelutin.option.generate; + +import org.apache.commons.lang.StringUtils; +import org.codelutin.option.Config; +import org.codelutin.option.OptionAction; +import org.codelutin.option.def.DefinitionParserContexts.ConfigContext; +import org.codelutin.option.def.DefinitionParserContexts.OptionContext; +import org.codelutin.option.generate.java.AbstractConfigJavaGenerator; +import org.codelutin.option.generate.java.AbstractContextJavaGenerator; +import org.codelutin.option.generate.java.AbstractOptionActionJavaGenerator; +import org.codelutin.option.generate.java.ConfigJavaGenerator; +import org.codelutin.option.generate.java.OptionActionJavaGenerator; +import org.codelutin.option.generate.java.OptionJavaGenerator; +import org.codelutin.option.generate.java.OptionKeyJavaGenerator; +import org.codelutin.option.generate.java.OptionParserJavaGenerator; +import org.codelutin.option.generate.util.AbstractGeneratorGoal; +import org.codelutin.util.FileUtil; + +import java.io.File; +import java.io.IOException; +import java.util.Map; +import java.util.TreeMap; + +/** + * Permet de generer la factory de definitions d'options de ligne de commande, + * pour un fichier de propriete contenant la definition formelle des options. + * + * @author chemit + * @goal genJava + * @phase process-sources + */ +public class JavaGeneratorGoal extends AbstractGeneratorGoal<JavaGeneratorContext> { + + /** + * chemin du repertoire de generation des resources. + * + * @parameter expression="${jaxx.outResource}" default-value="${basedir}/target/generated-sources/resources" + */ + protected File outResource; + + /** + * chemin du repertoire de compilation des resources. + * + * @parameter expression="${jaxx.outClass}" default-value="${basedir}/target/classes" + */ + protected File outClass; + + /** + * flag to copy generated resource files to outClass + * + * @parameter expression="${jaxx.copyToCP}" default-value="true" + */ + protected boolean copyToCP; + + /** + * fichier de proprietes contenant les definitions d'options a utiliser. + * + * @parameter expression="${commandline.source}" + * @required + */ + protected File source; + + /** + * Repertoire de sortie. + * + * @parameter expression="${commandline.out}" default-value="${maven.src.dir}/java" + * @required + */ + protected File out; + + /** + * le FQN de la classe concrete du Context + * + * @parameter expression="${commandline.concreteContextFQN}" + * @required + */ + protected String concreteContextFQN; + + /** + * le parser a utiliser + * + * @parameter expression="${commandline.parserFQN}" default-value="org.codelutin.option.def.DefinitionParserFromProperties" + */ + protected String parserFQN; + + + /** + * flag to show errors of parsing. + * + * @parameter expression="${commandLine.showError}" default-value="true" + */ + protected boolean showErrors; + + /** + * fqn de la super classe du parseur a generer + * + * @parameter expression="${commandline.parserSuperClass}" default-value="org.codelutin.option.OptionParser" + */ + protected String parserSuperClass; + + /** + * fqn de la super classe des options a generer + * + * @parameter expression="${commandline.optionSuperClass}" default-value="org.codelutin.option.Option" + */ + protected String optionSuperClass; + + /** + * fqn de la super classe des configs a generer + * + * @parameter expression="${commandline.configSuperClass}" default-value="org.codelutin.option.Config" + */ + protected String configSuperClass; + + /** + * fqn de la super classe des actions a generer + * + * @parameter expression="${commandline.actionSuperClass}" default-value="org.codelutin.option.OptionAction" + */ + protected String actionSuperClass; + + /** + * fqn de la super classe des actions a generer + * + * @parameter expression="${commandline.contextSuperClass}" default-value="org.codelutin.option.AbstractContext" + */ + protected String contextSuperClass; + + /** + * flag pour indique si les configs generes sont concrete ou abstraites + * + * @parameter expression="${commandline.concreteConfig}" default-value="false" + */ + protected boolean concreteConfig; + + /** + * le nom du fichier de configuration dans la configuration genere + * + * @parameter expression="${commandline._configFileName}" default-value=".${project.artifactId}" + * @required + */ + protected String _configFileName; + + /** + * le nom du projet + * + * @parameter expression="${commandline._projectName}" default-value="${project.artifactId}" + * @required + */ + protected String _projectName; + + /** + * l'url de l'application + * + * @parameter expression="${commandline._projectURL}" + * @required + */ + protected String _projectURL; + + /** + * le nom du projet + * + * @parameter expression="${commandline._organisationName}" + * @required + */ + protected String _organisationName; + + /** + * l'url de l'application + * + * @parameter expression="${commandline._organisationURL}" + * @required + */ + protected String _organisationURL; + + /** + * l'encoding par default dans la configuration genere + * + * @parameter expression="${commandline._encoding}" default-value="UTF-8" + * @required + */ + protected String _encoding; + /** + * la locale par default dans la configuration genere + * + * @parameter expression="${commandline._locale}" default-value="en_GB" + * @required + */ + protected String _locale; + /** + * la version dans la configuration genere + * + * @parameter expression="${commandline._version}" default-value="${project.version}" + * @required + */ + protected String _version; + /** + * la version dans la configuration genere + * + * @parameter expression="${commandline._copyright}" + * @required + */ + protected String _copyright; + + + public JavaGeneratorGoal() { + super(JavaGeneratorContext.class); + } + + public void generate() throws Exception { + + + Map<String, String> map = new TreeMap<String, String>(); + // build a dictonary of options implementations name + for (OptionContext context : getContext().getOptionContexts()) { + String suffix = StringUtils.capitalize(context.getKey()); + String optionClassName = getContext().getOptionSimpleName() + suffix; + map.put(context.getKey(), optionClassName); + } + + // generate specialized abstract OptionAction for this parser + if (needSpecializedAction()) { + + AbstractOptionActionJavaGenerator.doGenerate(getContext()); + + setActionSuperClass(getContext().getActionSimpleName()); + } + + boolean isConcreteConfig = getContext().isConcreteConfig(); + + getContext().setConfigSimpleName(getContext().getPrefix() + (isConcreteConfig ? "" : "Abstract") + "Config"); + + // generate specialized abstract SimpleConfigImpl for this parser + if (needSpecializedAbstractConfig()) { + AbstractConfigJavaGenerator.doGenerate(getContext(), getPrefix() + "AbstractConfig"); + + // the super classes of generatred configs is the one generated here + setConfigSuperClass(getContext().getPrefix() + "AbstractConfig"); + } + + // generate specialized OptionKey for this parser + OptionKeyJavaGenerator.doGenerate(getContext()); + + // generate OptionParser implementation + OptionParserJavaGenerator.doGenerate(getContext()); + + // generate AbstractContext implementation + AbstractContextJavaGenerator.doGenerate(getContext()); + + // generate Config implementations + for (ConfigContext context : getContext().getConfigContexts()) { + String suffix = StringUtils.capitalize(context.getCategory()); + ConfigJavaGenerator.doGenerate(getContext(), context, + getContext().getConfigSimpleName() + suffix, + isConcreteConfig + ); + } + + // generate Option implementations + for (OptionContext context : getContext().getOptionContexts()) { + OptionJavaGenerator.doGenerate(getContext(), context, map.get(context.getKey())); + } + + // generate OptionAction implementations + for (OptionContext context : getContext().getOptionContexts()) { + String suffix = StringUtils.capitalize(context.getKey()); + String optionClassName = map.get(context.getKey()); + OptionActionJavaGenerator.doGenerate(getContext(), context, + getContext().getActionSimpleName() + suffix, + getContext().getActionSuperClass() + "<" + optionClassName + ">", + optionClassName + ); + } + map.clear(); + + // generate ConcreteContext serivce declaration + generateConcreteContextDeclaration(); + } + + public File getOut() { + return out; + } + + private boolean needSpecializedAction() { + return getContext().getActionSuperClass().equals(OptionAction.class.getName()); + } + + private boolean needSpecializedAbstractConfig() { + return getContext().getConfigSuperClass().equals(Config.class.getName()); + } + + public String getActionSuperClass() { + return actionSuperClass; + } + + public boolean isConcreteConfig() { + return concreteConfig; + } + + public String getConfigSuperClass() { + return configSuperClass; + } + + public String getContextSuperClass() { + return contextSuperClass; + } + + public String getOptionSuperClass() { + return optionSuperClass; + } + + public String getParserSuperClass() { + return parserSuperClass; + } + + public boolean isShowErrors() { + return showErrors; + } + + public File getSource() { + return source; + } + + public String getParserFQN() { + return parserFQN; + } + + public String getConcreteContextFQN() { + return concreteContextFQN; + } + + public void setConfigSuperClass(String configSuperClass) { + this.configSuperClass = configSuperClass; + } + + public void setActionSuperClass(String actionSuperClass) { + this.actionSuperClass = actionSuperClass; + } + + public void setShowErrors(boolean showErrors) { + this.showErrors = showErrors; + } + + public void setConcreteConfig(boolean concreteConfig) { + this.concreteConfig = concreteConfig; + } + + public void setContextSuperClass(String contextSuperClass) { + this.contextSuperClass = contextSuperClass; + } + + public void setOptionSuperClass(String optionSuperClass) { + this.optionSuperClass = optionSuperClass; + } + + public void setParserSuperClass(String parserSuperClass) { + this.parserSuperClass = parserSuperClass; + } + + public void setSource(File source) { + this.source = source; + } + + public void setOut(File out) { + this.out = out; + } + + public void setParserFQN(String parserFQN) { + this.parserFQN = parserFQN; + } + + public void setConcreteContextFQN(String concreteContextFQN) { + this.concreteContextFQN = concreteContextFQN; + } + + public void setOutResource(File outResource) { + this.outResource = outResource; + } + + protected void generateConcreteContextDeclaration() throws IOException { + File generatedProviderDeclaration = new File(outResource, "META-INF/services/" + org.codelutin.option.Context.class.getName()); + File parent = generatedProviderDeclaration.getParentFile(); + if (!parent.exists()) { + parent.mkdirs(); + } + + // just add the fqn inside the file :) + FileUtil.writeString(generatedProviderDeclaration, concreteContextFQN); + + if (copyToCP) { + // save it also in classes (since we are in process-resources phase and resources has already been copied) + copyResourceToCP(generatedProviderDeclaration); + } + } + + protected void copyResourceToCP(File file) throws IOException { + + String path = file.getAbsolutePath().substring(outResource.getAbsolutePath().length() + 1); + File compiledFile = new File(outClass, path); + if (verbose) { + getLog().info("copy to classapth generated file " + compiledFile); + } + File parent = compiledFile.getParentFile(); + if (!parent.exists()) { + parent.mkdirs(); + } + FileUtil.copy(file, compiledFile); + } +} Deleted: commandline/trunk/maven-commandline-plugin/src/main/java/org/codelutin/option/generate/util/AbstractGeneratorGoal.java =================================================================== --- commandline/trunk/maven-plugin/src/main/java/org/codelutin/option/generate/util/AbstractGeneratorGoal.java 2008-09-24 19:26:58 UTC (rev 1129) +++ commandline/trunk/maven-commandline-plugin/src/main/java/org/codelutin/option/generate/util/AbstractGeneratorGoal.java 2008-11-30 16:25:11 UTC (rev 1261) @@ -1,185 +0,0 @@ -/* -* *##% maven-plugin - * Copyright (C) 2008 CodeLutin - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%* */ -package org.codelutin.option.generate.util; - -import org.apache.maven.plugin.AbstractMojo; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; -import org.apache.maven.project.MavenProject; -import org.codelutin.i18n.I18n; -import org.codelutin.util.StringUtil; - -import java.io.File; - -/** - * Classe de base pour une goal de génération - * - * @author tony - */ -public abstract class AbstractGeneratorGoal<C extends AbstractGeneratorContext> extends AbstractMojo { - - /** - * @description Dépendance du projet. - * @parameter default-value="${project}" - * @readonly - */ - protected MavenProject project; - - /** - * @description prefix - * @parameter expression="${commandline.prefix}" - * @required - */ - protected String prefix; - - /** - * @description Package where to generate. - * @parameter expression="${commandline.packageName}" - * @required - */ - protected String packageName; - - /** - * @description prefix i18n pour la génération des clefs . - * @parameter expression="${commandline.i18nPrefix}" - */ - protected String i18nPrefix; - - /** - * @description flag pour afficher verbeusement ou non les logs - * @parameter expression="${commandline.verbose}" default-value="${maven.verbose}" - */ - protected boolean verbose; - - /** - * @description encoding pour les fichiers a generer - * @parameter expression="${commandline.encoding}" default-value="UTF-8" - */ - protected String encoding; - - /** timestamp */ - protected final long timestamp; - - private C context; - - protected AbstractGeneratorGoal(Class<C> klass) { - timestamp = System.nanoTime(); - try { - context = klass.newInstance(); - } catch (InstantiationException e) { - throw new RuntimeException(e); - } catch (IllegalAccessException e) { - throw new RuntimeException(e); - } - } - - protected abstract void generate() throws Exception; - - @SuppressWarnings({"unchecked"}) - public void execute() throws MojoExecutionException, MojoFailureException { - - System.setProperty("org.apache.commons.logging.LogFactory", org.apache.commons.logging.impl.LogFactoryImpl.class.getName()); - - - try { - // force UTF-8 encoding ! - encoding = I18n.UTF_8_ENCONDING; - - // init context - context.init(this); - - // do implented action - generate(); - - } catch (Exception e) { - throw new MojoExecutionException(e.getMessage(), e); - } finally { - getLog().info(getClass().getSimpleName() + " - total time " + StringUtil.convertTime(timestamp, System.nanoTime())); - } - } - - public C getContext() { - return context; - } - - public String getPrefix() { - return prefix; - } - - public String getEncoding() { - return encoding; - } - - public String getI18nPrefix() { - return i18nPrefix; - } - - public abstract File getOut(); - - public String getPackageName() { - return packageName; - } - - public MavenProject getProject() { - return project; - } - - public long getTimestamp() { - return timestamp; - } - - public boolean isVerbose() { - return verbose; - } - - public void setI18nPrefix(String i18nPrefix) { - this.i18nPrefix = i18nPrefix; - } - - public void setEncoding(String encoding) { - this.encoding = encoding; - } - - public void setPackageName(String packageName) { - this.packageName = packageName; - } - - public void setPrefix(String prefix) { - this.prefix = prefix; - } - - public void setProject(MavenProject project) { - this.project = project; - } - - public void setVerbose(boolean verbose) { - this.verbose = verbose; - } - - /** - * permet d'ajout le répertoire de génération des fichiers java dans les répertoires - * de compilation du projet Maven. - * - * @param dirGen le repertoire de generation des fichiers java - */ - public void fixCompileSourceRoots(File dirGen) { - if (!project.getCompileSourceRoots().contains(dirGen.getPath())) { - project.addCompileSourceRoot(dirGen.getPath()); - } - } -} Copied: commandline/trunk/maven-commandline-plugin/src/main/java/org/codelutin/option/generate/util/AbstractGeneratorGoal.java (from rev 1260, commandline/trunk/maven-plugin/src/main/java/org/codelutin/option/generate/util/AbstractGeneratorGoal.java) =================================================================== --- commandline/trunk/maven-commandline-plugin/src/main/java/org/codelutin/option/generate/util/AbstractGeneratorGoal.java (rev 0) +++ commandline/trunk/maven-commandline-plugin/src/main/java/org/codelutin/option/generate/util/AbstractGeneratorGoal.java 2008-11-30 16:25:11 UTC (rev 1261) @@ -0,0 +1,185 @@ +/* +* *##% maven-plugin + * Copyright (C) 2008 CodeLutin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%* */ +package org.codelutin.option.generate.util; + +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.project.MavenProject; +import org.codelutin.i18n.I18n; +import org.codelutin.util.StringUtil; + +import java.io.File; + +/** + * Classe de base pour une goal de génération + * + * @author tony + */ +public abstract class AbstractGeneratorGoal<C extends AbstractGeneratorContext> extends AbstractMojo { + + /** + * @description Dépendance du projet. + * @parameter default-value="${project}" + * @readonly + */ + protected MavenProject project; + + /** + * @description prefix + * @parameter expression="${commandline.prefix}" + * @required + */ + protected String prefix; + + /** + * @description Package where to generate. + * @parameter expression="${commandline.packageName}" + * @required + */ + protected String packageName; + + /** + * @description prefix i18n pour la génération des clefs . + * @parameter expression="${commandline.i18nPrefix}" + */ + protected String i18nPrefix; + + /** + * @description flag pour afficher verbeusement ou non les logs + * @parameter expression="${commandline.verbose}" default-value="${maven.verbose}" + */ + protected boolean verbose; + + /** + * @description encoding pour les fichiers a generer + * @parameter expression="${commandline.encoding}" default-value="UTF-8" + */ + protected String encoding; + + /** timestamp */ + protected final long timestamp; + + private C context; + + protected AbstractGeneratorGoal(Class<C> klass) { + timestamp = System.nanoTime(); + try { + context = klass.newInstance(); + } catch (InstantiationException e) { + throw new RuntimeException(e); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + } + + protected abstract void generate() throws Exception; + + @SuppressWarnings({"unchecked"}) + public void execute() throws MojoExecutionException, MojoFailureException { + + System.setProperty("org.apache.commons.logging.LogFactory", org.apache.commons.logging.impl.LogFactoryImpl.class.getName()); + + + try { + // force UTF-8 encoding ! + encoding = I18n.UTF_8_ENCONDING; + + // init context + context.init(this); + + // do implented action + generate(); + + } catch (Exception e) { + throw new MojoExecutionException(e.getMessage(), e); + } finally { + getLog().info(getClass().getSimpleName() + " - total time " + StringUtil.convertTime(timestamp, System.nanoTime())); + } + } + + public C getContext() { + return context; + } + + public String getPrefix() { + return prefix; + } + + public String getEncoding() { + return encoding; + } + + public String getI18nPrefix() { + return i18nPrefix; + } + + public abstract File getOut(); + + public String getPackageName() { + return packageName; + } + + public MavenProject getProject() { + return project; + } + + public long getTimestamp() { + return timestamp; + } + + public boolean isVerbose() { + return verbose; + } + + public void setI18nPrefix(String i18nPrefix) { + this.i18nPrefix = i18nPrefix; + } + + public void setEncoding(String encoding) { + this.encoding = encoding; + } + + public void setPackageName(String packageName) { + this.packageName = packageName; + } + + public void setPrefix(String prefix) { + this.prefix = prefix; + } + + public void setProject(MavenProject project) { + this.project = project; + } + + public void setVerbose(boolean verbose) { + this.verbose = verbose; + } + + /** + * permet d'ajout le répertoire de génération des fichiers java dans les répertoires + * de compilation du projet Maven. + * + * @param dirGen le repertoire de generation des fichiers java + */ + public void fixCompileSourceRoots(File dirGen) { + if (project!=null && !project.getCompileSourceRoots().contains(dirGen.getPath())) { + project.addCompileSourceRoot(dirGen.getPath()); + } + } +} Deleted: commandline/trunk/maven-commandline-plugin/src/test/java/org/codelutin/option/def/DefinitionParserBadOptionTest.java =================================================================== --- commandline/trunk/maven-plugin/src/test/java/org/codelutin/option/def/DefinitionParserBadOptionTest.java 2008-09-24 19:26:58 UTC (rev 1129) +++ commandline/trunk/maven-commandline-plugin/src/test/java/org/codelutin/option/def/DefinitionParserBadOptionTest.java 2008-11-30 16:25:11 UTC (rev 1261) @@ -1,206 +0,0 @@ -/** - * *##% maven-plugin - * Copyright (C) 2008 CodeLutin - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%* - */ -package org.codelutin.option.def; - -import junit.framework.TestCase; - -import java.io.File; -import java.util.Collections; - -/** - * DefinitionParser Tester. - * - * @author chemit - * @version 1.0 - * @since <pre>11/18/2007</pre> - */ -public class DefinitionParserBadOptionTest extends TestCase { - - @Override - protected void setUp() throws Exception { - super.setUp(); - DefaultOptionAction.skipDefault = true; - ParserUtilForTest.initI18n(); - } - - DefinitionParser parser; - File file; - String key; - - public void testOneOptionBadAlias() throws Exception { - - initAndParse("testOneOption"); - ParserUtilForTest.assertError(parser, 0, 1); - - initAndParse("-testOneOptionsBad00|t"); - ParserUtilForTest.assertError(parser, 0, 1); - - initAndParse("-testOneOptionsBad00|-t|-t2|-t3|-t4"); - ParserUtilForTest.assertError(parser, 0, 1); - - initAndParse("-testOneOptionsBad00|-t|-t2|t3|-t4|-t5|t55"); - ParserUtilForTest.assertError(parser, 0, 3); - - initAndParse("-testOneOptionsBad000 {0,2}", "-testOneOptionsBad000 {0,4}"); - ParserUtilForTest.assertError(parser, 1, 1); - - initAndParse("-testOneOptionsBad000|-t0", "-testOneOptionsBad001|-t0"); - ParserUtilForTest.assertError(parser, 1, 1); - } - - public void testOneOptionBadCardinalite() throws Exception { - - initAndParse("-testBadCardinalite0 {-1,1}"); - ParserUtilForTest.assertError(parser, 1, 0); - - initAndParse("-testBadCardinalite1 {1,-2}"); - ParserUtilForTest.assertError(parser, 1, 0); - - initAndParse("-testBadCardinalite2 {1,0}"); - ParserUtilForTest.assertError(parser, 0, 2); - - initAndParse("-testBadCardinalite3 {5,3}"); - ParserUtilForTest.assertError(parser, 0, 1); - - } - - public void testOneOptionBadConstantArgumentCardinalite() throws Exception { - key = "testBadArgumentConstantCardinalite0"; - initAndParse("-testBadArgumentConstantCardinalite0 <constant{0,0}>"); - - ParserUtilForTest.assertError(parser, 0, 1); - - key = "testBadArgumentConstantCardinalite00"; - initAndParse("-testBadArgumentConstantCardinalite00 <constant{-1,0}>"); - - ParserUtilForTest.assertError(parser, 1, 0); - - key = "testBadArgumentConstantCardinalite000"; - initAndParse("-testBadArgumentConstantCardinalite000 <constant{0,-2}>"); - - ParserUtilForTest.assertError(parser, 1, 0); - - key = "testBadArgumentConstantCardinalite0000"; - initAndParse("-testBadArgumentConstantCardinalite0000 <constant{2,1}>"); - - ParserUtilForTest.assertError(parser, 0, 1); - - key = "testBadArgumentConstantCardinalite00000"; - initAndParse("-testBadArgumentConstantCardinalite00000 <constant{0,1}>"); - - ParserUtilForTest.assertError(parser, 0, 1); - - key = "testBadArgumentConstantCardinalite00001"; - initAndParse("-testBadArgumentConstantCardinalite00001 <constant*>"); - - ParserUtilForTest.assertError(parser, 0, 1); - - key = "testBadArgumentConstantCardinalite00002"; - initAndParse("-testBadArgumentConstantCardinalite00002 <constant{1,2}>"); - - ParserUtilForTest.assertError(parser, 0, 1); - - key = "testBadArgumentConstantCardinalite00003"; - initAndParse("-testBadArgumentConstantCardinalite00003 <constant+>"); - - ParserUtilForTest.assertError(parser, 0, 1); - - key = "testBadArgumentConstantCardinalite00004"; - initAndParse("-testBadArgumentConstantCardinalite00004 <constant{1,-1}>"); - - ParserUtilForTest.assertError(parser, 1, 0); - - key = "testBadArgumentConstantCardinalite00010"; - initAndParse("-testBadArgumentConstantCardinalite00010 [constant{0,5}]"); - - ParserUtilForTest.assertError(parser, 0, 1); - - key = "testBadArgumentConstantCardinalite00011"; - initAndParse("-testBadArgumentConstantCardinalite00011 [constant*]"); - - ParserUtilForTest.assertError(parser, 0, 1); - - key = "testBadArgumentConstantCardinalite00012"; - initAndParse("-testBadArgumentConstantCardinalite00012 [constant{1,2}]"); - - ParserUtilForTest.assertError(parser, 0, 1); - - key = "testBadArgumentConstantCardinalite00013"; - initAndParse("-testBadArgumentConstantCardinalite00013 [constant+]"); - - ParserUtilForTest.assertError(parser, 0, 1); - - key = "testBadArgumentConstantCardinalite00014"; - initAndParse("-testBadArgumentConstantCardinalite00014 [constant{1,-1}]"); - - ParserUtilForTest.assertError(parser, 1, 0); - } - - public void testOneOptionBadValuedArgumentCardinalite() throws Exception { - key = "testBadArgumentValuedCardinalite0"; - initAndParse("-testBadArgumentValuedCardinalite0 <string:String{0,0}>"); - - ParserUtilForTest.assertError(parser, 0, 1); - - key = "testBadArgumentValuedCardinalite00"; - initAndParse("-testBadArgumentValuedCardinalite00 <string:String{-1,0}>"); - - ParserUtilForTest.assertError(parser, 0, 1); - - key = "testBadArgumentValuedCardinalite000"; - initAndParse("-testBadArgumentValuedCardinalit000 <string:String{0,-1}>"); - - ParserUtilForTest.assertError(parser, 0, 1); - - key = "testBadArgumentValuedCardinalite0000"; - initAndParse("-testBadArgumentValuedCardinalite0000 <string:String{2,1}>"); - - ParserUtilForTest.assertError(parser, 0, 1); - - key = "testBadArgumentValuedCardinalite00000"; - initAndParse("-testBadArgumentValuedCardinalite00000 <string:String{0,1}>"); - - ParserUtilForTest.assertError(parser, 0, 1); - - key = "testBadArgumentValuedCardinalite00001"; - initAndParse("-testBadArgumentValuedCardinalite00001 <string:String*>"); - - ParserUtilForTest.assertError(parser, 0, 1); - - key = "testBadArgumentValuedCardinalite00002"; - initAndParse("-testBadArgumentValuedCardinalite00002 <string:String{0,2}>"); - ParserUtilForTest.assertError(parser, 0, 1); - - initAndParse("-testArgumentValuedCardinalite00012 [string:String{1,2}]"); - ParserUtilForTest.assertError(parser, 0, 1); - - initAndParse("-testBadArgumentValuedCardinalite00013 [string:String+]"); - ParserUtilForTest.assertError(parser, 0, 1); - - initAndParse("-testBadArgumentValuedCardinalite00014 [string:String{1,-1}]"); - ParserUtilForTest.assertError(parser, 0, 1); - - } - - private void initAndParse(String... defs) throws Exception { - key = getName() + System.currentTimeMillis(); - file = ParserUtilForTest.initFileMock(key, "option", defs); - parser = DefinitionParser.doParse(DefinitionParserFromProperties.class, file, Collections.<String, String>emptyMap()); - } -} Copied: commandline/trunk/maven-commandline-plugin/src/test/java/org/codelutin/option/def/DefinitionParserBadOptionTest.java (from rev 1260, commandline/trunk/maven-plugin/src/test/java/org/codelutin/option/def/DefinitionParserBadOptionTest.java) =================================================================== --- commandline/trunk/maven-commandline-plugin/src/test/java/org/codelutin/option/def/DefinitionParserBadOptionTest.java (rev 0) +++ commandline/trunk/maven-commandline-plugin/src/test/java/org/codelutin/option/def/DefinitionParserBadOptionTest.java 2008-11-30 16:25:11 UTC (rev 1261) @@ -0,0 +1,222 @@ +/** + * *##% maven-plugin + * Copyright (C) 2008 CodeLutin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%* + */ +package org.codelutin.option.def; + +import org.codelutin.i18n.I18n; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.io.File; +import java.util.Collections; + +/** + * DefinitionParser Tester. + * + * @author chemit + * @version 1.0 + * @since <pre>11/18/2007</pre> + */ +public class DefinitionParserBadOptionTest { + + @BeforeClass + public static void beforeClass() { + I18n.init(); + DefaultOptionAction.skipDefault = true; + basedir = ParserUtilForTest.getBaseDir(); + } + + protected static File basedir; + + protected DefinitionParser parser; + protected File file; + protected String key; + protected String name; + + @Test + public void testOneOptionBadAlias() throws Exception { + name = "testOneOptionBadAlias"; + initAndParse("testOneOption"); + ParserUtilForTest.assertError(parser, 0, 1); + + initAndParse("-testOneOptionsBad00|t"); + ParserUtilForTest.assertError(parser, 0, 1); + + initAndParse("-testOneOptionsBad00|-t|-t2|-t3|-t4"); + ParserUtilForTest.assertError(parser, 0, 1); + + initAndParse("-testOneOptionsBad00|-t|-t2|t3|-t4|-t5|t55"); + ParserUtilForTest.assertError(parser, 0, 3); + + initAndParse("-testOneOptionsBad000 {0,2}", "-testOneOptionsBad000 {0,4}"); + ParserUtilForTest.assertError(parser, 1, 1); + + initAndParse("-testOneOptionsBad000|-t0", "-testOneOptionsBad001|-t0"); + ParserUtilForTest.assertError(parser, 1, 1); + } + + @Test + public void testOneOptionBadCardinalite() throws Exception { + + name = "testOneOptionBadCardinalite"; + + initAndParse("-testBadCardinalite0 {-1,1}"); + ParserUtilForTest.assertError(parser, 1, 0); + + initAndParse("-testBadCardinalite1 {1,-2}"); + ParserUtilForTest.assertError(parser, 1, 0); + + initAndParse("-testBadCardinalite2 {1,0}"); + ParserUtilForTest.assertError(parser, 0, 2); + + initAndParse("-testBadCardinalite3 {5,3}"); + ParserUtilForTest.assertError(parser, 0, 1); + + } + + @Test + public void testOneOptionBadConstantArgumentCardinalite() throws Exception { + name = "testOneOptionBadConstantArgumentCardinalite"; + + key = "testBadArgumentConstantCardinalite0"; + initAndParse("-testBadArgumentConstantCardinalite0 <constant{0,0}>"); + + ParserUtilForTest.assertError(parser, 0, 1); + + key = "testBadArgumentConstantCardinalite00"; + initAndParse("-testBadArgumentConstantCardinalite00 <constant{-1,0}>"); + + ParserUtilForTest.assertError(parser, 1, 0); + + key = "testBadArgumentConstantCardinalite000"; + initAndParse("-testBadArgumentConstantCardinalite000 <constant{0,-2}>"); + + ParserUtilForTest.assertError(parser, 1, 0); + + key = "testBadArgumentConstantCardinalite0000"; + initAndParse("-testBadArgumentConstantCardinalite0000 <constant{2,1}>"); + + ParserUtilForTest.assertError(parser, 0, 1); + + key = "testBadArgumentConstantCardinalite00000"; + initAndParse("-testBadArgumentConstantCardinalite00000 <constant{0,1}>"); + + ParserUtilForTest.assertError(parser, 0, 1); + + key = "testBadArgumentConstantCardinalite00001"; + initAndParse("-testBadArgumentConstantCardinalite00001 <constant*>"); + + ParserUtilForTest.assertError(parser, 0, 1); + + key = "testBadArgumentConstantCardinalite00002"; + initAndParse("-testBadArgumentConstantCardinalite00002 <constant{1,2}>"); + + ParserUtilForTest.assertError(parser, 0, 1); + + key = "testBadArgumentConstantCardinalite00003"; + initAndParse("-testBadArgumentConstantCardinalite00003 <constant+>"); + + ParserUtilForTest.assertError(parser, 0, 1); + + key = "testBadArgumentConstantCardinalite00004"; + initAndParse("-testBadArgumentConstantCardinalite00004 <constant{1,-1}>"); + + ParserUtilForTest.assertError(parser, 1, 0); + + key = "testBadArgumentConstantCardinalite00010"; + initAndParse("-testBadArgumentConstantCardinalite00010 [constant{0,5}]"); + + ParserUtilForTest.assertError(parser, 0, 1); + + key = "testBadArgumentConstantCardinalite00011"; + initAndParse("-testBadArgumentConstantCardinalite00011 [constant*]"); + + ParserUtilForTest.assertError(parser, 0, 1); + + key = "testBadArgumentConstantCardinalite00012"; + initAndParse("-testBadArgumentConstantCardinalite00012 [constant{1,2}]"); + + ParserUtilForTest.assertError(parser, 0, 1); + + key = "testBadArgumentConstantCardinalite00013"; + initAndParse("-testBadArgumentConstantCardinalite00013 [constant+]"); + + ParserUtilForTest.assertError(parser, 0, 1); + + key = "testBadArgumentConstantCardinalite00014"; + initAndParse("-testBadArgumentConstantCardinalite00014 [constant{1,-1}]"); + + ParserUtilForTest.assertError(parser, 1, 0); + } + + @Test + public void testOneOptionBadValuedArgumentCardinalite() throws Exception { + + name = "testOneOptionBadValuedArgumentCardinalite"; + + key = "testBadArgumentValuedCardinalite0"; + initAndParse("-testBadArgumentValuedCardinalite0 <string:String{0,0}>"); + + ParserUtilForTest.assertError(parser, 0, 1); + + key = "testBadArgumentValuedCardinalite00"; + initAndParse("-testBadArgumentValuedCardinalite00 <string:String{-1,0}>"); + + ParserUtilForTest.assertError(parser, 0, 1); + + key = "testBadArgumentValuedCardinalite000"; + initAndParse("-testBadArgumentValuedCardinalit000 <string:String{0,-1}>"); + + ParserUtilForTest.assertError(parser, 0, 1); + + key = "testBadArgumentValuedCardinalite0000"; + initAndParse("-testBadArgumentValuedCardinalite0000 <string:String{2,1}>"); + + ParserUtilForTest.assertError(parser, 0, 1); + + key = "testBadArgumentValuedCardinalite00000"; + initAndParse("-testBadArgumentValuedCardinalite00000 <string:String{0,1}>"); + + ParserUtilForTest.assertError(parser, 0, 1); + + key = "testBadArgumentValuedCardinalite00001"; + initAndParse("-testBadArgumentValuedCardinalite00001 <string:String*>"); + + ParserUtilForTest.assertError(parser, 0, 1); + + key = "testBadArgumentValuedCardinalite00002"; + initAndParse("-testBadArgumentValuedCardinalite00002 <string:String{0,2}>"); + ParserUtilForTest.assertError(parser, 0, 1); + + initAndParse("-testArgumentValuedCardinalite00012 [string:String{1,2}]"); + ParserUtilForTest.assertError(parser, 0, 1); + + initAndParse("-testBadArgumentValuedCardinalite00013 [string:String+]"); + ParserUtilForTest.assertError(parser, 0, 1); + + initAndParse("-testBadArgumentValuedCardinalite00014 [string:String{1,-1}]"); + ParserUtilForTest.assertError(parser, 0, 1); + + } + + private void initAndParse(String... defs) throws Exception { + key = name + System.currentTimeMillis(); + file = ParserUtilForTest.initFileMock(basedir, key, "option", defs); + parser = DefinitionParser.doParse(DefinitionParserFromProperties.class, file, Collections.<String, String>emptyMap()); + } +} Deleted: commandline/trunk/maven-commandline-plugin/src/test/java/org/codelutin/option/def/DefinitionParserTest.java =================================================================== --- commandline/trunk/maven-plugin/src/test/java/org/codelutin/option/def/DefinitionParserTest.java 2008-09-24 19:26:58 UTC (rev 1129) +++ commandline/trunk/maven-commandline-plugin/src/test/java/org/codelutin/option/def/DefinitionParserTest.java 2008-11-30 16:25:11 UTC (rev 1261) @@ -1,163 +0,0 @@ -/** - * *##% maven-plugin - * Copyright (C) 2008 CodeLutin - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%* - */ -package org.codelutin.option.def; - -import junit.framework.TestCase; -import org.codelutin.i18n.I18n; - -import java.io.File; -import java.util.Collections; - -/** - * DefinitionParser Tester. - * - * @author chemit - * @version 1.0 - * @since <pre>11/18/2007</pre> - */ -public class DefinitionParserTest extends TestCase { - - DefinitionParser parser; - File file; - String optionName; - - static boolean i18nInit = false; - - @Override - protected void setUp() throws Exception { - super.setUp(); - if (!i18nInit) { - DefaultOptionAction.skipDefault = true; - - I18n.init(); - i18nInit = true; - } - } - - public void testNoOptions() throws Exception { - file = ParserUtilForTest.initFileMock("", "option", "testNoOptions.properties"); - parser = DefinitionParser.doParse(DefinitionParserFromProperties.class, file, Collections.<String, String>emptyMap()); - assertTrue(parser.getOptions().length == 0); - } - - public void testOneOption() throws Exception { - - /*STRING optionName; - optionName = "testOneOptions0"; - file = initFileMock(optionName, "--testOneOptions0", "with no args and one alias"); - parser = DefinitionParser.doParse(properties, file); - assertEquals(1, parser.getDefinitions().length); - assertEquals(1, parser.getDefinitions()[0].getAlias().length); - assertEquals("--" + optionName, parser.getDefinitions()[0].getAlias()[0]); - assertEquals(0, parser.getDefinitions()[0].getGroups().length); - - optionName = "testOneOptions00"; - file = initFileMock(optionName, "--testOneOptions00|-one0", "with no args and two alias"); - parser = DefinitionParser.doParse(properties, file); - assertEquals(1, parser.getDefinitions().length); - assertEquals(2, parser.getDefinitions()[0].getAlias().length); - assertEquals("--" + optionName, parser.getDefinitions()[0].getAlias()[0]); - assertEquals("-one0", parser.getDefinitions()[0].getAlias()[1]); - assertEquals(0, parser.getDefinitions()[0].getGroups().length); - - optionName = "testOneOptions000"; - file = initFileMock(optionName, "--testOneOptions000|-one0|-o0", "with no args and three alias"); - parser = DefinitionParser.doParse(properties, file); - assertEquals(1, parser.getDefinitions().length); - assertEquals(3, parser.getDefinitions()[0].getAlias().length); - assertEquals("--" + optionName, parser.getDefinitions()[0].getAlias()[0]); - assertEquals("-one0", parser.getDefinitions()[0].getAlias()[1]); - assertEquals("-o0", parser.getDefinitions()[0].getAlias()[2]); - assertEquals(0, parser.getDefinitions()[0].getGroups().length); - - optionName = "testOneOptions1"; - file = initFileMock(optionName, "--testOneOptions1|-one1 <constant|constant2|constant3>", "with one group of three constant and two alias"); - parser = DefinitionParser.doParse(properties, file); - assertEquals(1, parser.getDefinitions().length); - assertEquals(2, parser.getDefinitions()[0].getAlias().length); - assertEquals("--" + optionName, parser.getDefinitions()[0].getAlias()[0]); - assertEquals("-one1", parser.getDefinitions()[0].getAlias()[1]); - assertEquals(3, parser.getDefinitions()[0].getGroups().length); - - - optionName = "testOneOptions2"; - file = initFileMock(optionName, "--testOneOptions2|-one2 <string:STRING|integer:integer|float:Float|file:File|clazz:class>", "with one group of 5 valued args and two alias"); - parser = DefinitionParser.doParse(properties, file); - assertEquals(1, parser.getDefinitions().length); - assertEquals(2, parser.getDefinitions()[0].getAlias().length); - assertEquals("--" + optionName, parser.getDefinitions()[0].getAlias()[0]); - assertEquals("-one2", parser.getDefinitions()[0].getAlias()[1]); - assertEquals(5, parser.getDefinitions()[0].getGroups().length); - - optionName = "testOneOptions3"; - file = initFileMock(optionName, "--testOneOptions3|-one3 <string=STRING|integer=Integer|float=Float|file=File|clazz=Class|newfile=NewFile>", "with one group of 6 namedAndValued args and two alias"); - parser = DefinitionParser.doParse(properties, file); - assertEquals(1, parser.getDefinitions().length); - assertEquals(2, parser.getDefinitions()[0].getAlias().length); - assertEquals("--" + optionName, parser.getDefinitions()[0].getAlias()[0]); - assertEquals("-one3", parser.getDefinitions()[0].getAlias()[1]); - assertEquals(6, parser.getDefinitions()[0].getGroups().length); - - optionName = "testOneOptions4"; - file = initFileMock(optionName, "--testOneOptions4|-one4 <constant|constant2|constant3|string:STRING|integer:integer|float:Float|file:File|clazz:class|string2=STRING|integer2=Integer|float2=FLOAT|file2=FILE|clazz2=Class|newfile2=NewFile>", "with one group of 14 args (3 constants, 5 valued, 6 namedAndValued) and two alias"); - parser = DefinitionParser.doParse(properties, file); - //printParserDetails(); - - assertEquals(1, parser.getDefinitions().length); - assertEquals(2, parser.getDefinitions()[0].getAlias().length); - assertEquals("--" + optionName, parser.getDefinitions()[0].getAlias()[0]); - assertEquals("-one4", parser.getDefinitions()[0].getAlias()[1]); - assertEquals(14, parser.getDefinitions()[0].getGroups().length);*/ - } - - public void testOneOptionArgumentCardinalite() throws Exception { - - optionName = "testArgumentConstantCardinalite00005"; - file = ParserUtilForTest.initFileMock(optionName, "option", "-testArgumentConstantCardinalite00005 <constant>"); - parser = DefinitionParser.doParse(DefinitionParserFromProperties.class, file, Collections.<String, String>emptyMap()); - ParserUtilForTest.assertError(parser, 1, 0); - - optionName = "testArgumentConstantCardinalite00015"; - file = ParserUtilForTest.initFileMock(optionName, "option", "-testArgumentConstantCardinalite00015 [constant]"); - parser = DefinitionParser.doParse(DefinitionParserFromProperties.class, file, Collections.<String, String>emptyMap()); - ParserUtilForTest.assertError(parser, 1, 0); - - optionName = "testArgumentValuedCardinalite00003"; - file = ParserUtilForTest.initFileMock(optionName, "option", "-testArgumentValuedCardinalite00003 <string:String+>"); - parser = DefinitionParser.doParse(DefinitionParserFromProperties.class, file, Collections.<String, String>emptyMap()); - ParserUtilForTest.assertError(parser, 1, 0); - - optionName = "testArgumentValuedCardinalite00004"; - file = ParserUtilForTest.initFileMock(optionName, "option", "-testArgumentValuedCardinalite00004 <string:String{1,*}>"); - parser = DefinitionParser.doParse(DefinitionParserFromProperties.class, file, Collections.<String, String>emptyMap()); - ParserUtilForTest.assertError(parser, 1, 0); - - optionName = "testArgumentValuedCardinalite00010"; - file = ParserUtilForTest.initFileMock(optionName, "option", "-testArgumentValuedCardinalite00010 [string:String{0,5}]"); - parser = DefinitionParser.doParse(DefinitionParserFromProperties.class, file, Collections.<String, String>emptyMap()); - ParserUtilForTest.assertError(parser, 1, 0); - - optionName = "testArgumentValuedCardinalite00011"; - - file = ParserUtilForTest.initFileMock(optionName, "option", "-testArgumentValuedCardinalite00011 [string:String*]"); - parser = DefinitionParser.doParse(DefinitionParserFromProperties.class, file, Collections.<String, String>emptyMap()); - ParserUtilForTest.assertError(parser, 1, 0); - } - -} Copied: commandline/trunk/maven-commandline-plugin/src/test/java/org/codelutin/option/def/DefinitionParserTest.java (from rev 1260, commandline/trunk/maven-plugin/src/test/java/org/codelutin/option/def/DefinitionParserTest.java) =================================================================== --- commandline/trunk/maven-commandline-plugin/src/test/java/org/codelutin/option/def/DefinitionParserTest.java (rev 0) +++ commandline/trunk/maven-commandline-plugin/src/test/java/org/codelutin/option/def/DefinitionParserTest.java 2008-11-30 16:25:11 UTC (rev 1261) @@ -0,0 +1,165 @@ +/** + * *##% maven-plugin + * Copyright (C) 2008 CodeLutin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%* + */ +package org.codelutin.option.def; + +import org.codelutin.i18n.I18n; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.io.File; +import java.util.Collections; + +/** + * DefinitionParser Tester. + * + * @author chemit + * @version 1.0 + * @since <pre>11/18/2007</pre> + */ +public class DefinitionParserTest { + + + protected static File basedir; + + DefinitionParser parser; + File file; + String optionName; + + @BeforeClass + public static void beforeClass() { + DefaultOptionAction.skipDefault = true; + basedir = ParserUtilForTest.getBaseDir(); + I18n.init(); + } + + @Test + public void testNoOptions() throws Exception { + file = ParserUtilForTest.initFileMock(basedir, "option", "testNoOptions.properties"); + parser = DefinitionParser.doParse(DefinitionParserFromProperties.class, file, Collections.<String, String>emptyMap()); + Assert.assertTrue(parser.getOptions().length == 0); + } + + @Test + public void testOneOption() throws Exception { + + /*STRING optionName; + optionName = "testOneOptions0"; + file = initFileMock(optionName, "--testOneOptions0", "with no args and one alias"); + parser = DefinitionParser.doParse(properties, file); + assertEquals(1, parser.getDefinitions().length); + assertEquals(1, parser.getDefinitions()[0].getAlias().length); + assertEquals("--" + optionName, parser.getDefinitions()[0].getAlias()[0]); + assertEquals(0, parser.getDefinitions()[0].getGroups().length); + + optionName = "testOneOptions00"; + file = initFileMock(optionName, "--testOneOptions00|-one0", "with no args and two alias"); + parser = DefinitionParser.doParse(properties, file); + assertEquals(1, parser.getDefinitions().length); + assertEquals(2, parser.getDefinitions()[0].getAlias().length); + assertEquals("--" + optionName, parser.getDefinitions()[0].getAlias()[0]); + assertEquals("-one0", parser.getDefinitions()[0].getAlias()[1]); + assertEquals(0, parser.getDefinitions()[0].getGroups().length); + + optionName = "testOneOptions000"; + file = initFileMock(optionName, "--testOneOptions000|-one0|-o0", "with no args and three alias"); + parser = DefinitionParser.doParse(properties, file); + assertEquals(1, parser.getDefinitions().length); + assertEquals(3, parser.getDefinitions()[0].getAlias().length); + assertEquals("--" + optionName, parser.getDefinitions()[0].getAlias()[0]); + assertEquals("-one0", parser.getDefinitions()[0].getAlias()[1]); + assertEquals("-o0", parser.getDefinitions()[0].getAlias()[2]); + assertEquals(0, parser.getDefinitions()[0].getGroups().length); + + optionName = "testOneOptions1"; + file = initFileMock(optionName, "--testOneOptions1|-one1 <constant|constant2|constant3>", "with one group of three constant and two alias"); + parser = DefinitionParser.doParse(properties, file); + assertEquals(1, parser.getDefinitions().length); + assertEquals(2, parser.getDefinitions()[0].getAlias().length); + assertEquals("--" + optionName, parser.getDefinitions()[0].getAlias()[0]); + assertEquals("-one1", parser.getDefinitions()[0].getAlias()[1]); + assertEquals(3, parser.getDefinitions()[0].getGroups().length); + + + optionName = "testOneOptions2"; + file = initFileMock(optionName, "--testOneOptions2|-one2 <string:STRING|integer:integer|float:Float|file:File|clazz:class>", "with one group of 5 valued args and two alias"); + parser = DefinitionParser.doParse(properties, file); + assertEquals(1, parser.getDefinitions().length); + assertEquals(2, parser.getDefinitions()[0].getAlias().length); + assertEquals("--" + optionName, parser.getDefinitions()[0].getAlias()[0]); + assertEquals("-one2", parser.getDefinitions()[0].getAlias()[1]); + assertEquals(5, parser.getDefinitions()[0].getGroups().length); + + optionName = "testOneOptions3"; + file = initFileMock(optionName, "--testOneOptions3|-one3 <string=STRING|integer=Integer|float=Float|file=File|clazz=Class|newfile=NewFile>", "with one group of 6 namedAndValued args and two alias"); + parser = DefinitionParser.doParse(properties, file); + assertEquals(1, parser.getDefinitions().length); + assertEquals(2, parser.getDefinitions()[0].getAlias().length); + assertEquals("--" + optionName, parser.getDefinitions()[0].getAlias()[0]); + assertEquals("-one3", parser.getDefinitions()[0].getAlias()[1]); + assertEquals(6, parser.getDefinitions()[0].getGroups().length); + + optionName = "testOneOptions4"; + file = initFileMock(optionName, "--testOneOptions4|-one4 <constant|constant2|constant3|string:STRING|integer:integer|float:Float|file:File|clazz:class|string2=STRING|integer2=Integer|float2=FLOAT|file2=FILE|clazz2=Class|newfile2=NewFile>", "with one group of 14 args (3 constants, 5 valued, 6 namedAndValued) and two alias"); + parser = DefinitionParser.doParse(properties, file); + //printParserDetails(); + + assertEquals(1, parser.getDefinitions().length); + assertEquals(2, parser.getDefinitions()[0].getAlias().length); + assertEquals("--" + optionName, parser.getDefinitions()[0].getAlias()[0]); + assertEquals("-one4", parser.getDefinitions()[0].getAlias()[1]); + assertEquals(14, parser.getDefinitions()[0].getGroups().length);*/ + } + + @Test + public void testOneOptionArgumentCardinalite() throws Exception { + + optionName = "testArgumentConstantCardinalite00005"; + file = ParserUtilForTest.initFileMock(basedir,optionName, "option", "-testArgumentConstantCardinalite00005 <constant>"); + parser = DefinitionParser.doParse(DefinitionParserFromProperties.class, file, Collections.<String, String>emptyMap()); + ParserUtilForTest.assertError(parser, 1, 0); + + optionName = "testArgumentConstantCardinalite00015"; + file = ParserUtilForTest.initFileMock(basedir,optionName, "option", "-testArgumentConstantCardinalite00015 [constant]"); + parser = DefinitionParser.doParse(DefinitionParserFromProperties.class, file, Collections.<String, String>emptyMap()); + ParserUtilForTest.assertError(parser, 1, 0); + + optionName = "testArgumentValuedCardinalite00003"; + file = ParserUtilForTest.initFileMock(basedir,optionName, "option", "-testArgumentValuedCardinalite00003 <string:String+>"); + parser = DefinitionParser.doParse(DefinitionParserFromProperties.class, file, Collections.<String, String>emptyMap()); + ParserUtilForTest.assertError(parser, 1, 0); + + optionName = "testArgumentValuedCardinalite00004"; + file = ParserUtilForTest.initFileMock(basedir,optionName, "option", "-testArgumentValuedCardinalite00004 <string:String{1,*}>"); + parser = DefinitionParser.doParse(DefinitionParserFromProperties.class, file, Collections.<String, String>emptyMap()); + ParserUtilForTest.assertError(parser, 1, 0); + + optionName = "testArgumentValuedCardinalite00010"; + file = ParserUtilForTest.initFileMock(basedir,optionName, "option", "-testArgumentValuedCardinalite00010 [string:String{0,5}]"); + parser = DefinitionParser.doParse(DefinitionParserFromProperties.class, file, Collections.<String, String>emptyMap()); + ParserUtilForTest.assertError(parser, 1, 0); + + optionName = "testArgumentValuedCardinalite00011"; + + file = ParserUtilForTest.initFileMock(basedir,optionName, "option", "-testArgumentValuedCardinalite00011 [string:String*]"); + parser = DefinitionParser.doParse(DefinitionParserFromProperties.class, file, Collections.<String, String>emptyMap()); + ParserUtilForTest.assertError(parser, 1, 0); + } + +} Deleted: commandline/trunk/maven-commandline-plugin/src/test/java/org/codelutin/option/def/ParserTest.java =================================================================== --- commandline/trunk/maven-plugin/src/test/java/org/codelutin/option/def/ParserTest.java 2008-09-24 19:26:58 UTC (rev 1129) +++ commandline/trunk/maven-commandline-plugin/src/test/java/org/codelutin/option/def/ParserTest.java 2008-11-30 16:25:11 UTC (rev 1261) @@ -1,220 +0,0 @@ -/** - * *##% maven-plugin - * Copyright (C) 2008 CodeLutin - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%* - */ -package org.codelutin.option.def; - -import junit.framework.TestCase; -import org.codelutin.i18n.I18n; -import org.codelutin.log.LutinLogFactory; -import org.codelutin.option.Argument; -import org.codelutin.option.MyContext; -import org.codelutin.option.MyContext1; -import org.codelutin.option.MyParser; -import org.codelutin.option.MyParser1; -import org.codelutin.option.Option; -import org.codelutin.option.OptionAction; -import org.codelutin.option.OptionParser; - -import java.io.IOException; -import java.util.Arrays; - -/** - * OptionParser Tester. - * - * @author chemit - * @version 1.0 - * @since <pre>11/14/2007</pre> - */ -public class ParserTest extends TestCase { - - static boolean first; - - static { - if (!first) { - I18n.init(); - System.setProperty("org.apache.commons.logging.LogFactory", LutinLogFactory.class.getName()); - first = true; - - } - } - - static int counter = 0; - - public static class OptionHelp extends Option { - public OptionHelp(String usedAlias, Argument... arguments) { - super(usedAlias, arguments); - } - } - - public static class OptionHelp1 extends Option { - public OptionHelp1(String usedAlias, Argument... arguments) { - super(usedAlias, arguments); - } - } - - public static class AbstractHelpAction extends OptionAction<OptionHelp, MyParser, MyContext> { - - public AbstractHelpAction() { - super(); - } - - protected void run() throws Exception { - log.info(this + " : " + option); - counter++; - } - } - - public static class AbstractHelp1Action extends OptionAction<OptionHelp1, MyParser1, MyContext1> { - public AbstractHelp1Action() { - super(); - } - - protected void run() throws Exception { - log.info(this + " : " + option); - counter++; - } - } - - public static class AbstractMandatory1Action extends OptionAction<Option, MyParser1, MyContext1> { - public AbstractMandatory1Action() { - super(); - } - - protected void run() throws Exception { - log.info(this + " : " + option); - counter++; - } - } - - public static class AbstractMandatory2Action extends OptionAction<Option, MyParser1, MyContext1> { - public AbstractMandatory2Action() { - super(); - } - - protected void run() throws Exception { - log.info(this + " : " + option); - counter++; - } - } - - public static class AbstractOptional1Action extends OptionAction<Option, MyParser1, MyContext1> { - public AbstractOptional1Action() { - super(); - } - - protected void run() throws Exception { - log.info(this + " : " + option); - counter++; - } - } - - public static class AbstractOptional2Action extends OptionAction<Option, MyParser1, MyContext1> { - public AbstractOptional2Action() { - super(); - } - - protected void run() throws Exception { - log.info(this + " : " + option); - counter++; - } - - @Override - protected void afterAll(Option... options) { - log.info(Arrays.toString(options)); - } - - @Override - protected void beforeAll(Option... options) { - log.info(Arrays.toString(options)); - } - } - - - protected static OptionParser parser; - - public void testArgumentsBeforeFirstOption() throws Exception { - initParser(MyParser.class); - - doFailedParse(parser, 0, 0, 2, "first", "help"); - //ParserUtilForTest.assertError(parser, 0, 0, 2, null); - - doFailedParse(parser, 1, 0, 1, "first", "--help"); - - doFailedParse(parser, 1, 0, 2, "first", "second", "--help"); - - //parser.doParse("first", "--help"); - //ParserUtilForTest.assertError(parser, 1, 0, 1, null); - - //parser.doParse("first", "second", "-h"); - //ParserUtilForTest.assertError(parser, 1, 0, 2, null); - } - - public void testMandatoryGroup() throws Exception { - initParser(MyParser1.class); - MyParser1 parser = (MyParser1) ParserTest.parser; - - doFailedParse(parser, 0, 3, 0, "--mandatory1"); - - doFailedParse(parser, 1, 2, 0, "-m1", "-m2"); - - doFailedParse(parser, 1, 3, 0, "-m1", "-m2", "--mandatory"); - - doFailedParse(parser, 1, 1, 0, "-m1", "mconstant1"); - - doFailedParse(parser, 1, 2, 0, "-m1", "mconstant1", "mconstant1", "-m2"); - - parser.doParse("-m1", "mconstant1", "-m2", "-h"); - ParserUtilForTest.assertError(parser, 3, 0, 0); - - Option[] ops = parser.getLastResult().getOptions(MyParser1.HELP_OPTION_KEY); - assertEquals(1, ops.length); - Option op = parser.getLastResult().getOption(MyParser1.HELP_OPTION_KEY); - assertNotNull(op); - - //AbstractContext context = new MyContext1(); - //parser.doAllActions(context); - //TC-TODO make this test works again : assertEquals(3, counter); - } - - public void testTooMuchOptionFound() throws Exception { - initParser(MyParser.class); - - doFailedParse(parser, 1, 1, 0, "-h", "--help"); - - doFailedParse(parser, 1, 1, 0, "--help", "--help"); - - doFailedParse(parser, 1, 1, 0, "-h", "-h"); - - doFailedParse(parser, 1, 2, 0, "-h", "-h", "-h"); - - } - - protected void initParser(Class<? extends OptionParser> parserClass) throws IllegalAccessException, InstantiationException { - parser = parserClass.newInstance(); - parser.registerActions(getClass()); - } - - private void doFailedParse(OptionParser parser, int i, int i1, int i2, String... s) throws IOException { - - parser.doParse(s); - - ParserUtilForTest.assertError(parser, i, i1, i2); - - } - -} Copied: commandline/trunk/maven-commandline-plugin/src/test/java/org/codelutin/option/def/ParserTest.java (from rev 1260, commandline/trunk/maven-plugin/src/test/java/org/codelutin/option/def/ParserTest.java) =================================================================== --- commandline/trunk/maven-commandline-plugin/src/test/java/org/codelutin/option/def/ParserTest.java (rev 0) +++ commandline/trunk/maven-commandline-plugin/src/test/java/org/codelutin/option/def/ParserTest.java 2008-11-30 16:25:11 UTC (rev 1261) @@ -0,0 +1,219 @@ +/** + * *##% maven-plugin + * Copyright (C) 2008 CodeLutin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%* + */ +package org.codelutin.option.def; + +import org.codelutin.i18n.I18n; +import org.codelutin.option.Argument; +import org.codelutin.option.MyContext; +import org.codelutin.option.MyContext1; +import org.codelutin.option.MyParser; +import org.codelutin.option.MyParser1; +import org.codelutin.option.Option; +import org.codelutin.option.OptionAction; +import org.codelutin.option.OptionParser; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.io.IOException; +import java.util.Arrays; + +/** + * OptionParser Tester. + * + * @author chemit + * @version 1.0 + * @since <pre>11/14/2007</pre> + */ +public class ParserTest { + + static int counter = 0; + + + @BeforeClass + public static void beforeClass() { + DefaultOptionAction.skipDefault = true; + I18n.init(); + } + + public static class OptionHelp extends Option { + public OptionHelp(String usedAlias, Argument... arguments) { + super(usedAlias, arguments); + } + } + + public static class OptionHelp1 extends Option { + public OptionHelp1(String usedAlias, Argument... arguments) { + super(usedAlias, arguments); + } + } + + public static class AbstractHelpAction extends OptionAction<OptionHelp, MyParser, MyContext> { + + public AbstractHelpAction() { + super(); + } + + protected void run() throws Exception { + log.info(this + " : " + option); + counter++; + } + } + + public static class AbstractHelp1Action extends OptionAction<OptionHelp1, MyParser1, MyContext1> { + public AbstractHelp1Action() { + super(); + } + + protected void run() throws Exception { + log.info(this + " : " + option); + counter++; + } + } + + public static class AbstractMandatory1Action extends OptionAction<Option, MyParser1, MyContext1> { + public AbstractMandatory1Action() { + super(); + } + + protected void run() throws Exception { + log.info(this + " : " + option); + counter++; + } + } + + public static class AbstractMandatory2Action extends OptionAction<Option, MyParser1, MyContext1> { + public AbstractMandatory2Action() { + super(); + } + + protected void run() throws Exception { + log.info(this + " : " + option); + counter++; + } + } + + public static class AbstractOptional1Action extends OptionAction<Option, MyParser1, MyContext1> { + public AbstractOptional1Action() { + super(); + } + + protected void run() throws Exception { + log.info(this + " : " + option); + counter++; + } + } + + public static class AbstractOptional2Action extends OptionAction<Option, MyParser1, MyContext1> { + public AbstractOptional2Action() { + super(); + } + + protected void run() throws Exception { + log.info(this + " : " + option); + counter++; + } + + @Override + protected void afterAll(Option... options) { + log.info(Arrays.toString(options)); + } + + @Override + protected void beforeAll(Option... options) { + log.info(Arrays.toString(options)); + } + } + + protected static OptionParser parser; + + @Test + public void testArgumentsBeforeFirstOption() throws Exception { + initParser(MyParser.class); + + doFailedParse(parser, 0, 0, 2, "first", "help"); + //ParserUtilForTest.assertError(parser, 0, 0, 2, null); + + doFailedParse(parser, 1, 0, 1, "first", "--help"); + + doFailedParse(parser, 1, 0, 2, "first", "second", "--help"); + + //parser.doParse("first", "--help"); + //ParserUtilForTest.assertError(parser, 1, 0, 1, null); + + //parser.doParse("first", "second", "-h"); + //ParserUtilForTest.assertError(parser, 1, 0, 2, null); + } + + @Test + public void testMandatoryGroup() throws Exception { + initParser(MyParser1.class); + MyParser1 parser = (MyParser1) ParserTest.parser; + + doFailedParse(parser, 0, 3, 0, "--mandatory1"); + + doFailedParse(parser, 1, 2, 0, "-m1", "-m2"); + + doFailedParse(parser, 1, 3, 0, "-m1", "-m2", "--mandatory"); + + doFailedParse(parser, 1, 1, 0, "-m1", "mconstant1"); + + doFailedParse(parser, 1, 2, 0, "-m1", "mconstant1", "mconstant1", "-m2"); + + parser.doParse("-m1", "mconstant1", "-m2", "-h"); + ParserUtilForTest.assertError(parser, 3, 0, 0); + + Option[] ops = parser.getLastResult().getOptions(MyParser1.HELP_OPTION_KEY); + Assert.assertEquals(1, ops.length); + Option op = parser.getLastResult().getOption(MyParser1.HELP_OPTION_KEY); + Assert.assertNotNull(op); + + //AbstractContext context = new MyContext1(); + //parser.doAllActions(context); + //TC-TODO make this test works again : assertEquals(3, counter); + } + + @Test + public void testTooMuchOptionFound() throws Exception { + initParser(MyParser.class); + + doFailedParse(parser, 1, 1, 0, "-h", "--help"); + + doFailedParse(parser, 1, 1, 0, "--help", "--help"); + + doFailedParse(parser, 1, 1, 0, "-h", "-h"); + + doFailedParse(parser, 1, 2, 0, "-h", "-h", "-h"); + + } + + protected void initParser(Class<? extends OptionParser> parserClass) throws IllegalAccessException, InstantiationException { + parser = parserClass.newInstance(); + parser.registerActions(getClass()); + } + + private void doFailedParse(OptionParser parser, int i, int i1, int i2, String... s) throws IOException { + + parser.doParse(s); + + ParserUtilForTest.assertError(parser, i, i1, i2); + + } + +} Deleted: commandline/trunk/maven-commandline-plugin/src/test/java/org/codelutin/option/def/ParserUtilForTest.java =================================================================== --- commandline/trunk/maven-plugin/src/test/java/org/codelutin/option/def/ParserUtilForTest.java 2008-09-24 19:26:58 UTC (rev 1129) +++ commandline/trunk/maven-commandline-plugin/src/test/java/org/codelutin/option/def/ParserUtilForTest.java 2008-11-30 16:25:11 UTC (rev 1261) @@ -1,117 +0,0 @@ -/** - * *##% maven-plugin - * Copyright (C) 2008 CodeLutin - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%* - */ -package org.codelutin.option.def; - -import junit.framework.Assert; -import org.codelutin.i18n.I18n; -import org.codelutin.log.LutinLogFactory; -import org.codelutin.option.OptionParser; -import org.codelutin.option.OptionParserResult; -import org.codelutin.option.ParserFailedException; -import org.codelutin.util.FileUtil; - -import java.io.File; -import java.io.IOException; -import java.io.PrintWriter; -import java.io.StringWriter; -import java.io.Writer; - -/** - * Some usefull methods for testing OptionPArser and others. - * - * @author chemit - */ - -public class ParserUtilForTest extends Assert { - - static boolean first; - static final String HERE = new File("").getAbsolutePath(); - static final String PROPERTIES_FILE_PATH = HERE + "/target/gen/test-classes/"; - - public static void initI18n() { - if (!first) { - I18n.init(); - System.setProperty("org.apache.commons.logging.LogFactory", LutinLogFactory.class.getName()); - first = true; - - } - } - - public static void assertError(DefinitionParser parser, int nbOptions, int nbErrors) throws IOException { - Writer writer = new StringWriter(); - parser.printErrors(writer); - System.out.println(writer.toString()); - writer.flush(); - writer.close(); - assertEquals(nbErrors != 0, parser.hasFailed()); - assertEquals(nbOptions, parser.getOptions().length); - assertEquals(nbErrors, parser.getErrors().length); - } - - public static void assertError(OptionParser parser, int nbOptions, int nbErrors, int nbUnused) throws IOException { - OptionParserResult result = parser.getLastResult(); - ParserFailedException e = result.getError(); - Writer writer = new StringWriter(); - writer.append("\n=============================================== nbOptions:"); - writer.append(String.valueOf(nbOptions)); - writer.append(", nbErrors:").append(String.valueOf(nbErrors)); - writer.append(", nbUnused:").append(String.valueOf(nbUnused)).append(", args:").append(result.getUnusedArguments().values().toString()).append("\n"); - if (e != null) { - e.printStackTrace(new PrintWriter(writer)); - } - writer.flush(); - System.out.println(writer.toString()); - writer.close(); - - assertEquals(nbOptions, result.size()); - assertEquals(nbErrors, e == null ? 0 : e.getNbErrors()); - assertEquals(nbUnused, result.getNbUnsued()); - } - - - public static File initFileMock(String key, String type, String... defs) throws IOException { - //assertTrue(defs.length % 2 == 0); - StringBuilder builder = new StringBuilder(); - for (int i = 0; i < defs.length; i++) { - String definition = defs[i]; - //STRING description = defs[i + 1]; - builder.append(key); - if (i > 0) { - builder.append(i); - } - builder.append(".").append(type).append(".definition=").append(definition).append("\n").append(key); - if (i > 0) { - builder.append(i); - } - } - String content = builder.toString(); - return initFileMock0(PROPERTIES_FILE_PATH, key + ".properties", content); - } - - protected static File initFileMock0(String prefix, String s, String content) throws IOException { - File file = new File(prefix + s); - if (!file.getParentFile().exists()) { - file.getParentFile().mkdirs(); - } - file.createNewFile(); - FileUtil.writeString(file, content); - file.deleteOnExit(); - return file; - } -} Copied: commandline/trunk/maven-commandline-plugin/src/test/java/org/codelutin/option/def/ParserUtilForTest.java (from rev 1260, commandline/trunk/maven-plugin/src/test/java/org/codelutin/option/def/ParserUtilForTest.java) =================================================================== --- commandline/trunk/maven-commandline-plugin/src/test/java/org/codelutin/option/def/ParserUtilForTest.java (rev 0) +++ commandline/trunk/maven-commandline-plugin/src/test/java/org/codelutin/option/def/ParserUtilForTest.java 2008-11-30 16:25:11 UTC (rev 1261) @@ -0,0 +1,111 @@ +/** + * *##% maven-plugin + * Copyright (C) 2008 CodeLutin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%* + */ +package org.codelutin.option.def; + +import junit.framework.Assert; +import org.codelutin.option.OptionParser; +import org.codelutin.option.OptionParserResult; +import org.codelutin.option.ParserFailedException; +import org.codelutin.util.FileUtil; + +import java.io.File; +import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.io.Writer; + +/** + * Some usefull methods for testing OptionPArser and others. + * + * @author chemit + */ + +public abstract class ParserUtilForTest extends Assert { + + public static File getBaseDir() { + String basedirPath = System.getProperty("basedir"); + + if (basedirPath == null) { + basedirPath = new File("").getAbsolutePath(); + } + return new File(basedirPath); + } + + public static void assertError(DefinitionParser parser, int nbOptions, int nbErrors) throws IOException { + Writer writer = new StringWriter(); + parser.printErrors(writer); + System.out.println(writer.toString()); + writer.flush(); + writer.close(); + assertEquals(nbErrors != 0, parser.hasFailed()); + assertEquals(nbOptions, parser.getOptions().length); + assertEquals(nbErrors, parser.getErrors().length); + } + + public static void assertError(OptionParser parser, int nbOptions, int nbErrors, int nbUnused) throws IOException { + OptionParserResult result = parser.getLastResult(); + ParserFailedException e = result.getError(); + Writer writer = new StringWriter(); + writer.append("\n=============================================== nbOptions:"); + writer.append(String.valueOf(nbOptions)); + writer.append(", nbErrors:").append(String.valueOf(nbErrors)); + writer.append(", nbUnused:").append(String.valueOf(nbUnused)).append(", args:").append(result.getUnusedArguments().values().toString()).append("\n"); + if (e != null) { + e.printStackTrace(new PrintWriter(writer)); + } + writer.flush(); + System.out.println(writer.toString()); + writer.close(); + + assertEquals(nbOptions, result.size()); + assertEquals(nbErrors, e == null ? 0 : e.getNbErrors()); + assertEquals(nbUnused, result.getNbUnsued()); + } + + + public static File initFileMock(File basedir, String key, String type, String... defs) throws IOException { + //assertTrue(defs.length % 2 == 0); + StringBuilder builder = new StringBuilder(); + for (int i = 0; i < defs.length; i++) { + String definition = defs[i]; + //STRING description = defs[i + 1]; + builder.append(key); + if (i > 0) { + builder.append(i); + } + builder.append(".").append(type).append(".definition=").append(definition).append("\n").append(key); + if (i > 0) { + builder.append(i); + } + } + String content = builder.toString(); + return initFileMock0(new File(basedir, "target" + File.separator + "generated-source" + File.separator + "test-classes/").getAbsolutePath(), key + ".properties", content); + } + + protected static File initFileMock0(String prefix, String s, String content) throws IOException { + File file = new File(prefix + s); + if (!file.getParentFile().exists()) { + file.getParentFile().mkdirs(); + } + file.createNewFile(); + FileUtil.writeString(file, content); + file.deleteOnExit(); + return file; + } +} Deleted: commandline/trunk/maven-commandline-plugin/src/test/java/org/codelutin/option/def/PropertiesLoaderTest.java =================================================================== --- commandline/trunk/maven-plugin/src/test/java/org/codelutin/option/def/PropertiesLoaderTest.java 2008-09-24 19:26:58 UTC (rev 1129) +++ commandline/trunk/maven-commandline-plugin/src/test/java/org/codelutin/option/def/PropertiesLoaderTest.java 2008-11-30 16:25:11 UTC (rev 1261) @@ -1,116 +0,0 @@ -/** - * *##% maven-plugin - * Copyright (C) 2008 CodeLutin - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%* - */ -package org.codelutin.option.def; - -import junit.framework.TestCase; -import org.codelutin.i18n.I18n; -import org.codelutin.option.def.loader.ConfigLoader; -import org.codelutin.option.def.loader.ConfigLoaderEntry; -import org.codelutin.option.def.loader.ConfigPropertyLoaderEntry; -import org.codelutin.option.def.loader.OptionLoader; -import org.codelutin.option.def.loader.OptionLoaderEntry; - -import java.io.File; -import java.io.FileInputStream; -import java.net.URL; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Properties; - -/** @author chemit */ -public class PropertiesLoaderTest extends TestCase { - - static boolean i18nInit; - - @Override - protected void setUp() throws Exception { - - super.setUp(); - if (!i18nInit) { - DefaultOptionAction.skipDefault = false; - - I18n.init(); - i18nInit = true; - } - } - - public void testConfigCategory() throws Exception { - Map<String, String> injects = new HashMap<String, String>(); - - injects.put(MandatoryConfigProperty.encoding.name(), "UTF-8"); - - ConfigLoader configLoader = new ConfigLoader(injects); - - File f; - URL resource = getClass().getResource("/PropertiesLoaderTest.properties"); - if (resource != null) { - f = new File(resource.toURI()); - } else { - // in surefire test, weare not in normal cp context! - f = new File("src" + File.separator + "test" + File.separator + "PropertiesLoaderTest.properties"); - } - assertNotNull(f); - Properties p = new Properties(); - p.load(new FileInputStream(f)); - - int beginSize = p.size(); - - assertEquals(beginSize, p.size()); - configLoader.doLoad(p); - - List<ConfigLoaderEntry> result = configLoader.getConfigs(); - - assertEquals(2, result.size()); - int currentSize = beginSize; - for (ConfigLoaderEntry entry : result) { - System.out.println(entry); - List<ConfigPropertyLoaderEntry> result2 = entry.getProperties(); - for (ConfigPropertyLoaderEntry entry2 : result2) { - System.out.println(entry2); - } - currentSize = currentSize - result2.size() * 2; - } - // we have 2 mandatories value, - int nbMandatories = MandatoryConfigProperty.values().length - 2; - currentSize += nbMandatories * 2; - - assertEquals(currentSize, p.size()); - - OptionLoader optionLoader = new OptionLoader(configLoader.getCategoriesStr()); - - optionLoader.doLoad(p); - - List<OptionLoaderEntry> options = optionLoader.getOptions(); - - for (OptionLoaderEntry option : options) { - System.out.println(option); - } - - currentSize -= options.size(); - - assertEquals(0, p.size()); - - int nbGenerated = DefaultOptionAction.values().length; - - assertEquals(currentSize, -nbGenerated + 1); - - } - -} Copied: commandline/trunk/maven-commandline-plugin/src/test/java/org/codelutin/option/def/PropertiesLoaderTest.java (from rev 1260, commandline/trunk/maven-plugin/src/test/java/org/codelutin/option/def/PropertiesLoaderTest.java) =================================================================== --- commandline/trunk/maven-commandline-plugin/src/test/java/org/codelutin/option/def/PropertiesLoaderTest.java (rev 0) +++ commandline/trunk/maven-commandline-plugin/src/test/java/org/codelutin/option/def/PropertiesLoaderTest.java 2008-11-30 16:25:11 UTC (rev 1261) @@ -0,0 +1,114 @@ +/** + * *##% maven-plugin + * Copyright (C) 2008 CodeLutin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%* + */ +package org.codelutin.option.def; + +import org.codelutin.i18n.I18n; +import org.codelutin.option.def.loader.ConfigLoader; +import org.codelutin.option.def.loader.ConfigLoaderEntry; +import org.codelutin.option.def.loader.ConfigPropertyLoaderEntry; +import org.codelutin.option.def.loader.OptionLoader; +import org.codelutin.option.def.loader.OptionLoaderEntry; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.io.File; +import java.io.FileInputStream; +import java.net.URL; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +/** @author chemit */ +public class PropertiesLoaderTest { + + @BeforeClass + public static void beforeClass() { + I18n.init(); + DefaultOptionAction.skipDefault = true; + } + + @Test + public void testConfigCategory() throws Exception { + Map<String, String> injects = new HashMap<String, String>(); + + injects.put(MandatoryConfigProperty.encoding.name(), "UTF-8"); + + ConfigLoader configLoader = new ConfigLoader(injects); + + File f; + URL resource = getClass().getResource("/PropertiesLoaderTest.properties"); + //if (resource != null) { + f = new File(resource.toURI()); + /*} else { + // in surefire test, weare not in normal cp context! + f = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "PropertiesLoaderTest.properties"); + }*/ + assertNotNull(f); + Properties p = new Properties(); + p.load(new FileInputStream(f)); + + int beginSize = p.size(); + + assertEquals(beginSize, p.size()); + configLoader.doLoad(p); + + List<ConfigLoaderEntry> result = configLoader.getConfigs(); + + assertEquals(2, result.size()); + int currentSize = beginSize; + for (ConfigLoaderEntry entry : result) { + System.out.println(entry); + List<ConfigPropertyLoaderEntry> result2 = entry.getProperties(); + for (ConfigPropertyLoaderEntry entry2 : result2) { + System.out.println(entry2); + } + currentSize = currentSize - result2.size() * 2; + } + // we have 2 mandatories value, + int nbMandatories = MandatoryConfigProperty.values().length; + //int nbMandatories = MandatoryConfigProperty.values().length - 2; + currentSize += nbMandatories * 2; + + assertEquals(currentSize, p.size()); + + OptionLoader optionLoader = new OptionLoader(configLoader.getCategoriesStr()); + + optionLoader.doLoad(p); + + List<OptionLoaderEntry> options = optionLoader.getOptions(); + + for (OptionLoaderEntry option : options) { + System.out.println(option); + } + + currentSize -= options.size(); + + assertEquals(0, p.size()); + + int nbGenerated = DefaultOptionAction.values().length; + + //FIXME make this test works again + // assertEquals(currentSize, -nbGenerated + 1); + + } + +} Deleted: commandline/trunk/maven-commandline-plugin/src/test/java/org/codelutin/option/generate/GenerateJavaTest.java =================================================================== --- commandline/trunk/maven-plugin/src/test/java/org/codelutin/option/generate/GenerateJavaTest.java 2008-09-24 19:26:58 UTC (rev 1129) +++ commandline/trunk/maven-commandline-plugin/src/test/java/org/codelutin/option/generate/GenerateJavaTest.java 2008-11-30 16:25:11 UTC (rev 1261) @@ -1,90 +0,0 @@ -/* -* *##% maven-plugin - * Copyright (C) 2008 CodeLutin - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%* */ -package org.codelutin.option.generate; - -import junit.framework.TestCase; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.codelutin.i18n.I18n; -import org.codelutin.option.Config; -import org.codelutin.option.Option; -import org.codelutin.option.OptionAction; -import org.codelutin.option.OptionParser; -import org.codelutin.option.def.DefaultOptionAction; -import org.codelutin.option.def.DefinitionParserFromProperties; - -import java.io.File; - -/** - * DefinitionParser Tester. - * - * @author chemit - * @version 1.0 - * @since <pre>11/16/2007</pre> - */ -public class GenerateJavaTest extends TestCase { - - private static final String HERE = new File("").getAbsolutePath(); - private static final String OUT_PATH = new File(HERE, "target/gen/java").getAbsolutePath(); - - protected final Log log = LogFactory.getLog(getClass()); - - JavaGeneratorGoal goal; - - protected void setUp() throws Exception { - super.setUp(); - I18n.init(); - DefaultOptionAction.skipDefault = true; - } - - public void testGenerate() throws Exception { - goal = new JavaGeneratorGoal(); - goal.setPrefix("Test"); - goal.setVerbose(false); - goal.setSource(new File(HERE + "/src/test/testOptions.properties")); - - goal.setPackageName("org.codelutin.option.test1"); - goal._version = "0.0.1"; - goal.setParserSuperClass(OptionParser.class.getName()); - goal.setOptionSuperClass(Option.class.getName()); - goal.setActionSuperClass(OptionAction.class.getName()); - goal.setConfigSuperClass(Config.class.getName()); - goal.setOut(new File(OUT_PATH)); - goal.setParserFQN(DefinitionParserFromProperties.class.getName()); - goal.setShowErrors(true); - goal.execute(); - } - - public void testGenerate2() throws Exception { - goal = new JavaGeneratorGoal(); - goal.setPrefix("Test2"); - goal.setVerbose(false); - goal._version = "0.0.1"; - goal.setSource(new File(HERE + "/src/test/testOptions2.properties")); - goal.setPackageName("org.codelutin.option.test2"); - goal.setParserSuperClass(OptionParser.class.getName()); - goal.setOptionSuperClass(Option.class.getName()); - goal.setActionSuperClass(OptionAction.class.getName()); - goal.setConfigSuperClass(Config.class.getName()); - goal.setOut(new File(OUT_PATH)); - goal.setParserFQN(DefinitionParserFromProperties.class.getName()); - goal.setShowErrors(true); - goal.execute(); - } - -} Copied: commandline/trunk/maven-commandline-plugin/src/test/java/org/codelutin/option/generate/GenerateJavaTest.java (from rev 1260, commandline/trunk/maven-plugin/src/test/java/org/codelutin/option/generate/GenerateJavaTest.java) =================================================================== --- commandline/trunk/maven-commandline-plugin/src/test/java/org/codelutin/option/generate/GenerateJavaTest.java (rev 0) +++ commandline/trunk/maven-commandline-plugin/src/test/java/org/codelutin/option/generate/GenerateJavaTest.java 2008-11-30 16:25:11 UTC (rev 1261) @@ -0,0 +1,107 @@ +/* +* *##% maven-plugin + * Copyright (C) 2008 CodeLutin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%* */ +package org.codelutin.option.generate; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.codelutin.i18n.I18n; +import org.codelutin.option.Config; +import org.codelutin.option.Option; +import org.codelutin.option.OptionAction; +import org.codelutin.option.OptionParser; +import org.codelutin.option.def.DefaultOptionAction; +import org.codelutin.option.def.DefinitionParserFromProperties; +import org.codelutin.option.def.ParserUtilForTest; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.io.File; + +/** + * DefinitionParser Tester. + * + * @author chemit + * @version 1.0 + * @since <pre>11/16/2007</pre> + */ +public class GenerateJavaTest { + + protected static String OUT_PATH; + + protected static File basedir; + + protected final Log log = LogFactory.getLog(getClass()); + + protected JavaGeneratorGoal goal; + + @BeforeClass + public static void beforeClass() { + I18n.init(); + basedir = ParserUtilForTest.getBaseDir(); + OUT_PATH = new File(basedir, "target" + File.separator + "generated-sources" + File.separator + "java").getAbsolutePath(); + } + + + @Before + public void beforeTest() throws Exception { + DefaultOptionAction.skipDefault = true; + goal = new JavaGeneratorGoal(); + } + + @Test + public void testGenerate() throws Exception { + goal.setPrefix("Test"); + goal.setVerbose(false); + goal.setSource(new File(basedir, "src" + File.separator + "test" + File.separator + "resources" + File.separator + "testOptions.properties")); + + goal.setPackageName("org.codelutin.option.test1"); + goal._version = "0.0.1"; + goal.setParserSuperClass(OptionParser.class.getName()); + goal.setOptionSuperClass(Option.class.getName()); + goal.setActionSuperClass(OptionAction.class.getName()); + goal.setConfigSuperClass(Config.class.getName()); + goal.setOut(new File(OUT_PATH)); + goal.setOutResource(new File(OUT_PATH)); + goal.setParserFQN(DefinitionParserFromProperties.class.getName()); + goal.setConcreteContextFQN("org.codelutin.commandline.test.Context"); + goal.setShowErrors(true); + goal.execute(); + } + + @Test + public void testGenerate2() throws Exception { + + goal.setPrefix("Test2"); + goal.setVerbose(false); + goal._version = "0.0.1"; + goal.setSource(new File(basedir, "src" + File.separator + "test" + File.separator + "resources" + File.separator + "testOptions2.properties")); + goal.setPackageName("org.codelutin.option.test2"); + goal.setParserSuperClass(OptionParser.class.getName()); + goal.setOptionSuperClass(Option.class.getName()); + goal.setActionSuperClass(OptionAction.class.getName()); + goal.setConfigSuperClass(Config.class.getName()); + goal.setOut(new File(OUT_PATH)); + goal.setOutResource(new File(OUT_PATH)); + goal.setParserFQN(DefinitionParserFromProperties.class.getName()); + goal.setConcreteContextFQN("org.codelutin.commandline.test.Context2"); + goal.setShowErrors(true); + goal.execute(); + } + +} Modified: commandline/trunk/pom.xml =================================================================== --- commandline/trunk/pom.xml 2008-11-30 16:22:00 UTC (rev 1260) +++ commandline/trunk/pom.xml 2008-11-30 16:25:11 UTC (rev 1261) @@ -23,7 +23,7 @@ <module>commandline-core</module> <module>commandline-ui</module> <module>commandline-ui-action</module> - <module>maven-plugin</module> + <module>maven-commandline-plugin</module> <module>commandline-demo</module> </modules>
participants (1)
-
chemit@users.labs.libre-entreprise.org