001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.validator;
018:
019: import java.io.IOException;
020: import java.io.InputStream;
021:
022: import junit.framework.TestCase;
023:
024: import org.xml.sax.SAXException;
025:
026: /**
027: * Tests that validator rules split between 2 different XML files get
028: * merged properly.
029: *
030: * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
031: */
032: public class MultipleConfigFilesTest extends TestCase {
033:
034: /**
035: * Resources used for validation tests.
036: */
037: private ValidatorResources resources = null;
038:
039: /**
040: * The key used to retrieve the set of validation
041: * rules from the xml file.
042: */
043: private static final String FORM_KEY = "nameForm";
044:
045: /**
046: * The key used to retrieve the validator action.
047: */
048: private static final String ACTION = "required";
049:
050: /**
051: * Constructor for MultipleConfigFilesTest.
052: * @param name
053: */
054: public MultipleConfigFilesTest(String name) {
055: super (name);
056: }
057:
058: /**
059: * Load <code>ValidatorResources</code> from multiple xml files.
060: */
061: protected void setUp() throws IOException, SAXException {
062: InputStream[] streams = new InputStream[] {
063: this .getClass().getResourceAsStream(
064: "MultipleConfigFilesTest-1-config.xml"),
065: this .getClass().getResourceAsStream(
066: "MultipleConfigFilesTest-2-config.xml") };
067:
068: this .resources = new ValidatorResources(streams);
069:
070: for (int i = 0; i < streams.length; i++) {
071: streams[i].close();
072: }
073: }
074:
075: /**
076: * Check the forms and constants from different config files have
077: * been merged into the same FormSet.
078: */
079: public void testMergedConfig() throws ValidatorException {
080:
081: // *********** Default Locale *******************
082:
083: // Check the form from the first config file exists
084: Form form1 = resources.getForm("", "", "", "testForm1");
085: assertNotNull("Form 'testForm1' not found", form1);
086:
087: // Check the form from the second config file exists
088: Form form2 = resources.getForm("", "", "", "testForm2");
089: assertNotNull("Form 'testForm2' not found", form2);
090:
091: // Check the Constants for the form from the first config file
092: Field field1 = form1.getField("testProperty1");
093: assertEquals("testProperty1 - const 1", "testConstValue1",
094: field1.getVarValue("var11"));
095: assertEquals("testProperty1 - const 2", "testConstValue2",
096: field1.getVarValue("var12"));
097:
098: // Check the Constants for the form from the second config file
099: Field field2 = form2.getField("testProperty2");
100: assertEquals("testProperty2 - const 1", "testConstValue1",
101: field2.getVarValue("var21"));
102: assertEquals("testProperty2 - const 2", "testConstValue2",
103: field2.getVarValue("var22"));
104:
105: // *********** 'fr' locale *******************
106:
107: // Check the form from the first config file exists
108: Form form1_fr = resources.getForm("fr", "", "", "testForm1_fr");
109: assertNotNull("Form 'testForm1_fr' not found", form1_fr);
110:
111: // Check the form from the second config file exists
112: Form form2_fr = resources.getForm("fr", "", "", "testForm2_fr");
113: assertNotNull("Form 'testForm2_fr' not found", form2_fr);
114:
115: // Check the Constants for the form from the first config file
116: Field field1_fr = form1_fr.getField("testProperty1_fr");
117: assertEquals("testProperty1_fr - const 1",
118: "testConstValue1_fr", field1_fr.getVarValue("var11_fr"));
119: assertEquals("testProperty1_fr - const 2",
120: "testConstValue2_fr", field1_fr.getVarValue("var12_fr"));
121:
122: // Check the Constants for the form from the second config file
123: Field field2_fr = form2_fr.getField("testProperty2_fr");
124: assertEquals("testProperty2_fr - const 1",
125: "testConstValue1_fr", field2_fr.getVarValue("var21_fr"));
126: assertEquals("testProperty2_fr - const 2",
127: "testConstValue2_fr", field2_fr.getVarValue("var22_fr"));
128: }
129:
130: /**
131: * With nothing provided, we should fail both because both are required.
132: */
133: public void testBothBlank() throws ValidatorException {
134: // Create bean to run test on.
135: NameBean name = new NameBean();
136:
137: // Construct validator based on the loaded resources
138: // and the form key
139: Validator validator = new Validator(resources, FORM_KEY);
140: // add the name bean to the validator as a resource
141: // for the validations to be performed on.
142: validator.setParameter(Validator.BEAN_PARAM, name);
143:
144: // Get results of the validation.
145: ValidatorResults results = null;
146:
147: // throws ValidatorException,
148: // but we aren't catching for testing
149: // since no validation methods we use
150: // throw this
151: results = validator.validate();
152:
153: assertNotNull("Results are null.", results);
154:
155: ValidatorResult firstNameResult = results
156: .getValidatorResult("firstName");
157: ValidatorResult lastNameResult = results
158: .getValidatorResult("lastName");
159:
160: assertNotNull(firstNameResult);
161: assertTrue(firstNameResult.containsAction(ACTION));
162: assertTrue(!firstNameResult.isValid(ACTION));
163:
164: assertNotNull(lastNameResult);
165: assertTrue(lastNameResult.containsAction(ACTION));
166: assertTrue(!lastNameResult.isValid(ACTION));
167: assertTrue(!lastNameResult.containsAction("int"));
168: }
169:
170: /**
171: * If the first name fails required, and the second test fails int, we should get two errors.
172: */
173: public void testRequiredFirstNameBlankLastNameShort()
174: throws ValidatorException {
175: // Create bean to run test on.
176: NameBean name = new NameBean();
177: name.setFirstName("");
178: name.setLastName("Test");
179:
180: // Construct validator based on the loaded resources
181: // and the form key
182: Validator validator = new Validator(resources, FORM_KEY);
183: // add the name bean to the validator as a resource
184: // for the validations to be performed on.
185: validator.setParameter(Validator.BEAN_PARAM, name);
186:
187: // Get results of the validation.
188: ValidatorResults results = null;
189:
190: results = validator.validate();
191:
192: assertNotNull("Results are null.", results);
193:
194: ValidatorResult firstNameResult = results
195: .getValidatorResult("firstName");
196: ValidatorResult lastNameResult = results
197: .getValidatorResult("lastName");
198:
199: assertNotNull(firstNameResult);
200: assertTrue(firstNameResult.containsAction(ACTION));
201: assertTrue(!firstNameResult.isValid(ACTION));
202:
203: assertNotNull(lastNameResult);
204: assertTrue(lastNameResult.containsAction("int"));
205: assertTrue(!lastNameResult.isValid("int"));
206: }
207:
208: /**
209: * If the first name is there, and the last name fails int, we should get one error.
210: */
211: public void testRequiredLastNameShort() throws ValidatorException {
212: // Create bean to run test on.
213: NameBean name = new NameBean();
214: name.setFirstName("Test");
215: name.setLastName("Test");
216:
217: // Construct validator based on the loaded resources
218: // and the form key
219: Validator validator = new Validator(resources, FORM_KEY);
220: // add the name bean to the validator as a resource
221: // for the validations to be performed on.
222: validator.setParameter(Validator.BEAN_PARAM, name);
223:
224: // Get results of the validation.
225: ValidatorResults results = null;
226:
227: results = validator.validate();
228:
229: assertNotNull("Results are null.", results);
230:
231: ValidatorResult firstNameResult = results
232: .getValidatorResult("firstName");
233: ValidatorResult lastNameResult = results
234: .getValidatorResult("lastName");
235:
236: assertNotNull(firstNameResult);
237: assertTrue(firstNameResult.containsAction(ACTION));
238: assertTrue(firstNameResult.isValid(ACTION));
239:
240: assertNotNull(lastNameResult);
241: assertTrue(lastNameResult.containsAction("int"));
242: assertTrue(!lastNameResult.isValid("int"));
243: }
244:
245: /**
246: * If first name is ok and last name is ok and is an int, no errors.
247: */
248: public void testRequiredLastNameLong() throws ValidatorException {
249: // Create bean to run test on.
250: NameBean name = new NameBean();
251: name.setFirstName("Joe");
252: name.setLastName("12345678");
253:
254: // Construct validator based on the loaded resources
255: // and the form key
256: Validator validator = new Validator(resources, FORM_KEY);
257: // add the name bean to the validator as a resource
258: // for the validations to be performed on.
259: validator.setParameter(Validator.BEAN_PARAM, name);
260:
261: // Get results of the validation.
262: ValidatorResults results = null;
263:
264: results = validator.validate();
265:
266: assertNotNull("Results are null.", results);
267:
268: ValidatorResult firstNameResult = results
269: .getValidatorResult("firstName");
270: ValidatorResult lastNameResult = results
271: .getValidatorResult("lastName");
272:
273: assertNotNull(firstNameResult);
274: assertTrue(firstNameResult.containsAction(ACTION));
275: assertTrue(firstNameResult.isValid(ACTION));
276:
277: assertNotNull(lastNameResult);
278: assertTrue(lastNameResult.containsAction("int"));
279: assertTrue(lastNameResult.isValid("int"));
280: }
281:
282: }
|