1 package info.magnolia.cms.filters; 2 3 import info.magnolia.cms.beans.runtime.Document; 4 import info.magnolia.cms.beans.runtime.MultipartForm; 5 import info.magnolia.cms.core.SystemProperty; 6 import info.magnolia.test.MagnoliaTestUtils; 7 8 import java.io.ByteArrayOutputStream ; 9 import java.io.File ; 10 import java.io.InputStream ; 11 12 import javax.servlet.Filter ; 13 import javax.servlet.FilterChain ; 14 import javax.servlet.ServletException ; 15 import javax.servlet.http.HttpServletRequest ; 16 import javax.servlet.http.HttpServletResponse ; 17 18 import junit.framework.TestCase; 19 20 import org.apache.commons.httpclient.methods.PostMethod; 21 import org.apache.commons.httpclient.methods.multipart.FilePart; 22 import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; 23 import org.apache.commons.httpclient.methods.multipart.Part; 24 import org.apache.commons.httpclient.methods.multipart.StringPart; 25 import org.slf4j.Logger; import org.slf4j.LoggerFactory; 26 27 import com.mockrunner.mock.web.MockFilterChain; 28 import com.mockrunner.mock.web.MockHttpServletRequest; 29 import com.mockrunner.mock.web.MockHttpServletResponse; 30 31 32 37 public class MultipartRequestFilterTest extends TestCase { 38 39 42 private static Logger log = LoggerFactory.getLogger(MultipartRequestFilterTest.class); 43 44 private Filter filter; 45 46 protected void setUp() throws Exception { 47 super.setUp(); 48 49 SystemProperty.setProperty(SystemProperty.MAGNOLIA_UPLOAD_TMPDIR, MagnoliaTestUtils.getProjectRoot() 50 + "/target"); 51 52 } 53 54 protected void tearDown() throws Exception { 55 super.tearDown(); 56 this.filter = null; 57 } 58 59 public void testFilterCOS() throws Throwable { 60 this.filter = new MultipartRequestFilter(); 61 doTest(); 62 } 63 64 public void testFilterCommonsFileUpload() throws Throwable { 65 this.filter = new CommonsFileUploadMultipartRequestFilter(); 66 doTest(); 67 } 68 69 public void doTest() throws Throwable { 70 HttpServletRequest request = newHttpServletRequest(); 71 HttpServletResponse response = newHttpServletResponse(); 72 FilterChain chain = newFilterChain(); 73 74 try { 75 this.filter.doFilter(request, response, chain); 76 } 77 catch (ServletException e) { 78 throw e.getRootCause(); 79 } 80 81 MultipartForm form = (MultipartForm) request.getAttribute(MultipartForm.REQUEST_ATTRIBUTE_NAME); 82 assertNotNull("MultipartForm request attribute expected", form); 83 assertEquals(3, form.getParameters().size()); 84 assertEquals("value1", form.getParameter("param1")); 85 assertEquals("àèìòù", form.getParameter("param2")); 86 87 String [] value3 = form.getParameterValues("param3"); 88 assertNotNull("multi-value parameter has not been parsed", value3); 89 assertEquals(2, value3.length); 90 91 assertEquals(1, form.getDocuments().size()); 92 93 int expectedDocumentSize = 1495; 94 Document document = form.getDocument("document"); 95 assertNotNull("expected non-null Document", document); 96 assertEquals("document", document.getAtomName()); 97 assertEquals("xml", document.getExtension()); 98 assertEquals("log4j", document.getFileName()); 99 assertEquals("log4j.xml", document.getFileNameWithExtension()); 100 assertEquals(expectedDocumentSize, document.getLength()); 101 102 105 108 assertTrue(document.getType().startsWith("text/xml")); 109 110 File file1 = document.getFile(); 111 assertTrue(file1.exists()); 112 assertTrue(file1.canRead()); 113 InputStream stream1 = document.getStream(); 114 assertEquals(expectedDocumentSize, stream1.available()); 115 assertEquals(expectedDocumentSize, stream1.skip(expectedDocumentSize)); 116 assertEquals(0, stream1.available()); 117 } 118 119 private FilterChain newFilterChain() { 120 MockFilterChain chain = new MockFilterChain(); 121 122 return chain; 123 } 124 125 private HttpServletRequest newHttpServletRequest() throws Exception { 126 MultipartRequestEntity multipart = newMultipartRequestEntity(); 127 ByteArrayOutputStream output = new ByteArrayOutputStream (); 128 multipart.writeRequest(output); 129 130 MockHttpServletRequest request = new MockHttpServletRequest(); 131 request.setContentType(multipart.getContentType()); 132 request.setHeader("Content-type", multipart.getContentType()); 133 request.setContentLength((int) multipart.getContentLength()); 134 request.setBodyContent(output.toByteArray()); 135 request.setCharacterEncoding("UTF-8"); 136 137 return request; 138 } 139 140 private HttpServletResponse newHttpServletResponse() { 141 MockHttpServletResponse response = new MockHttpServletResponse(); 142 143 return response; 144 } 145 146 private MultipartRequestEntity newMultipartRequestEntity() throws Exception { 147 File f1 = new File (MagnoliaTestUtils.getProjectRoot() + "/src/test-resources/log4j.xml"); 148 149 PostMethod method = new PostMethod(); 150 Part[] parts = { 151 new StringPart("param1", "value1", "UTF-8"), 152 new StringPart("param2", "àèìòù", "UTF-8"), 153 new StringPart("param3", "value3a", "UTF-8"), 154 new StringPart("param3", "value3b", "UTF-8"), 155 new FilePart("document", f1, "text/xml", "UTF-8")}; 156 157 return new MultipartRequestEntity(parts, method.getParams()); 158 } 159 } 160 | Popular Tags |