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 d5b2f02 Context API refactoring.
d5b2f02 is described below
commit d5b2f020ef48566913fbe4d477527a9a25080c8f
Author: JamesBognar <[email protected]>
AuthorDate: Tue Sep 28 18:29:10 2021 -0400
Context API refactoring.
---
.../main/java/org/apache/juneau/config/Config.java | 2 +-
.../main/java/org/apache/juneau/BeanContext.java | 2 +-
.../org/apache/juneau/BeanTraverseContext.java | 2 +-
.../src/main/java/org/apache/juneau/Context.java | 88 +--------
.../java/org/apache/juneau/ContextBuilder.java | 16 +-
.../src/main/java/org/apache/juneau/Session.java | 6 +-
.../main/java/org/apache/juneau/SessionArgs.java | 10 +-
.../org/apache/juneau/annotation/BeanConfig.java | 2 +-
.../juneau/annotation/BeanConfigAnnotation.java | 3 +-
.../java/org/apache/juneau/parser/ParserPipe.java | 2 +-
.../org/apache/juneau/parser/ParserSession.java | 2 +-
.../org/apache/juneau/rest/client/RestClient.java | 2 +-
.../juneau/rest/client/RestClientBuilder.java | 3 +-
.../java/org/apache/juneau/ContextCacheTest.java | 209 ---------------------
14 files changed, 39 insertions(+), 310 deletions(-)
diff --git
a/juneau-core/juneau-config/src/main/java/org/apache/juneau/config/Config.java
b/juneau-core/juneau-config/src/main/java/org/apache/juneau/config/Config.java
index cf7e9fb..759080e 100644
---
a/juneau-core/juneau-config/src/main/java/org/apache/juneau/config/Config.java
+++
b/juneau-core/juneau-config/src/main/java/org/apache/juneau/config/Config.java
@@ -229,7 +229,7 @@ public final class Config extends Context implements
ConfigEventListener, Writab
}
Config(Config copyFrom, VarResolverSession varSession) {
- super(null, true);
+ super(copyFrom);
name = copyFrom.name;
store = copyFrom.store;
configMap = copyFrom.configMap;
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanContext.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanContext.java
index e7866bf..0a9413c 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanContext.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanContext.java
@@ -251,7 +251,7 @@ public class BeanContext extends Context {
* @param builder The builder for this object.
*/
public BeanContext(BeanContextBuilder builder) {
- super(builder.getContextProperties(), true);
+ super(builder);
ContextProperties cp = builder.getContextProperties();
if (cp == null)
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanTraverseContext.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanTraverseContext.java
index bbef063..6f8cd31 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanTraverseContext.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanTraverseContext.java
@@ -51,7 +51,7 @@ public abstract class BeanTraverseContext extends
BeanContextable {
ignoreRecursions = builder.ignoreRecursions;
detectRecursions = builder.detectRecursions;
- actualDetectRecursions = detectRecursions || ignoreRecursions;
+ actualDetectRecursions = detectRecursions || ignoreRecursions
|| super.isDebug();
}
@Override
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/Context.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/Context.java
index cb62141..be45593 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/Context.java
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/Context.java
@@ -22,7 +22,6 @@ import java.util.concurrent.*;
import org.apache.juneau.annotation.*;
import org.apache.juneau.collections.*;
-import org.apache.juneau.internal.*;
import org.apache.juneau.json.*;
import org.apache.juneau.reflect.*;
import org.apache.juneau.utils.*;
@@ -104,89 +103,21 @@ public abstract class Context implements MetaProvider {
*/
public static final String CONTEXT_annotations = PREFIX +
".annotations.lo";
- /**
- * Configuration property: Debug mode.
- *
- * <p>
- * Enables the following additional information during serialization:
- * <ul class='spaced-list'>
- * <li>
- * When bean getters throws exceptions, the exception
includes the object stack information
- * in order to determine how that method was invoked.
- * <li>
- * Enables {@link BeanTraverseBuilder#detectRecursions()}.
- * </ul>
- *
- * <h5 class='section'>Property:</h5>
- * <ul class='spaced-list'>
- * <li><b>ID:</b> {@link org.apache.juneau.Context#CONTEXT_debug
CONTEXT_debug}
- * <li><b>Name:</b> <js>"Context.debug.b"</js>
- * <li><b>Data type:</b> <jk>boolean</jk>
- * <li><b>System property:</b> <c>Context.debug</c>
- * <li><b>Environment variable:</b> <c>CONTEXT_DEBUG</c>
- * <li><b>Default:</b> <jk>false</jk>
- * <li><b>Session property:</b> <jk>true</jk>
- * <li><b>Annotations:</b>
- * <ul>
- * <li class='ja'>{@link
org.apache.juneau.annotation.BeanConfig#debug()}
- * </ul>
- * <li><b>Methods:</b>
- * <ul>
- * <li class='jm'>{@link
org.apache.juneau.ContextBuilder#debug()}
- * <li class='jm'>{@link
org.apache.juneau.SessionArgs#debug(Boolean)}
- * </ul>
- * </ul>
- */
- public static final String CONTEXT_debug = PREFIX + ".debug.b";
-
-
final ContextProperties properties;
private final int identityCode;
private final ReflectionMap<Annotation> annotations;
-
final boolean debug;
/**
- * Constructor for this class.
- *
- * <p>
- * Subclasses MUST implement the same public constructor.
+ * Copy constructor.
*
- * @param cp The read-only configuration for this context object.
- * @param allowReuse If <jk>true</jk>, subclasses that share the same
property store values can be reused.
+ * @param copyFrom The context to copy from.
*/
- public Context(ContextProperties cp, boolean allowReuse) {
- properties = cp == null ? ContextProperties.DEFAULT : cp;
- cp = properties;
- this.identityCode = allowReuse ? new
HashCode().add(className(this)).add(cp).get() : System.identityHashCode(this);
- debug = cp.getBoolean(CONTEXT_debug).orElse(false);
-
- ReflectionMap.Builder<Annotation> rmb =
ReflectionMap.create(Annotation.class);
- for (Annotation a : cp.getList(CONTEXT_annotations,
Annotation.class).orElse(emptyList())) {
- try {
- ClassInfo ci = ClassInfo.of(a.getClass());
-
- MethodInfo mi = ci.getMethod("onClass");
- if (mi != null) {
- if (!
mi.getReturnType().is(Class[].class))
- throw new
ConfigException("Invalid annotation @{0} used in BEAN_annotations property.
Annotation must define an onClass() method that returns a Class array.",
a.getClass().getSimpleName());
- for (Class<?> c :
(Class<?>[])mi.accessible().invoke(a))
- rmb.append(c.getName(), a);
- }
-
- mi = ci.getMethod("on");
- if (mi != null) {
- if (!
mi.getReturnType().is(String[].class))
- throw new
ConfigException("Invalid annotation @{0} used in BEAN_annotations property.
Annotation must define an on() method that returns a String array.",
a.getClass().getSimpleName());
- for (String s :
(String[])mi.accessible().invoke(a))
- rmb.append(s, a);
- }
-
- } catch (Exception e) {
- throw new ConfigException(e, "Invalid
annotation @{0} used in BEAN_annotations property.", className(a));
- }
- }
- this.annotations = rmb.build();
+ protected Context(Context copyFrom) {
+ identityCode = copyFrom.identityCode;
+ annotations = copyFrom.annotations;
+ debug = copyFrom.debug;
+ properties = copyFrom.properties;
}
/**
@@ -195,8 +126,7 @@ public abstract class Context implements MetaProvider {
* @param builder The builder for this class.
*/
protected Context(ContextBuilder builder) {
- ContextProperties cp = builder.getContextProperties();
- debug = cp.getBoolean(CONTEXT_debug).orElse(builder.debug);
+ debug = builder.debug;
identityCode = System.identityHashCode(this);
properties = builder.getContextProperties();
@@ -595,7 +525,7 @@ public abstract class Context implements MetaProvider {
/**
* Debug mode.
*
- * @see #CONTEXT_debug
+ * @see ContextBuilder#debug()
* @return
* <jk>true</jk> if debug mode is enabled.
*/
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ContextBuilder.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ContextBuilder.java
index bb5962d..7d7ab27 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ContextBuilder.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ContextBuilder.java
@@ -666,15 +666,25 @@ public abstract class ContextBuilder {
* <ul class='seealso'>
* <li class='ja'>{@link
org.apache.juneau.annotation.BeanConfig#debug()}
* <li class='jm'>{@link
org.apache.juneau.SessionArgs#debug(Boolean)}
- * <li class='jf'>{@link Context#CONTEXT_debug}
* </ul>
*
* @return This object (for method chaining).
*/
@FluentSetter
public ContextBuilder debug() {
- debug = true;
- return set(CONTEXT_debug);
+ return debug(true);
+ }
+
+ /**
+ * Same as {@link #debug()} but allows you to explicitly specify the
value.
+ *
+ * @param value The value for this setting.
+ * @return This object.
+ */
+ @FluentSetter
+ public ContextBuilder debug(boolean value) {
+ debug = value;
+ return this;
}
/**
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/Session.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/Session.java
index 2765723..4b0b949 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/Session.java
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/Session.java
@@ -13,7 +13,7 @@
package org.apache.juneau;
import static org.apache.juneau.internal.StringUtils.*;
-import static org.apache.juneau.Context.*;
+import static java.util.Optional.*;
import java.text.*;
import java.util.*;
@@ -53,7 +53,7 @@ public abstract class Session {
if (args.unmodifiable)
sp = sp.unmodifiable();
properties = sp;
- debug = sp.get(CONTEXT_debug,
Boolean.class).orElse(ctx.isDebug());
+ debug = ofNullable(args.debug).orElse(ctx.isDebug());
}
/**
@@ -170,7 +170,7 @@ public abstract class Session {
/**
* Configuration property: Debug mode.
*
- * @see BeanContext#CONTEXT_debug
+ * @see ContextBuilder#debug()
* @return
* <jk>true</jk> if debug mode is enabled.
*/
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/SessionArgs.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/SessionArgs.java
index d940e57..4a3a2e6 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/SessionArgs.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/SessionArgs.java
@@ -12,8 +12,6 @@
//
***************************************************************************************************************************
package org.apache.juneau;
-import static org.apache.juneau.Context.*;
-
import java.util.*;
import org.apache.juneau.collections.*;
@@ -29,6 +27,7 @@ public class SessionArgs {
SessionProperties properties = SessionProperties.create();
boolean unmodifiable;
+ Boolean debug;
/**
* Constructor.
@@ -49,13 +48,12 @@ public class SessionArgs {
* </ul>
*
* <p>
- * If not specified, defaults to {@link Context#CONTEXT_debug}.
+ * If not specified, defaults to {@link ContextBuilder#debug()}.
*
* <ul class='seealso'>
* <li class='ja'>{@link
org.apache.juneau.annotation.BeanConfig#debug()}
* <li class='jm'>{@link org.apache.juneau.ContextBuilder#debug()}
- * <li class='jf'>{@link Context#CONTEXT_debug}
- * </ul>
+ * <li class='jm'>{@link ContextBuilder#debug()}
*
* @param value
* The new value for this property.
@@ -64,7 +62,7 @@ public class SessionArgs {
*/
@FluentSetter
public SessionArgs debug(Boolean value) {
- property(CONTEXT_debug, value);
+ debug = value;
return this;
}
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/annotation/BeanConfig.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/annotation/BeanConfig.java
index ee9bb74..695be76 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/annotation/BeanConfig.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/annotation/BeanConfig.java
@@ -351,7 +351,7 @@ public @interface BeanConfig {
* <ul class='seealso'>
* <li class='jm'>{@link ContextBuilder#debug()}
* <li class='jm'>{@link SessionArgs#debug(Boolean)}
- * <li class='jf'>{@link Context#CONTEXT_debug}
+ * <li class='jm'>{@link ContextBuilder#debug()}
* </ul>
*/
String debug() default "";
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/annotation/BeanConfigAnnotation.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/annotation/BeanConfigAnnotation.java
index f6a6492..7efab12 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/annotation/BeanConfigAnnotation.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/annotation/BeanConfigAnnotation.java
@@ -12,7 +12,6 @@
//
***************************************************************************************************************************
package org.apache.juneau.annotation;
-import static org.apache.juneau.BeanContext.*;
import static java.util.Arrays.*;
import java.util.*;
@@ -54,7 +53,7 @@ public class BeanConfigAnnotation {
bool(a.beansRequireSerializable()).ifPresent(x ->
b.beansRequireSerializable(x));
bool(a.beansRequireSettersForGetters()).ifPresent(x ->
b.beansRequireSettersForGetters(x));
bool(a.disableBeansRequireSomeProperties()).ifPresent(x
-> b.disableBeansRequireSomeProperties(x));
- bool(a.debug()).ifPresent(x -> b.set(CONTEXT_debug, x));
+ bool(a.debug()).ifPresent(x -> b.debug(x));
bool(a.findFluentSetters()).ifPresent(x ->
b.findFluentSetters(x));
bool(a.ignoreInvocationExceptionsOnGetters()).ifPresent(x ->
b.ignoreInvocationExceptionsOnGetters(x));
bool(a.ignoreInvocationExceptionsOnSetters()).ifPresent(x ->
b.ignoreInvocationExceptionsOnSetters(x));
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/parser/ParserPipe.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/parser/ParserPipe.java
index 9777bfa..9a2cb02 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/parser/ParserPipe.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/parser/ParserPipe.java
@@ -284,7 +284,7 @@ public final class ParserPipe implements Closeable {
* Returns the input to this parser as a plain string.
*
* <p>
- * This method only returns a value if {@link Context#CONTEXT_debug} is
enabled.
+ * This method only returns a value if {@link ContextBuilder#debug()}
is enabled.
*
* @return The input as a string, or <jk>null</jk> if debug mode not
enabled.
*/
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/parser/ParserSession.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/parser/ParserSession.java
index d30c486..b71f6c5 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/parser/ParserSession.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/parser/ParserSession.java
@@ -893,7 +893,7 @@ public abstract class ParserSession extends BeanSession {
*
* <p>
* This always returns a value for input of type {@link CharSequence}.
- * <br>For other input types, use {@link Context#CONTEXT_debug} setting
to enable caching to a string
+ * <br>For other input types, use {@link ContextBuilder#debug()}
setting to enable caching to a string
* before parsing so that this method returns the input.
*
* @return The input as a string, or <jk>null</jk> if no pipe has been
created or we're reading from an uncached reader or input stream source.
diff --git
a/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestClient.java
b/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestClient.java
index 8e04b4a..e1e3a99 100644
---
a/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestClient.java
+++
b/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestClient.java
@@ -880,7 +880,7 @@ import org.apache.juneau.utils.*;
* <p class='w900'>
* Enabling debug mode has the following effects:
* <ul>
- * <li>{@link Context#CONTEXT_debug} is enabled.
+ * <li>{@link ContextBuilder#debug()} is enabled.
* <li>{@link RestClientBuilder#detectLeaks()} is enabled.
* <li>{@link RestClientBuilder#logToConsole()} is called.
* </ul>
diff --git
a/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestClientBuilder.java
b/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestClientBuilder.java
index 31bca5e..7f312d2 100644
---
a/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestClientBuilder.java
+++
b/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestClientBuilder.java
@@ -1882,6 +1882,7 @@ public class RestClientBuilder extends
BeanContextableBuilder {
@FluentSetter
public RestClientBuilder debug() {
super.debug();
+ serializers().forEach(x -> x.debug());
return headers(Debug.TRUE);
}
@@ -3207,7 +3208,7 @@ public class RestClientBuilder extends
BeanContextableBuilder {
* when the <c>finalize</c> methods are invoked.
*
* <p>
- * Automatically enabled with {@link Context#CONTEXT_debug}.
+ * Automatically enabled with {@link ContextBuilder#debug()}.
*
* <h5 class='section'>Example:</h5>
* <p class='bcode w800'>
diff --git a/juneau-utest/src/test/java/org/apache/juneau/ContextCacheTest.java
b/juneau-utest/src/test/java/org/apache/juneau/ContextCacheTest.java
deleted file mode 100644
index 29d0567..0000000
--- a/juneau-utest/src/test/java/org/apache/juneau/ContextCacheTest.java
+++ /dev/null
@@ -1,209 +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;
-
-import static org.apache.juneau.assertions.Assertions.*;
-import static org.junit.Assert.*;
-import static org.junit.runners.MethodSorters.*;
-
-import org.apache.juneau.annotation.*;
-import org.apache.juneau.collections.*;
-import org.junit.*;
-
-/**
- * Tests the ContextCache class.
- */
-@FixMethodOrder(NAME_ASCENDING)
-public class ContextCacheTest {
-
-
//-------------------------------------------------------------------------------------------------------------------
- // Basic tests
-
//-------------------------------------------------------------------------------------------------------------------
-
- @Test
- public void testBasic() {
-
- ContextPropertiesBuilder cpb = ContextProperties.create();
- ContextProperties cp = cpb.build();
-
- A a = ContextCache.INSTANCE.create(A.class, cp);
- B b = ContextCache.INSTANCE.create(B.class, cp);
- C c = ContextCache.INSTANCE.create(C.class, cp);
-
- assertObject(a).asJson().is("{f1:'xxx'}");
- assertObject(b).asJson().is("{f1:'xxx',f2:-1}");
- assertObject(c).asJson().is("{f1:'xxx',f2:-1,f3:false}");
-
- A a2 = ContextCache.INSTANCE.create(A.class, cp);
- B b2 = ContextCache.INSTANCE.create(B.class, cp);
- C c2 = ContextCache.INSTANCE.create(C.class, cp);
-
- assertTrue(a == a2);
- assertTrue(b == b2);
- assertTrue(c == c2);
-
- cpb.set("A.f1", "foo");
- cp = cpb.build();
-
- a2 = ContextCache.INSTANCE.create(A.class, cp);
- b2 = ContextCache.INSTANCE.create(B.class, cp);
- c2 = ContextCache.INSTANCE.create(C.class, cp);
-
- assertObject(a2).asJson().is("{f1:'foo'}");
- assertObject(b2).asJson().is("{f1:'foo',f2:-1}");
- assertObject(c2).asJson().is("{f1:'foo',f2:-1,f3:false}");
-
- assertTrue(a != a2);
- assertTrue(b != b2);
- assertTrue(c != c2);
-
- a = a2; b = b2; c = c2;
-
- cp = cpb.set("B.f2.i", 123).build();
-
- a2 = ContextCache.INSTANCE.create(A.class, cp);
- b2 = ContextCache.INSTANCE.create(B.class, cp);
- c2 = ContextCache.INSTANCE.create(C.class, cp);
-
- assertObject(a2).asJson().is("{f1:'foo'}");
- assertObject(b2).asJson().is("{f1:'foo',f2:123}");
- assertObject(c2).asJson().is("{f1:'foo',f2:123,f3:false}");
-
- assertTrue(a == a2);
- assertTrue(b != b2);
- assertTrue(c != c2);
-
- a = a2; b = b2; c = c2;
-
- cp = cpb.set("C.f3.b").build();
-
- a2 = ContextCache.INSTANCE.create(A.class, cp);
- b2 = ContextCache.INSTANCE.create(B.class, cp);
- c2 = ContextCache.INSTANCE.create(C.class, cp);
-
- assertObject(a2).asJson().is("{f1:'foo'}");
- assertObject(b2).asJson().is("{f1:'foo',f2:123}");
- assertObject(c2).asJson().is("{f1:'foo',f2:123,f3:true}");
-
- assertTrue(a == a2);
- assertTrue(b == b2);
- assertTrue(c != c2);
-
- a = a2; b = b2; c = c2;
-
- cp = cpb.set("D.bad.o", "xxx").build();
-
- a2 = ContextCache.INSTANCE.create(A.class, cp);
- b2 = ContextCache.INSTANCE.create(B.class, cp);
- c2 = ContextCache.INSTANCE.create(C.class, cp);
-
- assertObject(a2).asJson().is("{f1:'foo'}");
- assertObject(b2).asJson().is("{f1:'foo',f2:123}");
- assertObject(c2).asJson().is("{f1:'foo',f2:123,f3:true}");
-
- assertTrue(a == a2);
- assertTrue(b == b2);
- assertTrue(c == c2);
-
- assertTrue(a.getContextProperties() ==
a2.getContextProperties());
- assertTrue(b.getContextProperties() ==
b2.getContextProperties());
- assertTrue(c.getContextProperties() ==
c2.getContextProperties());
-
- a2 = ContextCache.INSTANCE.create(A.class,
a.getContextProperties().copy().set("A.f1", "foo").build());
- assertTrue(a == a2);
-
- a2 = ContextCache.INSTANCE.create(A.class,
a.getContextProperties().copy().set("A.f1", "bar").build());
- assertTrue(a != a2);
- }
-
- @ConfigurableContext
- public static class A extends Context {
- public final String f1;
-
- public A(ContextProperties cp) {
- super(cp, true);
- f1 =
getContextProperties().getString("A.f1").orElse("xxx");
- }
-
- @Override
- public Session createSession(SessionArgs args) {
- return null;
- }
-
- @Override
- public SessionArgs createDefaultSessionArgs() {
- return null;
- }
-
- @Override
- public OMap toMap() {
- return OMap.of("f1", f1);
- }
-
- @Override
- public ContextBuilder copy() {
- return null;
- }
- }
-
- @ConfigurableContext
- public static class B extends A {
- public int f2;
-
- public B(ContextProperties cp) {
- super(cp);
- f2 =
getContextProperties().getInteger("B.f2.i").orElse(-1);
-
- }
-
- @Override
- public OMap toMap() {
- return super.toMap().a("f2", f2);
- }
- }
-
- @ConfigurableContext
- public static class C extends B {
- public boolean f3;
- public C(ContextProperties cp) {
- super(cp);
- f3 =
getContextProperties().getBoolean("C.f3.b").orElse(false);
- }
-
- @Override
- public OMap toMap() {
- return super.toMap().a("f3", f3);
- }
- }
-
- @Test
- public void testBadConstructor() {
- ContextPropertiesBuilder cpb = ContextProperties.create();
- ContextProperties cp = cpb.build();
- assertThrown(()->ContextCache.INSTANCE.create(D1.class,
cp)).message().is("Could not create instance of class
'org.apache.juneau.ContextCacheTest$D1'");
- assertThrown(()->ContextCache.INSTANCE.create(D2.class,
cp)).message().is("Could not create instance of class
'org.apache.juneau.ContextCacheTest$D2'");
- }
-
- public static class D1 extends A {
- protected D1(ContextProperties cp) {
- super(cp);
- }
- }
-
- public static class D2 extends A {
- public D2(ContextProperties cp) {
- super(cp);
- throw new RuntimeException("Error!");
- }
- }
-}