Index: maven-commandline-plugin/src/test/org/codelutin/option/def/DefinitionParserTest.java diff -u /dev/null maven-commandline-plugin/src/test/org/codelutin/option/def/DefinitionParserTest.java:1.1 --- /dev/null Sun Mar 23 00:23:06 2008 +++ maven-commandline-plugin/src/test/org/codelutin/option/def/DefinitionParserTest.java Sun Mar 23 00:23:00 2008 @@ -0,0 +1,158 @@ +/** + * ##% Copyright (C) 2002, 2007 Code Lutin This program is free software; you + * can redistribute it and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation; either version 2 + * 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 Public License for more details. You + * should have received a copy of the GNU General Public License along with this + * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place + * - Suite 330, Boston, MA 02111-1307, USA. ##%* + */ +package org.codelutin.option.def; + +import junit.framework.TestCase; +import org.codelutin.i18n.I18n; +import org.codelutin.option.def.ParserUtilForTest; +import org.codelutin.option.def.DefinitionParserFromProperties; + +import java.io.File; + +/** + * DefinitionParser Tester. + * + * @author chemit + * @version 1.0 + * @since
11/18/2007
+ */ +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); + 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 ", "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 ", "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 ", "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 ", "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 "); + parser = DefinitionParser.doParse(DefinitionParserFromProperties.class, file); + ParserUtilForTest.assertError(parser, 1, 0); + + optionName = "testArgumentConstantCardinalite00015"; + file = ParserUtilForTest.initFileMock(optionName, "option", "-testArgumentConstantCardinalite00015 [constant]"); + parser = DefinitionParser.doParse(DefinitionParserFromProperties.class, file); + ParserUtilForTest.assertError(parser, 1, 0); + + optionName = "testArgumentValuedCardinalite00003"; + file = ParserUtilForTest.initFileMock(optionName, "option", "-testArgumentValuedCardinalite00003 "); + parser = DefinitionParser.doParse(DefinitionParserFromProperties.class, file); + ParserUtilForTest.assertError(parser, 1, 0); + + optionName = "testArgumentValuedCardinalite00004"; + file = ParserUtilForTest.initFileMock(optionName, "option", "-testArgumentValuedCardinalite00004 "); + parser = DefinitionParser.doParse(DefinitionParserFromProperties.class, file); + ParserUtilForTest.assertError(parser, 1, 0); + + optionName = "testArgumentValuedCardinalite00010"; + file = ParserUtilForTest.initFileMock(optionName, "option", "-testArgumentValuedCardinalite00010 [string:String{0,5}]"); + parser = DefinitionParser.doParse(DefinitionParserFromProperties.class, file); + ParserUtilForTest.assertError(parser, 1, 0); + + optionName = "testArgumentValuedCardinalite00011"; + + file = ParserUtilForTest.initFileMock(optionName, "option", "-testArgumentValuedCardinalite00011 [string:String*]"); + parser = DefinitionParser.doParse(DefinitionParserFromProperties.class, file); + ParserUtilForTest.assertError(parser, 1, 0); + } + +} Index: maven-commandline-plugin/src/test/org/codelutin/option/def/ParserUtilForTest.java diff -u /dev/null maven-commandline-plugin/src/test/org/codelutin/option/def/ParserUtilForTest.java:1.1 --- /dev/null Sun Mar 23 00:23:06 2008 +++ maven-commandline-plugin/src/test/org/codelutin/option/def/ParserUtilForTest.java Sun Mar 23 00:23:00 2008 @@ -0,0 +1,112 @@ +/** + * ##% Copyright (C) 2002, 2007 Code Lutin This program is free software; you + * can redistribute it and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation; either version 2 + * 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 Public License for more details. You + * should have received a copy of the GNU General Public License along with this + * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place + * - Suite 330, Boston, MA 02111-1307, USA. ##%* + */ +package org.codelutin.option.def; + +import junit.framework.Assert; +import org.codelutin.i18n.I18n; +import org.codelutin.log.LutinLogFactory; +import org.codelutin.option.def.DefinitionParser; +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; + } +} Index: maven-commandline-plugin/src/test/org/codelutin/option/def/DefinitionParserBadOptionTest.java diff -u /dev/null maven-commandline-plugin/src/test/org/codelutin/option/def/DefinitionParserBadOptionTest.java:1.1 --- /dev/null Sun Mar 23 00:23:06 2008 +++ maven-commandline-plugin/src/test/org/codelutin/option/def/DefinitionParserBadOptionTest.java Sun Mar 23 00:23:00 2008 @@ -0,0 +1,202 @@ +/** + * ##% Copyright (C) 2002, 2007 Code Lutin This program is free software; you + * can redistribute it and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation; either version 2 + * 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 Public License for more details. You + * should have received a copy of the GNU General Public License along with this + * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place + * - Suite 330, Boston, MA 02111-1307, USA. ##%* + */ +package org.codelutin.option.def; + +import junit.framework.TestCase; +import org.codelutin.option.def.ParserUtilForTest; +import org.codelutin.option.def.DefinitionParserFromProperties; + +import java.io.File; +import java.io.IOException; + +/** + * DefinitionParser Tester. + * + * @author chemit + * @version 1.0 + * @since
11/18/2007
+ */ +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 "); + + ParserUtilForTest.assertError(parser, 0, 1); + + key = "testBadArgumentConstantCardinalite00"; + initAndParse("-testBadArgumentConstantCardinalite00 "); + + ParserUtilForTest.assertError(parser, 1, 0); + + key = "testBadArgumentConstantCardinalite000"; + initAndParse("-testBadArgumentConstantCardinalite000 "); + + ParserUtilForTest.assertError(parser, 1, 0); + + key = "testBadArgumentConstantCardinalite0000"; + initAndParse("-testBadArgumentConstantCardinalite0000 "); + + ParserUtilForTest.assertError(parser, 0, 1); + + key = "testBadArgumentConstantCardinalite00000"; + initAndParse("-testBadArgumentConstantCardinalite00000 "); + + ParserUtilForTest.assertError(parser, 0, 1); + + key = "testBadArgumentConstantCardinalite00001"; + initAndParse("-testBadArgumentConstantCardinalite00001 "); + + ParserUtilForTest.assertError(parser, 0, 1); + + key = "testBadArgumentConstantCardinalite00002"; + initAndParse("-testBadArgumentConstantCardinalite00002 "); + + ParserUtilForTest.assertError(parser, 0, 1); + + key = "testBadArgumentConstantCardinalite00003"; + initAndParse("-testBadArgumentConstantCardinalite00003 "); + + ParserUtilForTest.assertError(parser, 0, 1); + + key = "testBadArgumentConstantCardinalite00004"; + initAndParse("-testBadArgumentConstantCardinalite00004 "); + + 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 "); + + ParserUtilForTest.assertError(parser, 0, 1); + + key = "testBadArgumentValuedCardinalite00"; + initAndParse("-testBadArgumentValuedCardinalite00 "); + + ParserUtilForTest.assertError(parser, 0, 1); + + key = "testBadArgumentValuedCardinalite000"; + initAndParse("-testBadArgumentValuedCardinalit000 "); + + ParserUtilForTest.assertError(parser, 0, 1); + + key = "testBadArgumentValuedCardinalite0000"; + initAndParse("-testBadArgumentValuedCardinalite0000 "); + + ParserUtilForTest.assertError(parser, 0, 1); + + key = "testBadArgumentValuedCardinalite00000"; + initAndParse("-testBadArgumentValuedCardinalite00000 "); + + ParserUtilForTest.assertError(parser, 0, 1); + + key = "testBadArgumentValuedCardinalite00001"; + initAndParse("-testBadArgumentValuedCardinalite00001 "); + + ParserUtilForTest.assertError(parser, 0, 1); + + key = "testBadArgumentValuedCardinalite00002"; + initAndParse("-testBadArgumentValuedCardinalite00002 "); + 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 IOException, InstantiationException, IllegalAccessException { + key = getName() + System.currentTimeMillis(); + file = ParserUtilForTest.initFileMock(key, "option", defs); + parser = DefinitionParser.doParse(DefinitionParserFromProperties.class, file); + } +} \ No newline at end of file Index: maven-commandline-plugin/src/test/org/codelutin/option/def/ParserTest.java diff -u /dev/null maven-commandline-plugin/src/test/org/codelutin/option/def/ParserTest.java:1.1 --- /dev/null Sun Mar 23 00:23:06 2008 +++ maven-commandline-plugin/src/test/org/codelutin/option/def/ParserTest.java Sun Mar 23 00:23:01 2008 @@ -0,0 +1,203 @@ +package org.codelutin.option.def; + +import junit.framework.TestCase; + +import java.io.IOException; +import java.util.Arrays; + +import org.codelutin.i18n.I18n; +import org.codelutin.log.LutinLogFactory; +import org.codelutin.option.Option; +import org.codelutin.option.Argument; +import org.codelutin.option.OptionAction; +import org.codelutin.option.OptionParser; +import org.codelutin.option.MyContext; +import org.codelutin.option.MyContext1; +import org.codelutin.option.MyParser; +import org.codelutin.option.MyParser1; + +/** + * OptionParser Tester. + * + * @author chemit + * @version 1.0 + * @since
11/14/2007
+ */ +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 { + + public AbstractHelpAction() { + super(); + } + + protected void run() throws Exception { + log.info(this + " : " + option); + counter++; + } + } + + public static class AbstractHelp1Action extends OptionAction { + public AbstractHelp1Action() { + super(); + } + + protected void run() throws Exception { + log.info(this + " : " + option); + counter++; + } + } + + public static class AbstractMandatory1Action extends OptionAction { + public AbstractMandatory1Action() { + super(); + } + + protected void run() throws Exception { + log.info(this + " : " + option); + counter++; + } + } + + public static class AbstractMandatory2Action extends OptionAction { + public AbstractMandatory2Action() { + super(); + } + + protected void run() throws Exception { + log.info(this + " : " + option); + counter++; + } + } + + public static class AbstractOptional1Action extends OptionAction { + public AbstractOptional1Action() { + super(); + } + + protected void run() throws Exception { + log.info(this + " : " + option); + counter++; + } + } + + public static class AbstractOptional2Action extends OptionAction { + 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 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); + + } + +} Index: maven-commandline-plugin/src/test/org/codelutin/option/def/PropertiesLoaderTest.java diff -u /dev/null maven-commandline-plugin/src/test/org/codelutin/option/def/PropertiesLoaderTest.java:1.1 --- /dev/null Sun Mar 23 00:23:06 2008 +++ maven-commandline-plugin/src/test/org/codelutin/option/def/PropertiesLoaderTest.java Sun Mar 23 00:23:01 2008 @@ -0,0 +1,102 @@ +/** + * # #% Copyright (C) 2008 Code Lutin, Tony Chemit + * This program is free software; you + * can redistribute it and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation; either version 2 + * 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 Public License for more details. You + * should have received a copy of the GNU General Public License along with this + * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place + * - Suite 330, Boston, MA 02111-1307, USA. + * # #% + */ +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.List; +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 { + ConfigLoader configLoader = new ConfigLoader(); + + 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 result = configLoader.getConfigs(); + + assertEquals(2, result.size()); + int currentSize = beginSize; + for (ConfigLoaderEntry entry : result) { + System.out.println(entry); + List result2 = entry.getProperties(); + for (ConfigPropertyLoaderEntry entry2 : result2) { + System.out.println(entry2); + } + currentSize = currentSize - result2.size() * 2; + } + assertEquals(currentSize, p.size()); + + OptionLoader optionLoader = new OptionLoader(configLoader.getCategoriesStr()); + + optionLoader.doLoad(p); + + List 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); + + } + +}