View Javadoc

1   /*
2    * Grapht, an open source dependency injector.
3    * Copyright 2014-2015 various contributors (see CONTRIBUTORS.txt)
4    * Copyright 2010-2014 Regents of the University of Minnesota
5    *
6    * This program is free software; you can redistribute it and/or modify
7    * it under the terms of the GNU Lesser General Public License as
8    * published by the Free Software Foundation; either version 2.1 of the
9    * License, or (at your option) any later version.
10   *
11   * This program is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13   * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14   * details.
15   *
16   * You should have received a copy of the GNU General Public License along with
17   * this program; if not, write to the Free Software Foundation, Inc., 51
18   * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19   */
20  package org.grouplens.grapht;
21  
22  import org.grouplens.grapht.types.dft.*;
23  import org.junit.Before;
24  import org.junit.Test;
25  
26  import static org.hamcrest.Matchers.*;
27  import static org.junit.Assert.assertThat;
28  
29  /**
30   * Acceptance tests for default bindings (via annotations and such). This may well have
31   * overlap with some other test cases, but consolidates various expected default behavior in one
32   * place.
33   */
34  public class DefaultBindingsTest {
35      private InjectorBuilder b;
36      // TODO Add tests here for DefaultInt and friends
37  
38      @Before
39      public void setup() {
40          b = InjectorBuilder.create(DefaultBindingsTest.class.getClassLoader());
41      }
42  
43      @Test
44      public void testDefaultImplementation() throws InjectionException {
45          Injector inj = b.build();
46          IDftImpl a = inj.getInstance(IDftImpl.class);
47          assertThat(a, notNullValue());
48          assertThat(a, instanceOf(CDftImplA.class));
49      }
50  
51      @Test
52      public void testOverrideDefaultImplementation() throws InjectionException {
53          b.bind(IDftImpl.class).to(CDftImplB.class);
54          Injector inj = b.build();
55          IDftImpl a = inj.getInstance(IDftImpl.class);
56          assertThat(a, notNullValue());
57          assertThat(a, instanceOf(CDftImplB.class));
58      }
59  
60      @Test
61      public void testDefaultProvider() throws InjectionException {
62          Injector inj = b.build();
63          IDftProvider a = inj.getInstance(IDftProvider.class);
64          assertThat(a, notNullValue());
65          assertThat(a, instanceOf(PDftProvider.Impl.class));
66      }
67  
68      @Test
69      public void testOverrideDefaultProvider() throws InjectionException {
70          b.bind(IDftProvider.class).to(CDftProvider.class);
71          Injector inj = b.build();
72          IDftProvider a = inj.getInstance(IDftProvider.class);
73          assertThat(a, notNullValue());
74          assertThat(a, instanceOf(CDftProvider.class));
75      }
76  
77      @Test
78      public void testPropDefaultImplementation() throws InjectionException {
79          Injector inj = b.build();
80          IPropDftImpl a = inj.getInstance(IPropDftImpl.class);
81          assertThat(a, notNullValue());
82          assertThat(a, instanceOf(CPropDftImplA.class));
83      }
84  
85      @Test
86      public void testPropOverrideDefaultImplementation() throws InjectionException {
87          b.bind(IPropDftImpl.class).to(CPropDftImplB.class);
88          Injector inj = b.build();
89          IPropDftImpl a = inj.getInstance(IPropDftImpl.class);
90          assertThat(a, notNullValue());
91          assertThat(a, instanceOf(CPropDftImplB.class));
92      }
93  
94      @Test
95      public void testPropDefaultProvider() throws InjectionException {
96          Injector inj = b.build();
97          IPropDftProvider a = inj.getInstance(IPropDftProvider.class);
98          assertThat(a, notNullValue());
99          assertThat(a, instanceOf(PPropDftProvider.Impl.class));
100     }
101 
102     @Test
103     public void testPropOverrideDefaultProvider() throws InjectionException {
104         b.bind(IPropDftProvider.class).to(CPropDftProvider.class);
105         Injector inj = b.build();
106         IPropDftProvider a = inj.getInstance(IPropDftProvider.class);
107         assertThat(a, notNullValue());
108         assertThat(a, instanceOf(CPropDftProvider.class));
109     }
110 
111     @Test
112     public void testPropImplDoubleDepCache() throws InjectionException {
113         Injector inj = b.build();
114         CPropImplDoubleDep obj = inj.getInstance(CPropImplDoubleDep.class);
115         assertThat(obj.right, sameInstance(obj.left));
116     }
117 
118     @Test
119     public void testPropImplDoubleDepUncache() throws InjectionException {
120         b.setDefaultCachePolicy(CachePolicy.NEW_INSTANCE);
121         Injector inj = b.build();
122         CPropImplDoubleDep obj = inj.getInstance(CPropImplDoubleDep.class);
123         assertThat(obj.right, not(sameInstance(obj.left)));
124     }
125 
126     @Test
127     public void testPropImplDoubleDepPropUncache() throws InjectionException {
128         Injector inj = b.build();
129         CPropImplDoubleDepNoCache obj = inj.getInstance(CPropImplDoubleDepNoCache.class);
130         assertThat(obj.right, not(sameInstance(obj.left)));
131     }
132 
133     @Test
134     public void testDftImplDoubleDepCache() throws InjectionException {
135         Injector inj = b.build();
136         CDoubleDep obj = inj.getInstance(CDoubleDep.class);
137         assertThat(obj.right, sameInstance(obj.left));
138     }
139 
140     @Test
141     public void testDftImplDoubleDepUncache() throws InjectionException {
142         b.setDefaultCachePolicy(CachePolicy.NEW_INSTANCE);
143         Injector inj = b.build();
144         CDoubleDep obj = inj.getInstance(CDoubleDep.class);
145         assertThat(obj.right, not(sameInstance(obj.left)));
146     }
147 
148     @Test
149     public void testDftImplDoubleDepPropUncache() throws InjectionException {
150         Injector inj = b.build();
151         CDoubleDepNoCache obj = inj.getInstance(CDoubleDepNoCache.class);
152         assertThat(obj.right, not(sameInstance(obj.left)));
153     }
154 
155     /**
156      * Test that the DefaultImplementation annotation overrides META-INF.
157      */
158     @Test
159     public void testPreemptDefaultImplementation() throws InjectionException {
160         Injector inj = b.build();
161         IPreemptDftImpl a = inj.getInstance(IPreemptDftImpl.class);
162         assertThat(a, notNullValue());
163         assertThat(a, instanceOf(CPreemptDftImplA.class));
164     }
165 }