1   package org.apache.turbine.services.intake;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Calendar;
23  import java.util.Date;
24  
25  import junit.framework.TestSuite;
26  
27  import org.apache.turbine.services.ServiceManager;
28  import org.apache.turbine.services.TurbineServices;
29  import org.apache.turbine.services.intake.model.Field;
30  import org.apache.turbine.services.intake.model.Group;
31  import org.apache.turbine.services.intake.validator.BooleanValidator;
32  import org.apache.turbine.services.intake.validator.DateRangeValidator;
33  import org.apache.turbine.services.intake.validator.IntegerRangeValidator;
34  import org.apache.turbine.test.BaseTurbineTest;
35  import org.apache.turbine.util.parser.DefaultParameterParser;
36  import org.apache.turbine.util.parser.ParameterParser;
37  
38  
39  public class IntakeServiceTest extends BaseTurbineTest
40  {
41  
42      Group booleanTestGroup = null;
43      Group rangeTestGroup = null;
44      Group integerRangeTestGroup = null;
45      Group requiredFalseTestGroup = null;
46      Group requiredTrueTestGroup = null;
47  
48      public IntakeServiceTest(String name) throws Exception
49      {
50          super(name, "conf/test/TurbineResourcesWithIntake.properties");
51  
52          ServiceManager serviceManager = TurbineServices.getInstance();
53          IntakeService intakeService = (IntakeService) serviceManager.getService(IntakeService.SERVICE_NAME);
54          booleanTestGroup = intakeService.getGroup("BooleanTest");
55          rangeTestGroup = intakeService.getGroup("DateRangeTest");
56          integerRangeTestGroup = intakeService.getGroup("IntRangeTest");
57          requiredFalseTestGroup = intakeService.getGroup("RequiredFalseTest");
58          requiredTrueTestGroup = intakeService.getGroup("RequiredTrueTest");
59      }
60  
61      public void testEmptyBooleanField() throws IntakeException
62      {
63          Field booleanField = booleanTestGroup.get("EmptyBooleanTestField");
64          assertTrue("The Default Validator of an intake Field type boolean should be BooleanValidator", (booleanField.getValidator() instanceof BooleanValidator));
65          assertFalse("An Empty intake Field type boolean should not be required", booleanField.isRequired());
66      }
67  
68      public void testBooleanField() throws IntakeException
69      {
70          Field booleanField = booleanTestGroup.get("BooleanTestField");
71          assertTrue("The Default Validator of an intake Field type boolean should be BooleanValidator", (booleanField.getValidator() instanceof BooleanValidator));
72          assertFalse("An intake Field type boolean, which is not required, should not be required", booleanField.isRequired());
73      }
74  
75      public void testRequiredBooleanField() throws IntakeException
76      {
77          Field booleanField = booleanTestGroup.get("RequiredBooleanTestField");
78          assertTrue("The Default Validator of an intake Field type boolean should be BooleanValidator", (booleanField.getValidator() instanceof BooleanValidator));
79          assertTrue("An intake Field type boolean, which is required, should be required", booleanField.isRequired());
80      }
81  
82      public void testDateRangeValidator() throws IntakeException
83      {
84          ParameterParser pp = new DefaultParameterParser();
85          pp.add("rt_0dmin", "05/11/2007");
86          pp.add("rt_0dmax", "05/12/2007");
87          pp.add("rt_0dmax2", "05/11/2007");
88          rangeTestGroup.init(Group.NEW, pp);
89  
90          Field dmax = rangeTestGroup.get("DateMax");
91          Field dmax2 = rangeTestGroup.get("DateMax2");
92  
93          assertTrue("The Validator of the field DateMax should be a DateRangeValidator", (dmax.getValidator() instanceof DateRangeValidator));
94          assertTrue("The date range should be valid", dmax.isValid());
95          assertFalse("The date range should not be valid", dmax2.isValid());
96      }
97  
98      public void testIntegerRangeValidator() throws IntakeException
99      {
100         ParameterParser pp = new DefaultParameterParser();
101         pp.add("irt_0imin", "1");
102         pp.add("irt_0imax", "3");
103         pp.add("irt_0imax2", "2");
104         integerRangeTestGroup.init(Group.NEW, pp);
105 
106         Field imax = integerRangeTestGroup.get("IntMax");
107         Field imax2 = integerRangeTestGroup.get("IntMax2");
108 
109         assertTrue("The Validator of the field IntMax should be an IntegerRangeValidator", (imax.getValidator() instanceof IntegerRangeValidator));
110         assertTrue("The integer range should be valid", imax.isValid());
111         assertFalse("The integer range should not be valid", imax2.isValid());
112     }
113 
114     /***
115      * This test verifies that an intake field returns true for isSet() even
116      * when an empty field is submitted.
117      *
118      * @throws IntakeException
119      */
120     public void testRequiredFalse() throws IntakeException
121     {
122         ParameterParser pp = new DefaultParameterParser();
123         pp.add("rft_0stringrf", "");
124         pp.add("rft_0integerrf", "");
125         pp.add("rft_0intrf", "");
126         pp.add("rft_0daterf", "");
127         requiredFalseTestGroup.init(Group.NEW, pp);
128 
129         Field stringRF = requiredFalseTestGroup.get("StringRF");
130         Field integerRF = requiredFalseTestGroup.get("IntegerRF");
131         Field intRF = requiredFalseTestGroup.get("IntRF");
132         Field dateRF = requiredFalseTestGroup.get("DateRF");
133 
134         assertTrue("StringRF should be set", stringRF.isSet());
135         assertTrue("StringRF should be valid", stringRF.isValid());
136         assertNull(stringRF.getValue());
137         assertTrue("IntegerRF should be set", integerRF.isSet());
138         assertTrue("IntegerRF should be valid", integerRF.isValid());
139         assertNull(integerRF.getValue());
140         assertTrue("IntRF should be set", intRF.isSet());
141         assertTrue("IntRF should be valid", intRF.isValid());
142         assertNull(intRF.getValue()); // zero?
143         assertTrue("DateRF should be set", dateRF.isSet());
144         assertTrue("DateRF should be valid", dateRF.isValid());
145         assertNull(dateRF.getValue());
146     }
147 
148     /***
149      * This test verify that an empty field can be used to clear existing
150      * values.
151      *
152      * @throws IntakeException
153      */
154     public void testClearValues() throws IntakeException
155     {
156         RequiredFalseGroupTestObject rfgto = new RequiredFalseGroupTestObject();
157         rfgto.setStringRF("originalString");
158         rfgto.setIntegerRF(new Integer(5));
159         rfgto.setIntRF(6);
160         Date testDate = new Date();
161         rfgto.setDateRF(testDate);
162 
163         ParameterParser pp = new DefaultParameterParser();
164         pp.add("rft_0stringrf", "");
165         pp.add("rft_0integerrf", "");
166         pp.add("rft_0intrf", "");
167         pp.add("rft_0daterf", "");
168         requiredFalseTestGroup.init(Group.NEW, pp);
169 
170         requiredFalseTestGroup.setProperties(rfgto);
171         assertNull("String value should have been cleared.", rfgto.getStringRF());
172         assertNull("Date value should have been cleared.", rfgto.getDateRF());
173         assertEquals("int value should have been cleared to zero.", 0, rfgto.getIntRF());
174 
175         // The following commented out test fails.
176         // The trouble is that the use of reflection forces Intake to use Integer rather than int
177         // when invoking the setter method on the object to which the group is being mapped.
178 
179         //assertNull("Integer value should have been cleared to null, but instead it is "
180         //        + rfgto.getIntegerRF(), rfgto.getIntegerRF());
181 
182         // The net result is that Intake is currently not well suited to validating
183         // Integer fields where a null value needs to be distinguished from a zero.
184     }
185 
186     /***
187      * This test attempts to verify that with that valid values coming from
188      * intake map through to an object correctly.
189      *
190      * @throws IntakeException
191      */
192     public void testSetValues() throws IntakeException
193     {
194         Calendar cal = Calendar.getInstance();
195         cal.setTime(new Date());
196         cal.set(Calendar.HOUR_OF_DAY, 0);
197         cal.set(Calendar.MINUTE, 0);
198         cal.set(Calendar.SECOND, 0);
199         cal.set(Calendar.MILLISECOND, 0);
200         Date testDate = cal.getTime();
201         // This is in dd/mm/yyyy format, as defined in the intake.xml for this group.
202         String testDateString = cal.get(Calendar.DAY_OF_MONTH) + "/"
203                 + (cal.get(Calendar.MONTH) + 1) + "/" + (cal.get(Calendar.YEAR));
204 
205         ParameterParser pp = new DefaultParameterParser();
206         pp.add("rft_0stringrf", "ABC"); // rules require 3 characters.
207         pp.add("rft_0integerrf", new Integer(10));
208         pp.add("rft_0intrf", 11);
209         pp.add("rft_0daterf", testDateString);
210         requiredFalseTestGroup.init(Group.NEW, pp);
211 
212         Field stringRF = requiredFalseTestGroup.get("StringRF");
213         Field integerRF = requiredFalseTestGroup.get("IntegerRF");
214         Field intRF = requiredFalseTestGroup.get("IntRF");
215         Field dateRF = requiredFalseTestGroup.get("DateRF");
216 
217         assertTrue("StringRF should be set", stringRF.isSet());
218         assertTrue("StringRF should be valid", stringRF.isValid());
219         assertEquals("ABC", stringRF.getValue());
220         assertTrue("IntegerRF should be set", integerRF.isSet());
221         assertTrue("IntegerRF should be valid", integerRF.isValid());
222         assertEquals(new Integer(10), integerRF.getValue());
223         assertTrue("IntRF should be set", intRF.isSet());
224         assertTrue("IntRF should be valid", intRF.isValid());
225         assertEquals(11, ((Integer) intRF.getValue()).intValue());
226         assertTrue("DateRF should be set", dateRF.isSet());
227         assertTrue("DateRF should be valid", dateRF.isValid());
228         assertEquals(testDate, dateRF.getValue());
229 
230         RequiredFalseGroupTestObject rfgto = new RequiredFalseGroupTestObject();
231         requiredFalseTestGroup.setProperties(rfgto);
232         assertEquals("ABC", rfgto.getStringRF());
233         assertEquals(new Integer(10), rfgto.getIntegerRF());
234         assertEquals(11, rfgto.getIntRF());
235         assertEquals(testDate, rfgto.getDateRF());
236     }
237 
238     /***
239      * Test that a required field with no value assigned is invalid.
240      *
241      * @throws IntakeException
242      */
243     public void testRequiredTrue() throws IntakeException
244     {
245         ParameterParser pp = new DefaultParameterParser();
246         pp.add("rtt_0stringrt", "");
247         requiredTrueTestGroup.init(Group.NEW, pp);
248 
249         Field stringRT = requiredTrueTestGroup.get("StringRT");
250 
251         assertTrue("StringRT should be set", stringRT.isSet());
252         assertFalse("StringRT should not be valid", stringRT.isValid());
253         assertEquals("", stringRT.getValue());
254     }
255 
256     /***
257      * This test verifies that a newly created group is not initiated in an
258      * error state.
259      *
260      * @throws IntakeException
261      */
262     public void testInitialErrorState() throws IntakeException
263     {
264         ParameterParser pp = new DefaultParameterParser();
265         requiredFalseTestGroup.init(Group.NEW, pp);
266 
267         Field stringRF = requiredFalseTestGroup.get("StringRF");
268         Field integerRF = requiredFalseTestGroup.get("IntegerRF");
269         Field intRF = requiredFalseTestGroup.get("IntRF");
270         Field dateRF = requiredFalseTestGroup.get("DateRF");
271 
272         assertFalse("StringRF should not be set", stringRF.isSet());
273         assertTrue("StringRF should be valid", stringRF.isValid());
274         assertEquals("StringRF should have no messages.", "", stringRF.getMessage());
275         assertNull(stringRF.getValue());
276         assertFalse("IntegerRF should not be set", integerRF.isSet());
277         assertTrue("IntegerRF should be valid", integerRF.isValid());
278         assertEquals("IntegerRF should have no messages", "", integerRF.getMessage());
279         assertNull(integerRF.getValue());
280         assertFalse("IntRF should not be set", intRF.isSet());
281         assertTrue("IntRF should be valid", intRF.isValid());
282         assertEquals("IntRF should have no messages", "", intRF.getMessage());
283         assertNull(intRF.getValue());
284         assertFalse("DateRF should not be set", dateRF.isSet());
285         assertTrue("DateRF should be valid", dateRF.isValid());
286         assertEquals("DateRF should have no messages", "", dateRF.getMessage());
287         assertNull(dateRF.getValue());
288 
289         requiredTrueTestGroup.init(Group.NEW, pp);
290         Field stringRT = requiredTrueTestGroup.get("StringRT");
291 
292         assertFalse("StringRT should not be set", stringRT.isSet());
293         assertTrue("StringRT should be valid", stringRT.isValid());
294         assertEquals("StringRT should have no messages.", "", stringRT.getMessage());
295         assertNull(stringRT.getValue());
296     }
297 
298     /***
299      * Factory method for creating a TestSuite for this class.
300      *
301      * @return the test suite
302      */
303     public static TestSuite suite()
304     {
305         TestSuite suite = new TestSuite(IntakeServiceTest.class);
306         return suite;
307     }
308 
309 }