View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.trinidad.validator;
20  
21  import javax.faces.validator.ValidatorException;
22  import javax.faces.component.UIComponent;
23  
24  import junit.framework.Test;
25  import junit.framework.TestSuite;
26  
27  import org.apache.myfaces.trinidad.validator.RegExpValidator;
28  import org.apache.myfaces.trinidadbuild.test.MockUIComponentWrapper;
29  import org.jmock.Mock;
30  
31  /**
32   * Unit tests for RegExpValidator
33   *
34   */
35  public class RegExpValidatorTest extends ValidatorTestCase
36  {
37    public RegExpValidatorTest(String testName)
38    {
39      super(testName);
40    }
41    
42    @Override
43    protected void setUp() throws Exception
44    {
45      super.setUp();
46    }
47    
48    @Override
49    protected void tearDown() throws Exception
50    {
51      super.tearDown();
52    }
53    
54    public static Test suite()
55    {
56      return new TestSuite(RegExpValidatorTest.class);
57    }
58  
59    /**
60     * Tests that null returns immediately.
61     *
62     * @throws ValidatorException  when test fails
63     */
64    public void testNull() throws ValidatorException
65    {
66      Mock mock = buildMockUIComponent();
67      UIComponent component = (UIComponent) mock.proxy();
68      MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
69      RegExpValidator validator = new RegExpValidator();
70  
71      doTestNull(facesContext, wrapper, validator);
72    }
73  
74    /**
75     * Test when context is set to null
76     */
77    public void testNullContext()
78    {
79      Mock mock = buildMockUIComponent();
80      UIComponent component = (UIComponent) mock.proxy();
81      MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
82      RegExpValidator validator = new RegExpValidator();
83  
84      doTestNullContext(wrapper, validator);
85    }
86  
87    public void testNullComponent()
88    {
89      RegExpValidator validator = new RegExpValidator();
90  
91      doTestNullComponent(facesContext, validator);
92    }
93  
94    /**
95     * Tests that non String objects throw a ValidationException.
96     */
97    public void testNotString()
98    {
99      doTestIsNotString(new RegExpValidator());
100   }
101 
102   /**
103    * Test that pattern when not set throws null pointer exceptin
104    */
105   public void testPatternNotSet()
106   {
107     // since the pattern has not been set it will be null
108     // let us push some arbitary value
109     Mock mock = mock(UIComponent.class); 
110     UIComponent component = (UIComponent) mock.proxy();
111 
112     try
113     {
114       RegExpValidator validator = new RegExpValidator();
115       validator.validate(facesContext, component, "someValue");
116       // test fails if it is here
117 
118       fail("Expected Null pointer exception");
119     }
120     catch (NullPointerException npe)
121     {
122       // suppress it - this is as expected
123     }
124     mock.verify();
125   }
126 
127   public void testWithPattern()
128   {
129     Mock mock = mock(UIComponent.class); 
130     UIComponent component = (UIComponent) mock.proxy();
131 
132     RegExpValidator validator = new RegExpValidator();
133     validator.setPattern("[1-9]|[1-9][0-9]|[1-9][0-9][0-9]");
134     validator.validate(facesContext, component, "15");
135 
136     mock.verify();
137   }
138 
139   /**
140    * Test that pattern when set to "" should fail validation
141    */
142   public void testBlankValueOnPattern()
143   {
144     // some very basic sanity test
145     Mock mock = buildMockUIComponent();
146     UIComponent component = (UIComponent) mock.proxy();
147     MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
148     setMockLabelForComponent(wrapper);
149 
150     try
151     {
152       RegExpValidator validator = new RegExpValidator();
153       String value = "999999";
154       validator.setPattern("");
155       validator.validate(facesContext, component, value);
156       fail("Expected ValidatorException");
157     }
158     catch (ValidatorException ve)
159     {
160       // if exception then fine.
161     }
162 
163     mock.verify();
164   }
165 
166   /**
167    * Simple test case which is expected to pass
168    * @todo need to add many test cases - add string to the values and
169    *       patterns to patterns array.
170    *
171    */
172   public void testSanitySuccess()
173   {
174     //some very basic sanity test
175     //
176     RegExpValidator validator = new RegExpValidator();
177     Mock mock = buildMockUIComponent();
178     UIComponent component = (UIComponent) mock.proxy();
179     MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
180 
181     String values[]   = {"9123456","9x"};
182     String patterns[] = {"[0-9]*","[9][x]"};
183     for (int i = 0; i < values.length ; i++)
184     {
185       validator.setPattern(patterns[i]);
186       doTestValidate(validator, facesContext, wrapper, values[i]);
187     }
188   }
189 
190   /**
191    * Tests that dates after the date range cause a ValidationException.
192    */
193   public void testStateHolderSaveRestore()
194   {
195     RegExpValidator validator = new RegExpValidator();
196     Mock mock = buildMockUIComponent();
197     UIComponent component = (UIComponent) mock.proxy();
198     MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
199 
200     validator.setPattern("TestSaveRestore");
201     validator.setMessageDetailNoMatch("\"{0}\" in \"{1}\" failed!! {4}");
202     RegExpValidator restoreValidator = new  RegExpValidator();
203 
204     doTestStateHolderSaveRestore(validator, restoreValidator,
205                                  facesContext, wrapper);
206   }
207 
208   /**
209    * Test for equality of validators
210    */
211   public void testIsEqual()
212   {
213     RegExpValidator validator = new RegExpValidator();
214     RegExpValidator otherValidator = new RegExpValidator();
215     doTestEquals(validator, otherValidator, true);
216     assertEquals(validator.hashCode(), otherValidator.hashCode());
217 
218     validator.setPattern("[0-9]");
219     validator.setMessageDetailNoMatch("\"{0}\" in \"{1}\" failed!! {4}");
220     otherValidator.setPattern("[0-9]");
221     otherValidator.setMessageDetailNoMatch("\"{0}\" in \"{1}\" failed!! {4}");
222     doTestEquals(validator, otherValidator, true);
223     assertEquals(validator.hashCode(), otherValidator.hashCode());
224 
225     otherValidator.setPattern(null);
226     doTestEquals(validator, otherValidator, false);
227     assertEquals(false, (validator.hashCode() == otherValidator.hashCode()));
228   }
229 
230   public void testCustomMessageIsSet()
231   {
232     Mock mock = buildMockUIComponent();
233     UIComponent component = (UIComponent) mock.proxy();
234     MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
235     setMockLabelForComponent(wrapper);
236     RegExpValidator validator = new RegExpValidator();
237 
238     validator.setPattern("[0-9]*");
239     validator.setMessageDetailNoMatch("\"{0}\" in \"{1}\" failed!! {4}");
240     //some very basic sanity test
241 
242     try
243     {
244       validator.validate(facesContext, component, "9123456");
245     }
246     catch (ValidatorException ve)
247     {
248       String msg = ve.getFacesMessage().getDetail();
249       assertEquals(msg, "\"four\" in \"label\" failed!! [0-9]*");
250     }
251   }
252 
253 }
254   //////////////////////////////////////////////////////////////////////////////
255   //                             MOCK OBJECTS
256   // 1. Get a MockControl for the interface we would like to simulate
257   // 2. get the MockObject from MockControl
258   // 3. specify the behaviour of the Mock Object (record state)
259   // 4. activate the MockObject via the control  (replay state)
260   //
261   //////////////////////////////////////////////////////////////////////////////