juneau-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jamesbog...@apache.org
Subject [juneau] branch master updated: Tests.
Date Sun, 13 May 2018 01:50:30 GMT
This is an automated email from the ASF dual-hosted git repository.

jamesbognar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/juneau.git


The following commit(s) were added to refs/heads/master by this push:
     new f9065c0  Tests.
f9065c0 is described below

commit f9065c013555630df15627904562f2682f54b99c
Author: JamesBognar <jamesbognar@apache.org>
AuthorDate: Sat May 12 21:50:17 2018 -0400

    Tests.
---
 .../juneau/rest/test/CharsetEncodingsResource.java |  76 --------------
 .../apache/juneau/rest/test/FormDataResource.java  |  36 -------
 .../java/org/apache/juneau/rest/test/Root.java     |   2 -
 .../juneau/rest/test/CharsetEncodingsTest.java     | 110 ---------------------
 .../apache/juneau/rest/test/ClientVersionTest.java |  77 ---------------
 .../org/apache/juneau/rest/test/FormDataTest.java  |  34 -------
 .../org/apache/juneau/rest/test/_TestSuite.java    |   2 -
 .../juneau/rest/mock/MockServletRequest.java       |  42 +++++++-
 .../org/apache/juneau/rest/ClientVersionTest.java  |  80 +++++++++------
 .../rest/annotation/FormDataAnnotationTest.java    |  62 ++++++++++++
 10 files changed, 153 insertions(+), 368 deletions(-)

diff --git a/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/CharsetEncodingsResource.java
b/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/CharsetEncodingsResource.java
deleted file mode 100644
index d386ee7..0000000
--- a/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/CharsetEncodingsResource.java
+++ /dev/null
@@ -1,76 +0,0 @@
-// ***************************************************************************************************************************
-// * 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.         
                                    *
-// ***************************************************************************************************************************
-package org.apache.juneau.rest.test;
-
-import static org.apache.juneau.internal.IOUtils.*;
-import static org.apache.juneau.http.HttpMethodName.*;
-
-import org.apache.juneau.*;
-import org.apache.juneau.parser.*;
-import org.apache.juneau.rest.*;
-import org.apache.juneau.rest.annotation.*;
-import org.apache.juneau.serializer.*;
-
-/**
- * JUnit automated testcase resource.
- */
-@RestResource(
-	path="/testCharsetEncodings",
-	defaultRequestHeaders={"Accept: text/s", "Content-Type: text/p"},
-	parsers={CharsetEncodingsResource.CtParser.class}, serializers={CharsetEncodingsResource.ASerializer.class}
-)
-public class CharsetEncodingsResource extends RestServlet {
-	private static final long serialVersionUID = 1L;
-
-	public static class CtParser extends ReaderParser {
-
-		public CtParser(PropertyStore ps) {
-			super(ps, "text/p");
-		}
-
-		@Override /* Parser */
-		public ReaderParserSession createSession(ParserSessionArgs args) {
-			return new ReaderParserSession(args) {
-
-				@Override /* ParserSession */
-				@SuppressWarnings("unchecked")
-				protected <T> T doParse(ParserPipe pipe, ClassMeta<T> type) throws Exception
{
-					return (T)read(pipe.getReader());
-				}
-			};
-		}
-	}
-
-	public static class ASerializer extends WriterSerializer {
-
-		public ASerializer(PropertyStore ps) {
-			super(ps, "text/s", null);
-		}
-
-		@Override /* Serializer */
-		public WriterSerializerSession createSession(SerializerSessionArgs args) {
-			return new WriterSerializerSession(args) {
-
-				@Override /* SerializerSession */
-				protected void doSerialize(SerializerPipe out, Object o) throws Exception {
-					out.getWriter().write(o.toString());
-				}
-			};
-		}
-	}
-
-	@RestMethod(name=PUT, path="/")
-	public String test1(RestRequest req, @Body String in) {
-		return req.getCharacterEncoding() + "/" + in + "/" + req.getCharacterEncoding();
-	}
-}
diff --git a/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/FormDataResource.java
b/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/FormDataResource.java
index 0f61d78..1183d5b 100644
--- a/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/FormDataResource.java
+++ b/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/FormDataResource.java
@@ -17,7 +17,6 @@ import static org.apache.juneau.http.HttpMethodName.*;
 
 import java.io.*;
 
