Skip to content

Commit

Permalink
Tests for dependent context
Browse files Browse the repository at this point in the history
git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@196 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
pmuir committed Oct 28, 2008
1 parent 0e5fe0a commit 1c6755a
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 57 deletions.
9 changes: 4 additions & 5 deletions webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java
Expand Up @@ -60,19 +60,19 @@ public List<Context> get(Class<? extends Annotation> key)

private Set<Bean<?>> beans;

public ManagerImpl(List<Class<? extends Annotation>> enabledDeploymentTypes)
public ManagerImpl()
{
contextMap = new ContextMap();
// TODO Are there any contexts that should be initialized here?
initEnabledDeploymentTypes(enabledDeploymentTypes);
initEnabledDeploymentTypes(null);
this.modelManager = new ModelManager();
this.ejbLookupManager = new EjbManager();
this.beans = new HashSet<Bean<?>>();
this.eventBus = new EventBus();
this.resolutionManager = new ResolutionManager(this);
}

private void initEnabledDeploymentTypes(List<Class<? extends Annotation>> enabledDeploymentTypes)
protected void initEnabledDeploymentTypes(List<Class<? extends Annotation>> enabledDeploymentTypes)
{
this.enabledDeploymentTypes = new ArrayList<Class<? extends Annotation>>();
if (enabledDeploymentTypes == null)
Expand Down Expand Up @@ -170,7 +170,6 @@ public Manager addContext(Context context)
contextMap.put(context.getScopeType(), sameScopeTypeContexts);
}
sameScopeTypeContexts.add(context);
// TODO: why manager? fluent interfaces?
return this;
}

Expand Down Expand Up @@ -218,7 +217,7 @@ public Context getContext(Class<? extends Annotation> scopeType)
List<Context> contexts = contextMap.get(scopeType);
if (contexts == null)
{
throw new ContextNotActiveException("No contexts for " + scopeType.getName());
throw new ContextNotActiveException("No active contexts for " + scopeType.getName());
}
// TODO performance? Just flag found=true and continue loop, failing when
// found=already true etc?
Expand Down
Expand Up @@ -62,7 +62,7 @@ public void destroy(Manager manager)
{
destroy(manager, bean);
}
beans = null;
beans = new BeanMap();
}

}
49 changes: 35 additions & 14 deletions webbeans-ri/src/test/java/org/jboss/webbeans/test/AbstractTest.java
Expand Up @@ -7,7 +7,10 @@
import javax.webbeans.Production;
import javax.webbeans.Standard;

import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.contexts.ApplicationContext;
import org.jboss.webbeans.contexts.DependentContext;
import org.jboss.webbeans.contexts.RequestContext;
import org.jboss.webbeans.contexts.SessionContext;
import org.jboss.webbeans.model.StereotypeModel;
import org.jboss.webbeans.test.annotations.AnimalStereotype;
import org.jboss.webbeans.test.annotations.AnotherDeploymentType;
Expand All @@ -23,31 +26,49 @@
public class AbstractTest
{

protected ManagerImpl manager;
protected MockManagerImpl manager;


@BeforeMethod
public void before()
public final void before()
{
manager = new MockManagerImpl();
init();
}

protected void init()
{
addStereotypes();
addBuiltInContexts();
addEnabledDeploymentTypes();
}

protected void addEnabledDeploymentTypes()
{

List<Class<? extends Annotation>> enabledDeploymentTypes = new ArrayList<Class<? extends Annotation>>();
enabledDeploymentTypes.add(Standard.class);
enabledDeploymentTypes.add(Production.class);
enabledDeploymentTypes.add(AnotherDeploymentType.class);
enabledDeploymentTypes.add(HornedAnimalDeploymentType.class);
manager = new MockManagerImpl(enabledDeploymentTypes);

initStereotypes(manager);
manager.setEnabledDeploymentTypes(enabledDeploymentTypes);
}

protected void addStereotypes()
{
manager.getModelManager().addStereotype(new StereotypeModel<AnimalStereotype>(AnimalStereotype.class));
manager.getModelManager().addStereotype(new StereotypeModel<HornedMammalStereotype>(HornedMammalStereotype.class));
manager.getModelManager().addStereotype(new StereotypeModel<MammalStereotype>(MammalStereotype.class));
manager.getModelManager().addStereotype(new StereotypeModel<FishStereotype>(FishStereotype.class));
manager.getModelManager().addStereotype(new StereotypeModel<RiverFishStereotype>(RiverFishStereotype.class));
manager.getModelManager().addStereotype(new StereotypeModel<RequestScopedAnimalStereotype>(RequestScopedAnimalStereotype.class));
}

private void initStereotypes(ManagerImpl container)
protected void addBuiltInContexts()
{
container.getModelManager().addStereotype(new StereotypeModel<AnimalStereotype>(AnimalStereotype.class));
container.getModelManager().addStereotype(new StereotypeModel<HornedMammalStereotype>(HornedMammalStereotype.class));
container.getModelManager().addStereotype(new StereotypeModel<MammalStereotype>(MammalStereotype.class));
container.getModelManager().addStereotype(new StereotypeModel<FishStereotype>(FishStereotype.class));
container.getModelManager().addStereotype(new StereotypeModel<RiverFishStereotype>(RiverFishStereotype.class));
container.getModelManager().addStereotype(new StereotypeModel<RequestScopedAnimalStereotype>(RequestScopedAnimalStereotype.class));
manager.addContext(new DependentContext());
manager.addContext(new RequestContext());
manager.addContext(new SessionContext());
manager.addContext(new ApplicationContext());
}

}
Expand Up @@ -10,7 +10,6 @@
import javax.webbeans.Observes;
import javax.webbeans.Standard;

