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.annotation.DefaultNull;
23  import org.junit.Test;
24  
25  import javax.annotation.Nullable;
26  import javax.inject.Inject;
27  import javax.inject.Qualifier;
28  import java.lang.annotation.*;
29  
30  import static org.hamcrest.Matchers.*;
31  import static org.junit.Assert.assertThat;
32  
33  /**
34   * Tests for <a href="https://github.com/grouplens/grapht/issues/73">#73</a>.
35   *
36   * @author <a href="http://www.grouplens.org">GroupLens Research</a>
37   */
38  public class QualifiedDefaultNullTest {
39      @Test
40      public void testQualifiedDefaultNull() throws InjectionException {
41          InjectorBuilder bld = InjectorBuilder.create();
42          Injector inj = bld.build();
43  
44          // With no bindings, we should get a null inner class.
45          MainC main = inj.getInstance(MainC.class);
46          assertThat(main, notNullValue());
47          assertThat(main.dependency, nullValue());
48      }
49  
50      @Test
51      public void testQualifiedDefaultNullWithBinding() throws InjectionException {
52          InjectorBuilder bld = InjectorBuilder.create();
53          bld.bind(TestQ.class, DepC.class)
54             .to(DepC.class);
55          Injector inj = bld.build();
56  
57          // With a bindings, we should get an instance
58          MainC main = inj.getInstance(MainC.class);
59          assertThat(main, notNullValue());
60          assertThat(main.dependency,
61                     allOf(notNullValue(),
62                           instanceOf(DepC.class)));
63      }
64  
65      /**
66       * Qualifier with default of {@code null}.
67       */
68      @Qualifier
69      @Retention(RetentionPolicy.RUNTIME)
70      @Target(ElementType.PARAMETER)
71      @DefaultNull
72      @Documented
73      public static @interface TestQ {}
74  
75      /**
76       * Component with qualified dependency on concrete class.
77       */
78      public static class MainC {
79          private final DepC dependency;
80  
81          @Inject
82          public MainC(@Nullable @TestQ DepC dep) {
83              dependency = dep;
84          }
85      }
86  
87      public static class DepC {
88          @Inject
89          public DepC() {}
90      }
91  }