Skip to content

Instantly share code, notes, and snippets.

@froop
Created March 20, 2012 01:02
Show Gist options
  • Save froop/2129348 to your computer and use it in GitHub Desktop.
Save froop/2129348 to your computer and use it in GitHub Desktop.
[java][Servlet] Apache Commons FileUpload の単体テスト用FileItemスタブ
import org.apache.commons.fileupload.FileItem;
public class FileUploadStub {
class FormFileStub extends FileItemStub {
private final String fileName;
private final byte[] fileData;
public FormFileStub(String fieldName, String fileName, byte[] fileData) {
super(fieldName);
this.fileName = fileName;
this.fileData = fileData.clone();
}
@Override
public String getName() {
return fileName;
}
@Override
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(fileData);
}
@Override
public boolean isFormField() {
return false;
}
private static final long serialVersionUID = 1L;
}
class FormFieldStub extends FileItemStub {
private final String value;
public FormFieldStub(String fieldName, String value) {
super(fieldName);
this.value = value;
}
@Override
public String getString(String encoding)
throws UnsupportedEncodingException {
return value;
}
@Override
public boolean isFormField() {
return true;
}
private static final long serialVersionUID = 1L;
}
private abstract class FileItemStub implements FileItem {
private final String fieldName;
public FileItemStub(String fieldName) {
this.fieldName = fieldName;
}
@Override
public String getFieldName() {
return fieldName;
}
@Override
public InputStream getInputStream() throws IOException {
throw new UnsupportedOperationException();
}
@Override
public String getContentType() {
throw new UnsupportedOperationException();
}
@Override
public String getName() {
throw new UnsupportedOperationException();
}
@Override
public boolean isInMemory() {
throw new UnsupportedOperationException();
}
@Override
public long getSize() {
throw new UnsupportedOperationException();
}
@Override
public byte[] get() {
throw new UnsupportedOperationException();
}
@Override
public String getString(String encoding)
throws UnsupportedEncodingException {
throw new UnsupportedOperationException();
}
@Override
public String getString() {
throw new UnsupportedOperationException();
}
@Override
public void write(File file) throws Exception {
throw new UnsupportedOperationException();
}
@Override
public void delete() {
throw new UnsupportedOperationException();
}
@Override
public void setFieldName(String name) {
throw new UnsupportedOperationException();
}
@Override
public boolean isFormField() {
throw new UnsupportedOperationException();
}
@Override
public void setFormField(boolean state) {
throw new UnsupportedOperationException();
}
@Override
public OutputStream getOutputStream() throws IOException {
throw new UnsupportedOperationException();
}
private static final long serialVersionUID = 1L;
}
@Test
public void testFormFileStub() throws IOException {
FileItem item = new FormFileStub("Field1", "NameA", "DataA".getBytes());
assertEquals("Field1", item.getFieldName());
assertEquals("NameA", item.getName());
assertEquals("DataA", IOUtils.toString(item.getInputStream()));
assertFalse(item.isFormField());
}
@Test
public void testFormFieldStub() throws IOException {
FileItem item = new FormFieldStub("Field2", "ValueA");
assertEquals("Field2", item.getFieldName());
assertEquals("ValueA", item.getString("UTF-8"));
assertTrue(item.isFormField());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment