3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertNotSame;
6 import static org.junit.Assert.assertNull;
7 import static org.junit.Assert.assertSame;
8 import static org.junit.Assert.assertTrue;
10 import java.util.Collection;
12 import org.junit.Before;
13 import org.junit.Test;
15 import dst.ass2.di.type.Container;
16 import dst.ass2.di.type.Invalid;
17 import dst.ass2.di.type.SimpleComponent;
18 import dst.ass2.di.type.SimpleSingleton;
20 public class BasicInjectionTest {
21 private IInjectionController ic;
24 public void before() {
25 ic = InjectionControllerFactory.getNewStandaloneInstance();
29 * Initializing two prototype components.
32 public void testSimpleComponent() {
33 SimpleComponent first = new SimpleComponent();
34 assertNull("The ID of an uninitialized component must be null", first.id);
37 assertEquals("The ID of the first component must be 0", Long.valueOf(0L), first.id);
39 SimpleComponent second = new SimpleComponent();
40 ic.initialize(second);
41 assertEquals("The ID of the second component must be 1", Long.valueOf(1L), second.id);
45 * Retrieving a singleton twice.
48 public void testSimpleSingleton() {
49 SimpleSingleton first = ic.getSingletonInstance(SimpleSingleton.class);
50 SimpleSingleton second = ic.getSingletonInstance(SimpleSingleton.class);
52 assertNotNull("The ID of the singleton must not be null.", first.id);
53 assertEquals("Singleton has wrong ID.", Long.valueOf(0L), first.id);
54 assertSame("Singletons must be the same object instance.", first, second);
58 * Injecting prototypes and singletons into an object.
61 public void testInject() {
62 Container container = new Container();
64 assertNotNull("'timestamp' must not be null.", container.timestamp);
65 Long timestamp = container.timestamp;
67 assertNull("'id' must be null.", container.id);
68 assertNull("'first' must be null.", container.first);
69 assertNull("'second' must be null.", container.second);
70 assertNull("'component' must ne null.", container.component);
71 assertNull("'anotherComponent' must be null.", container.anotherComponent);
73 ic.initialize(container);
74 assertSame("Initial timestamp was modified.", timestamp, container.timestamp);
75 assertNotNull("'id' must not be null.", container.id);
76 assertNotNull("'first' must not be null.", container.first);
77 assertNotNull("'second' must not be null.", container.second);
78 assertNotNull("'component' must not be null.", container.component);
79 assertNotNull("'anotherComponent' must not be null.", container.anotherComponent);
81 assertSame("Singletons must be the same object instance.", container.first, container.second);
82 assertNotSame("Prototype references must not be the same object instance.", container.component, container.anotherComponent);
84 Collection<Long> ids = InjectionUtils.getIds(container);
85 assertEquals("Initialized object graph with 4 components.", 4, ids.size());
87 for (long i = 0; i < ids.size(); i++) {
88 assertTrue("There is no component with ID " + i + ".", ids.contains(i));
93 * Attempts to initialize an object that is not annotated with {@link dst.ass2.di.annotation.Component @Component}.
95 @Test(expected = InjectionException.class)
96 public void testInvalidPrototype() {
97 ic.initialize(new Invalid());
101 * Attempts to retrieve a singleton instance of a class that is not annotated with
102 * {@link dst.ass2.di.annotation.Component @Component}.
104 @Test(expected = InjectionException.class)
105 public void testInvalidSingleton() {
106 ic.getSingletonInstance(Invalid.class);
110 * Attempts to retrieve a singleton instance of a prototype component.
112 @Test(expected = InjectionException.class)
113 public void testPrototypeAsSingleton() {
114 ic.getSingletonInstance(SimpleComponent.class);