001: package org.apache.turbine.services.intake;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.util.Calendar;
023: import java.util.Date;
024:
025: import junit.framework.TestSuite;
026:
027: import org.apache.turbine.services.ServiceManager;
028: import org.apache.turbine.services.TurbineServices;
029: import org.apache.turbine.services.intake.model.Field;
030: import org.apache.turbine.services.intake.model.Group;
031: import org.apache.turbine.services.intake.validator.BooleanValidator;
032: import org.apache.turbine.services.intake.validator.DateRangeValidator;
033: import org.apache.turbine.services.intake.validator.IntegerRangeValidator;
034: import org.apache.turbine.test.BaseTurbineTest;
035: import org.apache.turbine.util.parser.DefaultParameterParser;
036: import org.apache.turbine.util.parser.ParameterParser;
037:
038: public class IntakeServiceTest extends BaseTurbineTest {
039:
040: Group booleanTestGroup = null;
041: Group rangeTestGroup = null;
042: Group integerRangeTestGroup = null;
043: Group requiredFalseTestGroup = null;
044: Group requiredTrueTestGroup = null;
045:
046: public IntakeServiceTest(String name) throws Exception {
047: super (name, "conf/test/TurbineResourcesWithIntake.properties");
048:
049: ServiceManager serviceManager = TurbineServices.getInstance();
050: IntakeService intakeService = (IntakeService) serviceManager
051: .getService(IntakeService.SERVICE_NAME);
052: booleanTestGroup = intakeService.getGroup("BooleanTest");
053: rangeTestGroup = intakeService.getGroup("DateRangeTest");
054: integerRangeTestGroup = intakeService.getGroup("IntRangeTest");
055: requiredFalseTestGroup = intakeService
056: .getGroup("RequiredFalseTest");
057: requiredTrueTestGroup = intakeService
058: .getGroup("RequiredTrueTest");
059: }
060:
061: public void testEmptyBooleanField() throws IntakeException {
062: Field booleanField = booleanTestGroup
063: .get("EmptyBooleanTestField");
064: assertTrue(
065: "The Default Validator of an intake Field type boolean should be BooleanValidator",
066: (booleanField.getValidator() instanceof BooleanValidator));
067: assertFalse(
068: "An Empty intake Field type boolean should not be required",
069: booleanField.isRequired());
070: }
071:
072: public void testBooleanField() throws IntakeException {
073: Field booleanField = booleanTestGroup.get("BooleanTestField");
074: assertTrue(
075: "The Default Validator of an intake Field type boolean should be BooleanValidator",
076: (booleanField.getValidator() instanceof BooleanValidator));
077: assertFalse(
078: "An intake Field type boolean, which is not required, should not be required",
079: booleanField.isRequired());
080: }
081:
082: public void testRequiredBooleanField() throws IntakeException {
083: Field booleanField = booleanTestGroup
084: .get("RequiredBooleanTestField");
085: assertTrue(
086: "The Default Validator of an intake Field type boolean should be BooleanValidator",
087: (booleanField.getValidator() instanceof BooleanValidator));
088: assertTrue(
089: "An intake Field type boolean, which is required, should be required",
090: booleanField.isRequired());
091: }
092:
093: public void testDateRangeValidator() throws IntakeException {
094: ParameterParser pp = new DefaultParameterParser();
095: pp.add("rt_0dmin", "05/11/2007");
096: pp.add("rt_0dmax", "05/12/2007");
097: pp.add("rt_0dmax2", "05/11/2007");
098: rangeTestGroup.init(Group.NEW, pp);
099:
100: Field dmax = rangeTestGroup.get("DateMax");
101: Field dmax2 = rangeTestGroup.get("DateMax2");
102:
103: assertTrue(
104: "The Validator of the field DateMax should be a DateRangeValidator",
105: (dmax.getValidator() instanceof DateRangeValidator));
106: assertTrue("The date range should be valid", dmax.isValid());
107: assertFalse("The date range should not be valid", dmax2
108: .isValid());
109: }
110:
111: public void testIntegerRangeValidator() throws IntakeException {
112: ParameterParser pp = new DefaultParameterParser();
113: pp.add("irt_0imin", "1");
114: pp.add("irt_0imax", "3");
115: pp.add("irt_0imax2", "2");
116: integerRangeTestGroup.init(Group.NEW, pp);
117:
118: Field imax = integerRangeTestGroup.get("IntMax");
119: Field imax2 = integerRangeTestGroup.get("IntMax2");
120:
121: assertTrue(
122: "The Validator of the field IntMax should be an IntegerRangeValidator",
123: (imax.getValidator() instanceof IntegerRangeValidator));
124: assertTrue("The integer range should be valid", imax.isValid());
125: assertFalse("The integer range should not be valid", imax2
126: .isValid());
127: }
128:
129: /**
130: * This test verifies that an intake field returns true for isSet() even
131: * when an empty field is submitted.
132: *
133: * @throws IntakeException
134: */
135: public void testRequiredFalse() throws IntakeException {
136: ParameterParser pp = new DefaultParameterParser();
137: pp.add("rft_0stringrf", "");
138: pp.add("rft_0integerrf", "");
139: pp.add("rft_0intrf", "");
140: pp.add("rft_0daterf", "");
141: requiredFalseTestGroup.init(Group.NEW, pp);
142:
143: Field stringRF = requiredFalseTestGroup.get("StringRF");
144: Field integerRF = requiredFalseTestGroup.get("IntegerRF");
145: Field intRF = requiredFalseTestGroup.get("IntRF");
146: Field dateRF = requiredFalseTestGroup.get("DateRF");
147:
148: assertTrue("StringRF should be set", stringRF.isSet());
149: assertTrue("StringRF should be valid", stringRF.isValid());
150: assertNull(stringRF.getValue());
151: assertTrue("IntegerRF should be set", integerRF.isSet());
152: assertTrue("IntegerRF should be valid", integerRF.isValid());
153: assertNull(integerRF.getValue());
154: assertTrue("IntRF should be set", intRF.isSet());
155: assertTrue("IntRF should be valid", intRF.isValid());
156: assertNull(intRF.getValue()); // zero?
157: assertTrue("DateRF should be set", dateRF.isSet());
158: assertTrue("DateRF should be valid", dateRF.isValid());
159: assertNull(dateRF.getValue());
160: }
161:
162: /**
163: * This test verify that an empty field can be used to clear existing
164: * values.
165: *
166: * @throws IntakeException
167: */
168: public void testClearValues() throws IntakeException {
169: RequiredFalseGroupTestObject rfgto = new RequiredFalseGroupTestObject();
170: rfgto.setStringRF("originalString");
171: rfgto.setIntegerRF(new Integer(5));
172: rfgto.setIntRF(6);
173: Date testDate = new Date();
174: rfgto.setDateRF(testDate);
175:
176: ParameterParser pp = new DefaultParameterParser();
177: pp.add("rft_0stringrf", "");
178: pp.add("rft_0integerrf", "");
179: pp.add("rft_0intrf", "");
180: pp.add("rft_0daterf", "");
181: requiredFalseTestGroup.init(Group.NEW, pp);
182:
183: requiredFalseTestGroup.setProperties(rfgto);
184: assertNull("String value should have been cleared.", rfgto
185: .getStringRF());
186: assertNull("Date value should have been cleared.", rfgto
187: .getDateRF());
188: assertEquals("int value should have been cleared to zero.", 0,
189: rfgto.getIntRF());
190:
191: // The following commented out test fails.
192: // The trouble is that the use of reflection forces Intake to use Integer rather than int
193: // when invoking the setter method on the object to which the group is being mapped.
194:
195: //assertNull("Integer value should have been cleared to null, but instead it is "
196: // + rfgto.getIntegerRF(), rfgto.getIntegerRF());
197:
198: // The net result is that Intake is currently not well suited to validating
199: // Integer fields where a null value needs to be distinguished from a zero.
200: }
201:
202: /**
203: * This test attempts to verify that with that valid values coming from
204: * intake map through to an object correctly.
205: *
206: * @throws IntakeException
207: */
208: public void testSetValues() throws IntakeException {
209: Calendar cal = Calendar.getInstance();
210: cal.setTime(new Date());
211: cal.set(Calendar.HOUR_OF_DAY, 0);
212: cal.set(Calendar.MINUTE, 0);
213: cal.set(Calendar.SECOND, 0);
214: cal.set(Calendar.MILLISECOND, 0);
215: Date testDate = cal.getTime();
216: // This is in dd/mm/yyyy format, as defined in the intake.xml for this group.
217: String testDateString = cal.get(Calendar.DAY_OF_MONTH) + "/"
218: + (cal.get(Calendar.MONTH) + 1) + "/"
219: + (cal.get(Calendar.YEAR));
220:
221: ParameterParser pp = new DefaultParameterParser();
222: pp.add("rft_0stringrf", "ABC"); // rules require 3 characters.
223: pp.add("rft_0integerrf", new Integer(10));
224: pp.add("rft_0intrf", 11);
225: pp.add("rft_0daterf", testDateString);
226: requiredFalseTestGroup.init(Group.NEW, pp);
227:
228: Field stringRF = requiredFalseTestGroup.get("StringRF");
229: Field integerRF = requiredFalseTestGroup.get("IntegerRF");
230: Field intRF = requiredFalseTestGroup.get("IntRF");
231: Field dateRF = requiredFalseTestGroup.get("DateRF");
232:
233: assertTrue("StringRF should be set", stringRF.isSet());
234: assertTrue("StringRF should be valid", stringRF.isValid());
235: assertEquals("ABC", stringRF.getValue());
236: assertTrue("IntegerRF should be set", integerRF.isSet());
237: assertTrue("IntegerRF should be valid", integerRF.isValid());
238: assertEquals(new Integer(10), integerRF.getValue());
239: assertTrue("IntRF should be set", intRF.isSet());
240: assertTrue("IntRF should be valid", intRF.isValid());
241: assertEquals(11, ((Integer) intRF.getValue()).intValue());
242: assertTrue("DateRF should be set", dateRF.isSet());
243: assertTrue("DateRF should be valid", dateRF.isValid());
244: assertEquals(testDate, dateRF.getValue());
245:
246: RequiredFalseGroupTestObject rfgto = new RequiredFalseGroupTestObject();
247: requiredFalseTestGroup.setProperties(rfgto);
248: assertEquals("ABC", rfgto.getStringRF());
249: assertEquals(new Integer(10), rfgto.getIntegerRF());
250: assertEquals(11, rfgto.getIntRF());
251: assertEquals(testDate, rfgto.getDateRF());
252: }
253:
254: /**
255: * Test that a required field with no value assigned is invalid.
256: *
257: * @throws IntakeException
258: */
259: public void testRequiredTrue() throws IntakeException {
260: ParameterParser pp = new DefaultParameterParser();
261: pp.add("rtt_0stringrt", "");
262: requiredTrueTestGroup.init(Group.NEW, pp);
263:
264: Field stringRT = requiredTrueTestGroup.get("StringRT");
265:
266: assertTrue("StringRT should be set", stringRT.isSet());
267: assertFalse("StringRT should not be valid", stringRT.isValid());
268: assertEquals("", stringRT.getValue());
269: }
270:
271: /**
272: * This test verifies that a newly created group is not initiated in an
273: * error state.
274: *
275: * @throws IntakeException
276: */
277: public void testInitialErrorState() throws IntakeException {
278: ParameterParser pp = new DefaultParameterParser();
279: requiredFalseTestGroup.init(Group.NEW, pp);
280:
281: Field stringRF = requiredFalseTestGroup.get("StringRF");
282: Field integerRF = requiredFalseTestGroup.get("IntegerRF");
283: Field intRF = requiredFalseTestGroup.get("IntRF");
284: Field dateRF = requiredFalseTestGroup.get("DateRF");
285:
286: assertFalse("StringRF should not be set", stringRF.isSet());
287: assertTrue("StringRF should be valid", stringRF.isValid());
288: assertEquals("StringRF should have no messages.", "", stringRF
289: .getMessage());
290: assertNull(stringRF.getValue());
291: assertFalse("IntegerRF should not be set", integerRF.isSet());
292: assertTrue("IntegerRF should be valid", integerRF.isValid());
293: assertEquals("IntegerRF should have no messages", "", integerRF
294: .getMessage());
295: assertNull(integerRF.getValue());
296: assertFalse("IntRF should not be set", intRF.isSet());
297: assertTrue("IntRF should be valid", intRF.isValid());
298: assertEquals("IntRF should have no messages", "", intRF
299: .getMessage());
300: assertNull(intRF.getValue());
301: assertFalse("DateRF should not be set", dateRF.isSet());
302: assertTrue("DateRF should be valid", dateRF.isValid());
303: assertEquals("DateRF should have no messages", "", dateRF
304: .getMessage());
305: assertNull(dateRF.getValue());
306:
307: requiredTrueTestGroup.init(Group.NEW, pp);
308: Field stringRT = requiredTrueTestGroup.get("StringRT");
309:
310: assertFalse("StringRT should not be set", stringRT.isSet());
311: assertTrue("StringRT should be valid", stringRT.isValid());
312: assertEquals("StringRT should have no messages.", "", stringRT
313: .getMessage());
314: assertNull(stringRT.getValue());
315: }
316:
317: /**
318: * Factory method for creating a TestSuite for this class.
319: *
320: * @return the test suite
321: */
322: public static TestSuite suite() {
323: TestSuite suite = new TestSuite(IntakeServiceTest.class);
324: return suite;
325: }
326:
327: }
|