001: package test.org.jreform.criteria;
002:
003: import static org.jreform.criteria.Criteria.accept;
004: import static org.jreform.criteria.Criteria.acceptString;
005: import static org.jreform.criteria.Criteria.and;
006: import static org.jreform.criteria.Criteria.emailAddress;
007: import static org.jreform.criteria.Criteria.exactLength;
008: import static org.jreform.criteria.Criteria.length;
009: import static org.jreform.criteria.Criteria.max;
010: import static org.jreform.criteria.Criteria.maxLength;
011: import static org.jreform.criteria.Criteria.min;
012: import static org.jreform.criteria.Criteria.minLength;
013: import static org.jreform.criteria.Criteria.or;
014: import static org.jreform.criteria.Criteria.postcodeCA;
015: import static org.jreform.criteria.Criteria.range;
016: import static org.jreform.criteria.Criteria.startsWith;
017: import static org.jreform.criteria.Criteria.zipcode;
018: import junit.framework.TestCase;
019:
020: @SuppressWarnings("unchecked")
021: public class CriteriaTest extends TestCase {
022: public void testMin() {
023: assertFalse(min(10).isSatisfied(3));
024: assertTrue(min(10).isSatisfied(11));
025: assertTrue(min(10).isSatisfied(10));
026: }
027:
028: public void testMax() {
029: assertFalse(max(10).isSatisfied(15));
030: assertTrue(max(10).isSatisfied(7));
031: assertTrue(max(10).isSatisfied(10));
032: }
033:
034: public void testMinLength() {
035: assertFalse(minLength(5).isSatisfied("abc"));
036: assertTrue(minLength(5).isSatisfied("abcde"));
037: assertTrue(minLength(5).isSatisfied("abcdefgh"));
038: }
039:
040: public void testMaxLength() {
041: assertFalse(maxLength(5).isSatisfied("abcdefgh"));
042: assertTrue(maxLength(5).isSatisfied("abcde"));
043: assertTrue(maxLength(5).isSatisfied("a"));
044: }
045:
046: public void testExactLength() {
047: assertFalse(exactLength(5).isSatisfied("abc"));
048: assertTrue(exactLength(5).isSatisfied("abcde"));
049: }
050:
051: public void testLenght() {
052: assertTrue(length(1, 5).isSatisfied("a"));
053: assertTrue(length(1, 5).isSatisfied("abcde"));
054: assertTrue(length(0, 0).isSatisfied(""));
055:
056: assertFalse(length(1, 5).isSatisfied(""));
057: assertFalse(length(1, 5).isSatisfied("abcdef"));
058: }
059:
060: public void testRange() {
061: assertTrue(range(5, 10).isSatisfied(5));
062: assertTrue(range(5, 10).isSatisfied(7));
063: assertTrue(range(5, 10).isSatisfied(10));
064:
065: assertFalse(range(5, 10).isSatisfied(4));
066: assertFalse(range(5, 10).isSatisfied(11));
067: }
068:
069: public void testAccept() {
070: assertTrue(accept(-3, 7, 145).isSatisfied(-3));
071: assertTrue(accept(-3, 7, 145).isSatisfied(7));
072: assertTrue(accept(-3, 7, 145).isSatisfied(145));
073: assertFalse(accept(-3, 7, 145).isSatisfied(-2));
074: assertFalse(accept(-3, 7, 145).isSatisfied(0));
075: assertFalse(accept(-3, 7, 145).isSatisfied(8));
076:
077: assertTrue(accept('A', 'B', 'C').isSatisfied('A'));
078: assertTrue(accept('A', 'B', 'C').isSatisfied('B'));
079: assertTrue(accept('A', 'B', 'C').isSatisfied('C'));
080: assertFalse(accept('A', 'B', 'C').isSatisfied('a'));
081: }
082:
083: public void testAcceptString() {
084: assertFalse(acceptString("A", "B", "C").isSatisfied("a"));
085: assertFalse(acceptString("A", "B", "C").isSatisfied("b"));
086: assertTrue(acceptString("A", "B", "C").isSatisfied("B"));
087:
088: assertTrue(acceptString("A", "B", "C").ignoreCase()
089: .isSatisfied("A"));
090: assertTrue(acceptString("A", "B", "C").ignoreCase()
091: .isSatisfied("B"));
092: assertTrue(acceptString("A", "B", "C").ignoreCase()
093: .isSatisfied("a"));
094: assertTrue(acceptString("A", "B", "C").ignoreCase()
095: .isSatisfied("b"));
096: }
097:
098: public void testStartsWith() {
099: assertTrue(startsWith("moo").isSatisfied("moo"));
100: assertTrue(startsWith("moo").isSatisfied("moose"));
101:
102: assertFalse(startsWith("moo").isSatisfied("mo"));
103: assertFalse(startsWith("moo").isSatisfied("Moose"));
104: }
105:
106: public void testOr() {
107: assertTrue(or(acceptString("a", "b"), acceptString("x", "y"))
108: .isSatisfied("a"));
109: assertTrue(or(acceptString("a", "b"), acceptString("x", "y"))
110: .isSatisfied("b"));
111: assertTrue(or(acceptString("a", "b"), acceptString("x", "y"))
112: .isSatisfied("x"));
113: assertTrue(or(acceptString("a", "b"), acceptString("x", "y"))
114: .isSatisfied("y"));
115:
116: assertFalse(or(acceptString("a", "b"), acceptString("x", "y"))
117: .isSatisfied("R"));
118: assertFalse(or(acceptString("a", "b"), acceptString("x", "y"))
119: .isSatisfied("z"));
120: assertFalse(or(acceptString("a", "b"), acceptString("x", "y"))
121: .isSatisfied("n"));
122:
123: try {
124: or(min(0));
125: fail("and(...) requires at least two criterion arguments");
126: } catch (IllegalArgumentException ex) { /* empty */
127: }
128: }
129:
130: public void testAnd() {
131: assertTrue(and(min(200), max(300)).isSatisfied(200));
132: assertTrue(and(min(200), max(300)).isSatisfied(250));
133: assertTrue(and(min(200), max(300)).isSatisfied(300));
134:
135: assertFalse(and(min(200), max(300)).isSatisfied(5));
136: assertFalse(and(min(200), max(300)).isSatisfied(301));
137: assertFalse(and(min(200), max(300)).isSatisfied(-5));
138:
139: try {
140: and(min(0));
141: fail("and(...) requires at least two criterion arguments");
142: } catch (IllegalArgumentException ex) { /* empty */
143: }
144: }
145:
146: public void testEmail() {
147: assertTrue(emailAddress().isSatisfied("xxxx@domain.xx"));
148: assertTrue(emailAddress().isSatisfied("xxxx@domain.xx.xx"));
149: assertTrue(emailAddress().isSatisfied(
150: "xxxx@subdomain.domain.xx"));
151: assertTrue(emailAddress().isSatisfied(
152: "xxxx.x.x@subdomain.domain.xx"));
153: assertTrue(emailAddress().isSatisfied(
154: "xxx_-#%$&^*(&*xx.x.x@subdomain.domain.xx.xxxxxxx"));
155: }
156:
157: public void testZipcode() {
158: assertTrue(zipcode().isSatisfied("12345"));
159: assertTrue(zipcode().isSatisfied("12345-2345"));
160:
161: assertFalse(zipcode().isSatisfied("1234"));
162: assertFalse(zipcode().isSatisfied("123456"));
163: assertFalse(zipcode().isSatisfied("1234534-2345"));
164: assertFalse(zipcode().isSatisfied("1234X"));
165: assertFalse(zipcode().isSatisfied("-12345"));
166: assertFalse(zipcode().isSatisfied("x12345"));
167: assertFalse(zipcode().isSatisfied("abcde"));
168: }
169:
170: public void testPostcodeCA() {
171: assertTrue(postcodeCA().isSatisfied("V1V 1V1"));
172: assertTrue(postcodeCA().isSatisfied("v1v 1v1"));
173:
174: assertFalse(postcodeCA().isSatisfied("1V1V 1V1"));
175: assertFalse(postcodeCA().isSatisfied("V1V 1V11"));
176:
177: assertFalse(postcodeCA().isSatisfied("V1V 1V1"));
178: assertFalse(postcodeCA().isSatisfied("V1V1V1"));
179: assertFalse(postcodeCA().isSatisfied("VVV 1V1"));
180: assertFalse(postcodeCA().isSatisfied("1A1 1V1"));
181:
182: assertFalse(postcodeCA().isSatisfied("Z1Z 1Z1"));
183: }
184:
185: }
|