Source Code Cross Referenced for FileUploadFieldTest.java in  » J2EE » wicket » org » apache » wicket » markup » html » form » upload » 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 » wicket » org.apache.wicket.markup.html.form.upload 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         *
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:        package org.apache.wicket.markup.html.form.upload;
018:
019:        import java.io.BufferedOutputStream;
020:        import java.io.FileOutputStream;
021:        import java.io.IOException;
022:        import java.io.InputStream;
023:        import java.io.OutputStream;
024:
025:        import org.apache.wicket.Page;
026:        import org.apache.wicket.RequestCycle;
027:        import org.apache.wicket.WicketTestCase;
028:        import org.apache.wicket.protocol.http.MockHttpServletRequest;
029:        import org.apache.wicket.protocol.http.servlet.MultipartServletWebRequest;
030:        import org.apache.wicket.util.file.File;
031:        import org.apache.wicket.util.lang.Bytes;
032:        import org.apache.wicket.util.tester.ITestPageSource;
033:
034:        /**
035:         * Test of FileUploadField
036:         * 
037:         * @author Frank Bille (billen)
038:         */
039:        public class FileUploadFieldTest extends WicketTestCase {
040:
041:            /**
042:             * Construct.
043:             */
044:            public FileUploadFieldTest() {
045:                super ("Test of FileUploadField");
046:            }
047:
048:            /**
049:             * Test that detach closes the streams
050:             * 
051:             * @throws Exception 
052:             */
053:            public void testInternalDetach() throws Exception {
054:                final MockPageWithFormAndUploadField page = new MockPageWithFormAndUploadField();
055:
056:                FileUploadField field = new FileUploadField("upload");
057:                page.getForm().add(field);
058:
059:                tester.startPage(new ITestPageSource() {
060:                    private static final long serialVersionUID = 1L;
061:
062:                    public Page getTestPage() {
063:                        return page;
064:                    }
065:                });
066:
067:                // Setup the request. It should be a IMultipartWebRequest
068:                RequestCycle requestCycle = tester.createRequestCycle();
069:                MockHttpServletRequest servletRequest = tester
070:                        .getServletRequest();
071:                servletRequest.setMethod("POST");
072:                servletRequest.setParameter("form2:hf:fs", "");
073:                servletRequest.setParameter("wicketState", "");
074:
075:                File tmp = null;
076:                try {
077:                    // Write out a large text file. We need to make this file reasonably sizable,
078:                    // because things get handled using input streams, and we want to check to make
079:                    // sure they're closed properly if we abort mid-request.
080:
081:                    // We create a temp file because we don't want to depend on a file we might not
082:                    // know the path of (e.g. the big DTD this test used previously). This enables
083:                    // us to run the test out of a JAR file if need be, and also with an unknown
084:                    // running directory (e.g. when run from wicket-parent).
085:                    tmp = new File(java.io.File.createTempFile(this .getClass()
086:                            .getName(), ".txt"));
087:                    OutputStream os = new BufferedOutputStream(
088:                            new FileOutputStream(tmp));
089:                    for (int i = 0; i < 1000; i++) {
090:                        os.write("test test test test test\n".getBytes());
091:                    }
092:                    os.close();
093:
094:                    // Let's upload the dtd file. It's large enough to avoid being in memory.
095:                    servletRequest.addFile("upload", tmp, "text/plain");
096:
097:                    requestCycle.setRequest(new MultipartServletWebRequest(
098:                            servletRequest, Bytes.MAX));
099:
100:                    // attach manually for the test
101:                    field.attach();
102:
103:                    // Get the file upload
104:                    FileUpload fileUpload = field.getFileUpload();
105:
106:                    assertNotNull(fileUpload);
107:
108:                    // Get an input stream from the file upload
109:                    InputStream is = fileUpload.getInputStream();
110:
111:                    // We should be able to read a byte
112:                    assertTrue(is.read() != -1);
113:
114:                    field.detach();
115:
116:                    // The input stream should be closed so we shouldn't be able to read any more bytes
117:                    try {
118:                        is.read();
119:                        fail();
120:                    } catch (IOException e) {
121:                        // Expected
122:                    } catch (Exception e) {
123:                        fail();
124:                    }
125:                } finally {
126:                    if (tmp != null && tmp.exists()) {
127:                        tmp.delete();
128:                    }
129:                }
130:            }
131:        }
w_w__w_._ja__v__a_2___s_._c_om | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.