KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > filters > MultipartRequestFilterTest


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 JavaDoc;
9 import java.io.File JavaDoc;
10 import java.io.InputStream JavaDoc;
11
12 import javax.servlet.Filter JavaDoc;
13 import javax.servlet.FilterChain JavaDoc;
14 import javax.servlet.ServletException JavaDoc;
15 import javax.servlet.http.HttpServletRequest JavaDoc;
16 import javax.servlet.http.HttpServletResponse JavaDoc;
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 /**
33  * @author Andreas Brenk
34  * @author Fabrizio Giustina
35  * @version $Id: MultipartRequestFilterTest.java 6341 2006-09-12 09:18:27Z philipp $
36  */

37 public class MultipartRequestFilterTest extends TestCase {
38
39     /**
40      * Logger.
41      */

42     private static Logger log = LoggerFactory.getLogger(MultipartRequestFilterTest.class);
43
44     private Filter JavaDoc filter;
45
46     protected void setUp() throws Exception JavaDoc {
47         super.setUp();
48
49         SystemProperty.setProperty(SystemProperty.MAGNOLIA_UPLOAD_TMPDIR, MagnoliaTestUtils.getProjectRoot()
50             + "/target");
51
52     }
53
54     protected void tearDown() throws Exception JavaDoc {
55         super.tearDown();
56         this.filter = null;
57     }
58
59     public void testFilterCOS() throws Throwable JavaDoc {
60         this.filter = new MultipartRequestFilter();
61         doTest();
62     }
63
64     public void testFilterCommonsFileUpload() throws Throwable JavaDoc {
65         this.filter = new CommonsFileUploadMultipartRequestFilter();
66         doTest();
67     }
68
69     public void doTest() throws Throwable JavaDoc {
70         HttpServletRequest JavaDoc request = newHttpServletRequest();
71         HttpServletResponse JavaDoc response = newHttpServletResponse();
72         FilterChain JavaDoc chain = newFilterChain();
73
74         try {
75             this.filter.doFilter(request, response, chain);
76         }
77         catch (ServletException JavaDoc 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 JavaDoc[] 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         // commons file upload
103
// assertEquals("text/xml; charset=UTF-8", document.getType());
104

105         // COS
106
// assertEquals("text/xml", document.getType());
107

108         assertTrue(document.getType().startsWith("text/xml"));
109
110         File JavaDoc file1 = document.getFile();
111         assertTrue(file1.exists());
112         assertTrue(file1.canRead());
113         InputStream JavaDoc stream1 = document.getStream();
114         assertEquals(expectedDocumentSize, stream1.available());
115         assertEquals(expectedDocumentSize, stream1.skip(expectedDocumentSize));
116         assertEquals(0, stream1.available());
117     }
118
119     private FilterChain JavaDoc newFilterChain() {
120         MockFilterChain chain = new MockFilterChain();
121
122         return chain;
123     }
124
125     private HttpServletRequest JavaDoc newHttpServletRequest() throws Exception JavaDoc {
126         MultipartRequestEntity multipart = newMultipartRequestEntity();
127         ByteArrayOutputStream JavaDoc output = new ByteArrayOutputStream JavaDoc();
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 JavaDoc newHttpServletResponse() {
141         MockHttpServletResponse response = new MockHttpServletResponse();
142
143         return response;
144     }
145
146     private MultipartRequestEntity newMultipartRequestEntity() throws Exception JavaDoc {
147         File JavaDoc f1 = new File JavaDoc(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