]> git.somenet.org - pub/jan/dst18.git/blob - ass2-di/src/test/java/dst/ass2/di/TransparentInjectionTest.java
Add template for assignment 2
[pub/jan/dst18.git] / ass2-di / src / test / java / dst / ass2 / di / TransparentInjectionTest.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.assertSame;
7 import static org.junit.Assert.assertTrue;
8 import static org.junit.Assert.fail;
9
10 import java.util.Collections;
11 import java.util.List;
12
13 import org.junit.Before;
14 import org.junit.FixMethodOrder;
15 import org.junit.Test;
16 import org.junit.runners.MethodSorters;
17
18 import dst.ass2.di.type.Container;
19 import dst.ass2.di.type.SimpleSingleton;
20
21 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
22 public class TransparentInjectionTest {
23
24     protected IInjectionController ic;
25
26     @Before
27     public void before() {
28         ic = InjectionControllerFactory.getTransparentInstance();
29     }
30
31     /**
32      * Injecting prototypes and singletons into an object.
33      */
34     @Test
35     public void test_01_inject() {
36         Container container = new Container();
37
38         assertNotNull("'timestamp' must not be null.", container.timestamp);
39         Long timestamp = container.timestamp;
40
41         assertSame("Initial timestamp was modified.", timestamp, container.timestamp);
42         assertNotNull("'id' must not be null.", container.id);
43         assertNotNull("'first' must not be null.", container.first);
44         assertNotNull("'second' must not be null.", container.second);
45         assertNotNull("'component' must not be null.", container.component);
46         assertNotNull("'anotherComponent' must not be null.", container.anotherComponent);
47
48         assertSame("Singletons must be the same object instance.", container.first, container.second);
49         assertNotSame("Prototype references must not be the same object instance.", container.component, container.anotherComponent);
50
51         List<Long> ids = InjectionUtils.getIds(container);
52         Collections.sort(ids);
53         assertEquals("Initialized object graph with 4 components.", 4, ids.size());
54
55         for (long i = ids.get(0); i < ids.get(0) + ids.size(); i++) {
56             assertTrue("There is no component with ID " + i + ".", ids.contains(i));
57         }
58     }
59
60     /**
61      * Injecting components into hierarchies.
62      */
63     @Test
64     public void test_02_hierarchy() throws IllegalAccessException {
65         Container container = new Container();
66         Long oldId = container.id;
67
68         SimpleSingleton singleton = ic.getSingletonInstance(SimpleSingleton.class);
69         assertNotNull("'id' must not be null.", singleton.id);
70
71         // Check that the same singleton is used
72         assertSame("Singletons must be the same object instance.", container.first, singleton);
73
74         // Verify that exactly 4 new component IDs where used
75         assertEquals("More than 4 components were instantiated with the container.", oldId + 3L, new Container().id.longValue());
76
77         try {
78             new SimpleSingleton();
79             fail(InjectionException.class.getSimpleName() + " expected");
80         } catch (InjectionException e) {
81             // expected
82         }
83     }
84 }