1   /*
2    * Copyright 2007-2008 Naoki NOSE.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package jp.liq.container;
17  
18  import static org.junit.Assert.*;
19  
20  import org.junit.Before;
21  import org.junit.Test;
22  
23  /**
24   * @author nosen
25   *
26   */
27  public class ContainerTest implements ContainerVocabulary {
28      
29      private Container container;
30  
31      @Before
32      public void setUp() {
33          this.container = new Container();
34      }
35      public static interface A {
36          
37      }
38  
39      public static class AImpl1 implements A {
40          
41      }
42      
43      public static class AImpl2 implements A {
44          
45      }
46  
47      public static class B {
48          private A a;
49          private D d;
50  
51          public B(D d, A a) {
52              this.d = d;
53              this.a = a;
54          }
55          
56          public A getA() {
57              return a;
58          }
59          
60          public D getD() {
61              return d;
62          }
63          
64      }
65  
66   
67      public static class C {
68          private A a;
69          private B b;
70          public C(A a, B b) {
71              this.a = a;
72              this.b = b;
73          }
74  
75          public A getA() {
76              return a;
77          }
78          
79          public B getB() {
80              return b;
81          }
82      }
83      
84      public static class D {
85          
86      }
87  
88      @Test
89      public void recursiveResolution() {
90  
91          components.configure(container)
92              .define(component.mapping(A.class, AImpl1.class));
93          
94          sentinel.configure(container);
95          
96          C c = container.get(C.class);
97  
98          assertNotNull(c);
99          assertNotNull(c.getA());
100         assertNotNull(c.getB());
101         assertNotNull(c.getB().getD());
102     }
103 
104     @Test
105     public void multiModuleResoution() {
106  
107         components.configure(container)
108             .define(component.mapping(A.class, AImpl2.class));
109 
110         components.configure(container)
111             .define(component.mapping(A.class, AImpl1.class));
112     
113         sentinel.configure(container);
114 
115         C c = container.get(C.class);
116 
117         assertNotNull(c);
118         assertNotNull(c.getA());
119         assertTrue(c.getA() instanceof AImpl2);
120         assertNotNull(c.getB());
121         assertNotNull(c.getB().getD());
122     }
123 
124     @Test
125     public void componentNotFound() {
126         try {
127             container.get(A.class);
128             fail();
129         } catch (ContainerException e) {
130             assertEquals(e.getErrorCode(), ContainerException.Reason.COMPONENT_NOT_FOUND);
131         }
132         
133     }
134 
135     @Test
136     public void missingDependency() {
137         components.configure(container)
138             .define(component.from(ctor.of(B.class)))
139             .define(component.from(ctor.of(D.class)))
140             .define(component.from(ctor.of(AImpl1.class)));
141         
142         try {
143             container.get(B.class);
144             fail();
145         } catch (ContainerException e) {
146             assertEquals(e.getErrorCode(), ContainerException.Reason.MISSING_DEPENDENCY);
147         }
148             
149     }
150 
151     public static class C1 {
152         public C1(C2 c) {
153             
154         }
155     }
156 
157     public static class C2 {
158         public C2(C3 c) {
159             
160         }
161     }
162 
163     public static class C3 {
164         public C3(C3 c3) {
165             
166         }
167     }
168 
169     @Test
170     public void circularDependency() {
171         
172         sentinel.configure(container);
173 
174         try {
175             container.get(C1.class);
176             fail();
177         } catch (ContainerException e) {
178             assertEquals(e.getErrorCode(), ContainerException.Reason.CIRCULAR_REFERENCE);
179         }
180     }
181 
182     @Test
183     public void inclusion() {
184         Container parent = new Container();
185         
186         components.configure(parent)
187             .define(component.mapping(A.class, AImpl1.class));
188         
189         container.include(parent);
190 
191         sentinel.configure(container);
192 
193         C c = container.get(C.class);
194 
195         assertNotNull(c);
196         assertNotNull(c.getA());
197         assertNotNull(c.getB());
198         assertNotNull(c.getB().getD());
199     }
200 }