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.assertEquals;
19  import static org.junit.Assert.*;
20  import jp.liq.container.Container;
21  import jp.liq.container.ContainerVocabulary;
22  import jp.liq.container.vocabulary.Factory;
23  import jp.liq.container.vocabulary.Recycle;
24  
25  import org.junit.Before;
26  import org.junit.Test;
27  /**
28   * @author nosen
29   *
30   */
31  public class ComponentsTest implements ContainerVocabulary {
32      private Container container;
33      public class StringFactory  {
34  
35          @Factory
36          public String createString() {
37              return "foo";
38          }
39  
40      }
41  
42      @Before
43      public void setUp() {
44          container = new Container();
45      }
46  
47      @Test
48      public void factoryObject() {
49  
50          components.configure(container)
51              .define(component.factory(new StringFactory()));
52  
53          assertEquals("foo", container.get(String.class));
54  
55      }
56  
57      public static class Foo {
58          @Factory
59          public String createString() {
60              return "bar";
61          }
62      }
63  
64      @Test
65      public void factoryComponent() {
66  
67          components.configure(container)
68              .define(component.factory(Foo.class));
69          
70          sentinel.configure(container);
71  
72          assertEquals("bar", container.get(String.class));
73      }
74      
75      @Test
76      public void objectComponent() {
77          String s1 = "foo";
78          
79          components.configure(container)
80              .define(component.instance(String.class, s1));
81          
82          String s2 = container.get(String.class);
83          
84          assertSame(s1, s2);
85      }
86  
87      @Recycle
88      public static class Recycled {
89      	
90      }
91  
92      @Test
93      public void recycledComponent() {
94      	sentinel.configure(container);
95      	Recycled r1 = container.get(Recycled.class);
96      	Recycled r2 = container.get(Recycled.class);
97      	assertSame(r1, r2);
98      }
99  }