Index: lutinutil/src/test/org/codelutin/option/ParserUtilForTest.java
diff -u /dev/null lutinutil/src/test/org/codelutin/option/ParserUtilForTest.java:1.1
--- /dev/null Sun Dec 30 22:51:23 2007
+++ lutinutil/src/test/org/codelutin/option/ParserUtilForTest.java Sun Dec 30 22:51:18 2007
@@ -0,0 +1,105 @@
+/**
+ * ##% 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;
+
+import junit.framework.Assert;
+import org.codelutin.i18n.I18n;
+import org.codelutin.log.LutinLogFactory;
+import org.codelutin.option.def.DefinitionParser;
+import org.codelutin.util.FileUtil;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.util.Arrays;
+
+/**
+ * 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.init2(System.getProperty("user.language", "fr"), System.getProperty("user.country", "FR"), "ISO-8859-1");
+ 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 {
+ 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(Arrays.toString(parser.getArguments())).append("\n");
+ parser.printErrors(writer);
+ System.out.println(writer.toString());
+ writer.flush();
+ writer.close();
+ assertEquals(nbErrors != 0, parser.hasFailed());
+ assertEquals(nbOptions, parser.getNbOptions());
+ assertEquals(nbUnused, parser.getUnusedArguments().length);
+ assertEquals(nbErrors, parser.getErrors().length);
+ }
+
+
+ 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: lutinutil/src/test/org/codelutin/option/ParserTest.java
diff -u /dev/null lutinutil/src/test/org/codelutin/option/ParserTest.java:1.1
--- /dev/null Sun Dec 30 22:51:23 2007
+++ lutinutil/src/test/org/codelutin/option/ParserTest.java Sun Dec 30 22:51:18 2007
@@ -0,0 +1,256 @@
+package org.codelutin.option;
+
+import junit.framework.TestCase;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import static org.codelutin.option.def.ArgumentType.constant;
+import static org.codelutin.option.def.ArgumentValueType.STRING;
+import org.codelutin.option.def.OptionDefinition;
+import org.codelutin.option.def.OptionDefinitionBuilder;
+
+import java.io.IOException;
+import java.io.StringWriter;
+import java.util.Arrays;
+
+/**
+ * OptionParser Tester.
+ *
+ * @author chemit
+ * @version 1.0
+ * @since
11/14/2007
+ */
+public class ParserTest extends TestCase {
+
+ static private Log log = LogFactory.getLog(ParserTest.class);
+
+ static {
+ ParserUtilForTest.initI18n();
+ }
+
+ 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(MyParser parser) {
+ super(parser);
+ }
+
+ protected void run() throws Exception {
+ log.info(this + " : " + option);
+ counter++;
+ }
+ }
+
+ public static class AbstractHelp1Action extends OptionAction {
+ public AbstractHelp1Action(MyParser1 parser) {
+ super(parser);
+ }
+
+ protected void run() throws Exception {
+ log.info(this + " : " + option);
+ counter++;
+ }
+ }
+
+ public static class AbstractMandatory1Action extends OptionAction