1   /*
2    * $Header: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/MultipleConfigFilesTest.java,v 1.10 2004/02/21 17:10:30 rleland Exp $
3    * $Revision: 1.10 $
4    * $Date: 2004/02/21 17:10:30 $
5    *
6    * ====================================================================
7    * Copyright 2001-2004 The Apache Software Foundation
8    *
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   *
13   *     http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   */
21  
22  package org.apache.commons.validator;
23  
24  import java.io.IOException;
25  import java.io.InputStream;
26  
27  import junit.framework.TestCase;
28  
29  import org.xml.sax.SAXException;
30  
31  /***
32   * Tests that validator rules split between 2 different XML files get 
33   * merged properly.
34   */
35  public class MultipleConfigFilesTest extends TestCase {
36  
37      /***
38       * Resources used for validation tests.
39       */
40      private ValidatorResources resources = null;
41      
42      /***
43       * The key used to retrieve the set of validation
44       * rules from the xml file.
45       */
46      private static final String FORM_KEY = "nameForm";
47  
48      /***
49       * The key used to retrieve the validator action.
50       */
51      private static final String ACTION = "required";
52  
53      /***
54       * Constructor for MultipleConfigFilesTest.
55       * @param name
56       */
57      public MultipleConfigFilesTest(String name) {
58          super(name);
59      }
60  
61      /*** 
62       * Load <code>ValidatorResources</code> from multiple xml files.
63       */
64      protected void setUp() throws IOException, SAXException {
65          InputStream[] streams =
66              new InputStream[] {
67                  this.getClass().getResourceAsStream(
68                      "validator-multiple-config-1.xml"),
69                  this.getClass().getResourceAsStream(
70                      "validator-multiple-config-2.xml")};
71  
72          this.resources = new ValidatorResources(streams);
73  
74          for (int i = 0; i < streams.length; i++) {
75              streams[i].close();
76          }
77      }
78  
79      /***
80      * With nothing provided, we should fail both because both are required.
81      */
82      public void testBothBlank() throws ValidatorException {
83          // Create bean to run test on.
84          NameBean name = new NameBean();
85  
86          // Construct validator based on the loaded resources
87          // and the form key
88          Validator validator = new Validator(resources, FORM_KEY);
89          // add the name bean to the validator as a resource
90          // for the validations to be performed on.
91          validator.setParameter(Validator.BEAN_PARAM, name);
92  
93          // Get results of the validation.
94          ValidatorResults results = null;
95  
96          // throws ValidatorException,
97          // but we aren't catching for testing
98          // since no validation methods we use
99          // throw this
100         results = validator.validate();
101 
102         assertNotNull("Results are null.", results);
103 
104         ValidatorResult firstNameResult = results.getValidatorResult("firstName");
105         ValidatorResult lastNameResult = results.getValidatorResult("lastName");
106 
107         assertNotNull(firstNameResult);
108         assertTrue(firstNameResult.containsAction(ACTION));
109         assertTrue(!firstNameResult.isValid(ACTION));
110 
111         assertNotNull(lastNameResult);
112         assertTrue(lastNameResult.containsAction(ACTION));
113         assertTrue(!lastNameResult.isValid(ACTION));
114         assertTrue(!lastNameResult.containsAction("int"));
115     }
116 
117     /***
118      * If the first name fails required, and the second test fails int, we should get two errors.
119     */
120     public void testRequiredFirstNameBlankLastNameShort()
121         throws ValidatorException {
122         // Create bean to run test on.
123         NameBean name = new NameBean();
124         name.setFirstName("");
125         name.setLastName("Test");
126 
127         // Construct validator based on the loaded resources
128         // and the form key
129         Validator validator = new Validator(resources, FORM_KEY);
130         // add the name bean to the validator as a resource
131         // for the validations to be performed on.
132         validator.setParameter(Validator.BEAN_PARAM, name);
133 
134         // Get results of the validation.
135         ValidatorResults results = null;
136 
137         results = validator.validate();
138 
139         assertNotNull("Results are null.", results);
140 
141         ValidatorResult firstNameResult = results.getValidatorResult("firstName");
142         ValidatorResult lastNameResult = results.getValidatorResult("lastName");
143 
144         assertNotNull(firstNameResult);
145         assertTrue(firstNameResult.containsAction(ACTION));
146         assertTrue(!firstNameResult.isValid(ACTION));
147 
148         assertNotNull(lastNameResult);
149         assertTrue(lastNameResult.containsAction("int"));
150         assertTrue(!lastNameResult.isValid("int"));
151     }
152 
153     /***
154      * If the first name is there, and the last name fails int, we should get one error.
155     */
156     public void testRequiredLastNameShort() throws ValidatorException {
157         // Create bean to run test on.
158         NameBean name = new NameBean();
159         name.setFirstName("Test");
160         name.setLastName("Test");
161 
162         // Construct validator based on the loaded resources
163         // and the form key
164         Validator validator = new Validator(resources, FORM_KEY);
165         // add the name bean to the validator as a resource
166         // for the validations to be performed on.
167         validator.setParameter(Validator.BEAN_PARAM, name);
168 
169         // Get results of the validation.
170         ValidatorResults results = null;
171 
172         results = validator.validate();
173 
174         assertNotNull("Results are null.", results);
175 
176         ValidatorResult firstNameResult = results.getValidatorResult("firstName");
177         ValidatorResult lastNameResult = results.getValidatorResult("lastName");
178 
179         assertNotNull(firstNameResult);
180         assertTrue(firstNameResult.containsAction(ACTION));
181         assertTrue(firstNameResult.isValid(ACTION));
182 
183         assertNotNull(lastNameResult);
184         assertTrue(lastNameResult.containsAction("int"));
185         assertTrue(!lastNameResult.isValid("int"));
186     }
187 
188     /***
189      * If first name is ok and last name is ok and is an int, no errors.
190     */
191     public void testRequiredLastNameLong() throws ValidatorException {
192         // Create bean to run test on.
193         NameBean name = new NameBean();
194         name.setFirstName("Joe");
195         name.setLastName("12345678");
196 
197         // Construct validator based on the loaded resources
198         // and the form key
199         Validator validator = new Validator(resources, FORM_KEY);
200         // add the name bean to the validator as a resource
201         // for the validations to be performed on.
202         validator.setParameter(Validator.BEAN_PARAM, name);
203 
204         // Get results of the validation.
205         ValidatorResults results = null;
206 
207         results = validator.validate();
208 
209         assertNotNull("Results are null.", results);
210 
211         ValidatorResult firstNameResult = results.getValidatorResult("firstName");
212         ValidatorResult lastNameResult = results.getValidatorResult("lastName");
213 
214         assertNotNull(firstNameResult);
215         assertTrue(firstNameResult.containsAction(ACTION));
216         assertTrue(firstNameResult.isValid(ACTION));
217 
218         assertNotNull(lastNameResult);
219         assertTrue(lastNameResult.containsAction("int"));
220         assertTrue(lastNameResult.isValid("int"));
221     }
222 
223 }