org.picocontainer.adapters.InstanceAdapterTestCase Maven / Gradle / Ivy
/*****************************************************************************
* 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.picocontainer.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.picocontainer.ComponentAdapter;
import org.picocontainer.DefaultPicoContainer;
import org.picocontainer.Disposable;
import org.picocontainer.MutablePicoContainer;
import org.picocontainer.PicoContainer;
import org.picocontainer.Startable;
import org.picocontainer.lifecycle.NullLifecycleStrategy;
import org.picocontainer.lifecycle.StartableLifecycleStrategy;
import org.picocontainer.monitors.NullComponentMonitor;
import org.picocontainer.tck.AbstractComponentAdapterTest;
import org.picocontainer.testmodel.NullLifecycle;
import org.picocontainer.testmodel.SimpleTouchable;
import org.picocontainer.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, null));
}
@Test public void testDefaultLifecycleStrategy() {
LifecycleComponent component = new LifecycleComponent();
InstanceAdapter adapter =
new InstanceAdapter(LifecycleComponent.class, component, new StartableLifecycleStrategy(new NullComponentMonitor()),
new NullComponentMonitor());
PicoContainer pico = new DefaultPicoContainer();
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());
PicoContainer pico = new DefaultPicoContainer();
adapter.start(pico);
adapter.stop(pico);
adapter.dispose(pico);
assertEquals("