View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.webapp;
21  
22  import org.apache.commons.fileupload.FileItem;
23  import org.apache.myfaces.tobago.internal.mock.servlet.MockHttpServletRequest;
24  import org.apache.myfaces.tobago.internal.webapp.TobagoMultipartFormdataRequest;
25  import org.junit.Assert;
26  import org.junit.Test;
27  
28  import java.io.UnsupportedEncodingException;
29  import java.util.Arrays;
30  import java.util.Enumeration;
31  import java.util.HashMap;
32  import java.util.HashSet;
33  import java.util.Map;
34  import java.util.Set;
35  
36  public class TobagoMultipartFormdataRequestUnitTest {
37  
38    private static final String SNIP = "--";
39  
40    private static final String BOUNDARY = "xxx";
41  
42    private static final String NEWLINE = "\r\n";
43  
44    private TobagoMultipartFormdataRequest request;
45  
46    public TobagoMultipartFormdataRequestUnitTest() throws UnsupportedEncodingException {
47      final String body
48          = SNIP + BOUNDARY + NEWLINE
49          + parameter("color", "red")
50          + parameter("city", "Amsterdam")
51          + parameter("city", "Bonn")
52          + parameter("city", "Pisa")
53          + parameter("color", "green")
54          + fileItem("file", "hello.txt", "Hello World!")
55          + parameter("color", "blue")
56          + parameter("color", "yellow")
57          + parameter("country", "Trinidad & Tobago")
58          + SNIP + BOUNDARY + SNIP + NEWLINE;
59  
60      final MockHttpServletRequest mockRequest
61          = new MockHttpServletRequest(body.getBytes("UTF-8"));
62      mockRequest.setMethod("post");
63  
64      request = new TobagoMultipartFormdataRequest(mockRequest, System.getProperty("java.io.tmpdir"), 1024 * 1024);
65    }
66  
67    private String parameter(final String key, final String value) {
68      return SNIP + BOUNDARY + NEWLINE
69          + "Content-Disposition: form-data; name=\"" + key + "\"" + NEWLINE
70          + NEWLINE
71          + value + NEWLINE;
72    }
73  
74    private String fileItem(final String key, final String filename, final String value) {
75      return SNIP + BOUNDARY + NEWLINE
76          + "Content-Disposition: form-data; name=\""
77          + key + "\"; filename=\"" + filename + "\"" + NEWLINE
78          + "Content-Type: text/plain" + NEWLINE
79          + NEWLINE
80          + value + NEWLINE;
81    }
82  
83    @Test
84    public void testGetFileItem() {
85  
86      final FileItem item = request.getFileItem("file");
87      Assert.assertNotNull(item);
88      Assert.assertEquals("filename", "hello.txt", item.getName());
89      Assert.assertEquals("content", "Hello World!", item.getString());
90    }
91  
92    @Test
93    public void testGetParameter() {
94  
95      Assert.assertEquals("red", request.getParameter("color"));
96      Assert.assertEquals("Amsterdam", request.getParameter("city"));
97      Assert.assertEquals("Trinidad & Tobago", request.getParameter("country"));
98      Assert.assertEquals(null, request.getParameter("empty"));
99    }
100 
101   @Test
102   public void testGetParameterValues() {
103 
104     Set<String> expectedSet;
105     Set<String> actualSet;
106 
107     expectedSet = new HashSet<String>(
108         Arrays.asList("red", "green", "blue", "yellow"));
109     actualSet
110         = new HashSet<String>(Arrays.asList(request.getParameterValues("color")));
111     Assert.assertEquals("color", expectedSet, actualSet);
112 
113     expectedSet = new HashSet<String>(
114         Arrays.asList("Amsterdam", "Bonn", "Pisa"));
115     actualSet = new HashSet<String>(Arrays.asList(request.getParameterValues("city")));
116     Assert.assertEquals("city", expectedSet, actualSet);
117 
118     expectedSet = new HashSet<String>(
119         Arrays.asList("Trinidad & Tobago"));
120     actualSet
121         = new HashSet<String>(Arrays.asList(request.getParameterValues("country")));
122     Assert.assertEquals("country", expectedSet, actualSet);
123 
124     Assert.assertEquals("empty", null, request.getParameterValues("empty"));
125   }
126 
127   @Test
128   public void testGetParameterNames() {
129 
130     final Set<Object> actual = new HashSet<Object>();
131     final Enumeration e = request.getParameterNames();
132     while (e.hasMoreElements()) {
133       actual.add(e.nextElement());
134     }
135 
136     final Set<String> expected = new HashSet<String>(
137         Arrays.asList("color", "city", "country"));
138 
139     Assert.assertEquals(expected, actual);
140   }
141 
142   @Test
143   public void testGetParameterMap() {
144 
145     final Map actual = request.getParameterMap();
146 
147     final Map<String, String[]> expected = new HashMap<String, String[]>();
148     expected.put("color", new String[]{"red", "green", "blue", "yellow"});
149     expected.put("city", new String[]{"Amsterdam", "Bonn", "Pisa"});
150     expected.put("country", new String[]{"Trinidad & Tobago"});
151 
152     Assert.assertEquals(expected.keySet(), actual.keySet());
153 
154     final Set keys = actual.keySet();
155     for (final Object key1 : keys) {
156       final String key = (String) key1;
157       final String[] expectedStrings = expected.get(key);
158       final String[] actualStrings = (String[]) actual.get(key);
159       Assert.assertEquals(expectedStrings.length, actualStrings.length);
160       final Set<String> expectedSet = new HashSet<String>(Arrays.asList(expectedStrings));
161       final Set<String> actualSet = new HashSet<String>(Arrays.asList(actualStrings));
162       Assert.assertEquals(expectedSet, actualSet);
163     }
164   }
165 }