1*f585d8a3SJacky Wang /* 2*f585d8a3SJacky Wang * Copyright (C) 2016 The Dagger Authors. 3*f585d8a3SJacky Wang * 4*f585d8a3SJacky Wang * Licensed under the Apache License, Version 2.0 (the "License"); 5*f585d8a3SJacky Wang * you may not use this file except in compliance with the License. 6*f585d8a3SJacky Wang * You may obtain a copy of the License at 7*f585d8a3SJacky Wang * 8*f585d8a3SJacky Wang * http://www.apache.org/licenses/LICENSE-2.0 9*f585d8a3SJacky Wang * 10*f585d8a3SJacky Wang * Unless required by applicable law or agreed to in writing, software 11*f585d8a3SJacky Wang * distributed under the License is distributed on an "AS IS" BASIS, 12*f585d8a3SJacky Wang * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*f585d8a3SJacky Wang * See the License for the specific language governing permissions and 14*f585d8a3SJacky Wang * limitations under the License. 15*f585d8a3SJacky Wang */ 16*f585d8a3SJacky Wang 17*f585d8a3SJacky Wang package dagger.functional.builderbinds; 18*f585d8a3SJacky Wang 19*f585d8a3SJacky Wang import static com.google.common.truth.Truth.assertThat; 20*f585d8a3SJacky Wang import static org.junit.Assert.fail; 21*f585d8a3SJacky Wang 22*f585d8a3SJacky Wang import com.google.common.collect.ImmutableList; 23*f585d8a3SJacky Wang import dagger.functional.builderbinds.TestComponent.Builder; 24*f585d8a3SJacky Wang import java.util.Arrays; 25*f585d8a3SJacky Wang import org.junit.Test; 26*f585d8a3SJacky Wang import org.junit.runner.RunWith; 27*f585d8a3SJacky Wang import org.junit.runners.JUnit4; 28*f585d8a3SJacky Wang 29*f585d8a3SJacky Wang @RunWith(JUnit4.class) 30*f585d8a3SJacky Wang public final class BuilderBindsTest { 31*f585d8a3SJacky Wang 32*f585d8a3SJacky Wang @Test builderBinds()33*f585d8a3SJacky Wang public void builderBinds() { 34*f585d8a3SJacky Wang TestComponent.Builder builder = 35*f585d8a3SJacky Wang DaggerTestComponent.builder() 36*f585d8a3SJacky Wang .count(5) 37*f585d8a3SJacky Wang .l(10L) 38*f585d8a3SJacky Wang .input("foo") 39*f585d8a3SJacky Wang .nullableInput("bar") 40*f585d8a3SJacky Wang .listOfString(Arrays.asList("x", "y", "z")); 41*f585d8a3SJacky Wang builder.boundInSubtype(20); 42*f585d8a3SJacky Wang TestComponent component = builder.build(); 43*f585d8a3SJacky Wang assertThat(component.count()).isEqualTo(5); 44*f585d8a3SJacky Wang assertThat(component.input()).isEqualTo("foo"); 45*f585d8a3SJacky Wang assertThat(component.nullableInput()).isEqualTo("bar"); 46*f585d8a3SJacky Wang assertThat(component.listOfString()).containsExactly("x", "y", "z").inOrder(); 47*f585d8a3SJacky Wang } 48*f585d8a3SJacky Wang 49*f585d8a3SJacky Wang @Test builderBindsNullableWithNull()50*f585d8a3SJacky Wang public void builderBindsNullableWithNull() { 51*f585d8a3SJacky Wang Builder builder = 52*f585d8a3SJacky Wang DaggerTestComponent.builder() 53*f585d8a3SJacky Wang .count(5) 54*f585d8a3SJacky Wang .l(10L) 55*f585d8a3SJacky Wang .input("foo") 56*f585d8a3SJacky Wang .nullableInput(null) 57*f585d8a3SJacky Wang .listOfString(ImmutableList.of()); 58*f585d8a3SJacky Wang builder.boundInSubtype(20); 59*f585d8a3SJacky Wang TestComponent component = builder.build(); 60*f585d8a3SJacky Wang 61*f585d8a3SJacky Wang assertThat(component.count()).isEqualTo(5); 62*f585d8a3SJacky Wang assertThat(component.input()).isEqualTo("foo"); 63*f585d8a3SJacky Wang assertThat(component.nullableInput()).isNull(); 64*f585d8a3SJacky Wang assertThat(component.listOfString()).isEmpty(); 65*f585d8a3SJacky Wang } 66*f585d8a3SJacky Wang 67*f585d8a3SJacky Wang @Test builderBindsNonNullableWithNull()68*f585d8a3SJacky Wang public void builderBindsNonNullableWithNull() { 69*f585d8a3SJacky Wang try { 70*f585d8a3SJacky Wang DaggerTestComponent.builder().count(5).l(10L).input(null); 71*f585d8a3SJacky Wang fail("expected NullPointerException"); 72*f585d8a3SJacky Wang } catch (NullPointerException expected) { 73*f585d8a3SJacky Wang } 74*f585d8a3SJacky Wang } 75*f585d8a3SJacky Wang 76*f585d8a3SJacky Wang @Test builderBindsPrimitiveNotSet()77*f585d8a3SJacky Wang public void builderBindsPrimitiveNotSet() { 78*f585d8a3SJacky Wang try { 79*f585d8a3SJacky Wang TestComponent.Builder builder = 80*f585d8a3SJacky Wang DaggerTestComponent.builder() 81*f585d8a3SJacky Wang .l(10L) 82*f585d8a3SJacky Wang .input("foo") 83*f585d8a3SJacky Wang .nullableInput("bar") 84*f585d8a3SJacky Wang .listOfString(ImmutableList.of()); 85*f585d8a3SJacky Wang builder.boundInSubtype(20); 86*f585d8a3SJacky Wang builder.build(); 87*f585d8a3SJacky Wang fail("expected IllegalStateException"); 88*f585d8a3SJacky Wang } catch (IllegalStateException expected) { 89*f585d8a3SJacky Wang } 90*f585d8a3SJacky Wang } 91*f585d8a3SJacky Wang 92*f585d8a3SJacky Wang @Test builderBindsNonNullableNotSet()93*f585d8a3SJacky Wang public void builderBindsNonNullableNotSet() { 94*f585d8a3SJacky Wang try { 95*f585d8a3SJacky Wang TestComponent.Builder builder = 96*f585d8a3SJacky Wang DaggerTestComponent.builder() 97*f585d8a3SJacky Wang .count(5) 98*f585d8a3SJacky Wang .l(10L) 99*f585d8a3SJacky Wang .nullableInput("foo") 100*f585d8a3SJacky Wang .listOfString(ImmutableList.of()); 101*f585d8a3SJacky Wang builder.boundInSubtype(20); 102*f585d8a3SJacky Wang builder.build(); 103*f585d8a3SJacky Wang fail("expected IllegalStateException"); 104*f585d8a3SJacky Wang } catch (IllegalStateException expected) { 105*f585d8a3SJacky Wang } 106*f585d8a3SJacky Wang } 107*f585d8a3SJacky Wang 108*f585d8a3SJacky Wang @Test builderBindsNullableNotSet()109*f585d8a3SJacky Wang public void builderBindsNullableNotSet() { 110*f585d8a3SJacky Wang Builder builder = 111*f585d8a3SJacky Wang DaggerTestComponent.builder().count(5).l(10L).input("foo").listOfString(ImmutableList.of()); 112*f585d8a3SJacky Wang builder.boundInSubtype(20); 113*f585d8a3SJacky Wang TestComponent component = builder.build(); 114*f585d8a3SJacky Wang assertThat(component.count()).isEqualTo(5); 115*f585d8a3SJacky Wang assertThat(component.input()).isEqualTo("foo"); 116*f585d8a3SJacky Wang assertThat(component.nullableInput()).isNull(); 117*f585d8a3SJacky Wang assertThat(component.listOfString()).isEmpty(); 118*f585d8a3SJacky Wang } 119*f585d8a3SJacky Wang } 120