001: // Copyright 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.upload.components;
016:
017: import static org.easymock.EasyMock.expectLastCall;
018:
019: import org.apache.tapestry.Field;
020: import org.apache.tapestry.FieldValidator;
021: import org.apache.tapestry.MarkupWriter;
022: import org.apache.tapestry.ValidationDecorator;
023: import org.apache.tapestry.ValidationException;
024: import org.apache.tapestry.ValidationTracker;
025: import org.apache.tapestry.dom.Element;
026: import org.apache.tapestry.services.FormSupport;
027: import org.apache.tapestry.test.TapestryTestCase;
028: import org.apache.tapestry.upload.services.MultipartDecoder;
029: import org.apache.tapestry.upload.services.UploadedFile;
030: import org.testng.annotations.Test;
031:
032: public class UploadTest extends TapestryTestCase {
033:
034: @Test
035: public void upload_is_field() throws Exception {
036: assertTrue(Field.class.isAssignableFrom(Upload.class));
037: }
038:
039: @Test
040: public void begin_render_writes_input_tag() throws Exception {
041: MarkupWriter writer = createMarkupWriter();
042: writer.element("form");
043: FormSupport formSupport = mockFormSupport();
044:
045: formSupport.setEncodingType(Upload.MULTIPART_ENCTYPE);
046:
047: replay();
048:
049: Upload component = new Upload();
050:
051: setValidationDecorator(component, new StubValidationDecorator());
052:
053: setFormSupport(component, formSupport);
054:
055: component.beginRender(writer);
056:
057: Element element = writer.getElement();
058: assertNotNull(element);
059: assertEquals(element.getName(), "input");
060: assertEquals(element.getAttribute("type"), "file");
061: // assertEquals(element.getAttribute("name"),null);
062: // assertEquals(element.getAttribute("id"),null);
063:
064: verify();
065:
066: }
067:
068: protected final FormSupport mockFormSupport() {
069: return newMock(FormSupport.class);
070: }
071:
072: private void setFormSupport(Upload component,
073: FormSupport formSupport) throws IllegalAccessException {
074: setField(component, "_formSupport", formSupport);
075: }
076:
077: @Test
078: public void validation_decorator_invoked_inside_begin_render()
079: throws Exception {
080: Upload component = new Upload();
081: MarkupWriter writer = createMarkupWriter();
082: writer.element("form");
083:
084: FormSupport formSupport = mockFormSupport();
085: formSupport.setEncodingType(Upload.MULTIPART_ENCTYPE);
086: setField(component, "_formSupport", formSupport);
087:
088: ValidationDecorator decorator = mockValidationDecorator();
089:
090: setValidationDecorator(component, decorator);
091:
092: decorator.insideField(component);
093:
094: replay();
095:
096: component.beginRender(writer);
097:
098: verify();
099: }
100:
101: private ValidationDecorator mockValidationDecorator() {
102: return newMock(ValidationDecorator.class);
103: }
104:
105: @Test
106: public void begin_render_invokes_field_validator() throws Exception {
107: FieldValidator<Object> validate = mockFieldValidator();
108: Upload component = new Upload(null, validate, null, null);
109: MarkupWriter writer = createMarkupWriter();
110: writer.element("form");
111:
112: FormSupport formSupport = mockFormSupport();
113: formSupport.setEncodingType(Upload.MULTIPART_ENCTYPE);
114: setField(component, "_formSupport", formSupport);
115:
116: ValidationDecorator decorator = mockValidationDecorator();
117:
118: setValidationDecorator(component, decorator);
119:
120: validate.render(writer);
121: decorator.insideField(component);
122:
123: replay();
124:
125: component.beginRender(writer);
126:
127: verify();
128: }
129:
130: @Test
131: public void after_render_closes_element() throws Exception {
132: Upload component = new Upload();
133: MarkupWriter writer = mockMarkupWriter();
134:
135: expect(writer.end()).andReturn(null);
136:
137: replay();
138:
139: component.afterRender(writer);
140:
141: verify();
142:
143: }
144:
145: @Test
146: public void process_submission_extracts_value_from_decoder()
147: throws Exception {
148: FormSupport formSupport = mockFormSupport();
149: MultipartDecoder decoder = mockMultipartDecoder();
150: UploadedFile uploadedFile = mockUploadedFile();
151:
152: Upload component = new Upload(null, null, decoder, null);
153:
154: expect(decoder.getFileUpload("test")).andReturn(uploadedFile);
155: expect(uploadedFile.getFileName()).andReturn("foo").anyTimes();
156:
157: replay();
158:
159: component.processSubmission(formSupport, "test");
160:
161: verify();
162:
163: assertSame(component.getValue(), uploadedFile);
164: }
165:
166: @Test
167: public void process_submission_ignores_null_value()
168: throws Exception {
169: FormSupport formSupport = mockFormSupport();
170: MultipartDecoder decoder = mockMultipartDecoder();
171: UploadedFile uploadedFile = mockUploadedFile();
172:
173: Upload component = new Upload(null, null, decoder, null);
174:
175: expect(decoder.getFileUpload("test")).andReturn(uploadedFile);
176: expect(uploadedFile.getFileName()).andReturn("").atLeastOnce();
177:
178: replay();
179:
180: component.processSubmission(formSupport, "test");
181:
182: verify();
183:
184: assertNull(component.getValue());
185: }
186:
187: @Test
188: public void process_submission_calls_validator() throws Exception {
189: FormSupport formSupport = mockFormSupport();
190: MultipartDecoder decoder = mockMultipartDecoder();
191: UploadedFile uploadedFile = mockUploadedFile();
192: FieldValidator<Object> validate = mockFieldValidator();
193:
194: Upload component = new Upload(null, validate, decoder, null);
195:
196: expect(decoder.getFileUpload("test")).andReturn(uploadedFile);
197: expect(uploadedFile.getFileName()).andReturn("test")
198: .atLeastOnce();
199: validate.validate(uploadedFile);
200: replay();
201:
202: component.processSubmission(formSupport, "test");
203:
204: verify();
205: }
206:
207: @Test
208: public void process_submission_tracks_validator_errors()
209: throws Exception {
210: FormSupport formSupport = mockFormSupport();
211: MultipartDecoder decoder = mockMultipartDecoder();
212: UploadedFile uploadedFile = mockUploadedFile();
213: FieldValidator<Object> validate = mockFieldValidator();
214: ValidationTracker tracker = mockValidationTracker();
215:
216: Upload component = new Upload(null, validate, decoder, tracker);
217:
218: expect(decoder.getFileUpload("test")).andReturn(uploadedFile);
219: expect(uploadedFile.getFileName()).andReturn("test")
220: .atLeastOnce();
221: validate.validate(uploadedFile);
222: expectLastCall().andThrow(new ValidationException("an error"));
223: tracker.recordError(component, "an error");
224: replay();
225:
226: component.processSubmission(formSupport, "test");
227:
228: verify();
229: }
230:
231: protected final UploadedFile mockUploadedFile() {
232: return newMock(UploadedFile.class);
233: }
234:
235: protected final MultipartDecoder mockMultipartDecoder() {
236: return newMock(MultipartDecoder.class);
237: }
238:
239: private void setValidationDecorator(Upload component,
240: ValidationDecorator decorator)
241: throws IllegalAccessException {
242: setField(component, "_decorator", decorator);
243: }
244:
245: private void setField(Object bean, String name, Object value)
246: throws IllegalAccessException {
247: Class clazz = bean.getClass();
248: java.lang.reflect.Field field = null;
249:
250: while (field == null && clazz != null) {
251: try {
252: field = clazz.getDeclaredField(name);
253: } catch (NoSuchFieldException e) {
254: field = null;
255: }
256: clazz = clazz.getSuperclass();
257: }
258: if (field != null) {
259: field.setAccessible(true);
260: field.set(bean, value);
261: }
262: }
263: }
|