1   /*
2    * Copyright 2006 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.example;
17  
18  import static org.junit.Assert.assertNotNull;
19  
20  import java.lang.annotation.Retention;
21  import java.lang.annotation.RetentionPolicy;
22  
23  import jp.liq.container.Container;
24  import jp.liq.container.ContainerVocabulary;
25  import jp.liq.container.vocabulary.Factory;
26  
27  import org.junit.Test;
28  
29  /**
30   * @author nose
31   *
32   */
33  public class ComponentExample implements ContainerVocabulary {
34      public static class Foo {
35          public Foo() {
36              
37          }
38  
39          public Foo(Bar bar) {
40              
41          }
42      }
43  
44      public interface Bar {
45      }
46      
47      public static class BarImpl implements Bar {
48          
49      }
50  
51      @Test
52      public void ctor() {
53          Container container = new Container();
54  
55          components.configure(container)
56              .define(component.from(ctor.of(Foo.class, Bar.class)))
57              .define(component.instance(Bar.class, new BarImpl()));
58  
59          Foo foo = container.get(Foo.class);
60          assertNotNull(foo);
61          
62      }
63  
64      @Retention(RetentionPolicy.RUNTIME)
65      public @interface FactoryMethod {
66          
67      }
68  
69      public class ObjectMethodModule {
70  
71          @Factory
72          public Bar createBar() {
73              return new BarImpl();
74          }
75  
76          @Factory
77          public Foo createFoo(Bar bar) {
78              return new Foo(bar);
79          }
80      }
81  
82      @Test
83      public void objectMethods() {
84          Container container = new Container();
85  
86          components.configure(container)
87              .define(component.factory(new ObjectMethodModule()));
88  
89          Foo foo = container.get(Foo.class);
90          assertNotNull(foo);        
91      }
92  
93  
94      public static class Factory1 {
95          @Factory
96          public Bar createBar() {
97              return new BarImpl();
98          }
99  
100         @Factory
101         public Foo createFoo(Bar bar) {
102             return new Foo(bar);
103         }        
104     }
105 
106     @Test
107     public void componentMethods() {
108         Container container = new Container();
109 
110         components.configure(container)
111             .define(component.factory(Factory1.class));
112             
113         sentinel.configure(container);
114 
115         Foo foo = container.get(Foo.class);
116 
117         assertNotNull(foo);
118     }
119 
120 }