1 /*
2  * Copyright (C) 2016 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.builderbinds;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 import static org.junit.Assert.fail;
21 
22 import com.google.common.collect.ImmutableList;
23 import dagger.functional.builderbinds.TestComponent.Builder;
24 import java.util.Arrays;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.junit.runners.JUnit4;
28 
29 @RunWith(JUnit4.class)
30 public final class BuilderBindsTest {
31 
32   @Test
builderBinds()33   public void builderBinds() {
34     TestComponent.Builder builder =
35         DaggerTestComponent.builder()
36             .count(5)
37             .l(10L)
38             .input("foo")
39             .nullableInput("bar")
40             .listOfString(Arrays.asList("x", "y", "z"));
41     builder.boundInSubtype(20);
42     TestComponent component = builder.build();
43     assertThat(component.count()).isEqualTo(5);
44     assertThat(component.input()).isEqualTo("foo");
45     assertThat(component.nullableInput()).isEqualTo("bar");
46     assertThat(component.listOfString()).containsExactly("x", "y", "z").inOrder();
47   }
48 
49   @Test
builderBindsNullableWithNull()50   public void builderBindsNullableWithNull() {
51     Builder builder =
52         DaggerTestComponent.builder()
53             .count(5)
54             .l(10L)
55             .input("foo")
56             .nullableInput(null)
57             .listOfString(ImmutableList.of());
58     builder.boundInSubtype(20);
59     TestComponent component = builder.build();
60 
61     assertThat(component.count()).isEqualTo(5);
62     assertThat(component.input()).isEqualTo("foo");
63     assertThat(component.nullableInput()).isNull();
64     assertThat(component.listOfString()).isEmpty();
65   }
66 
67   @Test
builderBindsNonNullableWithNull()68   public void builderBindsNonNullableWithNull() {
69     try {
70       DaggerTestComponent.builder().count(5).l(10L).input(null);
71       fail("expected NullPointerException");
72     } catch (NullPointerException expected) {
73     }
74   }
75 
76   @Test
builderBindsPrimitiveNotSet()77   public void builderBindsPrimitiveNotSet() {
78     try {
79       TestComponent.Builder builder =
80           DaggerTestComponent.builder()
81               .l(10L)
82               .input("foo")
83               .nullableInput("bar")
84               .listOfString(ImmutableList.of());
85       builder.boundInSubtype(20);
86       builder.build();
87       fail("expected IllegalStateException");
88     } catch (IllegalStateException expected) {
89     }
90   }
91 
92   @Test
builderBindsNonNullableNotSet()93   public void builderBindsNonNullableNotSet() {
94     try {
95       TestComponent.Builder builder =
96           DaggerTestComponent.builder()
97               .count(5)
98               .l(10L)
99               .nullableInput("foo")
100               .listOfString(ImmutableList.of());
101       builder.boundInSubtype(20);
102       builder.build();
103       fail("expected IllegalStateException");
104     } catch (IllegalStateException expected) {
105     }
106   }
107 
108   @Test
builderBindsNullableNotSet()109   public void builderBindsNullableNotSet() {
110     Builder builder =
111         DaggerTestComponent.builder().count(5).l(10L).input("foo").listOfString(ImmutableList.of());
112     builder.boundInSubtype(20);
113     TestComponent component = builder.build();
114     assertThat(component.count()).isEqualTo(5);
115     assertThat(component.input()).isEqualTo("foo");
116     assertThat(component.nullableInput()).isNull();
117     assertThat(component.listOfString()).isEmpty();
118   }
119 }
120