MentaContainer

Rev

Rev 20 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 soliveira 1
package org.mentacontainer.impl;
2
 
3
import junit.framework.Assert;
4
 
5
import org.junit.Test;
6
import org.mentacontainer.Container;
7
 
8
public class MentaContainerTest {
9
 
10
    @Test
11
    public void testSimpleGet() {
12
 
13
        Container c = new MentaContainer();
14
 
15
        c.ioc("myStr", String.class);
16
 
17
        Assert.assertEquals("", c.get("myStr"));
18
 
19
        String s1 = (String) c.get("myStr");
20
 
21
        String s2 = (String) c.get("myStr");
22
 
23
        Assert.assertTrue(s1 != s2);
24
 
25
        Assert.assertTrue(s1.equals(s2));
26
    }
27
 
28
    @Test
29
    public void testConstructorInit() {
30
 
31
        Container c = new MentaContainer();
32
 
33
        c.ioc("myStr", String.class);
34
 
35
        c.init("myStr", "hello");
36
 
37
        Assert.assertEquals("hello", c.get("myStr"));
38
 
39
        String s1 = (String) c.get("myStr");
40
 
41
        String s2 = (String) c.get("myStr");
42
 
43
        Assert.assertTrue(s1 != s2);
44
 
45
        Assert.assertTrue(s1.equals(s2));
46
 
47
        c.ioc("anotherStr", String.class);
48
 
49
        c.init("anotherStr", "hi");
50
 
51
        String s3 = (String) c.get("anotherStr");
52
 
53
        Assert.assertTrue(s1 != s3);
54
 
55
        Assert.assertFalse(s1.equals(s3));
56
    }
57
 
58
    @Test
59
    public void testSingleton() {
60
 
61
        Container c = new MentaContainer();
62
 
63
        c.ioc("myStr", String.class, true);
64
 
65
        c.init("myStr", "hello");
66
 
67
        Assert.assertEquals("hello", c.get("myStr"));
68
 
69
        String s1 = (String) c.get("myStr");
70
 
71
        String s2 = (String) c.get("myStr");
72
 
73
        Assert.assertTrue(s1 == s2);
74
 
75
        Assert.assertTrue(s1.equals(s2));
76
    }
77
 
78
    @Test(expected = RuntimeException.class)
79
    public void testAddingComponentTwice() {
80
 
81
        Container c = new MentaContainer();
82
 
83
        c.ioc("myStr", String.class).ioc("myStr", String.class);
84
    }
85
 
86
    @Test(expected = RuntimeException.class)
87
    public void testSettingWiringTwice() {
88
 
89
        Container c = new MentaContainer();
90
 
91
        c.autowire("myThread", Runnable.class);
92
 
93
        c.autowire("myThread", Runnable.class);
94
    }
95
 
96
    @Test(expected = RuntimeException.class)
97
    public void testAddingWiringNoInterface() {
98
 
99
        Container c = new MentaContainer();
100
 
101
        c.autowire("myThread", Thread.class);
102
    }
103
}