001: package abbot.util;
002:
003: import junit.extensions.abbot.TestHelper;
004: import junit.framework.TestCase;
005:
006: /** Test ArgumentParser functions. */
007:
008: public class ExtendedComparatorTest extends TestCase {
009:
010: public void testCompareNull() {
011: assertTrue("Compare against null should fail",
012: !ExtendedComparator.equals("this", null));
013: assertTrue("Compare against null should fail",
014: !ExtendedComparator.equals(null, "this"));
015: }
016:
017: public void testCompareStrings() {
018: String[][] matchValues = { { "Orange", "Orange" },
019: { "/O.*/", "Orange" }, { "/^Or....$/", "Orange" },
020: { "/One|Two/", "One" }, { "/One|Two/", "Two" },
021: { "/", "/" }, { "///", "/" }, };
022: for (int i = 0; i < matchValues.length; i++) {
023: assertTrue("String <" + matchValues[i][0]
024: + "> should match <" + matchValues[i][1] + ">",
025: ExtendedComparator.stringsMatch(matchValues[i][0],
026: matchValues[i][1]));
027: }
028: String[][] nonmatchValues = { { "Black", "Orange" },
029: { "Ora", "Orange" }, { "O.*", "XOrange" },
030: { "/^Or....$/", "XOrange" }, { "///", "No slash" }, };
031: for (int i = 0; i < nonmatchValues.length; i++) {
032: assertTrue("String <" + nonmatchValues[i][0]
033: + "> should not match <" + nonmatchValues[i][1]
034: + ">", !ExtendedComparator.stringsMatch(
035: nonmatchValues[i][0], nonmatchValues[i][1]));
036: }
037: // I can't get multi-line matches to work, either with gnu regexp or
038: // java.util.regex.*;
039: /*
040: String[][] multiValues = {
041: { "/(?m)One$Two/", "One\nTwo" },
042: };
043: for (int i=0;i < multiValues.length;i++) {
044: assertTrue("String <" + multiValues[i][0]
045: + "> should match <" + multiValues[i][1] + ">",
046: ExtendedComparator.
047: stringsMatch(multiValues[i][0],
048: multiValues[i][1]));
049: }
050: String[][] nmultiValues = {
051: { "/^Two$/", "One\nTwo\nThree" },
052: };
053: for (int i=0;i < nmultiValues.length;i++) {
054: assertTrue("String <" + nmultiValues[i][0]
055: + "> should not match <" + nmultiValues[i][1] + ">",
056: !ExtendedComparator.
057: stringsMatch(nmultiValues[i][0],
058: nmultiValues[i][1]));
059: }
060: */
061: }
062:
063: /** Checks array comparisons. */
064: public void testArrayEquals() {
065: String[] array0 = {};
066: String[] array0a = {};
067: String[] array1 = { "one" };
068: String[] array1a = { "one" };
069: String[] array1b = { "six" };
070: String[] array2 = { "one", "two" };
071: String[] array2a = { "one", "two" };
072: String[] array5 = { "one", "two", "three", "four", "five" };
073: String[] array5a = { "one", "two", "three", "four", "five" };
074: String[] array6 = { "/[0-9]* count/", "Orange", "Black" };
075: String[] array6a = { "99 count", "Orange", "Black" };
076: String[] array6b = { "99 count", "XOrange", "Black" };
077:
078: assertTrue("Array size 0 comparison failed", ExtendedComparator
079: .equals(array0, array0a));
080: assertTrue("Array size 0 discomparison failed",
081: !ExtendedComparator.equals(array0, array1));
082: assertTrue("Array size 1 comparison failed", ExtendedComparator
083: .equals(array1, array1a));
084: assertTrue("Array size 1 discomparison failed",
085: !ExtendedComparator.equals(array1, array1b));
086: assertTrue("Array size 1 discomparison failed",
087: !ExtendedComparator.equals(array1, array2));
088: assertTrue("Array size 2 comparison failed", ExtendedComparator
089: .equals(array2, array2a));
090: assertTrue("Array size 5 comparison failed", ExtendedComparator
091: .equals(array5, array5a));
092: assertTrue("Array/object discomparison failed",
093: !ExtendedComparator.equals(array5, new Integer(0)));
094: assertTrue("Arrays of different types discomparison failed",
095: !ExtendedComparator.equals(array5, new Integer[5]));
096: assertTrue("String array with regexp pattern should match",
097: ExtendedComparator.equals(array6, array6a));
098: assertTrue("String array with regexp pattern should not match",
099: !ExtendedComparator.equals(array6, array6b));
100: }
101:
102: /** Create a new test case with the given name. */
103: public ExtendedComparatorTest(String name) {
104: super (name);
105: }
106:
107: /** Return the default test suite. */
108: public static void main(String[] args) {
109: TestHelper.runTests(args, ExtendedComparatorTest.class);
110: }
111: }
|