-import org.apache.juneau.*;
 import org.apache.juneau.rest.*;
 import org.apache.juneau.rest.annotation.*;
 
@@ -38,39 +37,4 @@ public class FormDataResource extends BasicRestServlet {
 		return new StringReader("Content-Type=["+req.getContentType()+"], contents=["+read(req.getReader())+"]");
 	}
 
-	//====================================================================================================
-	// Default values.
-	//====================================================================================================
-
-	@RestMethod(name=POST, path="/defaultFormData", defaultFormData={"f1:1","f2=2"," f3 : 3
"})
-	public ObjectMap defaultFormData(RequestFormData formData) {
-		return new ObjectMap()
-			.append("f1", formData.getString("f1"))
-			.append("f2", formData.getString("f2"))
-			.append("f3", formData.getString("f3"));
-	}
-
-	@RestMethod(name=POST, path="/annotatedFormData")
-	public ObjectMap annotatedFormData(@FormData("f1") String f1, @FormData("f2") String f2,
@FormData("f3") String f3) {
-		return new ObjectMap()
-			.append("f1", f1)
-			.append("f2", f2)
-			.append("f3", f3);
-	}
-
-	@RestMethod(name=POST, path="/annotatedFormDataDefault")
-	public ObjectMap annotatedFormDataDefault(@FormData(value="f1",_default="1") String f1,
@FormData(value="f2",_default="2") String f2, @FormData(value="f3",_default="3") String f3)
{
-		return new ObjectMap()
-			.append("f1", f1)
-			.append("f2", f2)
-			.append("f3", f3);
-	}
-
-	@RestMethod(name=POST, path="/annotatedAndDefaultFormData", defaultFormData={"f1:1","f2=2","
f3 : 3 "})
-	public ObjectMap annotatedAndDefaultFormData(@FormData(value="f1",_default="4") String f1,
@FormData(value="f2",_default="5") String f2, @FormData(value="f3",_default="6") String f3)
{
-		return new ObjectMap()
-			.append("f1", f1)
-			.append("f2", f2)
-			.append("f3", f3);
-	}
 }
diff --git a/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/Root.java
b/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/Root.java
index 4e8d6e4..b45068f 100644
--- a/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/Root.java
+++ b/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/Root.java
@@ -26,9 +26,7 @@ import org.apache.juneau.rest.helper.*;
 		LogsResource.class,
 		BpiResource.class,
 		CallbackStringsResource.class,
-		CharsetEncodingsResource.class,
 		ClientFuturesResource.class,
-		ClientVersionResource.class,
 		ConfigResource.class,
 		TransformsResource.class,
 		FormDataResource.class,
diff --git a/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/CharsetEncodingsTest.java
b/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/CharsetEncodingsTest.java
deleted file mode 100644
index b103aea..0000000
--- a/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/CharsetEncodingsTest.java
+++ /dev/null
@@ -1,110 +0,0 @@
-// ***************************************************************************************************************************
-// * 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.         
                                    *
-// ***************************************************************************************************************************
-package org.apache.juneau.rest.test;
-
-import static javax.servlet.http.HttpServletResponse.*;
-import static org.apache.juneau.internal.IOUtils.*;
-import static org.apache.juneau.microservice.testutils.TestUtils.*;
-import static org.junit.Assert.*;
-
-import java.io.*;
-
-import org.apache.juneau.rest.client.*;
-import org.junit.*;
-
-
-public class CharsetEncodingsTest extends RestTestcase {
-
-	private static boolean debug = false;
-
-	/**
-	 * Basic tests to ensure that the correct charsets are found and used
-	 * under a variety of scenarios.
-	 */
-	@Test
-	public void test() throws Exception {
-		String url = "/testCharsetEncodings";
-		RestClientBuilder cb = TestMicroservice.client().accept("text/s").contentType("text/p");
-		RestClient client = cb.build();
-		InputStream is;
-		String r;
-
-		r = client.doPut(url, new StringReader("foo")).getResponseAsString();
-		if (debug) System.err.println(r);
-		assertEquals("utf-8/foo/utf-8", r);
-
-		is = client.doPut(url, new StringReader("foo")).getInputStream();
-		r = read(new InputStreamReader(is, "utf-8"));
-		if (debug) System.err.println(r);
-		assertEquals("utf-8/foo/utf-8", r);
-
-		client.closeQuietly();
-
-		client = cb.acceptCharset("utf-8").contentType("text/p;charset=utf-8").build();
-		is = client.doPut(url, new StringReader("foo")).acceptCharset("utf-8").contentType("text/p;charset=utf-8").getInputStream();
-		r = read(new InputStreamReader(is, "utf-8"));
-		if (debug) System.err.println(r);
-		assertEquals("utf-8/foo/utf-8", r);
-
-		client.closeQuietly();
-
-		client = cb.acceptCharset("Shift_JIS").contentType("text/p;charset=shift_jis").build();
-		is = client.doPut(url, new StringReader("foo")).getInputStream();
-		r = read(new InputStreamReader(is, "Shift_JIS"));
-		if (debug) System.err.println(r);
-		assertEquals("shift_jis/foo/shift_jis", r);
-
-		client.closeQuietly();
-
-		try {
-			client = cb.acceptCharset("BAD").contentType("text/p;charset=sjis").build();
-			is = client.doPut(url + "?noTrace=true", new StringReader("foo")).getInputStream();
-			r = read(new InputStreamReader(is, "sjis"));
-			if (debug) System.err.println(r);
-			fail("Exception expected");
-		} catch (RestCallException e) {
-			checkErrorResponse(debug, e, SC_NOT_ACCEPTABLE, "No supported charsets in header 'Accept-Charset':
'BAD'");
-		}
-
-		client.closeQuietly();
-
-		client = cb.accept("text/s").acceptCharset("utf-8").contentType("text/p").build();
-		is = client.doPut(url+"?Content-Type=text/p", new StringReader("foo")).getInputStream();
-		r = read(new InputStreamReader(is, "utf-8"));
-		if (debug) System.err.println(r);
-		assertEquals("utf-8/foo/utf-8", r);
-
-		client.closeQuietly();
-
-		client = cb.accept("text/s").contentType("text/bad").acceptCharset("utf-8").build();
-		is = client.doPut(url+"?Content-Type=text/p;charset=utf-8", new StringReader("foo")).getInputStream();
-		r = read(new InputStreamReader(is, "utf-8"));
-		if (debug) System.err.println(r);
-		assertEquals("utf-8/foo/utf-8", r);
-
-		client.closeQuietly();
-
-		try {
-			client = cb.accept("text/s").contentType("text/p").acceptCharset("utf-8").build();
-			is = client.doPut(url+"?Content-Type=text/p;charset=BAD&noTrace=true", new StringReader("foo")).getInputStream();
-			r = read(new InputStreamReader(is, "utf-8"));
-			if (debug) System.err.println(r);
-			assertEquals("utf-8/foo/utf-8", r);
-			fail("Exception expected");
-		} catch (RestCallException e) {
-			checkErrorResponse(debug, e, SC_UNSUPPORTED_MEDIA_TYPE, "Unsupported charset in header
'Content-Type': 'text/p;charset=BAD'");
-		}
-
-		client.closeQuietly();
-	}
-}
diff --git a/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/ClientVersionTest.java
b/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/ClientVersionTest.java
deleted file mode 100644
index 3eda27b..0000000
--- a/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/ClientVersionTest.java
+++ /dev/null
@@ -1,77 +0,0 @@
-// ***************************************************************************************************************************
-// * 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.         
                                    *
-// ***************************************************************************************************************************
-package org.apache.juneau.rest.test;
-
-import static org.junit.Assert.*;
-
-import org.apache.juneau.rest.client.*;
-import org.junit.*;
-
-public class ClientVersionTest extends RestTestcase {
-
-	private static String URL = "/testClientVersion";
-
-	//====================================================================================================
-	// Basic tests - default X-Client-Version header.
-	//====================================================================================================
-	@Test
-	public void testDefaultHeader() throws Exception {
-		RestClient client = TestMicroservice.DEFAULT_CLIENT_PLAINTEXT;
-		String url = URL + "/defaultHeader";
-
-		assertEquals("no-version", client.doGet(url).getResponseAsString());
-
-//		for (String s : "0, 0.0, 0.1, .1, .9, .99".split("\\s*,\\s*")) {
-//			assertEquals(s, "[0.0,1.0)", client.doGet(url).clientVersion(s).getResponseAsString());
-//		}
-
-		for (String s : "1, 1.0, 1.0.0, 1.0.1".split("\\s*,\\s*")) {
-			assertEquals(s, "[1.0,1.0]", client.doGet(url).clientVersion(s).getResponseAsString());
-		}
-
-		for (String s : "1.1, 1.1.1, 1.2, 1.9.9".split("\\s*,\\s*")) {
-			assertEquals(s, "[1.1,2)", client.doGet(url).clientVersion(s).getResponseAsString());
-		}
-
-		for (String s : "2, 2.0, 2.1, 9, 9.9".split("\\s*,\\s*")) {
-			assertEquals(s, "2", client.doGet(url).clientVersion(s).getResponseAsString());
-		}
-	}
-
-	//====================================================================================================
-	// Basic tests - Custom-Client-Version header.
-	//====================================================================================================
-	@Test
-	public void testCustomHeader() throws Exception {
-		RestClient client = TestMicroservice.DEFAULT_CLIENT_PLAINTEXT;
-		String url = URL + "/customHeader";
-
-		assertEquals("no-version", client.doGet(url).getResponseAsString());
-
-		for (String s : "0, 0.0, 0.1, .1, .9, .99".split("\\s*,\\s*")) {
-			assertEquals("[0.0,1.0)", client.doGet(url).header("Custom-Client-Version", s).getResponseAsString());
-		}
-
-		for (String s : "1, 1.0, 1.0.0, 1.0.1".split("\\s*,\\s*")) {
-			assertEquals("[1.0,1.0]", client.doGet(url).header("Custom-Client-Version", s).getResponseAsString());
-		}
-
-		for (String s : "1.1, 1.1.1, 1.2, 1.9.9".split("\\s*,\\s*")) {
-			assertEquals("[1.1,2)", client.doGet(url).header("Custom-Client-Version", s).getResponseAsString());
-		}
-
-		for (String s : "2, 2.0, 2.1, 9, 9.9".split("\\s*,\\s*")) {
-			assertEquals("2", client.doGet(url).header("Custom-Client-Version", s).getResponseAsString());
-		}
-	}
-}
diff --git a/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/FormDataTest.java
b/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/FormDataTest.java
index 54bc8f7..b78a4fd 100644
--- a/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/FormDataTest.java
+++ b/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/FormDataTest.java
@@ -12,12 +12,10 @@
 // ***************************************************************************************************************************
 package org.apache.juneau.rest.test;
 
-import static org.apache.juneau.rest.testutils.TestUtils.*;
 import static org.junit.Assert.*;
 
 import java.util.*;
 
-import org.apache.juneau.*;
 import org.apache.juneau.rest.client.*;
 import org.apache.juneau.urlencoding.*;
 import org.apache.juneau.utils.*;
@@ -104,36 +102,4 @@ public class FormDataTest extends RestTestcase {
 			c.close();
 		}
 	}
-
-	//====================================================================================================
-	// Default values.
-	//====================================================================================================
-
-	@Test
-	public void defaultFormData() throws Exception {
-		assertObjectEquals("{f1:'1',f2:'2',f3:'3'}", client.doPost(URL + "/defaultFormData").getResponse(ObjectMap.class));
-		assertObjectEquals("{f1:'4',f2:'5',f3:'6'}", client.doPost(URL + "/defaultFormData").formData("f1",4).formData("f2",5).formData("f3",6).getResponse(ObjectMap.class));
-		assertObjectEquals("{f1:'4',f2:'5',f3:'6'}", client.doPost(URL + "/defaultFormData").formData("f1",4).formData("f2",5).formData("f3",6).getResponse(ObjectMap.class));
-	}
-
-	@Test
-	public void annotatedFormData() throws Exception {
-		assertObjectEquals("{f1:null,f2:null,f3:null}", client.doPost(URL + "/annotatedFormData").getResponse(ObjectMap.class));
-		assertObjectEquals("{f1:'4',f2:'5',f3:'6'}", client.doPost(URL + "/annotatedFormData").formData("f1",4).formData("f2",5).formData("f3",6).getResponse(ObjectMap.class));
-		assertObjectEquals("{f1:'4',f2:'5',f3:'6'}", client.doPost(URL + "/annotatedFormData").formData("f1",4).formData("f2",5).formData("f3",6).getResponse(ObjectMap.class));
-	}
-
-	@Test
-	public void annotatedFormDataDefault() throws Exception {
-		assertObjectEquals("{f1:'1',f2:'2',f3:'3'}", client.doPost(URL + "/annotatedFormDataDefault").getResponse(ObjectMap.class));
-		assertObjectEquals("{f1:'4',f2:'5',f3:'6'}", client.doPost(URL + "/annotatedFormDataDefault").formData("f1",4).formData("f2",5).formData("f3",6).getResponse(ObjectMap.class));
-		assertObjectEquals("{f1:'4',f2:'5',f3:'6'}", client.doPost(URL + "/annotatedFormDataDefault").formData("f1",4).formData("f2",5).formData("f3",6).getResponse(ObjectMap.class));
-	}
-
-	@Test
-	public void annotatedAndDefaultFormData() throws Exception {
-		assertObjectEquals("{f1:'4',f2:'5',f3:'6'}", client.doPost(URL + "/annotatedAndDefaultFormData").getResponse(ObjectMap.class));
-		assertObjectEquals("{f1:'7',f2:'8',f3:'9'}", client.doPost(URL + "/annotatedAndDefaultFormData").formData("f1",7).formData("f2",8).formData("f3",9).getResponse(ObjectMap.class));
-		assertObjectEquals("{f1:'7',f2:'8',f3:'9'}", client.doPost(URL + "/annotatedAndDefaultFormData").formData("f1",7).formData("f2",8).formData("f3",9).getResponse(ObjectMap.class));
-	}
 }
diff --git a/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/_TestSuite.java
b/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/_TestSuite.java
index 04d902a..a952343 100644
--- a/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/_TestSuite.java
+++ b/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/_TestSuite.java
@@ -25,9 +25,7 @@ import org.junit.runners.Suite.*;
 @RunWith(Suite.class)
 @SuiteClasses({
 	CallbackStringsTest.class,
-	CharsetEncodingsTest.class,
 	ClientFuturesTest.class,
-	ClientVersionTest.class,
 	ConfigTest.class,
 	FormDataTest.class,
 	GroupsTest.class,
diff --git a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/mock/MockServletRequest.java
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/mock/MockServletRequest.java
index 3f4de59..d18e88d 100644
--- a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/mock/MockServletRequest.java
+++ b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/mock/MockServletRequest.java
@@ -24,6 +24,8 @@ import javax.servlet.http.*;
 import org.apache.juneau.internal.*;
 import org.apache.juneau.rest.*;
 import org.apache.juneau.rest.util.*;
+import org.apache.juneau.urlencoding.*;
+import org.apache.juneau.utils.*;
 
 /**
  * An implementation of {@link HttpServletRequest} for mocking purposes.
@@ -37,6 +39,7 @@ public class MockServletRequest implements HttpServletRequest {
 	
 	private String method = "GET";
 	private Map<String,String[]> parameterMap;
+	private Map<String,String[]> formDataMap;
 	private Map<String,String[]> headerMap = new LinkedHashMap<>();	
 	private Map<String,Object> attributeMap = new LinkedHashMap<>();
 	private String characterEncoding = "UTF-8";
@@ -645,6 +648,8 @@ public class MockServletRequest implements HttpServletRequest {
 
 	@Override /* HttpServletRequest */
 	public ServletInputStream getInputStream() throws IOException {
+		if (formDataMap != null)
+			body = UrlEncodingSerializer.DEFAULT.toString(formDataMap).getBytes();
 		return new BoundedServletInputStream(new ByteArrayInputStream(body), Integer.MAX_VALUE);
 	}
 
@@ -669,7 +674,10 @@ public class MockServletRequest implements HttpServletRequest {
 		if (parameterMap == null) {
 			try {
 				if ("POST".equalsIgnoreCase(method)) {
-					parameterMap = RestUtils.parseQuery(IOUtils.read(body));
+					if (formDataMap != null)
+						parameterMap = formDataMap;
+					else
+						parameterMap = RestUtils.parseQuery(IOUtils.read(body));
 				} else {
 					parameterMap = RestUtils.parseQuery(getQueryString());
 				}
@@ -702,7 +710,7 @@ public class MockServletRequest implements HttpServletRequest {
 
 	@Override /* HttpServletRequest */
 	public BufferedReader getReader() throws IOException {
-		return new BufferedReader(new InputStreamReader(new ByteArrayInputStream(body), characterEncoding));
+		return new BufferedReader(new InputStreamReader(getInputStream(), characterEncoding));
 	}
 
 	@Override /* HttpServletRequest */
@@ -1027,4 +1035,34 @@ public class MockServletRequest implements HttpServletRequest {
 	public MockServletRequest acceptCharset(String value) {
 		return header("Accept-Charset", value);
 	}
+
+	/**
+	 * Specifies the <code>X-Client-Version</code> header value on the request.
+	 * 
+	 * @param value The new value.
+	 * @return This object (for method chaining).
+	 */
+	public MockServletRequest clientVersion(String value) {
+		return header("X-Client-Version", value);
+	}
+
+	/**
+	 * Adds a form data entry to this request.
+	 * 
+	 * @param key 
+	 * @param value 
+	 * @return This object (for method chaining).
+	 */
+	public MockServletRequest formData(String key, Object value) {
+		if (formDataMap == null)
+			formDataMap = new LinkedHashMap<>();
+		String s = asString(value);
+		String[] existing = formDataMap.get(key);
+		if (existing == null)
+			existing = new String[]{s};
+		else
+			existing = new AList<>().appendAll(Arrays.asList(existing)).append(s).toArray(new
String[0]);
+		formDataMap.put(key, existing);
+		return this;
+	}
 }
diff --git a/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/ClientVersionResource.java
b/juneau-rest/juneau-rest-server/src/test/java/org/apache/juneau/rest/ClientVersionTest.java
similarity index 52%
rename from juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/ClientVersionResource.java
rename to juneau-rest/juneau-rest-server/src/test/java/org/apache/juneau/rest/ClientVersionTest.java
index 5a9e6e6..daf961d 100644
--- a/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/ClientVersionResource.java
+++ b/juneau-rest/juneau-rest-server/src/test/java/org/apache/juneau/rest/ClientVersionTest.java
@@ -10,86 +10,108 @@
 // * "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.         
                                    *
 // ***************************************************************************************************************************
-package org.apache.juneau.rest.test;
+package org.apache.juneau.rest;
 
 import static org.apache.juneau.http.HttpMethodName.*;
 
-import org.apache.juneau.rest.*;
 import org.apache.juneau.rest.annotation.*;
+import org.apache.juneau.rest.mock.*;
+import org.junit.*;
+import org.junit.runners.*;
 
 /**
- * JUnit automated testcase resource.
+ * Tests the @RestResource(clientVersion) attribute.
  */
-@RestResource(
-	path="/testClientVersion",
-	children={
-		ClientVersionResource.DefaultHeader.class,
-		ClientVersionResource.CustomHeader.class
-	}
-)
-@SuppressWarnings("serial")
-public class ClientVersionResource extends BasicRestServlet {
+@SuppressWarnings({"javadoc"})
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
+public class ClientVersionTest {
 
-	@RestResource(
-		path="/defaultHeader"
-	)
-	public static class DefaultHeader extends BasicRestServlet {
+	//====================================================================================================
+	// Basic tests - default X-Client-Version header.
+	//====================================================================================================
 
+	@RestResource
+	public static class A {
 		@RestMethod(name=GET, path="/")
 		public String test0() {
 			return "no-version";
 		}
-
 		@RestMethod(name=GET, path="/", clientVersion="[0.0,1.0)")
 		public String test1() {
 			return "[0.0,1.0)";
 		}
-
 		@RestMethod(name=GET, path="/", clientVersion="[1.0,1.0]")
 		public String test2() {
 			return "[1.0,1.0]";
 		}
-
 		@RestMethod(name=GET, path="/", clientVersion="[1.1,2)")
 		public String test3() {
 			return "[1.1,2)";
 		}
-
 		@RestMethod(name=GET, path="/", clientVersion="2")
 		public String test4() {
 			return "2";
 		}
 	}
+	static MockRest a = MockRest.create(A.class);
+	
+	@Test
+	public void a01_defaultHeader() throws Exception {
+		a.request("GET", "/").execute().assertBody("no-version");
+		for (String s : "1, 1.0, 1.0.0, 1.0.1".split("\\s*,\\s*")) {
+			a.request("GET", "/").clientVersion(s).execute().assertBody("[1.0,1.0]");
+		}
+		for (String s : "1.1, 1.1.1, 1.2, 1.9.9".split("\\s*,\\s*")) {
+			a.request("GET", "/").clientVersion(s).execute().assertBody("[1.1,2)");
+		}
+		for (String s : "2, 2.0, 2.1, 9, 9.9".split("\\s*,\\s*")) {
+			a.request("GET", "/").clientVersion(s).execute().assertBody("2");
+		}
+	}
 
-	@RestResource(
-		path="/customHeader",
-		clientVersionHeader="Custom-Client-Version"
-	)
-	public static class CustomHeader extends BasicRestServlet {
+	//====================================================================================================
+	// Basic tests - Custom-Client-Version header.
+	//====================================================================================================
 
+	@RestResource(clientVersionHeader="Custom-Client-Version")
+	public static class B {
 		@RestMethod(name=GET, path="/")
 		public String test0() {
 			return "no-version";
 		}
-
 		@RestMethod(name=GET, path="/", clientVersion="[0.0,1.0)")
 		public String test1() {
 			return "[0.0,1.0)";
 		}
-
 		@RestMethod(name=GET, path="/", clientVersion="[1.0,1.0]")
 		public String test2() {
 			return "[1.0,1.0]";
 		}
-
 		@RestMethod(name=GET, path="/", clientVersion="[1.1,2)")
 		public String test3() {
 			return "[1.1,2)";
 		}
-
 		@RestMethod(name=GET, path="/", clientVersion="2")
 		public String test4() {
 			return "2";
 		}
 	}
+	static MockRest b = MockRest.create(B.class);
+	
+	@Test
+	public void b01_testCustomHeader() throws Exception {
+		b.request("GET", "/").execute().assertBody("no-version");
+		for (String s : "0, 0.0, 0.1, .1, .9, .99".split("\\s*,\\s*")) {
+			b.request("GET", "/").header("Custom-Client-Version", s).execute().assertBody("[0.0,1.0)");
+		}
+		for (String s : "1, 1.0, 1.0.0, 1.0.1".split("\\s*,\\s*")) {
+			b.request("GET", "/").header("Custom-Client-Version", s).execute().assertBody("[1.0,1.0]");
+		}
+		for (String s : "1.1, 1.1.1, 1.2, 1.9.9".split("\\s*,\\s*")) {
+			b.request("GET", "/").header("Custom-Client-Version", s).execute().assertBody("[1.1,2)");
+		}
+		for (String s : "2, 2.0, 2.1, 9, 9.9".split("\\s*,\\s*")) {
+			b.request("GET", "/").header("Custom-Client-Version", s).execute().assertBody("2");
+		}
+	}
 }
diff --git a/juneau-rest/juneau-rest-server/src/test/java/org/apache/juneau/rest/annotation/FormDataAnnotationTest.java
b/juneau-rest/juneau-rest-server/src/test/java/org/apache/juneau/rest/annotation/FormDataAnnotationTest.java
index 270108a..ed83464 100644
--- a/juneau-rest/juneau-rest-server/src/test/java/org/apache/juneau/rest/annotation/FormDataAnnotationTest.java
+++ b/juneau-rest/juneau-rest-server/src/test/java/org/apache/juneau/rest/annotation/FormDataAnnotationTest.java
@@ -14,6 +14,7 @@ package org.apache.juneau.rest.annotation;
 
 import static org.apache.juneau.http.HttpMethodName.*;
 
+import org.apache.juneau.*;
 import org.apache.juneau.httppart.*;
 import org.apache.juneau.rest.*;
 import org.apache.juneau.rest.mock.*;
@@ -75,4 +76,65 @@ public class FormDataAnnotationTest {
 		b.request("POST", "").body("p1=p1").contentType("application/x-www-form-urlencoded").execute().assertBody("p1=[p1,p1,p1]");
 		b.request("POST", "").body("p1='p1'").contentType("application/x-www-form-urlencoded").execute().assertBody("p1=['p1','p1',p1]");
 	}
+	
+	//====================================================================================================
+	// Default values.
+	//====================================================================================================
+
+	@RestResource
+	public static class C {
+		@RestMethod(name=POST, path="/defaultFormData", defaultFormData={"f1:1","f2=2"," f3 : 3
"})
+		public ObjectMap c01(RequestFormData formData) {
+			return new ObjectMap()
+				.append("f1", formData.getString("f1"))
+				.append("f2", formData.getString("f2"))
+				.append("f3", formData.getString("f3"));
+		}
+		@RestMethod(name=POST, path="/annotatedFormData")
+		public ObjectMap c02(@FormData("f1") String f1, @FormData("f2") String f2, @FormData("f3")
String f3) {
+			return new ObjectMap()
+				.append("f1", f1)
+				.append("f2", f2)
+				.append("f3", f3);
+		}
+		@RestMethod(name=POST, path="/annotatedFormDataDefault")
+		public ObjectMap c03(@FormData(value="f1",_default="1") String f1, @FormData(value="f2",_default="2")
String f2, @FormData(value="f3",_default="3") String f3) {
+			return new ObjectMap()
+				.append("f1", f1)
+				.append("f2", f2)
+				.append("f3", f3);
+		}
+		@RestMethod(name=POST, path="/annotatedAndDefaultFormData", defaultFormData={"f1:1","f2=2","
f3 : 3 "})
+		public ObjectMap c04(@FormData(value="f1",_default="4") String f1, @FormData(value="f2",_default="5")
String f2, @FormData(value="f3",_default="6") String f3) {
+			return new ObjectMap()
+				.append("f1", f1)
+				.append("f2", f2)
+				.append("f3", f3);
+		}
+	}
+	static MockRest c = MockRest.create(C.class);
+
+	@Test
+	public void c01_defaultFormData() throws Exception {
+		c.request("POST", "/defaultFormData").contentType("application/x-www-form-urlencoded").execute().assertBody("{f1:'1',f2:'2',f3:'3'}");
+		c.request("POST", "/defaultFormData").contentType("application/x-www-form-urlencoded").formData("f1",4).formData("f2",5).formData("f3",6).execute().assertBody("{f1:'4',f2:'5',f3:'6'}");
+	}
+
+	@Test
+	public void c02_annotatedFormData() throws Exception {
+		c.request("POST", "/annotatedFormData").contentType("application/x-www-form-urlencoded").execute().assertBody("{f1:null,f2:null,f3:null}");
+		c.request("POST", "/annotatedFormData").contentType("application/x-www-form-urlencoded").formData("f1",4).formData("f2",5).formData("f3",6).execute().assertBody("{f1:'4',f2:'5',f3:'6'}");
+	}
+
+	@Test
+	public void c03_annotatedFormDataDefault() throws Exception {
+		c.request("POST", "/annotatedFormDataDefault").contentType("application/x-www-form-urlencoded").execute().assertBody("{f1:'1',f2:'2',f3:'3'}");
+		c.request("POST", "/annotatedFormDataDefault").contentType("application/x-www-form-urlencoded").formData("f1",4).formData("f2",5).formData("f3",6).execute().assertBody("{f1:'4',f2:'5',f3:'6'}");
+	}
+
+	@Test
+	public void c04_annotatedAndDefaultFormData() throws Exception {
+		c.request("POST", "/annotatedAndDefaultFormData").contentType("application/x-www-form-urlencoded").execute().assertBody("{f1:'4',f2:'5',f3:'6'}");
+		c.request("POST", "/annotatedAndDefaultFormData").contentType("application/x-www-form-urlencoded").formData("f1",7).formData("f2",8).formData("f3",9).execute().assertBody("{f1:'7',f2:'8',f3:'9'}");
+	}
 }

-- 
To stop receiving notification emails like this one, please contact
jamesbognar@apache.org.

Mime
View raw message