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/15 01:30:47 UTC
svn commit: r1183557 - in /oodt/branches/cas-cl: ./
src/main/java/org/apache/oodt/cas/cl/option/handler/
src/main/java/org/apache/oodt/cas/cl/parser/
src/main/java/org/apache/oodt/cas/cl/util/
src/test/org/apache/oodt/cas/cl/util/
Author: bfoster
Date: Fri Oct 14 23:30:46 2011
New Revision: 1183557
URL: http://svn.apache.org/viewvc?rev=1183557&view=rev
Log:
- more unit-tests
- added dependency on Google Guava... check it out... pretty sweet (http://code.google.com/p/guava-libraries/)... cool helper methods for Collection types and also annotations such as @VisibleForTesting for notating a method as being visible for testing purposes
Modified:
oodt/branches/cas-cl/pom.xml
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/CmdLineOptionBeanHandler.java
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/parser/StdCmdLineOptionParser.java
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/util/CmdLineUtils.java
oodt/branches/cas-cl/src/test/org/apache/oodt/cas/cl/util/TestCmdLineUtils.java
Modified: oodt/branches/cas-cl/pom.xml
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/pom.xml?rev=1183557&r1=1183556&r2=1183557&view=diff
==============================================================================
--- oodt/branches/cas-cl/pom.xml (original)
+++ oodt/branches/cas-cl/pom.xml Fri Oct 14 23:30:46 2011
@@ -51,26 +51,26 @@
</profile>
</profiles>
<dependencies>
-<!-- <dependency>
- <groupId>org.apache.oodt</groupId>
- <artifactId>oodt-commons</artifactId>
- <version>${project.parent.version}</version>
- </dependency> -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.3</version>
</dependency>
<dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>2.5.4</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-hibernate3</artifactId>
- <version>2.0.8</version>
- </dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-core</artifactId>
+ <version>2.5.4</version>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-hibernate3</artifactId>
+ <version>2.0.8</version>
+ </dependency>
+ <dependency>
+ <groupId>com.google.guava</groupId>
+ <artifactId>guava</artifactId>
+ <version>10.0.1</version>
+ </dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Modified: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/CmdLineOptionBeanHandler.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/CmdLineOptionBeanHandler.java?rev=1183557&r1=1183556&r2=1183557&view=diff
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/CmdLineOptionBeanHandler.java (original)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/CmdLineOptionBeanHandler.java Fri Oct 14 23:30:46 2011
@@ -16,15 +16,12 @@
*/
package org.apache.oodt.cas.cl.option.handler;
-//JDK imports
+//OODT static imports
import static org.apache.oodt.cas.cl.util.CmdLineUtils.convertToType;
-import java.io.File;
-import java.net.MalformedURLException;
-import java.net.URL;
+//JDK imports
import java.util.Arrays;
import java.util.HashSet;
-import java.util.LinkedList;
import java.util.List;
//OODT imports
Modified: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/parser/StdCmdLineOptionParser.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/parser/StdCmdLineOptionParser.java?rev=1183557&r1=1183556&r2=1183557&view=diff
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/parser/StdCmdLineOptionParser.java (original)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/parser/StdCmdLineOptionParser.java Fri Oct 14 23:30:46 2011
@@ -38,6 +38,8 @@ import org.apache.oodt.cas.cl.option.Gro
import org.apache.oodt.cas.cl.option.GroupCmdLineOption.SubOption;
import org.apache.oodt.cas.cl.util.Args;
+import com.google.common.annotations.VisibleForTesting;
+
/**
* @author bfoster
* @version $Revision$
@@ -128,6 +130,7 @@ public class StdCmdLineOptionParser impl
return optionInstances;
}
+ @VisibleForTesting
/*package*/ static Set<CmdLineOption> verifyGroupHasRequiredSubOptions(CmdLineOptionInstance group) {
Validate.isTrue(group.isGroup());
@@ -146,6 +149,7 @@ public class StdCmdLineOptionParser impl
return missingSubOptions;
}
+ @VisibleForTesting
/*package*/ static CmdLineOptionInstance getOption(Args args, CmdLineOption option) throws IOException {
CmdLineOptionInstance specifiedOption = new CmdLineOptionInstance();
specifiedOption.setOption(option);
@@ -162,6 +166,7 @@ public class StdCmdLineOptionParser impl
return specifiedOption;
}
+ @VisibleForTesting
/*package*/ static List<String> getValues(Args args) {
List<String> values = new ArrayList<String>();
String nextValue = args.getCurrentArg();
@@ -172,10 +177,12 @@ public class StdCmdLineOptionParser impl
return values;
}
+ @VisibleForTesting
/*package*/ static boolean isOption(String arg) {
return (arg.startsWith("-"));
}
+ @VisibleForTesting
/*package*/ static String getOptionName(String arg) {
if (arg.startsWith("--")) {
return arg.substring(2);
Modified: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/util/CmdLineUtils.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/util/CmdLineUtils.java?rev=1183557&r1=1183556&r2=1183557&view=diff
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/util/CmdLineUtils.java (original)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/util/CmdLineUtils.java Fri Oct 14 23:30:46 2011
@@ -14,7 +14,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
package org.apache.oodt.cas.cl.util;
//JDK imports
@@ -47,13 +46,25 @@ import org.apache.oodt.cas.cl.option.val
import org.apache.oodt.cas.cl.action.CmdLineAction;
/**
- * @author bfoster
- * @version $Revision$
+ * Collection of common helper methods.
+ *
+ * @author bfoster (Brian Foster)
*/
public class CmdLineUtils {
private CmdLineUtils() {}
+ /**
+ * Determines which of the given {@link CmdLineOption}s are required because
+ * the given {@link CmdLineAction} was specified.
+ *
+ * @param action
+ * The {@link CmdLineAction} which was specified.
+ * @param options
+ * The {@link CmdLineOption}s in question of being required or not.
+ * @return The {@link Set} of {@link CmdLineOption}s where are required
+ * because the given {@link CmdLineAction} was specified.
+ */
public static Set<CmdLineOption> determineRequired(CmdLineAction action,
Set<CmdLineOption> options) {
Set<CmdLineOption> requiredOptions = getRequiredOptions(options);
@@ -65,6 +76,16 @@ public class CmdLineUtils {
return requiredOptions;
}
+ /**
+ * Determines if the given {@link CmdLineOption} is required because the given
+ * {@link CmdLineAction} was specified.
+ *
+ * @param action
+ * The {@link CmdLineAction} which was specified.
+ * @param option
+ * The {@link CmdLineOption} in question of being required or not.
+ * @return True is option is required, false otherwise.
+ */
public static boolean isRequired(CmdLineAction action, CmdLineOption option) {
Validate.notNull(option);
Validate.notNull(action);
@@ -81,7 +102,19 @@ public class CmdLineUtils {
return false;
}
- public static Set<CmdLineOption> determineOptional(CmdLineAction action, Set<CmdLineOption> options) {
+ /**
+ * Determines which of the given {@link CmdLineOption}s are optional because
+ * the given {@link CmdLineAction} was specified.
+ *
+ * @param action
+ * The {@link CmdLineAction} which was specified.
+ * @param options
+ * The {@link CmdLineOption} in question of being optional or not.
+ * @return The {@link Set} of {@link CmdLineOption}s where are optional
+ * because the given {@link CmdLineAction} was specified.
+ */
+ public static Set<CmdLineOption> determineOptional(CmdLineAction action,
+ Set<CmdLineOption> options) {
Set<CmdLineOption> optionalOptions = new HashSet<CmdLineOption>();
for (CmdLineOption option : options) {
if (isOptional(action, option)) {
@@ -91,6 +124,16 @@ public class CmdLineUtils {
return optionalOptions;
}
+ /**
+ * Determines if the given {@link CmdLineOption} is optional because the given
+ * {@link CmdLineAction} was specified.
+ *
+ * @param action
+ * The {@link CmdLineAction} which was specified.
+ * @param option
+ * The {@link CmdLineOption} in question of being optional or not.
+ * @return True is option is optional, false otherwise.
+ */
public static boolean isOptional(CmdLineAction action, CmdLineOption option) {
Validate.notNull(option);
Validate.notNull(action);
@@ -107,16 +150,56 @@ public class CmdLineUtils {
return false;
}
+ /**
+ * Get {@link CmdLineOption}s which are always required regardless of
+ * {@link CmdLineAction} specified. NOTE: Ignores {@link CmdLineOption}s of
+ * type {@link ActionCmdLineOption}.
+ *
+ * @param options
+ * The {@link CmdLineOption}S to check for required
+ * {@link CmdLineOption}s
+ * @return The {@link CmdLineOption}s which will be check for always required
+ * {@link CmdLineOption}s
+ */
public static Set<CmdLineOption> getRequiredOptions(Set<CmdLineOption> options) {
+ return getRequiredOptions(options, true);
+ }
+
+ /**
+ * Get {@link CmdLineOption}s which are always required regardless of
+ * {@link CmdLineAction} specified.
+ *
+ * @param options
+ * The {@link CmdLineOption}S to check for required
+ * {@link CmdLineOption}s
+ * @param ignoreActionOption
+ * Where or not to ignore {@link CmdLineOption}s of type
+ * {@link ActionCmdLineOption}
+ * @return The {@link CmdLineOption}s which will be check for always required
+ * {@link CmdLineOption}s
+ */
+ public static Set<CmdLineOption> getRequiredOptions(
+ Set<CmdLineOption> options, boolean ignoreActionOption) {
HashSet<CmdLineOption> requiredOptions = new HashSet<CmdLineOption>();
for (CmdLineOption option : options) {
- if (!(option instanceof ActionCmdLineOption) && option.isRequired()) {
+ if (option.isRequired()
+ && !(isActionOption(option) && ignoreActionOption)) {
requiredOptions.add(option);
}
}
return requiredOptions;
}
+ /**
+ * Sorts {@link CmdLineOption}s by requirement levels. {@link CmdLineOption}s
+ * which are always required have highest sort score, followed by
+ * {@link CmdLineOption}s which have {@link RequirementRule}s, followed by all
+ * others.
+ *
+ * @param options
+ * The {@link Set} of {@link CmdLineOption}s to sort.
+ * @return The {@link CmdLineOption}s sorted by requirement.
+ */
public static List<CmdLineOption> sortOptionsByRequiredStatus(
Set<CmdLineOption> options) {
ArrayList<CmdLineOption> optionsList = new ArrayList<CmdLineOption>(options);
Modified: oodt/branches/cas-cl/src/test/org/apache/oodt/cas/cl/util/TestCmdLineUtils.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/test/org/apache/oodt/cas/cl/util/TestCmdLineUtils.java?rev=1183557&r1=1183556&r2=1183557&view=diff
==============================================================================
--- oodt/branches/cas-cl/src/test/org/apache/oodt/cas/cl/util/TestCmdLineUtils.java (original)
+++ oodt/branches/cas-cl/src/test/org/apache/oodt/cas/cl/util/TestCmdLineUtils.java Fri Oct 14 23:30:46 2011
@@ -1,11 +1,31 @@
+/*
+ * 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.util;
+//JDK imports
import java.util.Collections;
import java.util.HashSet;
+import java.util.List;
import java.util.Set;
+//JUnit imports
import junit.framework.TestCase;
+//OODT imports
import org.apache.oodt.cas.cl.action.CmdLineAction;
import org.apache.oodt.cas.cl.option.ActionCmdLineOption;
import org.apache.oodt.cas.cl.option.CmdLineOption;
@@ -17,64 +37,120 @@ import org.apache.oodt.cas.cl.option.req
import org.apache.oodt.cas.cl.option.require.ActionDependency;
import org.apache.oodt.cas.cl.util.CmdLineUtils;
+//Google imports
+import com.google.common.collect.Sets;
+
+/**
+ * Test class for {@link CmdLineUtils}.
+ *
+ * @author bfoster (Brian Foster)
+ */
public class TestCmdLineUtils extends TestCase {
public void testDetermineRequired() {
CmdLineAction action = createAction("TestAction");
- ActionCmdLineOption actionOption = createActionOption("operation");
+ CmdLineOption urlOption, passOption;
- HashSet<CmdLineOption> options = new HashSet<CmdLineOption>();
- options.add(createSimpleOption("url", createRequiredRequirementRule(action)));
- options.add(createSimpleOption("pass", false));
- options.add(createSimpleOption("user", false));
- options.add(actionOption);
+ HashSet<CmdLineOption> options = Sets.newHashSet(
+ urlOption = createSimpleOption("url", createRequiredRequirementRule(action)),
+ passOption = createSimpleOption("pass", false),
+ createSimpleOption("user", false),
+ createActionOption("operation"));
Set<CmdLineOption> requiredOptions = CmdLineUtils.determineRequired(action, options);
- assertEquals(1, requiredOptions.size());
- assertNotNull(CmdLineUtils.getOptionByName("url", requiredOptions));
+ assertEquals(Sets.newHashSet(urlOption), requiredOptions);
- options = new HashSet<CmdLineOption>();
- options.add(createSimpleOption("url", createRequiredRequirementRule(action)));
- options.add(createSimpleOption("pass", true));
- options.add(createSimpleOption("user", false));
- options.add(actionOption);
+ options = Sets.newHashSet(
+ urlOption = createSimpleOption("url", createRequiredRequirementRule(action)),
+ passOption = createSimpleOption("pass", true),
+ createSimpleOption("user", false),
+ createActionOption("operation"));
requiredOptions = CmdLineUtils.determineRequired(action, options);
- assertEquals(2, requiredOptions.size());
- assertNotNull(CmdLineUtils.getOptionByName("url", requiredOptions));
- assertNotNull(CmdLineUtils.getOptionByName("pass", requiredOptions));
+ assertEquals(Sets.newHashSet(urlOption, passOption), requiredOptions);
+ }
+
+ public void testIsRequired() {
+ CmdLineAction action = createAction("TestAction");
+ assertTrue(CmdLineUtils.isRequired(action, createSimpleOption("url", createRequiredRequirementRule(action))));
+ assertFalse(CmdLineUtils.isRequired(action, createSimpleOption("url", true)));
+ assertFalse(CmdLineUtils.isRequired(action, createSimpleOption("url", false)));
}
public void testDetermineOptional() {
CmdLineAction action = createAction("TestAction");
- ActionCmdLineOption actionOption = new ActionCmdLineOption();
+ CmdLineOption actionOption = new ActionCmdLineOption();
- HashSet<CmdLineOption> options = new HashSet<CmdLineOption>();
- options.add(createSimpleOption("url", createRequiredRequirementRule(action)));
- options.add(createSimpleOption("pass", false));
- options.add(createSimpleOption("user", false));
- options.add(actionOption);
+ HashSet<CmdLineOption> options = Sets.newHashSet(
+ createSimpleOption("url", createRequiredRequirementRule(action)),
+ createSimpleOption("pass", false),
+ createSimpleOption("user", false),
+ actionOption);
Set<CmdLineOption> optionalOptions = CmdLineUtils.determineOptional(action, options);
- assertEquals(0, optionalOptions.size());
+ assertTrue(optionalOptions.isEmpty());
- options = new HashSet<CmdLineOption>();
- options.add(createSimpleOption("pass", true));
- options.add(createSimpleOption("user", true));
- options.add(actionOption);
+ options = Sets.newHashSet(
+ createSimpleOption("pass", true),
+ createSimpleOption("user", true),
+ actionOption);
optionalOptions = CmdLineUtils.determineOptional(action, options);
- assertEquals(0, optionalOptions.size());
+ assertTrue(optionalOptions.isEmpty());
- options = new HashSet<CmdLineOption>();
- options.add(createSimpleOption("pass", createOptionalRequirementRule(action)));
- options.add(createSimpleOption("user", createOptionalRequirementRule(action)));
- options.add(actionOption);
+ CmdLineOption passOption, userOption;
+ options = Sets.newHashSet(
+ passOption = createSimpleOption("pass", createOptionalRequirementRule(action)),
+ userOption = createSimpleOption("user", createOptionalRequirementRule(action)),
+ actionOption);
optionalOptions = CmdLineUtils.determineOptional(action, options);
- assertEquals(2, optionalOptions.size());
- assertNotNull(CmdLineUtils.getOptionByName("pass", optionalOptions));
- assertNotNull(CmdLineUtils.getOptionByName("user", optionalOptions));
+ assertEquals(Sets.newHashSet(passOption, userOption), optionalOptions);
+ }
+
+ public void testIsOptional() {
+ CmdLineAction action = createAction("TestAction");
+ assertTrue(CmdLineUtils.isOptional(action, createSimpleOption("url", createOptionalRequirementRule(action))));
+ assertFalse(CmdLineUtils.isOptional(action, createSimpleOption("url", true)));
+ assertFalse(CmdLineUtils.isOptional(action, createSimpleOption("url", false)));
+ }
+
+ public void testGetRequiredOptions() {
+ CmdLineOption urlOption = createSimpleOption("url", true);
+ HashSet<CmdLineOption> options = Sets.newHashSet(
+ urlOption,
+ createActionOption("action"),
+ createSimpleOption("user", false),
+ createSimpleOption("pass", false));
+
+ assertEquals(Sets.newHashSet(urlOption), CmdLineUtils.getRequiredOptions(options));
+ }
+
+ public void testGetRequiredOptionsDoNotIgnoreActionOptions() {
+ CmdLineOption actionOption, urlOption;
+ HashSet<CmdLineOption> options = Sets.newHashSet(
+ actionOption = createActionOption("action"),
+ urlOption = createSimpleOption("url", true),
+ createSimpleOption("user", false),
+ createSimpleOption("pass", false));
+
+ assertEquals(Sets.newHashSet(actionOption, urlOption), CmdLineUtils.getRequiredOptions(options, false));
+ }
+
+ public void testSortOptionsByRequiredStatus() {
+ CmdLineAction action = createAction("action");
+ CmdLineOption userOption, urlOption, passOption, actionOption;
+ HashSet<CmdLineOption> options = Sets.newHashSet(
+ userOption = createSimpleOption("user", false),
+ urlOption = createSimpleOption("url", createRequiredRequirementRule(action)),
+ passOption = createSimpleOption("pass", false),
+ actionOption = createActionOption("action"));
+
+ List<CmdLineOption> sortedOptions = CmdLineUtils.sortOptionsByRequiredStatus(options);
+ assertEquals(options.size(), sortedOptions.size());
+ assertEquals(actionOption, sortedOptions.get(0));
+ assertEquals(urlOption, sortedOptions.get(1));
+ assertEquals(Sets.newHashSet(userOption, passOption), Sets.newHashSet(sortedOptions.get(2), sortedOptions.get(3)));
}
private static CmdLineAction createAction(String name) {
Re: svn commit: r1183557 - in /oodt/branches/cas-cl: ./
src/main/java/org/apache/oodt/cas/cl/option/handler/
src/main/java/org/apache/oodt/cas/cl/parser/
src/main/java/org/apache/oodt/cas/cl/util/
src/test/org/apache/oodt/cas/cl/util/
Posted by "Mattmann, Chris A (388J)" <ch...@jpl.nasa.gov>.
On Oct 14, 2011, at 4:30 PM, <bf...@apache.org> <bf...@apache.org> wrote:
> Author: bfoster
> Date: Fri Oct 14 23:30:46 2011
> New Revision: 1183557
>
> URL: http://svn.apache.org/viewvc?rev=1183557&view=rev
> Log:
> - more unit-tests
> - added dependency on Google Guava... check it out... pretty sweet (http://code.google.com/p/guava-libraries/)... cool helper methods for Collection types and also annotations such as @VisibleForTesting for notating a method as being visible for testing purposes
Very cool, will check it out!
Cheers,
Chris
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Chris Mattmann, Ph.D.
Senior Computer Scientist
NASA Jet Propulsion Laboratory Pasadena, CA 91109 USA
Office: 171-266B, Mailstop: 171-246
Email: chris.a.mattmann@nasa.gov
WWW: http://sunset.usc.edu/~mattmann/
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Adjunct Assistant Professor, Computer Science Department
University of Southern California, Los Angeles, CA 90089 USA
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++