Author: rob.stryker(a)jboss.com
Date: 2011-12-15 10:40:55 -0500 (Thu, 15 Dec 2011)
New Revision: 37356
Added:
trunk/as/tests/org.jboss.ide.eclipse.as.test/src/org/jboss/ide/eclipse/as/test/util/ArgsUtilTest.java
Modified:
trunk/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/util/ArgsUtil.java
trunk/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/util/internal/FileUtils.java
trunk/as/tests/org.jboss.ide.eclipse.as.test/src/org/jboss/ide/eclipse/as/test/ASTestSuite.java
Log:
JBIDE-10492 - argutils cleanup and test case
Modified:
trunk/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/util/ArgsUtil.java
===================================================================
---
trunk/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/util/ArgsUtil.java 2011-12-15
13:59:09 UTC (rev 37355)
+++
trunk/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/util/ArgsUtil.java 2011-12-15
15:40:55 UTC (rev 37356)
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2007 Red Hat, Inc.
+ * Copyright (c) 2011 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v1.0 which accompanies this distribution,
@@ -11,6 +11,7 @@
package org.jboss.ide.eclipse.as.core.util;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@@ -29,39 +30,8 @@
public static final String EMPTY=""; //$NON-NLS-1$
public static final String QUOTE="\""; //$NON-NLS-1$
- public static Map<String, Object> getSystemProperties(String argString) {
- String[] args = parse(argString);
- HashMap<String, Object> map = new HashMap<String, Object>();
-
- for( int i = 0; i < args.length; i++ ) {
- if( args[i].startsWith(VMO)) {
- int eq = args[i].indexOf(EQ);
- if( eq != -1 ) {
- map.put(args[i].substring(2, eq),
- args[i].substring(eq+1));
- } else {
- map.put(args[i], NO_VALUE);
- }
- }
- }
- return map;
- }
-
- public static String getValue(String allArgs, String shortOpt, String longOpt) {
- return getValue(parse(allArgs), shortOpt, longOpt);
- }
-
- public static String getValue(String[] args, String shortOpt, String longOpt ) {
- for( int i = 0; i < args.length; i++ ) {
- if( args[i].equals(shortOpt) && i+1 < args.length)
- return args[i+1];
- if( longOpt != null && args[i].startsWith(longOpt + EQ))
- return args[i].substring(args[i].indexOf(EQ) + 1);
- }
- return null;
- }
-
public static String[] parse(String s) {
+ s = s.trim();
try {
ArrayList<String> l = new ArrayList<String>();
int length = s.length();
@@ -83,7 +53,8 @@
case ' ':
if( !inQuotes ) {
tmp = buf.toString();
- l.add(tmp);
+ if( !(tmp.trim()).equals("")) //$NON-NLS-1$
+ l.add(tmp);
buf = new StringBuffer();
} else {
buf.append(' ');
@@ -112,14 +83,99 @@
return new String[] { };
}
}
+
+ public static Map<String, Object> getSystemProperties(String argString) {
+ String[] args = parse(argString);
+ HashMap<String, Object> map = new HashMap<String, Object>();
+
+ for( int i = 0; i < args.length; i++ ) {
+ if( args[i].startsWith(VMO)) {
+ int eq = args[i].indexOf(EQ);
+ if( eq != -1 ) {
+ map.put(args[i].substring(2, eq),
+ args[i].substring(eq+1));
+ } else {
+ map.put(args[i], NO_VALUE);
+ }
+ }
+ }
+ return map;
+ }
+
+ public static String getValue(String allArgs, String shortOpt, String longOpt) {
+ return getValue(parse(allArgs), shortOpt, longOpt);
+ }
+ public static String getValue(String[] args, String shortOpt, String longOpt ) {
+ String[] shortOpt2 = shortOpt == null ? new String[0] : new String[]{shortOpt};
+ String[] longOpt2 = longOpt == null ? new String[0] : new String[]{longOpt};
+ return getValue(args,shortOpt2,longOpt2);
+ }
+
+ public static String getValue(String allArgs, String[] shortOpt, String[] longOpt) {
+ return getValue(parse(allArgs), shortOpt, longOpt);
+ }
+
+ public static String getValue(String[] args, String[] shortOpt, String[] longOpt ) {
+ for( int i = 0; i < args.length; i++ ) {
+ if( shortOpt != null && matchesShortArg(args[i], shortOpt) && i+1 <
args.length)
+ return args[i+1];
+ if( longOpt != null && matchesLongArg(args[i], longOpt))
+ return args[i].substring(args[i].indexOf(EQ) + 1);
+ }
+ return null;
+ }
+
+ public static boolean matchesShortArg(String needle, String[] haystack) {
+ if( haystack == null )
+ return false;
+ return Arrays.asList(haystack).contains(needle);
+ }
+
+ public static boolean matchesLongArg(String needle, String[] haystack) {
+ if( haystack == null )
+ return false;
+ for( int i = 0; i < haystack.length; i++ ) {
+ if( needle.startsWith(haystack[i] + EQ) || needle.startsWith(QUOTE + haystack[i] +
EQ))
+ return true;
+ }
+ return false;
+ }
+
public static String setArg(String allArgs, String shortOpt, String longOpt, String
value ) {
- if( value.contains(SPACE))
+ if( value != null && value.contains(SPACE))
value = QUOTE + value + QUOTE;
return setArg(allArgs, shortOpt, longOpt, value, false);
}
public static String setArg(String allArgs, String shortOpt, String longOpt, String
value, boolean addQuotes ) {
+ String[] shortOpt2 = shortOpt == null ? new String[0] : new String[]{shortOpt};
+ String[] longOpt2 = longOpt == null ? new String[0] : new String[]{longOpt};
+ return setArg(allArgs, shortOpt2, longOpt2, value, addQuotes);
+ }
+
+ /**
+ * Replace (or add) an argument.
+ * Parse through the "allArgs" parameter to create a list of arguments.
+ * Compare each element in allArgs until you find a match against
+ * one of the short argument (-b value) or long argument (--host=etcetcetc)
+ * patterns. The set of short and long form arguments should be 100% interchangeable,
+ * and the caller must not have a preference which is ultimately returned.
+ *
+ * If a match is found, and the match is in the short-form arguments,
+ * do not change the arg (-b value), but update the value in the next segment.
+ *
+ * If a match is found and it is a long form argument, replace the string
+ * (ex: --host=localhost) with the first longOpt (--newLongOpt=127.0.0.1)
+ *
+ * @param allArgs
+ * @param shortOpt
+ * @param longOpt An array of possible long-form options
+ * @param value The new value, or null if you want to clear the option
+ * @param addQuotes
+ * @return
+ */
+ public static String setArg(String allArgs, String[] shortOpt, String[] longOpt, String
value, boolean addQuotes ) {
String originalValue = value;
if( addQuotes )
value = QUOTE + value + QUOTE;
@@ -127,20 +183,23 @@
String[] args = parse(allArgs);
String retVal = EMPTY;
for( int i = 0; i < args.length; i++ ) {
- if( args[i].equals(shortOpt)) {
- args[i+1] = value;
- retVal += args[i] + SPACE + args[++i] + SPACE;
+ if( matchesShortArg(args[i], shortOpt)) {
+ if( value != null ) {
+ args[i+1] = value;
+ retVal += args[i] + SPACE + args[++i] + SPACE;
+ }
found = true;
- } else if( longOpt != null &&
- (args[i].startsWith(longOpt + EQ) || args[i].startsWith(QUOTE + longOpt + EQ))) {
- String newVal = null;
- if( args[i].startsWith(QUOTE)) {
- newVal = QUOTE + longOpt + EQ + originalValue + QUOTE;
- } else {
- newVal = longOpt + EQ + value;
+ } else if( matchesLongArg(args[i], longOpt)) {
+ if( value != null ) {
+ String newVal = null;
+ if( args[i].startsWith(QUOTE)) {
+ newVal = QUOTE + longOpt[0] + EQ + originalValue + QUOTE;
+ } else {
+ newVal = longOpt[0] + EQ + value;
+ }
+ args[i] = newVal;
+ retVal += args[i] + SPACE;
}
- args[i] = newVal;
- retVal += args[i] + SPACE;
found = true;
} else {
retVal += args[i] + SPACE;
@@ -149,10 +208,10 @@
// turn this to a retval;
if( !found ) {
- if( longOpt != null )
- retVal = retVal + longOpt + EQ + value;
+ if( longOpt != null && longOpt.length > 0 )
+ retVal = retVal + longOpt[0] + EQ + value;
else
- retVal = retVal + shortOpt + SPACE + value;
+ retVal = retVal + shortOpt[0] + SPACE + value;
}
return retVal;
}
Modified:
trunk/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/util/internal/FileUtils.java
===================================================================
---
trunk/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/util/internal/FileUtils.java 2011-12-15
13:59:09 UTC (rev 37355)
+++
trunk/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/util/internal/FileUtils.java 2011-12-15
15:40:55 UTC (rev 37356)
@@ -1,3 +1,13 @@
+/*******************************************************************************
+ * Copyright (c) 2011 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
package org.jboss.ide.eclipse.as.core.util.internal;
import java.io.BufferedOutputStream;
Modified:
trunk/as/tests/org.jboss.ide.eclipse.as.test/src/org/jboss/ide/eclipse/as/test/ASTestSuite.java
===================================================================
---
trunk/as/tests/org.jboss.ide.eclipse.as.test/src/org/jboss/ide/eclipse/as/test/ASTestSuite.java 2011-12-15
13:59:09 UTC (rev 37355)
+++
trunk/as/tests/org.jboss.ide.eclipse.as.test/src/org/jboss/ide/eclipse/as/test/ASTestSuite.java 2011-12-15
15:40:55 UTC (rev 37356)
@@ -36,16 +36,18 @@
import org.jboss.ide.eclipse.as.test.publishing.v2.JSTDeployBinaryChildModuleTest;
import org.jboss.ide.eclipse.as.test.publishing.v2.JSTDeploymentTester;
import org.jboss.ide.eclipse.as.test.publishing.v2.JSTDeploymentWarUpdateXML;
+import org.jboss.ide.eclipse.as.test.publishing.v2.MockJSTPublisherTest;
import org.jboss.ide.eclipse.as.test.publishing.v2.MockJSTPublisherTestDynUtil;
import
org.jboss.ide.eclipse.as.test.publishing.v2.SingleFileDeployableMockDeploymentTester;
-import org.jboss.ide.eclipse.as.test.publishing.v2.MockJSTPublisherTest;
import org.jboss.ide.eclipse.as.test.publishing.v2.SingleFileDeploymentTester;
import org.jboss.ide.eclipse.as.test.server.JBossServerAPITest;
+import org.jboss.ide.eclipse.as.test.util.ArgsUtilTest;
public class ASTestSuite extends TestSuite {
public static Test suite() {
ValidationFramework.getDefault().suspendAllValidation(true);
TestSuite suite = new TestSuite("ASTools Test Suite");
+ suite.addTestSuite(ArgsUtilTest.class);
suite.addTestSuite(PreReqTest.class);
suite.addTestSuite(RuntimeServerModelTest.class);
suite.addTestSuite(JEEClasspathContainerTest.class);
Added:
trunk/as/tests/org.jboss.ide.eclipse.as.test/src/org/jboss/ide/eclipse/as/test/util/ArgsUtilTest.java
===================================================================
---
trunk/as/tests/org.jboss.ide.eclipse.as.test/src/org/jboss/ide/eclipse/as/test/util/ArgsUtilTest.java
(rev 0)
+++
trunk/as/tests/org.jboss.ide.eclipse.as.test/src/org/jboss/ide/eclipse/as/test/util/ArgsUtilTest.java 2011-12-15
15:40:55 UTC (rev 37356)
@@ -0,0 +1,112 @@
+/*******************************************************************************
+ * Copyright (c) 2011 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.ide.eclipse.as.test.util;
+
+import org.jboss.ide.eclipse.as.core.util.ArgsUtil;
+
+import junit.framework.TestCase;
+
+public class ArgsUtilTest extends TestCase {
+ public void testParse() {
+ assertEquals(1, ArgsUtil.parse("").length);
+ assertEquals(1, ArgsUtil.parse("a").length);
+ assertEquals(1, ArgsUtil.parse("abc ").length);
+ assertEquals(1, ArgsUtil.parse(" abcde ").length);
+ assertEquals(1, ArgsUtil.parse(" abcde ").length);
+ assertEquals(1, ArgsUtil.parse(" abcde ").length);
+ assertEquals(2, ArgsUtil.parse("a b").length);
+ assertEquals(2, ArgsUtil.parse("a b").length);
+ assertEquals(2, ArgsUtil.parse(" a b").length);
+ assertEquals(2, ArgsUtil.parse(" a b ").length);
+ assertEquals(2, ArgsUtil.parse("-b test ").length);
+ assertEquals(2, ArgsUtil.parse("-b --host=someval ").length);
+ assertEquals(2, ArgsUtil.parse("-b \"--host=some val\"").length);
+ assertEquals(3, ArgsUtil.parse("-b val --host=someval").length);
+ assertEquals(2, ArgsUtil.parse("-b --host=\"some val\"").length);
+ assertEquals(2, ArgsUtil.parse("-b \"--host=some val\"").length);
+ }
+
+ public void testShortArgMatch() {
+ assertTrue(ArgsUtil.matchesShortArg("-b", split("-b -c -d -f")));
+ assertFalse(ArgsUtil.matchesShortArg("-b", split("-c opt d two
-f")));
+ assertFalse(ArgsUtil.matchesShortArg("-b", split("b")));
+ assertFalse(ArgsUtil.matchesShortArg("-b", split("b opt")));
+ assertTrue(ArgsUtil.matchesShortArg("-b", split("-b opt")));
+
+ assertTrue(ArgsUtil.matchesShortArg("-host", split("-host -c -d
-f")));
+ assertFalse(ArgsUtil.matchesShortArg("-host", split("-c val d two
-f")));
+ assertFalse(ArgsUtil.matchesShortArg("-host", split("host")));
+ assertFalse(ArgsUtil.matchesShortArg("-host", split("host val")));
+ assertTrue(ArgsUtil.matchesShortArg("-host", split("-host val")));
+ }
+
+ public void testLongArgMatch() {
+ assertTrue(ArgsUtil.matchesLongArg("--host=localhost", split("--host
--longopt1 -longopt2 -f")));
+ assertFalse(ArgsUtil.matchesLongArg("-host=localhost", split("--host
--longopt1 -longopt2 -f")));
+ assertTrue(ArgsUtil.matchesLongArg("-longopt2=v1", split("--host
--longopt1 -longopt2 -f")));
+ assertFalse(ArgsUtil.matchesLongArg("-longopt2 v1", split("--host
--longopt1 -longopt2 -f")));
+ }
+
+ public void testReplace() {
+ String allArgs = "";
+ allArgs = ArgsUtil.setArg(allArgs, "-h", null, "new");
+ assertTrue(ArgsUtil.parse(allArgs).length == 2);
+ assertTrue(ArgsUtil.getValue(allArgs, "-h", null).equals("new"));
+ allArgs = ArgsUtil.setArg(allArgs, "-h", null, "correct");
+ assertTrue(ArgsUtil.parse(allArgs).length == 2);
+ assertTrue(ArgsUtil.getValue(allArgs, "-h",
null).equals("correct"));
+
+ allArgs = ArgsUtil.setArg(allArgs, null, "--two", "newtwo");
+ assertTrue(ArgsUtil.parse(allArgs).length == 3);
+
+ // opt was set as long opt, --two=newtwo. Search for short opt fails
+ assertTrue(ArgsUtil.getValue(allArgs, "--two", null) == null);
+
+ // clear long opt
+ allArgs = ArgsUtil.setArg(allArgs, null, "--two", null);
+ assertTrue(ArgsUtil.parse(allArgs).length == 2);
+
+
+ // test replacement of short args
+ allArgs = ArgsUtil.setArg(allArgs, new String[]{"-h", "-o"}, new
String[]{}, "twoOpt", false);
+ assertTrue(ArgsUtil.parse(allArgs).length == 2); // no change
+
+ assertTrue(ArgsUtil.getValue(allArgs, "-h",
null).equals("twoOpt"));
+ assertTrue(ArgsUtil.getValue(allArgs, new String[]{"-h", "-o"},
null).equals("twoOpt"));
+ assertTrue(ArgsUtil.getValue(allArgs, new String[]{"-o", "-h"},
null).equals("twoOpt"));
+
+ // test clear of short arg
+ allArgs = ArgsUtil.setArg(allArgs, new String[]{"-h", "-o"}, new
String[]{}, null, false);
+ assertTrue(ArgsUtil.parse(allArgs).length == 1); // no change
+
+ allArgs = ArgsUtil.setArg(allArgs, null, "--three", "three");
+ assertTrue(ArgsUtil.getValue(allArgs, null,
"--three").equals("three"));
+
+ allArgs = ArgsUtil.setArg(allArgs, null, "--three", "threea");
+ assertTrue(ArgsUtil.getValue(allArgs, null,
"--three").equals("threea"));
+
+ // already has --three, test replace, ensure --four is the new arg
+ // since it is the first of the two acceptable replacements
+ assertTrue(allArgs.contains("--three"));
+ allArgs = ArgsUtil.setArg(allArgs, null, new String[]{"--four",
"--three"}, "three_b", false);
+ assertTrue(allArgs.contains("--four"));
+ assertTrue(ArgsUtil.getValue(allArgs, null, "--three") == null);
+ assertTrue(ArgsUtil.getValue(allArgs, null,
"--four").equals("three_b"));
+ assertTrue(ArgsUtil.getValue(allArgs, null, new String[]{"--three",
"--four"}).equals("three_b"));
+
+ }
+
+ // Just for testing, simply split this string into a bunch of options.
+ // So I don't need to make new arrays all the time...
+ public String[] split(String val) {
+ return val.split(" ");
+ }
+}