Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WELD-1766 Differentiate between caching and non-caching client proxies
- Loading branch information
Showing
8 changed files
with
291 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
tests-arquillian/src/test/java/org/jboss/weld/tests/proxy/weld1766/CustomContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source | ||
* Copyright 2012, Red Hat, Inc., and individual contributors | ||
* by the @authors tag. See the copyright.txt in the distribution for a | ||
* full listing of individual contributors. | ||
* | ||
* 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.jboss.weld.tests.proxy.weld1766; | ||
|
||
import javax.enterprise.context.spi.Context; | ||
import javax.enterprise.context.spi.Contextual; | ||
import javax.enterprise.context.spi.CreationalContext; | ||
import java.lang.annotation.Annotation; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
|
||
public class CustomContext implements Context { | ||
|
||
private boolean active = false; | ||
|
||
private static class Instance { | ||
private final Object instance; | ||
private final CreationalContext<?> ctx; | ||
|
||
public Instance(Object instance, CreationalContext<?> ctx) { | ||
this.instance = instance; | ||
this.ctx = ctx; | ||
} | ||
} | ||
|
||
private final Map<Contextual<?>, Instance> storage = new ConcurrentHashMap<Contextual<?>, Instance>(); | ||
|
||
@Override | ||
public Class<? extends Annotation> getScope() { | ||
return CustomScoped.class; | ||
} | ||
|
||
@Override | ||
public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext) { | ||
T instance = get(contextual); | ||
if (instance == null) { | ||
storage.put(contextual, new Instance(contextual.create(creationalContext), creationalContext)); | ||
instance = get(contextual); | ||
} | ||
return instance; | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public <T> T get(Contextual<T> contextual) { | ||
Instance instance = storage.get(contextual); | ||
if (instance != null) { | ||
return (T) instance.instance; | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public boolean isActive() { | ||
return active; | ||
} | ||
|
||
public void activate() { | ||
active = true; | ||
} | ||
|
||
public void deactivate() { | ||
active = false; | ||
for (Map.Entry<Contextual<?>, Instance> contextualInstanceEntry : storage.entrySet()) { | ||
Contextual contextual = contextualInstanceEntry.getKey(); | ||
Instance instance = contextualInstanceEntry.getValue(); | ||
contextual.destroy(instance.instance, instance.ctx); | ||
} | ||
storage.clear(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
tests-arquillian/src/test/java/org/jboss/weld/tests/proxy/weld1766/CustomScopeExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source | ||
* Copyright 2012, Red Hat, Inc., and individual contributors | ||
* by the @authors tag. See the copyright.txt in the distribution for a | ||
* full listing of individual contributors. | ||
* | ||
* 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.jboss.weld.tests.proxy.weld1766; | ||
|
||
import javax.enterprise.event.Observes; | ||
import javax.enterprise.inject.spi.AfterBeanDiscovery; | ||
import javax.enterprise.inject.spi.Extension; | ||
|
||
public class CustomScopeExtension implements Extension { | ||
|
||
private final CustomContext context = new CustomContext(); | ||
|
||
void registerContext(@Observes AfterBeanDiscovery event) { | ||
event.addContext(context); | ||
} | ||
|
||
public CustomContext getContext() { | ||
return context; | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
tests-arquillian/src/test/java/org/jboss/weld/tests/proxy/weld1766/CustomScoped.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source | ||
* Copyright 2012, Red Hat, Inc., and individual contributors | ||
* by the @authors tag. See the copyright.txt in the distribution for a | ||
* full listing of individual contributors. | ||
* | ||
* 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.jboss.weld.tests.proxy.weld1766; | ||
|
||
import javax.enterprise.context.NormalScope; | ||
import java.lang.annotation.Inherited; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import static java.lang.annotation.ElementType.*; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
@NormalScope(passivating = true) | ||
@Inherited | ||
@Target({ TYPE, METHOD, FIELD }) | ||
@Retention(RUNTIME) | ||
public @interface CustomScoped { | ||
|
||
} |
87 changes: 87 additions & 0 deletions
87
tests-arquillian/src/test/java/org/jboss/weld/tests/proxy/weld1766/ProducerProxyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source | ||
* Copyright 2012, Red Hat, Inc., and individual contributors | ||
* by the @authors tag. See the copyright.txt in the distribution for a | ||
* full listing of individual contributors. | ||
* | ||
* 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.jboss.weld.tests.proxy.weld1766; | ||
|
||
import junit.framework.Assert; | ||
import org.jboss.arquillian.container.test.api.Deployment; | ||
import org.jboss.arquillian.junit.Arquillian; | ||
import org.jboss.shrinkwrap.api.Archive; | ||
import org.jboss.shrinkwrap.api.BeanArchive; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import javax.enterprise.context.RequestScoped; | ||
import javax.enterprise.inject.Produces; | ||
import javax.enterprise.inject.spi.BeanManager; | ||
import javax.enterprise.inject.spi.Extension; | ||
import javax.inject.Inject; | ||
|
||
/** | ||
* Tests for https://issues.jboss.org/browse/CDI-9999 | ||
* | ||
* @author Marcel Kolsteren | ||
* | ||
*/ | ||
@RunWith(Arquillian.class) | ||
public class ProducerProxyTest { | ||
|
||
@Deployment | ||
public static Archive<?> getDeployment() { | ||
return ShrinkWrap.create(BeanArchive.class).addPackage(ProducerProxyTest.class.getPackage()) | ||
.addAsServiceProvider(Extension.class, CustomScopeExtension.class); | ||
} | ||
|
||
@Inject | ||
private BeanManager manager; | ||
|
||
@Inject | ||
@Qualifier1 | ||
private TestComponent requestScopedComponent; | ||
|
||
@Inject | ||
@Qualifier2 | ||
private TestComponent customScopedComponent; | ||
|
||
@Produces | ||
@Qualifier1 | ||
@RequestScoped | ||
public TestComponent produceRequestScopedComponent() { | ||
return new TestComponent(); | ||
} | ||
|
||
@Produces | ||
@Qualifier2 | ||
@CustomScoped | ||
public TestComponent produceCustomScopedComponent() { | ||
return new TestComponent(); | ||
} | ||
|
||
@Inject | ||
private CustomScopeExtension customScopeExtension; | ||
|
||
@Test | ||
public void testCustomScopedComponent() { | ||
customScopeExtension.getContext().activate(); | ||
customScopedComponent.setValue("test"); | ||
customScopeExtension.getContext().deactivate(); | ||
|
||
customScopeExtension.getContext().activate(); | ||
Assert.assertNull(customScopedComponent.getValue()); | ||
customScopeExtension.getContext().deactivate(); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
tests-arquillian/src/test/java/org/jboss/weld/tests/proxy/weld1766/Qualifier1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.jboss.weld.tests.proxy.weld1766; | ||
|
||
import javax.inject.Qualifier; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import static java.lang.annotation.ElementType.*; | ||
|
||
@Qualifier | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ METHOD, FIELD, PARAMETER, TYPE }) | ||
public @interface Qualifier1 { | ||
} |
14 changes: 14 additions & 0 deletions
14
tests-arquillian/src/test/java/org/jboss/weld/tests/proxy/weld1766/Qualifier2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.jboss.weld.tests.proxy.weld1766; | ||
|
||
import javax.inject.Qualifier; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import static java.lang.annotation.ElementType.*; | ||
|
||
@Qualifier | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ METHOD, FIELD, PARAMETER, TYPE }) | ||
public @interface Qualifier2 { | ||
} |
15 changes: 15 additions & 0 deletions
15
tests-arquillian/src/test/java/org/jboss/weld/tests/proxy/weld1766/TestComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.jboss.weld.tests.proxy.weld1766; | ||
|
||
import java.io.Serializable; | ||
|
||
public class TestComponent implements Serializable { | ||
private String value; | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
|
||
public void setValue(String value) { | ||
this.value = value; | ||
} | ||
} |