1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sourceforge.domian.specification;
17
18
19 import junit.framework.TestCase;
20
21
22 public class RegularExpressionMatcherStringSpecificationTest extends TestCase {
23
24
25 static Specification<String> createRegularExpressionMatcherStringSpecification(final String regularExpression) {
26 return new RegularExpressionMatcherStringSpecification(regularExpression);
27 }
28
29
30 static Specification<String> like(final String pattern) {
31 return createRegularExpressionMatcherStringSpecification(pattern);
32 }
33
34
35 static Specification<String> matchesRegularExpression(final String pattern) {
36 return createRegularExpressionMatcherStringSpecification(pattern);
37 }
38
39
40 static Specification<String> isStringOfLength(final Integer length) {
41 return createRegularExpressionMatcherStringSpecification(".{" + length + "}");
42 }
43
44
45 static Specification<String> isStringLongerThan(final Integer length) {
46 return createRegularExpressionMatcherStringSpecification(".{" + (length + 1) + ",}");
47 }
48
49
50 static Specification<String> isStringShorterThan(final Integer length) {
51 return createRegularExpressionMatcherStringSpecification(".{0," + (length - 1) + "}");
52 }
53
54
55 static Specification<String> isStringOfIntegers() {
56 return createStringOfIntegersSpecification();
57 }
58
59
60 static Specification<String> createStringOfIntegersSpecification() {
61 return isInteger();
62 }
63
64
65 static Specification<String> isInteger() {
66 return isWholeNumber();
67 }
68
69
70 static Specification<String> isWholeNumber() {
71 return createRegularExpressionMatcherStringSpecification("\\d+");
72 }
73
74
75 public void testShouldNotAcceptNullAsValue() {
76 try {
77 new RegularExpressionMatcherStringSpecification(null);
78 fail("Should have thrown exception");
79
80 } catch (IllegalArgumentException e) {
81 String expectedMessage = "Regular expression parameter cannot be null";
82 String actualMessage = e.getMessage();
83 assertEquals(expectedMessage, actualMessage);
84 }
85 }
86
87
88 public void testShouldNotAcceptInvalidPattern() {
89 try {
90 new RegularExpressionMatcherStringSpecification("??");
91 fail("Should have thrown exception");
92
93
94 } catch (IllegalArgumentException e) {
95 String expectedMessageSubstring1 = "Illegal regex expression parameter: ";
96 String expectedMessageSubstring2 = "Dangling meta character '?' near index 0";
97 String actualMessage = e.getMessage();
98 assertTrue(actualMessage.contains(expectedMessageSubstring1));
99 assertTrue(actualMessage.contains(expectedMessageSubstring2));
100 }
101 }
102
103
104 public void testStringsOfLength() {
105 Specification<String> lengthOfThree = new RegularExpressionMatcherStringSpecification(".{3}");
106
107 assertFalse(lengthOfThree.isSatisfiedBy(null));
108 assertFalse(lengthOfThree.isSatisfiedBy(""));
109 assertFalse(lengthOfThree.isSatisfiedBy(" "));
110 assertTrue(lengthOfThree.isSatisfiedBy(" "));
111 assertTrue(lengthOfThree.isSatisfiedBy(" ; "));
112 assertTrue(lengthOfThree.isSatisfiedBy(" a"));
113 assertFalse(lengthOfThree.isSatisfiedBy(" "));
114 }
115
116
117 public void testStringsShorterThan() {
118 Specification<String> shorterThanThree = isStringShorterThan(3);
119
120 assertFalse(shorterThanThree.isSatisfiedBy(null));
121 assertTrue(shorterThanThree.isSatisfiedBy(""));
122 assertTrue(shorterThanThree.isSatisfiedBy("1"));
123 assertTrue(shorterThanThree.isSatisfiedBy("12"));
124 assertFalse(shorterThanThree.isSatisfiedBy("123"));
125 assertFalse(shorterThanThree.isSatisfiedBy("1 2 3 4 5 6 7 9"));
126 }
127
128
129 public void testRegularExpressionMatcherStringSpecificationBehaviour() {
130 Specification<String> stringWithLengthThree = isStringOfLength(3);
131 assertFalse(stringWithLengthThree.isSatisfiedBy(null));
132 assertFalse(stringWithLengthThree.isSatisfiedBy(""));
133 assertFalse(stringWithLengthThree.isSatisfiedBy(" "));
134 assertTrue(stringWithLengthThree.isSatisfiedBy(" "));
135 assertFalse(stringWithLengthThree.isSatisfiedBy(" "));
136
137 Specification<String> stringLongerThanThree = isStringLongerThan(3);
138 assertFalse(stringLongerThanThree.isSatisfiedBy(null));
139 assertFalse(stringLongerThanThree.isSatisfiedBy(""));
140 assertFalse(stringLongerThanThree.isSatisfiedBy(" "));
141 assertFalse(stringLongerThanThree.isSatisfiedBy(" "));
142 assertTrue(stringLongerThanThree.isSatisfiedBy(" "));
143
144 Specification<String> stringShorterThanThree = isStringShorterThan(3);
145 assertFalse(stringShorterThanThree.isSatisfiedBy(null));
146 assertTrue(stringShorterThanThree.isSatisfiedBy(""));
147 assertTrue(stringShorterThanThree.isSatisfiedBy(" "));
148 assertFalse(stringShorterThanThree.isSatisfiedBy(" "));
149 assertFalse(stringShorterThanThree.isSatisfiedBy(" "));
150
151 Specification<String> stringOfIntegers = isInteger();
152 assertFalse(stringOfIntegers.isSatisfiedBy(null));
153 assertFalse(stringOfIntegers.isSatisfiedBy(""));
154 assertFalse(stringOfIntegers.isSatisfiedBy(" "));
155 assertFalse(stringOfIntegers.isSatisfiedBy(" "));
156 assertFalse(stringOfIntegers.isSatisfiedBy(" "));
157 assertTrue(stringOfIntegers.isSatisfiedBy("0"));
158 assertTrue(stringOfIntegers.isSatisfiedBy("023456789"));
159 assertFalse(stringOfIntegers.isSatisfiedBy("1.000.000"));
160 assertFalse(stringOfIntegers.isSatisfiedBy("3,14"));
161 }
162 }