]> source.dussan.org Git - aspectj.git/commitdiff
parseBoolean utility
authorwisberg <wisberg>
Tue, 17 May 2005 00:03:14 +0000 (00:03 +0000)
committerwisberg <wisberg>
Tue, 17 May 2005 00:03:14 +0000 (00:03 +0000)
testing-util/src/org/aspectj/testing/util/TestUtil.java
testing-util/testsrc/org/aspectj/testingutil/TestUtilTest.java
testing/src/org/aspectj/testing/harness/bridge/JavaRun.java

index 4b62f9a907c5b3a285b6a2a957e46e9c90484504..3095b6e2d63dc0f9a3864af39c5a12ebec907bdf 100644 (file)
@@ -179,6 +179,52 @@ public final class TestUtil {
         return path.toString();
     }
     
+    /**
+     * @param s the String to parse for [on|off|true|false]
+     * @throws IllegalArgumentException if input is bad
+     **/
+    public static boolean parseBoolean(String input) {
+        return parseBoolean(input, true);
+    }
+    
+    /**
+     * @param s the String to parse for [on|off|true|false]
+     * @param iaxOnError if true and input is bad, throw
+     * IllegalArgumentException
+     * @return true if input is true, false otherwise
+     * @throws IllegalArgumentException if iaxOnError and input is bad
+     */
+     public static boolean parseBoolean(final String input, boolean iaxOnError) {
+        final String syntax = ": [on|off|true|false]";
+        if (null == input) {
+            return false;
+        }
+        String lc = input.trim().toLowerCase();
+        boolean result = false;
+        boolean valid = false;
+        switch (lc.length()) {
+        case 2:
+            if (valid = "on".equals(lc)) {
+                result = true;                
+            }
+            break;
+        case 3:
+            valid = "off".equals(lc);
+            break;
+        case 4:
+            if (valid = "true".equals(lc)) {
+                result = true;                
+            }
+            break;
+        case 5:
+            valid = "false".equals(lc);
+            break;
+        }
+        if (iaxOnError && !valid) {
+            throw new IllegalArgumentException(input + syntax);
+        }
+        return result;
+    }
 
     public static File aspectjrtJarFile() {
         return (File) LIB_ENTRIES.get(ASPECTJRT_KEY + ".file");
index 00fb0f385fbda3aca545841a3c162fdeb3ff177c..3efe7757b6f92a552587b005190300b29b1deac2 100644 (file)
@@ -93,6 +93,40 @@ public class TestUtilTest extends TestCase {
         assertTrue(!TestUtil.sameFiles(holder, expectedBaseDir, actualBaseDir, filename));
     }
 
+    public void testParseBoolean() {
+        {
+            String[] trues = {"true", "TRUE", "on", "ON" };
+            for (int i = 0; i < trues.length; i++) {
+                assertTrue(trues[i], TestUtil.parseBoolean(trues[i]));
+            }
+        }
+        {
+            String[] falses = {"false", "FALSE", "off", "off" };
+            for (int i = 0; i < falses.length; i++) {
+                assertTrue(falses[i], !TestUtil.parseBoolean(falses[i]));
+            }
+        }
+        String[] errors = {"fals", "tru", "T", "on of" };
+        boolean fail = false;
+        final int MAX = errors.length-1;
+        for (int i = 0; i <= MAX; i++) {
+            try {
+                TestUtil.parseBoolean(errors[i], fail);
+                assertTrue("no exception: " + errors[i], !fail);
+            } catch (IllegalArgumentException e) {
+                assertTrue("exception: " + errors[i], fail);
+                String m = e.getMessage();
+                if (-1 == m.indexOf(errors[i])) {
+                    fail(errors[i] + " not in " + m);
+                }
+            }
+            if ((i == MAX) && !fail) {
+                i = -1;
+                fail = true;
+            }
+        }
+        
+    }
     public void testFileCompareClass() throws IOException {
         if (!TestUtil.ClassLineator.haveDisassembler()) {
             System.err.println("skipping testFileCompareClass - no disassembler on classpath");
index 7a16e25d5c5fc90dc18e93b8aea36ba1c33a1291..223a0c8a680244bda28afef0d79b636d69b7349c 100644 (file)
@@ -20,6 +20,7 @@ import org.aspectj.testing.run.IRunIterator;
 import org.aspectj.testing.run.IRunStatus;
 import org.aspectj.testing.run.WrappedRunIterator;
 import org.aspectj.testing.util.TestClassLoader;
+import org.aspectj.testing.util.TestUtil;
 import org.aspectj.testing.xml.SoftMessage;
 import org.aspectj.testing.xml.XMLWriter;
 import org.aspectj.util.FileUtil;
@@ -669,7 +670,7 @@ public class JavaRun implements IAjcRun {
         }
         
         public void setLTW(String ltw) {
-            useLTW = Boolean.parseBoolean(ltw);
+            useLTW = TestUtil.parseBoolean(ltw);
         }
         
         public void setAspectpath(String path) {
@@ -683,11 +684,11 @@ public class JavaRun implements IAjcRun {
             this.classpath = XMLWriter.unflattenList(path);
         }
         public void setErrStreamIsError(String errStreamIsError) {
-            this.errStreamIsError = Boolean.parseBoolean(errStreamIsError);
+            this.errStreamIsError = TestUtil.parseBoolean(errStreamIsError);
         }
 
         public void setOutStreamIsError(String outStreamIsError) {
-            this.outStreamIsError = Boolean.parseBoolean(outStreamIsError);
+            this.outStreamIsError = TestUtil.parseBoolean(outStreamIsError);
         }
 
         /** @param skip if true, then do not set up Tester */