Source Code Cross Referenced for FileUploadInterceptorTest.java in  » J2EE » webwork-2.2.6 » com » opensymphony » webwork » 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 » J2EE » webwork 2.2.6 » com.opensymphony.webwork.interceptor 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (c) 2002-2006 by OpenSymphony
003:         * All rights reserved.
004:         */
005:        package com.opensymphony.webwork.interceptor;
006:
007:        import com.opensymphony.util.ClassLoaderUtil;
008:        import com.opensymphony.webwork.ServletActionContext;
009:        import com.opensymphony.webwork.WebWorkTestCase;
010:        import com.opensymphony.webwork.dispatcher.multipart.MultiPartRequestWrapper;
011:        import com.opensymphony.xwork.*;
012:        import com.opensymphony.xwork.mock.MockActionInvocation;
013:        import org.springframework.mock.web.MockHttpServletRequest;
014:
015:        import javax.servlet.http.HttpServletRequest;
016:        import java.io.File;
017:        import java.io.IOException;
018:        import java.net.URI;
019:        import java.net.URL;
020:        import java.util.HashMap;
021:        import java.util.List;
022:        import java.util.Locale;
023:        import java.util.Map;
024:
025:        /**
026:         * Test case for FileUploadInterceptor.
027:         *
028:         * @author tmjee
029:         * @author Claus Ibsen
030:         * @version $Date: 2007-02-08 02:57:26 +0100 (Thu, 08 Feb 2007) $ $Id: FileUploadInterceptorTest.java 2839 2007-02-08 01:57:26Z tschneider22 $
031:         */
032:        public class FileUploadInterceptorTest extends WebWorkTestCase {
033:
034:            private FileUploadInterceptor interceptor;
035:
036:            public void testAcceptFileWithEmptyAllowedTypes() throws Exception {
037:                // when allowed type is empty
038:                ValidationAwareSupport validation = new ValidationAwareSupport();
039:                boolean ok = interceptor.acceptFile(new File(""), "text/plain",
040:                        "inputName", validation, Locale.getDefault());
041:
042:                assertTrue(ok);
043:                assertTrue(validation.getFieldErrors().isEmpty());
044:                assertFalse(validation.hasErrors());
045:            }
046:
047:            public void testAcceptFileWithoutEmptyTypes() throws Exception {
048:                interceptor.setAllowedTypes("text/plain");
049:
050:                // when file is of allowed types
051:                ValidationAwareSupport validation = new ValidationAwareSupport();
052:                boolean ok = interceptor.acceptFile(new File(""), "text/plain",
053:                        "inputName", validation, Locale.getDefault());
054:
055:                assertTrue(ok);
056:                assertTrue(validation.getFieldErrors().isEmpty());
057:                assertFalse(validation.hasErrors());
058:
059:                // when file is not of allowed types
060:                validation = new ValidationAwareSupport();
061:                boolean notOk = interceptor.acceptFile(new File(""),
062:                        "text/html", "inputName", validation, Locale
063:                                .getDefault());
064:
065:                assertFalse(notOk);
066:                assertFalse(validation.getFieldErrors().isEmpty());
067:                assertTrue(validation.hasErrors());
068:            }
069:
070:            public void testAcceptFileWithNoFile() throws Exception {
071:                FileUploadInterceptor interceptor = new FileUploadInterceptor();
072:                interceptor.setAllowedTypes("text/plain");
073:
074:                // when file is not of allowed types
075:                ValidationAwareSupport validation = new ValidationAwareSupport();
076:                boolean notOk = interceptor.acceptFile(null, "text/html",
077:                        "inputName", validation, Locale.getDefault());
078:
079:                assertFalse(notOk);
080:                assertFalse(validation.getFieldErrors().isEmpty());
081:                assertTrue(validation.hasErrors());
082:                List errors = (List) validation.getFieldErrors().get(
083:                        "inputName");
084:                assertEquals(1, errors.size());
085:                String msg = (String) errors.get(0);
086:                assertTrue(msg.startsWith("Error uploading:"));
087:                assertTrue(msg.indexOf("inputName") > 0);
088:            }
089:
090:            public void testAcceptFileWithMaxSize() throws Exception {
091:                interceptor.setAllowedTypes("text/plain");
092:                interceptor.setMaximumSize(new Long(10));
093:
094:                // when file is not of allowed types
095:                ValidationAwareSupport validation = new ValidationAwareSupport();
096:
097:                URL url = ClassLoaderUtil.getResource("log4j.properties",
098:                        FileUploadInterceptorTest.class);
099:                File file = new File(new URI(url.toString()));
100:                assertTrue("log4j.properties should be in src/test folder",
101:                        file.exists());
102:                boolean notOk = interceptor.acceptFile(file, "text/html",
103:                        "inputName", validation, Locale.getDefault());
104:
105:                assertFalse(notOk);
106:                assertFalse(validation.getFieldErrors().isEmpty());
107:                assertTrue(validation.hasErrors());
108:                List errors = (List) validation.getFieldErrors().get(
109:                        "inputName");
110:                assertEquals(1, errors.size());
111:                String msg = (String) errors.get(0);
112:                // the error message shoul contain at least this test
113:                assertTrue(msg
114:                        .startsWith("The file is to large to be uploaded"));
115:                assertTrue(msg.indexOf("inputName") > 0);
116:                assertTrue(msg.indexOf("log4j.properties") > 0);
117:            }
118:
119:            public void testNoMultipartRequest() throws Exception {
120:                MyFileupAction action = new MyFileupAction();
121:
122:                MockActionInvocation mai = new MockActionInvocation();
123:                mai.setAction(action);
124:                mai.setResultCode("NoMultipart");
125:                mai.setInvocationContext(ActionContext.getContext());
126:
127:                // if no multipart request it will bypass and execute it
128:                assertEquals("NoMultipart", interceptor.intercept(mai));
129:            }
130:
131:            public void testInvalidContentTypeMultipartRequest()
132:                    throws Exception {
133:                MockHttpServletRequest req = new MockHttpServletRequest();
134:
135:                req.setCharacterEncoding("text/html");
136:                req.setContentType("text/xml"); // not a multipart contentype
137:                req.addHeader("Content-type", "multipart/form-data");
138:
139:                MyFileupAction action = new MyFileupAction();
140:                MockActionInvocation mai = new MockActionInvocation();
141:                mai.setAction(action);
142:                mai.setResultCode("success");
143:                mai.setInvocationContext(ActionContext.getContext());
144:
145:                Map param = new HashMap();
146:                ActionContext.getContext().setParameters(param);
147:                ActionContext.getContext().put(
148:                        ServletActionContext.HTTP_REQUEST,
149:                        createMultipartRequest((HttpServletRequest) req, 2000));
150:
151:                interceptor.intercept(mai);
152:
153:                assertTrue(action.hasErrors());
154:            }
155:
156:            public void testNoContentMultipartRequest() throws Exception {
157:                MockHttpServletRequest req = new MockHttpServletRequest();
158:
159:                req.setCharacterEncoding("text/html");
160:                req.setContentType("multipart/form-data; boundary=---1234");
161:                req.setContent(null); // there is no content
162:
163:                MyFileupAction action = new MyFileupAction();
164:                MockActionInvocation mai = new MockActionInvocation();
165:                mai.setAction(action);
166:                mai.setResultCode("success");
167:                mai.setInvocationContext(ActionContext.getContext());
168:
169:                Map param = new HashMap();
170:                ActionContext.getContext().setParameters(param);
171:                ActionContext.getContext().put(
172:                        ServletActionContext.HTTP_REQUEST,
173:                        createMultipartRequest((HttpServletRequest) req, 2000));
174:
175:                interceptor.intercept(mai);
176:
177:                assertTrue(action.hasErrors());
178:            }
179:
180:            public void testSuccessUploadOfATextFileMultipartRequest()
181:                    throws Exception {
182:                if (true)
183:                    return; // this test doesn't work with the build currently, so disable for now
184:
185:                MockHttpServletRequest req = new MockHttpServletRequest();
186:                req.setCharacterEncoding("text/html");
187:                req.setContentType("multipart/form-data; boundary=---1234");
188:                req.addHeader("Content-type", "multipart/form-data");
189:
190:                // inspired by the unit tests for jakarta commons fileupload
191:                String content = ("-----1234\r\n"
192:                        + "Content-Disposition: form-data; name=\"file\"; filename=\"deleteme.txt\"\r\n"
193:                        + "Content-Type: text/html\r\n" + "\r\n"
194:                        + "Unit test of FileUploadInterceptor" + "\r\n"
195:                        + "-----1234--\r\n");
196:                req.setContent(content.getBytes("US-ASCII"));
197:
198:                MyFileupAction action = new MyFileupAction();
199:
200:                MockActionInvocation mai = new MockActionInvocation();
201:                mai.setAction(action);
202:                mai.setResultCode("success");
203:                mai.setInvocationContext(ActionContext.getContext());
204:                Map param = new HashMap();
205:                ActionContext.getContext().setParameters(param);
206:                ActionContext.getContext().put(
207:                        ServletActionContext.HTTP_REQUEST,
208:                        createMultipartRequest((HttpServletRequest) req, 2000));
209:
210:                interceptor.intercept(mai);
211:
212:                assertTrue(!action.hasErrors());
213:
214:                assertTrue(param.size() == 3);
215:                File[] files = (File[]) param.get("file");
216:                String[] fileContentTypes = (String[]) param
217:                        .get("fileContentType");
218:                String[] fileRealFilenames = (String[]) param
219:                        .get("fileFileName");
220:
221:                assertNotNull(files);
222:                assertNotNull(fileContentTypes);
223:                assertNotNull(fileRealFilenames);
224:                assertTrue(files.length == 1);
225:                assertTrue(fileContentTypes.length == 1);
226:                assertTrue(fileRealFilenames.length == 1);
227:                assertEquals("text/html", fileContentTypes[0]);
228:                assertNotNull("deleteme.txt", fileRealFilenames[0]);
229:            }
230:
231:            private MultiPartRequestWrapper createMultipartRequest(
232:                    HttpServletRequest req, int maxsize) throws IOException {
233:                return new MultiPartRequestWrapper(req, "build"
234:                        + File.separator + "test", maxsize);
235:            }
236:
237:            protected void setUp() throws Exception {
238:                super .setUp();
239:                interceptor = new FileUploadInterceptor();
240:            }
241:
242:            protected void tearDown() throws Exception {
243:                interceptor.destroy();
244:                super .tearDown();
245:            }
246:
247:            private class MyFileupAction extends ActionSupport {
248:                // no methods
249:            }
250:
251:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.