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.component.UIComponent;
22  import javax.faces.validator.ValidatorException;
23  
24  import junit.framework.Test;
25  import junit.framework.TestSuite;
26  
27  import org.apache.myfaces.trinidadbuild.test.MockUIComponentWrapper;
28  import org.jmock.Mock;
29  
30  /**
31   * Unit tests for DoubleRangeValidator
32   *
33   */
34  public class DoubleRangeValidatorTest extends ValidatorTestCase
35  {
36    public DoubleRangeValidatorTest(String testName)
37    {
38      super(testName);
39    }
40    
41    @Override
42    protected void setUp() throws Exception
43    {
44      super.setUp();
45    }
46    
47    @Override
48    protected void tearDown() throws Exception
49    {
50      super.tearDown();
51    }
52    
53    public static Test suite()
54    {
55      return new TestSuite(DoubleRangeValidatorTest.class);
56    }
57  
58    /**
59     * Tests that null returns immediately.
60     *
61     * @throws ValidatorException  when test fails
62     */
63    public void testNull() throws ValidatorException
64    {
65      Mock mock = buildMockUIComponent();
66      UIComponent component = (UIComponent) mock.proxy();
67      MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
68      DoubleRangeValidator validator = new DoubleRangeValidator();
69  
70      doTestNull(facesContext, wrapper, validator);
71    }
72  
73    /**
74     * Test when context is set to null
75     */
76    public void testNullContext()
77    {
78      Mock mock = buildMockUIComponent();
79      UIComponent component = (UIComponent) mock.proxy();
80      MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
81      DoubleRangeValidator validator = new DoubleRangeValidator();
82  
83      doTestNullContext(wrapper, validator);
84    }
85  
86    public void testNullComponent()
87    {
88      DoubleRangeValidator validator = new DoubleRangeValidator();
89  
90      doTestNullComponent(facesContext, validator);
91    }
92  
93    public void testTooLarge()
94    {
95      // since the pattern has not been set it will be null
96      // let us push some arbitary value
97      Mock mock = mock(UIComponent.class); 
98      UIComponent component = (UIComponent) mock.proxy();
99      MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
100     setMockLabelForComponent(wrapper);
101 
102     try
103     {
104       DoubleRangeValidator validator = new DoubleRangeValidator();
105       validator.setMaximum(100);
106       validator.validate(facesContext, component, 1000);
107       // test fails if it is here
108 
109       fail("Expected Null pointer exception");
110     }
111     catch (ValidatorException ve)
112     {
113       // suppress it - this is as expected
114     }
115     mock.verify();
116   }
117 
118   public void testWrongType()
119   {
120     // since the pattern has not been set it will be null
121     // let us push some arbitary value
122     Mock mock = mock(UIComponent.class); 
123     UIComponent component = (UIComponent) mock.proxy();
124     MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
125     setMockLabelForComponent(wrapper);
126 
127     try
128     {
129       DoubleRangeValidator validator = new DoubleRangeValidator();
130       validator.setMaximum(2);
131       validator.validate(facesContext, component, "thisShouldFail");
132       // test fails if it is here
133 
134       fail("Expected Null pointer exception");
135     }
136     catch (ValidatorException ve)
137     {
138       // suppress it - this is as expected
139     }
140     mock.verify();
141   }
142 
143   public void testExactFailure()
144   {
145     // some very basic sanity test
146     Mock mock = buildMockUIComponent();
147     UIComponent component = (UIComponent) mock.proxy();
148     MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
149     setMockLabelForComponent(wrapper);
150 
151     try
152     {
153       DoubleRangeValidator validator = new DoubleRangeValidator();
154       double value = 20d;
155       validator.setMinimum(2);
156       validator.setMaximum(2);
157       validator.validate(facesContext, component, value);
158       fail("Expected ValidatorException for exact");
159     }
160     catch (ValidatorException ve)
161     {
162       // if exception then fine.
163     }
164 
165     mock.verify();
166   }
167 
168   public void testSanitySuccess()
169   {
170     //some very basic sanity test
171     //
172     DoubleRangeValidator validator = new DoubleRangeValidator();
173     Mock mock = buildMockUIComponent();
174     UIComponent component = (UIComponent) mock.proxy();
175     MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
176 
177     Double values[]   = {200d,500d};
178     validator.setMinimum(2);
179     for (int i = 0; i < values.length ; i++)
180     {
181       doTestValidate(validator, facesContext, wrapper, values[i]);
182     }
183   }
184 
185   public void testStringBasedValues()
186   {
187     DoubleRangeValidator validator = new DoubleRangeValidator();
188     Mock mock = buildMockUIComponent();
189     UIComponent component = (UIComponent) mock.proxy();
190     MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
191 
192     String values[]   = {"200.05","500.00"};
193     validator.setMinimum(200);
194     for (int i = 0; i < values.length ; i++)
195     {
196       doTestValidate(validator, facesContext, wrapper, values[i]);
197     }
198   }
199 
200   public void testStateHolderSaveRestore()
201   {
202     DoubleRangeValidator validator = new DoubleRangeValidator();
203     Mock mock = buildMockUIComponent();
204     UIComponent component = (UIComponent) mock.proxy();
205     MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
206 
207     validator.setMaximum(5);
208     validator.setMessageDetailMaximum("Validation failed");
209     DoubleRangeValidator restoreValidator = new  DoubleRangeValidator();
210 
211     doTestStateHolderSaveRestore(validator, restoreValidator,
212                                  facesContext, wrapper);
213   }
214 }
215   //////////////////////////////////////////////////////////////////////////////
216   //                             MOCK OBJECTS
217   // 1. Get a MockControl for the interface we would like to simulate
218   // 2. get the MockObject from MockControl
219   // 3. specify the behaviour of the Mock Object (record state)
220   // 4. activate the MockObject via the control  (replay state)
221   //
222   //////////////////////////////////////////////////////////////////////////////