- added getParts() + test
git-svn-id: https://svn.apache.org/repos/asf/turbine/fulcrum/trunk/parser@1837808 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/pom.xml b/pom.xml
index 34ad201..70937df 100644
--- a/pom.xml
+++ b/pom.xml
@@ -109,6 +109,12 @@
<version>1.0.6</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-core</artifactId>
+ <version>2.18.3</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
diff --git a/src/java/org/apache/fulcrum/parser/DefaultParameterParser.java b/src/java/org/apache/fulcrum/parser/DefaultParameterParser.java
index e9942d8..7e3a9ba 100644
--- a/src/java/org/apache/fulcrum/parser/DefaultParameterParser.java
+++ b/src/java/org/apache/fulcrum/parser/DefaultParameterParser.java
@@ -20,9 +20,12 @@
*/
import java.net.URLDecoder;
+import java.util.Arrays;
+import java.util.Collection;
import java.util.Enumeration;
import java.util.List;
import java.util.StringTokenizer;
+import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
@@ -312,4 +315,14 @@
return null;
}
}
+
+ @Override
+ public Collection<Part> getParts()
+ {
+ return parameters.values().stream().
+ filter( p-> p instanceof Part[]).
+ flatMap(c -> Arrays.stream( (Part[]) c )).
+ collect( Collectors.toList() );
+
+ }
}
diff --git a/src/java/org/apache/fulcrum/parser/ParameterParser.java b/src/java/org/apache/fulcrum/parser/ParameterParser.java
index 4f64f79..331de69 100644
--- a/src/java/org/apache/fulcrum/parser/ParameterParser.java
+++ b/src/java/org/apache/fulcrum/parser/ParameterParser.java
@@ -19,6 +19,8 @@
* under the License.
*/
+import java.util.Collection;
+
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
@@ -117,4 +119,13 @@
* @return A Part[].
*/
Part[] getParts(String name);
+
+ /**
+ * Return an array of all Part objects. If no parts
+ * exist or the object stored is not a Part
+ * array, return an empty list.
+
+ * @return Collection<Part>
+ */
+ Collection<Part> getParts();
}
diff --git a/src/test/org/apache/fulcrum/parser/BaseValueParserTest.java b/src/test/org/apache/fulcrum/parser/BaseValueParserTest.java
index 266ee34..94e7299 100644
--- a/src/test/org/apache/fulcrum/parser/BaseValueParserTest.java
+++ b/src/test/org/apache/fulcrum/parser/BaseValueParserTest.java
@@ -26,8 +26,6 @@
* specific language governing permissions and limitations
* under the License.
*/
-
-
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Locale;
diff --git a/src/test/org/apache/fulcrum/parser/ParameterParserTest.java b/src/test/org/apache/fulcrum/parser/ParameterParserTest.java
index 02470d4..9625f38 100644
--- a/src/test/org/apache/fulcrum/parser/ParameterParserTest.java
+++ b/src/test/org/apache/fulcrum/parser/ParameterParserTest.java
@@ -46,16 +46,78 @@
public class ParameterParserTest extends BaseUnit4Test
{
private ParameterParser parameterParser = null;
-
- private ParserService parserService;
+
+ private Part test;
@Before
public void setUpBefore() throws Exception
{
try
{
- parserService = (ParserService)this.lookup(ParserService.ROLE);
+ ParserService parserService = (ParserService)this.lookup(ParserService.ROLE);
parameterParser = parserService.getParser(DefaultParameterParser.class);
+
+ test = new Part()
+ {
+
+ @Override
+ public void write(String fileName) throws IOException
+ {
+ }
+
+ @Override
+ public String getSubmittedFileName()
+ {
+ return null;
+ }
+
+ @Override
+ public long getSize()
+ {
+ return 0;
+ }
+
+ @Override
+ public String getName()
+ {
+ return "upload-field";
+ }
+
+ @Override
+ public InputStream getInputStream() throws IOException
+ {
+ return null;
+ }
+
+ @Override
+ public Collection<String> getHeaders(String name)
+ {
+ return null;
+ }
+
+ @Override
+ public Collection<String> getHeaderNames()
+ {
+ return null;
+ }
+
+ @Override
+ public String getHeader(String name)
+ {
+ return null;
+ }
+
+ @Override
+ public String getContentType()
+ {
+ return "application/octet-stream";
+ }
+
+ @Override
+ public void delete() throws IOException
+ {
+ }
+ };
}
catch (ComponentException e)
{
@@ -69,14 +131,6 @@
{
assertTrue(parameterParser.getUrlFolding() == URLCaseFolding.NONE);
}
- @Test
- public void testConfiguredAutomaticUpload() throws Exception {
- assertTrue(parserService.getAutomaticUpload());
- }
- @Test
- public void testConfiguredParameterEncoding() throws Exception {
- assertEquals("utf-8", parserService.getParameterEncoding());
- }
/**
* Simple test to verify that URL Case Folding works properly
@@ -103,68 +157,6 @@
{
assertEquals("keySet() is not empty!", 0, parameterParser.keySet().size());
- Part test = new Part()
- {
-
- @Override
- public void write(String fileName) throws IOException
- {
- }
-
- @Override
- public String getSubmittedFileName()
- {
- return null;
- }
-
- @Override
- public long getSize()
- {
- return 0;
- }
-
- @Override
- public String getName()
- {
- return "upload-field";
- }
-
- @Override
- public InputStream getInputStream() throws IOException
- {
- return null;
- }
-
- @Override
- public Collection<String> getHeaders(String name)
- {
- return null;
- }
-
- @Override
- public Collection<String> getHeaderNames()
- {
- return null;
- }
-
- @Override
- public String getHeader(String name)
- {
- return null;
- }
-
- @Override
- public String getContentType()
- {
- return "application/octet-stream";
- }
-
- @Override
- public void delete() throws IOException
- {
- }
- };
-
// Push this into the parser using DefaultParameterParser's add() method.
((DefaultParameterParser) parameterParser).add("upload-field", test);
@@ -188,5 +180,11 @@
// The following will actually cause a ClassCastException because getStrings() (and others) are not catering for Parts.
assertNull("The returned should be null because a Part is not a String", parameterParser.getStrings("upload-field"));
assertFalse(parameterParser.containsKey("missing-field"));
+
+ // The following will actually cause a ClassCastException because getPart() (and others) are not catering for Non-Parts, e.g String.
+ assertNull("The returned should be null because a String is not a Part", parameterParser.getPart( "other-field" ));
+ Part uploadField = parameterParser.getPart( "upload-field" );
+ assertTrue(uploadField.getName().equals( "upload-field" ));
}
+
}
diff --git a/src/test/org/apache/fulcrum/parser/ParserServiceTest.java b/src/test/org/apache/fulcrum/parser/ParserServiceTest.java
new file mode 100644
index 0000000..f9676ad
--- /dev/null
+++ b/src/test/org/apache/fulcrum/parser/ParserServiceTest.java
@@ -0,0 +1,213 @@
+package org.apache.fulcrum.parser;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Vector;
+
+import javax.servlet.ReadListener;
+import javax.servlet.ServletInputStream;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpSession;
+import javax.servlet.http.Part;
+
+import org.apache.avalon.framework.component.ComponentException;
+import org.apache.fulcrum.testcontainer.BaseUnit4Test;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+/**
+ * Basic test that ParameterParser instantiates.
+ *
+ * @author <a href="epugh@opensourceconnections.com">Eric Pugh</a>
+ * @version $Id: ParameterParserTest.java 1837188 2018-07-31 22:23:27Z tv $
+ */
+public class ParserServiceTest extends BaseUnit4Test
+{
+
+ private ParserService parserService;
+ private ParameterParser parameterParser = null;
+ private Part test;
+
+ @Before
+ public void setUpBefore() throws Exception
+ {
+ try
+ {
+ parserService = (ParserService)this.lookup(ParserService.ROLE);
+ parameterParser = parserService.getParser(DefaultParameterParser.class);
+
+ test = getPart("upload-field");
+ }
+ catch (ComponentException e)
+ {
+ e.printStackTrace();
+ fail(e.getMessage());
+ }
+ }
+
+ private Part getPart( String name )
+ {
+ return new Part()
+ {
+
+ @Override
+ public void write(String fileName) throws IOException
+ {
+ }
+
+ @Override
+ public String getSubmittedFileName()
+ {
+ return null;
+ }
+
+ @Override
+ public long getSize()
+ {
+ return 0;
+ }
+
+ @Override
+ public String getName()
+ {
+ return name;
+ }
+
+ @Override
+ public InputStream getInputStream() throws IOException
+ {
+ return null;
+ }
+
+ @Override
+ public Collection<String> getHeaders(String name)
+ {
+ return null;
+ }
+
+ @Override
+ public Collection<String> getHeaderNames()
+ {
+ return null;
+ }
+
+ @Override
+ public String getHeader(String name)
+ {
+ return null;
+ }
+
+ @Override
+ public String getContentType()
+ {
+ return "application/octet-stream";
+ }
+
+ @Override
+ public void delete() throws IOException
+ {
+ }
+ };
+ }
+
+ @Test
+ public void testConfiguredAutomaticUpload() throws Exception {
+ assertTrue(parserService.getAutomaticUpload());
+ }
+ @Test
+ public void testConfiguredParameterEncoding() throws Exception {
+ assertEquals("utf-8", parserService.getParameterEncoding());
+ }
+
+ @Test
+ public void testUploadParts() throws Exception {
+ HttpServletRequest request = getMockRequest();
+ // TODO check
+ when(request.getContentType()).thenReturn("multipart/form-data; boundary=boundary");
+ when(request.getMethod()).thenReturn("post");
+ ((DefaultParameterParser) parameterParser).add(test.getName(), test);
+ Part secondPart = getPart("second-field");
+ ((DefaultParameterParser) parameterParser).add(secondPart.getName(), secondPart);
+
+ List<Part> parts = parserService.parseUpload( request );
+ assertTrue( !parts.isEmpty() );
+ assertTrue( parts.size() == 2 );
+ }
+
+ @Test
+ public void testNoUploadParts() throws Exception {
+ HttpServletRequest request = getMockRequest();
+ parameterParser.add("other-field", "foo");
+
+ List<Part> parts = parserService.parseUpload( request );
+ assertTrue( parts.isEmpty() );
+ //assertTrue( parts.size() == 2 );
+ }
+
+ // from Turbine org.apache.turbine.test.BaseTestCase, should be later in Fulcrum Testcontainer BaseUnit4Test
+ protected HttpServletRequest getMockRequest() throws Exception
+ {
+ HttpServletRequest request = mock(HttpServletRequest.class);
+ when(request.getHeader("Content-type")).thenReturn("html/text");
+ when(request.getHeader("Accept-Language")).thenReturn("en-US");
+
+ Vector<String> v = new Vector<String>();
+ when(request.getParameterNames()).thenReturn(v.elements());
+
+ when(request.getPart(anyString())).thenAnswer( new Answer<Object>()
+ {
+ @Override
+ public Object answer(InvocationOnMock invocation) throws Throwable
+ {
+ String name = (String) invocation.getArguments()[0];
+ return parameterParser.getPart(name);
+ }
+ } );
+ when(request.getParts()).thenAnswer( new Answer<Object>()
+ {
+ @Override
+ public Object answer(InvocationOnMock invocation) throws Throwable
+ {
+ return ((DefaultParameterParser)parameterParser).getParts();
+ }
+ } );
+ return request;
+ }
+
+}