You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@oodt.apache.org by bf...@apache.org on 2011/10/14 18:36:35 UTC

svn commit: r1183415 - in /oodt/branches/cas-cl/src/test/org/apache/oodt/cas/cl: option/util/TestArgs.java parser/ parser/TestStdCmdLineOptionParser.java

Author: bfoster
Date: Fri Oct 14 16:36:34 2011
New Revision: 1183415

URL: http://svn.apache.org/viewvc?rev=1183415&view=rev
Log:
NOTE: eclipse is being retard so these changes are going in over a few commits... it's not adding edited files to list of files to commit
- more unit-tests
- bug fixes found while unit-testing
- beginning of recurring option support

Added:
    oodt/branches/cas-cl/src/test/org/apache/oodt/cas/cl/option/util/TestArgs.java   (with props)
    oodt/branches/cas-cl/src/test/org/apache/oodt/cas/cl/parser/
    oodt/branches/cas-cl/src/test/org/apache/oodt/cas/cl/parser/TestStdCmdLineOptionParser.java   (with props)

Added: oodt/branches/cas-cl/src/test/org/apache/oodt/cas/cl/option/util/TestArgs.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/test/org/apache/oodt/cas/cl/option/util/TestArgs.java?rev=1183415&view=auto
==============================================================================
--- oodt/branches/cas-cl/src/test/org/apache/oodt/cas/cl/option/util/TestArgs.java (added)
+++ oodt/branches/cas-cl/src/test/org/apache/oodt/cas/cl/option/util/TestArgs.java Fri Oct 14 16:36:34 2011
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.oodt.cas.cl.option.util;
+
+//JDK imports
+import java.util.Arrays;
+
+//JUnit imports
+import junit.framework.TestCase;
+
+/**
+ * Test class for {@link Args}.
+ *
+ * @author bfoster (Brian Foster)
+ */
+public class TestArgs extends TestCase {
+
+	private static final String ARGS_STRING = "--operation download --url http://somewhere.com --user foo --pass bar --toDir /tmp";
+
+	public void testIteration() {
+		StringBuffer argsString = new StringBuffer("");
+		Args args = createArgs();
+		for (String arg : args) {
+			argsString.append(arg).append(" ");
+			int i = 0;
+			for (String argInner : args) {
+				argsString.append(argInner).append(" ");
+				if (i++ > 1) {
+					break;
+				}
+			}
+			argsString.append(args.getAndIncrement()).append(" ");
+		}
+
+		assertEquals(Arrays.asList(ARGS_STRING.split(" ")), Arrays.asList(argsString.toString().split(" ")));
+	}
+
+	public void testIndexOutOfBoundsException() {
+		Args args = new Args(new String[] {});
+		try {
+			args.iterator().next();
+			fail("Should have thrown IndexOutOfBoundsException");
+		} catch (IndexOutOfBoundsException ignore) { /* expect throw */ }
+	}
+
+	private Args createArgs() {
+		return new Args(ARGS_STRING.split(" "));
+	}
+}

