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   /**
128    * Test that pattern when set to "" should fail validation
129    */
130   public void testBlankValueOnPattern()
131   {
132     // some very basic sanity test
133     Mock mock = buildMockUIComponent();
134     UIComponent component = (UIComponent) mock.proxy();
135     MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
136     setMockLabelForComponent(wrapper);
137 
138     try
139     {
140       RegExpValidator validator = new RegExpValidator();
141       String value = "999999";
142       validator.setPattern("");
143       validator.validate(facesContext, component, value);
144       fail("Expected ValidatorException");
145     }
146     catch (ValidatorException ve)
147     {
148       // if exception then fine.
149     }
150 
151     mock.verify();
152   }
153 
154   /**
155    * Simple test case which is expected to pass
156    * @todo need to add many test cases - add string to the values and
157    *       patterns to patterns array.
158    *
159    */
160   public void testSanitySuccess()
161   {
162     //some very basic sanity test
163     //
164     RegExpValidator validator = new RegExpValidator();
165     Mock mock = buildMockUIComponent();
166     UIComponent component = (UIComponent) mock.proxy();
167     MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
168 
169     String values[]   = {"9123456","9x"};
170     String patterns[] = {"[0-9]*","[9][x]"};
171     for (int i = 0; i < values.length ; i++)
172     {
173       validator.setPattern(patterns[i]);
174       doTestValidate(validator, facesContext, wrapper, values[i]);
175     }
176   }
177 
178   /**
179    * Tests that dates after the date range cause a ValidationException.
180    */
181   public void testStateHolderSaveRestore()
182   {
183     RegExpValidator validator = new RegExpValidator();
184     Mock mock = buildMockUIComponent();
185     UIComponent component = (UIComponent) mock.proxy();
186     MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
187 
188     validator.setPattern("TestSaveRestore");
189     validator.setMessageDetailNoMatch("\"{0}\" in \"{1}\" failed!! {4}");
190     RegExpValidator restoreValidator = new  RegExpValidator();
191 
192     doTestStateHolderSaveRestore(validator, restoreValidator,
193                                  facesContext, wrapper);
194   }
195 
196   /**
197    * Test for equality of validators
198    */
199   public void testIsEqual()
200   {
201     RegExpValidator validator = new RegExpValidator();
202     RegExpValidator otherValidator = new RegExpValidator();
203     doTestEquals(validator, otherValidator, true);
204     assertEquals(validator.hashCode(), otherValidator.hashCode());
205 
206     validator.setPattern("[0-9]");
207     validator.setMessageDetailNoMatch("\"{0}\" in \"{1}\" failed!! {4}");
208     otherValidator.setPattern("[0-9]");
209     otherValidator.setMessageDetailNoMatch("\"{0}\" in \"{1}\" failed!! {4}");
210     doTestEquals(validator, otherValidator, true);
211     assertEquals(validator.hashCode(), otherValidator.hashCode());
212 
213     otherValidator.setPattern(null);
214     doTestEquals(validator, otherValidator, false);
215     assertEquals(false, (validator.hashCode() == otherValidator.hashCode()));
216   }
217 
218   public void testCustomMessageIsSet()
219   {
220     Mock mock = buildMockUIComponent();
221     UIComponent component = (UIComponent) mock.proxy();
222     MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
223     setMockLabelForComponent(wrapper);
224     RegExpValidator validator = new RegExpValidator();
225 
226     validator.setPattern("[0-9]*");
227     validator.setMessageDetailNoMatch("\"{0}\" in \"{1}\" failed!! {4}");
228     //some very basic sanity test
229 
230     try
231     {
232       validator.validate(facesContext, component, "9123456");
233     }
234     catch (ValidatorException ve)
235     {
236       String msg = ve.getFacesMessage().getDetail();
237       assertEquals(msg, "\"four\" in \"label\" failed!! [0-9]*");
238     }
239   }
240 
241 }
242   //////////////////////////////////////////////////////////////////////////////
243   //                             MOCK OBJECTS
244   // 1. Get a MockControl for the interface we would like to simulate
245   // 2. get the MockObject from MockControl
246   // 3. specify the behaviour of the Mock Object (record state)
247   // 4. activate the MockObject via the control  (replay state)
248   //
249   //////////////////////////////////////////////////////////////////////////////