You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@juneau.apache.org by ja...@apache.org on 2019/03/24 13:27:30 UTC
[juneau] branch master updated: Add deprecated Mock APIs back into
the code for compatability.
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 cb45c7c Add deprecated Mock APIs back into the code for compatability.
cb45c7c is described below
commit cb45c7c6f5d08e416696b3d27d5d4fd593b436f7
Author: JamesBognar <ja...@apache.org>
AuthorDate: Sun Mar 24 09:27:10 2019 -0400
Add deprecated Mock APIs back into the code for compatability.
---
.../apache/juneau/utils/MockHttpConnection.java | 8 +-
.../org/apache/juneau/utils/MockHttpRequest.java | 3 +
.../org/apache/juneau/utils/MockHttpResponse.java | 3 +
.../client/mock}/MockHttpClientConnection.java | 9 +-
.../mock}/MockHttpClientConnectionManager.java | 5 +-
.../juneau/rest/client/mock/package-info.java | 18 +
.../rest/mock2/MockHttpClientConnection.java | 3 +-
.../mock2/MockHttpClientConnectionManager.java | 1 -
.../juneau/rest/mock2}/MockHttpConnection.java | 2 +-
.../apache/juneau/rest/mock2}/MockHttpRequest.java | 4 +-
.../juneau/rest/mock2}/MockHttpResponse.java | 2 +-
.../org/apache/juneau/rest/mock2/MockRest.java | 1 -
.../juneau/rest/mock2/MockServletResponse.java | 1 -
.../{mock => mock2}/MockServletRequestTest.java | 3 +-
.../apache/juneau/rest/mock/MockHttpSession.java | 120 ++
.../java/org/apache/juneau/rest/mock/MockRest.java | 194 +++
.../juneau/rest/mock/MockServletRequest.java | 1369 ++++++++++++++++++++
.../juneau/rest/mock}/MockServletResponse.java | 9 +-
18 files changed, 1733 insertions(+), 22 deletions(-)
diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MockHttpConnection.java b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MockHttpConnection.java
index 15b393c..294e8dd 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MockHttpConnection.java
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MockHttpConnection.java
@@ -12,15 +12,16 @@
// ***************************************************************************************************************************
package org.apache.juneau.utils;
-import java.util.*;
-
/**
* Represent the basic connection for mock HTTP requests.
*
* <p>
* Used as a shim between the server and client APIs that allow the <code>RestClient</code>
* class to send and receive mocked requests using the <code>MockRest</code> interface.
+ *
+ * @deprecated Use <code>org.apache.juneau.rest.mock2</code>
*/
+@Deprecated
public interface MockHttpConnection {
/**
@@ -28,10 +29,9 @@ public interface MockHttpConnection {
*
* @param method The HTTP request method.
* @param path The HTTP request path.
- * @param headers Optional HTTP request headers.
* @param body The HTTP request body.
* @return A new mock request.
* @throws Exception
*/
- MockHttpRequest request(String method, String path, Map<String,Object> headers, Object body) throws Exception;
+ MockHttpRequest request(String method, String path, Object body) throws Exception;
}
diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MockHttpRequest.java b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MockHttpRequest.java
index f2b9962..9a7db00 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MockHttpRequest.java
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MockHttpRequest.java
@@ -18,7 +18,10 @@ package org.apache.juneau.utils;
* <p>
* Used as a shim between the server and client APIs that allow the <code>RestClient</code>
* class to send and receive mocked requests using the <code>MockRest</code> interface.
+ *
+ * @deprecated Use <code>org.apache.juneau.rest.mock2</code>
*/
+@Deprecated
public interface MockHttpRequest {
/**
diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MockHttpResponse.java b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MockHttpResponse.java
index 53d3783..e17d453 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MockHttpResponse.java
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MockHttpResponse.java
@@ -20,7 +20,10 @@ import java.util.*;
* <p>
* Used as a shim between the server and client APIs that allow the <code>RestClient</code>
* class to send and receive mocked requests using the <code>MockRest</code> interface.
+ *
+ * @deprecated Use <code>org.apache.juneau.rest.mock2</code>
*/
+@Deprecated
public interface MockHttpResponse {
/**
diff --git a/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockHttpClientConnection.java b/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/mock/MockHttpClientConnection.java
similarity index 95%
copy from juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockHttpClientConnection.java
copy to juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/mock/MockHttpClientConnection.java
index 1fa840f..33ddfee 100644
--- a/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockHttpClientConnection.java
+++ b/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/mock/MockHttpClientConnection.java
@@ -10,7 +10,7 @@
// * "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.mock2;
+package org.apache.juneau.rest.client.mock;
import java.io.*;
import java.util.*;
@@ -25,7 +25,10 @@ import org.apache.juneau.utils.*;
* An implementation of {@link HttpClientConnection} specifically for use in mocked connections using the {@link MockHttpConnection} class.
*
* This implementation is NOT thread safe.
+ *
+ * @deprecated Use <code>org.apache.juneau.rest.mock2</code>
*/
+@Deprecated
public class MockHttpClientConnection implements HttpClientConnection {
private final MockHttpConnection c;
@@ -79,8 +82,8 @@ public class MockHttpClientConnection implements HttpClientConnection {
public void sendRequestHeader(HttpRequest request) throws HttpException, IOException {
try {
RequestLine rl = request.getRequestLine();
- req = c.request(rl.getMethod(), rl.getUri(), null, null);
- for (Header h : request.getAllHeaders())
+ req = c.request(rl.getMethod(), rl.getUri(), null);
+ for (Header h : request.getAllHeaders())
req.header(h.getName(), h.getValue());
} catch (Exception e) {
throw new HttpException(e.getMessage(), e);
diff --git a/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockHttpClientConnectionManager.java b/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/mock/MockHttpClientConnectionManager.java
similarity index 96%
copy from juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockHttpClientConnectionManager.java
copy to juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/mock/MockHttpClientConnectionManager.java
index 99ce6ab..57e83d7 100644
--- a/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockHttpClientConnectionManager.java
+++ b/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/mock/MockHttpClientConnectionManager.java
@@ -10,7 +10,7 @@
// * "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.mock2;
+package org.apache.juneau.rest.client.mock;
import java.io.*;
import java.util.concurrent.*;
@@ -25,7 +25,10 @@ import org.apache.juneau.utils.*;
* An implementation of {@link HttpClientConnectionManager} specifically for use in mocked connections using the {@link MockHttpConnection} class.
*
* This implementation is NOT thread safe.
+ *
+ * @deprecated Use <code>org.apache.juneau.rest.mock2</code>
*/
+@Deprecated
public class MockHttpClientConnectionManager implements HttpClientConnectionManager {
final ConnectionRequest cr;
diff --git a/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/mock/package-info.java b/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/mock/package-info.java
new file mode 100644
index 0000000..dc23713
--- /dev/null
+++ b/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/mock/package-info.java
@@ -0,0 +1,18 @@
+/***************************************************************************************************************************
+ * 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.
+ *
+ ***************************************************************************************************************************/
+
+/**
+ * REST Client Mock API (deprecated)
+ */
+package org.apache.juneau.rest.client.mock;
\ No newline at end of file
diff --git a/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockHttpClientConnection.java b/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockHttpClientConnection.java
index 1fa840f..77ef590 100644
--- a/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockHttpClientConnection.java
+++ b/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockHttpClientConnection.java
@@ -19,7 +19,6 @@ import org.apache.http.*;
import org.apache.http.entity.*;
import org.apache.http.message.*;
import org.apache.juneau.internal.*;
-import org.apache.juneau.utils.*;
/**
* An implementation of {@link HttpClientConnection} specifically for use in mocked connections using the {@link MockHttpConnection} class.
@@ -80,7 +79,7 @@ public class MockHttpClientConnection implements HttpClientConnection {
try {
RequestLine rl = request.getRequestLine();
req = c.request(rl.getMethod(), rl.getUri(), null, null);
- for (Header h : request.getAllHeaders())
+ for (Header h : request.getAllHeaders())
req.header(h.getName(), h.getValue());
} catch (Exception e) {
throw new HttpException(e.getMessage(), e);
diff --git a/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockHttpClientConnectionManager.java b/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockHttpClientConnectionManager.java
index 99ce6ab..51cf236 100644
--- a/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockHttpClientConnectionManager.java
+++ b/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockHttpClientConnectionManager.java
@@ -19,7 +19,6 @@ import org.apache.http.*;
import org.apache.http.conn.*;
import org.apache.http.conn.routing.*;
import org.apache.http.protocol.*;
-import org.apache.juneau.utils.*;
/**
* An implementation of {@link HttpClientConnectionManager} specifically for use in mocked connections using the {@link MockHttpConnection} class.
diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MockHttpConnection.java b/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockHttpConnection.java
similarity index 98%
copy from juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MockHttpConnection.java
copy to juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockHttpConnection.java
index 15b393c..6ee80ba 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MockHttpConnection.java
+++ b/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockHttpConnection.java
@@ -10,7 +10,7 @@
// * "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.utils;
+package org.apache.juneau.rest.mock2;
import java.util.*;
diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MockHttpRequest.java b/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockHttpRequest.java
similarity index 96%
copy from juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MockHttpRequest.java
copy to juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockHttpRequest.java
index f2b9962..ab0fb05 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MockHttpRequest.java
+++ b/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockHttpRequest.java
@@ -10,10 +10,10 @@
// * "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.utils;
+package org.apache.juneau.rest.mock2;
/**
- * Represent the basic interface for an HTTP rquest.
+ * Represent the basic interface for an HTTP request.
*
* <p>
* Used as a shim between the server and client APIs that allow the <code>RestClient</code>
diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MockHttpResponse.java b/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockHttpResponse.java
similarity index 98%
copy from juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MockHttpResponse.java
copy to juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockHttpResponse.java
index 53d3783..75a8b13 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MockHttpResponse.java
+++ b/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockHttpResponse.java
@@ -10,7 +10,7 @@
// * "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.utils;
+package org.apache.juneau.rest.mock2;
import java.util.*;
diff --git a/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockRest.java b/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockRest.java
index 71dcde7..5cb902c 100644
--- a/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockRest.java
+++ b/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockRest.java
@@ -22,7 +22,6 @@ import org.apache.juneau.rest.*;
import org.apache.juneau.rest.annotation.*;
import org.apache.juneau.rest.util.*;
import org.apache.juneau.serializer.*;
-import org.apache.juneau.utils.*;
/**
* Creates a mocked interface against a REST resource class.
diff --git a/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockServletResponse.java b/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockServletResponse.java
index 2ceda90..8a95b05 100644
--- a/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockServletResponse.java
+++ b/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockServletResponse.java
@@ -24,7 +24,6 @@ import javax.servlet.http.*;
import org.apache.juneau.internal.*;
import org.apache.juneau.rest.util.*;
-import org.apache.juneau.utils.*;
/**
* An implementation of {@link HttpServletResponse} for mocking purposes.
diff --git a/juneau-rest/juneau-rest-server-test/src/test/java/org/apache/juneau/rest/mock/MockServletRequestTest.java b/juneau-rest/juneau-rest-server-test/src/test/java/org/apache/juneau/rest/mock2/MockServletRequestTest.java
similarity index 98%
rename from juneau-rest/juneau-rest-server-test/src/test/java/org/apache/juneau/rest/mock/MockServletRequestTest.java
rename to juneau-rest/juneau-rest-server-test/src/test/java/org/apache/juneau/rest/mock2/MockServletRequestTest.java
index 6a0f310..993016a 100644
--- a/juneau-rest/juneau-rest-server-test/src/test/java/org/apache/juneau/rest/mock/MockServletRequestTest.java
+++ b/juneau-rest/juneau-rest-server-test/src/test/java/org/apache/juneau/rest/mock2/MockServletRequestTest.java
@@ -10,12 +10,11 @@
// * "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.mock;
+package org.apache.juneau.rest.mock2;
import static org.apache.juneau.rest.testutils.TestUtils.*;
import static org.junit.Assert.*;
-import org.apache.juneau.rest.mock2.*;
import org.junit.*;
import org.junit.runners.*;
diff --git a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/mock/MockHttpSession.java b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/mock/MockHttpSession.java
new file mode 100644
index 0000000..8ebe936
--- /dev/null
+++ b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/mock/MockHttpSession.java
@@ -0,0 +1,120 @@
+// ***************************************************************************************************************************
+// * 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.mock;
+
+import java.util.*;
+
+import javax.servlet.*;
+import javax.servlet.http.*;
+
+/**
+ * An implementation of {@link HttpSession} for mocking purposes.
+ *
+ * <h5 class='section'>See Also:</h5>
+ * <ul>
+ * <li class='link'>{@doc juneau-rest-server.UnitTesting}
+ * </ul>
+ *
+ * @deprecated Use <code>org.apache.juneau.rest.mock2</code>
+ */
+@Deprecated
+public class MockHttpSession implements HttpSession {
+
+ /**
+ * Creates a new HTTP session.
+ *
+ * @return A new HTTP session.
+ */
+ public static MockHttpSession create() {
+ return new MockHttpSession();
+ }
+
+ @Override /* HttpSession */
+ public long getCreationTime() {
+ return 0;
+ }
+
+ @Override /* HttpSession */
+ public String getId() {
+ return null;
+ }
+
+ @Override /* HttpSession */
+ public long getLastAccessedTime() {
+ return 0;
+ }
+
+ @Override /* HttpSession */
+ public ServletContext getServletContext() {
+ return null;
+ }
+
+ @Override /* HttpSession */
+ public void setMaxInactiveInterval(int interval) {
+ }
+
+ @Override /* HttpSession */
+ public int getMaxInactiveInterval() {
+ return 0;
+ }
+
+ @Override /* HttpSession */
+ public HttpSessionContext getSessionContext() {
+ return null;
+ }
+
+ @Override /* HttpSession */
+ public Object getAttribute(String name) {
+ return null;
+ }
+
+ @Override /* HttpSession */
+ public Object getValue(String name) {
+ return null;
+ }
+
+ @Override /* HttpSession */
+ public Enumeration<String> getAttributeNames() {
+ return null;
+ }
+
+ @Override /* HttpSession */
+ public String[] getValueNames() {
+ return null;
+ }
+
+ @Override /* HttpSession */
+ public void setAttribute(String name, Object value) {
+ }
+
+ @Override /* HttpSession */
+ public void putValue(String name, Object value) {
+ }
+
+ @Override /* HttpSession */
+ public void removeAttribute(String name) {
+ }
+
+ @Override /* HttpSession */
+ public void removeValue(String name) {
+ }
+
+ @Override /* HttpSession */
+ public void invalidate() {
+ }
+
+ @Override /* HttpSession */
+ public boolean isNew() {
+ return false;
+ }
+}
diff --git a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/mock/MockRest.java b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/mock/MockRest.java
new file mode 100644
index 0000000..733ea88
--- /dev/null
+++ b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/mock/MockRest.java
@@ -0,0 +1,194 @@
+// ***************************************************************************************************************************
+// * 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.mock;
+
+import java.util.*;
+import java.util.concurrent.*;
+
+import org.apache.juneau.rest.*;
+import org.apache.juneau.utils.*;
+
+/**
+ * Creates a mocked interface against a REST resource class.
+ *
+ * <p>
+ * Allows you to test your REST resource classes without a running servlet container.
+ *
+ * <h5 class='figure'>Example:</h5>
+ * <p class='bcode w800'>
+ * <jk>public class</jk> MockTest {
+ *
+ * <jc>// Our REST resource to test.</jc>
+ * <ja>@RestResource</ja>(serializers=JsonSerializer.Simple.<jk>class</jk>, parsers=JsonParser.<jk>class</jk>)
+ * <jk>public static class</jk> MyRest {
+ *
+ * <ja>@RestMethod</ja>(name=<jsf>PUT</jsf>, path=<js>"/String"</js>)
+ * <jk>public</jk> String echo(<ja>@Body</ja> String b) {
+ * <jk>return</jk> b;
+ * }
+ * }
+ *
+ * <ja>@Test</ja>
+ * <jk>public void</jk> testEcho() <jk>throws</jk> Exception {
+ * MockRest
+ * .<jsf>create</jsf>(MyRest.<jk>class</jk>)
+ * .put(<js>"/String"</js>, <js>"'foo'"</js>)
+ * .execute()
+ * .assertStatus(200)
+ * .assertBody(<js>"'foo'"</js>);
+ * }
+ * </p>
+ *
+ * <h5 class='section'>See Also:</h5>
+ * <ul>
+ * <li class='link'>{@doc juneau-rest-server.UnitTesting}
+ * <li class='link'>{@doc juneau-rest-client.UnitTesting}
+ * </ul>
+ *
+ * @deprecated Use <code>org.apache.juneau.rest.mock2</code>
+ */
+@Deprecated
+public class MockRest implements MockHttpConnection {
+ private static Map<Class<?>,RestContext> CONTEXTS = new ConcurrentHashMap<>();
+
+ private final RestContext rc;
+
+ private MockRest(Class<?> c, boolean debug) throws Exception {
+ if (! CONTEXTS.containsKey(c)) {
+ Object r = c.newInstance();
+ RestContext rc = RestContext.create(r).logger(debug ? BasicRestLogger.class : NoOpRestLogger.class).build();
+ if (r instanceof RestServlet) {
+ ((RestServlet)r).setContext(rc);
+ } else {
+ rc.postInit();
+ }
+ rc.postInitChildFirst();
+ CONTEXTS.put(c, rc);
+ }
+ rc = CONTEXTS.get(c);
+ }
+
+ /**
+ * Create a new mock REST interface
+ *
+ * @param c The REST class.
+ * @return A new mock interface.
+ * @throws RuntimeException
+ * For testing conveniences, this method wraps all exceptions in a RuntimeException so that you can easily define mocks as reusable fields.
+ */
+ public static MockRest create(Class<?> c) throws RuntimeException {
+ return create(c, false);
+ }
+
+ /**
+ * Create a new mock REST interface
+ *
+ * @param c The REST class.
+ * @param debug
+ * If <jk>true</jk>, the REST interface will use the {@link BasicRestLogger} for logging.
+ * <br>Otherwise, uses {@link NoOpRestLogger}.
+ * @return A new mock interface.
+ * @throws RuntimeException
+ * For testing conveniences, this method wraps all exceptions in a RuntimeException so that you can easily define mocks as reusable fields.
+ */
+ public static MockRest create(Class<?> c, boolean debug) throws RuntimeException {
+ try {
+ return new MockRest(c, debug);
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ /**
+ * Performs a REST request against the REST interface.
+ *
+ * @param method The HTTP method
+ * @param path The URI path.
+ * @param body The body of the request.
+ * @return A new servlet request.
+ * @throws Exception
+ */
+ @Override /* MockHttpConnection */
+ public MockServletRequest request(String method, String path, Object body) throws Exception {
+ return MockServletRequest.create(method, path).body(body).restContext(rc);
+ }
+
+ /**
+ * Performs a REST request against the REST interface.
+ *
+ * @param method The HTTP method
+ * @param path The URI path.
+ * @return A new servlet request.
+ * @throws Exception
+ */
+ public MockServletRequest request(String method, String path) throws Exception {
+ return request(method, path, null);
+ }
+
+ /**
+ * Perform a GET request.
+ *
+ * @param path The URI path.
+ * @return A new servlet request.
+ * @throws Exception
+ */
+ public MockServletRequest get(String path) throws Exception {
+ return request("GET", path, null);
+ }
+
+ /**
+ * Perform a PUT request.
+ *
+ * @param path The URI path.
+ * @param body The body of the request.
+ * @return A new servlet request.
+ * @throws Exception
+ */
+ public MockServletRequest put(String path, Object body) throws Exception {
+ return request("PUT", path, body);
+ }
+
+ /**
+ * Perform a POST request.
+ *
+ * @param path The URI path.
+ * @param body The body of the request.
+ * @return A new servlet request.
+ * @throws Exception
+ */
+ public MockServletRequest post(String path, Object body) throws Exception {
+ return request("POST", path, body);
+ }
+
+ /**
+ * Perform a DELETE request.
+ *
+ * @param path The URI path.
+ * @return A new servlet request.
+ * @throws Exception
+ */
+ public MockServletRequest delete(String path) throws Exception {
+ return request("DELETE", path, null);
+ }
+
+ /**
+ * Perform an OPTIONS request.
+ *
+ * @param path The URI path.
+ * @return A new servlet request.
+ * @throws Exception
+ */
+ public MockServletRequest options(String path) throws Exception {
+ return request("OPTIONS", path, null);
+ }
+}
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
new file mode 100644
index 0000000..c70c81d
--- /dev/null
+++ b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/mock/MockServletRequest.java
@@ -0,0 +1,1369 @@
+// ***************************************************************************************************************************
+// * 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.mock;
+
+import static org.apache.juneau.internal.StringUtils.*;
+
+import java.io.*;
+import java.security.*;
+import java.util.*;
+
+import javax.servlet.*;
+import javax.servlet.http.*;
+
+import org.apache.juneau.internal.*;
+import org.apache.juneau.rest.*;
+import org.apache.juneau.rest.util.*;
+import org.apache.juneau.rest.util.RestUtils;
+import org.apache.juneau.urlencoding.*;
+import org.apache.juneau.utils.*;
+
+/**
+ * An implementation of {@link HttpServletRequest} for mocking purposes.
+ *
+ * <h5 class='section'>See Also:</h5>
+ * <ul>
+ * <li class='link'>{@doc juneau-rest-server.UnitTesting}
+ * </ul>
+ *
+ * @deprecated Use <code>org.apache.juneau.rest.mock2</code>
+ */
+@Deprecated
+public class MockServletRequest implements HttpServletRequest, MockHttpRequest {
+
+ private String method = "GET";
+ private Map<String,String[]> queryData;
+ private Map<String,String[]> formDataMap;
+ private Map<String,String[]> headerMap = new LinkedHashMap<>();
+ private Map<String,Object> attributeMap = new LinkedHashMap<>();
+ private String characterEncoding = "UTF-8";
+ private byte[] body = new byte[0];
+ private String protocol = "HTTP/1.1";
+ private String scheme = "http";
+ private String serverName = "localhost";
+ private int serverPort = 8080;
+ private String remoteAddr = "";
+ private String remoteHost = "";
+ private Locale locale = Locale.ENGLISH;
+ private String realPath;
+ private int remotePort;
+ private String localName;
+ private String localAddr;
+ private int localPort;
+ private RequestDispatcher requestDispatcher;
+ private ServletContext servletContext;
+ private DispatcherType dispatcherType;
+ private String authType;
+ private Cookie[] cookies;
+ private String pathInfo;
+ private String pathTranslated;
+ private String contextPath = "";
+ private String queryString;
+ private String remoteUser;
+ private Principal userPrincipal;
+ private String requestedSessionId;
+ private String requestURI;
+ private String servletPath = "";
+ private HttpSession httpSession = MockHttpSession.create();
+ private RestContext restContext;
+ private String uri = "";
+ private boolean debug = false;
+
+ /**
+ * Creates a new servlet request.
+ *
+ * Initialized with the following:
+ * <ul>
+ * <li><code>"Accept: text/json+simple"</code>
+ * <li><code>"Content-Type: text/json"</code>
+ * </ul>
+ *
+ * @return A new request.
+ */
+ public static MockServletRequest create() {
+ MockServletRequest r = new MockServletRequest();
+ return r;
+ }
+
+ /**
+ * Creates a new servlet request with the specified method name and request path.
+ *
+ * Initialized with the following:
+ * <ul>
+ * <li><code>"Accept: text/json+simple"</code>
+ * <li><code>"Content-Type: text/json"</code>
+ * </ul>
+ *
+ * @param method The HTTP method name.
+ * @param path The request path.
+ * @param pathArgs Optional path arguments.
+ *
+ * @return A new request.
+ */
+ public static MockServletRequest create(String method, String path, Object...pathArgs) {
+ return create()
+ .method(method)
+ .uri(StringUtils.format(path, pathArgs));
+ }
+
+ /**
+ * Convenience method for setting <code>Accept</code> and <code>Content-Type</code> headers to <js>"application/json"</js>.
+ *
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest json() {
+ return header("Accept", "application/json").header("Content-Type", "application/json");
+ }
+
+ /**
+ * Convenience method for setting <code>Accept</code> and <code>Content-Type</code> headers to <js>"text/xml"</js>.
+ *
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest xml() {
+ return header("Accept", "text/xml").header("Content-Type", "text/xml");
+ }
+
+ /**
+ * Convenience method for setting <code>Accept</code> and <code>Content-Type</code> headers to <js>"text/html"</js>.
+ *
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest html() {
+ return header("Accept", "text/html").header("Content-Type", "text/html");
+ }
+
+ /**
+ * Convenience method for setting <code>Accept</code> and <code>Content-Type</code> headers to <js>"text/plain"</js>.
+ *
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest plainText() {
+ return header("Accept", "text/plain").header("Content-Type", "text/plain");
+ }
+
+ /**
+ * Convenience method for setting <code>Accept</code> and <code>Content-Type</code> headers to <js>"octal/msgpack"</js>.
+ *
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest msgpack() {
+ return header("Accept", "octal/msgpack").header("Content-Type", "octal/msgpack");
+ }
+
+ /**
+ * Convenience method for setting <code>Accept</code> and <code>Content-Type</code> headers to <js>"text/uon"</js>.
+ *
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest uon() {
+ return header("Accept", "text/uon").header("Content-Type", "text/uon");
+ }
+
+ /**
+ * Convenience method for setting <code>Accept</code> and <code>Content-Type</code> headers to <js>"application/x-www-form-urlencoded"</js>.
+ *
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest urlEnc() {
+ return header("Accept", "application/x-www-form-urlencoded").header("Content-Type", "application/x-www-form-urlencoded");
+ }
+
+ /**
+ * Convenience method for setting <code>Accept</code> and <code>Content-Type</code> headers to <js>"text/yaml"</js>.
+ *
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest yaml() {
+ return header("Accept", "text/yaml").header("Content-Type", "text/yaml");
+ }
+
+ /**
+ * Fluent setter.
+ *
+ * @param uri The URI of the request.
+ * @return This object (for method chaining).
+ */
+ @Override /* MockHttpRequest */
+ public MockServletRequest uri(String uri) {
+ this.uri = emptyIfNull(uri);
+ return this;
+ }
+
+ /**
+ * Fluent setter.
+ *
+ * @param restContext The rest context.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest restContext(RestContext restContext) {
+ this.restContext = restContext;
+ return this;
+ }
+
+ /**
+ * Executes this request and returns the response object.
+ *
+ * @return The response object.
+ * @throws Exception
+ */
+ @Override /* MockHttpRequest */
+ public MockServletResponse execute() throws Exception {
+ MockServletResponse res = MockServletResponse.create();
+ restContext.getCallHandler().service(this, res);
+
+ // If the status isn't set, something's broken.
+ if (res.getStatus() == 0)
+ throw new RuntimeException("Response status was 0.");
+
+ if (debug)
+ log(this, res);
+
+ return res;
+ }
+
+ private void log(MockServletRequest req, MockServletResponse res) {
+ StringBuilder sb = new StringBuilder();
+ sb.append("\n=== HTTP Call =================================================================");
+
+ sb.append("\n=== REQUEST ===");
+ sb.append("\nTODO");
+ sb.append("\n=== RESPONSE ===");
+ sb.append("\nStatus: ").append(res.getStatus());
+ sb.append("\n---response headers---");
+ for (Map.Entry<String,String[]> h : res.getHeaders().entrySet())
+ for (String h2 : h.getValue())
+ sb.append("\n").append(h.getKey()).append(": ").append(h2);
+ sb.append("\n---response content---\n");
+ sb.append(res.getBodyAsString());
+ sb.append("\n=== END ========================================================================");
+
+ System.err.println(sb); // NOT DEBUG
+ }
+
+ /**
+ * Fluent setter.
+ *
+ * @param value The method name for this request.
+ * @return This object (for method chaining).
+ */
+ @Override /* MockHttpRequest */
+ public MockServletRequest method(String value) {
+ this.method = value;
+ return this;
+ }
+
+ /**
+ * Fluent setter.
+ *
+ * @param value The character encoding.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest characterEncoding(String value) {
+ this.characterEncoding = value;
+ return this;
+ }
+
+ /**
+ * Fluent setter.
+ *
+ * @param value The protocol.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest protocol(String value) {
+ this.protocol = value;
+ return this;
+ }
+
+ /**
+ * Fluent setter.
+ *
+ * @param value The scheme.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest scheme(String value) {
+ this.scheme = value;
+ return this;
+ }
+
+ /**
+ * Fluent setter.
+ *
+ * @param value The server name.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest serverName(String value) {
+ this.serverName = value;
+ return this;
+ }
+
+ /**
+ * Fluent setter.
+ *
+ * @param value The server port.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest serverPort(int value) {
+ this.serverPort = value;
+ return this;
+ }
+
+ /**
+ * Fluent setter.
+ *
+ * @param value The remote address.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest remoteAddr(String value) {
+ this.remoteAddr = value;
+ return this;
+ }
+
+ /**
+ * Fluent setter.
+ *
+ * @param value The remote port.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest remoteHost(String value) {
+ this.remoteHost = value;
+ return this;
+ }
+
+ /**
+ * Fluent setter.
+ *
+ * @param value The locale.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest locale(Locale value) {
+ this.locale = value;
+ return this;
+ }
+
+ /**
+ * Fluent setter.
+ *
+ * @param value The real path.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest realPath(String value) {
+ this.realPath = value;
+ return this;
+ }
+
+ /**
+ * Fluent setter.
+ *
+ * @param value The remote port.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest remotePort(int value) {
+ this.remotePort = value;
+ return this;
+ }
+
+ /**
+ * Fluent setter.
+ *
+ * @param value The local name.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest localName(String value) {
+ this.localName = value;
+ return this;
+ }
+
+ /**
+ * Fluent setter.
+ *
+ * @param value The local address.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest localAddr(String value) {
+ this.localAddr = value;
+ return this;
+ }
+
+ /**
+ * Fluent setter.
+ *
+ * @param value The local port.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest localPort(int value) {
+ this.localPort = value;
+ return this;
+ }
+
+ /**
+ * Fluent setter.
+ *
+ * @param value The request dispatcher.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest requestDispatcher(RequestDispatcher value) {
+ this.requestDispatcher = value;
+ return this;
+ }
+
+ /**
+ * Fluent setter.
+ *
+ * @param value The servlet context.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest servletContext(ServletContext value) {
+ this.servletContext = value;
+ return this;
+ }
+
+ /**
+ * Fluent setter.
+ *
+ * @param value The dispatcher type.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest dispatcherType(DispatcherType value) {
+ this.dispatcherType = value;
+ return this;
+ }
+
+ /**
+ * Fluent setter.
+ *
+ * @param value The auth type.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest authType(String value) {
+ this.authType = value;
+ return this;
+ }
+
+ /**
+ * Fluent setter.
+ *
+ * @param value The cookies.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest cookies(Cookie[] value) {
+ this.cookies = value;
+ return this;
+ }
+
+ /**
+ * Fluent setter.
+ *
+ * @param value The path info.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest pathInfo(String value) {
+ this.pathInfo = value;
+ return this;
+ }
+
+ /**
+ * Fluent setter.
+ *
+ * @param value The path translated.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest pathTranslated(String value) {
+ this.pathTranslated = value;
+ return this;
+ }
+
+ /**
+ * Fluent setter.
+ *
+ * @param value The context path.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest contextPath(String value) {
+ this.contextPath = value;
+ return this;
+ }
+
+ /**
+ * Fluent setter.
+ *
+ * @param value The query string.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest queryString(String value) {
+ this.queryString = value;
+ return this;
+ }
+
+ /**
+ * Fluent setter.
+ *
+ * @param value The remote user.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest remoteUser(String value) {
+ this.remoteUser = value;
+ return this;
+ }
+
+ /**
+ * Fluent setter.
+ *
+ * @param value The user principal.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest userPrincipal(Principal value) {
+ this.userPrincipal = value;
+ return this;
+ }
+
+ /**
+ * Fluent setter.
+ *
+ * @param value The requested session ID.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest requestedSessionId(String value) {
+ this.requestedSessionId = value;
+ return this;
+ }
+
+ /**
+ * Fluent setter.
+ *
+ * @param value The request URI.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest requestURI(String value) {
+ this.requestURI = value;
+ return this;
+ }
+
+ /**
+ * Fluent setter.
+ *
+ * @param value The servlet path.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest servletPath(String value) {
+ this.servletPath = value;
+ return this;
+ }
+
+ /**
+ * Fluent setter.
+ *
+ * @param value The HTTP session.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest httpSession(HttpSession value) {
+ this.httpSession = value;
+ return this;
+ }
+
+ @Override /* HttpServletRequest */
+ public Object getAttribute(String name) {
+ return attributeMap.get(name);
+ }
+
+ @Override /* HttpServletRequest */
+ public Enumeration<String> getAttributeNames() {
+ return Collections.enumeration(attributeMap.keySet());
+ }
+
+ @Override /* HttpServletRequest */
+ public String getCharacterEncoding() {
+ return characterEncoding;
+ }
+
+ @Override /* HttpServletRequest */
+ public void setCharacterEncoding(String characterEncoding) throws UnsupportedEncodingException {
+ this.characterEncoding = characterEncoding;
+ }
+
+ @Override /* HttpServletRequest */
+ public int getContentLength() {
+ return body == null ? 0 : body.length;
+ }
+
+ @Override /* HttpServletRequest */
+ public long getContentLengthLong() {
+ return body == null ? 0 : body.length;
+ }
+
+ @Override /* HttpServletRequest */
+ public String getContentType() {
+ return getHeader("Content-Type");
+ }
+
+ @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);
+ }
+
+ @Override /* HttpServletRequest */
+ public String getParameter(String name) {
+ String[] s = getParameterMap().get(name);
+ return s == null || s.length == 0 ? null : s[0];
+ }
+
+ @Override /* HttpServletRequest */
+ public Enumeration<String> getParameterNames() {
+ return Collections.enumeration(new ArrayList<>(getParameterMap().keySet()));
+ }
+
+ @Override /* HttpServletRequest */
+ public String[] getParameterValues(String name) {
+ return getParameterMap().get(name);
+ }
+
+ @Override /* HttpServletRequest */
+ public Map<String,String[]> getParameterMap() {
+ if (queryData == null) {
+ try {
+ if ("POST".equalsIgnoreCase(method)) {
+ if (formDataMap != null)
+ queryData = formDataMap;
+ else
+ queryData = RestUtils.parseQuery(IOUtils.read(body));
+ } else {
+ queryData = RestUtils.parseQuery(getQueryString());
+ }
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+ return queryData;
+ }
+
+ @Override /* HttpServletRequest */
+ public String getProtocol() {
+ return protocol;
+ }
+
+ @Override /* HttpServletRequest */
+ public String getScheme() {
+ return scheme;
+ }
+
+ @Override /* HttpServletRequest */
+ public String getServerName() {
+ return serverName;
+ }
+
+ @Override /* HttpServletRequest */
+ public int getServerPort() {
+ return serverPort;
+ }
+
+ @Override /* HttpServletRequest */
+ public BufferedReader getReader() throws IOException {
+ return new BufferedReader(new InputStreamReader(getInputStream(), characterEncoding));
+ }
+
+ @Override /* HttpServletRequest */
+ public String getRemoteAddr() {
+ return remoteAddr;
+ }
+
+ @Override /* HttpServletRequest */
+ public String getRemoteHost() {
+ return remoteHost;
+ }
+
+ @Override /* HttpServletRequest */
+ public void setAttribute(String name, Object o) {
+ this.attributeMap.put(name, o);
+ }
+
+ @Override /* HttpServletRequest */
+ public void removeAttribute(String name) {
+ this.attributeMap.remove(name);
+ }
+
+ @Override /* HttpServletRequest */
+ public Locale getLocale() {
+ return locale;
+ }
+
+ @Override /* HttpServletRequest */
+ public Enumeration<Locale> getLocales() {
+ return Collections.enumeration(Arrays.asList(locale));
+ }
+
+ @Override /* HttpServletRequest */
+ public boolean isSecure() {
+ return false;
+ }
+
+ @Override /* HttpServletRequest */
+ public RequestDispatcher getRequestDispatcher(String path) {
+ return requestDispatcher;
+ }
+
+ @Override /* HttpServletRequest */
+ public String getRealPath(String path) {
+ return realPath;
+ }
+
+ @Override /* HttpServletRequest */
+ public int getRemotePort() {
+ return remotePort;
+ }
+
+ @Override /* HttpServletRequest */
+ public String getLocalName() {
+ return localName;
+ }
+
+ @Override /* HttpServletRequest */
+ public String getLocalAddr() {
+ return localAddr;
+ }
+
+ @Override /* HttpServletRequest */
+ public int getLocalPort() {
+ return localPort;
+ }
+
+ @Override /* HttpServletRequest */
+ public ServletContext getServletContext() {
+ return servletContext;
+ }
+
+ @Override /* HttpServletRequest */
+ public AsyncContext startAsync() throws IllegalStateException {
+ return null;
+ }
+
+ @Override /* HttpServletRequest */
+ public AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse) throws IllegalStateException {
+ return null;
+ }
+
+ @Override /* HttpServletRequest */
+ public boolean isAsyncStarted() {
+ return false;
+ }
+
+ @Override /* HttpServletRequest */
+ public boolean isAsyncSupported() {
+ return false;
+ }
+
+ @Override /* HttpServletRequest */
+ public AsyncContext getAsyncContext() {
+ return null;
+ }
+
+ @Override /* HttpServletRequest */
+ public DispatcherType getDispatcherType() {
+ return dispatcherType;
+ }
+
+ @Override /* HttpServletRequest */
+ public String getAuthType() {
+ return authType;
+ }
+
+ @Override /* HttpServletRequest */
+ public Cookie[] getCookies() {
+ return cookies;
+ }
+
+ @Override /* HttpServletRequest */
+ public long getDateHeader(String name) {
+ String s = getHeader(name);
+ return s == null ? 0 : org.apache.juneau.http.Date.forString(s).asDate().getTime();
+ }
+
+ @Override /* HttpServletRequest */
+ public String getHeader(String name) {
+ String[] s = headerMap.get(name);
+ return s == null || s.length == 0 ? null : s[0];
+ }
+
+ @Override /* HttpServletRequest */
+ public Enumeration<String> getHeaders(String name) {
+ String[] s = headerMap.get(name);
+ return Collections.enumeration(Arrays.asList(s == null ? new String[0] : s));
+ }
+
+ @Override /* HttpServletRequest */
+ public Enumeration<String> getHeaderNames() {
+ return Collections.enumeration(headerMap.keySet());
+ }
+
+ @Override /* HttpServletRequest */
+ public int getIntHeader(String name) {
+ String s = getHeader(name);
+ return s == null || s.isEmpty() ? 0 : Integer.parseInt(s);
+ }
+
+ @Override /* HttpServletRequest */
+ public String getMethod() {
+ return method;
+ }
+
+ @Override /* HttpServletRequest */
+ public String getPathInfo() {
+ if (pathInfo == null) {
+ pathInfo = getRequestURI();
+ if (isNotEmpty(contextPath))
+ pathInfo = pathInfo.substring(contextPath.length());
+ if (isNotEmpty(servletPath))
+ pathInfo = pathInfo.substring(servletPath.length());
+ }
+ return nullIfEmpty(urlDecode(pathInfo));
+ }
+
+ @Override /* HttpServletRequest */
+ public String getPathTranslated() {
+ if (pathTranslated == null)
+ pathTranslated = "/mock-path" + getPathInfo();
+ return pathTranslated;
+ }
+
+ @Override /* HttpServletRequest */
+ public String getContextPath() {
+ return contextPath;
+ }
+
+ @Override /* HttpServletRequest */
+ public String getQueryString() {
+ if (queryString == null) {
+ queryString = "";
+ if (uri.indexOf('?') != -1) {
+ queryString = uri.substring(uri.indexOf('?') + 1);
+ if (queryString.indexOf('#') != -1)
+ queryString = queryString.substring(0, queryString.indexOf('#'));
+ }
+ }
+ return isEmpty(queryString) ? null : queryString;
+ }
+
+ @Override /* HttpServletRequest */
+ public String getRemoteUser() {
+ return remoteUser;
+ }
+
+ @Override /* HttpServletRequest */
+ public boolean isUserInRole(String role) {
+ return false;
+ }
+
+ @Override /* HttpServletRequest */
+ public Principal getUserPrincipal() {
+ return userPrincipal;
+ }
+
+ @Override /* HttpServletRequest */
+ public String getRequestedSessionId() {
+ return requestedSessionId;
+ }
+
+ @Override /* HttpServletRequest */
+ public String getRequestURI() {
+ if (requestURI == null) {
+ requestURI = uri;
+ requestURI = requestURI.replaceAll("^\\w+\\:\\/\\/[^\\/]+", "").replaceAll("\\?.*$", "");
+ }
+ return requestURI;
+ }
+
+ @Override /* HttpServletRequest */
+ public StringBuffer getRequestURL() {
+ return new StringBuffer(uri.replaceAll("\\?.*$", ""));
+ }
+
+ @Override /* HttpServletRequest */
+ public String getServletPath() {
+ return servletPath;
+ }
+
+ @Override /* HttpServletRequest */
+ public HttpSession getSession(boolean create) {
+ return httpSession;
+ }
+
+ @Override /* HttpServletRequest */
+ public HttpSession getSession() {
+ return httpSession;
+ }
+
+ @Override /* HttpServletRequest */
+ public String changeSessionId() {
+ return null;
+ }
+
+ @Override /* HttpServletRequest */
+ public boolean isRequestedSessionIdValid() {
+ return false;
+ }
+
+ @Override /* HttpServletRequest */
+ public boolean isRequestedSessionIdFromCookie() {
+ return false;
+ }
+
+ @Override /* HttpServletRequest */
+ public boolean isRequestedSessionIdFromURL() {
+ return false;
+ }
+
+ @Override /* HttpServletRequest */
+ public boolean isRequestedSessionIdFromUrl() {
+ return false;
+ }
+
+ @Override /* HttpServletRequest */
+ public boolean authenticate(HttpServletResponse response) throws IOException, ServletException {
+ return false;
+ }
+
+ @Override /* HttpServletRequest */
+ public void login(String username, String password) throws ServletException {
+ }
+
+ @Override /* HttpServletRequest */
+ public void logout() throws ServletException {
+ }
+
+ @Override /* HttpServletRequest */
+ public Collection<Part> getParts() throws IOException, ServletException {
+ return null;
+ }
+
+ @Override /* HttpServletRequest */
+ public Part getPart(String name) throws IOException, ServletException {
+ return null;
+ }
+
+ @Override /* HttpServletRequest */
+ public <T extends HttpUpgradeHandler> T upgrade(Class<T> handlerClass) throws IOException, ServletException {
+ return null;
+ }
+
+ //=================================================================================================================
+ // Convenience methods
+ //=================================================================================================================
+
+ /**
+ * Fluent setter.
+ *
+ * @param name Header name.
+ * @param value
+ * Header value.
+ * <br>The value is converted to a simple string using {@link Object#toString()}.
+ * @return This object (for method chaining).
+ */
+ @Override /* MockHttpRequest */
+ public MockServletRequest header(String name, Object value) {
+ this.headerMap.put(name, new String[] {asString(value)});
+ return this;
+ }
+
+ /**
+ * Fluent setter.
+ *
+ * @param name Request attribute name.
+ * @param value Request attribute value.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest attribute(String name, Object value) {
+ this.attributeMap.put(name, value);
+ return this;
+ }
+
+ /**
+ * Fluent setter.
+ *
+ * @param value
+ * The body of the request.
+ * <br>Can be any of the following data types:
+ * <ul>
+ * <li><code><jk>byte</jk>[]</code>
+ * <li>{@link Reader}
+ * <li>{@link InputStream}
+ * <li>{@link CharSequence}
+ * </ul>
+ * @return This object (for method chaining).
+ */
+ @Override /* MockHttpRequest */
+ public MockServletRequest body(Object value) {
+ try {
+ if (value instanceof byte[])
+ this.body = (byte[])value;
+ if (value instanceof Reader)
+ this.body = IOUtils.read((Reader)value).getBytes();
+ if (value instanceof InputStream)
+ this.body = IOUtils.readBytes((InputStream)value, 1024);
+ if (value instanceof CharSequence)
+ this.body = ((CharSequence)value).toString().getBytes();
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ return this;
+ }
+
+ /**
+ * Adds a form data entry to this request.
+ *
+ * @param key The form data key.
+ * @param value The form data value.
+ * <br>The value is converted to a simple string using {@link Object#toString()}.
+ * @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;
+ }
+
+ /**
+ * Adds a query data entry to this request.
+ *
+ * @param key The query key.
+ * @param value The query value.
+ * <br>The value is converted to a simple string using {@link Object#toString()}.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest query(String key, Object value) {
+ if (queryData == null)
+ queryData = new LinkedHashMap<>();
+ String s = asString(value);
+ String[] existing = queryData.get(key);
+ if (existing == null)
+ existing = new String[]{s};
+ else
+ existing = new AList<>().appendAll(Arrays.asList(existing)).append(s).toArray(new String[0]);
+ queryData.put(key, existing);
+ return this;
+ }
+
+ //=================================================================================================================
+ // Convenience methods - headers
+ //=================================================================================================================
+
+ /**
+ * Specifies the <code>Accept</code> header value on the request.
+ *
+ * @param value The new value.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest accept(Object value) {
+ return header("Accept", value);
+ }
+
+ /**
+ * Specifies the <code>Accept-Charset</code> header value on the request.
+ *
+ * @param value The new value.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest acceptCharset(Object value) {
+ return header("Accept-Charset", value);
+ }
+
+ /**
+ * Specifies the <code>Accept-Encoding</code> header value on the request.
+ *
+ * @param value The new value.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest acceptEncoding(Object value) {
+ return header("Accept-Encoding", value);
+ }
+
+ /**
+ * Specifies the <code>Accept-Language</code> header value on the request.
+ *
+ * @param value The new value.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest acceptLanguage(Object value) {
+ return header("Accept-Language", value);
+ }
+
+ /**
+ * Specifies the <code>Authorization</code> header value on the request.
+ *
+ * @param value The new value for the header.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest authorization(Object value) {
+ return header("Authorization", value);
+ }
+
+ /**
+ * Specifies the <code>Cache-Control</code> header value on the request.
+ *
+ * @param value The new value for the header.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest cacheControl(Object value) {
+ return header("Cache-Control", 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(Object value) {
+ return header("X-Client-Version", value);
+ }
+
+ /**
+ * Specifies the <code>Connection</code> header value on the request.
+ *
+ * @param value The new value for the header.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest connection(Object value) {
+ return header("Connection", value);
+ }
+
+ /**
+ * Specifies the <code>Content-Encoding</code> header value on the request.
+ *
+ * @param value The new value.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest contentEncoding(Object value) {
+ return header("Content-Encoding", value);
+ }
+
+ /**
+ * Specifies the <code>Content-Length</code> header value on the request.
+ *
+ * @param value The new value for the header.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest contentLength(Object value) {
+ return header("Content-Length", value);
+ }
+
+ /**
+ * Specifies the <code>Content-Type</code> header value on the request.
+ *
+ * @param value The new value.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest contentType(Object value) {
+ return header("Content-Type", value);
+ }
+
+ /**
+ * Specifies the <code>Date</code> header value on the request.
+ *
+ * @param value The new value for the header.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest date(Object value) {
+ return header("Date", value);
+ }
+
+ /**
+ * Specifies the <code>Expect</code> header value on the request.
+ *
+ * @param value The new value for the header.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest expect(Object value) {
+ return header("Expect", value);
+ }
+
+ /**
+ * Specifies the <code>From</code> header value on the request.
+ *
+ * @param value The new value for the header.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest from(Object value) {
+ return header("From", value);
+ }
+
+ /**
+ * Specifies the <code>Host</code> header value on the request.
+ *
+ * @param value The new value for the header.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest host(Object value) {
+ return header("Host", value);
+ }
+
+ /**
+ * Specifies the <code>If-Match</code> header value on the request.
+ *
+ * @param value The new value for the header.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest ifMatch(Object value) {
+ return header("If-Match", value);
+ }
+
+ /**
+ * Specifies the <code>If-Modified-Since</code> header value on the request.
+ *
+ * @param value The new value for the header.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest ifModifiedSince(Object value) {
+ return header("If-Modified-Since", value);
+ }
+
+ /**
+ * Specifies the <code>If-None-Match</code> header value on the request.
+ *
+ * @param value The new value for the header.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest ifNoneMatch(Object value) {
+ return header("If-None-Match", value);
+ }
+
+ /**
+ * Specifies the <code>If-Range</code> header value on the request.
+ *
+ * @param value The new value for the header.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest ifRange(Object value) {
+ return header("If-Range", value);
+ }
+
+ /**
+ * Specifies the <code>If-Unmodified-Since</code> header value on the request.
+ *
+ * @param value The new value for the header.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest ifUnmodifiedSince(Object value) {
+ return header("If-Unmodified-Since", value);
+ }
+
+ /**
+ * Specifies the <code>Max-Forwards</code> header value on the request.
+ *
+ * @param value The new value for the header.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest maxForwards(Object value) {
+ return header("Max-Forwards", value);
+ }
+
+ /**
+ * Specifies the <code>Pragma</code> header value on the request.
+ *
+ * @param value The new value for the header.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest pragma(Object value) {
+ return header("Pragma", value);
+ }
+
+ /**
+ * Specifies the <code>Proxy-Authorization</code> header value on the request.
+ *
+ * @param value The new value for the header.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest proxyAuthorization(Object value) {
+ return header("Proxy-Authorization", value);
+ }
+
+ /**
+ * Specifies the <code>Range</code> header value on the request.
+ *
+ * @param value The new value for the header.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest range(Object value) {
+ return header("Range", value);
+ }
+
+ /**
+ * Specifies the <code>Referer</code> header value on the request.
+ *
+ * @param value The new value for the header.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest referer(Object value) {
+ return header("Referer", value);
+ }
+
+ /**
+ * Specifies the <code>TE</code> header value on the request.
+ *
+ * @param value The new value for the header.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest te(Object value) {
+ return header("TE", value);
+ }
+
+ /**
+ * Specifies the <code>Upgrade</code> header value on the request.
+ *
+ * @param value The new value for the header.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest upgrade(Object value) {
+ return header("Upgrade", value);
+ }
+
+ /**
+ * Specifies the <code>User-Agent</code> header value on the request.
+ *
+ * @param value The new value for the header.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest userAgent(Object value) {
+ return header("User-Agent", value);
+ }
+
+ /**
+ * Specifies the <code>Warning</code> header value on the request.
+ *
+ * @param value The new value for the header.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest warning(Object value) {
+ return header("Warning", value);
+ }
+
+ /**
+ * Enabled debug mode on this request.
+ *
+ * <p>
+ * Causes information about the request execution to be sent to STDERR.
+ *
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest debug() {
+ this.debug = true;
+ return this;
+ }
+}
diff --git a/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockServletResponse.java b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/mock/MockServletResponse.java
similarity index 98%
copy from juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockServletResponse.java
copy to juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/mock/MockServletResponse.java
index 2ceda90..84a85d9 100644
--- a/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockServletResponse.java
+++ b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/mock/MockServletResponse.java
@@ -10,7 +10,7 @@
// * "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.mock2;
+package org.apache.juneau.rest.mock;
import static org.apache.juneau.internal.StringUtils.*;
@@ -31,9 +31,12 @@ import org.apache.juneau.utils.*;
*
* <h5 class='section'>See Also:</h5>
* <ul>
- * <li class='link'>{@doc juneau-rest-mock.MockRest}
+ * <li class='link'>{@doc juneau-rest-server.UnitTesting}
* </ul>
+ *
+ * @deprecated Use <code>org.apache.juneau.rest.mock2</code>
*/
+@Deprecated
public class MockServletResponse implements HttpServletResponse, MockHttpResponse {
private String characterEncoding = "UTF-8";
@@ -252,7 +255,7 @@ public class MockServletResponse implements HttpServletResponse, MockHttpRespons
@Override /* HttpServletResponse */
public Collection<String> getHeaders(String name) {
String[] s = headerMap.get(name);
- return s == null ? Collections.emptyList() : Arrays.asList(s);
+ return s == null ? Collections.EMPTY_LIST : Arrays.asList(s);
}
@Override /* HttpServletResponse */