1 /* 2 * Copyright (C) 2015 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.builder; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static org.junit.Assert.fail; 21 22 import org.junit.Test; 23 import org.junit.runner.RunWith; 24 import org.junit.runners.JUnit4; 25 26 @RunWith(JUnit4.class) 27 public class BuilderTest { 28 interfaceBuilder()29 @Test public void interfaceBuilder() { 30 TestComponentWithBuilderInterface.Builder builder = 31 DaggerTestComponentWithBuilderInterface.builder(); 32 33 // Make sure things fail if we don't set our required modules. 34 try { 35 builder.build(); 36 fail(); 37 } catch(IllegalStateException expected) {} 38 39 builder.intModule(new IntModuleIncludingDoubleAndFloat(1)) 40 .stringModule(new StringModule("sam")) 41 .depComponent(new DepComponent() {}); 42 builder.doubleModule(new DoubleModule()); 43 // Don't set other modules -- make sure it works. 44 45 TestComponentWithBuilderInterface component = builder.build(); 46 assertThat(component.s()).isEqualTo("sam"); 47 assertThat(component.i()).isEqualTo(1); 48 assertThat(component.d()).isEqualTo(4.2d); 49 assertThat(component.f()).isEqualTo(5.5f); 50 assertThat(component.l()).isEqualTo(6L); 51 } 52 abstractClassBuilder()53 @Test public void abstractClassBuilder() { 54 TestComponentWithBuilderAbstractClass.Builder builder = 55 TestComponentWithBuilderAbstractClass.builder(); 56 57 // Make sure things fail if we don't set our required modules. 58 try { 59 builder.build(); 60 fail(); 61 } catch(IllegalStateException expected) {} 62 63 builder.intModule(new IntModuleIncludingDoubleAndFloat(1)) 64 .stringModule(new StringModule("sam")) 65 .depComponent(new DepComponent() {}); 66 builder.doubleModule(new DoubleModule()); 67 // Don't set other modules -- make sure it works. 68 69 TestComponentWithBuilderAbstractClass component = builder.build(); 70 assertThat(component.s()).isEqualTo("sam"); 71 assertThat(component.i()).isEqualTo(1); 72 assertThat(component.d()).isEqualTo(4.2d); 73 assertThat(component.f()).isEqualTo(5.5f); 74 assertThat(component.l()).isEqualTo(6L); 75 } 76 interfaceGenericBuilder()77 @Test public void interfaceGenericBuilder() { 78 TestComponentWithGenericBuilderInterface.Builder builder = 79 DaggerTestComponentWithGenericBuilderInterface.builder(); 80 81 // Make sure things fail if we don't set our required modules. 82 try { 83 builder.build(); 84 fail(); 85 } catch(IllegalStateException expected) {} 86 87 builder.setM2(new IntModuleIncludingDoubleAndFloat(1)) 88 .setM1(new StringModule("sam")) 89 .depComponent(new DepComponent() {}); 90 builder.doubleModule(new DoubleModule()); 91 // Don't set other modules -- make sure it works. 92 93 TestComponentWithGenericBuilderInterface component = builder.build(); 94 assertThat(component.s()).isEqualTo("sam"); 95 assertThat(component.i()).isEqualTo(1); 96 assertThat(component.d()).isEqualTo(4.2d); 97 assertThat(component.f()).isEqualTo(5.5f); 98 assertThat(component.l()).isEqualTo(6L); 99 } 100 abstractClassGenericBuilder()101 @Test public void abstractClassGenericBuilder() { 102 TestComponentWithGenericBuilderAbstractClass.Builder builder = 103 DaggerTestComponentWithGenericBuilderAbstractClass.builder(); 104 105 // Make sure things fail if we don't set our required modules. 106 try { 107 builder.build(); 108 fail(); 109 } catch(IllegalStateException expected) {} 110 111 builder.setM2(new IntModuleIncludingDoubleAndFloat(1)) 112 .setM1(new StringModule("sam")) 113 .depComponent(new DepComponent() {}); 114 builder.doubleModule(new DoubleModule()); 115 // Don't set other modules -- make sure it works. 116 117 TestComponentWithGenericBuilderAbstractClass component = builder.build(); 118 assertThat(component.s()).isEqualTo("sam"); 119 assertThat(component.i()).isEqualTo(1); 120 assertThat(component.d()).isEqualTo(4.2d); 121 assertThat(component.f()).isEqualTo(5.5f); 122 assertThat(component.l()).isEqualTo(6L); 123 } 124 subcomponents_interface()125 @Test public void subcomponents_interface() { 126 ParentComponent parent = DaggerParentComponent.create(); 127 TestChildComponentWithBuilderInterface.Builder builder1 = parent.childInterfaceBuilder(); 128 try { 129 builder1.build(); 130 fail(); 131 } catch(IllegalStateException expected) {} 132 133 builder1.setM2(new IntModuleIncludingDoubleAndFloat(1)) 134 .setM1(new StringModule("sam")) 135 .set(new ByteModule((byte)7)); 136 builder1.set(new FloatModule()); 137 TestChildComponentWithBuilderInterface child1 = builder1.build(); 138 assertThat(child1.s()).isEqualTo("sam"); 139 assertThat(child1.i()).isEqualTo(1); 140 assertThat(child1.d()).isEqualTo(4.2d); 141 assertThat(child1.f()).isEqualTo(5.5f); 142 assertThat(child1.l()).isEqualTo(6L); 143 assertThat(child1.b()).isEqualTo((byte)7); 144 } 145 subcomponents_abstractclass()146 @Test public void subcomponents_abstractclass() { 147 ParentComponent parent = DaggerParentComponent.create(); 148 TestChildComponentWithBuilderAbstractClass.Builder builder2 = 149 parent.childAbstractClassBuilder(); 150 try { 151 builder2.build(); 152 fail(); 153 } catch(IllegalStateException expected) {} 154 155 builder2.setM2(new IntModuleIncludingDoubleAndFloat(10)) 156 .setM1(new StringModule("tara")) 157 .set(new ByteModule((byte)70)); 158 builder2.set(new FloatModule()); 159 TestChildComponentWithBuilderAbstractClass child2 = builder2.build(); 160 assertThat(child2.s()).isEqualTo("tara"); 161 assertThat(child2.i()).isEqualTo(10); 162 assertThat(child2.d()).isEqualTo(4.2d); 163 assertThat(child2.f()).isEqualTo(5.5f); 164 assertThat(child2.l()).isEqualTo(6L); 165 assertThat(child2.b()).isEqualTo((byte)70); 166 } 167 168 @Test grandchildren()169 public void grandchildren() { 170 ParentComponent parent = DaggerParentComponent.create(); 171 MiddleChild middle1 = parent.middleBuilder().set(new StringModule("sam")).build(); 172 Grandchild grandchild1 = 173 middle1.grandchildBuilder().set(new IntModuleIncludingDoubleAndFloat(21)).build(); 174 Grandchild grandchild2 = 175 middle1.grandchildBuilder().set(new IntModuleIncludingDoubleAndFloat(22)).build(); 176 177 assertThat(middle1.s()).isEqualTo("sam"); 178 assertThat(grandchild1.i()).isEqualTo(21); 179 assertThat(grandchild1.s()).isEqualTo("sam"); 180 assertThat(grandchild2.i()).isEqualTo(22); 181 assertThat(grandchild2.s()).isEqualTo("sam"); 182 183 // Make sure grandchildren from newer children have no relation to the older ones. 184 MiddleChild middle2 = parent.middleBuilder().set(new StringModule("tara")).build(); 185 Grandchild grandchild3 = 186 middle2.grandchildBuilder().set(new IntModuleIncludingDoubleAndFloat(23)).build(); 187 Grandchild grandchild4 = 188 middle2.grandchildBuilder().set(new IntModuleIncludingDoubleAndFloat(24)).build(); 189 190 assertThat(middle2.s()).isEqualTo("tara"); 191 assertThat(grandchild3.i()).isEqualTo(23); 192 assertThat(grandchild3.s()).isEqualTo("tara"); 193 assertThat(grandchild4.i()).isEqualTo(24); 194 assertThat(grandchild4.s()).isEqualTo("tara"); 195 } 196 197 @Test diamondGrandchildren()198 public void diamondGrandchildren() { 199 ParentComponent parent = DaggerParentComponent.create(); 200 MiddleChild middle = parent.middleBuilder().set(new StringModule("sam")).build(); 201 OtherMiddleChild other = parent.otherBuilder().set(new StringModule("tara")).build(); 202 203 Grandchild middlegrand = 204 middle.grandchildBuilder().set(new IntModuleIncludingDoubleAndFloat(21)).build(); 205 Grandchild othergrand = 206 other.grandchildBuilder().set(new IntModuleIncludingDoubleAndFloat(22)).build(); 207 208 assertThat(middle.s()).isEqualTo("sam"); 209 assertThat(other.s()).isEqualTo("tara"); 210 assertThat(middlegrand.s()).isEqualTo("sam"); 211 assertThat(othergrand.s()).isEqualTo("tara"); 212 assertThat(middlegrand.i()).isEqualTo(21); 213 assertThat(othergrand.i()).isEqualTo(22); 214 } 215 216 @Test genericSubcomponentMethod()217 public void genericSubcomponentMethod() { 218 ParentOfGenericComponent parent = 219 DaggerParentOfGenericComponent.builder().stringModule(new StringModule("sam")).build(); 220 Grandchild.Builder builder = parent.subcomponentBuilder(); 221 Grandchild child = builder.set(new IntModuleIncludingDoubleAndFloat(21)).build(); 222 assertThat(child.s()).isEqualTo("sam"); 223 assertThat(child.i()).isEqualTo(21); 224 } 225 226 @Test requireSubcomponentBuilderProviders()227 public void requireSubcomponentBuilderProviders() { 228 ParentComponent parent = DaggerParentComponent.create(); 229 MiddleChild middle = 230 parent 231 .requiresMiddleChildBuilder() 232 .subcomponentBuilderProvider() 233 .get() 234 .set(new StringModule("sam")) 235 .build(); 236 Grandchild grandchild = 237 middle 238 .requiresGrandchildBuilder() 239 .subcomponentBuilderProvider() 240 .get() 241 .set(new IntModuleIncludingDoubleAndFloat(12)) 242 .build(); 243 assertThat(middle.s()).isEqualTo("sam"); 244 assertThat(grandchild.i()).isEqualTo(12); 245 assertThat(grandchild.s()).isEqualTo("sam"); 246 } 247 248 @Test requireSubcomponentBuilders()249 public void requireSubcomponentBuilders() { 250 ParentComponent parent = DaggerParentComponent.create(); 251 MiddleChild middle = 252 parent 253 .requiresMiddleChildBuilder() 254 .subcomponentBuilder() 255 .set(new StringModule("sam")) 256 .build(); 257 Grandchild grandchild = 258 middle 259 .requiresGrandchildBuilder() 260 .subcomponentBuilder() 261 .set(new IntModuleIncludingDoubleAndFloat(12)) 262 .build(); 263 assertThat(middle.s()).isEqualTo("sam"); 264 assertThat(grandchild.i()).isEqualTo(12); 265 assertThat(grandchild.s()).isEqualTo("sam"); 266 } 267 } 268