You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@composer.apache.org by ha...@apache.org on 2008/01/21 06:41:50 UTC
svn commit: r613775 [9/15] - in /incubator/composer: ./ core/ core/src/
core/src/java/ core/src/java/org/ core/src/java/org/apache/
core/src/java/org/apache/composer/ core/src/java/org/apache/composer/core/
core/src/java/org/apache/composer/core/adapte...
Added: incubator/composer/core/src/test/org/apache/composer/core/adapters/InstanceAdapterTestCase.java
URL: http://svn.apache.org/viewvc/incubator/composer/core/src/test/org/apache/composer/core/adapters/InstanceAdapterTestCase.java?rev=613775&view=auto
==============================================================================
--- incubator/composer/core/src/test/org/apache/composer/core/adapters/InstanceAdapterTestCase.java (added)
+++ incubator/composer/core/src/test/org/apache/composer/core/adapters/InstanceAdapterTestCase.java Sun Jan 20 22:41:26 2008
@@ -0,0 +1,182 @@
+/*****************************************************************************
+ * Copyright (C) PicoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ * Original code by Joerg Schaible *
+ *****************************************************************************/
+package org.apache.composer.core.adapters;
+
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.fail;
+
+import java.util.Map;
+
+import org.junit.Test;
+import org.apache.composer.core.ComponentAdapter;
+import org.apache.composer.core.DefaultContainer;
+import org.apache.composer.core.Disposable;
+import org.apache.composer.core.MutableContainer;
+import org.apache.composer.core.Container;
+import org.apache.composer.core.Startable;
+import org.apache.composer.core.lifecycle.NullLifecycleStrategy;
+import org.apache.composer.core.lifecycle.StartableLifecycleStrategy;
+import org.apache.composer.core.monitors.NullComponentMonitor;
+import org.apache.composer.core.tck.AbstractComponentAdapterTest;
+import org.apache.composer.core.testmodel.NullLifecycle;
+import org.apache.composer.core.testmodel.SimpleTouchable;
+import org.apache.composer.core.testmodel.Touchable;
+
+
+/**
+ * Test the InstanceAdapter.
+ *
+ * @author Jörg Schaible
+ */
+public final class InstanceAdapterTestCase extends AbstractComponentAdapterTest {
+
+ @Test public void testComponentAdapterReturnsSame() {
+ final Touchable touchable = new SimpleTouchable();
+ final ComponentAdapter componentAdapter = new InstanceAdapter(Touchable.class, touchable, new NullLifecycleStrategy(),
+ new NullComponentMonitor());
+ assertSame(touchable, componentAdapter.getComponentInstance(null));
+ }
+
+ @Test public void testDefaultLifecycleStrategy() {
+ LifecycleComponent component = new LifecycleComponent();
+ InstanceAdapter adapter =
+ new InstanceAdapter(LifecycleComponent.class, component, new StartableLifecycleStrategy(new NullComponentMonitor()),
+ new NullComponentMonitor());
+ Container pico = new DefaultContainer();
+ adapter.start(pico);
+ adapter.stop(pico);
+ adapter.dispose(pico);
+ assertEquals("start>stop>dispose>", component.buffer.toString());
+ adapter.start(component);
+ adapter.stop(component);
+ adapter.dispose(component);
+ assertEquals("start>stop>dispose>start>stop>dispose>", component.buffer.toString());
+ }
+
+ private static final class LifecycleComponent implements Startable, Disposable {
+ final StringBuffer buffer = new StringBuffer();
+
+ public void start() {
+ buffer.append("start>");
+ }
+
+ public void stop() {
+ buffer.append("stop>");
+ }
+
+ public void dispose() {
+ buffer.append("dispose>");
+ }
+ }
+
+ @Test public void testCustomLifecycleCanBeInjected() {
+ NullLifecycle component = new NullLifecycle();
+ RecordingLifecycleStrategy strategy = new RecordingLifecycleStrategy(new StringBuffer());
+ InstanceAdapter adapter = new InstanceAdapter(NullLifecycle.class, component, strategy, new NullComponentMonitor());
+ Container pico = new DefaultContainer();
+ adapter.start(pico);
+ adapter.stop(pico);
+ adapter.dispose(pico);
+ assertEquals("<start<stop<dispose", strategy.recording());
+ adapter.start(component);
+ adapter.stop(component);
+ adapter.dispose(component);
+ assertEquals("<start<stop<dispose<start<stop<dispose", strategy.recording());
+ }
+
+ @Test public void testComponentAdapterCanIgnoreLifecycle() {
+ final Touchable touchable = new SimpleTouchable();
+ InstanceAdapter adapter = new InstanceAdapter(Touchable.class, touchable, new NullLifecycleStrategy(),
+ new NullComponentMonitor());
+ Container pico = new DefaultContainer();
+ adapter.start(pico);
+ adapter.stop(pico);
+ adapter.dispose(pico);
+ adapter.start(touchable);
+ adapter.stop(touchable);
+ adapter.dispose(touchable);
+ }
+
+ @Test public void testGuardAgainstNullInstance() {
+ try {
+ new InstanceAdapter(Map.class, null, new NullLifecycleStrategy(),
+ new NullComponentMonitor());
+ fail("should have barfed");
+ } catch (NullPointerException e) {
+ assertEquals("componentInstance cannot be null", e.getMessage());
+ }
+ }
+
+
+ /**
+ * {@inheritDoc}
+ * @see org.apache.composer.core.tck.AbstractComponentAdapterTestCase#getComponentAdapterType()
+ */
+ protected Class getComponentAdapterType() {
+ return InstanceAdapter.class;
+ }
+
+ /**
+ * {@inheritDoc}
+ * @see org.apache.composer.core.tck.AbstractComponentAdapterTestCase#getComponentAdapterNature()
+ */
+ protected int getComponentAdapterNature() {
+ return super.getComponentAdapterNature() & ~(RESOLVING | VERIFYING | INSTANTIATING );
+ }
+
+ /**
+ * {@inheritDoc}
+ * @see org.apache.composer.core.tck.AbstractComponentAdapterTestCase#prepDEF_verifyWithoutDependencyWorks(org.apache.composer.core.MutableContainer)
+ */
+ protected ComponentAdapter prepDEF_verifyWithoutDependencyWorks(MutableContainer picoContainer) {
+ return new InstanceAdapter("foo", "bar", new NullLifecycleStrategy(),
+ new NullComponentMonitor());
+ }
+
+ /**
+ * {@inheritDoc}
+ * @see org.apache.composer.core.tck.AbstractComponentAdapterTestCase#prepDEF_verifyDoesNotInstantiate(org.apache.composer.core.MutableContainer)
+ */
+ protected ComponentAdapter prepDEF_verifyDoesNotInstantiate(
+ MutableContainer picoContainer) {
+ return new InstanceAdapter("Key", 4711, new NullLifecycleStrategy(),
+ new NullComponentMonitor());
+ }
+
+ /**
+ * {@inheritDoc}
+ * @see org.apache.composer.core.tck.AbstractComponentAdapterTestCase#prepDEF_visitable()
+ */
+ protected ComponentAdapter prepDEF_visitable() {
+ return new InstanceAdapter("Key", 4711, new NullLifecycleStrategy(),
+ new NullComponentMonitor());
+ }
+
+ /**
+ * {@inheritDoc}
+ * @see org.apache.composer.core.tck.AbstractComponentAdapterTestCase#prepSER_isSerializable(org.apache.composer.core.MutableContainer)
+ */
+ protected ComponentAdapter prepSER_isSerializable(MutableContainer picoContainer) {
+ return new InstanceAdapter("Key", 4711, new NullLifecycleStrategy(),
+ new NullComponentMonitor());
+ }
+
+ /**
+ * {@inheritDoc}
+ * @see org.apache.composer.core.tck.AbstractComponentAdapterTestCase#prepSER_isXStreamSerializable(org.apache.composer.core.MutableContainer)
+ */
+ protected ComponentAdapter prepSER_isXStreamSerializable(MutableContainer picoContainer) {
+ return new InstanceAdapter("Key", 4711, new NullLifecycleStrategy(),
+ new NullComponentMonitor());
+ }
+
+}
Added: incubator/composer/core/src/test/org/apache/composer/core/adapters/SimpleNamedBindingAnnotationTestCase.java
URL: http://svn.apache.org/viewvc/incubator/composer/core/src/test/org/apache/composer/core/adapters/SimpleNamedBindingAnnotationTestCase.java?rev=613775&view=auto
==============================================================================
--- incubator/composer/core/src/test/org/apache/composer/core/adapters/SimpleNamedBindingAnnotationTestCase.java (added)
+++ incubator/composer/core/src/test/org/apache/composer/core/adapters/SimpleNamedBindingAnnotationTestCase.java Sun Jan 20 22:41:26 2008
@@ -0,0 +1,166 @@
+package org.apache.composer.core.adapters;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.Serializable;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import java.lang.reflect.Field;
+import java.util.Properties;
+
+import org.junit.Test;
+import org.apache.composer.core.Characteristics;
+import org.apache.composer.core.ComponentAdapter;
+import org.apache.composer.core.ComponentMonitor;
+import org.apache.composer.core.DefaultContainer;
+import org.apache.composer.core.InjectionFactory;
+import org.apache.composer.core.LifecycleStrategy;
+import org.apache.composer.core.MutableContainer;
+import org.apache.composer.core.Parameter;
+import org.apache.composer.core.CompositionException;
+import org.apache.composer.core.Container;
+import org.apache.composer.core.behaviors.AbstractBehaviorFactory;
+import org.apache.composer.core.injectors.AbstractInjector;
+
+
+/**
+ * @author Paul Hammant
+ * @author Jörg Schaible
+ */
+public class SimpleNamedBindingAnnotationTestCase {
+
+ @Test public void testNamedBinding() {
+ MutableContainer mpc = new DefaultContainer(new FieldInjection());
+ mpc.addComponent(FruitBasket.class);
+ mpc.addComponent(bindKey(Apple.class, "one"), AppleImpl1.class);
+ mpc.addComponent(bindKey(Apple.class, "two"), AppleImpl2.class);
+ mpc.addComponent(bindKey(Apple.class, "three"), AppleImpl3.class);
+ mpc.addComponent(bindKey(Apple.class, "four"), AppleImpl4.class);
+ // this level of terseness is the other way ....
+ // this should not be barfing if if we can get binding to annotations working
+ FruitBasket fb = mpc.getComponent(FruitBasket.class);
+ assertEquals(fb.one.getX(), 1);
+ assertEquals(fb.two.getX(), 2);
+ assertEquals(fb.three.getX(), 3);
+ assertEquals(fb.four.getX(), 4);
+ }
+
+ public interface Apple {
+ int getX();
+ }
+
+ public static class AppleImpl1 implements Apple {
+ public int getX() {
+ return 1;
+ }
+ }
+
+ public static class AppleImpl2 implements Apple {
+ public int getX() {
+ return 2;
+ }
+ }
+
+ public static class AppleImpl3 implements Apple {
+ public int getX() {
+ return 3;
+ }
+ }
+
+ public static class AppleImpl4 implements Apple {
+ public int getX() {
+ return 4;
+ }
+ }
+
+ public static class FruitBasket {
+ private @Named("one")
+ Apple one;
+ private @Named("two")
+ Apple two;
+ private @Named("three")
+ Apple three;
+ private @Named("four")
+ Apple four;
+
+ public FruitBasket() {
+ }
+ }
+
+ // to become an annotation
+ @Retention(RetentionPolicy.RUNTIME)
+ @Target({ElementType.FIELD, ElementType.PARAMETER})
+ public @interface Named {
+ String value();
+ }
+
+ // implicitly this function goes into DPC
+ public static String bindKey(Class type, String bindingId) {
+ return type.getName() + "/" + bindingId;
+ }
+
+ public class FieldInjection implements InjectionFactory, Serializable {
+
+ public <T> ComponentAdapter<T> createComponentAdapter(
+ ComponentMonitor componentMonitor, LifecycleStrategy lifecycleStrategy,
+ Properties componentProperties, Object componentKey,
+ Class<T> componentImplementation, Parameter ... parameters)
+ throws CompositionException {
+ boolean useNames = AbstractBehaviorFactory.removePropertiesIfPresent(
+ componentProperties, Characteristics.USE_NAMES);
+ return new FieldInjector(
+ componentKey, componentImplementation, parameters, componentMonitor,
+ lifecycleStrategy, useNames);
+ }
+ }
+
+ public static class FieldInjector<T> extends AbstractInjector<T> {
+
+ protected FieldInjector(
+ Object componentKey, Class componentImplementation, Parameter[] parameters,
+ ComponentMonitor monitor, LifecycleStrategy lifecycleStrategy, boolean useNames) {
+ super(
+ componentKey, componentImplementation, parameters, monitor, lifecycleStrategy,
+ useNames);
+ }
+
+ @Override
+ public void verify(Container container) throws CompositionException {
+ // @todo Auto-generated method stub
+ }
+
+ public T getComponentInstance(Container container) throws CompositionException {
+ final T inst;
+ try {
+ inst = getComponentImplementation().newInstance();
+ Field[] declaredFields = getComponentImplementation().getDeclaredFields();
+ for (int i = 0; i < declaredFields.length; i++ ) {
+ final Field field = declaredFields[i];
+ Named bindAnnotation = field.getAnnotation(Named.class);
+ Object value;
+ if (bindAnnotation != null) {
+ value = container.getComponent(bindKey(field.getType(), bindAnnotation
+ .value()));
+ } else {
+ value = container.getComponent(field.getType());
+ }
+ field.setAccessible(true);
+ field.set(inst, value);
+ }
+
+ } catch (InstantiationException e) {
+ return caughtInstantiationException(currentMonitor(), null, e, container);
+ } catch (IllegalAccessException e) {
+ return caughtIllegalAccessException(currentMonitor(), null, e, container);
+ }
+ return inst;
+ }
+
+ public String getDescriptor() {
+ return "FieldInjector";
+ }
+
+ }
+}
Added: incubator/composer/core/src/test/org/apache/composer/core/adapters/TypedBindingAnnotationTestCase.java
URL: http://svn.apache.org/viewvc/incubator/composer/core/src/test/org/apache/composer/core/adapters/TypedBindingAnnotationTestCase.java?rev=613775&view=auto
==============================================================================
--- incubator/composer/core/src/test/org/apache/composer/core/adapters/TypedBindingAnnotationTestCase.java (added)
+++ incubator/composer/core/src/test/org/apache/composer/core/adapters/TypedBindingAnnotationTestCase.java Sun Jan 20 22:41:26 2008
@@ -0,0 +1,183 @@
+package org.apache.composer.core.adapters;
+
+import static org.apache.composer.core.BindKey.bindKey;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.apache.composer.core.DefaultContainer;
+import org.apache.composer.core.MutableContainer;
+import org.apache.composer.core.annotations.Bind;
+import org.apache.composer.core.annotations.Inject;
+import org.apache.composer.core.injectors.AbstractInjector;
+import org.apache.composer.core.injectors.AnnotatedFieldInjection;
+import org.apache.composer.core.injectors.ConstructorInjection;
+import org.apache.composer.core.injectors.MethodInjection;
+import org.apache.composer.core.injectors.SetterInjection;
+
+/** @author Paul Hammant */
+public class TypedBindingAnnotationTestCase {
+
+ @Test public void testFieldInjectionWithBindings() {
+ MutableContainer mpc = new DefaultContainer(new AnnotatedFieldInjection());
+
+ addFiveComponents(mpc);
+ FruitBasket fb = mpc.getComponent(FruitBasket.class);
+ assertFourMemberApplesAreRight(fb);
+ assertGettingOfAppleOneWorks(mpc);
+ }
+
+ private void assertGettingOfAppleOneWorks(MutableContainer mpc) {
+ try {
+ mpc.getComponent(Apple.class);
+ Assert.fail("should have barfed");
+ } catch (AbstractInjector.AmbiguousComponentResolutionException e) {
+ System.out.println("");
+ // expected
+ }
+ Assert.assertNotNull(mpc.getComponent(Apple.class, Bramley.class));
+ }
+
+ @Test public void testBindingAnnotationsWithConstructorInjection() {
+ MutableContainer mpc = new DefaultContainer(new ConstructorInjection());
+
+ addFiveComponents(mpc, FruitBasketConstructor.class);
+ FruitBasket fb = mpc.getComponent(FruitBasketConstructor.class);
+ assertFourMemberApplesAreRight(fb);
+ assertGettingOfAppleOneWorks(mpc);
+ }
+
+ private void assertFourMemberApplesAreRight(FruitBasket fb) {
+ Assert.assertNotNull(fb);
+ Assert.assertEquals(fb.bramley.getX(), 1);
+ Assert.assertEquals(fb.cox.getX(), 2);
+ Assert.assertEquals(fb.granny.getX(), 3);
+ Assert.assertEquals(fb.braeburn.getX(), 4);
+ }
+
+ @Test public void testBindingAnnotationsWithMethodInjection() {
+ MutableContainer mpc = new DefaultContainer(new MethodInjection("foo"));
+ addFiveComponents(mpc);
+ FruitBasket fb = mpc.getComponent(FruitBasket.class);
+ assertFourMemberApplesAreRight(fb);
+ assertGettingOfAppleOneWorks(mpc);
+
+ }
+
+ @Test public void testBindingAnnotationsWithSetterInjection() {
+ MutableContainer mpc = new DefaultContainer(new SetterInjection());
+ addFiveComponents(mpc);
+ FruitBasket fb = mpc.getComponent(FruitBasket.class);
+ assertFourMemberApplesAreRight(fb);
+ assertGettingOfAppleOneWorks(mpc);
+
+ }
+
+ private void addFiveComponents(MutableContainer mpc) {
+ addFiveComponents(mpc, FruitBasket.class);
+ }
+
+ private void addFiveComponents(MutableContainer mpc, Class clazz) {
+ mpc.addComponent(clazz);
+ mpc.addComponent(bindKey(Apple.class, Bramley.class), AppleImpl1.class);
+ mpc.addComponent(bindKey(Apple.class, Cox.class), AppleImpl2.class);
+ mpc.addComponent(bindKey(Apple.class, Granny.class), AppleImpl3.class);
+ mpc.addComponent(bindKey(Apple.class, Braeburn.class), AppleImpl4.class);
+ }
+
+ public interface Apple {
+ int getX();
+ }
+ public static class AppleImpl1 implements Apple {
+ public int getX() {
+ return 1;
+ }
+ }
+ public static class AppleImpl2 implements Apple {
+ public int getX() {
+ return 2;
+ }
+ }
+ public static class AppleImpl3 implements Apple {
+ public int getX() {
+ return 3;
+ }
+ }
+ public static class AppleImpl4 implements Apple {
+ public int getX() {
+ return 4;
+ }
+ }
+
+ @Retention(RetentionPolicy.RUNTIME)
+ @Target({ElementType.FIELD, ElementType.PARAMETER})
+ @Bind
+ public static @interface Bramley {}
+
+ @Retention(RetentionPolicy.RUNTIME)
+ @Target({ElementType.FIELD, ElementType.PARAMETER})
+ @Bind
+ public static @interface Cox {}
+
+ @Retention(RetentionPolicy.RUNTIME)
+ @Target({ElementType.FIELD, ElementType.PARAMETER})
+ @Bind
+ public static @interface Granny {}
+
+ @Retention(RetentionPolicy.RUNTIME)
+ @Target({ElementType.FIELD, ElementType.PARAMETER})
+ @Bind
+ public static @interface Braeburn {}
+
+ public static class FruitBasketConstructor extends FruitBasket {
+ // used in testBindingAnnotationsWithConstructorInjection()
+ public FruitBasketConstructor(@Bramley Apple bramley, @Cox Apple cox, @Granny Apple granny, @Braeburn Apple braeburn) {
+ foo(bramley, cox, granny, braeburn);
+ }
+
+ }
+ public static class FruitBasket {
+ @Inject
+ private @Bramley Apple bramley;
+ @Inject
+ private @Cox Apple cox;
+ @Inject
+ private @Granny Apple granny;
+ @Inject
+ private @Braeburn Apple braeburn;
+
+ public FruitBasket() {
+ }
+
+
+ // used in testBindingAnnotationsWithMethodInjection()
+ public void foo(@Bramley Apple bramley, @Cox Apple cox, @Granny Apple granny, @Braeburn Apple braeburn) {
+ this.bramley = bramley;
+ this.cox = cox;
+ this.granny = granny;
+ this.braeburn = braeburn;
+ }
+
+ public void setOne(@Bramley Apple bramley) {
+ this.bramley = bramley;
+ }
+
+ public void setTwo(@Cox Apple cox) {
+ this.cox = cox;
+ }
+
+ public void setThree(@Granny Apple granny) {
+ this.granny = granny;
+ }
+
+ public void setFour(@Braeburn Apple braeburn) {
+ this.braeburn = braeburn;
+ }
+ }
+
+
+}
Added: incubator/composer/core/src/test/org/apache/composer/core/alternatives/ParanamerContainerTestCase.java
URL: http://svn.apache.org/viewvc/incubator/composer/core/src/test/org/apache/composer/core/alternatives/ParanamerContainerTestCase.java?rev=613775&view=auto
==============================================================================
--- incubator/composer/core/src/test/org/apache/composer/core/alternatives/ParanamerContainerTestCase.java (added)
+++ incubator/composer/core/src/test/org/apache/composer/core/alternatives/ParanamerContainerTestCase.java Sun Jan 20 22:41:26 2008
@@ -0,0 +1,26 @@
+/*****************************************************************************
+ * Copyright (C) PicoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ * Original code by *
+ *****************************************************************************/
+package org.apache.composer.core.alternatives;
+
+import static org.junit.Assert.assertNotNull;
+
+import org.junit.Test;
+
+import com.thoughtworks.paranamer.DefaultParanamer;
+import com.thoughtworks.paranamer.Paranamer;
+
+public class ParanamerContainerTestCase {
+
+ @Test public void testCanInstantiateParanamer(){
+ Paranamer paranamer = new DefaultParanamer();
+ assertNotNull(paranamer);
+ }
+
+}
Added: incubator/composer/core/src/test/org/apache/composer/core/alternatives/issues/Issue0214TestCase.java
URL: http://svn.apache.org/viewvc/incubator/composer/core/src/test/org/apache/composer/core/alternatives/issues/Issue0214TestCase.java?rev=613775&view=auto
==============================================================================
--- incubator/composer/core/src/test/org/apache/composer/core/alternatives/issues/Issue0214TestCase.java (added)
+++ incubator/composer/core/src/test/org/apache/composer/core/alternatives/issues/Issue0214TestCase.java Sun Jan 20 22:41:26 2008
@@ -0,0 +1,85 @@
+/*****************************************************************************
+ * Copyright (C) PicoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ * Original code by *
+ *****************************************************************************/
+package org.apache.composer.core.alternatives.issues;
+
+import org.junit.Test;
+import org.apache.composer.core.DefaultContainer;
+import org.apache.composer.core.MutableContainer;
+import org.apache.composer.core.behaviors.ImplementationHiding;
+import org.apache.composer.core.injectors.ConstructorInjection;
+import org.apache.composer.core.visitors.VerifyingVisitor;
+
+public class Issue0214TestCase {
+
+ // This bug as described in the bug report, http://jira.codehaus.org/browse/PICO-214, cannot be reproduced.
+ @Test public void testTheBug() {
+ final MutableContainer pico = new DefaultContainer(new ImplementationHiding().wrap(new ConstructorInjection()));
+ pico.addComponent(A.class);
+
+ /* This is a workaround for the bug described further down. Normally
+ * this method call should only be needed if specific requirements for
+ * parameters exist, but not if PicoContainer shall resolve the
+ * dependencies itself. However, with ImplementationHidingPicoContainer
+ * this is currently the only way to register a class/interface such
+ * that the automatic resolution works.
+ */
+ pico.addComponent(I1.class, B.class);
+
+ /* The following addAdapter(Object, Class) of
+ * ImplementationHidingPicoContainer is buggy, as it contains
+ * "ComponentAdapter delegate = componentFactory.createComponentAdapter(componentKey,
+ * componentImplementation, new Parameter[0]);". Instead of "new
+ * Parameter[0]" it should be "null" to have a behaviour consistent to
+ * DefaultPicoContainer, i.e. if PicoContainer shall resolve
+ * dependencies itself.
+ */
+ pico.addComponent(I2.class, C.class);
+
+ /* The following verify() throws the exception, but is expected not to
+ * throw: "org.apache.composer.core.PicoVerificationException:
+ * [[org.apache.composer.core.PicoCompositionException: Either do the
+ * specified parameters not match any of the following constructors:
+ * [public PicoContainerBugTest$C(PicoContainerBugTest$A)] or the
+ * constructors were not accessible for 'class
+ * PicoContainerBugTest$C']]".
+ *
+ * I believe that the error comes this way: In method
+ * getGreediestSatisfiableConstructor parameters are checked against
+ * null and if parameters is not null it is assumed that specific
+ * parameters have been given so that no automatic resolution takes
+ * place. As now during registration instead of "null" falsly "new
+ * Parameter[0]" was stored, this is now interpreted as if only the
+ * nullary constructor shall be used, and if that doesn't exist, the
+ * exception is thrown.
+ */
+ new VerifyingVisitor().traverse(pico);
+ }
+
+ public static interface I1 {
+ }
+
+ public static interface I2 {
+ }
+
+ public static class A {
+ public A() {
+ }
+ }
+
+ public static class B implements I1 {
+ public B(final A a) {
+ }
+ }
+
+ public static class C implements I2 {
+ public C(final A a) {
+ }
+ }
+}
Added: incubator/composer/core/src/test/org/apache/composer/core/behaviors/AdaptingBehaviorTestCase.java
URL: http://svn.apache.org/viewvc/incubator/composer/core/src/test/org/apache/composer/core/behaviors/AdaptingBehaviorTestCase.java?rev=613775&view=auto
==============================================================================
--- incubator/composer/core/src/test/org/apache/composer/core/behaviors/AdaptingBehaviorTestCase.java (added)
+++ incubator/composer/core/src/test/org/apache/composer/core/behaviors/AdaptingBehaviorTestCase.java Sun Jan 20 22:41:26 2008
@@ -0,0 +1,216 @@
+/*****************************************************************************
+ * Copyright (C) PicoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ * Original code by *
+ *****************************************************************************/
+package org.apache.composer.core.behaviors;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+import org.junit.Test;
+import org.apache.composer.core.Characteristics;
+import org.apache.composer.core.ComponentAdapter;
+import org.apache.composer.core.adapters.InstanceAdapter;
+import org.apache.composer.core.annotations.Cache;
+import org.apache.composer.core.containers.EmptyContainer;
+import org.apache.composer.core.injectors.SetterInjector;
+import org.apache.composer.core.lifecycle.NullLifecycleStrategy;
+import org.apache.composer.core.monitors.NullComponentMonitor;
+
+import com.thoughtworks.xstream.XStream;
+
+public class AdaptingBehaviorTestCase {
+
+ @Test public void testCachingBehaviorCanBeAddedByCharacteristics() {
+ AdaptingBehavior abf = new AdaptingBehavior();
+ Properties cc = new Properties();
+ mergeInto(Characteristics.CACHE,cc);
+ ComponentAdapter ca = abf.createComponentAdapter(new NullComponentMonitor(), new NullLifecycleStrategy(), cc, Map.class, HashMap.class);
+ assertTrue(ca instanceof Cached);
+ Map map = (Map)ca.getComponentInstance(new EmptyContainer());
+ assertNotNull(map);
+ Map map2 = (Map)ca.getComponentInstance(new EmptyContainer());
+ assertSame(map, map2);
+ assertEquals(0, cc.size());
+ assertEquals("Cached:ConstructorInjector-interface java.util.Map",ca.toString());
+ }
+
+ @Test public void testCachingBehaviorCanBeAddedByAnnotation() {
+ AdaptingBehavior abf = new AdaptingBehavior();
+ Properties cc = new Properties();
+ ComponentAdapter ca = abf.createComponentAdapter(new NullComponentMonitor(), new NullLifecycleStrategy(), cc, Map.class, MyHashMap.class);
+ assertTrue(ca instanceof Cached);
+ Map map = (Map)ca.getComponentInstance(new EmptyContainer());
+ assertNotNull(map);
+ Map map2 = (Map)ca.getComponentInstance(new EmptyContainer());
+ assertSame(map, map2);
+ assertEquals(0, cc.size());
+ assertEquals("Cached:ConstructorInjector-interface java.util.Map",ca.toString());
+ }
+
+ @Cache
+ public static class MyHashMap extends HashMap {
+ public MyHashMap() {
+ }
+ }
+
+ public static class MyHashMap2 extends HashMap {
+ private String foo;
+
+ public MyHashMap2() {
+ }
+ public void setFoo(String foo) {
+ this.foo = foo;
+ }
+ }
+
+ @Test public void testImplementationHidingBehaviorCanBeAddedByCharacteristics() {
+ AdaptingBehavior abf = new AdaptingBehavior();
+ Properties cc = new Properties();
+ mergeInto(Characteristics.HIDE_IMPL,cc);
+ ComponentAdapter ca = abf.createComponentAdapter(new NullComponentMonitor(), new NullLifecycleStrategy(), cc, Map.class, HashMap.class);
+ assertTrue(ca instanceof HiddenImplementation);
+ Map map = (Map)ca.getComponentInstance(new EmptyContainer());
+ assertNotNull(map);
+ assertTrue(!(map instanceof HashMap));
+
+ assertEquals(0, cc.size());
+ assertEquals("Hidden:ConstructorInjector-interface java.util.Map",ca.toString());
+
+ }
+
+ @Test public void testPropertyApplyingBehaviorCanBeAddedByCharacteristics() {
+ AdaptingBehavior abf = new AdaptingBehavior();
+ Properties cc = new Properties();
+ mergeInto(Characteristics.PROPERTY_APPLYING,cc);
+ ComponentAdapter ca = abf.createComponentAdapter(new NullComponentMonitor(), new NullLifecycleStrategy(), cc, Map.class, MyHashMap2.class);
+ assertTrue(ca instanceof PropertyApplicator);
+ PropertyApplicator pa = (PropertyApplicator)ca;
+ pa.setProperty("foo", "bar");
+ Map map = (Map)ca.getComponentInstance(new EmptyContainer());
+ assertNotNull(map);
+ assertTrue(map instanceof HashMap);
+ assertTrue(map instanceof MyHashMap2);
+ assertEquals("bar", ((MyHashMap2) map).foo);
+
+ assertEquals(0, cc.size());
+ assertEquals("PropertyApplied:ConstructorInjector-interface java.util.Map",ca.toString());
+
+ }
+
+ @Test public void testSetterInjectionCanBeTriggereedMeaningAdaptiveInjectorIsUsed() {
+ AdaptingBehavior abf = new AdaptingBehavior();
+ Properties cc = new Properties();
+ mergeInto(Characteristics.SDI,cc);
+ ComponentAdapter ca = abf.createComponentAdapter(new NullComponentMonitor(), new NullLifecycleStrategy(), cc, Map.class, HashMap.class);
+ assertTrue(ca instanceof SetterInjector);
+ Map map = (Map)ca.getComponentInstance(new EmptyContainer());
+ assertNotNull(map);
+ assertEquals(0, cc.size());
+ assertEquals("SetterInjector-interface java.util.Map",ca.toString());
+
+ }
+
+ @Test public void testCachingAndImplHidingAndThreadSafetySetupCorrectly() {
+ AdaptingBehavior abf = new AdaptingBehavior();
+ Properties cc = new Properties();
+ mergeInto(Characteristics.CACHE,cc);
+ mergeInto(Characteristics.HIDE_IMPL,cc);
+ mergeInto(Characteristics.SYNCHRONIZE,cc);
+ ComponentAdapter ca = abf.createComponentAdapter(new NullComponentMonitor(), new NullLifecycleStrategy(), cc, Map.class, HashMap.class);
+ assertTrue(ca instanceof Cached);
+ Map map = (Map)ca.getComponentInstance(new EmptyContainer());
+ assertNotNull(map);
+ assertTrue(!(map instanceof HashMap));
+
+ XStream xs = new XStream();
+ String foo = xs.toXML(ca);
+
+ int ih = foo.indexOf(HiddenImplementation.class.getName());
+ int sb = foo.indexOf(Synchronized.class.getName());
+
+ // check right nesting order
+ assertTrue(ih>0);
+ assertTrue(sb>0);
+ assertTrue(sb>ih);
+
+ assertEquals(0, cc.size());
+ assertEquals("Cached:Hidden:Synchronized:ConstructorInjector-interface java.util.Map",ca.toString());
+
+
+
+
+ }
+
+ @Test public void testCachingAndImplHidingAndThreadSafetySetupCorrectlyForExtraCaching() {
+ Caching cbf = new Caching();
+ AdaptingBehavior abf = new AdaptingBehavior();
+ cbf.wrap(abf);
+ Properties cc = new Properties();
+ mergeInto(Characteristics.CACHE,cc);
+ mergeInto(Characteristics.HIDE_IMPL,cc);
+ mergeInto(Characteristics.SYNCHRONIZE,cc);
+ ComponentAdapter ca = cbf.createComponentAdapter(new NullComponentMonitor(), new NullLifecycleStrategy(), cc, Map.class, HashMap.class);
+ assertTrue(ca instanceof Cached);
+ Map map = (Map)ca.getComponentInstance(new EmptyContainer());
+ assertNotNull(map);
+ assertTrue(!(map instanceof HashMap));
+
+ XStream xs = new XStream();
+ String foo = xs.toXML(ca);
+
+ assertTrue(foo.indexOf("<" + Cached.class.getName() + ">", 0) > -1); // xml does start with CB
+ assertFalse(foo.indexOf("<" + Cached.class.getName() + ">", 1) > -1); // but only contains it once.
+ assertEquals("Cached:Hidden:Synchronized:ConstructorInjector-interface java.util.Map",ca.toString());
+
+ }
+
+ @Test public void testCachingAndImplHidingAndThreadSafetySetupCorrectlyForExtraCachingForAdapter() {
+ Caching cbf = new Caching();
+ AdaptingBehavior abf = new AdaptingBehavior();
+ cbf.wrap(abf);
+ Properties cc = new Properties();
+ mergeInto(Characteristics.CACHE,cc);
+ mergeInto(Characteristics.HIDE_IMPL,cc);
+ mergeInto(Characteristics.SYNCHRONIZE,cc);
+ ComponentAdapter ca = cbf.addComponentAdapter(new NullComponentMonitor(), new NullLifecycleStrategy(), cc, new InstanceAdapter(Map.class, new HashMap(), new NullLifecycleStrategy(), new NullComponentMonitor()));
+ assertTrue(ca instanceof Cached);
+ Map map = (Map)ca.getComponentInstance(new EmptyContainer());
+ assertNotNull(map);
+ assertTrue(!(map instanceof HashMap));
+
+ XStream xs = new XStream();
+ String foo = xs.toXML(ca);
+
+ assertTrue(foo.indexOf("<" + Cached.class.getName() + ">", 0) > -1); // xml does start with CB
+ assertFalse(foo.indexOf("<" + Cached.class.getName() + ">", 1) > -1); // but only contains it once.
+ assertEquals("Cached:Hidden:Synchronized:Instance-interface java.util.Map",ca.toString());
+
+ }
+
+
+
+ public void mergeInto(Properties p, Properties into) {
+ Enumeration e = p.propertyNames();
+ while (e.hasMoreElements()) {
+ String s = (String)e.nextElement();
+ into.setProperty(s, p.getProperty(s));
+ }
+
+ }
+
+
+}
Added: incubator/composer/core/src/test/org/apache/composer/core/behaviors/AutomatingTestCase.java
URL: http://svn.apache.org/viewvc/incubator/composer/core/src/test/org/apache/composer/core/behaviors/AutomatingTestCase.java?rev=613775&view=auto
==============================================================================
--- incubator/composer/core/src/test/org/apache/composer/core/behaviors/AutomatingTestCase.java (added)
+++ incubator/composer/core/src/test/org/apache/composer/core/behaviors/AutomatingTestCase.java Sun Jan 20 22:41:26 2008
@@ -0,0 +1,158 @@
+/*****************************************************************************
+ * Copyright (C) PicoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ * Original code by *
+ *****************************************************************************/
+package org.apache.composer.core.behaviors;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.apache.composer.core.Characteristics.AUTOMATIC;
+import static org.apache.composer.core.behaviors.Behaviors.automatic;
+import static org.apache.composer.core.behaviors.Behaviors.caching;
+
+import org.junit.Test;
+import org.apache.composer.core.DefaultContainer;
+import org.apache.composer.core.MutableContainer;
+import org.apache.composer.core.ContainerBuilder;
+import org.apache.composer.core.injectors.ConstructorInjector;
+import org.apache.composer.core.lifecycle.NullLifecycleStrategy;
+import org.apache.composer.core.monitors.NullComponentMonitor;
+
+public class AutomatingTestCase {
+
+ private static String MESSAGE =
+ "Foo was instantiated, even though it was not required to be given it was not depended on by anything looked up";
+
+ public static class Foo {
+ public Foo(StringBuilder sb) {
+ sb.append(MESSAGE);
+ }
+ }
+
+ public static class Bar {
+ }
+
+ @Test public void testAutomaticBehavior() {
+ DefaultContainer pico = new DefaultContainer(new Caching().wrap(new Automating()));
+ pico.addComponent(StringBuilder.class);
+ pico.addComponent(Foo.class);
+ pico.addComponent(Bar.class);
+ pico.start();
+ assertNotNull(pico.getComponent(Bar.class));
+ StringBuilder sb = pico.getComponent(StringBuilder.class);
+ assertEquals(MESSAGE, sb.toString());
+ assertEquals("Cached:Automated:ConstructorInjector-class org.apache.composer.core.behaviors.AutomatingTestCase$Foo", pico.getComponentAdapter(Foo.class).toString());
+ }
+
+ @Test public void testAutomaticBehaviorViaAdapter() {
+ DefaultContainer pico = new DefaultContainer(new Caching().wrap(new Automating()));
+ pico.addComponent(StringBuilder.class);
+ pico.addAdapter(new ConstructorInjector(Foo.class, Foo.class, null, new NullComponentMonitor(), new NullLifecycleStrategy(), false));
+ pico.addComponent(Bar.class);
+ pico.start();
+ assertNotNull(pico.getComponent(Bar.class));
+ StringBuilder sb = pico.getComponent(StringBuilder.class);
+ assertEquals(MESSAGE, sb.toString());
+ assertEquals("Cached:Automated:ConstructorInjector-class org.apache.composer.core.behaviors.AutomatingTestCase$Foo", pico.getComponentAdapter(Foo.class).toString());
+ }
+
+ @Test public void testNonAutomaticBehaviorAsContrastToTheAbove() {
+ DefaultContainer pico = new DefaultContainer(new Caching());
+ pico.addComponent(StringBuilder.class);
+ pico.addComponent(Foo.class);
+ pico.addComponent(Bar.class);
+ pico.start();
+ assertNotNull(pico.getComponent(Bar.class));
+ StringBuilder sb = pico.getComponent(StringBuilder.class);
+ assertEquals("", sb.toString());
+ }
+
+ @Test public void testNonAutomaticBehaviorAsContrastToTheAboveViaAdapter() {
+ DefaultContainer pico = new DefaultContainer(new Caching());
+ pico.addComponent(StringBuilder.class);
+ pico.addAdapter(new ConstructorInjector(Foo.class, Foo.class, null, new NullComponentMonitor(), new NullLifecycleStrategy(), false));
+ pico.addComponent(Bar.class);
+ pico.start();
+ assertNotNull(pico.getComponent(Bar.class));
+ StringBuilder sb = pico.getComponent(StringBuilder.class);
+ assertEquals("", sb.toString());
+ }
+
+ @Test public void testAutomaticBehaviorByBuilder() {
+ MutableContainer pico = new ContainerBuilder().withCaching().withAutomatic().build();
+ pico.addComponent(StringBuilder.class);
+ pico.addComponent(Foo.class);
+ pico.addComponent(Bar.class);
+ pico.start();
+ assertNotNull(pico.getComponent(Bar.class));
+ StringBuilder sb = pico.getComponent(StringBuilder.class);
+ assertEquals(MESSAGE, sb.toString());
+ assertEquals("Cached:Automated:ConstructorInjector-class org.apache.composer.core.behaviors.AutomatingTestCase$Foo", pico.getComponentAdapter(Foo.class).toString());
+ }
+
+ @Test public void testAutomaticBehaviorByBuilderViaAdapter() {
+ MutableContainer pico = new ContainerBuilder().withCaching().withAutomatic().build();
+ pico.addComponent(StringBuilder.class);
+ pico.addAdapter(new ConstructorInjector(Foo.class, Foo.class, null, new NullComponentMonitor(), new NullLifecycleStrategy(), false));
+ pico.addComponent(Bar.class);
+ pico.start();
+ assertNotNull(pico.getComponent(Bar.class));
+ StringBuilder sb = pico.getComponent(StringBuilder.class);
+ assertEquals(MESSAGE, sb.toString());
+ assertEquals("Cached:Automated:ConstructorInjector-class org.apache.composer.core.behaviors.AutomatingTestCase$Foo", pico.getComponentAdapter(Foo.class).toString());
+ }
+
+ @Test public void testAutomaticBehaviorByBuilderADifferentWay() {
+ MutableContainer pico = new ContainerBuilder().withBehaviors(caching(), automatic()).build();
+ pico.addComponent(StringBuilder.class);
+ pico.addComponent(Foo.class);
+ pico.addComponent(Bar.class);
+ pico.start();
+ assertNotNull(pico.getComponent(Bar.class));
+ StringBuilder sb = pico.getComponent(StringBuilder.class);
+ assertEquals(MESSAGE, sb.toString());
+ assertEquals("Cached:Automated:ConstructorInjector-class org.apache.composer.core.behaviors.AutomatingTestCase$Foo", pico.getComponentAdapter(Foo.class).toString());
+ }
+
+ @Test public void testAutomaticBehaviorByBuilderADifferentWayViaAdapter() {
+ MutableContainer pico = new ContainerBuilder().withBehaviors(caching(), automatic()).build();
+ pico.addComponent(StringBuilder.class);
+ pico.addAdapter(new ConstructorInjector(Foo.class, Foo.class, null, new NullComponentMonitor(), new NullLifecycleStrategy(), false));
+ pico.addComponent(Bar.class);
+ pico.start();
+ assertNotNull(pico.getComponent(Bar.class));
+ StringBuilder sb = pico.getComponent(StringBuilder.class);
+ assertEquals(MESSAGE, sb.toString());
+ assertEquals("Cached:Automated:ConstructorInjector-class org.apache.composer.core.behaviors.AutomatingTestCase$Foo", pico.getComponentAdapter(Foo.class).toString());
+ }
+
+ @Test public void testAutomaticBehaviorWorksForAdaptiveBehaviorToo() {
+ MutableContainer pico = new ContainerBuilder().withBehaviors(caching(), automatic()).build();
+ pico.addComponent(StringBuilder.class);
+ pico.as(AUTOMATIC).addComponent(Foo.class);
+ pico.addComponent(Bar.class);
+ pico.start();
+ assertNotNull(pico.getComponent(Bar.class));
+ StringBuilder sb = pico.getComponent(StringBuilder.class);
+ assertEquals(MESSAGE, sb.toString());
+ assertEquals("Cached:Automated:ConstructorInjector-class org.apache.composer.core.behaviors.AutomatingTestCase$Foo", pico.getComponentAdapter(Foo.class).toString());
+ }
+
+ @Test public void testAutomaticBehaviorWorksForAdaptiveBehaviorTooViaAdapter() {
+ MutableContainer pico = new ContainerBuilder().withBehaviors(caching(), automatic()).build();
+ pico.addComponent(StringBuilder.class);
+ pico.as(AUTOMATIC).addAdapter(new ConstructorInjector(Foo.class, Foo.class, null, new NullComponentMonitor(), new NullLifecycleStrategy(), false));
+ pico.addComponent(Bar.class);
+ pico.start();
+ assertNotNull(pico.getComponent(Bar.class));
+ StringBuilder sb = pico.getComponent(StringBuilder.class);
+ assertEquals(MESSAGE, sb.toString());
+ assertEquals("Cached:Automated:ConstructorInjector-class org.apache.composer.core.behaviors.AutomatingTestCase$Foo", pico.getComponentAdapter(Foo.class).toString());
+ }
+
+}
Added: incubator/composer/core/src/test/org/apache/composer/core/behaviors/BehaviorAdapterTestCase.java
URL: http://svn.apache.org/viewvc/incubator/composer/core/src/test/org/apache/composer/core/behaviors/BehaviorAdapterTestCase.java?rev=613775&view=auto
==============================================================================
--- incubator/composer/core/src/test/org/apache/composer/core/behaviors/BehaviorAdapterTestCase.java (added)
+++ incubator/composer/core/src/test/org/apache/composer/core/behaviors/BehaviorAdapterTestCase.java Sun Jan 20 22:41:26 2008
@@ -0,0 +1,129 @@
+/*****************************************************************************
+ * Copyright (C) PicoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ * Original code by *
+ *****************************************************************************/
+package org.apache.composer.core.behaviors;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.fail;
+import static org.apache.composer.core.tck.MockFactory.mockeryWithCountingNamingScheme;
+
+import org.jmock.Expectations;
+import org.jmock.Mockery;
+import org.jmock.integration.junit4.JMock;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.apache.composer.core.Behavior;
+import org.apache.composer.core.ComponentAdapter;
+import org.apache.composer.core.ComponentMonitor;
+import org.apache.composer.core.ComponentMonitorStrategy;
+import org.apache.composer.core.DefaultContainer;
+import org.apache.composer.core.LifecycleStrategy;
+import org.apache.composer.core.CompositionException;
+import org.apache.composer.core.Container;
+import org.apache.composer.core.testmodel.SimpleTouchable;
+import org.apache.composer.core.testmodel.Touchable;
+
+/**
+ * @author Mauro Talevi
+ */
+@RunWith(JMock.class)
+public class BehaviorAdapterTestCase {
+
+ private Mockery mockery = mockeryWithCountingNamingScheme();
+
+ @Test public void testDecoratingComponentAdapterDelegatesToMonitorThatDoesSupportStrategy() {
+ AbstractBehavior adapter = new FooAbstractBehavior(mockComponentAdapterThatDoesSupportStrategy());
+ adapter.changeMonitor(mockMonitorWithNoExpectedMethods());
+ assertNotNull(adapter.currentMonitor());
+ }
+
+ @Test public void testDecoratingComponentAdapterDelegatesToMonitorThatDoesNotSupportStrategy() {
+ AbstractBehavior adapter = new FooAbstractBehavior(mockComponentAdapter());
+ adapter.changeMonitor(mockMonitorWithNoExpectedMethods());
+ try {
+ adapter.currentMonitor();
+ fail("PicoCompositionException expected");
+ } catch (CompositionException e) {
+ assertEquals("No component monitor found in delegate", e.getMessage());
+ }
+ }
+
+ @Test public void testDecoratingComponentAdapterDelegatesLifecycleManagement() {
+ AbstractBehavior adapter = new FooAbstractBehavior(mockComponentAdapterThatCanManageLifecycle());
+ Container pico = new DefaultContainer();
+ adapter.start(pico);
+ adapter.stop(pico);
+ adapter.dispose(pico);
+ Touchable touchable = new SimpleTouchable();
+ adapter.start(touchable);
+ adapter.stop(touchable);
+ adapter.dispose(touchable);
+ }
+
+ @Test public void testDecoratingComponentAdapterIgnoresLifecycleManagementIfDelegateDoesNotSupportIt() {
+ AbstractBehavior adapter = new FooAbstractBehavior(mockComponentAdapter());
+ Container pico = new DefaultContainer();
+ adapter.start(pico);
+ adapter.stop(pico);
+ adapter.dispose(pico);
+ Touchable touchable = new SimpleTouchable();
+ adapter.start(touchable);
+ adapter.stop(touchable);
+ adapter.dispose(touchable);
+ }
+
+ ComponentMonitor mockMonitorWithNoExpectedMethods() {
+ return mockery.mock(ComponentMonitor.class);
+ }
+
+ private ComponentAdapter mockComponentAdapterThatDoesSupportStrategy() {
+ final ComponentAdapterThatSupportsStrategy ca = mockery.mock(ComponentAdapterThatSupportsStrategy.class);
+ mockery.checking(new Expectations(){{
+ one(ca).changeMonitor(with(any(ComponentMonitor.class)));
+ one(ca).currentMonitor();
+ will(returnValue(mockMonitorWithNoExpectedMethods()));
+ }});
+ return ca;
+ }
+
+ private ComponentAdapter mockComponentAdapter() {
+ return mockery.mock(ComponentAdapter.class);
+ }
+
+ public static interface ComponentAdapterThatSupportsStrategy extends ComponentAdapter, ComponentMonitorStrategy {
+ }
+
+ private ComponentAdapter mockComponentAdapterThatCanManageLifecycle() {
+ final ComponentAdapterThatCanManageLifecycle ca = mockery.mock(ComponentAdapterThatCanManageLifecycle.class);
+ mockery.checking(new Expectations(){{
+ one(ca).start(with(any(Container.class)));
+ one(ca).stop(with(any(Container.class)));
+ one(ca).dispose(with(any(Container.class)));
+ one(ca).start(with(any(Touchable.class)));
+ one(ca).stop(with(any(Touchable.class)));
+ one(ca).dispose(with(any(Touchable.class)));
+ }});
+ return ca;
+ }
+
+ public static interface ComponentAdapterThatCanManageLifecycle extends ComponentAdapter, Behavior, LifecycleStrategy {
+ }
+
+ static class FooAbstractBehavior extends AbstractBehavior {
+
+ public FooAbstractBehavior(ComponentAdapter delegate) {
+ super(delegate);
+ }
+
+ public String getDescriptor() {
+ return null;
+ }
+ }
+}
Added: incubator/composer/core/src/test/org/apache/composer/core/behaviors/CachedTestCase.java
URL: http://svn.apache.org/viewvc/incubator/composer/core/src/test/org/apache/composer/core/behaviors/CachedTestCase.java?rev=613775&view=auto
==============================================================================
--- incubator/composer/core/src/test/org/apache/composer/core/behaviors/CachedTestCase.java (added)
+++ incubator/composer/core/src/test/org/apache/composer/core/behaviors/CachedTestCase.java Sun Jan 20 22:41:26 2008
@@ -0,0 +1,202 @@
+/*****************************************************************************
+ * Copyright (C) PicoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ *****************************************************************************/
+package org.apache.composer.core.behaviors;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.fail;
+import static org.apache.composer.core.tck.MockFactory.mockeryWithCountingNamingScheme;
+
+import org.jmock.Expectations;
+import org.jmock.Mockery;
+import org.jmock.integration.junit4.JMock;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.apache.composer.core.ComponentAdapter;
+import org.apache.composer.core.DefaultContainer;
+import org.apache.composer.core.LifecycleStrategy;
+import org.apache.composer.core.MutableContainer;
+import org.apache.composer.core.Container;
+import org.apache.composer.core.testmodel.SimpleTouchable;
+import org.apache.composer.core.testmodel.Touchable;
+
+
+
+/**
+ * @author Mauro Talevi
+ */
+@RunWith(JMock.class)
+public class CachedTestCase {
+
+ private Mockery mockery = mockeryWithCountingNamingScheme();
+
+ @Test public void testComponentIsNotStartedWhenCachedAndCanBeStarted() {
+ Cached adapter = new Cached(
+ mockComponentAdapterSupportingLifecycleStrategy(true, false, false));
+ Container pico = new DefaultContainer();
+ adapter.getComponentInstance(pico);
+ adapter.start(pico);
+ }
+
+ @Test public void testComponentCanBeStartedAgainAfterBeingStopped() {
+ Cached adapter = new Cached(
+ mockComponentAdapterSupportingLifecycleStrategy(true, true, false));
+ Container pico = new DefaultContainer();
+ adapter.start(pico);
+ Object instanceAfterFirstStart = adapter.getComponentInstance(pico);
+ adapter.stop(pico);
+ adapter.start(pico);
+ Object instanceAfterSecondStart = adapter.getComponentInstance(pico);
+ assertSame(instanceAfterFirstStart, instanceAfterSecondStart);
+ }
+
+ @Test public void testComponentCannotBeStartedIfDisposed() {
+ Cached adapter = new Cached(
+ mockComponentAdapterSupportingLifecycleStrategy(false, false, true));
+ Container pico = new DefaultContainer();
+ adapter.dispose(pico);
+ try {
+ adapter.start(pico);
+ fail("IllegalStateException expected");
+ } catch (Exception e) {
+ assertEquals("Already disposed", e.getMessage());
+ }
+ }
+
+ @Test public void testComponentCannotBeStartedIfAlreadyStarted() {
+ Cached adapter = new Cached(
+ mockComponentAdapterSupportingLifecycleStrategy(true, false, false));
+ Container pico = new DefaultContainer();
+ adapter.start(pico);
+ try {
+ adapter.start(pico);
+ fail("IllegalStateException expected");
+ } catch (Exception e) {
+ assertEquals("Already started", e.getMessage());
+ }
+ }
+
+ @Test public void testComponentCannotBeStoppeddIfDisposed() {
+ Cached adapter = new Cached(
+ mockComponentAdapterSupportingLifecycleStrategy(false, false, true));
+ Container pico = new DefaultContainer();
+ adapter.dispose(pico);
+ try {
+ adapter.stop(pico);
+ fail("IllegalStateException expected");
+ } catch (Exception e) {
+ assertEquals("Already disposed", e.getMessage());
+ }
+ }
+
+ @Test public void testComponentCannotBeStoppedIfNotStarted() {
+ Cached adapter = new Cached(
+ mockComponentAdapterSupportingLifecycleStrategy(true, true, false));
+ Container pico = new DefaultContainer();
+ adapter.start(pico);
+ adapter.stop(pico);
+ try {
+ adapter.stop(pico);
+ fail("IllegalStateException expected");
+ } catch (Exception e) {
+ assertEquals("Not started", e.getMessage());
+ }
+ }
+
+ @Test public void testComponentCannotBeDisposedIfAlreadyDisposed() {
+ Cached adapter = new Cached(
+ mockComponentAdapterSupportingLifecycleStrategy(true, true, true));
+ Container pico = new DefaultContainer();
+ adapter.start(pico);
+ adapter.stop(pico);
+ adapter.dispose(pico);
+ try {
+ adapter.dispose(pico);
+ fail("IllegalStateException expected");
+ } catch (Exception e) {
+ assertEquals("Already disposed", e.getMessage());
+ }
+ }
+
+ @Test public void testComponentIsStoppedAndDisposedIfStartedWhenFlushed() {
+ Cached adapter = new Cached(
+ mockComponentAdapterSupportingLifecycleStrategy(true, true, true));
+ Container pico = new DefaultContainer();
+ adapter.start(pico);
+ adapter.flush();
+ }
+
+ @Test public void testComponentIsNotStoppedAndDisposedWhenFlushedIfNotStarted() {
+ Cached adapter = new Cached(
+ mockComponentAdapterSupportingLifecycleStrategy(false, false, false));
+ adapter.flush();
+ }
+
+ @Test public void testComponentIsNotStoppedAndDisposedWhenFlushedIfDelegateDoesNotSupportLifecycle() {
+ Cached adapter = new Cached(
+ mockComponentAdapterNotSupportingLifecycleStrategy());
+ adapter.flush();
+ }
+
+ @Test public void testLifecycleIsIgnoredIfDelegateDoesNotSupportIt() {
+ Cached adapter = new Cached(
+ mockComponentAdapterNotSupportingLifecycleStrategy());
+ Container pico = new DefaultContainer();
+ adapter.start(pico);
+ adapter.stop(pico);
+ adapter.dispose(pico);
+ }
+
+ @Test public void testCanStopAComponentThatWasNeverStartedBecauseItHasNoLifecycle() {
+ MutableContainer pico = new DefaultContainer();
+
+ pico.addComponent(StringBuffer.class);
+
+ pico.start();
+
+ assertNotNull(pico.getComponent(StringBuffer.class));
+
+ pico.stop();
+ pico.dispose();
+ }
+
+ private ComponentAdapter mockComponentAdapterNotSupportingLifecycleStrategy() {
+ return mockery.mock(ComponentAdapter.class);
+ }
+
+ private ComponentAdapter mockComponentAdapterSupportingLifecycleStrategy(
+ final boolean start, final boolean stop, final boolean dispose) {
+ final boolean hasLifecycle = start || stop || dispose;
+ final ComponentAdapterSupportingLifecycleStrategy ca = mockery.mock(ComponentAdapterSupportingLifecycleStrategy.class);
+ mockery.checking(new Expectations(){{
+ if (start) {
+ atLeast(1).of(ca).start(with(any(Touchable.class)));
+ }
+ if (stop) {
+ one(ca).stop(with(any(Touchable.class)));
+ }
+ if (dispose) {
+ one(ca).dispose(with(any(Touchable.class)));
+ }
+ if (hasLifecycle) {
+ one(ca).getComponentInstance(with(any(Container.class)));
+ will(returnValue(new SimpleTouchable()));
+ }
+ one(ca).getComponentImplementation();
+ will(returnValue(SimpleTouchable.class));
+ one(ca).hasLifecycle(with(same(SimpleTouchable.class)));
+ will(returnValue(hasLifecycle));
+ }});
+ return ca;
+ }
+
+ public static interface ComponentAdapterSupportingLifecycleStrategy extends ComponentAdapter,
+ LifecycleStrategy {
+ }
+}
\ No newline at end of file
Added: incubator/composer/core/src/test/org/apache/composer/core/behaviors/CachingTestCase.java
URL: http://svn.apache.org/viewvc/incubator/composer/core/src/test/org/apache/composer/core/behaviors/CachingTestCase.java?rev=613775&view=auto
==============================================================================
--- incubator/composer/core/src/test/org/apache/composer/core/behaviors/CachingTestCase.java (added)
+++ incubator/composer/core/src/test/org/apache/composer/core/behaviors/CachingTestCase.java Sun Jan 20 22:41:26 2008
@@ -0,0 +1,90 @@
+/*****************************************************************************
+ * Copyright (C) PicoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ * Original code by *
+ *****************************************************************************/
+package org.apache.composer.core.behaviors;
+
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+import org.apache.composer.core.Characteristics;
+import org.apache.composer.core.ComponentAdapter;
+import org.apache.composer.core.ComponentFactory;
+import org.apache.composer.core.DefaultContainer;
+import org.apache.composer.core.adapters.InstanceAdapter;
+import org.apache.composer.core.injectors.ConstructorInjection;
+import org.apache.composer.core.injectors.ConstructorInjector;
+import org.apache.composer.core.lifecycle.NullLifecycleStrategy;
+import org.apache.composer.core.monitors.NullComponentMonitor;
+import org.apache.composer.core.tck.AbstractComponentFactoryTest;
+
+
+/**
+ * @author <a href="Rafal.Krzewski">rafal@caltha.pl</a>
+ */
+public class CachingTestCase extends AbstractComponentFactoryTest {
+
+ protected ComponentFactory createComponentFactory() {
+ return new Caching().wrap(new ConstructorInjection());
+ }
+
+ @Test public void testAddComponentUsesCachingBehavior() {
+ DefaultContainer pico =
+ new DefaultContainer(new Caching().wrap(new ConstructorInjection()));
+ pico.addComponent("foo", String.class);
+ ComponentAdapter foo = pico.getComponentAdapter("foo");
+ assertEquals(Cached.class, foo.getClass());
+ assertEquals(ConstructorInjector.class, ((AbstractBehavior) foo).getDelegate().getClass());
+ }
+
+ @Test public void testAddComponentUsesCachingBehaviorWithRedundantCacheProperty() {
+ DefaultContainer pico =
+ new DefaultContainer(new Caching().wrap(new ConstructorInjection()));
+ pico.change(Characteristics.CACHE).addComponent("foo", String.class);
+ ComponentAdapter foo = pico.getComponentAdapter("foo");
+ assertEquals(Cached.class, foo.getClass());
+ assertEquals(ConstructorInjector.class, ((AbstractBehavior) foo).getDelegate().getClass());
+ }
+
+ @Test public void testAddComponentNoesNotUseCachingBehaviorWhenNoCachePropertyIsSpecified() {
+ DefaultContainer pico =
+ new DefaultContainer(new Caching().wrap(new ConstructorInjection()));
+ pico.change(Characteristics.NO_CACHE).addComponent("foo", String.class);
+ ComponentAdapter foo = pico.getComponentAdapter("foo");
+ assertEquals(ConstructorInjector.class, foo.getClass());
+ }
+
+ @Test public void testAddAdapterUsesCachingBehavior() {
+ DefaultContainer pico =
+ new DefaultContainer(new Caching().wrap(new ConstructorInjection()));
+ pico.addAdapter(new InstanceAdapter("foo", "bar", new NullLifecycleStrategy(), new NullComponentMonitor()));
+ ComponentAdapter foo = pico.getComponentAdapter("foo");
+ assertEquals(Cached.class, foo.getClass());
+ assertEquals(InstanceAdapter.class, ((AbstractBehavior) foo).getDelegate().getClass());
+ }
+
+ @Test public void testAddAdapterUsesCachingBehaviorWithRedundantCacheProperty() {
+ DefaultContainer pico =
+ new DefaultContainer(new Caching().wrap(new ConstructorInjection()));
+ pico.change(Characteristics.CACHE).addAdapter(new InstanceAdapter("foo", "bar", new NullLifecycleStrategy(), new NullComponentMonitor()));
+ ComponentAdapter foo = pico.getComponentAdapter("foo");
+ assertEquals(Cached.class, foo.getClass());
+ assertEquals(InstanceAdapter.class, ((AbstractBehavior) foo).getDelegate().getClass());
+ }
+
+ @Test public void testAddAdapterNoesNotUseCachingBehaviorWhenNoCachePropertyIsSpecified() {
+ DefaultContainer pico =
+ new DefaultContainer(new Caching().wrap(new ConstructorInjection()));
+ pico.change(Characteristics.NO_CACHE).addAdapter(new InstanceAdapter("foo", "bar", new NullLifecycleStrategy(), new NullComponentMonitor()));
+ ComponentAdapter foo = pico.getComponentAdapter("foo");
+ assertEquals(InstanceAdapter.class, foo.getClass());
+ }
+
+
+}
\ No newline at end of file
Added: incubator/composer/core/src/test/org/apache/composer/core/behaviors/HiddenImplementationTestCase.java
URL: http://svn.apache.org/viewvc/incubator/composer/core/src/test/org/apache/composer/core/behaviors/HiddenImplementationTestCase.java?rev=613775&view=auto
==============================================================================
--- incubator/composer/core/src/test/org/apache/composer/core/behaviors/HiddenImplementationTestCase.java (added)
+++ incubator/composer/core/src/test/org/apache/composer/core/behaviors/HiddenImplementationTestCase.java Sun Jan 20 22:41:26 2008
@@ -0,0 +1,73 @@
+/*****************************************************************************
+ * Copyright (C) PicoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ * Original code by *
+ *****************************************************************************/
+package org.apache.composer.core.behaviors;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.MouseEvent;
+import java.awt.event.MouseListener;
+
+import org.junit.Test;
+import org.apache.composer.core.ComponentAdapter;
+import org.apache.composer.core.CompositionException;
+import org.apache.composer.core.injectors.ConstructorInjector;
+import org.apache.composer.core.lifecycle.NullLifecycleStrategy;
+import org.apache.composer.core.monitors.NullComponentMonitor;
+
+public class HiddenImplementationTestCase {
+
+ @Test public void testMultipleInterfacesCanBeHidden() {
+ ComponentAdapter ca = new ConstructorInjector(new Class[]{ActionListener.class, MouseListener.class}, Footle.class, null, new NullComponentMonitor(), new NullLifecycleStrategy(), false);
+ HiddenImplementation ihca = new HiddenImplementation(ca);
+ Object comp = ihca.getComponentInstance(null);
+ assertNotNull(comp);
+ assertTrue(comp instanceof ActionListener);
+ assertTrue(comp instanceof MouseListener);
+ }
+
+ @Test public void testNonInterfaceInArrayCantBeHidden() {
+ ComponentAdapter ca = new ConstructorInjector(new Class[]{String.class}, Footle.class, null, new NullComponentMonitor(), new NullLifecycleStrategy(), false);
+ HiddenImplementation ihca = new HiddenImplementation(ca);
+ try {
+ ihca.getComponentInstance(null);
+ fail("PicoCompositionException expected");
+ } catch (CompositionException e) {
+ // expected
+ }
+ }
+
+
+
+ public class Footle implements ActionListener, MouseListener {
+ public void actionPerformed(ActionEvent e) {
+ }
+
+ public void mouseClicked(MouseEvent e) {
+ }
+
+ public void mouseEntered(MouseEvent e) {
+ }
+
+ public void mouseExited(MouseEvent e) {
+ }
+
+ public void mousePressed(MouseEvent e) {
+ }
+
+ public void mouseReleased(MouseEvent e) {
+ }
+
+ }
+
+}
Added: incubator/composer/core/src/test/org/apache/composer/core/behaviors/ImplementationHidingTestCase.java
URL: http://svn.apache.org/viewvc/incubator/composer/core/src/test/org/apache/composer/core/behaviors/ImplementationHidingTestCase.java?rev=613775&view=auto
==============================================================================
--- incubator/composer/core/src/test/org/apache/composer/core/behaviors/ImplementationHidingTestCase.java (added)
+++ incubator/composer/core/src/test/org/apache/composer/core/behaviors/ImplementationHidingTestCase.java Sun Jan 20 22:41:26 2008
@@ -0,0 +1,134 @@
+/*****************************************************************************
+ * Copyright (c) PicoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ *****************************************************************************/
+
+package org.apache.composer.core.behaviors;
+
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import org.junit.Test;
+import org.apache.composer.core.Characteristics;
+import org.apache.composer.core.ComponentAdapter;
+import org.apache.composer.core.ComponentFactory;
+import org.apache.composer.core.DefaultContainer;
+import org.apache.composer.core.adapters.InstanceAdapter;
+import org.apache.composer.core.injectors.AdaptingInjection;
+import org.apache.composer.core.injectors.ConstructorInjection;
+import org.apache.composer.core.injectors.ConstructorInjector;
+import org.apache.composer.core.lifecycle.NullLifecycleStrategy;
+import org.apache.composer.core.monitors.NullComponentMonitor;
+import org.apache.composer.core.tck.AbstractComponentFactoryTest;
+
+
+public class ImplementationHidingTestCase extends AbstractComponentFactoryTest{
+
+ @Test public void testAddComponentUsesImplementationHidingBehavior() {
+ DefaultContainer pico =
+ new DefaultContainer(new ImplementationHiding().wrap(new ConstructorInjection()));
+ pico.addComponent("foo", String.class);
+ ComponentAdapter<?> foo = pico.getComponentAdapter("foo");
+ assertEquals(HiddenImplementation.class, foo.getClass());
+ assertEquals(ConstructorInjector.class, ((AbstractBehavior) foo).getDelegate().getClass());
+ }
+
+ @Test public void testAddComponentUsesImplementationHidingBehaviorWithRedundantHideImplProperty() {
+ DefaultContainer pico =
+ new DefaultContainer(new ImplementationHiding().wrap(new ConstructorInjection()));
+ pico.change(Characteristics.HIDE_IMPL).addComponent("foo", String.class);
+ ComponentAdapter<?> foo = pico.getComponentAdapter("foo");
+ assertEquals(HiddenImplementation.class, foo.getClass());
+ assertEquals(ConstructorInjector.class, ((AbstractBehavior) foo).getDelegate().getClass());
+ }
+
+ @Test public void testAddComponentNoesNotUseImplementationHidingBehaviorWhenNoCachePropertyIsSpecified() {
+ DefaultContainer pico =
+ new DefaultContainer(new ImplementationHiding().wrap(new ConstructorInjection()));
+ pico.change(Characteristics.NO_HIDE_IMPL).addComponent("foo", String.class);
+ ComponentAdapter<?> foo = pico.getComponentAdapter("foo");
+ assertEquals(ConstructorInjector.class, foo.getClass());
+ }
+
+ @Test public void testAddAdapterUsesImplementationHidingBehavior() {
+ DefaultContainer pico =
+ new DefaultContainer(new ImplementationHiding().wrap(new ConstructorInjection()));
+ pico.addAdapter(new InstanceAdapter("foo", "bar", new NullLifecycleStrategy(), new NullComponentMonitor()));
+ ComponentAdapter<?> foo = pico.getComponentAdapter("foo");
+ assertEquals(HiddenImplementation.class, foo.getClass());
+ assertEquals(InstanceAdapter.class, ((AbstractBehavior) foo).getDelegate().getClass());
+ }
+
+ @Test public void testAddAdapterUsesImplementationHidingBehaviorWithRedundantHideImplProperty() {
+ DefaultContainer pico =
+ new DefaultContainer(new ImplementationHiding().wrap(new ConstructorInjection()));
+ pico.change(Characteristics.HIDE_IMPL).addAdapter(new InstanceAdapter("foo", "bar", new NullLifecycleStrategy(), new NullComponentMonitor()));
+ ComponentAdapter<?> foo = pico.getComponentAdapter("foo");
+ assertEquals(HiddenImplementation.class, foo.getClass());
+ assertEquals(InstanceAdapter.class, ((AbstractBehavior) foo).getDelegate().getClass());
+ }
+
+ @Test public void testAddAdapterNoesNotUseImplementationHidingBehaviorWhenNoCachePropertyIsSpecified() {
+ DefaultContainer pico =
+ new DefaultContainer(new ImplementationHiding().wrap(new ConstructorInjection()));
+ pico.change(Characteristics.NO_HIDE_IMPL).addAdapter(new InstanceAdapter("foo", "bar", new NullLifecycleStrategy(), new NullComponentMonitor()));
+ ComponentAdapter<?> foo = pico.getComponentAdapter("foo");
+ assertEquals(InstanceAdapter.class, foo.getClass());
+ }
+
+
+ private final ComponentFactory implementationHidingComponentFactory =
+ new ImplementationHiding().wrap(new AdaptingInjection());
+
+ protected ComponentFactory createComponentFactory() {
+ return implementationHidingComponentFactory;
+ }
+
+
+ public static interface NeedsStringBuilder {
+ void foo();
+ }
+ public static class NeedsStringBuilderImpl implements NeedsStringBuilder {
+ StringBuilder sb;
+
+ public NeedsStringBuilderImpl(StringBuilder sb) {
+ this.sb = sb;
+ sb.append("<init>");
+ }
+ public void foo() {
+ sb.append("foo()");
+ }
+ }
+ public static class NeedsNeedsStringBuilder {
+
+ NeedsStringBuilder nsb;
+
+ public NeedsNeedsStringBuilder(NeedsStringBuilder nsb) {
+ this.nsb = nsb;
+ }
+ public void foo() {
+ nsb.foo();
+ }
+ }
+
+ @Test public void testLazyInstantiationSideEffectWhenForceOfDelayedInstantiationOfDependantClass() {
+ DefaultContainer pico =
+ new DefaultContainer(new ImplementationHiding().wrap(new Caching().wrap(new ConstructorInjection())));
+ pico.addComponent(StringBuilder.class);
+ pico.addComponent(NeedsStringBuilder.class, NeedsStringBuilderImpl.class);
+ pico.addComponent(NeedsNeedsStringBuilder.class);
+ NeedsNeedsStringBuilder nnsb = pico.getComponent(NeedsNeedsStringBuilder.class);
+ assertNotNull(nnsb);
+ StringBuilder sb = pico.getComponent(StringBuilder.class);
+ assertEquals("", sb.toString()); // not instantiated yet
+ nnsb.foo();
+ assertEquals("<init>foo()", sb.toString()); // instantiated
+ }
+
+
+}
\ No newline at end of file
Added: incubator/composer/core/src/test/org/apache/composer/core/behaviors/InterceptingTestCase.java
URL: http://svn.apache.org/viewvc/incubator/composer/core/src/test/org/apache/composer/core/behaviors/InterceptingTestCase.java?rev=613775&view=auto
==============================================================================
--- incubator/composer/core/src/test/org/apache/composer/core/behaviors/InterceptingTestCase.java (added)
+++ incubator/composer/core/src/test/org/apache/composer/core/behaviors/InterceptingTestCase.java Sun Jan 20 22:41:26 2008
@@ -0,0 +1,160 @@
+/*****************************************************************************
+ * Copyright (C) PicoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ * Original code by *
+ *****************************************************************************/
+package org.apache.composer.core.behaviors;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import org.junit.Test;
+import org.apache.composer.core.DefaultContainer;
+
+public class InterceptingTestCase {
+
+ public static interface Foo {
+ void one();
+ String two(String a, int b);
+ }
+
+ public static class FooImpl implements Foo {
+ private StringBuilder sb;
+
+ public FooImpl(StringBuilder sb) {
+ this.sb = sb;
+ }
+
+ public void one() {
+ sb.append("call-one(),");
+ }
+
+ public String two(String a, int b) {
+ sb.append("call-two('"+a+"',"+b+"),");
+ return "two";
+ }
+ }
+
+ @Test public void testPreAndPostObservation() {
+ final StringBuilder sb = new StringBuilder();
+ DefaultContainer pico = new DefaultContainer(new Intercepting());
+ pico.addComponent(StringBuilder.class, sb);
+ pico.addComponent(Foo.class, FooImpl.class);
+
+ Intercepted intercepted = pico.getComponentAdapter(Foo.class).findAdapterOfType(Intercepted.class);
+ final Intercepted.Controller interceptor = intercepted.getController();
+ intercepted.addPreInvocation(Foo.class, new Foo() {
+ public void one() {
+ sb.append("pre-one(),");
+ }
+ public String two(String a, int b) {
+ sb.append("pre-two('"+a+"',"+b+"),");
+ return null;
+ }
+ });
+ intercepted.addPostInvocation(Foo.class, new Foo() {
+ public void one() {
+ sb.append("addPostInvocation-one(),");
+ }
+ public String two(String a, int b) {
+ assertEquals("two", interceptor.getOriginalRetVal());
+ sb.append("addPostInvocation-two('"+a+"',"+b+"),");
+ return null;
+ }
+ });
+
+ Foo foo = pico.getComponent(Foo.class);
+ assertNotNull(foo);
+ foo.one();
+ assertEquals("two", foo.two("hello", 99));
+ assertEquals("pre-one(),call-one(),addPostInvocation-one(),pre-two('hello',99),call-two('hello',99),addPostInvocation-two('hello',99),", sb.toString());
+ assertEquals("Intercepted:ConstructorInjector-interface org.apache.composer.core.behaviors.InterceptingTestCase$Foo", pico.getComponentAdapter(Foo.class).toString());
+ }
+
+ @Test public void testPreCanBlockInvocationWithAlternateReturnValue() {
+ final StringBuilder sb = new StringBuilder();
+ DefaultContainer pico = new DefaultContainer(new Intercepting());
+ pico.addComponent(Foo.class, FooImpl.class);
+ pico.addComponent(StringBuilder.class, sb);
+
+ Intercepted intercepted = pico.getComponentAdapter(Foo.class).findAdapterOfType(Intercepted.class);
+ final Intercepted.Controller interceptor = intercepted.getController();
+ intercepted.addPreInvocation(Foo.class, new Foo() {
+ public void one() {
+ interceptor.veto();
+ sb.append("veto-one(),");
+ }
+
+ public String two(String a, int b) {
+ interceptor.veto();
+ sb.append("veto-two('"+a+"',"+b+"),");
+ return "isVetoed";
+ }
+ });
+
+ Foo foo = pico.getComponent(Foo.class);
+ assertNotNull(foo);
+ foo.one();
+ assertEquals("isVetoed", foo.two("hello", 99));
+ assertEquals("veto-one(),veto-two('hello',99),", sb.toString());
+ assertEquals("Intercepted:ConstructorInjector-interface org.apache.composer.core.behaviors.InterceptingTestCase$Foo", pico.getComponentAdapter(Foo.class).toString());
+ }
+
+ @Test public void testOverrideOfReturnValue() {
+ final StringBuilder sb = new StringBuilder();
+ DefaultContainer pico = new DefaultContainer(new Intercepting());
+ pico.addComponent(Foo.class, FooImpl.class);
+ pico.addComponent(StringBuilder.class, sb);
+ Intercepted intercepted = pico.getComponentAdapter(Foo.class).findAdapterOfType(Intercepted.class);
+ final Intercepted.Controller interceptor = intercepted.getController();
+ intercepted.addPreInvocation(Foo.class, new Foo() {
+ public void one() {
+ sb.append("pre-one(),");
+ }
+
+ public String two(String a, int b) {
+ sb.append("pre-two('"+a+"',"+b+"),");
+ return null;
+ }
+ });
+ intercepted.addPostInvocation(Foo.class, new Foo() {
+ public void one() {
+ interceptor.override();
+ sb.append("override-one(),");
+ }
+
+ public String two(String a, int b) {
+ interceptor.override();
+ sb.append("override-two('"+a+"',"+b+"),");
+ return "x";
+ }
+ });
+
+ Foo foo = pico.getComponent(Foo.class);
+ assertNotNull(foo);
+ foo.one();
+ assertEquals("x", foo.two("hello", 99));
+ assertEquals("pre-one(),call-one(),override-one(),pre-two('hello',99),call-two('hello',99),override-two('hello',99),", sb.toString());
+ assertEquals("Intercepted:ConstructorInjector-interface org.apache.composer.core.behaviors.InterceptingTestCase$Foo", pico.getComponentAdapter(Foo.class).toString());
+ }
+
+ @Test public void testNothingHappensIfNoPreOrPost() {
+ final StringBuilder sb = new StringBuilder();
+ DefaultContainer pico = new DefaultContainer(new Intercepting());
+ pico.addComponent(Foo.class, FooImpl.class);
+ pico.addComponent(StringBuilder.class, sb);
+ Foo foo = pico.getComponent(Foo.class);
+ assertNotNull(foo);
+ foo.one();
+ assertEquals("two", foo.two("hello", 99));
+ assertEquals("call-one(),call-two('hello',99),", sb.toString());
+ assertEquals("Intercepted:ConstructorInjector-interface org.apache.composer.core.behaviors.InterceptingTestCase$Foo", pico.getComponentAdapter(Foo.class).toString());
+ }
+
+
+
+}
\ No newline at end of file
Added: incubator/composer/core/src/test/org/apache/composer/core/behaviors/LockedTestCase.java
URL: http://svn.apache.org/viewvc/incubator/composer/core/src/test/org/apache/composer/core/behaviors/LockedTestCase.java?rev=613775&view=auto
==============================================================================
--- incubator/composer/core/src/test/org/apache/composer/core/behaviors/LockedTestCase.java (added)
+++ incubator/composer/core/src/test/org/apache/composer/core/behaviors/LockedTestCase.java Sun Jan 20 22:41:26 2008
@@ -0,0 +1,27 @@
+/*****************************************************************************
+ * Copyright (C) PicoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ * Original code by *
+ *****************************************************************************/
+package org.apache.composer.core.behaviors;
+
+import org.apache.composer.core.BehaviorFactory;
+import org.apache.composer.core.ComponentAdapter;
+
+/** @author Paul Hammant */
+public final class LockedTestCase extends SynchronizedTestCase {
+
+ protected ComponentAdapter makeComponentAdapter(ComponentAdapter componentAdapter) {
+ return new Locked(componentAdapter);
+ }
+
+ protected BehaviorFactory makeBehaviorFactory() {
+ return new Locking();
+ }
+
+
+}
\ No newline at end of file