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 051e0d6 Context API refactoring.
051e0d6 is described below
commit 051e0d6f311104db6e13fdba03b9601c380cf1c5
Author: JamesBognar <[email protected]>
AuthorDate: Fri Sep 24 18:20:25 2021 -0400
Context API refactoring.
---
.../apache/juneau/rest/mock/MockRestClient.java | 157 ++++++++++-----------
.../juneau/rest/mock/MockRestClientBuilder.java | 36 ++++-
2 files changed, 101 insertions(+), 92 deletions(-)
diff --git
a/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock/MockRestClient.java
b/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock/MockRestClient.java
index a93ad30..bd818f8 100644
---
a/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock/MockRestClient.java
+++
b/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock/MockRestClient.java
@@ -218,94 +218,11 @@ import org.apache.juneau.rest.logging.*;
public class MockRestClient extends RestClient implements HttpClientConnection
{
//-------------------------------------------------------------------------------------------------------------------
- // Configurable properties
+ // Static
//-------------------------------------------------------------------------------------------------------------------
- private static final String PREFIX = "RestClient.";
-
- @SuppressWarnings("javadoc")
- public static final String
- MOCKRESTCLIENT_restBean = PREFIX + "restBean.o",
- MOCKRESTCLIENT_restBeanCtx = PREFIX + "restBeanCtx.o",
- MOCKRESTCLIENT_servletPath = PREFIX + "servletPath.s",
- MOCKRESTCLIENT_contextPath = PREFIX + "contextPath.s",
- MOCKRESTCLIENT_pathVars = PREFIX + "pathVars.oms";
-
-
private static Map<Class<?>,RestContext> REST_CONTEXTS = new
ConcurrentHashMap<>();
-
//-------------------------------------------------------------------------------------------------------------------
- // Instance properties
-
//-------------------------------------------------------------------------------------------------------------------
-
- private final RestContext restBeanCtx;
- private final Object restObject;
- private final String contextPath, servletPath;
- private final Map<String,String> pathVars;
-
- private final ThreadLocal<HttpRequest> rreq = new ThreadLocal<>();
- private final ThreadLocal<MockRestResponse> rres = new ThreadLocal<>();
- private final ThreadLocal<MockServletRequest> sreq = new
ThreadLocal<>();
- private final ThreadLocal<MockServletResponse> sres = new
ThreadLocal<>();
-
- /**
- * Constructor.
- *
- * @param builder
- * The builder for this object.
- */
- public MockRestClient(MockRestClientBuilder builder) {
- super(preInit(builder));
- ContextProperties cp = getContextProperties();
- this.restBeanCtx = cp.getInstance(MOCKRESTCLIENT_restBeanCtx,
RestContext.class).get();
- this.restObject = restBeanCtx.getResource();
- this.contextPath =
cp.getString(MOCKRESTCLIENT_contextPath).orElse("");
- this.servletPath =
cp.getString(MOCKRESTCLIENT_servletPath).orElse("");
- this.pathVars = cp.getMap(MOCKRESTCLIENT_pathVars,
String.class).orElse(emptyMap());
-
- HttpClientConnectionManager ccm =
getHttpClientConnectionManager();
- if (ccm instanceof MockHttpClientConnectionManager)
- ((MockHttpClientConnectionManager)ccm).init(this);
- }
-
- private static MockRestClientBuilder preInit(MockRestClientBuilder
builder) {
- try {
- ContextProperties cp = builder.getContextProperties();
- Object restBean =
cp.getInstance(MOCKRESTCLIENT_restBean, Object.class).orElse(null);
- String contextPath = cp.get(MOCKRESTCLIENT_contextPath,
String.class).orElse(null);
- String servletPath = cp.get(MOCKRESTCLIENT_servletPath,
String.class).orElse(null);
- String rootUrl =
ofNullable(builder.getRootUri()).orElse("http://localhost");
-
- Class<?> c = restBean instanceof Class ?
(Class<?>)restBean : restBean.getClass();
- if (! REST_CONTEXTS.containsKey(c)) {
- boolean isClass = restBean instanceof Class;
- Object o = isClass ?
((Class<?>)restBean).newInstance() : restBean;
- RestContext rc = RestContext
- .create(o.getClass(), null, null)
-
.defaultClasses(BasicTestRestLogger.class)
- .debugDefault(CONDITIONAL)
- .init(()->o)
- .build()
- .postInit()
- .postInitChildFirst();
- REST_CONTEXTS.put(c, rc);
- }
- RestContext restBeanCtx = REST_CONTEXTS.get(c);
- builder.set(MOCKRESTCLIENT_restBeanCtx, restBeanCtx);
-
- if (servletPath == null)
- servletPath =
toValidContextPath(restBeanCtx.getFullPath());
-
- rootUrl = rootUrl + emptyIfNull(contextPath) +
emptyIfNull(servletPath);
-
- builder.set(MOCKRESTCLIENT_servletPath, servletPath);
- builder.rootUri(rootUrl);
- return builder;
- } catch (Exception e) {
- throw new ConfigException(e, "Could not initialize
MockRestClient");
- }
- }
-
/**
* Creates a new {@link RestClientBuilder} configured with the
specified REST implementation bean or bean class.
*
@@ -450,6 +367,76 @@ public class MockRestClient extends RestClient implements
HttpClientConnection {
return
create(impl).simpleJson().ignoreErrors().noTrace().build();
}
+
//-------------------------------------------------------------------------------------------------------------------
+ // Instance
+
//-------------------------------------------------------------------------------------------------------------------
+
+ private final RestContext restContext;
+ private final Object restObject;
+ private final String contextPath, servletPath;
+ private final Map<String,String> pathVars;
+
+ private final ThreadLocal<HttpRequest> rreq = new ThreadLocal<>();
+ private final ThreadLocal<MockRestResponse> rres = new ThreadLocal<>();
+ private final ThreadLocal<MockServletRequest> sreq = new
ThreadLocal<>();
+ private final ThreadLocal<MockServletResponse> sres = new
ThreadLocal<>();
+
+ /**
+ * Constructor.
+ *
+ * @param builder
+ * The builder for this object.
+ */
+ public MockRestClient(MockRestClientBuilder builder) {
+ super(preInit(builder));
+ restContext = builder.restContext;
+ contextPath = ofNullable(builder.contextPath).orElse("");
+ servletPath = ofNullable(builder.servletPath).orElse("");
+ pathVars = ofNullable(builder.pathVars).orElse(emptyMap());
+ restObject = restContext.getResource();
+
+ HttpClientConnectionManager ccm =
getHttpClientConnectionManager();
+ if (ccm instanceof MockHttpClientConnectionManager)
+ ((MockHttpClientConnectionManager)ccm).init(this);
+ }
+
+ private static MockRestClientBuilder preInit(MockRestClientBuilder
builder) {
+ try {
+ Object restBean = builder.restBean;
+ String contextPath = builder.contextPath;
+ String servletPath = builder.servletPath;
+ String rootUrl =
ofNullable(builder.getRootUri()).orElse("http://localhost");
+
+ Class<?> c = restBean instanceof Class ?
(Class<?>)restBean : restBean.getClass();
+ if (! REST_CONTEXTS.containsKey(c)) {
+ boolean isClass = restBean instanceof Class;
+ Object o = isClass ?
((Class<?>)restBean).newInstance() : restBean;
+ RestContext rc = RestContext
+ .create(o.getClass(), null, null)
+
.defaultClasses(BasicTestRestLogger.class)
+ .debugDefault(CONDITIONAL)
+ .init(()->o)
+ .build()
+ .postInit()
+ .postInitChildFirst();
+ REST_CONTEXTS.put(c, rc);
+ }
+ RestContext restBeanCtx = REST_CONTEXTS.get(c);
+ builder.restContext(restBeanCtx);
+
+ if (servletPath == null)
+ servletPath =
toValidContextPath(restBeanCtx.getFullPath());
+
+ rootUrl = rootUrl + emptyIfNull(contextPath) +
emptyIfNull(servletPath);
+
+ builder.servletPath = servletPath;
+ builder.rootUri(rootUrl);
+ return builder;
+ } catch (Exception e) {
+ throw new ConfigException(e, "Could not initialize
MockRestClient");
+ }
+ }
+
//------------------------------------------------------------------------------------------------------------------
// Entry point methods.
//------------------------------------------------------------------------------------------------------------------
@@ -760,7 +747,7 @@ public class MockRestClient extends RestClient implements
HttpClientConnection {
public HttpResponse receiveResponseHeader() throws HttpException,
IOException {
try {
MockServletResponse res = MockServletResponse.create();
- restBeanCtx.execute(restObject, sreq.get(), res);
+ restContext.execute(restObject, sreq.get(), res);
// If the status isn't set, something's broken.
if (res.getStatus() == 0)
diff --git
a/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock/MockRestClientBuilder.java
b/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock/MockRestClientBuilder.java
index fae09d5..73a4d2d 100644
---
a/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock/MockRestClientBuilder.java
+++
b/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock/MockRestClientBuilder.java
@@ -17,7 +17,6 @@ import java.util.concurrent.*;
import java.util.function.*;
import java.util.logging.*;
-import static org.apache.juneau.rest.mock.MockRestClient.*;
import static org.apache.juneau.rest.util.RestUtils.*;
import java.io.*;
@@ -36,8 +35,11 @@ import org.apache.juneau.httppart.*;
import org.apache.juneau.internal.*;
import org.apache.juneau.marshall.*;
import org.apache.juneau.parser.*;
+import org.apache.juneau.rest.*;
import org.apache.juneau.rest.annotation.*;
import org.apache.juneau.rest.client.*;
+import org.apache.juneau.rest.client.RestRequest;
+import org.apache.juneau.rest.client.RestResponse;
import org.apache.juneau.serializer.*;
import org.apache.juneau.uon.*;
@@ -64,6 +66,11 @@ import org.apache.http.protocol.*;
@FluentSetters(ignore="debug")
public class MockRestClientBuilder extends RestClientBuilder {
+ Object restBean;
+ String contextPath, servletPath;
+ RestContext restContext;
+ Map<String,String> pathVars;
+
/**
* No-arg constructor.
*
@@ -84,11 +91,23 @@ public class MockRestClientBuilder extends
RestClientBuilder {
/**
* Specifies the {@link Rest}-annotated bean class or instance to test
against.
*
- * @param bean The {@link Rest}-annotated bean class or instance.
+ * @param value The {@link Rest}-annotated bean class or instance.
+ * @return This object (for method chaining).
+ */
+ public MockRestClientBuilder restBean(Object value) {
+ restBean = value;
+ return this;
+ }
+
+ /**
+ * Specifies the {@link RestContext} created for the REST bean.
+ *
+ * @param value The {@link RestContext} created for the REST bean.
* @return This object (for method chaining).
*/
- public MockRestClientBuilder restBean(Object bean) {
- return set(MOCKRESTCLIENT_restBean, bean);
+ public MockRestClientBuilder restContext(RestContext value) {
+ restContext = value;
+ return this;
}
/**
@@ -113,7 +132,8 @@ public class MockRestClientBuilder extends
RestClientBuilder {
* @return This object (for method chaining).
*/
public MockRestClientBuilder contextPath(String value) {
- return set(MOCKRESTCLIENT_contextPath,
toValidContextPath(value));
+ contextPath = toValidContextPath(value);
+ return this;
}
/**
@@ -138,7 +158,8 @@ public class MockRestClientBuilder extends
RestClientBuilder {
* @return This object (for method chaining).
*/
public MockRestClientBuilder servletPath(String value) {
- return set(MOCKRESTCLIENT_servletPath,
toValidContextPath(value));
+ servletPath = toValidContextPath(value);
+ return this;
}
/**
@@ -188,7 +209,8 @@ public class MockRestClientBuilder extends
RestClientBuilder {
* @see MockServletRequest#pathVars(Map)
*/
public MockRestClientBuilder pathVars(Map<String,String> value) {
- return putAllTo(MOCKRESTCLIENT_pathVars, value);
+ pathVars = value;
+ return this;
}
/**