KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > util > typevalidator > test > BaseTypeValidatorTest


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.util.typevalidator.test;
8
9
10 import junit.framework.TestCase;
11
12 import com.inversoft.beans.BeanException;
13 import com.inversoft.error.BasicError;
14 import com.inversoft.error.PropertyError;
15 import com.inversoft.util.typevalidator.EmailTypeValidator;
16
17
18 /**
19  * <p>
20  * This class tests the BaseTypeValidator via the
21  * EmailTypeValidator.
22  * </p>
23  *
24  * @author bpontarelli
25  * @version 2.0
26  * @since 2.0
27  */

28 public class BaseTypeValidatorTest extends TestCase {
29
30     /**
31      * Constructs a new <code>BaseTypeValidatorTest</code>
32      *
33      * @param name The name of the test case being run
34      */

35     public BaseTypeValidatorTest(String JavaDoc name) {
36         super(name);
37     }
38
39
40     /**
41      * Tests all the success cases.
42      */

43     public void testSuccess() {
44         BasicError be = null;
45         EmailTypeValidator tv = new EmailTypeValidator();
46
47         be = tv.validate("brian@yahoo.com", null, "error {0}", new Object JavaDoc[]{"foo"});
48         assertNull(be);
49
50         be = tv.validate("brian@yahoo.com", null,
51             "com.inversoft.util.typevalidator.test.bundle", "error", null, null,
52             this);
53         assertNull(be);
54
55         be = tv.validate("email", "brian@inversoft.com", null, "error", null);
56         assertNull(be);
57
58         be = tv.validate("email", "good@email.address.com", null,
59             "com.inversoft.util.typevalidator.test.bundle", "error", null, null,
60             this);
61         assertNull(be);
62
63         try {
64             be = tv.fetchAndValidate("goodEmail", new Bean(), null, "error", null);
65             assertNull(be);
66         } catch (BeanException e) {
67             fail(e.toString());
68         }
69
70         try {
71             be = tv.fetchAndValidate("goodEmail", new Bean(), null,
72                 "com.inversoft.util.typevalidator.test.bundle", "error", null, null,
73                 this);
74             assertNull(be);
75         } catch (BeanException e) {
76             fail(e.toString());
77         }
78     }
79
80     /**
81      * Tests all the failure cases.
82      */

83     public void testFailures() {
84         BasicError be = null;
85         EmailTypeValidator tv = new EmailTypeValidator();
86
87         be = tv.validate("@yahoo.com", null, "error {0}", new Object JavaDoc[]{"foo"});
88         assertNotNull(be);
89         assertEquals("error foo", be.getMessage());
90
91         be = tv.validate("brian yahoo.com", null,
92             "com.inversoft.util.typevalidator.test.bundle", "error", null, null,
93             this);
94         assertNotNull(be);
95         assertEquals("Some error", be.getMessage());
96
97         be = tv.validate("email", "bad email.address.com", null, "error", null);
98         assertNotNull(be);
99         assertEquals("error", be.getMessage());
100         assertEquals("email", ((PropertyError) be).getProperty());
101
102         be = tv.validate("email", "bad email.address.com", null,
103             "com.inversoft.util.typevalidator.test.bundle", "error", null,
104             null, this);
105         assertNotNull(be);
106         assertEquals("Some error", be.getMessage());
107         assertEquals("email", ((PropertyError) be).getProperty());
108
109         try {
110             be = tv.fetchAndValidate("badEmail", new Bean(), null, "error", null);
111             assertNotNull(be);
112             assertEquals("error", be.getMessage());
113             assertEquals("badEmail", ((PropertyError) be).getProperty());
114         } catch (BeanException e) {
115             fail(e.toString());
116         }
117
118         try {
119             be = tv.fetchAndValidate("badEmail", new Bean(), null,
120                 "com.inversoft.util.typevalidator.test.bundle", "error", null, null,
121                 this);
122             assertNotNull(be);
123             assertEquals("Some error", be.getMessage());
124             assertEquals("badEmail", ((PropertyError) be).getProperty());
125         } catch (BeanException e) {
126             fail(e.toString());
127         }
128     }
129 }
Popular Tags