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