import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.event.DeferredEventNotification;
import org.jboss.webbeans.injectable.InjectableMethod;
import org.jboss.webbeans.introspector.AnnotatedType;
Expand All @@ -20,7 +19,6 @@
import org.jboss.webbeans.test.annotations.Asynchronous;
import org.jboss.webbeans.test.beans.Tuna;
import org.jboss.webbeans.test.bindings.AsynchronousAnnotationLiteral;
import org.jboss.webbeans.test.mock.MockManagerImpl;
import org.jboss.webbeans.test.mock.MockObserverImpl;
import org.testng.annotations.Test;

Expand All @@ -38,6 +36,15 @@ public class Event
{
// Simple class used for testing
}

@Override
protected void addEnabledDeploymentTypes()
{
List<Class<? extends Annotation>> enabledDeploymentTypes = new ArrayList<Class<? extends Annotation>>();
enabledDeploymentTypes.add(Standard.class);
enabledDeploymentTypes.add(AnotherDeploymentType.class);
manager.setEnabledDeploymentTypes(enabledDeploymentTypes);
}

public class AnObserver
{
Expand All @@ -61,13 +68,9 @@ public final void testBeforeCompletion() throws Exception
// When the transaction is committed, the beforeCompletion() method is
// invoked which in turn invokes the observer. Here the mock observer
// is used to keep track of the event being fired.
ManagerImpl manager;
SimpleBeanModel<Tuna> tuna;
InjectableMethod<Object> om;
List<Class<? extends Annotation>> enabledDeploymentTypes = new ArrayList<Class<? extends Annotation>>();
enabledDeploymentTypes.add(Standard.class);
enabledDeploymentTypes.add(AnotherDeploymentType.class);
manager = new MockManagerImpl(enabledDeploymentTypes);


// Create an observer with known binding types
Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>();
Expand Down
@@ -1,11 +1,19 @@
package org.jboss.webbeans.test;

import static org.jboss.webbeans.test.util.Util.createSimpleWebBean;

import javax.webbeans.ContextNotActiveException;
import javax.webbeans.Dependent;
import javax.webbeans.manager.Bean;

import org.jboss.webbeans.contexts.DependentContext;
import org.jboss.webbeans.test.beans.Fox;
import org.testng.annotations.Test;

public class DependentContextTest extends AbstractTest
{

@Test(groups="contexts") @SpecAssertion(section="8.3")
@Test(groups={"contexts", "injection"}) @SpecAssertion(section="8.3")
public void testInstanceNotSharedBetweenInjectionPoints()
{
assert false;
Expand All @@ -32,19 +40,28 @@ public void testInstanceUsedForObserverMethodNotShared()
@Test(groups="contexts") @SpecAssertion(section="8.3")
public void testContextGetWithCreateTrueReturnsNewInstance()
{
assert false;
Bean<Fox> foxBean = createSimpleWebBean(Fox.class, manager);
manager.addBean(foxBean);
DependentContext context = new DependentContext();
context.setActive(true);
assert context.get(foxBean, true) != null;
assert context.get(foxBean, true) instanceof Fox;
}

@Test(groups="contexts") @SpecAssertion(section="8.3")
public void testContextGetWithCreateFalseReturnsNull()
{
assert false;
Bean<Fox> foxBean = createSimpleWebBean(Fox.class, manager);
manager.addBean(foxBean);
DependentContext context = new DependentContext();
context.setActive(true);
assert context.get(foxBean, false) == null;
}

@Test(groups="contexts") @SpecAssertion(section="8.3")
@Test(groups="contexts", expectedExceptions=ContextNotActiveException.class) @SpecAssertion(section="8.3")
public void testContextIsInactive()
{
assert false;
manager.getContext(Dependent.class).isActive();
}

@Test(groups={"contexts", "producerMethod"}) @SpecAssertion(section="8.3")
Expand Down Expand Up @@ -108,7 +125,7 @@ public void testServletBeanMayMayCreateInstanceFromInitializer()
assert false;
}

@Test(groups={"contexts"}) @SpecAssertion(section="8.3")
@Test(groups={"contexts", "beanLifecycle"}) @SpecAssertion(section="8.3")
public void testDestroyingParentDestroysDependents()
{
assert false;
Expand All @@ -126,7 +143,7 @@ public void testDestroyingServletDestroysDependents()
assert false;
}

@Test(groups={"contexts"}) @SpecAssertion(section="8.3")
@Test(groups={"contexts", "el"}) @SpecAssertion(section="8.3")
public void testDependentsDestroyedWhenElEvaluationCompletes()
{
assert false;
Expand Down
Expand Up @@ -16,7 +16,6 @@
import javax.webbeans.Standard;
import javax.webbeans.manager.Bean;

import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.introspector.AnnotatedType;
import org.jboss.webbeans.introspector.SimpleAnnotatedType;
import org.jboss.webbeans.model.bean.SimpleBeanModel;
Expand All @@ -32,7 +31,6 @@
import org.jboss.webbeans.test.beans.broken.Gazelle;
import org.jboss.webbeans.test.bindings.AnotherDeploymentTypeAnnotationLiteral;
import org.jboss.webbeans.test.bindings.FishStereotypeAnnotationLiteral;
import org.jboss.webbeans.test.mock.MockManagerImpl;
import org.testng.annotations.Test;

@SpecVersion("PDR")
Expand Down Expand Up @@ -124,10 +122,10 @@ public void testBeanWithDisabledDeploymentTypeNotInstantiated()
@Test @SpecAssertion(section={"2.5.6", "2.5.7"})
public void testDefaultEnabledDeploymentTypes()
{
ManagerImpl container = new MockManagerImpl(null);
assert container.getEnabledDeploymentTypes().size() == 2;
assert container.getEnabledDeploymentTypes().get(0).equals(Standard.class);
assert container.getEnabledDeploymentTypes().get(1).equals(Production.class);
manager.setEnabledDeploymentTypes(null);
assert manager.getEnabledDeploymentTypes().size() == 2;
assert manager.getEnabledDeploymentTypes().get(0).equals(Standard.class);
assert manager.getEnabledDeploymentTypes().get(1).equals(Production.class);
}

@Test @SpecAssertion(section={"2.5.6", "2.5.7"})
Expand All @@ -137,11 +135,11 @@ public void testCustomDeploymentTypes()
enabledDeploymentTypes.add(Standard.class);
enabledDeploymentTypes.add(AnotherDeploymentType.class);
enabledDeploymentTypes.add(HornedAnimalDeploymentType.class);
ManagerImpl container = new MockManagerImpl(enabledDeploymentTypes);
assert container.getEnabledDeploymentTypes().size() == 3;
assert container.getEnabledDeploymentTypes().get(0).equals(Standard.class);
assert container.getEnabledDeploymentTypes().get(1).equals(AnotherDeploymentType.class);
assert container.getEnabledDeploymentTypes().get(2).equals(HornedAnimalDeploymentType.class);
manager.setEnabledDeploymentTypes(enabledDeploymentTypes);
assert manager.getEnabledDeploymentTypes().size() == 3;
assert manager.getEnabledDeploymentTypes().get(0).equals(Standard.class);
assert manager.getEnabledDeploymentTypes().get(1).equals(AnotherDeploymentType.class);
assert manager.getEnabledDeploymentTypes().get(2).equals(HornedAnimalDeploymentType.class);
}

@Test(expectedExceptions=DeploymentException.class) @SpecAssertion(section="2.5.6")
Expand All @@ -150,7 +148,7 @@ public void testStandardMustBeDeclared()
List<Class<? extends Annotation>> enabledDeploymentTypes = new ArrayList<Class<? extends Annotation>>();
enabledDeploymentTypes.add(AnotherDeploymentType.class);
enabledDeploymentTypes.add(HornedAnimalDeploymentType.class);
new MockManagerImpl(enabledDeploymentTypes);
manager.setEnabledDeploymentTypes(enabledDeploymentTypes);
}

@Test(groups="webbeansxml", expectedExceptions=DeploymentException.class) @SpecAssertion(section="2.5.6")
Expand Down
Expand Up @@ -39,7 +39,8 @@ public void before() throws Exception
List<Class<? extends Annotation>> enabledDeploymentTypes = new ArrayList<Class<? extends Annotation>>();
enabledDeploymentTypes.add(Standard.class);
enabledDeploymentTypes.add(AnotherDeploymentType.class);
manager = new MockManagerImpl(enabledDeploymentTypes);
manager = new MockManagerImpl();
manager.setEnabledDeploymentTypes(enabledDeploymentTypes);
Field eventModelField = this.getClass().getDeclaredField("eventModelField");
eventBeanModel = new EventBeanModel<EventImpl<DangerCall>>(
new SimpleAnnotatedField<EventImpl<DangerCall>>(eventModelField),
Expand Down
Expand Up @@ -54,7 +54,8 @@ public void before() throws Exception
List<Class<? extends Annotation>> enabledDeploymentTypes = new ArrayList<Class<? extends Annotation>>();
enabledDeploymentTypes.add(Standard.class);
enabledDeploymentTypes.add(AnotherDeploymentType.class);
manager = new MockManagerImpl(enabledDeploymentTypes);
manager = new MockManagerImpl();
manager.setEnabledDeploymentTypes(enabledDeploymentTypes);
}

/**
Expand Down
Expand Up @@ -12,6 +12,12 @@
public class ManagerTest extends AbstractTest
{

@Override
protected void addBuiltInContexts()
{
// No -op, done manually here
}

@Test(groups={"manager", "injection"}) @SpecAssertion(section="4.8")
public void testInjectingManager()
{
Expand Down
Expand Up @@ -10,7 +10,6 @@
import javax.webbeans.Observes;
import javax.webbeans.Standard;

import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.injectable.InjectableMethod;
import org.jboss.webbeans.introspector.AnnotatedType;
import org.jboss.webbeans.introspector.SimpleAnnotatedType;
Expand All @@ -33,7 +32,7 @@
@SpecVersion("20081024-PDR")
public class ObserverTest
{
private ManagerImpl manager;
private MockManagerImpl manager;
private SimpleBeanModel<Tuna> tuna;
private InjectableMethod<?> om;

Expand All @@ -59,7 +58,8 @@ public void before() throws Exception
List<Class<? extends Annotation>> enabledDeploymentTypes = new ArrayList<Class<? extends Annotation>>();
enabledDeploymentTypes.add(Standard.class);
enabledDeploymentTypes.add(AnotherDeploymentType.class);
manager = new MockManagerImpl(enabledDeploymentTypes);
manager = new MockManagerImpl();
manager.setEnabledDeploymentTypes(enabledDeploymentTypes);

// Create an observer with known binding types
Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>();
Expand Down
@@ -0,0 +1,9 @@
package org.jboss.webbeans.test.beans;

import javax.webbeans.Dependent;

@Dependent
public class Fox
{

}
Expand Up @@ -19,11 +19,6 @@ public class MockManagerImpl extends ManagerImpl
private Annotation[] eventBindings = null;
private Observer<?> observer = null;

public MockManagerImpl(List<Class<? extends Annotation>> enabledDeploymentTypes)
{
super(enabledDeploymentTypes);
}

/* (non-Javadoc)
* @see org.jboss.webbeans.ManagerImpl#fireEvent(java.lang.Object, java.lang.annotation.Annotation[])
*/
Expand Down Expand Up @@ -97,5 +92,10 @@ public final Observer<?> getObserver()
{
return observer;
}

public void setEnabledDeploymentTypes(List<Class<? extends Annotation>> enabledDeploymentTypes)
{
initEnabledDeploymentTypes(enabledDeploymentTypes);
}

}

0 comments on commit 1c6755a

Please sign in to comment.