View Javadoc
1   package org.apache.fulcrum.parser;
2   
3   
4   import static org.junit.jupiter.api.Assertions.assertEquals;
5   import static org.junit.jupiter.api.Assertions.assertTrue;
6   import static org.junit.jupiter.api.Assertions.fail;
7   
8   /*
9    * Licensed to the Apache Software Foundation (ASF) under one
10   * or more contributor license agreements.  See the NOTICE file
11   * distributed with this work for additional information
12   * regarding copyright ownership.  The ASF licenses this file
13   * to you under the Apache License, Version 2.0 (the
14   * "License"); you may not use this file except in compliance
15   * with the License.  You may obtain a copy of the License at
16   *
17   *   http://www.apache.org/licenses/LICENSE-2.0
18   *
19   * Unless required by applicable law or agreed to in writing,
20   * software distributed under the License is distributed on an
21   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
22   * KIND, either express or implied.  See the License for the
23   * specific language governing permissions and limitations
24   * under the License.
25   */
26  
27  import static org.mockito.Mockito.when;
28  
29  import java.io.IOException;
30  import java.io.InputStream;
31  import java.util.Arrays;
32  import java.util.Collection;
33  import java.util.List;
34  
35  import javax.servlet.http.HttpServletRequest;
36  import javax.servlet.http.Part;
37  
38  import org.apache.avalon.framework.component.ComponentException;
39  import org.apache.fulcrum.testcontainer.BaseUnit5Test;
40  import org.junit.jupiter.api.BeforeEach;
41  import org.junit.jupiter.api.Test;
42  /**
43   * Basic test that ParameterParser instantiates.
44   *
45   * @author <a href="epugh@opensourceconnections.com">Eric Pugh</a>
46   * @version $Id: ParameterParserTest.java 1837188 2018-07-31 22:23:27Z tv $
47   */
48  public class ParserServiceTest extends BaseUnit5Test
49  {
50  
51      private ParserService parserService;
52      private ParameterParser parameterParser = null;
53      private Part test;
54  
55      @BeforeEach
56      public void setUpBefore() throws Exception
57      {
58          try
59          {
60              parserService = (ParserService)this.lookup(ParserService.ROLE);
61              parameterParser = parserService.getParser(DefaultParameterParser.class);
62              
63              test = getPart("upload-field");
64          }
65          catch (ComponentException e)
66          {
67              e.printStackTrace();
68              fail(e.getMessage());
69          }
70      }
71  
72      private Part getPart( String name )
73      {
74          return new Part()
75          {
76  
77              @Override
78              public void write(String fileName) throws IOException
79              {
80              }
81  
82              @Override
83              public String getSubmittedFileName()
84              {
85                  return null;
86              }
87  
88              @Override
89              public long getSize()
90              {
91                  return 0;
92              }
93  
94              @Override
95              public String getName()
96              {
97                  return name;
98              }
99  
100             @Override
101             public InputStream getInputStream() throws IOException
102             {
103                 return null;
104             }
105 
106             @Override
107             public Collection<String> getHeaders(String name)
108             {
109                 return null;
110             }
111 
112             @Override
113             public Collection<String> getHeaderNames()
114             {
115                 return null;
116             }
117 
118             @Override
119             public String getHeader(String name)
120             {
121                 return null;
122             }
123 
124             @Override
125             public String getContentType()
126             {
127                 return "application/octet-stream";
128             }
129 
130             @Override
131             public void delete() throws IOException
132             {
133             }
134         };
135     }
136 
137     @Test
138     public void testConfiguredAutomaticUpload() throws Exception {
139         assertTrue(parserService.getAutomaticUpload());
140     }
141     @Test
142     public void testConfiguredParameterEncoding() throws Exception {
143         assertEquals("utf-8", parserService.getParameterEncoding());
144     }
145     
146     @Test
147     public void testUploadParts() throws Exception {
148         HttpServletRequest request = getMockRequest();
149         // TODO check
150         when(request.getContentType()).thenReturn("multipart/form-data; boundary=boundary");
151         when(request.getMethod()).thenReturn("POST");
152         parameterParser.add(test.getName(), test);
153         Part secondPart = getPart("second-field");
154         parameterParser.add(secondPart.getName(), secondPart);
155 
156         when(request.getParts()).thenReturn(Arrays.asList(test, secondPart));
157         
158         parameterParser.setRequest(request);
159         
160         List<Part> parts = parserService.parseUpload( request );
161         assertTrue( !parts.isEmpty() );
162         assertTrue( parts.size() == 2 );
163     }
164     
165     @Test
166     public void testNoUploadParts() throws Exception {
167         HttpServletRequest request = getMockRequest();
168         parameterParser.add("other-field", "foo");
169         
170         List<Part> parts = parserService.parseUpload( request );
171         assertTrue( parts.isEmpty() );
172         //assertTrue( parts.size() == 2 );
173     }
174 }