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.example;
17  
18  import static org.junit.Assert.assertNotNull;
19  import jp.liq.container.Container;
20  import jp.liq.container.ContainerVocabulary;
21  import jp.liq.container.vocabulary.Inject;
22  
23  import org.junit.Test;
24  
25  /**
26   * @author nose
27   *
28   */
29  public class InjectorExample implements ContainerVocabulary {
30  
31      public static class Foo1 {
32          @Inject 
33          public Bar bar;
34      }
35  
36      public static class Foo2 {
37          private Bar bar;
38  
39          public Bar getBar() {
40              return bar;
41          }
42  
43          @Inject
44          public void setBar(Bar bar) {
45              this.bar = bar;
46          }
47      }
48  
49      public static class Bar {
50          
51      }
52  
53      @Test
54      public void fieldInjector() {
55          Container container = new Container();
56  
57          sentinel.configure(container).with(injector.toFields());
58          
59          Foo1 foo1 = container.get(Foo1.class);
60  
61          assertNotNull(foo1);
62          assertNotNull(foo1.bar);
63      }
64  
65      @Test
66      public void methodInjector() {
67          Container container = new Container();
68  
69          sentinel.configure(container).with(injector.toMethods());
70  
71          Foo2 foo2 = container.get(Foo2.class);
72          assertNotNull(foo2);
73          assertNotNull(foo2.getBar());
74      }
75  
76  }