1   /*
2    * Copyright 2006-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sourceforge.domian.specification;
17  
18  
19  import junit.framework.TestCase;
20  
21  
22  public class RegularExpressionMatcherStringSpecificationTest extends TestCase {
23  
24      /* SpecificationFactory candidate */
25      static Specification<String> createRegularExpressionMatcherStringSpecification(final String regularExpression) {
26          return new RegularExpressionMatcherStringSpecification(regularExpression);
27      }
28  
29      /* SpecificationFactory candidate */
30      static Specification<String> like(final String pattern) {
31          return createRegularExpressionMatcherStringSpecification(pattern);
32      }
33  
34      /* SpecificationFactory candidate */
35      static Specification<String> matchesRegularExpression(final String pattern) {
36          return createRegularExpressionMatcherStringSpecification(pattern);
37      }
38  
39      /* SpecificationFactory candidate */
40      static Specification<String> isStringOfLength(final Integer length) {
41          return createRegularExpressionMatcherStringSpecification(".{" + length + "}");
42      }
43  
44      /* SpecificationFactory candidate */
45      static Specification<String> isStringLongerThan(final Integer length) {
46          return createRegularExpressionMatcherStringSpecification(".{" + (length + 1) + ",}");
47      }
48  
49      /* SpecificationFactory candidate */
50      static Specification<String> isStringShorterThan(final Integer length) {
51          return createRegularExpressionMatcherStringSpecification(".{0," + (length - 1) + "}");
52      }
53  
54      /* SpecificationFactory candidate */
55      static Specification<String> isStringOfIntegers() {
56          return createStringOfIntegersSpecification();
57      }
58  
59      /* SpecificationFactory candidate */
60      static Specification<String> createStringOfIntegersSpecification() {
61          return isInteger();
62      }
63  
64      /* SpecificationFactory candidate */
65      static Specification<String> isInteger() {
66          return isWholeNumber();
67      }
68  
69      /* SpecificationFactory candidate */
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              //} catch (PatternSyntaxException e) {
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 }