Propchange: oodt/branches/cas-cl/src/test/org/apache/oodt/cas/cl/option/util/TestArgs.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: oodt/branches/cas-cl/src/test/org/apache/oodt/cas/cl/parser/TestStdCmdLineOptionParser.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/test/org/apache/oodt/cas/cl/parser/TestStdCmdLineOptionParser.java?rev=1183415&view=auto
==============================================================================
--- oodt/branches/cas-cl/src/test/org/apache/oodt/cas/cl/parser/TestStdCmdLineOptionParser.java (added)
+++ oodt/branches/cas-cl/src/test/org/apache/oodt/cas/cl/parser/TestStdCmdLineOptionParser.java Fri Oct 14 16:36:34 2011
@@ -0,0 +1,87 @@
+package org.apache.oodt.cas.cl.parser;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.oodt.cas.cl.option.CmdLineOption;
+import org.apache.oodt.cas.cl.option.CmdLineOptionInstance;
+import org.apache.oodt.cas.cl.option.GroupCmdLineOption;
+import org.apache.oodt.cas.cl.option.GroupCmdLineOption.SubOption;
+import org.apache.oodt.cas.cl.option.HelpCmdLineOption;
+import org.apache.oodt.cas.cl.option.SimpleCmdLineOption;
+import org.apache.oodt.cas.cl.option.util.Args;
+import org.apache.oodt.cas.cl.parser.StdCmdLineOptionParser;
+
+import junit.framework.TestCase;
+
+public class TestStdCmdLineOptionParser extends TestCase {
+
+	public void testParser() throws IOException {
+		Args args = new Args("--group --list one two three four --scalar one --none --group --list one --scalar one".split(" "));
+		Set<CmdLineOption> options = new HashSet<CmdLineOption>();
+		SimpleCmdLineOption listOption, scalarOption, noneOption;
+		options.add(listOption = createSimpleOption("list", true));
+		options.add(scalarOption = createSimpleOption("scalar", true));
+		options.add(noneOption = createSimpleOption("none", false));
+		options.add(noneOption = createGroupOption("group",
+				new SubOption(listOption, true),
+				new SubOption(scalarOption, true),
+				new SubOption(noneOption, false)));
+		options.add(new HelpCmdLineOption());
+
+		StdCmdLineOptionParser parser = new StdCmdLineOptionParser();
+		Set<CmdLineOptionInstance> specifiedOptions = parser.parse(args, options);
+		assertEquals(2, specifiedOptions.size());
+	}
+
+	public void testGetOptions() throws IOException {
+		Args args = new Args("--scalar one --none".split(" "));
+		SimpleCmdLineOption option = createSimpleOption(StdCmdLineOptionParser.getOptionName(args.getAndIncrement()), true);
+		CmdLineOptionInstance specifiedOption = StdCmdLineOptionParser.getOption(args, option);
+		assertEquals(specifiedOption.getOption(), option);
+		assertEquals(Arrays.asList("one"), specifiedOption.getValues());
+		assertTrue(specifiedOption.getSubOptions().isEmpty());
+	}
+
+	public void testGetValues() {
+		Args args = new Args("--list one two three four --scalar one --none".split(" "));
+		assertEquals("--list", args.getAndIncrement());
+		assertEquals(Arrays.asList("one", "two", "three", "four"), StdCmdLineOptionParser.getValues(args));
+		assertEquals("--scalar", args.getAndIncrement());
+		assertEquals(Arrays.asList("one"), StdCmdLineOptionParser.getValues(args));		
+		assertEquals("--none", args.getAndIncrement());
+		assertEquals(Collections.emptyList(), StdCmdLineOptionParser.getValues(args));		
+		assertNull(args.getCurrentArg());
+	}
+
+	public void testIsOption() {
+		assertTrue(StdCmdLineOptionParser.isOption("--arg"));
+		assertTrue(StdCmdLineOptionParser.isOption("-arg"));
+		assertFalse(StdCmdLineOptionParser.isOption("arg"));
+	}
+
+	public void testGetOptionName() {
+		assertEquals("arg", StdCmdLineOptionParser.getOptionName("--arg"));
+		assertEquals("arg", StdCmdLineOptionParser.getOptionName("-arg"));
+		assertNull(StdCmdLineOptionParser.getOptionName("arg"));
+	}
+
+	private static GroupCmdLineOption createGroupOption(String longName, SubOption... subOptions) {
+		GroupCmdLineOption option = new GroupCmdLineOption();
+		option.setLongOption(longName);
+		option.setShortOption(longName);
+		option.setSubOptions(new HashSet<SubOption>(Arrays.asList(subOptions)));
+		return option;
+	}
+
+	private static SimpleCmdLineOption createSimpleOption(String longName, boolean hasArgs) {
+		SimpleCmdLineOption option = new SimpleCmdLineOption();
+		option.setLongOption(longName);
+		option.setShortOption(longName);
+		option.setHasArgs(hasArgs);
+		return option;
+	}
+}

Propchange: oodt/branches/cas-cl/src/test/org/apache/oodt/cas/cl/parser/TestStdCmdLineOptionParser.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain