]> git.somenet.org - pub/jan/dst18.git/blob - ass2-di/src/test/java/dst/ass2/di/BasicInjectionTest.java
Add template for assignment 2
[pub/jan/dst18.git] / ass2-di / src / test / java / dst / ass2 / di / BasicInjectionTest.java
1 package dst.ass2.di;
2
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;
9
10 import java.util.Collection;
11
12 import org.junit.Before;
13 import org.junit.Test;
14
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;
19
20 public class BasicInjectionTest {
21     private IInjectionController ic;
22
23     @Before
24     public void before() {
25         ic = InjectionControllerFactory.getNewStandaloneInstance();
26     }
27
28     /**
29      * Initializing two prototype components.
30      */
31     @Test
32     public void testSimpleComponent() {
33         SimpleComponent first = new SimpleComponent();
34         assertNull("The ID of an uninitialized component must be null", first.id);
35
36         ic.initialize(first);
37         assertEquals("The ID of the first component must be 0", Long.valueOf(0L), first.id);
38
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);
42     }
43
44     /**
45      * Retrieving a singleton twice.
46      */
47     @Test
48     public void testSimpleSingleton() {
49         SimpleSingleton first = ic.getSingletonInstance(SimpleSingleton.class);
50         SimpleSingleton second = ic.getSingletonInstance(SimpleSingleton.class);
51
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);
55     }
56
57     /**
58      * Injecting prototypes and singletons into an object.
59      */
60     @Test
61     public void testInject() {
62         Container container = new Container();
63
64         assertNotNull("'timestamp' must not be null.", container.timestamp);
65         Long timestamp = container.timestamp;
66
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);
72
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);
80
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);
83
84         Collection<Long> ids = InjectionUtils.getIds(container);
85         assertEquals("Initialized object graph with 4 components.", 4, ids.size());
86
87         for (long i = 0; i < ids.size(); i++) {
88             assertTrue("There is no component with ID " + i + ".", ids.contains(i));
89         }
90     }
91
92     /**
93      * Attempts to initialize an object that is not annotated with {@link dst.ass2.di.annotation.Component @Component}.
94      */
95     @Test(expected = InjectionException.class)
96     public void testInvalidPrototype() {
97         ic.initialize(new Invalid());
98     }
99
100     /**
101      * Attempts to retrieve a singleton instance of a class that is not annotated with
102      * {@link dst.ass2.di.annotation.Component @Component}.
103      */
104     @Test(expected = InjectionException.class)
105     public void testInvalidSingleton() {
106         ic.getSingletonInstance(Invalid.class);
107     }
108
109     /**
110      * Attempts to retrieve a singleton instance of a prototype component.
111      */
112     @Test(expected = InjectionException.class)
113     public void testPrototypeAsSingleton() {
114         ic.getSingletonInstance(SimpleComponent.class);
115     }
116 }