01: /*
02: * Copyright 2005-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
05: * in compliance with the License. You may obtain a copy of the License at
06: *
07: * http://www.apache.org/licenses/LICENSE-2.0
08: *
09: * Unless required by applicable law or agreed to in writing, software distributed under the License
10: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11: * or implied. See the License for the specific language governing permissions and limitations under
12: * the License.
13: */
14:
15: package org.strecks.web.form;
16:
17: import java.util.Iterator;
18:
19: import org.apache.struts.action.ActionErrors;
20: import org.apache.struts.action.ActionMessage;
21: import org.apache.struts.util.MessageResources;
22: import org.apache.struts.util.MessageResourcesFactory;
23: import org.apache.struts.util.PropertyMessageResourcesFactory;
24: import org.testng.annotations.BeforeMethod;
25:
26: /**
27: * @author Phil Zoio
28: */
29: public abstract class AbstractTestFormValidation {
30:
31: protected MessageResources resources;
32:
33: @BeforeMethod
34: public void beforeClass() {
35: MessageResourcesFactory factory = new PropertyMessageResourcesFactory();
36: resources = factory
37: .createResources("org.strecks.web.form.ApplicationMessages");
38: }
39:
40: /* ****** helper methods ****** */
41:
42: protected String getValidationMessage(ActionErrors errors,
43: String propertyName) {
44: ActionMessage message = firstError(errors, propertyName);
45: assert message != null;
46:
47: String outputMessage = resources.getMessage(message.getKey(),
48: message.getValues());
49: return outputMessage;
50: }
51:
52: protected ActionMessage firstError(ActionErrors errors,
53: String propertyName) {
54: Iterator error = errors.get(propertyName);
55: ActionMessage message = (ActionMessage) error.next();
56: return message;
57: }
58:
59: protected boolean propertyHasError(ActionErrors errors,
60: String propertyName) {
61: return errors.get(propertyName).hasNext();
62: }
63:
64: }
|