Source Code Cross Referenced for FileUploadInterceptorTest.java in  » Web-Framework » struts-2.0.11 » org » apache » struts2 » interceptor » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Framework » struts 2.0.11 » org.apache.struts2.interceptor 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Id: FileUploadInterceptorTest.java 484717 2006-12-08 19:57:59Z mrdon $
003:         *
004:         * Licensed to the Apache Software Foundation (ASF) under one
005:         * or more contributor license agreements.  See the NOTICE file
006:         * distributed with this work for additional information
007:         * regarding copyright ownership.  The ASF licenses this file
008:         * to you under the Apache License, Version 2.0 (the
009:         * "License"); you may not use this file except in compliance
010:         * with the License.  You may obtain a copy of the License at
011:         *
012:         *  http://www.apache.org/licenses/LICENSE-2.0
013:         *
014:         * Unless required by applicable law or agreed to in writing,
015:         * software distributed under the License is distributed on an
016:         * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017:         * KIND, either express or implied.  See the License for the
018:         * specific language governing permissions and limitations
019:         * under the License.
020:         */
021:        package org.apache.struts2.interceptor;
022:
023:        import java.io.File;
024:        import java.io.IOException;
025:        import java.net.URI;
026:        import java.net.URL;
027:        import java.util.HashMap;
028:        import java.util.List;
029:        import java.util.Locale;
030:        import java.util.Map;
031:
032:        import javax.servlet.http.HttpServletRequest;
033:
034:        import org.apache.struts2.ServletActionContext;
035:        import org.apache.struts2.StrutsTestCase;
036:        import org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest;
037:        import org.apache.struts2.dispatcher.multipart.MultiPartRequest;
038:        import org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper;
039:        import org.springframework.mock.web.MockHttpServletRequest;
040:
041:        import com.opensymphony.xwork2.util.ClassLoaderUtil;
042:        import com.opensymphony.xwork2.ActionContext;
043:        import com.opensymphony.xwork2.ActionSupport;
044:        import com.opensymphony.xwork2.ValidationAwareSupport;
045:        import com.opensymphony.xwork2.mock.MockActionInvocation;
046:
047:        /**
048:         * Test case for FileUploadInterceptor.
049:         *
050:         */
051:        public class FileUploadInterceptorTest extends StrutsTestCase {
052:
053:            private FileUploadInterceptor interceptor;
054:            private File tempDir;
055:
056:            public void testAcceptFileWithEmptyAllowedTypes() throws Exception {
057:                // when allowed type is empty
058:                ValidationAwareSupport validation = new ValidationAwareSupport();
059:                boolean ok = interceptor.acceptFile(new File(""), "text/plain",
060:                        "inputName", validation, Locale.getDefault());
061:
062:                assertTrue(ok);
063:                assertTrue(validation.getFieldErrors().isEmpty());
064:                assertFalse(validation.hasErrors());
065:            }
066:
067:            public void testAcceptFileWithoutEmptyTypes() throws Exception {
068:                interceptor.setAllowedTypes("text/plain");
069:
070:                // when file is of allowed types
071:                ValidationAwareSupport validation = new ValidationAwareSupport();
072:                boolean ok = interceptor.acceptFile(new File(""), "text/plain",
073:                        "inputName", validation, Locale.getDefault());
074:
075:                assertTrue(ok);
076:                assertTrue(validation.getFieldErrors().isEmpty());
077:                assertFalse(validation.hasErrors());
078:
079:                // when file is not of allowed types
080:                validation = new ValidationAwareSupport();
081:                boolean notOk = interceptor.acceptFile(new File(""),
082:                        "text/html", "inputName", validation, Locale
083:                                .getDefault());
084:
085:                assertFalse(notOk);
086:                assertFalse(validation.getFieldErrors().isEmpty());
087:                assertTrue(validation.hasErrors());
088:            }
089:
090:            public void testAcceptFileWithNoFile() throws Exception {
091:                FileUploadInterceptor interceptor = new FileUploadInterceptor();
092:                interceptor.setAllowedTypes("text/plain");
093:
094:                // when file is not of allowed types
095:                ValidationAwareSupport validation = new ValidationAwareSupport();
096:                boolean notOk = interceptor.acceptFile(null, "text/html",
097:                        "inputName", validation, Locale.getDefault());
098:
099:                assertFalse(notOk);
100:                assertFalse(validation.getFieldErrors().isEmpty());
101:                assertTrue(validation.hasErrors());
102:                List errors = (List) validation.getFieldErrors().get(
103:                        "inputName");
104:                assertEquals(1, errors.size());
105:                String msg = (String) errors.get(0);
106:                assertTrue(msg.startsWith("Error uploading:"));
107:                assertTrue(msg.indexOf("inputName") > 0);
108:            }
109:
110:            public void testAcceptFileWithMaxSize() throws Exception {
111:                interceptor.setAllowedTypes("text/plain");
112:                interceptor.setMaximumSize(new Long(10));
113:
114:                // when file is not of allowed types
115:                ValidationAwareSupport validation = new ValidationAwareSupport();
116:
117:                URL url = ClassLoaderUtil.getResource("log4j.properties",
118:                        FileUploadInterceptorTest.class);
119:                File file = new File(new URI(url.toString()));
120:                assertTrue("log4j.properties should be in src/test folder",
121:                        file.exists());
122:                boolean notOk = interceptor.acceptFile(file, "text/html",
123:                        "inputName", validation, Locale.getDefault());
124:
125:                assertFalse(notOk);
126:                assertFalse(validation.getFieldErrors().isEmpty());
127:                assertTrue(validation.hasErrors());
128:                List errors = (List) validation.getFieldErrors().get(
129:                        "inputName");
130:                assertEquals(1, errors.size());
131:                String msg = (String) errors.get(0);
132:                // the error message shoul contain at least this test
133:                assertTrue(msg
134:                        .startsWith("The file is to large to be uploaded"));
135:                assertTrue(msg.indexOf("inputName") > 0);
136:                assertTrue(msg.indexOf("log4j.properties") > 0);
137:            }
138:
139:            public void testNoMultipartRequest() throws Exception {
140:                MyFileupAction action = new MyFileupAction();
141:
142:                MockActionInvocation mai = new MockActionInvocation();
143:                mai.setAction(action);
144:                mai.setResultCode("NoMultipart");
145:                mai.setInvocationContext(ActionContext.getContext());
146:
147:                // if no multipart request it will bypass and execute it
148:                assertEquals("NoMultipart", interceptor.intercept(mai));
149:            }
150:
151:            public void testInvalidContentTypeMultipartRequest()
152:                    throws Exception {
153:                MockHttpServletRequest req = new MockHttpServletRequest();
154:
155:                req.setCharacterEncoding("text/html");
156:                req.setContentType("text/xml"); // not a multipart contentype
157:                req.addHeader("Content-type", "multipart/form-data");
158:
159:                MyFileupAction action = new MyFileupAction();
160:                MockActionInvocation mai = new MockActionInvocation();
161:                mai.setAction(action);
162:                mai.setResultCode("success");
163:                mai.setInvocationContext(ActionContext.getContext());
164:
165:                Map param = new HashMap();
166:                ActionContext.getContext().setParameters(param);
167:                ActionContext.getContext().put(
168:                        ServletActionContext.HTTP_REQUEST,
169:                        createMultipartRequest((HttpServletRequest) req, 2000));
170:
171:                interceptor.intercept(mai);
172:
173:                assertTrue(action.hasErrors());
174:            }
175:
176:            public void testNoContentMultipartRequest() throws Exception {
177:                MockHttpServletRequest req = new MockHttpServletRequest();
178:
179:                req.setCharacterEncoding("text/html");
180:                req.setContentType("multipart/form-data; boundary=---1234");
181:                req.setContent(null); // there is no content
182:
183:                MyFileupAction action = new MyFileupAction();
184:                MockActionInvocation mai = new MockActionInvocation();
185:                mai.setAction(action);
186:                mai.setResultCode("success");
187:                mai.setInvocationContext(ActionContext.getContext());
188:
189:                Map param = new HashMap();
190:                ActionContext.getContext().setParameters(param);
191:                ActionContext.getContext().put(
192:                        ServletActionContext.HTTP_REQUEST,
193:                        createMultipartRequest((HttpServletRequest) req, 2000));
194:
195:                interceptor.intercept(mai);
196:
197:                assertTrue(action.hasErrors());
198:            }
199:
200:            public void testSuccessUploadOfATextFileMultipartRequest()
201:                    throws Exception {
202:                MockHttpServletRequest req = new MockHttpServletRequest();
203:                req.setCharacterEncoding("text/html");
204:                req.setContentType("multipart/form-data; boundary=---1234");
205:                req.addHeader("Content-type", "multipart/form-data");
206:
207:                // inspired by the unit tests for jakarta commons fileupload
208:                String content = ("-----1234\r\n"
209:                        + "Content-Disposition: form-data; name=\"file\"; filename=\"deleteme.txt\"\r\n"
210:                        + "Content-Type: text/html\r\n" + "\r\n"
211:                        + "Unit test of FileUploadInterceptor" + "\r\n"
212:                        + "-----1234--\r\n");
213:                req.setContent(content.getBytes("US-ASCII"));
214:
215:                MyFileupAction action = new MyFileupAction();
216:
217:                MockActionInvocation mai = new MockActionInvocation();
218:                mai.setAction(action);
219:                mai.setResultCode("success");
220:                mai.setInvocationContext(ActionContext.getContext());
221:                Map param = new HashMap();
222:                ActionContext.getContext().setParameters(param);
223:                ActionContext.getContext().put(
224:                        ServletActionContext.HTTP_REQUEST,
225:                        createMultipartRequest((HttpServletRequest) req, 2000));
226:
227:                interceptor.intercept(mai);
228:
229:                assertTrue(!action.hasErrors());
230:
231:                assertTrue(param.size() == 3);
232:                File[] files = (File[]) param.get("file");
233:                String[] fileContentTypes = (String[]) param
234:                        .get("fileContentType");
235:                String[] fileRealFilenames = (String[]) param
236:                        .get("fileFileName");
237:
238:                assertNotNull(files);
239:                assertNotNull(fileContentTypes);
240:                assertNotNull(fileRealFilenames);
241:                assertTrue(files.length == 1);
242:                assertTrue(fileContentTypes.length == 1);
243:                assertTrue(fileRealFilenames.length == 1);
244:                assertEquals("text/html", fileContentTypes[0]);
245:                assertNotNull("deleteme.txt", fileRealFilenames[0]);
246:            }
247:
248:            private MultiPartRequestWrapper createMultipartRequest(
249:                    HttpServletRequest req, int maxsize) throws IOException {
250:                JakartaMultiPartRequest jak = new JakartaMultiPartRequest();
251:                jak.setMaxSize(String.valueOf(maxsize));
252:                return new MultiPartRequestWrapper(jak, req, tempDir
253:                        .getAbsolutePath());
254:            }
255:
256:            protected void setUp() throws Exception {
257:                super .setUp();
258:                interceptor = new FileUploadInterceptor();
259:                tempDir = File.createTempFile("struts", "fileupload");
260:                tempDir.delete();
261:                tempDir.mkdirs();
262:            }
263:
264:            protected void tearDown() throws Exception {
265:                tempDir.delete();
266:                interceptor.destroy();
267:                super .tearDown();
268:            }
269:
270:            private class MyFileupAction extends ActionSupport {
271:
272:                private static final long serialVersionUID = 6255238895447968889L;
273:
274:                // no methods
275:            }
276:
277:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.