Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

TCK: Standardize test clients and context lookups #292

Merged
merged 4 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.jupiter.api.Test;

import ee.jakarta.tck.concurrent.framework.TestConstants;
import ee.jakarta.tck.concurrent.framework.TestLogger;
import ee.jakarta.tck.concurrent.framework.TestUtil;
import ee.jakarta.tck.concurrent.framework.junit.anno.Web;
import jakarta.annotation.Resource;
import jakarta.enterprise.concurrent.ContextService;
import jakarta.enterprise.concurrent.ManagedTaskListener;

@Web
Expand All @@ -42,6 +44,9 @@ public static WebArchive createDeployment() {
return ShrinkWrap.create(WebArchive.class)
.addPackages(true, ContextServiceTests.class.getPackage());
}

@Resource(lookup = TestConstants.DefaultContextService)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to explicitly specify default values? I would use the lookup only when needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In some cases, yes.
This resource is injected by the Arquillian enricher and not the application server itself.

public ContextService context;

/*
* @testName: ContextServiceWithIntf
Expand All @@ -55,7 +60,7 @@ public static WebArchive createDeployment() {
public void ContextServiceWithIntf() {
boolean pass = false;
try {
Runnable proxy = (Runnable) TestUtil.getContextService().createContextualProxy(new TestRunnableWork(), Runnable.class);
Runnable proxy = (Runnable) context.createContextualProxy(new TestRunnableWork(), Runnable.class);
pass = true;
} catch (Exception e) {
log.severe("Unexpected Exception Caught", e);
Expand All @@ -76,7 +81,7 @@ public void ContextServiceWithIntf() {
public void ContextServiceWithIntfAndIntfNoImplemented() {
boolean pass = false;
try {
Object proxy = TestUtil.getContextService().createContextualProxy(new Object(), Runnable.class);
Object proxy = context.createContextualProxy(new Object(), Runnable.class);
} catch (IllegalArgumentException ie) {
pass = true;
} catch (Exception e) {
Expand All @@ -98,7 +103,7 @@ public void ContextServiceWithIntfAndIntfNoImplemented() {
public void ContextServiceWithIntfAndInstanceIsNull() {
boolean pass = false;
try {
Object proxy = TestUtil.getContextService().createContextualProxy(null, Runnable.class);
Object proxy = context.createContextualProxy(null, Runnable.class);
log.info(proxy.toString());
} catch (IllegalArgumentException ie) {
pass = true;
Expand All @@ -120,7 +125,7 @@ public void ContextServiceWithIntfAndInstanceIsNull() {
public void ContextServiceWithMultiIntfs() {
boolean pass = false;
try {
Object proxy = TestUtil.getContextService().createContextualProxy(new TestRunnableWork(), Runnable.class, TestWorkInterface.class);
Object proxy = context.createContextualProxy(new TestRunnableWork(), Runnable.class, TestWorkInterface.class);
pass = proxy instanceof Runnable && proxy instanceof TestWorkInterface;
} catch (Exception e) {
log.severe("Unexpected Exception Caught", e);
Expand All @@ -141,7 +146,7 @@ public void ContextServiceWithMultiIntfs() {
public void ContextServiceWithMultiIntfsAndIntfNoImplemented() {
boolean pass = false;
try {
Object proxy = TestUtil.getContextService().createContextualProxy(new TestRunnableWork(), Runnable.class, TestWorkInterface.class,
Object proxy = context.createContextualProxy(new TestRunnableWork(), Runnable.class, TestWorkInterface.class,
ManagedTaskListener.class);
} catch (IllegalArgumentException ie) {
pass = true;
Expand All @@ -164,7 +169,7 @@ public void ContextServiceWithMultiIntfsAndIntfNoImplemented() {
public void ContextServiceWithMultiIntfsAndInstanceIsNull() {
boolean pass = false;
try {
Object proxy = TestUtil.getContextService().createContextualProxy(null, Runnable.class, TestWorkInterface.class);
Object proxy = context.createContextualProxy(null, Runnable.class, TestWorkInterface.class);
log.info(proxy.toString());
} catch (IllegalArgumentException ie) {
pass = true;
Expand All @@ -190,7 +195,7 @@ public void ContextServiceWithIntfAndProperties() {
execProps.put("vendor_a.security.tokenexpiration", "15000");
execProps.put("USE_PARENT_TRANSACTION", "true");

Runnable proxy = (Runnable) TestUtil.getContextService().createContextualProxy(new TestRunnableWork(), execProps, Runnable.class);
Runnable proxy = (Runnable) context.createContextualProxy(new TestRunnableWork(), execProps, Runnable.class);
pass = true;
} catch (Exception e) {
log.severe("Unexpected Exception Caught", e);
Expand All @@ -214,7 +219,7 @@ public void ContextServiceWithMultiIntfsAndProperties() {
execProps.put("vendor_a.security.tokenexpiration", "15000");
execProps.put("USE_PARENT_TRANSACTION", "true");

Object proxy = TestUtil.getContextService().createContextualProxy(new TestRunnableWork(), execProps, Runnable.class,
Object proxy = context.createContextualProxy(new TestRunnableWork(), execProps, Runnable.class,
TestWorkInterface.class);
pass = proxy instanceof Runnable && proxy instanceof TestWorkInterface;
} catch (Exception e) {
Expand All @@ -240,7 +245,7 @@ public void ContextServiceWithIntfAndPropertiesAndIntfNoImplemented() {
execProps.put("vendor_a.security.tokenexpiration", "15000");
execProps.put("USE_PARENT_TRANSACTION", "true");

Object proxy = TestUtil.getContextService().createContextualProxy(new TestRunnableWork(), execProps, Runnable.class,
Object proxy = context.createContextualProxy(new TestRunnableWork(), execProps, Runnable.class,
ManagedTaskListener.class);
} catch (IllegalArgumentException ie) {
pass = true;
Expand All @@ -267,7 +272,7 @@ public void ContextServiceWithIntfsAndPropertiesAndInstanceIsNull() {
execProps.put("vendor_a.security.tokenexpiration", "15000");
execProps.put("USE_PARENT_TRANSACTION", "true");

Object proxy = TestUtil.getContextService().createContextualProxy(null, execProps, Runnable.class);
Object proxy = context.createContextualProxy(null, execProps, Runnable.class);
log.info(proxy.toString());
} catch (IllegalArgumentException ie) {
pass = true;
Expand All @@ -294,7 +299,7 @@ public void ContextServiceWithMultiIntfsAndPropertiesAndIntfNoImplemented() {
execProps.put("vendor_a.security.tokenexpiration", "15000");
execProps.put("USE_PARENT_TRANSACTION", "true");

Object proxy = TestUtil.getContextService().createContextualProxy(new TestRunnableWork(), execProps, Runnable.class,
Object proxy = context.createContextualProxy(new TestRunnableWork(), execProps, Runnable.class,
TestWorkInterface.class, ManagedTaskListener.class);
} catch (IllegalArgumentException ie) {
pass = true;
Expand All @@ -321,7 +326,7 @@ public void ContextServiceWithMultiIntfsAndPropertiesAndInstanceIsNull() {
execProps.put("vendor_a.security.tokenexpiration", "15000");
execProps.put("USE_PARENT_TRANSACTION", "true");

Object proxy = TestUtil.getContextService().createContextualProxy(null, execProps, Runnable.class, TestWorkInterface.class);
Object proxy = context.createContextualProxy(null, execProps, Runnable.class, TestWorkInterface.class);
log.info(proxy.toString());
} catch (IllegalArgumentException ie) {
pass = true;
Expand All @@ -347,9 +352,9 @@ public void GetExecutionProperties() {
Map<String, String> execProps = new HashMap<String, String>();
execProps.put("USE_PARENT_TRANSACTION", "true");

Object proxy = TestUtil.getContextService().createContextualProxy(new TestRunnableWork(), execProps, Runnable.class,
Object proxy = context.createContextualProxy(new TestRunnableWork(), execProps, Runnable.class,
TestWorkInterface.class);
Map<String, String> returnedExecProps = TestUtil.getContextService().getExecutionProperties(proxy);
Map<String, String> returnedExecProps = context.getExecutionProperties(proxy);

if (!"true".equals(returnedExecProps.get("USE_PARENT_TRANSACTION"))) {
log.severe("Expected:true, actual message=" + returnedExecProps.get("USE_PARENT_TRANSACTION"));
Expand All @@ -374,7 +379,7 @@ public void GetExecutionProperties() {
public void GetExecutionPropertiesNoProxy() {
boolean pass = false;
try {
Map<String, String> returnedExecProps = TestUtil.getContextService().getExecutionProperties(new Object());
Map<String, String> returnedExecProps = context.getExecutionProperties(new Object());
pass = true;
} catch (IllegalArgumentException ie) {
pass = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
import ee.jakarta.tck.concurrent.framework.junit.anno.Common.PACKAGE;
import ee.jakarta.tck.concurrent.framework.junit.anno.TestName;
import ee.jakarta.tck.concurrent.framework.junit.anno.Web;
import jakarta.annotation.Resource;
import jakarta.enterprise.concurrent.ManagedExecutors;
import jakarta.enterprise.concurrent.ManagedScheduledExecutorService;
import jakarta.enterprise.concurrent.ManagedTask;

@Web
Expand All @@ -60,6 +62,9 @@ public void reset() {

@TestName
public String testname;

@Resource(lookup = TestConstants.DefaultManagedScheduledExecutorService)
public ManagedScheduledExecutorService scheduledExecutor;

/*
* @testName: lastExecutionGetIdentityNameTest
Expand All @@ -76,7 +81,7 @@ public void lastExecutionGetIdentityNameTest() {
Map<String, String> executionProperties = new HashMap<String, String>();
executionProperties.put(ManagedTask.IDENTITY_NAME, IDENTITY_NAME_TEST_ID);

ScheduledFuture sf = TestUtil.getManagedScheduledExecutorService().schedule(
ScheduledFuture sf = scheduledExecutor.schedule(
ManagedExecutors.managedTask(new CounterRunnableTask(), executionProperties, null),
new LogicDrivenTrigger(TestConstants.PollInterval.toMillis(), testname));
TestUtil.waitTillFutureIsDone(sf);
Expand All @@ -96,7 +101,7 @@ public void lastExecutionGetIdentityNameTest() {
@Test
public void lastExecutionGetResultRunnableTest() {
// test with runnable, LastExecution should return null
ScheduledFuture sf = TestUtil.getManagedScheduledExecutorService()
ScheduledFuture sf = scheduledExecutor
.schedule(ManagedExecutors.managedTask(new CounterRunnableTask(), null, null), new LogicDrivenTrigger(
TestConstants.PollInterval.toMillis(), testname));
TestUtil.waitTillFutureIsDone(sf);
Expand All @@ -117,7 +122,7 @@ public void lastExecutionGetResultRunnableTest() {
@Test
public void lastExecutionGetResultCallableTest() {
// test with callable, LastExecution should return 1
ScheduledFuture sf = TestUtil.getManagedScheduledExecutorService().schedule(ManagedExecutors.managedTask(new CounterCallableTask(), null, null),
ScheduledFuture sf = scheduledExecutor.schedule(ManagedExecutors.managedTask(new CounterCallableTask(), null, null),
new LogicDrivenTrigger(TestConstants.PollInterval.toMillis(), testname));
TestUtil.waitTillFutureIsDone(sf);

Expand All @@ -136,7 +141,7 @@ public void lastExecutionGetResultCallableTest() {
*/
@Test
public void lastExecutionGetRunningTimeTest() {
ScheduledFuture sf = TestUtil.getManagedScheduledExecutorService().schedule(ManagedExecutors.managedTask(
ScheduledFuture sf = scheduledExecutor.schedule(ManagedExecutors.managedTask(
new CounterRunnableTask(TestConstants.PollInterval.toMillis()), null, null),
new LogicDrivenTrigger(TestConstants.PollInterval.toMillis(), testname));
TestUtil.waitTillFutureIsDone(sf);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.jupiter.api.Test;

import ee.jakarta.tck.concurrent.framework.TestConstants;
import ee.jakarta.tck.concurrent.framework.TestLogger;
import ee.jakarta.tck.concurrent.framework.TestUtil;
import ee.jakarta.tck.concurrent.framework.junit.anno.Web;
import jakarta.annotation.Resource;
import jakarta.enterprise.concurrent.ManageableThread;
import jakarta.enterprise.concurrent.ManagedThreadFactory;

@Web
public class ManageableThreadTests {
Expand All @@ -39,6 +41,9 @@ public static WebArchive createDeployment() {
return ShrinkWrap.create(WebArchive.class)
.addPackages(true, ManageableThreadTests.class.getPackage());
}

@Resource(lookup = TestConstants.DefaultManagedThreadFactory)
public ManagedThreadFactory threadFactory;

/*
* @testName: isShutdown
Expand All @@ -52,7 +57,7 @@ public static WebArchive createDeployment() {
public void isShutdown() {
boolean pass = false;
try {
ManageableThread m = (ManageableThread) TestUtil.getManagedThreadFactory().newThread(new TestRunnableWork());
ManageableThread m = (ManageableThread) threadFactory.newThread(new TestRunnableWork());
pass = !m.isShutdown();
} catch (Exception e) {
log.warning("Unexpected Exception Caught", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,17 @@
import ee.jakarta.tck.concurrent.common.managed.task.listener.ManagedTaskListenerImpl;
import ee.jakarta.tck.concurrent.common.tasks.CallableTask;
import ee.jakarta.tck.concurrent.common.tasks.RunnableTask;
import ee.jakarta.tck.concurrent.framework.TestConstants;
import ee.jakarta.tck.concurrent.framework.TestLogger;
import ee.jakarta.tck.concurrent.framework.TestUtil;
import ee.jakarta.tck.concurrent.framework.junit.anno.Common;
import ee.jakarta.tck.concurrent.framework.junit.anno.Common.PACKAGE;
import ee.jakarta.tck.concurrent.framework.junit.anno.Web;
import jakarta.annotation.Resource;
import jakarta.enterprise.concurrent.ManagedExecutorService;
import jakarta.enterprise.concurrent.ManagedExecutors;
import jakarta.enterprise.concurrent.ManagedTask;
import jakarta.enterprise.concurrent.ManagedThreadFactory;

@Web
@Common({ PACKAGE.MANAGED_TASK_LISTENER, PACKAGE.TASKS })
Expand All @@ -60,6 +64,12 @@ public static WebArchive createDeployment() {
private ManagedTaskListenerImpl managedTaskListener = new ManagedTaskListenerImpl();

private boolean shutdown = true;

@Resource(lookup = TestConstants.DefaultManagedThreadFactory)
public ManagedThreadFactory threadFactory;

@Resource(lookup = TestConstants.DefaultManagedExecutorService)
public ManagedExecutorService executor;

@AfterEach
public void after() {
Expand Down Expand Up @@ -110,7 +120,7 @@ private void assertListenerComplete(RunnableTask task) {
*/
@Test
public void IsCurrentThreadShutdown() {
Thread createdThread = TestUtil.getManagedThreadFactory().newThread(new Runnable() {
Thread createdThread = threadFactory.newThread(new Runnable() {
@Override
public void run() {
shutdown = ManagedExecutors.isCurrentThreadShutdown();
Expand All @@ -136,7 +146,7 @@ public void run() {
*/
@Test
public void IsCurrentThreadShutdown_ManageableThread() {
Thread createdThread = TestUtil.getManagedThreadFactory().newThread(new Runnable() {
Thread createdThread = threadFactory.newThread(new Runnable() {
@Override
public void run() {
shutdown = ManagedExecutors.isCurrentThreadShutdown();
Expand All @@ -145,7 +155,7 @@ public void run() {
// Executors.newSingleThreadExecutor(managedThreadFactory) uses
// ManagedThreadFactory
// to create new (Manageable) thread.
Future<?> future = Executors.newSingleThreadExecutor(TestUtil.getManagedThreadFactory()).submit(createdThread);
Future<?> future = Executors.newSingleThreadExecutor(threadFactory).submit(createdThread);
TestUtil.waitForTaskComplete(future);
if (shutdown) {
throw new RuntimeException("Failed because shutdown is set to be true when running job");
Expand All @@ -168,7 +178,7 @@ public void run() {
public void ManageRunnableTaskWithTaskListener() {
RunnableTask runnableTask = createRunnableTask();
Runnable taskWithListener = ManagedExecutors.managedTask(runnableTask, managedTaskListener);
Future<?> futureResult = TestUtil.getManagedExecutorService().submit(taskWithListener);
Future<?> futureResult = executor.submit(taskWithListener);
assertTaskAndListenerComplete(futureResult, runnableTask);
}

Expand Down Expand Up @@ -217,7 +227,7 @@ public void ManageRunnableTaskWithTaskListenerAndMap() {
throw new RuntimeException("Failed to get expected property");
}

assertTaskAndListenerComplete(TestUtil.getManagedExecutorService().submit(task), runnableTask);
assertTaskAndListenerComplete(executor.submit(task), runnableTask);
}

/*
Expand Down Expand Up @@ -258,7 +268,7 @@ public void ManageCallableTaskWithTaskListener() {
CallableTask<String> callableTask = createCallableTask(expectedResultStr);
Callable<String> taskWithListener = ManagedExecutors.managedTask((Callable<String>) callableTask,
managedTaskListener);
Future<String> futureResult = TestUtil.getManagedExecutorService().submit(taskWithListener);
Future<String> futureResult = executor.submit(taskWithListener);
assertTaskAndListenerComplete(expectedResultStr, futureResult, callableTask);
}

Expand Down Expand Up @@ -311,7 +321,7 @@ public void ManageCallableTaskWithTaskListenerAndMap() {
if (managedTask.getExecutionProperties().get("key") != "value")
throw new RuntimeException("Failed to get expected property");
}
assertTaskAndListenerComplete(expectedResultStr, TestUtil.getManagedExecutorService().submit(task),
assertTaskAndListenerComplete(expectedResultStr, executor.submit(task),
callableTask);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@
import ee.jakarta.tck.concurrent.common.tasks.CallableTask;
import ee.jakarta.tck.concurrent.common.tasks.CommonTriggers;
import ee.jakarta.tck.concurrent.common.tasks.RunnableTask;
import ee.jakarta.tck.concurrent.framework.TestConstants;
import ee.jakarta.tck.concurrent.framework.TestUtil;
import ee.jakarta.tck.concurrent.framework.junit.anno.Common;
import ee.jakarta.tck.concurrent.framework.junit.anno.Common.PACKAGE;
import ee.jakarta.tck.concurrent.framework.junit.anno.Web;
import jakarta.annotation.Resource;
import jakarta.enterprise.concurrent.ManagedScheduledExecutorService;

@Web
@Common({ PACKAGE.TASKS })
Expand All @@ -51,6 +54,9 @@ public static WebArchive createDeployment() {

private static final String TEST_CLASSLOADER_CLASS_NAME = ManagedScheduledExecutorServiceTests.class
.getCanonicalName();

@Resource(lookup = TestConstants.DefaultManagedScheduledExecutorService)
public ManagedScheduledExecutorService scheduledExecutor;

/*
* @testName: normalScheduleProcess1Test
Expand All @@ -64,7 +70,7 @@ public static WebArchive createDeployment() {
*/
@Test
public void normalScheduleProcess1Test() throws Exception {
ScheduledFuture result = TestUtil.getManagedScheduledExecutorService().schedule(
ScheduledFuture result = scheduledExecutor.schedule(
new RunnableTask(TEST_JNDI_EVN_ENTRY_JNDI_NAME, TEST_JNDI_EVN_ENTRY_VALUE, TEST_CLASSLOADER_CLASS_NAME),
new CommonTriggers.OnceTrigger());
TestUtil.waitForTaskComplete(result);
Expand All @@ -87,7 +93,7 @@ public void nullCommandScheduleProcessTest() {
Runnable command = null;

try {
TestUtil.getManagedScheduledExecutorService().schedule(command, new CommonTriggers.OnceTrigger());
scheduledExecutor.schedule(command, new CommonTriggers.OnceTrigger());
} catch (NullPointerException e) {
return; // expected
}
Expand All @@ -108,7 +114,7 @@ public void nullCommandScheduleProcessTest() {
*/
@Test
public void normalScheduleProcess2Test() throws Exception {
ScheduledFuture result = TestUtil.getManagedScheduledExecutorService()
ScheduledFuture result = scheduledExecutor
.schedule(
(Callable) new CallableTask(TEST_JNDI_EVN_ENTRY_JNDI_NAME, TEST_JNDI_EVN_ENTRY_VALUE,
TEST_CLASSLOADER_CLASS_NAME, CALLABLETESTTASK1_RUN_RESULT),
Expand Down Expand Up @@ -136,7 +142,7 @@ public void nullCallableScheduleProcessTest() {
Callable callable = null;

try {
TestUtil.getManagedScheduledExecutorService().schedule(callable, new CommonTriggers.OnceTrigger());
scheduledExecutor.schedule(callable, new CommonTriggers.OnceTrigger());
} catch (NullPointerException e) {
return; // expected
}
Expand Down
Loading