diff options
author | wisberg <wisberg> | 2005-05-17 00:03:14 +0000 |
---|---|---|
committer | wisberg <wisberg> | 2005-05-17 00:03:14 +0000 |
commit | be5b8333d32e6efc02a73f4a83fbf10f1c5a9018 (patch) | |
tree | 60511ca7cf5795eef0451a5bf012a89a50dcc0bd /testing-util | |
parent | a2a3654d02ff77485fb35613b25f0b138074c0ba (diff) | |
download | aspectj-be5b8333d32e6efc02a73f4a83fbf10f1c5a9018.tar.gz aspectj-be5b8333d32e6efc02a73f4a83fbf10f1c5a9018.zip |
parseBoolean utility
Diffstat (limited to 'testing-util')
-rw-r--r-- | testing-util/src/org/aspectj/testing/util/TestUtil.java | 46 | ||||
-rw-r--r-- | testing-util/testsrc/org/aspectj/testingutil/TestUtilTest.java | 34 |
2 files changed, 80 insertions, 0 deletions
diff --git a/testing-util/src/org/aspectj/testing/util/TestUtil.java b/testing-util/src/org/aspectj/testing/util/TestUtil.java index 4b62f9a90..3095b6e2d 100644 --- a/testing-util/src/org/aspectj/testing/util/TestUtil.java +++ b/testing-util/src/org/aspectj/testing/util/TestUtil.java @@ -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"); diff --git a/testing-util/testsrc/org/aspectj/testingutil/TestUtilTest.java b/testing-util/testsrc/org/aspectj/testingutil/TestUtilTest.java index 00fb0f385..3efe7757b 100644 --- a/testing-util/testsrc/org/aspectj/testingutil/TestUtilTest.java +++ b/testing-util/testsrc/org/aspectj/testingutil/TestUtilTest.java @@ -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"); |