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