Skip to content

Commit

Permalink
Remove circular deps from CDI test.
Browse files Browse the repository at this point in the history
  • Loading branch information
manovotn committed Sep 10, 2019
1 parent 05b6b1e commit 899c481
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public static WebArchive createDeployment() {

return ShrinkWrap.create(WebArchive.class, BasicCDITest.class.getSimpleName() + ".war")
.addClass(CDIBean.class)
.addClass(CdiBeanProducer.class)
.addClass(BasicCDITest.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")
.addAsLibraries(fakeContextProviders);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package org.eclipse.microprofile.context.tck.cdi;

import static org.eclipse.microprofile.context.tck.contexts.priority.spi.ThreadPriorityContextProvider.THREAD_PRIORITY;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.*;

Expand All @@ -30,7 +29,6 @@
import java.util.concurrent.TimeUnit;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.inject.Qualifier;

Expand All @@ -53,24 +51,13 @@ public class CDIBean {
@Inject @AppProducedExecutor
ManagedExecutor appProduced;

@Produces @ApplicationScoped @AppProducedExecutor
public ManagedExecutor createExec() {
return ManagedExecutor.builder().cleared(ThreadContext.TRANSACTION).propagated(ThreadContext.ALL_REMAINING).build();
}

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER })
public @interface LabelContextPropagator {}

@Produces @ApplicationScoped @LabelContextPropagator
ThreadContext labelContextPropagator1 = ThreadContext.builder().propagated(Label.CONTEXT_NAME)
.unchanged()
.cleared(ThreadContext.ALL_REMAINING)
.build();

@Inject @LabelContextPropagator
ThreadContext labelContextPropagator2;
ThreadContext labelContextPropagator;

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
Expand All @@ -85,27 +72,6 @@ public ManagedExecutor createExec() {
@Inject @Priority3Executor
Executor priority3Executor;

@Produces @ApplicationScoped @Priority3Executor
public Executor createPriority3Executor(@PriorityContext ThreadContext ctx) {
int originalPriority = Thread.currentThread().getPriority();
try {
Thread.currentThread().setPriority(3);
Label.set("do-not-propagate-this-label");
Buffer.set(new StringBuffer("do-not-propagate-this-buffer"));

return ctx.currentContextExecutor();
}
finally {
// restore previous values
Buffer.set(null);
Label.set(null);
Thread.currentThread().setPriority(originalPriority);
}
}

@Produces @ApplicationScoped @PriorityContext
ThreadContext threadPriorityContext = ThreadContext.builder().propagated(THREAD_PRIORITY).build();

/**
* Extra sanity check test to verify injection is occurring. However, if CDI is
* set up properly, this bean should not even be reachable if injection fails.
Expand All @@ -127,9 +93,7 @@ public void testBasicExecutorUsable() throws Exception {
* Application can provide producers of ThreadContext that are qualified.
*/
public void testAppDefinedProducerOfThreadContext() {
Assert.assertNotNull(labelContextPropagator1,
"Application should be able to use Produces to define its own producer of ThreadContext.");
Assert.assertNotNull(labelContextPropagator2,
Assert.assertNotNull(labelContextPropagator,
"Application should be able to use qualifier to obtain produced instance of ThreadContext.");

int originalPriority = Thread.currentThread().getPriority();
Expand All @@ -139,7 +103,7 @@ public void testAppDefinedProducerOfThreadContext() {
Label.set("testAppDefinedProducerOfThreadContext-label");
Buffer.set(new StringBuffer("testAppDefinedProducerOfThreadContext-buffer"));

Runnable testLabelContext = labelContextPropagator2.contextualRunnable(() -> {
Runnable testLabelContext = labelContextPropagator.contextualRunnable(() -> {
Assert.assertEquals(Label.get(), "testAppDefinedProducerOfThreadContext-label",
"Thread context type was not propagated.");
Assert.assertEquals(Buffer.get().toString(), "",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.eclipse.microprofile.context.tck.cdi;

import org.eclipse.microprofile.context.ManagedExecutor;
import org.eclipse.microprofile.context.ThreadContext;
import org.eclipse.microprofile.context.tck.contexts.buffer.Buffer;
import org.eclipse.microprofile.context.tck.contexts.label.Label;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import java.util.concurrent.Executor;

import static org.eclipse.microprofile.context.tck.contexts.priority.spi.ThreadPriorityContextProvider.THREAD_PRIORITY;

@ApplicationScoped
public class CdiBeanProducer {

@Produces
@ApplicationScoped
@CDIBean.AppProducedExecutor
public ManagedExecutor createExec() {
return ManagedExecutor.builder().cleared(ThreadContext.TRANSACTION).propagated(ThreadContext.ALL_REMAINING).build();
}

@Produces
@ApplicationScoped
@CDIBean.LabelContextPropagator
ThreadContext labelContextPropagator1 = ThreadContext.builder().propagated(Label.CONTEXT_NAME)
.unchanged()
.cleared(ThreadContext.ALL_REMAINING)
.build();

@Produces
@ApplicationScoped
@CDIBean.Priority3Executor
public Executor createPriority3Executor(@CDIBean.PriorityContext ThreadContext ctx) {
int originalPriority = Thread.currentThread().getPriority();
try {
Thread.currentThread().setPriority(3);
Label.set("do-not-propagate-this-label");
Buffer.set(new StringBuffer("do-not-propagate-this-buffer"));

return ctx.currentContextExecutor();
} finally {
// restore previous values
Buffer.set(null);
Label.set(null);
Thread.currentThread().setPriority(originalPriority);
}
}

@Produces
@ApplicationScoped
@CDIBean.PriorityContext
ThreadContext threadPriorityContext = ThreadContext.builder().propagated(THREAD_PRIORITY).build();
}

0 comments on commit 899c481

Please sign in to comment.