Permalink
Browse files

Improve multipart tests with capture

  • Loading branch information...
1 parent efe6c0c commit af7e2840209889bf2d59bf3e679f5fdbcb6f3ec1 @garcia-jj garcia-jj committed Aug 31, 2012
@@ -79,7 +79,7 @@
private static final Logger logger = LoggerFactory.getLogger(Servlet3MultipartInterceptor.class);
- private static final Pattern EXTRACT_FILENAME = Pattern.compile("(.*)filename=\"(.*)\"");
+ private static final Pattern EXTRACT_FILENAME = Pattern.compile("(.*)name=\"(.*)\"");
public static final String ACCEPT_MULTIPART = "multipart/form-data";
public static final String CONTENT_DISPOSITION_KEY = "content-disposition";
@@ -4,11 +4,10 @@
import static com.google.common.io.ByteStreams.toByteArray;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
-import static org.junit.Assert.assertEquals;
+import static org.hamcrest.Matchers.is;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
@@ -17,9 +16,7 @@
import java.io.IOException;
import java.util.ArrayList;
-import java.util.LinkedHashMap;
import java.util.List;
-import java.util.Map;
import javax.servlet.http.HttpServletRequest;
@@ -30,10 +27,9 @@
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.junit.Before;
import org.junit.Test;
+import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
-import org.mockito.invocation.InvocationOnMock;
-import org.mockito.stubbing.Answer;
import br.com.caelum.vraptor.Validator;
import br.com.caelum.vraptor.core.InterceptorStack;
@@ -267,34 +263,26 @@ public void shouldCreateDirInsideAppIfTempDirAreNotAvailable() throws Exception
}
@Test
- public void checkUploadedFile() throws Exception {
+ public void checkIfFileHasBeenUploaded() throws Exception {
final List<FileItem> elements = new ArrayList<FileItem>();
- elements.add(new MockFileItem("thefile0", "text/plain", "foo.txt", "foo".getBytes()));
+ byte[] content = "foo".getBytes();
+ elements.add(new MockFileItem("thefile0", "text/plain", "file.txt", content));
- final Map<String, Object> attributes = new LinkedHashMap<String, Object>();
-
interceptor = new CommonsUploadMultipartInterceptor(request, parameters, config, validator, mockCreator);
when(request.getContentType()).thenReturn("multipart/form-data");
when(request.getMethod()).thenReturn("POST");
- Answer<Object> answer = new Answer<Object>() {
- public Object answer(InvocationOnMock invocation) throws Throwable {
- Object[] args = invocation.getArguments();
- attributes.put((String) args[0], args[1]);
- return null;
- }
- };
-
- doAnswer(answer).when(request).setAttribute(anyString(), anyString());
when(mockUpload.parseRequest(request)).thenReturn(elements);
interceptor.intercept(stack, method, instance);
- UploadedFile file = (UploadedFile) attributes.get("thefile0");
-
- assertEquals("foo.txt", file.getFileName());
- assertEquals("text/plain", file.getContentType());
- assertEquals("foo", new String(toByteArray(file.getFile())));
+ ArgumentCaptor<UploadedFile> argument = ArgumentCaptor.forClass(UploadedFile.class);
+ verify(request).setAttribute(anyString(), argument.capture());
+
+ UploadedFile file = argument.getValue();
+ assertThat(file.getFileName(), is("file.txt"));
+ assertThat(file.getContentType(), is("text/plain"));
+ assertThat(toByteArray(file.getFile()), is(content));
}
}
@@ -1,7 +1,10 @@
package br.com.caelum.vraptor.interceptor.multipart;
+import static com.google.common.io.ByteStreams.toByteArray;
+import static java.util.Arrays.asList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.is;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
@@ -18,6 +21,7 @@
import org.junit.Before;
import org.junit.Test;
+import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
@@ -163,7 +167,7 @@ public void multipleUpload() throws Exception {
when(request.getContentType()).thenReturn("multipart/form-data");
when(request.getMethod()).thenReturn("POST");
- when(request.getParts()).thenReturn(Arrays.asList(file0, file1));
+ when(request.getParts()).thenReturn(asList(file0, file1));
interceptor.intercept(stack, method, instance);
@@ -175,6 +179,26 @@ public void multipleUpload() throws Exception {
}
@Test
+ public void checkIfFileHasBeenUploaded() throws Exception {
+ byte[] content = "vraptor3".getBytes();
+ Part file0 = new VraptorPart("myfile0[]", "text/plain", "file.txt", content);
+
+ when(request.getContentType()).thenReturn("multipart/form-data");
+ when(request.getMethod()).thenReturn("POST");
+ when(request.getParts()).thenReturn(asList(file0));
+
+ interceptor.intercept(stack, method, instance);
+
+ ArgumentCaptor<UploadedFile> argument = ArgumentCaptor.forClass(UploadedFile.class);
+ verify(request).setAttribute(anyString(), argument.capture());
+
+ UploadedFile file = argument.getValue();
+ assertThat(file.getFileName(), is("file.txt"));
+ assertThat(file.getContentType(), is("text/plain"));
+ assertThat(toByteArray(file.getFile()), is(content));
+ }
+
+ @Test
public void shouldValidateWhenSizeLimitExceededExceptionOccurs() throws Exception {
when(request.getContentType()).thenReturn("multipart/form-data");
when(request.getMethod()).thenReturn("POST");
@@ -1,11 +1,11 @@
package br.com.caelum.vraptor.interceptor.multipart;
import static br.com.caelum.vraptor.interceptor.multipart.Servlet3MultipartInterceptor.CONTENT_DISPOSITION_KEY;
+import static java.util.Arrays.asList;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
-import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
@@ -31,7 +31,7 @@ public VraptorPart(String name, String contentType, String filename, byte[] cont
this.name = name;
this.contentType = contentType;
this.content = content;
- headers.put(CONTENT_DISPOSITION_KEY, Arrays.asList("form-data; name=\"" + filename + "\""));
+ headers.put(CONTENT_DISPOSITION_KEY, asList("form-data; name=\"" + name + "\"; filename=\"" + filename + "\""));
}
public VraptorPart(String name, String content) {

0 comments on commit af7e284

Please sign in to comment.