Issue #23507 Enabled some context-propagation tests - resolved cyclic dependencies and side effects (at least partially, so now tests pass in maven
diff --git a/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/adaptors/BootstrapUtils.java b/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/adaptors/BootstrapUtils.java index fa2de04..08317e2 100644 --- a/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/adaptors/BootstrapUtils.java +++ b/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/adaptors/BootstrapUtils.java
@@ -91,11 +91,6 @@ public static void bootstrap(WireAdapter wireAdapter) { reset(); - /* - * ThreadLocalAccessor tla = Deencapsulation.getField(ContextBootstrap.class, - * "threadLocalAccessor"); - * tla.set(null); - */ ContextBootstrap.configure(new MockLoggerAdapter(), wireAdapter, new MockThreadLocalAccessor(), new MockContextAccessController(), "guid"); }
diff --git a/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/internal/AccessControlledMapFinderTest.java b/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/internal/AccessControlledMapFinderTest.java index 4e62b05..40b5c69 100644 --- a/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/internal/AccessControlledMapFinderTest.java +++ b/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/internal/AccessControlledMapFinderTest.java
@@ -20,9 +20,9 @@ import org.glassfish.contextpropagation.adaptors.BootstrapUtils; import org.glassfish.contextpropagation.internal.Utils.AccessControlledMapFinder; import org.glassfish.contextpropagation.wireadapters.glassfish.DefaultWireAdapter; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; - import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; @@ -34,26 +34,28 @@ @BeforeEach public void setup() { BootstrapUtils.bootstrap(new DefaultWireAdapter()); - mapFinder.getMapAndCreateIfNeeded(); } + @AfterEach + public void reset() { + BootstrapUtils.reset(); + } @Test public void testGetMapIfItExistsButDoesnt() { - BootstrapUtils.bootstrap(new DefaultWireAdapter()); assertNull(mapFinder.getMapIfItExists()); } @Test public void testGetMapIfItExistsWhenItDoes() { + mapFinder.getMapAndCreateIfNeeded(); assertNotNull(mapFinder.getMapIfItExists()); } @Test public void testCreateMapIfItExistsButDoesnt() { - BootstrapUtils.bootstrap(new DefaultWireAdapter()); assertNull(mapFinder.getMapIfItExists()); assertNotNull(mapFinder.getMapAndCreateIfNeeded()); } @@ -61,6 +63,7 @@ @Test public void testCreateMapIfItExistsWhenItDoes() { + assertNotNull(mapFinder.getMapAndCreateIfNeeded()); assertEquals(mapFinder.getMapIfItExists(), mapFinder.getMapAndCreateIfNeeded()); }
diff --git a/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/internal/AccessControlledMapTest.java b/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/internal/AccessControlledMapTest.java index 4fc6a2f..a9479db 100644 --- a/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/internal/AccessControlledMapTest.java +++ b/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/internal/AccessControlledMapTest.java
@@ -30,6 +30,7 @@ import org.glassfish.contextpropagation.internal.AccessControlledMap.ContextAccessLevel; import org.glassfish.contextpropagation.internal.Entry.ContextType; import org.glassfish.contextpropagation.wireadapters.glassfish.DefaultWireAdapter; +import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -53,38 +54,16 @@ @BeforeAll public static void setupClass() { + // must be called here too, because isConfigured is initialized in static block BootstrapUtils.reset(); ContextBootstrap.configure(new MockLoggerAdapter(), new DefaultWireAdapter(), new MockThreadLocalAccessor(), - new ContextAccessController() { - - @Override - public boolean isAccessAllowed(String key, ContextAccessLevel type) { - switch (type) { - case READ: - return key.contains("read") || key.contains("create") || key.contains("delete") - || key.contains("update"); - case CREATE: - return key.contains("create"); - case DELETE: - return key.contains("delete"); - case UPDATE: - return key.contains("update"); - } - return false; - } - - - @Override - public boolean isEveryoneAllowedToRead(String key) { - if ("put".equals(Thread.currentThread().getStackTrace()[2].getMethodName())) { - return true; - } else { - throw new UnsupportedOperationException(); - } - } - }, "guid"); + new ContextAccessController4ACMTest(), "guid"); } + @AfterAll + public static void reset() { + BootstrapUtils.reset(); + } @BeforeEach public void setup() { @@ -230,4 +209,32 @@ assertEquals(4, count); } + + private static final class ContextAccessController4ACMTest extends ContextAccessController { + + @Override + public boolean isAccessAllowed(String key, ContextAccessLevel type) { + switch (type) { + case READ: + return key.contains("read") || key.contains("create") || key.contains("delete") + || key.contains("update"); + case CREATE: + return key.contains("create"); + case DELETE: + return key.contains("delete"); + case UPDATE: + return key.contains("update"); + } + return false; + } + + @Override + public boolean isEveryoneAllowedToRead(String key) { + if ("put".equals(Thread.currentThread().getStackTrace()[2].getMethodName())) { + return true; + } else { + throw new UnsupportedOperationException(); + } + } + } }
diff --git a/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/internal/ContextMapImplTest.java b/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/internal/ContextMapImplTest.java index 4200a9b..b3d4fc4 100644 --- a/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/internal/ContextMapImplTest.java +++ b/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/internal/ContextMapImplTest.java
@@ -33,10 +33,12 @@ import org.glassfish.contextpropagation.internal.Utils.AccessControlledMapFinder; import org.glassfish.contextpropagation.spi.ContextMapHelper; import org.glassfish.contextpropagation.wireadapters.glassfish.DefaultWireAdapter; +import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import static org.glassfish.tests.utils.Utils.getStaticField; import static org.glassfish.tests.utils.Utils.setStaticField; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; @@ -49,47 +51,57 @@ public class ContextMapImplTest { - private static Entry DUMMY_ENTRY; - private static EnumSet<PropagationMode> PROP_MODES = PropagationMode.defaultSet(); - private static ContextMap cm; - private static AccessControlledMap acMap; - private static AccessControlledMap savedMap; - static AccessControlledMapFinder mapFinder = new AccessControlledMapFinder() { + private static final Entry DUMMY_ENTRY + = new Entry("dummy", PropagationMode.defaultSet(), ContextType.STRING).init(true, true); + private static final EnumSet<PropagationMode> PROP_MODES = PropagationMode.defaultSet(); + private static ContextMap contextMap; + private static AccessControlledMap acMapForFinder; + private static AccessControlledMap acMapCustom; + private static AccessControlledMapFinder originalFinder; + private static AccessControlledMapFinder mapFinder = new AccessControlledMapFinder() { @Override protected AccessControlledMap getMapIfItExists() { AccessControlledMap map = super.getMapIfItExists(); - return map == null ? acMap : map; + return map == null ? acMapForFinder : map; } }; @BeforeAll public static void setupClass() { BootstrapUtils.bootstrap(new DefaultWireAdapter()); - DUMMY_ENTRY = new Entry("dummy", PropagationMode.defaultSet(), ContextType.STRING).init(true, true); - savedMap = new AccessControlledMap(); + originalFinder = getStaticField(Utils.class, "mapFinder"); setStaticField(Utils.class, "mapFinder", mapFinder); - cm = Utils.getScopeAwareContextMap(); - savedMap.simpleMap.put("key", DUMMY_ENTRY); - savedMap.simpleMap.put("removeMe", DUMMY_ENTRY); + + contextMap = Utils.getScopeAwareContextMap(); + + acMapCustom = new AccessControlledMap(); + acMapCustom.simpleMap.put("key", DUMMY_ENTRY); + acMapCustom.simpleMap.put("removeMe", DUMMY_ENTRY); + Entry entry = new Entry(new Location(new ViewImpl(Location.KEY)) { }, PropagationMode.defaultSet(), ContextType.VIEW_CAPABLE).init(true, true); - savedMap.simpleMap.put(Location.KEY, entry); + acMapCustom.simpleMap.put(Location.KEY, entry); } @BeforeEach public void setup() { BootstrapUtils.bootstrap(new DefaultWireAdapter()); - acMap = null; + acMapForFinder = null; } + @AfterAll + public static void reset() { + BootstrapUtils.reset(); + setStaticField(Utils.class, "mapFinder", originalFinder); + } @Test public void testGet() throws InsufficientCredentialException { - assertNull(cm.get("key")); - acMap = savedMap; - assertEquals("dummy", cm.get("key")); + assertNull(contextMap.get("key")); + acMapForFinder = acMapCustom; + assertEquals("dummy", contextMap.get("key")); } @@ -97,9 +109,9 @@ public void testPutString() throws InsufficientCredentialException { String key = "a String"; String origContext = "string"; - cm.put(key, origContext, PROP_MODES); + contextMap.put(key, origContext, PROP_MODES); checkPut(key, origContext); - assertEquals(origContext, cm.put(key, "new string", PROP_MODES)); + assertEquals(origContext, contextMap.put(key, "new string", PROP_MODES)); } @@ -114,9 +126,9 @@ public void testPutNumber() throws InsufficientCredentialException { String key = "a long"; long origContext = 1L; - cm.put(key, origContext, PROP_MODES); + contextMap.put(key, origContext, PROP_MODES); checkPut(key, origContext); - assertThat(cm.put(key, 2L, PROP_MODES), equalTo(origContext)); + assertThat(contextMap.put(key, 2L, PROP_MODES), equalTo(origContext)); } @@ -124,15 +136,15 @@ public void testPutBoolean() throws InsufficientCredentialException { String key = "a boolean"; boolean origContext = true; - cm.put(key, origContext, PROP_MODES); + contextMap.put(key, origContext, PROP_MODES); checkPut(key, origContext); - assertThat(cm.put(key, false, PROP_MODES), equalTo(origContext)); + assertThat(contextMap.put(key, false, PROP_MODES), equalTo(origContext)); } @Test public void testCreateViewCapable() throws InsufficientCredentialException { - acMap = savedMap; + acMapForFinder = acMapCustom; String prefix = "a view capable"; ContextMapHelper.registerContextFactoryForPrefixNamed(prefix, new ContextViewFactory() { @@ -148,23 +160,23 @@ /* dummy instance */}; } }); - assertNotNull(cm.createViewCapable(prefix)); + assertNotNull(contextMap.createViewCapable(prefix)); } @Test public void testGetPropagationModes() throws InsufficientCredentialException { - assertNull(cm.getPropagationModes("key")); - acMap = savedMap; - assertEquals(PropagationMode.defaultSet(), cm.getPropagationModes("key")); + assertNull(contextMap.getPropagationModes("key")); + acMapForFinder = acMapCustom; + assertEquals(PropagationMode.defaultSet(), contextMap.getPropagationModes("key")); } @Test public void testRemove() throws InsufficientCredentialException { - acMap = savedMap; - assertNull(cm.remove("nonexistent")); - assertNotNull(cm.remove("removeMe")); + acMapForFinder = acMapCustom; + assertNull(contextMap.remove("nonexistent")); + assertNotNull(contextMap.remove("removeMe")); } @@ -172,36 +184,36 @@ public void testPutCharacter() throws InsufficientCredentialException { String key = "a Character"; char origContext = 'c'; - cm.put(key, origContext, PROP_MODES); + contextMap.put(key, origContext, PROP_MODES); checkPut(key, origContext); - assertThat(cm.put(key, 'd', PROP_MODES), equalTo(origContext)); + assertThat(contextMap.put(key, 'd', PROP_MODES), equalTo(origContext)); } @Test public void testGetLocationNormalCase() { - acMap = savedMap; - Location location = cm.getLocation(); + acMapForFinder = acMapCustom; + Location location = contextMap.getLocation(); assertNotNull(location); } @Test public void testIsEmpty() { - assertTrue(cm.isEmpty()); - acMap = new AccessControlledMap(); - assertTrue(cm.isEmpty()); - acMap = savedMap; - assertFalse(cm.isEmpty()); + assertTrue(contextMap.isEmpty()); + acMapForFinder = new AccessControlledMap(); + assertTrue(contextMap.isEmpty()); + acMapForFinder = acMapCustom; + assertFalse(contextMap.isEmpty()); } @Test public void testNames() { - Iterator<?> iter = cm.names(); + Iterator<?> iter = contextMap.names(); assertNull(iter); - acMap = savedMap; - iter = cm.names(); + acMapForFinder = acMapCustom; + iter = contextMap.names(); while (iter.hasNext()) { MockLoggerAdapter.debug((String) iter.next()); }
diff --git a/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/internal/ContextMapPropagatorTest.java b/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/internal/ContextMapPropagatorTest.java index 82bc170..a9acc0f 100644 --- a/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/internal/ContextMapPropagatorTest.java +++ b/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/internal/ContextMapPropagatorTest.java
@@ -32,6 +32,7 @@ import org.glassfish.contextpropagation.internal.Utils.AccessControlledMapFinder; import org.glassfish.contextpropagation.spi.ContextMapPropagator; import org.glassfish.contextpropagation.wireadapters.WireAdapter; +import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; @@ -90,6 +91,10 @@ soapEntry = simpleMap.getEntry("soap"); } + @AfterAll + public static void reset() { + BootstrapUtils.reset(); + } @Test public void testSendRequest() throws Exception {
diff --git a/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/internal/SimpleMapTest.java b/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/internal/SimpleMapTest.java index dde6656..e3a7d8f 100644 --- a/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/internal/SimpleMapTest.java +++ b/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/internal/SimpleMapTest.java
@@ -36,6 +36,7 @@ import org.glassfish.contextpropagation.internal.Entry.ContextType; import org.glassfish.contextpropagation.internal.SimpleMap.Filter; import org.glassfish.contextpropagation.wireadapters.glassfish.DefaultWireAdapter; +import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -110,13 +111,19 @@ @BeforeAll - public static void setupClass() { - logger = new RecordingLoggerAdapter(); + public static void bootstrap() { + // must be called here too, because isConfigured is initialized in static block BootstrapUtils.reset(); + logger = new RecordingLoggerAdapter(); ContextBootstrap.configure(logger, new DefaultWireAdapter(), new MockThreadLocalAccessor(), new MockContextAccessController(), "guid"); } + @AfterAll + public static void reset() { + BootstrapUtils.reset(); + } + @BeforeEach public void setup() {
diff --git a/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/internal/UtilsTest.java b/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/internal/UtilsTest.java index 72a553d..1845773 100644 --- a/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/internal/UtilsTest.java +++ b/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/internal/UtilsTest.java
@@ -32,6 +32,7 @@ import org.glassfish.contextpropagation.bootstrap.LoggerAdapter.Level; import org.glassfish.contextpropagation.bootstrap.LoggerAdapter.MessageID; import org.glassfish.contextpropagation.wireadapters.glassfish.DefaultWireAdapter; +import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -42,10 +43,15 @@ public class UtilsTest { @BeforeAll - public static void setupClass() { + public static void bootstrap() { BootstrapUtils.bootstrap(new DefaultWireAdapter()); } + @AfterAll + public static void reset() { + BootstrapUtils.reset(); + } + @Test public void testGetScopeAwarePropagator() {
diff --git a/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/internal/ViewImplTest.java b/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/internal/ViewImplTest.java index 05a73ba..bb0d6db 100644 --- a/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/internal/ViewImplTest.java +++ b/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/internal/ViewImplTest.java
@@ -29,8 +29,8 @@ public class ViewImplTest { - static ViewImpl view; - static SimpleMap sm; + private static ViewImpl view; + private static SimpleMap sm; @BeforeAll public static void setupClass() {
diff --git a/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/weblogic/workarea/PropagationTest.java b/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/weblogic/workarea/PropagationTest.java index 14d48dd..8d6bc8d 100644 --- a/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/weblogic/workarea/PropagationTest.java +++ b/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/weblogic/workarea/PropagationTest.java
@@ -17,95 +17,99 @@ package org.glassfish.contextpropagation.weblogic.workarea; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectInput; +import java.io.ObjectInputStream; +import java.io.ObjectOutput; +import java.io.ObjectOutputStream; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +import org.glassfish.contextpropagation.adaptors.MockLoggerAdapter; +import org.glassfish.contextpropagation.adaptors.TestableThread; +import org.glassfish.contextpropagation.internal.Utils; +import org.glassfish.contextpropagation.weblogic.workarea.spi.WorkContextMapInterceptor; +import org.glassfish.contextpropagation.weblogic.workarea.utils.WorkContextInputAdapter; +import org.glassfish.contextpropagation.weblogic.workarea.utils.WorkContextOutputAdapter; +import org.glassfish.contextpropagation.wireadapters.wls.MySerializable; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + public class PropagationTest { -// public static WorkContextMap wcMap; -// -// @BeforeClass -// public static void setup() throws PropertyReadOnlyException, IOException { -// wcMap = WorkContextHelper.getWorkContextHelper().getWorkContextMap(); -// wcMap.put("long", PrimitiveContextFactory.create(1L)); -// wcMap.put("string", PrimitiveContextFactory.create("string")); -// wcMap.put("ascii", PrimitiveContextFactory.createASCII("ascii")); -// wcMap.put("serializable", PrimitiveContextFactory.createMutable(new MySerializable())); -// wcMap.put("workcontext", new MyWorkContext()); -// } -// -// @Test -// public void testRequestPropagation() throws IOException, InterruptedException { -// final byte[] bytes = serialize(); -// //MockLoggerAdaper.debug(Utils.toString(bytes)); -// new TestableThread() { -// @Override public void runTest() throws Exception { -// final WorkContextMap map = WorkContextHelper.getWorkContextHelper().getWorkContextMap(); -// assertTrue(map.isEmpty()); -// deserialize(bytes); -// Set<?> expectedSet = map2Set(wcMap); -// Set<?> resultSet = map2Set(map); -// assertEquals(expectedSet, resultSet); -// } -// -// @SuppressWarnings("serial") -// private HashSet<?> map2Set(final WorkContextMap map) { -// return new HashSet<Object>() {{ -// Iterator<?> it = map.iterator(); -// while (it.hasNext()) { -// add(it.next()); -// } -// }}; -// } -// }.startJoinAndCheckForFailures(); -// MockLoggerAdapter.debug(Utils.toString(bytes)); -// } -// -// @Ignore("there seems to be a problem with jmockit") -// @Test public void fromGlassfish() throws InsufficientCredentialException, IOException, InterruptedException { -// WorkContextMapInterceptor interceptor = WorkContextHelper.getWorkContextHelper().createInterceptor(); -// //byte[] gBytes = getFirstBytes(); -// byte[] bytes = WLSWireAdapterTest.toWLSBytes(); -// //MockLoggerAdaper.debug(">>>" + Utils.toString(gBytes) + "<<<"); -// //MockLoggerAdaper.debug(">>>" + Utils.toString(bytes) + "<<<"); -// ByteArrayInputStream bais = new ByteArrayInputStream(bytes); -// WorkContextInput wci = new WorkContextInputAdapter(new ObjectInputStream(bais)); -// interceptor.receiveRequest(wci); -// WorkContextMap map = (WorkContextMap) interceptor; -// @SuppressWarnings("unchecked") -// Iterator<String> keys = map.keys(); -// while(keys.hasNext()) { -// String key = keys.next(); -// MockLoggerAdapter.debug(key + ": " + map.get(key)); -// } -// assertEquals("ascii", ((PrimitiveWorkContext) map.get("ascii")).get()); -// assertEquals("string", ((PrimitiveWorkContext)map.get("string")).get()); -// assertEquals(1L, ((PrimitiveWorkContext)map.get("one")).get()); -// assertEquals(MyWorkContext.class, map.get("workcontext").getClass()); -// SerializableWorkContext swc = (SerializableWorkContext) map.get("serializable"); -// MockLoggerAdapter.debug("Serializable contents: " + swc.get().getClass() + swc.get()); -// Set<?> set = (Set<?>) swc.get(); -// assertEquals(1, set.size()); -// } -// -// public static byte[] serialize() throws IOException { -// WorkContextMapInterceptor wcInterceptor = WorkContextHelper.getWorkContextHelper().getLocalInterceptor(); -// ByteArrayOutputStream baos = new ByteArrayOutputStream(); -// ObjectOutput oo = new ObjectOutputStream(baos); -// WorkContextOutput wco = new WorkContextOutputAdapter(oo); -// wcInterceptor.sendRequest(wco, PropagationMode.RMI); -// oo.flush(); -// return baos.toByteArray(); -// } -// -// public static void deserialize(byte[] bytes) { -// try { -// //WorkContextHelper.getWorkContextHelper(). -// WorkContextMapInterceptor wcInterceptor = WorkContextHelper.getWorkContextHelper().getInterceptor(); -// ByteArrayInputStream bais = new ByteArrayInputStream(bytes); -// ObjectInput oi = new ObjectInputStream(bais); -// WorkContextInput wci = new WorkContextInputAdapter(oi); -// wcInterceptor.receiveRequest(wci); -// } catch (IOException ioe) { -// throw new RuntimeException(ioe); -// } -// } + + private static WorkContextMap wcMap; + + @BeforeAll + public static void setup() throws Exception { + wcMap = WorkContextHelper.getWorkContextHelper().getWorkContextMap(); + wcMap.put("long", PrimitiveContextFactory.create(1L)); + wcMap.put("string", PrimitiveContextFactory.create("string")); + wcMap.put("ascii", PrimitiveContextFactory.createASCII("ascii")); + wcMap.put("serializable", PrimitiveContextFactory.createMutable(new MySerializable())); + wcMap.put("workcontext", new MyWorkContext()); + } + @Test + public void testRequestPropagation() throws IOException, InterruptedException { + final byte[] bytes = serialize(); + // MockLoggerAdaper.debug(Utils.toString(bytes)); + new TestableThread() { + + @Override + public void runTest() throws Exception { + final WorkContextMap map = WorkContextHelper.getWorkContextHelper().getWorkContextMap(); + assertTrue(map.isEmpty()); + deserialize(bytes); + Set<?> expectedSet = map2Set(wcMap); + Set<?> resultSet = map2Set(map); + assertEquals(expectedSet, resultSet); + } + + + @SuppressWarnings("serial") + private HashSet<?> map2Set(final WorkContextMap map) { + return new HashSet<>() { + + { + Iterator<?> it = map.iterator(); + while (it.hasNext()) { + add(it.next()); + } + } + }; + } + }.startJoinAndCheckForFailures(); + MockLoggerAdapter.debug(Utils.toString(bytes)); + } + + + public static byte[] serialize() throws IOException { + WorkContextMapInterceptor wcInterceptor = WorkContextHelper.getWorkContextHelper().getLocalInterceptor(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutput oo = new ObjectOutputStream(baos); + WorkContextOutput wco = new WorkContextOutputAdapter(oo); + wcInterceptor.sendRequest(wco, PropagationMode.RMI); + oo.flush(); + return baos.toByteArray(); + } + + + private static void deserialize(byte[] bytes) { + try { + WorkContextMapInterceptor wcInterceptor = WorkContextHelper.getWorkContextHelper().getInterceptor(); + ByteArrayInputStream bais = new ByteArrayInputStream(bytes); + ObjectInput oi = new ObjectInputStream(bais); + WorkContextInput wci = new WorkContextInputAdapter(oi); + wcInterceptor.receiveRequest(wci); + } catch (IOException ioe) { + throw new IllegalStateException(ioe); + } + } }
diff --git a/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/wireadapters/CatalogTest.java b/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/wireadapters/CatalogTest.java index 3c2b6c3..b87a3f1 100644 --- a/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/wireadapters/CatalogTest.java +++ b/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/wireadapters/CatalogTest.java
@@ -23,28 +23,29 @@ public class CatalogTest { - @Test - public void testUpdateMeta() { - byte dot = '.'; - Catalog cat = new Catalog(); - cat.add((short) 5); - cat.add((short) 0x02FF); - byte[] bytes = "..xxxx....".getBytes(); - cat.updateCatalogMetadata(bytes); - assertEquals(dot, bytes[0]); - assertEquals(dot, bytes[1]); - assertEquals((byte) 0, bytes[2]); - assertEquals((byte) 5, bytes[3]); - assertEquals((byte) 2, bytes[4]); - assertEquals((byte) 0xFF, bytes[5]); - } + @Test + public void testUpdateMeta() { + byte dot = '.'; + Catalog cat = new Catalog(); + cat.add((short) 5); + cat.add((short) 0x02FF); + byte[] bytes = "..xxxx....".getBytes(); + cat.updateCatalogMetadata(bytes); + assertEquals(dot, bytes[0]); + assertEquals(dot, bytes[1]); + assertEquals((byte) 0, bytes[2]); + assertEquals((byte) 5, bytes[3]); + assertEquals((byte) 2, bytes[4]); + assertEquals((byte) 0xFF, bytes[5]); + } - @Test - public void testSetMeta() { - Catalog cat = new Catalog(); - cat.setMeta(0xABCD1234); - assertEquals((short) 0xABCD, cat.start); - assertEquals((short) 0x1234, cat.end); - } + + @Test + public void testSetMeta() { + Catalog cat = new Catalog(); + cat.setMeta(0xABCD1234); + assertEquals((short) 0xABCD, cat.start); + assertEquals((short) 0x1234, cat.end); + } }
diff --git a/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/wireadapters/PositionAwareObjectOutputStreamTest.java b/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/wireadapters/PositionAwareObjectOutputStreamTest.java index 8453208..a498544 100644 --- a/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/wireadapters/PositionAwareObjectOutputStreamTest.java +++ b/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/wireadapters/PositionAwareObjectOutputStreamTest.java
@@ -17,90 +17,129 @@ package org.glassfish.contextpropagation.wireadapters; -//import mockit.Deencapsulation; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectOutputStream; +import java.io.OutputStream; +import java.util.LinkedList; +import java.util.List; +import org.glassfish.contextpropagation.adaptors.MockLoggerAdapter; +import org.glassfish.contextpropagation.internal.Utils; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; public class PositionAwareObjectOutputStreamTest { -// -// @Test -// public void testSimple() throws IOException { -// Populateable p = new Populateable() { -// @Override public void populate(ObjectOutputStream oos, List<Short> positions) throws IOException { -// oos.writeLong(1); -// oos.writeObject("foo"); -// oos.writeLong(2); -// oos.writeObject("bar"); -// } -// }; -// assertArrayEquals(populate(ObjectOutputStream.class, p, null), -// populate(PositionAwareObjectOutputStream.class, p, null)); -// } -// -// @Test public void testPositions() throws IOException { -// LinkedList<Short> positions = new LinkedList<Short>(); -// Populateable p = new Populateable() { -// @Override public void populate(ObjectOutputStream oos, List<Short> positions) throws IOException { -// byte[] buf = "Some Bytes".getBytes(); -// addPosition(positions, oos); oos.writeObject("foo"); -// addPosition(positions, oos); oos.writeShort(1); -// addPosition(positions, oos); oos.writeLong(2); -// addPosition(positions, oos); oos.writeBoolean(true); -// addPosition(positions, oos); oos.writeDouble(2.1E5); -// addPosition(positions, oos); oos.writeFloat(2.1F); -// addPosition(positions, oos); oos.write(buf); -// addPosition(positions, oos); oos.writeByte((byte) 'b'); -// addPosition(positions, oos); oos.writeChar('c'); -// addPosition(positions, oos); oos.writeChars("chars"); -// addPosition(positions, oos); oos.writeInt(1); -// addPosition(positions, oos); oos.writeObject("bar"); -// addPosition(positions, oos); -// } -// }; -// byte[] bytes = populate(PositionAwareObjectOutputStream.class, p, positions); -// MockLoggerAdapter.debug("bytes size: " + bytes.length + ", positions: " + positions); -// byte [] expectedBytes = populate(ObjectOutputStream.class, p, null); -// MockLoggerAdapter.debug(Utils.toString(expectedBytes)); -// MockLoggerAdapter.debug(Utils.toString(bytes)); -// assertArrayEquals(expectedBytes, bytes); -// } -// -// -// private void addPosition(List<Short> positions, ObjectOutputStream oos) throws IOException { -// if (oos instanceof PositionAwareObjectOutputStream) { -// positions.add(((PositionAwareObjectOutputStream) oos).position()); -// } -// } -// -// private interface Populateable { -// void populate(ObjectOutputStream oos, List<Short> positions) throws IOException; -// } -// -// private <T extends ObjectOutputStream> byte[] populate(Class<T> clz, Populateable p, List<Short> positions) throws IOException { -// StreamWrapper<T> sw = new StreamWrapper<T>(clz); -// ObjectOutputStream oos = sw.getObjectOutputStream(); -// p.populate(oos, positions); -// return sw.getBytes(); -// } -// -// private static class StreamWrapper<T extends ObjectOutputStream> { -// ByteArrayOutputStream baos; -// ObjectOutputStream oos; -// -// private StreamWrapper(Class<T> clz) throws IOException { -// baos = new ByteArrayOutputStream(); -// //oos = clz == PositionAwareObjectOutputStream.class ? new PositionAwareObjectOutputStream(baos) : new ObjectOutputStream(baos); -// oos = Deencapsulation.newInstance(clz, baos); // Above approach produces the same results. Something strange with the instantiation of this stream. -// } -// -// private ObjectOutputStream getObjectOutputStream() { -// return oos; -// } -// -// private byte[] getBytes() throws IOException { -// oos.flush(); -// return baos.toByteArray(); -// } -// } + + @Test + public void testSimple() throws IOException { + Populateable p = new Populateable() { + + @Override + public void populate(ObjectOutputStream oos, List<Short> positions) throws IOException { + oos.writeLong(1); + oos.writeObject("foo"); + oos.writeLong(2); + oos.writeObject("bar"); + } + }; + assertArrayEquals( + populate(ObjectOutputStream.class, p, null), + populate(PositionAwareObjectOutputStream.class, p, null) + ); + } + + + @Test + public void testPositions() throws IOException { + LinkedList<Short> positions = new LinkedList<>(); + Populateable p = new Populateable() { + + @Override + public void populate(ObjectOutputStream oos, List<Short> positions) throws IOException { + byte[] buf = "Some Bytes".getBytes(); + addPosition(positions, oos); + oos.writeObject("foo"); + addPosition(positions, oos); + oos.writeShort(1); + addPosition(positions, oos); + oos.writeLong(2); + addPosition(positions, oos); + oos.writeBoolean(true); + addPosition(positions, oos); + oos.writeDouble(2.1E5); + addPosition(positions, oos); + oos.writeFloat(2.1F); + addPosition(positions, oos); + oos.write(buf); + addPosition(positions, oos); + oos.writeByte((byte) 'b'); + addPosition(positions, oos); + oos.writeChar('c'); + addPosition(positions, oos); + oos.writeChars("chars"); + addPosition(positions, oos); + oos.writeInt(1); + addPosition(positions, oos); + oos.writeObject("bar"); + addPosition(positions, oos); + } + }; + byte[] bytes = populate(PositionAwareObjectOutputStream.class, p, positions); + MockLoggerAdapter.debug("bytes size: " + bytes.length + ", positions: " + positions); + byte[] expectedBytes = populate(ObjectOutputStream.class, p, null); + MockLoggerAdapter.debug(Utils.toString(expectedBytes)); + MockLoggerAdapter.debug(Utils.toString(bytes)); + assertArrayEquals(expectedBytes, bytes); + } + + + private void addPosition(List<Short> positions, ObjectOutputStream oos) throws IOException { + if (oos instanceof PositionAwareObjectOutputStream) { + positions.add(((PositionAwareObjectOutputStream) oos).position()); + } + } + + private interface Populateable { + + void populate(ObjectOutputStream oos, List<Short> positions) throws IOException; + } + + private <T extends ObjectOutputStream> byte[] populate(Class<T> clz, Populateable p, List<Short> positions) + throws IOException { + StreamWrapper<T> sw = new StreamWrapper<>(clz); + ObjectOutputStream oos = sw.getObjectOutputStream(); + p.populate(oos, positions); + return sw.getBytes(); + } + + private static class StreamWrapper<T extends ObjectOutputStream> { + + private final ByteArrayOutputStream baos; + private ObjectOutputStream oos; + + private StreamWrapper(Class<T> clz) throws IOException { + baos = new ByteArrayOutputStream(); + // oos = clz == PositionAwareObjectOutputStream.class ? new PositionAwareObjectOutputStream(baos) : new ObjectOutputStream(baos); + // Above approach produces the same results. Something strange with the instantiation of this stream. + try { + oos = clz.getConstructor(OutputStream.class).newInstance(baos); + } catch (ReflectiveOperationException e) { + throw new IllegalArgumentException(e); + } + } + + + private ObjectOutputStream getObjectOutputStream() { + return oos; + } + + + private byte[] getBytes() throws IOException { + oos.flush(); + return baos.toByteArray(); + } + } }
diff --git a/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/wireadapters/glassfish/DefaultWireAdapterTest.java b/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/wireadapters/glassfish/DefaultWireAdapterTest.java index 16a9192..bb4767e 100644 --- a/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/wireadapters/glassfish/DefaultWireAdapterTest.java +++ b/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/wireadapters/glassfish/DefaultWireAdapterTest.java
@@ -17,132 +17,179 @@ package org.glassfish.contextpropagation.wireadapters.glassfish; -//import mockit.Deencapsulation; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.util.Map; +import org.glassfish.contextpropagation.ContextMap; +import org.glassfish.contextpropagation.InsufficientCredentialException; +import org.glassfish.contextpropagation.PropagationMode; +import org.glassfish.contextpropagation.SerializableContextFactory; +import org.glassfish.contextpropagation.adaptors.BootstrapUtils; +import org.glassfish.contextpropagation.adaptors.MockLoggerAdapter; +import org.glassfish.contextpropagation.adaptors.TestableThread; +import org.glassfish.contextpropagation.internal.Utils; +import org.glassfish.contextpropagation.spi.ContextMapHelper; +import org.glassfish.contextpropagation.spi.ContextMapPropagator; +import org.glassfish.contextpropagation.weblogic.workarea.PropagationTest; +import org.glassfish.contextpropagation.weblogic.workarea.PropertyReadOnlyException; +import org.glassfish.contextpropagation.wireadapters.AbstractWireAdapter; +import org.glassfish.contextpropagation.wireadapters.Catalog; +import org.glassfish.contextpropagation.wireadapters.WireAdapter; +import org.glassfish.contextpropagation.wireadapters.wls.MyWLSContext; +import org.glassfish.contextpropagation.wireadapters.wls.WLSWireAdapter; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import static org.glassfish.tests.utils.Utils.getField; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; public class DefaultWireAdapterTest { -// ContextMap wcMap; -// -// @Before -// public void setup() throws InsufficientCredentialException { -// BootstrapUtils.reset(); -// BootstrapUtils.bootstrap(new DefaultWireAdapter()); -// BootstrapUtils.populateMap(); -// wcMap = ContextMapHelper.getScopeAwareContextMap(); -// } -// -// @Test -// public void testWrite() throws IOException, InterruptedException { -// ByteArrayOutputStream out = new ByteArrayOutputStream(); -// ContextMapHelper.getScopeAwarePropagator().sendRequest(out, PropagationMode.RMI); -// byte[] bytes = out.toByteArray(); -// MockLoggerAdapter.debug("length: " + bytes.length + ", " + Utils.toString(bytes)); -// -// // Move to its own test -// final ByteArrayInputStream bais = new ByteArrayInputStream(bytes); -// new TestableThread() { -// @Override public void runTest() throws Exception { -// ContextMap map = ContextMapHelper.getScopeAwareContextMap(); -// assertNull(map.get("one")); -// ContextMapHelper.getScopeAwarePropagator().receiveRequest(bais); -// assertNotNull(map.get("one")); -// } -// }.startJoinAndCheckForFailures(); -// } -// -// @Test -// public void testPropagateOpaque() throws PropertyReadOnlyException, IOException, InsufficientCredentialException { -// BootstrapUtils.reset(); -// WireAdapter adapter = new WLSWireAdapter(); -// BootstrapUtils.bootstrap(adapter); -// // Receive data using WLS adaptor but lacking the necessary factory to instantiate the opaque. Make sure the object is opaque -// PropagationTest.setup(); -// byte[] bytes = PropagationTest.serialize(); -// ByteArrayInputStream bais = new ByteArrayInputStream(bytes); -// registerWorkContextFactory(); -// ContextMapPropagator propagator = Utils.getScopeAwarePropagator(); -// propagator.useWireAdapter(adapter); -// propagator.receiveRequest(bais); -// ContextMap cm = Utils.getScopeAwareContextMap(); -// assertNotNull(cm.get("workcontext")); -// -// // Propagate and receive using Default adaptors, but this time the factory is available. Make sure the object is properly instantiated. -// // Propagate to a process where there is no factory for workcontext -// ByteArrayOutputStream baos = new ByteArrayOutputStream(); -// propagator = Utils.getScopeAwarePropagator(); -// adapter = new DefaultWireAdapter(); -// propagator.useWireAdapter(adapter); -// propagator.sendRequest(baos, PropagationMode.RMI); -// -// // Receive on a process where the factory is not registered -// Map<String, SerializableContextFactory> contextFactoriesByContextName = -// Deencapsulation.getField(WireAdapter.HELPER, "contextFactoriesByContextName"); -// contextFactoriesByContextName.remove("workcontext"); -// BootstrapUtils.reset(); -// adapter = new DefaultWireAdapter(); -// BootstrapUtils.bootstrap(adapter); -// bytes = baos.toByteArray(); -// bais = new ByteArrayInputStream(bytes); -// propagator.useWireAdapter(adapter); -// propagator.receiveRequest(bais); -// assertNotNull(cm.get("workcontext")); -// // Then propagate again to a process where the library is registered -// baos = new ByteArrayOutputStream(); -// propagator = Utils.getScopeAwarePropagator(); -// adapter = new DefaultWireAdapter(); -// propagator.useWireAdapter(adapter); -// propagator.sendRequest(baos, PropagationMode.RMI); -// BootstrapUtils.reset(); -// adapter = new DefaultWireAdapter(); -// BootstrapUtils.bootstrap(adapter); -// bytes = baos.toByteArray(); -// bais = new ByteArrayInputStream(bytes); -// propagator.useWireAdapter(adapter); -// registerWorkContextFactory(); -// propagator.receiveRequest(bais); -// MyWLSContext mwc = cm.get("workcontext"); -// assertNotNull(mwc); -// assertEquals((long) 200, (long) mwc.l); -// } -// -// private void registerWorkContextFactory() { -// WireAdapter.HELPER.registerContextFactoryForContextNamed("workcontext", null, -// new SerializableContextFactory() { -// @Override -// public WLSContext createInstance() { -// return new MyWLSContext(); -// } -// }); -// } -// -// @Test public void testWithCatalog() throws PropertyReadOnlyException, IOException { -// ByteArrayOutputStream baos = new ByteArrayOutputStream(); -// ContextMapPropagator propagator = Utils.getScopeAwarePropagator(); -// WireAdapter adapter = new DefaultWireAdapter(); -// propagator.useWireAdapter(adapter); -// propagator.sendRequest(baos, PropagationMode.RMI); -// Catalog catalog = Deencapsulation.getField((AbstractWireAdapter) adapter, "catalog"); -// BootstrapUtils.reset(); -// adapter = new DefaultWireAdapter(); -// BootstrapUtils.bootstrap(adapter); -// byte[] bytes = baos.toByteArray(); -// MockLoggerAdapter.debug(Utils.toString(bytes)); -// ByteArrayInputStream bais = new ByteArrayInputStream(bytes); -// propagator.useWireAdapter(adapter); -// propagator.receiveRequest(bais); -// Catalog newCatalog = Deencapsulation.getField(adapter, "catalog"); -// assertEquals(catalog, newCatalog); -// } -// -// @Test public void testObjectInputStream() throws IOException { -// ByteArrayOutputStream baos = new ByteArrayOutputStream(); -// ObjectOutputStream oos = new ObjectOutputStream(baos); -// oos.write("Some data".getBytes()); -// oos.flush(); -// ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); -// ObjectInputStream ois = new ObjectInputStream(bais); -// assertFalse(ois.markSupported()); -// } + + ContextMap wcMap; + + @BeforeEach + public void setup() throws InsufficientCredentialException { + BootstrapUtils.bootstrap(new DefaultWireAdapter()); + BootstrapUtils.populateMap(); + wcMap = ContextMapHelper.getScopeAwareContextMap(); + } + + + @AfterEach + public void reset() { + BootstrapUtils.reset(); + } + + + @Test + public void testWrite() throws IOException, InterruptedException { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ContextMapHelper.getScopeAwarePropagator().sendRequest(out, PropagationMode.RMI); + byte[] bytes = out.toByteArray(); + MockLoggerAdapter.debug("length: " + bytes.length + ", " + Utils.toString(bytes)); + + // Move to its own test + final ByteArrayInputStream bais = new ByteArrayInputStream(bytes); + new TestableThread() { + + @Override + public void runTest() throws Exception { + ContextMap map = ContextMapHelper.getScopeAwareContextMap(); + assertNull(map.get("one")); + ContextMapHelper.getScopeAwarePropagator().receiveRequest(bais); + assertNotNull(map.get("one")); + } + }.startJoinAndCheckForFailures(); + } + + + @Test + public void testPropagateOpaque() throws Exception { + BootstrapUtils.reset(); + WireAdapter adapter = new WLSWireAdapter(); + BootstrapUtils.bootstrap(adapter); + // Receive data using WLS adaptor but lacking the necessary factory to instantiate the + // opaque. Make sure the object is opaque + PropagationTest.setup(); + byte[] bytes = PropagationTest.serialize(); + ByteArrayInputStream bais = new ByteArrayInputStream(bytes); + registerWorkContextFactory(); + ContextMapPropagator propagator = Utils.getScopeAwarePropagator(); + propagator.useWireAdapter(adapter); + propagator.receiveRequest(bais); + ContextMap cm = Utils.getScopeAwareContextMap(); + assertNotNull(cm.get("workcontext")); + + // Propagate and receive using Default adaptors, but this time the factory is available. + // Make sure the object is properly instantiated. + // Propagate to a process where there is no factory for workcontext + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + propagator = Utils.getScopeAwarePropagator(); + adapter = new DefaultWireAdapter(); + propagator.useWireAdapter(adapter); + propagator.sendRequest(baos, PropagationMode.RMI); + + // Receive on a process where the factory is not registered + Map<String, SerializableContextFactory> contextFactoriesByContextName = getField(WireAdapter.HELPER, + "contextFactoriesByContextName"); + contextFactoriesByContextName.remove("workcontext"); + BootstrapUtils.reset(); + adapter = new DefaultWireAdapter(); + BootstrapUtils.bootstrap(adapter); + bytes = baos.toByteArray(); + bais = new ByteArrayInputStream(bytes); + propagator.useWireAdapter(adapter); + propagator.receiveRequest(bais); + assertNotNull(cm.get("workcontext")); + // Then propagate again to a process where the library is registered + baos = new ByteArrayOutputStream(); + propagator = Utils.getScopeAwarePropagator(); + adapter = new DefaultWireAdapter(); + propagator.useWireAdapter(adapter); + propagator.sendRequest(baos, PropagationMode.RMI); + BootstrapUtils.reset(); + adapter = new DefaultWireAdapter(); + BootstrapUtils.bootstrap(adapter); + bytes = baos.toByteArray(); + bais = new ByteArrayInputStream(bytes); + propagator.useWireAdapter(adapter); + registerWorkContextFactory(); + propagator.receiveRequest(bais); + MyWLSContext mwc = cm.get("workcontext"); + assertNotNull(mwc); + assertEquals(200, mwc.l); + } + + + private void registerWorkContextFactory() { + WireAdapter.HELPER.registerContextFactoryForContextNamed("workcontext", null, new SerializableContextFactory() { + + @Override + public WLSContext createInstance() { + return new MyWLSContext(); + } + }); + } + + + @Test + public void testWithCatalog() throws PropertyReadOnlyException, IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ContextMapPropagator propagator = Utils.getScopeAwarePropagator(); + WireAdapter adapter = new DefaultWireAdapter(); + propagator.useWireAdapter(adapter); + propagator.sendRequest(baos, PropagationMode.RMI); + Catalog catalog = getField(adapter, "catalog", AbstractWireAdapter.class); + BootstrapUtils.reset(); + adapter = new DefaultWireAdapter(); + BootstrapUtils.bootstrap(adapter); + byte[] bytes = baos.toByteArray(); + MockLoggerAdapter.debug(Utils.toString(bytes)); + ByteArrayInputStream bais = new ByteArrayInputStream(bytes); + propagator.useWireAdapter(adapter); + propagator.receiveRequest(bais); + Catalog newCatalog = getField(adapter, "catalog", AbstractWireAdapter.class); + assertEquals(catalog, newCatalog); + } + + + @Test + public void testObjectInputStream() throws IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(baos); + oos.write("Some data".getBytes()); + oos.flush(); + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + ObjectInputStream ois = new ObjectInputStream(bais); + assertFalse(ois.markSupported()); + } }
diff --git a/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/wireadapters/glassfish/WirePropagationTest.java b/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/wireadapters/glassfish/WirePropagationTest.java index e2ab71d..dbff179 100644 --- a/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/wireadapters/glassfish/WirePropagationTest.java +++ b/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/wireadapters/glassfish/WirePropagationTest.java
@@ -17,19 +17,37 @@ package org.glassfish.contextpropagation.wireadapters.glassfish; +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +import org.glassfish.contextpropagation.PropagationMode; +import org.glassfish.contextpropagation.adaptors.BootstrapUtils; +import org.glassfish.contextpropagation.adaptors.MockLoggerAdapter; +import org.glassfish.contextpropagation.internal.Utils; +import org.glassfish.contextpropagation.spi.ContextMapHelper; +import org.glassfish.contextpropagation.spi.ContextMapPropagator; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + public class WirePropagationTest { -// @BeforeClass -// public static void setup() throws InsufficientCredentialException { -// BootstrapUtils.bootstrap(new DefaultWireAdapter()); -// BootstrapUtils.populateMap(); -// } -// -// @Test -// public void testPropagateOverWire() throws IOException { -// ContextMapPropagator wcPropagator = ContextMapHelper.getScopeAwarePropagator(); -// ByteArrayOutputStream baos = new ByteArrayOutputStream(); -// wcPropagator.sendRequest(baos, PropagationMode.SOAP); -// MockLoggerAdapter.debug(Utils.toString(baos.toByteArray())); -// } + @BeforeEach + public void setup() throws Exception { + BootstrapUtils.bootstrap(new DefaultWireAdapter()); + BootstrapUtils.populateMap(); + } + + @AfterEach + public void reset() { + BootstrapUtils.reset(); + } + + @Test + public void testPropagateOverWire() throws IOException { + ContextMapPropagator wcPropagator = ContextMapHelper.getScopeAwarePropagator(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + wcPropagator.sendRequest(baos, PropagationMode.SOAP); + MockLoggerAdapter.debug(Utils.toString(baos.toByteArray())); + } }
diff --git a/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/wireadapters/wls/WLSWireAdapterTest.java b/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/wireadapters/wls/WLSWireAdapterTest.java index b4e972b..98726cc 100644 --- a/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/wireadapters/wls/WLSWireAdapterTest.java +++ b/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/wireadapters/wls/WLSWireAdapterTest.java
@@ -17,340 +17,369 @@ package org.glassfish.contextpropagation.wireadapters.wls; -//import mockit.Deencapsulation; +import java.io.BufferedInputStream; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectInput; +import java.io.ObjectInputStream; +import java.io.ObjectOutput; +import java.io.ObjectOutputStream; +import java.io.Serializable; +import java.lang.reflect.Method; +import java.util.EnumSet; +import org.glassfish.contextpropagation.ContextMap; +import org.glassfish.contextpropagation.InsufficientCredentialException; +import org.glassfish.contextpropagation.PropagationMode; +import org.glassfish.contextpropagation.SerializableContextFactory; +import org.glassfish.contextpropagation.SerializableContextFactory.WLSContext; +import org.glassfish.contextpropagation.adaptors.BootstrapUtils; +import org.glassfish.contextpropagation.adaptors.MockLoggerAdapter; +import org.glassfish.contextpropagation.internal.AccessControlledMap; +import org.glassfish.contextpropagation.internal.Entry; +import org.glassfish.contextpropagation.internal.Utils; +import org.glassfish.contextpropagation.internal.Utils.PrivilegedWireAdapterAccessor; +import org.glassfish.contextpropagation.spi.ContextMapHelper; +import org.glassfish.contextpropagation.spi.ContextMapPropagator; +import org.glassfish.contextpropagation.weblogic.workarea.PropagationTest; +import org.glassfish.contextpropagation.weblogic.workarea.PropertyReadOnlyException; +import org.glassfish.contextpropagation.wireadapters.AbstractWireAdapter; +import org.glassfish.contextpropagation.wireadapters.Catalog; +import org.glassfish.contextpropagation.wireadapters.WireAdapter; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.glassfish.tests.utils.Utils.getField; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; public class WLSWireAdapterTest { -// WLSWireAdapter adapter = new WLSWireAdapter(); -// -// @BeforeClass -// public static void setupClass() { -// BootstrapUtils.bootstrap(new WLSWireAdapter()); -// WLSWireAdapter.HELPER.registerContextFactoryForClass(MyWLSContext.class, -// "org.glassfish.contextpropagation.weblogic.workarea.MyContext", -// new SerializableContextFactory() { -// @Override -// public WLSContext createInstance() { -// return new MyWLSContext(); -// } -// }); -// } -// -// @Before public void before() { -// BootstrapUtils.reset(); -// BootstrapUtils.bootstrap(new WLSWireAdapter()); -// } -// -// @Test -// public void testFromWLS() throws IOException, PropertyReadOnlyException, ClassNotFoundException { -// PropagationTest.setup(); -// byte[] bytes = PropagationTest.serialize(); -// ByteArrayInputStream bais = new ByteArrayInputStream(bytes); -// WireAdapter adapter = new WLSWireAdapter(); -// adapter.prepareToReadFrom(bais); -// for(String key = adapter.readKey(); key != null; key = adapter.readKey()) { -// adapter.readEntry(); -// } -// } -// -// @Test public void testWithCatalog() throws PropertyReadOnlyException, IOException, InsufficientCredentialException { -// BootstrapUtils.populateMap(); -// ByteArrayOutputStream baos = new ByteArrayOutputStream(); -// ContextMapPropagator propagator = Utils.getScopeAwarePropagator(); -// WLSWireAdapter adapter = new WLSWireAdapter(); -// propagator.useWireAdapter(adapter); -// propagator.sendRequest(baos, PropagationMode.RMI); -// Catalog catalog = Deencapsulation.getField((AbstractWireAdapter) adapter, "catalog"); -// BootstrapUtils.reset(); -// adapter = new WLSWireAdapter(); -// BootstrapUtils.bootstrap(adapter); -// byte[] bytes = baos.toByteArray(); -// MockLoggerAdapter.debug(Utils.toString(bytes)); -// ByteArrayInputStream bais = new ByteArrayInputStream(bytes); -// propagator.useWireAdapter(adapter); -// propagator.receiveRequest(bais); -// Catalog newCatalog = Deencapsulation.getField(adapter, "wlsCatalog"); -// assertEquals(catalog, newCatalog); -// //MockLoggerAdapter.debug("start: " + Deencapsulation.getField(newCatalog, "start") + ", end: " + Deencapsulation.getField(newCatalog, "end")); -// } -// -// @Test -// public void testResilientWithBadSerializableInsertFirst() throws IOException, InsufficientCredentialException { -// badSerializable(true); -// } -// -// @Test -// public void testResilientWithBadSerializableInsertLast() throws IOException, InsufficientCredentialException { -// badSerializable(false); -// } -// -// private void badSerializable(boolean insertFirst) throws IOException, InsufficientCredentialException { -// ContextMap wcMap = ContextMapHelper.getScopeAwareContextMap(); -// String key = "faulty serializable"; -// @SuppressWarnings("serial") -// Serializable faultySerializable = new Serializable() { -// @SuppressWarnings("unused") -// transient String s = ""; -// private void writeObject(ObjectOutputStream out) -// throws IOException { -// out.writeLong(1L); -// out.writeUTF("a string"); -// } -// private void readObject(ObjectInputStream in) -// throws IOException, ClassNotFoundException { -// MockLoggerAdapter.debug("*******"); -// in.readFully(new byte[25]); // expected to fail since we should be reading a long and produce stack traces -// MockLoggerAdapter.debug(" -- done"); -// } -// }; -// if (insertFirst) Deencapsulation.invoke(wcMap, "putSerializable", key, faultySerializable, PropagationMode.defaultSet()); -// BootstrapUtils.populateMap(); -// if (!insertFirst) Deencapsulation.invoke(wcMap, "putSerializable", key, faultySerializable, PropagationMode.defaultSet()); -// ByteArrayOutputStream baos = new ByteArrayOutputStream(); -// ContextMapPropagator propagator = Utils.getScopeAwarePropagator(); -// WLSWireAdapter adapter = new WLSWireAdapter(); -// propagator.useWireAdapter(adapter); -// propagator.sendRequest(baos, PropagationMode.RMI); -// Catalog catalog = Deencapsulation.getField((AbstractWireAdapter) adapter, "catalog"); -// BootstrapUtils.reset(); -// adapter = new WLSWireAdapter(); -// BootstrapUtils.bootstrap(adapter); -// byte[] bytes = baos.toByteArray(); -// MockLoggerAdapter.debug(Utils.toString(bytes)); -// ByteArrayInputStream bais = new ByteArrayInputStream(bytes); -// propagator.useWireAdapter(adapter); -// propagator.receiveRequest(bais); -// Catalog newCatalog = Deencapsulation.getField(adapter, "wlsCatalog"); -// assertNull(wcMap.get(key)); -// assertEquals(catalog, newCatalog); // Check that the catalog is read since the faulty context is read before it. -// } -// -// @Test(expected = IOException.class) public void markReset() throws IOException { -// ByteArrayOutputStream baos = new ByteArrayOutputStream(); -// ObjectOutputStream oos = new ObjectOutputStream(baos); -// oos.write("Some data".getBytes()); -// oos.flush(); -// ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); -// bais.mark(100); -// bais.read(); -// bais.reset(); -// ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(bais)); -// ois.mark(100); -// ois.read(); -// ois.skip(5); // It can skip but cannot reset -// assertEquals(false, ois.markSupported()); -// ois.reset(); // ObjectOutputStream does not support reset even if we give it a BufferedInputStream -// } -// -// @Test -// public void testResilientWithBadWorkContext() throws IOException, InsufficientCredentialException { -// ContextMap wcMap = ContextMapHelper.getScopeAwareContextMap(); -// AccessControlledMap acm = ((PrivilegedWireAdapterAccessor) wcMap).getAccessControlledMap(true); -// String key = "bad work context"; -// WLSContext ctx = new WLSContext() { -// @Override -// public void writeContext(ObjectOutput out) throws IOException { -// out.writeUTF("a string"); -// } -// @Override -// public void readContext(ObjectInput in) throws IOException { -// in.readLong(); // Expected to fail -// } -// }; -// acm.put(key, Entry.createOpaqueEntryInstance(ctx, PropagationMode.defaultSet(), ctx.getClass().getName()).init( -// true, false)); -// BootstrapUtils.populateMap(); -// ByteArrayOutputStream baos = new ByteArrayOutputStream(); -// ContextMapPropagator propagator = Utils.getScopeAwarePropagator(); -// WLSWireAdapter adapter = new WLSWireAdapter(); -// propagator.useWireAdapter(adapter); -// propagator.sendRequest(baos, PropagationMode.RMI); -// Catalog catalog = Deencapsulation.getField((AbstractWireAdapter) adapter, "catalog"); -// BootstrapUtils.reset(); -// adapter = new WLSWireAdapter(); -// BootstrapUtils.bootstrap(adapter); -// byte[] bytes = baos.toByteArray(); -// MockLoggerAdapter.debug(Utils.toString(bytes)); -// ByteArrayInputStream bais = new ByteArrayInputStream(bytes); -// propagator.useWireAdapter(adapter); -// propagator.receiveRequest(bais); -// Catalog newCatalog = Deencapsulation.getField(adapter, "wlsCatalog"); -// assertEquals(catalog, newCatalog); -// assertNull(wcMap.get(key)); -// } -// -// public static byte[] toWLSBytes() throws InsufficientCredentialException, IOException { -// ByteArrayOutputStream baos = new ByteArrayOutputStream(); -// WireAdapter adapter = new WLSWireAdapter(); -// BootstrapUtils.bootstrap(adapter); -// BootstrapUtils.populateMap(); -// //adapter.prepareToWriteTo(baos); -// ContextMapPropagator propagator = ContextMapHelper.getScopeAwarePropagator(); -// Deencapsulation.setField(WLSWireAdapter.class, "WLS_CARRIER_CLASS_NAME", -// "org.glassfish.contextpropagation.weblogic.workarea.SerializableWorkContext$Carrier"); -// Deencapsulation.setField(ClassNames.class, "ASCII", -// "org.glassfish.contextpropagation.weblogic.workarea.AsciiWorkContext".getBytes()); -// Deencapsulation.setField(ClassNames.class, "LONG", -// "org.glassfish.contextpropagation.weblogic.workarea.LongWorkContext".getBytes()); -// Deencapsulation.setField(ClassNames.class, "STRING", -// "org.glassfish.contextpropagation.weblogic.workarea.StringWorkContext".getBytes()); -// Deencapsulation.setField(ClassNames.class, "SERIALIZABLE", -// "org.glassfish.contextpropagation.weblogic.workarea.SerializableWorkContext".getBytes()); -// propagator.sendRequest(baos, PropagationMode.RMI); -// //MockLoggerAdapter.debug(Utils.toString(baos.toByteArray())); -// return baos.toByteArray(); -// } -// -// @Test @Ignore("moved functionality to Carrier, check if duplicate test or needs rework") -// public void testObjectOutputStreamWriteObject() throws IOException { -// /* -// * checks the capability to change the class name on the wire when serializing objects. -// */ -// ByteArrayOutputStream baos = new ByteArrayOutputStream(); -// SerializableContextFactory contextFactory = new SerializableContextFactory() { -// @Override -// public WLSContext createInstance() { -// // Auto-generated method stub -// return null; -// }}; -// Serializable s = new MySerializable(); -// WLSWireAdapter.HELPER.registerContextFactoryForClass(MySerializable.class, -// "workarea.String", contextFactory); -// WLSWireAdapter.HELPER.registerContextFactoryForContextNamed( -// "foo", "workarea.Foo", contextFactory); -// Object oois = Deencapsulation.newInstance( -// "weblogic.workarea.WLSWireAdapter$ObjectOutputInterceptorStream", baos); -// Deencapsulation.invoke(oois, "writeObject", s); -// Deencapsulation.invoke(oois, "setContextName", "foo"); -// Deencapsulation.invoke(oois, "writeObject", new MySerializable2()); -// Deencapsulation.invoke(oois, "flush"); -// String output = new String(baos.toByteArray()); -// assertTrue(output.contains("workarea.String")); -// assertTrue(output.contains("workarea.Foo")); -// } -// -// @Test public void convertPropagationMode() { -// // LOCAL, WORK, RMI, TRANSACTION, JMS_QUEUE, JMS_TOPIC, SOAP, MIME_HEADER, ONEWAY -// checkPropModeConversion(EnumSet.of(PropagationMode.LOCAL), -// org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.LOCAL); -// checkPropModeConversion(EnumSet.of(PropagationMode.THREAD), -// org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.WORK); -// checkPropModeConversion(EnumSet.of(PropagationMode.RMI), -// org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.RMI); -// checkPropModeConversion(EnumSet.of(PropagationMode.TRANSACTION), -// org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.TRANSACTION); -// checkPropModeConversion(EnumSet.of(PropagationMode.JMS_QUEUE), -// org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.JMS_QUEUE); -// checkPropModeConversion(EnumSet.of(PropagationMode.JMS_TOPIC), -// org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.JMS_TOPIC); -// checkPropModeConversion(EnumSet.of(PropagationMode.SOAP), -// org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.SOAP); -// checkPropModeConversion(EnumSet.of(PropagationMode.MIME_HEADER), -// org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.MIME_HEADER); -// checkPropModeConversion(EnumSet.of(PropagationMode.ONEWAY), -// org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.ONEWAY); -// /* -// * Glassfish's default includes all of the WLS default plus THREAD (which -// * is equivalent to the WLS propagation mode WORK) -// */ -// checkPropModeConversion(PropagationMode.defaultSet(), -// org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.DEFAULT + -// org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.WORK); -// checkPropModeConversion(PropagationMode.defaultSet(), -// org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.GLOBAL + -// org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.WORK); -// } -// -// private void checkPropModeConversion(EnumSet<PropagationMode> propagationModes, int expectedWLSPropMode) { -// assertEquals(expectedWLSPropMode, WLSWireAdapter.toWlsPropagationMode(propagationModes)); -// } -// -// @Test public void toPropagationMode() { -// checkReversePropModeConversion(EnumSet.of(PropagationMode.LOCAL), -// org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.LOCAL); -// checkReversePropModeConversion(EnumSet.of(PropagationMode.THREAD), -// org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.WORK); -// checkReversePropModeConversion(EnumSet.of(PropagationMode.RMI), -// org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.RMI); -// checkReversePropModeConversion(EnumSet.of(PropagationMode.TRANSACTION), -// org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.TRANSACTION); -// checkReversePropModeConversion(EnumSet.of(PropagationMode.JMS_QUEUE), -// org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.JMS_QUEUE); -// checkReversePropModeConversion(EnumSet.of(PropagationMode.JMS_TOPIC), -// org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.JMS_TOPIC); -// checkReversePropModeConversion(EnumSet.of(PropagationMode.SOAP), -// org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.SOAP); -// checkReversePropModeConversion(EnumSet.of(PropagationMode.MIME_HEADER), -// org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.MIME_HEADER); -// checkReversePropModeConversion(EnumSet.of(PropagationMode.ONEWAY), -// org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.ONEWAY); -// checkReversePropModeConversion(PropagationMode.defaultSet(), -// org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.DEFAULT + -// org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.WORK); -// checkReversePropModeConversion(PropagationMode.defaultSet(), -// org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.GLOBAL + -// org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.WORK); -// } -// -// private void checkReversePropModeConversion( -// EnumSet<PropagationMode> modes, int mode) { -// assertEquals(modes, WLSWireAdapter.toPropagationMode(mode)); -// -// } -// -// void foo(Object o) {System.out.println(o);} -// -// @Test public void testFoo() { -// foo(1); -// } -// -// @Test public void testStreams() throws IOException { -// ByteArrayOutputStream baos = new ByteArrayOutputStream(); -// ObjectOutputStream oos = new ObjectOutputStream(baos); -// oos.write("foo".getBytes()); -// oos.write("bar".getBytes()); -// oos.flush(); -// MockLoggerAdapter.debug(Utils.toString(baos.toByteArray())); -// -// baos = new ByteArrayOutputStream(); -// oos = new ObjectOutputStream(baos); -// oos.write("foo".getBytes()); -// ByteArrayOutputStream baos2 = new ByteArrayOutputStream(); -// ObjectOutputStream oos2 = new ObjectOutputStream(baos2); -// oos2.write("bar".getBytes()); -// oos2.flush(); -// oos.write(baos2.toByteArray()); -// oos.flush(); -// MockLoggerAdapter.debug(Utils.toString(baos.toByteArray())); -// -// baos = new ByteArrayOutputStream(); -// oos = new ObjectOutputStream(baos); -// oos.write("foo".getBytes()); -// baos2 = new ByteArrayOutputStream(); -// oos2 = new ObjectOutputStream(baos2); -// oos2.write("bar".getBytes()); -// oos2.flush(); -// oos.flush(); -// baos.write(baos2.toByteArray()); -// MockLoggerAdapter.debug(Utils.toString(baos.toByteArray())); -// -// baos = new ByteArrayOutputStream(); -// baos.write("foo".getBytes()); -// MockLoggerAdapter.debug(Utils.toString(baos.toByteArray())); -// -// baos = new ByteArrayOutputStream(); -// oos = new ObjectOutputStream(baos); -// oos.write("foo".getBytes()); -// baos2 = new ByteArrayOutputStream(); -// oos2 = new ObjectOutputStream(baos2); -// oos2.write("bar".getBytes()); -// oos2.flush(); -// byte[] bytes = baos2.toByteArray(); -// oos.write(bytes, 6, bytes.length - 6); -// oos.flush(); -// MockLoggerAdapter.debug(Utils.toString(baos.toByteArray())); -// } + + private WLSWireAdapter adapter; + + @BeforeAll + public static void setupClass() { + WireAdapter.HELPER.registerContextFactoryForClass(MyWLSContext.class, + "org.glassfish.contextpropagation.weblogic.workarea.MyContext", new SerializableContextFactory() { + + @Override + public WLSContext createInstance() { + return new MyWLSContext(); + } + }); + } + @BeforeEach + public void before() { + BootstrapUtils.bootstrap(new WLSWireAdapter()); + adapter = new WLSWireAdapter(); + } + + + @AfterEach + public void after() { + BootstrapUtils.reset(); + } + + + @Test + public void testFromWLS() throws Exception { + PropagationTest.setup(); + byte[] bytes = PropagationTest.serialize(); + ByteArrayInputStream bais = new ByteArrayInputStream(bytes); + WireAdapter adapter = new WLSWireAdapter(); + adapter.prepareToReadFrom(bais); + for (String key = adapter.readKey(); key != null; key = adapter.readKey()) { + adapter.readEntry(); + } + } + + + @Test + public void testWithCatalog() throws PropertyReadOnlyException, IOException, InsufficientCredentialException { + BootstrapUtils.populateMap(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ContextMapPropagator propagator = Utils.getScopeAwarePropagator(); + WLSWireAdapter adapter = new WLSWireAdapter(); + propagator.useWireAdapter(adapter); + propagator.sendRequest(baos, PropagationMode.RMI); + Catalog catalog = getField(adapter, "catalog", AbstractWireAdapter.class); + BootstrapUtils.reset(); + adapter = new WLSWireAdapter(); + BootstrapUtils.bootstrap(adapter); + byte[] bytes = baos.toByteArray(); + MockLoggerAdapter.debug(Utils.toString(bytes)); + ByteArrayInputStream bais = new ByteArrayInputStream(bytes); + propagator.useWireAdapter(adapter); + propagator.receiveRequest(bais); + Catalog newCatalog = getField(adapter, "wlsCatalog"); + assertEquals(catalog, newCatalog); + // MockLoggerAdapter.debug("start: " + Deencapsulation.getField(newCatalog, "start") + ", + // end: " + Deencapsulation.getField(newCatalog, "end")); + } + + + @Test + public void testResilientWithBadSerializableInsertFirst() throws Exception { + badSerializable(true); + } + + + @Test + public void testResilientWithBadSerializableInsertLast() throws Exception { + badSerializable(false); + } + + + private void badSerializable(boolean insertFirst) throws Exception { + ContextMap wcMap = ContextMapHelper.getScopeAwareContextMap(); + String key = "faulty serializable"; + @SuppressWarnings("serial") + Serializable faultySerializable = new Serializable() { + + @SuppressWarnings("unused") + transient String s = ""; + + private void writeObject(ObjectOutputStream out) throws IOException { + out.writeLong(1L); + out.writeUTF("a string"); + } + + + private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { + MockLoggerAdapter.debug("*******"); + in.readFully(new byte[25]); // expected to fail since we should be reading a long + // and produce stack traces + MockLoggerAdapter.debug(" -- done"); + } + }; + Method method = wcMap.getClass().getDeclaredMethod("putSerializable", String.class, Serializable.class, EnumSet.class); + method.setAccessible(true); + if (insertFirst) { + method.invoke(wcMap, key, faultySerializable, PropagationMode.defaultSet()); + } + BootstrapUtils.populateMap(); + if (!insertFirst) { + method.invoke(wcMap, key, faultySerializable, PropagationMode.defaultSet()); + } + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ContextMapPropagator propagator = Utils.getScopeAwarePropagator(); + WLSWireAdapter adapter = new WLSWireAdapter(); + propagator.useWireAdapter(adapter); + propagator.sendRequest(baos, PropagationMode.RMI); + Catalog catalog = getField(adapter, "catalog", AbstractWireAdapter.class); + BootstrapUtils.reset(); + adapter = new WLSWireAdapter(); + BootstrapUtils.bootstrap(adapter); + byte[] bytes = baos.toByteArray(); + MockLoggerAdapter.debug(Utils.toString(bytes)); + ByteArrayInputStream bais = new ByteArrayInputStream(bytes); + propagator.useWireAdapter(adapter); + propagator.receiveRequest(bais); + Catalog newCatalog = getField(adapter, "wlsCatalog"); + assertNull(wcMap.get(key)); + // Check that the catalog is read since the faulty context is read before it. + assertEquals(catalog, newCatalog); + } + + + @Test + public void markReset() throws IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(baos); + oos.write("Some data".getBytes()); + oos.flush(); + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + bais.mark(100); + bais.read(); + bais.reset(); + ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(bais)); + ois.mark(100); + ois.read(); + ois.skip(5); // It can skip but cannot reset + assertEquals(false, ois.markSupported()); + // ObjectOutputStream does not support reset even if we give it a BufferedInputStream + assertThrows(IOException.class, ois::reset); + } + + + @Test + public void testResilientWithBadWorkContext() throws IOException, InsufficientCredentialException { + ContextMap wcMap = ContextMapHelper.getScopeAwareContextMap(); + AccessControlledMap acm = ((PrivilegedWireAdapterAccessor) wcMap).getAccessControlledMap(true); + String key = "bad work context"; + WLSContext ctx = new WLSContext() { + + @Override + public void writeContext(ObjectOutput out) throws IOException { + out.writeUTF("a string"); + } + + + @Override + public void readContext(ObjectInput in) throws IOException { + in.readLong(); // Expected to fail + } + }; + acm.put(key, Entry.createOpaqueEntryInstance(ctx, PropagationMode.defaultSet(), ctx.getClass().getName()) + .init(true, false)); + BootstrapUtils.populateMap(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ContextMapPropagator propagator = Utils.getScopeAwarePropagator(); + WLSWireAdapter adapter = new WLSWireAdapter(); + propagator.useWireAdapter(adapter); + propagator.sendRequest(baos, PropagationMode.RMI); + Catalog catalog = getField(adapter, "catalog", AbstractWireAdapter.class); + BootstrapUtils.reset(); + adapter = new WLSWireAdapter(); + BootstrapUtils.bootstrap(adapter); + byte[] bytes = baos.toByteArray(); + MockLoggerAdapter.debug(Utils.toString(bytes)); + ByteArrayInputStream bais = new ByteArrayInputStream(bytes); + propagator.useWireAdapter(adapter); + propagator.receiveRequest(bais); + Catalog newCatalog = getField(adapter, "wlsCatalog"); + assertEquals(catalog, newCatalog); + assertNull(wcMap.get(key)); + } + + + @Test + public void convertPropagationMode() { + // LOCAL, WORK, RMI, TRANSACTION, JMS_QUEUE, JMS_TOPIC, SOAP, MIME_HEADER, ONEWAY + checkPropModeConversion(EnumSet.of(PropagationMode.LOCAL), + org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.LOCAL); + checkPropModeConversion(EnumSet.of(PropagationMode.THREAD), + org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.WORK); + checkPropModeConversion(EnumSet.of(PropagationMode.RMI), + org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.RMI); + checkPropModeConversion(EnumSet.of(PropagationMode.TRANSACTION), + org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.TRANSACTION); + checkPropModeConversion(EnumSet.of(PropagationMode.JMS_QUEUE), + org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.JMS_QUEUE); + checkPropModeConversion(EnumSet.of(PropagationMode.JMS_TOPIC), + org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.JMS_TOPIC); + checkPropModeConversion(EnumSet.of(PropagationMode.SOAP), + org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.SOAP); + checkPropModeConversion(EnumSet.of(PropagationMode.MIME_HEADER), + org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.MIME_HEADER); + checkPropModeConversion(EnumSet.of(PropagationMode.ONEWAY), + org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.ONEWAY); + /* + * Glassfish's default includes all of the WLS default plus THREAD (which + * is equivalent to the WLS propagation mode WORK) + */ + checkPropModeConversion(PropagationMode.defaultSet(), + org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.DEFAULT + + org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.WORK); + checkPropModeConversion(PropagationMode.defaultSet(), + org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.GLOBAL + + org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.WORK); + } + + + private void checkPropModeConversion(EnumSet<PropagationMode> propagationModes, int expectedWLSPropMode) { + assertEquals(expectedWLSPropMode, WLSWireAdapter.toWlsPropagationMode(propagationModes)); + } + + + @Test + public void toPropagationMode() { + checkReversePropModeConversion(EnumSet.of(PropagationMode.LOCAL), + org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.LOCAL); + checkReversePropModeConversion(EnumSet.of(PropagationMode.THREAD), + org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.WORK); + checkReversePropModeConversion(EnumSet.of(PropagationMode.RMI), + org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.RMI); + checkReversePropModeConversion(EnumSet.of(PropagationMode.TRANSACTION), + org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.TRANSACTION); + checkReversePropModeConversion(EnumSet.of(PropagationMode.JMS_QUEUE), + org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.JMS_QUEUE); + checkReversePropModeConversion(EnumSet.of(PropagationMode.JMS_TOPIC), + org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.JMS_TOPIC); + checkReversePropModeConversion(EnumSet.of(PropagationMode.SOAP), + org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.SOAP); + checkReversePropModeConversion(EnumSet.of(PropagationMode.MIME_HEADER), + org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.MIME_HEADER); + checkReversePropModeConversion(EnumSet.of(PropagationMode.ONEWAY), + org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.ONEWAY); + checkReversePropModeConversion(PropagationMode.defaultSet(), + org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.DEFAULT + + org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.WORK); + checkReversePropModeConversion(PropagationMode.defaultSet(), + org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.GLOBAL + + org.glassfish.contextpropagation.weblogic.workarea.PropagationMode.WORK); + } + + + private void checkReversePropModeConversion(EnumSet<PropagationMode> modes, int mode) { + assertEquals(modes, WLSWireAdapter.toPropagationMode(mode)); + + } + + + void foo(Object o) { + System.out.println(o); + } + + + @Test + public void testFoo() { + foo(1); + } + + + @Test + public void testStreams() throws IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(baos); + oos.write("foo".getBytes()); + oos.write("bar".getBytes()); + oos.flush(); + MockLoggerAdapter.debug(Utils.toString(baos.toByteArray())); + + baos = new ByteArrayOutputStream(); + oos = new ObjectOutputStream(baos); + oos.write("foo".getBytes()); + ByteArrayOutputStream baos2 = new ByteArrayOutputStream(); + ObjectOutputStream oos2 = new ObjectOutputStream(baos2); + oos2.write("bar".getBytes()); + oos2.flush(); + oos.write(baos2.toByteArray()); + oos.flush(); + MockLoggerAdapter.debug(Utils.toString(baos.toByteArray())); + + baos = new ByteArrayOutputStream(); + oos = new ObjectOutputStream(baos); + oos.write("foo".getBytes()); + baos2 = new ByteArrayOutputStream(); + oos2 = new ObjectOutputStream(baos2); + oos2.write("bar".getBytes()); + oos2.flush(); + oos.flush(); + baos.write(baos2.toByteArray()); + MockLoggerAdapter.debug(Utils.toString(baos.toByteArray())); + + baos = new ByteArrayOutputStream(); + baos.write("foo".getBytes()); + MockLoggerAdapter.debug(Utils.toString(baos.toByteArray())); + + baos = new ByteArrayOutputStream(); + oos = new ObjectOutputStream(baos); + oos.write("foo".getBytes()); + baos2 = new ByteArrayOutputStream(); + oos2 = new ObjectOutputStream(baos2); + oos2.write("bar".getBytes()); + oos2.flush(); + byte[] bytes = baos2.toByteArray(); + oos.write(bytes, 6, bytes.length - 6); + oos.flush(); + MockLoggerAdapter.debug(Utils.toString(baos.toByteArray())); + } }
diff --git a/nucleus/test-utils/utils/src/main/java/org/glassfish/tests/utils/Utils.java b/nucleus/test-utils/utils/src/main/java/org/glassfish/tests/utils/Utils.java index 3794965..0237282 100644 --- a/nucleus/test-utils/utils/src/main/java/org/glassfish/tests/utils/Utils.java +++ b/nucleus/test-utils/utils/src/main/java/org/glassfish/tests/utils/Utils.java
@@ -138,11 +138,11 @@ } } - public static void getStaticField(Class<?> clazz, String fieldName) { + public static <T> T getStaticField(Class<?> clazz, String fieldName) { try { Field field = clazz.getDeclaredField(fieldName); field.setAccessible(true); - field.get(null); + return (T) field.get(null); } catch (Exception e) { throw new IllegalStateException("Failed to get static field " + fieldName + " of " + clazz, e); } @@ -159,8 +159,12 @@ } public static <T> T getField(Object instance, String fieldName) { + return getField(instance, fieldName, instance.getClass()); + } + + public static <T> T getField(Object instance, String fieldName, Class<?> parentOfInstance) { try { - Field field = instance.getClass().getDeclaredField(fieldName); + Field field = parentOfInstance.getDeclaredField(fieldName); field.setAccessible(true); return (T) field.get(instance); } catch (Exception e) {