001: /*
002: * Copyright 2005-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005: * in compliance with the License. You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software distributed under the License
010: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011: * or implied. See the License for the specific language governing permissions and limitations under
012: * the License.
013: */
014:
015: package org.strecks.web.form;
016:
017: import java.util.Date;
018: import java.util.HashMap;
019: import java.util.Map;
020:
021: import org.strecks.form.controller.DelegatingForm;
022: import org.strecks.form.controller.FormTestUtils;
023: import org.strecks.web.form.impl.NewProjectForm;
024: import org.strecks.web.form.impl.Project;
025: import org.strecks.web.form.impl.ProjectType;
026: import org.testng.annotations.BeforeMethod;
027: import org.testng.annotations.Test;
028:
029: /**
030: * @author Phil Zoio
031: */
032: public class TestProjectFormBinding {
033:
034: private NewProjectForm form;
035: private DelegatingForm delegator;
036:
037: private static final ProjectType projectType1 = new ProjectType(1,
038: "Project type 1");
039:
040: private static final ProjectType projectType2 = new ProjectType(1,
041: "Project type 2");
042:
043: @BeforeMethod
044: public void testProjectForm() {
045:
046: form = new NewProjectForm();
047:
048: //set up form
049: Map<Integer, ProjectType> availableProjectTypes = new HashMap<Integer, ProjectType>();
050: availableProjectTypes.put(1, projectType1);
051: availableProjectTypes.put(1, projectType2);
052: form.setAvailableProjectTypes(availableProjectTypes);
053:
054: Project project = new Project();
055: form.setProject(project);
056:
057: delegator = FormTestUtils.getDelegatingForm(form);
058: }
059:
060: @Test
061: public void testInwardBinding() {
062:
063: Project project = new Project();
064: form.setProject(project);
065:
066: // now set properties
067: // form.setSelectedProjectStatus("1");
068: form.setSelectedProjectType("1");
069: form.setProjectName("projectName");
070: form.setReadyDate("19 July 2005");
071:
072: delegator.bindInwards(null);
073:
074: assert project.getProjectType() != null;
075: assert project.getReadyDate() != null;
076: assert project.getProjectName() != null;
077:
078: }
079:
080: @Test
081: public void testEmptyBinding() {
082:
083: Project project = new Project();
084: form.setProject(project);
085:
086: // now set properties
087: form.setSelectedProjectType("");
088: form.setProjectName("");
089: form.setProjectDescription("");
090: form.setReadyDate("");
091:
092: delegator.bindInwards(null);
093:
094: assert project.getProjectType() == null;
095: assert project.getReadyDate() == null;
096: assert project.getProjectName() == null;
097:
098: // this uses SafeBeanUtilsConverter.class, so results in null
099: assert project.getProjectDescription() == null;
100:
101: }
102:
103: /**
104: * Null inward binding with availableProjectTypes not set to null
105: */
106: @Test
107: public void testNullInwardBinding1() {
108:
109: Project project = new Project();
110: form.setProject(project);
111:
112: delegator.bindInwards(null);
113:
114: assert project.getProjectType() == null;
115: assert project.getReadyDate() == null;
116: assert project.getProjectName() == null;
117:
118: }
119:
120: /**
121: * Null inward binding with availableProjectTypes set to null
122: */
123: @Test
124: public void testNullInwardBinding2() {
125:
126: Project project = new Project();
127: form.setProject(project);
128:
129: form.setAvailableProjectTypes(null);
130: delegator.bindInwards(null);
131:
132: assert project.getProjectType() == null;
133: assert project.getReadyDate() == null;
134: assert project.getProjectName() == null;
135:
136: }
137:
138: @Test
139: public void testOutwardBinding1() {
140:
141: Project project = new Project();
142: form.setProject(project);
143:
144: project.setProjectType(new ProjectType());
145: project.setProjectName("my project");
146: project.setReadyDate(new Date());
147:
148: delegator.bindOutwards(null);
149:
150: // because the ProjectType has no ID set, this will be null. A log message should be set
151: assert form.getSelectedProjectType() == null;
152:
153: assert form.getProjectName() != null;
154: assert form.getReadyDate() != null;
155:
156: }
157:
158: @Test
159: public void testOutwardBinding2() {
160:
161: Project project = new Project();
162: form.setProject(project);
163:
164: project.setProjectType(projectType1);
165: project.setProjectName("my project");
166: project.setReadyDate(new Date());
167:
168: delegator.bindOutwards(null);
169:
170: assert form.getSelectedProjectType() != null;
171: assert form.getProjectName() != null;
172: assert form.getReadyDate() != null;
173:
174: }
175:
176: @Test
177: public void testNullOutwardBinding() {
178:
179: Project project = new Project();
180: form.setProject(project);
181:
182: delegator.bindOutwards(null);
183:
184: assert form.getSelectedProjectType() == null;
185: assert form.getProjectName() == null;
186: assert form.getReadyDate() == null;
187:
188: }
189:
190: @Test
191: public void testOverwriteInwardBinding() {
192:
193: Project project = new Project();
194: form.setProject(project);
195: project.setProjectName("my project");
196:
197: form.setProjectName(null);
198:
199: delegator.bindInwards(null);
200:
201: // check that these have been set to null
202: assert project.getProjectName() == null;
203:
204: }
205:
206: @Test
207: public void testOverwriteOutwardBinding() {
208: Project project = new Project();
209: form.setProject(project);
210:
211: form.setProjectName("project name");
212:
213: project.setProjectType(new ProjectType());
214: project.setProjectName(null);
215:
216: delegator.bindOutwards(null);
217:
218: // check that these have been set to null
219: assert form.getProjectName() == null;
220:
221: }
222:
223: }
|