Commits

Anonymous committed 6399083

Added unit tests for verifiers.

git-svn-id: http://svn.opensymphony.com/svn/propertyset/trunk@139bf3cbcdd-1c1a-0410-9a68-d6f521e3fa7b

  • Participants
  • Parent commits a7084b8

Comments (0)

Files changed (2)

File src/test/com/opensymphony/module/propertyset/verifiers/NumberVerifierTest.java

+package com.opensymphony.module.propertyset.verifiers;
+
+import junit.framework.TestCase;
+
+/**
+ * User: bbulger
+ * Date: May 22, 2004
+ */
+public class NumberVerifierTest extends TestCase {
+
+    protected NumberVerifier verifier;
+
+    protected void setUp() throws Exception {
+        super.setUp();
+        verifier = new NumberVerifier();
+        verifier.setMin(new Integer(7));
+        verifier.setMax(new Integer(11));
+        verifier.setType(Integer.class);
+    }
+
+    public void testVerifyNumberGreaterThanMax() {
+        try {
+            verifier.verify(new Integer(15));
+            fail("Should not be able to verify a number greater than the max.");
+        } catch (VerifyException e) {
+            // success
+        }
+    }
+
+    public void testVerifyNumberLessThanMin() {
+        try {
+            verifier.verify(new Integer(1));
+            fail("Should not be able to verify a number less than the min.");
+        } catch (VerifyException e) {
+            // success
+        }
+    }
+
+    public void testVerifyNumberBetweenMaxAndMin() {
+        assertEquals(new Integer(7), verifier.getMin());
+        assertEquals(new Integer(11), verifier.getMax());
+        verifier.verify(new Integer(10));
+    }
+
+    public void testVerifyNumberWithDifferentTypes() {
+        assertEquals(Integer.class, verifier.getType());
+        try {
+            verifier.verify(new Long(100000));
+            fail("Should not be able to verify a type of number other than the type specified.");
+        } catch (VerifyException e) {
+            // success
+        }
+    }
+
+}

File src/test/com/opensymphony/module/propertyset/verifiers/StringVerifierTest.java

+package com.opensymphony.module.propertyset.verifiers;
+
+import junit.framework.TestCase;
+
+/**
+ * User: bbulger
+ * Date: May 22, 2004
+ */
+public class StringVerifierTest extends TestCase {
+
+    public void testVerifyStringLengthShorterThanMin() {
+        StringVerifier verifier = new StringVerifier();
+        verifier.setMinLength(10);
+        assertEquals(10, verifier.getMinLength());
+        try {
+            verifier.verify("value1");
+            fail("Should not be able to verify a string shorter than the min.");
+        } catch (VerifyException e) {
+            // success
+        }
+    }
+
+    public void testVerifyStringLengthLongerThanMax() {
+        StringVerifier verifier = new StringVerifier();
+        verifier.setMaxLength(10);
+        assertEquals(10, verifier.getMaxLength());
+        try {
+            verifier.verify("123456789012");
+            fail("Should not be able to verify a string longer than the max.");
+        } catch (VerifyException e) {
+            // success
+        }
+    }
+
+    public void testVerifyStringWithLengthBetweenMinAndMax() {
+        StringVerifier verifier = new StringVerifier(0, 100);
+        verifier.verify("this should work!");
+    }
+
+    public void testVerifyStringWithInvalidPrefix() {
+        StringVerifier verifier = new StringVerifier();
+        verifier.setPrefix("test");
+        try {
+            verifier.verify("username");
+            fail("Should not be able to verify a string with an invalid prefix.");
+        } catch (VerifyException e) {
+            // success
+        }
+    }
+
+    public void testVerifyStringWithValidPrefix() {
+        StringVerifier verifier = new StringVerifier();
+        verifier.setPrefix("test");
+        assertEquals("test", verifier.getPrefix());
+        verifier.verify("testInt");
+    }
+
+    public void testVerifyStringWithInvalidSuffix() {
+        StringVerifier verifier = new StringVerifier();
+        verifier.setSuffix("Int");
+        try {
+            verifier.verify("testDouble");
+            fail("Should not be able to verify string with an invalid suffix.");
+        } catch (VerifyException e) {
+            // success
+        }
+    }
+
+    public void testVerifyStringWithValidSuffix() {
+        StringVerifier verifier = new StringVerifier();
+        verifier.setSuffix("Int");
+        assertEquals("Int", verifier.getSuffix());
+        verifier.verify("testInt");
+    }
+
+    public void testVerifyStringContainsValidString() {
+        StringVerifier verifier = new StringVerifier();
+        verifier.setContains("thing");
+        assertEquals("thing", verifier.getContains());
+        verifier.verify("somethingelse");
+    }
+
+    public void testVerifyStringContainsInValidString() {
+        StringVerifier verifier = new StringVerifier();
+        verifier.setContains("thing");
+        try {
+            verifier.verify("tryagain");
+            fail("Should not be able to verify a string contains an invalid string.");
+        } catch (VerifyException e) {
+            // success
+        }
+    }
+
+    public void testVerifyStringInAllowableValues() {
+        String allowed[] = { "value1", "value2" };
+        StringVerifier verifier = new StringVerifier(allowed);
+        verifier.verify("value2");
+    }
+
+    public void testVerifyStringNotInAllowableValues() {
+        String allowed[] = { "value1", "value2" };
+        StringVerifier verifier = new StringVerifier();
+        verifier.setAllowableValues(allowed);
+        try {
+            verifier.verify("not there");
+            fail("Should not be able to verify a String that isn't in the allowed values.");
+        } catch (VerifyException e) {
+            // success
+        }
+    }
+
+}