• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 The Dagger Authors.
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 
17 package dagger.functional.factory;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 import static java.lang.annotation.ElementType.FIELD;
21 import static java.lang.annotation.ElementType.METHOD;
22 import static java.lang.annotation.ElementType.PARAMETER;
23 import static java.lang.annotation.RetentionPolicy.RUNTIME;
24 import static org.junit.Assert.fail;
25 
26 import dagger.BindsInstance;
27 import dagger.Component;
28 import java.lang.annotation.Retention;
29 import java.lang.annotation.Target;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.junit.runners.JUnit4;
33 
34 /** Tests for component factories with {@link BindsInstance} parameters. */
35 @RunWith(JUnit4.class)
36 public final class FactoryBindsInstanceTest {
37 
38   @Component
39   interface BindsInstanceComponent {
string()40     String string();
41 
42     @Component.Factory
43     interface Factory {
create(@indsInstance String string)44       BindsInstanceComponent create(@BindsInstance String string);
45     }
46   }
47 
48   @Test
bindsInstance()49   public void bindsInstance() {
50     BindsInstanceComponent component =
51         DaggerFactoryBindsInstanceTest_BindsInstanceComponent.factory().create("baz");
52     assertThat(component.string()).isEqualTo("baz");
53   }
54 
55   @Test
nonNullableBindsInstance_failsOnNull()56   public void nonNullableBindsInstance_failsOnNull() {
57     try {
58       DaggerFactoryBindsInstanceTest_BindsInstanceComponent.factory().create(null);
59       fail();
60     } catch (NullPointerException expected) {
61     }
62   }
63 
64   @Target({METHOD, PARAMETER, FIELD})
65   @Retention(RUNTIME)
66   @interface Nullable {}
67 
68   @Component
69   interface NullableBindsInstanceComponent {
70     @Nullable
string()71     String string();
72 
73     @Component.Factory
74     interface Factory {
create(@indsInstance @ullable String string)75       NullableBindsInstanceComponent create(@BindsInstance @Nullable String string);
76     }
77   }
78 
79   @Test
nullableBindsInstance_doesNotFailOnNull()80   public void nullableBindsInstance_doesNotFailOnNull() {
81     NullableBindsInstanceComponent component =
82         DaggerFactoryBindsInstanceTest_NullableBindsInstanceComponent.factory().create(null);
83     assertThat(component.string()).isEqualTo(null);
84   }
85 
86   @Component
87   interface PrimitiveBindsInstanceComponent {
getInt()88     int getInt();
89 
90     @Component.Factory
91     interface Factory {
create(@indsInstance int i)92       PrimitiveBindsInstanceComponent create(@BindsInstance int i);
93     }
94   }
95 
96   @Test
primitiveBindsInstance()97   public void primitiveBindsInstance() {
98     PrimitiveBindsInstanceComponent component =
99         DaggerFactoryBindsInstanceTest_PrimitiveBindsInstanceComponent.factory().create(1);
100     assertThat(component.getInt()).isEqualTo(1);
101   }
102 }
103