001: // Copyright 2006, 2007 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // 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
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry;
016:
017: import java.io.ByteArrayInputStream;
018: import java.io.ByteArrayOutputStream;
019: import java.io.ObjectInputStream;
020: import java.io.ObjectOutputStream;
021: import java.util.Arrays;
022:
023: import org.apache.tapestry.test.TapestryTestCase;
024: import org.testng.annotations.Test;
025:
026: public class ValidationTrackerImplTest extends TapestryTestCase {
027: @Test
028: public void empty_tracker_has_no_errors() {
029: ValidationTracker tracker = new ValidationTrackerImpl();
030:
031: assertTrue(tracker.getErrors().isEmpty());
032: assertFalse(tracker.getHasErrors());
033: }
034:
035: @Test
036: public void order_added_is_maintained() {
037: Field fielda = newFieldWithElementName("fieldA");
038: Field fieldb = newFieldWithElementName("fieldB");
039:
040: replay();
041:
042: ValidationTracker tracker = new ValidationTrackerImpl();
043:
044: tracker.recordError("one");
045: tracker.recordError(fieldb, "fieldb: two");
046: tracker.recordError("three");
047: tracker.recordError(fielda, "fielda: four");
048:
049: assertEquals(tracker.getErrors(), Arrays.asList("one", "three",
050: "fieldb: two", "fielda: four"));
051:
052: verify();
053: }
054:
055: @Test
056: public void record_input() {
057: Field field = newFieldWithElementName("field");
058:
059: replay();
060:
061: ValidationTracker tracker = new ValidationTrackerImpl();
062:
063: assertNull(tracker.getInput(field));
064:
065: tracker.recordInput(field, "one");
066:
067: assertEquals(tracker.getInput(field), "one");
068:
069: tracker.recordInput(field, "two");
070:
071: assertEquals(tracker.getInput(field), "two");
072:
073: verify();
074: }
075:
076: @Test
077: public void record_error_for_field() {
078: Field field = newFieldWithElementName("field");
079:
080: replay();
081:
082: ValidationTracker tracker = new ValidationTrackerImpl();
083:
084: assertFalse(tracker.getHasErrors());
085: assertFalse(tracker.inError(field));
086: assertNull(tracker.getError(field));
087:
088: tracker.recordError(field, "one");
089:
090: assertTrue(tracker.getHasErrors());
091: assertTrue(tracker.inError(field));
092: assertEquals(tracker.getError(field), "one");
093:
094: tracker.recordError(field, "two");
095: assertEquals(tracker.getError(field), "two");
096:
097: verify();
098: }
099:
100: @Test
101: public void record_error_for_form() {
102: ValidationTracker tracker = new ValidationTrackerImpl();
103:
104: assertFalse(tracker.getHasErrors());
105:
106: assertTrue(tracker.getErrors().isEmpty());
107:
108: tracker.recordError("one");
109:
110: assertEquals(tracker.getErrors(), Arrays.asList("one"));
111:
112: tracker.recordError("two");
113:
114: assertEquals(tracker.getErrors(), Arrays.asList("one", "two"));
115: }
116:
117: @Test
118: public void data_survives_serialization() throws Exception {
119: Field fielda = newFieldWithElementName("fieldA");
120: Field fieldb = newFieldWithElementName("fieldB");
121: Field fieldc = newFieldWithElementName("fieldC");
122:
123: replay();
124:
125: ValidationTracker tracker = new ValidationTrackerImpl();
126:
127: tracker.recordError("one");
128: tracker.recordError(fieldb, "fieldb: two");
129: tracker.recordError("three");
130: tracker.recordError(fielda, "fielda: four");
131:
132: ValidationTracker copy = cloneBySerialiation(tracker);
133:
134: copy.recordError(fieldc, "fieldc: five");
135:
136: assertEquals(copy.getErrors(), Arrays.asList("one", "three",
137: "fieldb: two", "fielda: four", "fieldc: five"));
138:
139: verify();
140: }
141:
142: @Test
143: public void clear_removes_all() {
144: Field fielda = newFieldWithElementName("fieldA");
145: Field fieldb = newFieldWithElementName("fieldB");
146:
147: replay();
148:
149: ValidationTracker tracker = new ValidationTrackerImpl();
150:
151: tracker.recordError("one");
152: tracker.recordInput(fieldb, "input b");
153: tracker.recordError(fieldb, "fieldb: two");
154: tracker.recordError("three");
155: tracker.recordInput(fielda, "input a");
156: tracker.recordError(fielda, "fielda: four");
157:
158: tracker.clear();
159:
160: assertFalse(tracker.getHasErrors());
161: assertTrue(tracker.getErrors().isEmpty());
162: assertNull(tracker.getInput(fielda));
163: assertNull(tracker.getInput(fieldb));
164:
165: verify();
166: }
167:
168: private final Field newFieldWithElementName(String elementName) {
169: Field field = mockField();
170:
171: // Fields generated this way, for the purposes of this test, do not
172: // ever change their elementName. In real life, elementNames can change.
173:
174: expect(field.getElementName()).andReturn(elementName)
175: .atLeastOnce();
176:
177: return field;
178: }
179:
180: @SuppressWarnings("unchecked")
181: protected final <T> T cloneBySerialiation(T input) throws Exception {
182: ByteArrayOutputStream bos = new ByteArrayOutputStream();
183: ObjectOutputStream oos = new ObjectOutputStream(bos);
184:
185: oos.writeObject(input);
186:
187: oos.close();
188:
189: ByteArrayInputStream bis = new ByteArrayInputStream(bos
190: .toByteArray());
191: ObjectInputStream ois = new ObjectInputStream(bis);
192:
193: T result = (T) ois.readObject();
194:
195: ois.close();
196:
197: return result;
198: }
199: }
|