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.services;
016:
017: import java.io.File;
018: import java.io.InputStream;
019:
020: import org.apache.commons.fileupload.FileItem;
021: import org.apache.commons.io.input.NullInputStream;
022: import org.apache.tapestry.test.TapestryTestCase;
023: import org.testng.annotations.Test;
024:
025: public class UploadedFileItemTest extends TapestryTestCase {
026: @Test
027: public void contentTypeIsFileItemContentType() throws Exception {
028: FileItem item = newMock(FileItem.class);
029: UploadedFileItem uploadedFile = new UploadedFileItem(item);
030:
031: expect(item.getContentType()).andReturn("foo");
032:
033: replay();
034:
035: assertEquals(uploadedFile.getContentType(), "foo");
036:
037: verify();
038: }
039:
040: @Test
041: public void fileNameExtractedFromFileItemName() throws Exception {
042: FileItem item = newMock(FileItem.class);
043: UploadedFileItem uploadedFile = new UploadedFileItem(item);
044:
045: expect(item.getName()).andReturn("foo/blah.txt");
046:
047: replay();
048:
049: assertEquals(uploadedFile.getFileName(), "blah.txt");
050:
051: verify();
052: }
053:
054: @Test
055: public void filePathIsFileItemName() throws Exception {
056: FileItem item = newMock(FileItem.class);
057: UploadedFileItem uploadedFile = new UploadedFileItem(item);
058:
059: expect(item.getName()).andReturn("foo/blah.txt");
060:
061: replay();
062:
063: assertEquals(uploadedFile.getFilePath(), "foo/blah.txt");
064:
065: verify();
066: }
067:
068: @Test
069: public void sizeIsFileItemSize() throws Exception {
070: FileItem item = newMock(FileItem.class);
071: UploadedFileItem uploadedFile = new UploadedFileItem(item);
072:
073: expect(item.getSize()).andReturn(66l);
074:
075: replay();
076:
077: assertEquals(uploadedFile.getSize(), 66);
078:
079: verify();
080: }
081:
082: @Test
083: public void inMemoryIsFileItemInMemory() throws Exception {
084: FileItem item = newMock(FileItem.class);
085: UploadedFileItem uploadedFile = new UploadedFileItem(item);
086:
087: expect(item.isInMemory()).andReturn(true);
088:
089: replay();
090:
091: assertTrue(uploadedFile.isInMemory());
092:
093: verify();
094: }
095:
096: @Test
097: public void streamIsFileItemStream() throws Exception {
098: FileItem item = newMock(FileItem.class);
099: InputStream stream = new NullInputStream(3);
100: UploadedFileItem uploadedFile = new UploadedFileItem(item);
101:
102: expect(item.getInputStream()).andReturn(stream);
103:
104: replay();
105:
106: assertSame(uploadedFile.getStream(), stream);
107:
108: verify();
109: }
110:
111: @Test
112: public void writeUsesFileItemWrite() throws Exception {
113: FileItem item = newMock(FileItem.class);
114: File out = new File("");
115: UploadedFileItem uploadedFile = new UploadedFileItem(item);
116:
117: item.write(out);
118:
119: replay();
120:
121: uploadedFile.write(out);
122:
123: verify();
124:
125: }
126:
127: @Test
128: public void cleanupCallsFileItemDelete() throws Exception {
129: FileItem item = newMock(FileItem.class);
130: UploadedFileItem uploadedFile = new UploadedFileItem(item);
131: item.delete();
132:
133: replay();
134: uploadedFile.cleanup();
135: verify();
136: }
137:
138: }
|