Source Code Cross Referenced for StreamResultTest.java in  » J2EE » webwork-2.2.6 » com » opensymphony » webwork » dispatcher » 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.dispatcher 
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.dispatcher;
006:
007:        import com.opensymphony.util.ClassLoaderUtil;
008:        import com.opensymphony.webwork.ServletActionContext;
009:        import com.opensymphony.xwork.Action;
010:        import com.opensymphony.xwork.ActionContext;
011:        import com.opensymphony.xwork.mock.MockActionInvocation;
012:        import com.opensymphony.xwork.util.OgnlValueStack;
013:        import junit.framework.TestCase;
014:        import org.springframework.mock.web.MockHttpServletResponse;
015:
016:        import java.io.File;
017:        import java.io.FileInputStream;
018:        import java.io.InputStream;
019:        import java.net.URI;
020:        import java.net.URL;
021:
022:        /**
023:         * Unit test for {@link StreamResult}.
024:         *
025:         * @author Claus Ibsen
026:         * @author tm_jee
027:         */
028:        public class StreamResultTest extends TestCase {
029:
030:            private StreamResult result;
031:            private MockHttpServletResponse response;
032:
033:            private MockActionInvocation mai;
034:            private OgnlValueStack stack;
035:            private int contentLength = 0;
036:
037:            public void testStreamResultNoInputName() throws Exception {
038:                result.setParse(false);
039:                result.setInputName(null);
040:
041:                try {
042:                    result.doExecute("helloworld", mai);
043:                    fail("Should have thrown an IllegalArgumentException");
044:                } catch (IllegalArgumentException e) {
045:                    // success
046:                }
047:            }
048:
049:            public void testStreamResultParseNoInputName() throws Exception {
050:                result.setParse(true);
051:                result.setInputName("${top}");
052:
053:                try {
054:                    result.doExecute("helloworld", mai);
055:                    fail("Should have thrown an IllegalArgumentException");
056:                } catch (IllegalArgumentException e) {
057:                    // success
058:                }
059:            }
060:
061:            public void testStreamResultDefault() throws Exception {
062:                result.setInputName("streamForImage");
063:
064:                result.doExecute("helloworld", mai);
065:
066:                assertEquals(null, result.getContentLength());
067:                assertEquals("text/plain", result.getContentType());
068:                assertEquals("streamForImage", result.getInputName());
069:                assertEquals(1024, result.getBufferSize()); // 1024 is default
070:                assertEquals("inline", result.getContentDisposition());
071:
072:                assertEquals("text/plain", response.getContentType());
073:                assertEquals(0, response.getContentLength());
074:                assertEquals("inline", response
075:                        .getHeader("Content-disposition"));
076:            }
077:
078:            public void testStreamResultNoDefault() throws Exception {
079:                // it's not easy to test using easymock as we use getOutputStream on HttpServletResponse.
080:                result.setParse(false);
081:                result.setInputName("streamForImage");
082:                result.setBufferSize(128);
083:                result.setContentLength(String.valueOf(contentLength));
084:                result.setContentDisposition("filename=\"logo.png\"");
085:                result.setContentType("image/jpeg");
086:
087:                result.doExecute("helloworld", mai);
088:
089:                assertEquals(String.valueOf(contentLength), result
090:                        .getContentLength());
091:                assertEquals("image/jpeg", result.getContentType());
092:                assertEquals("streamForImage", result.getInputName());
093:                assertEquals(128, result.getBufferSize());
094:                assertEquals("filename=\"logo.png\"", result
095:                        .getContentDisposition());
096:
097:                assertEquals("image/jpeg", response.getContentType());
098:                assertEquals(contentLength, response.getContentLength());
099:                assertEquals("filename=\"logo.png\"", response
100:                        .getHeader("Content-disposition"));
101:            }
102:
103:            public void testStreamResultParse1() throws Exception {
104:                ///////////////////
105:                result.setParse(true);
106:                // ${...} conditionalParse of Result, returns String, 
107:                // which gets evaluated to the stack, that's how it works.
108:                // We use ${streamForImageAsString} that returns "streamForImage"
109:                // which is a property that returns an InputStream object.
110:                result.setInputName("${streamForImageAsString}");
111:                result.setBufferSize(128);
112:                result.setContentLength(String.valueOf(contentLength));
113:                result.setContentDisposition("filename=\"logo.png\"");
114:                result.setContentType("image/jpeg");
115:
116:                result.doExecute("helloworld", mai);
117:
118:                assertEquals(String.valueOf(contentLength), result
119:                        .getContentLength());
120:                assertEquals("image/jpeg", result.getContentType());
121:                assertEquals("${streamForImageAsString}", result.getInputName());
122:                assertEquals(128, result.getBufferSize());
123:                assertEquals("filename=\"logo.png\"", result
124:                        .getContentDisposition());
125:
126:                assertEquals("image/jpeg", response.getContentType());
127:                assertEquals(contentLength, response.getContentLength());
128:                assertEquals("filename=\"logo.png\"", response
129:                        .getHeader("Content-disposition"));
130:            }
131:
132:            public void testStreamResultParse2() throws Exception {
133:                ///////////////////
134:                result.setParse(true);
135:                // This time we dun use ${...}, so streamForImage will
136:                // be evaluated to the stack, which should reaturn an
137:                // InputStream object, cause there's such a property in 
138:                // the action object itself.
139:                result.setInputName("streamForImage");
140:                result.setBufferSize(128);
141:                result.setContentLength(String.valueOf(contentLength));
142:                result.setContentDisposition("filename=\"logo.png\"");
143:                result.setContentType("image/jpeg");
144:
145:                result.doExecute("helloworld", mai);
146:
147:                assertEquals(String.valueOf(contentLength), result
148:                        .getContentLength());
149:                assertEquals("image/jpeg", result.getContentType());
150:                assertEquals("streamForImage", result.getInputName());
151:                assertEquals(128, result.getBufferSize());
152:                assertEquals("filename=\"logo.png\"", result
153:                        .getContentDisposition());
154:
155:                assertEquals("image/jpeg", response.getContentType());
156:                assertEquals(contentLength, response.getContentLength());
157:                assertEquals("filename=\"logo.png\"", response
158:                        .getHeader("Content-disposition"));
159:            }
160:
161:            protected void setUp() throws Exception {
162:                response = new MockHttpServletResponse();
163:
164:                result = new StreamResult();
165:                stack = new OgnlValueStack();
166:                ActionContext.getContext().setValueStack(stack);
167:
168:                MyImageAction action = new MyImageAction();
169:                contentLength = (int) action.getContentLength();
170:
171:                mai = new com.opensymphony.xwork.mock.MockActionInvocation();
172:                mai.setAction(action);
173:                mai.setStack(stack);
174:                mai.setInvocationContext(ActionContext.getContext());
175:                stack.push(action);
176:
177:                ActionContext.getContext().put(
178:                        ServletActionContext.HTTP_RESPONSE, response);
179:            }
180:
181:            protected void tearDown() {
182:                response = null;
183:                result = null;
184:                stack = null;
185:                contentLength = 0;
186:                mai = null;
187:            }
188:
189:            public class MyImageAction implements  Action {
190:
191:                public InputStream getStreamForImage() throws Exception {
192:                    // just use src/test/log4j.properties as test file 
193:                    URL url = ClassLoaderUtil.getResource("log4j.properties",
194:                            StreamResultTest.class);
195:                    File file = new File(new URI(url.toString()));
196:                    FileInputStream fis = new FileInputStream(file);
197:                    return fis;
198:                }
199:
200:                public String execute() throws Exception {
201:                    return SUCCESS;
202:                }
203:
204:                public long getContentLength() throws Exception {
205:                    URL url = ClassLoaderUtil.getResource("log4j.properties",
206:                            StreamResultTest.class);
207:                    File file = new File(new URI(url.toString()));
208:                    return file.length();
209:                }
210:
211:                public String getStreamForImageAsString() {
212:                    return "streamForImage";
213:                }
214:            }
215:
216:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.