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 java.lang.annotation.RetentionPolicy.RUNTIME; 21 import static org.junit.Assert.fail; 22 23 import dagger.Component; 24 import dagger.Module; 25 import dagger.Provides; 26 import dagger.Subcomponent; 27 import java.lang.annotation.Retention; 28 import javax.inject.Inject; 29 import javax.inject.Provider; 30 import javax.inject.Scope; 31 import javax.inject.Singleton; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import org.junit.runners.JUnit4; 35 36 @RunWith(JUnit4.class) 37 public class BuilderTest { 38 @Subcomponent( 39 modules = { 40 StringModule.class, 41 IntModuleIncludingDoubleAndFloat.class, 42 LongModule.class, 43 ByteModule.class 44 }) 45 interface TestChildComponentWithBuilderAbstractClass { s()46 String s(); 47 i()48 int i(); 49 l()50 long l(); 51 f()52 float f(); 53 d()54 double d(); 55 b()56 byte b(); 57 58 abstract static class SharedBuilder<B, C, M1, M2> { build()59 abstract C build(); // Test resolving return type of build() 60 setM1(M1 m1)61 abstract B setM1(M1 m1); // Test resolving return type & param of setter 62 setM2(M2 m2)63 abstract SharedBuilder<B, C, M1, M2> setM2(M2 m2); // Test being overridden 64 setM3(DoubleModule doubleModule)65 abstract void setM3(DoubleModule doubleModule); // Test being overridden 66 set( FloatModule floatModule)67 abstract SharedBuilder<B, C, M1, M2> set( 68 FloatModule floatModule); // Test returning supertype. 69 } 70 71 @Subcomponent.Builder 72 abstract static class Builder 73 extends TestChildComponentWithBuilderAbstractClass.SharedBuilder< 74 Builder, 75 TestChildComponentWithBuilderAbstractClass, 76 StringModule, 77 IntModuleIncludingDoubleAndFloat> { 78 @Override setM2(IntModuleIncludingDoubleAndFloat m2)79 abstract Builder setM2(IntModuleIncludingDoubleAndFloat m2); // Test covariance 80 81 @Override setM3(DoubleModule doubleModule)82 abstract void setM3(DoubleModule doubleModule); // Test simple overrides allowed 83 set(ByteModule byteModule)84 abstract void set(ByteModule byteModule); 85 86 // Note we're missing LongModule -- it's implicit 87 } 88 } 89 90 @Subcomponent( 91 modules = { 92 StringModule.class, 93 IntModuleIncludingDoubleAndFloat.class, 94 LongModule.class, 95 ByteModule.class 96 }) 97 interface TestChildComponentWithBuilderInterface { s()98 String s(); 99 i()100 int i(); 101 l()102 long l(); 103 f()104 float f(); 105 d()106 double d(); 107 b()108 byte b(); 109 110 interface SharedBuilder<B, C, M1, M2> { build()111 C build(); // Test resolving return type of build() 112 setM1(M1 m1)113 B setM1(M1 m1); // Test resolving return type & param of setter 114 setM2(M2 m2)115 SharedBuilder<B, C, M1, M2> setM2(M2 m2); // Test being overridden 116 setM3(DoubleModule doubleModule)117 void setM3(DoubleModule doubleModule); // Test being overridden 118 set(FloatModule floatModule)119 SharedBuilder<B, C, M1, M2> set(FloatModule floatModule); // Test return type is supertype. 120 } 121 122 @Subcomponent.Builder 123 interface Builder 124 extends TestChildComponentWithBuilderInterface.SharedBuilder< 125 Builder, 126 TestChildComponentWithBuilderInterface, 127 StringModule, 128 IntModuleIncludingDoubleAndFloat> { 129 @Override setM2(IntModuleIncludingDoubleAndFloat m2)130 Builder setM2(IntModuleIncludingDoubleAndFloat m2); // Test covariant overrides 131 132 @Override setM3(DoubleModule doubleModule)133 void setM3(DoubleModule doubleModule); // Test simple overrides allowed 134 set(ByteModule byteModule)135 void set(ByteModule byteModule); 136 137 // Note we're missing LongModule -- it's implicit 138 } 139 } 140 141 @Component( 142 modules = {StringModule.class, IntModuleIncludingDoubleAndFloat.class, LongModule.class}, 143 dependencies = DepComponent.class) 144 abstract static class TestComponentWithBuilderAbstractClass { 145 builder()146 static Builder builder() { 147 return DaggerBuilderTest_TestComponentWithBuilderAbstractClass.builder(); 148 } 149 s()150 abstract String s(); 151 i()152 abstract int i(); 153 l()154 abstract long l(); 155 f()156 abstract float f(); 157 d()158 abstract double d(); 159 160 abstract static class SharedBuilder { 161 // Make sure we use the overriding signature. build()162 abstract Object build(); 163 stringModule(@uppressWarnings"unused") StringModule stringModule)164 Object stringModule(@SuppressWarnings("unused") StringModule stringModule) { 165 return null; 166 } 167 ignoredLongModule(@uppressWarnings"unused") LongModule longModule)168 SharedBuilder ignoredLongModule(@SuppressWarnings("unused") LongModule longModule) { 169 return null; 170 } 171 } 172 173 @Component.Builder 174 abstract static class Builder extends TestComponentWithBuilderAbstractClass.SharedBuilder { 175 @Override build()176 abstract TestComponentWithBuilderAbstractClass build(); // Narrowing return type 177 178 @Override stringModule(StringModule stringModule)179 abstract Builder stringModule(StringModule stringModule); // Make abstract & narrow 180 intModule(IntModuleIncludingDoubleAndFloat intModule)181 abstract Builder intModule(IntModuleIncludingDoubleAndFloat intModule); 182 doubleModule(DoubleModule doubleModule)183 abstract void doubleModule(DoubleModule doubleModule); // Module w/o args 184 depComponent(DepComponent depComponent)185 abstract void depComponent(DepComponent depComponent); 186 ignoredIntModule( @uppressWarnings"unused") IntModuleIncludingDoubleAndFloat intModule)187 Builder ignoredIntModule( 188 @SuppressWarnings("unused") IntModuleIncludingDoubleAndFloat intModule) { 189 return null; 190 } 191 192 // Note we're missing LongModule & FloatModule -- they/re implicit 193 } 194 } 195 196 @Component( 197 modules = {StringModule.class, IntModuleIncludingDoubleAndFloat.class, LongModule.class}, 198 dependencies = DepComponent.class) 199 interface TestComponentWithBuilderInterface { s()200 String s(); 201 i()202 int i(); 203 l()204 long l(); 205 f()206 float f(); 207 d()208 double d(); 209 210 interface SharedBuilder { 211 // Make sure we use the overriding signature. build()212 Object build(); 213 stringModule(StringModule m1)214 Object stringModule(StringModule m1); 215 } 216 217 @Component.Builder 218 interface Builder extends TestComponentWithBuilderInterface.SharedBuilder { 219 @Override build()220 TestComponentWithBuilderInterface build(); // Narrowing return type 221 222 @Override stringModule(StringModule stringModule)223 Builder stringModule(StringModule stringModule); // Narrowing return type 224 intModule(IntModuleIncludingDoubleAndFloat intModule)225 Builder intModule(IntModuleIncludingDoubleAndFloat intModule); 226 doubleModule(DoubleModule doubleModule)227 void doubleModule(DoubleModule doubleModule); // Module w/o args 228 depComponent(DepComponent depComponent)229 void depComponent(DepComponent depComponent); 230 231 // Note we're missing LongModule & FloatModule -- they/re implicit 232 } 233 } 234 235 @Component( 236 modules = {StringModule.class, IntModuleIncludingDoubleAndFloat.class, LongModule.class}, 237 dependencies = DepComponent.class) 238 interface TestComponentWithGenericBuilderAbstractClass { s()239 String s(); 240 i()241 int i(); 242 l()243 long l(); 244 f()245 float f(); 246 d()247 double d(); 248 249 abstract static class SharedBuilder<B, C, M1, M2> { build()250 abstract C build(); // Test resolving return type of build() 251 setM1(M1 m1)252 abstract B setM1(M1 m1); // Test resolving return type & param of setter 253 setM2(M2 m2)254 abstract SharedBuilder<B, C, M1, M2> setM2(M2 m2); // Test being overridden 255 doubleModule(DoubleModule doubleModule)256 abstract void doubleModule(DoubleModule doubleModule); // Test being overridden 257 depComponent( FloatModule floatModule)258 abstract SharedBuilder<B, C, M1, M2> depComponent( 259 FloatModule floatModule); // Test return type 260 } 261 262 @Component.Builder 263 abstract static class Builder 264 extends TestComponentWithGenericBuilderAbstractClass.SharedBuilder< 265 Builder, 266 TestComponentWithGenericBuilderAbstractClass, 267 StringModule, 268 IntModuleIncludingDoubleAndFloat> { 269 @Override setM2(IntModuleIncludingDoubleAndFloat m2)270 abstract Builder setM2(IntModuleIncludingDoubleAndFloat m2); // Test covariant overrides 271 272 @Override doubleModule(DoubleModule module3)273 abstract void doubleModule(DoubleModule module3); // Test simple overrides allowed 274 depComponent(DepComponent depComponent)275 abstract void depComponent(DepComponent depComponent); 276 277 // Note we're missing LongModule & FloatModule -- they're implicit 278 } 279 } 280 281 @Component( 282 modules = {StringModule.class, IntModuleIncludingDoubleAndFloat.class, LongModule.class}, 283 dependencies = DepComponent.class) 284 interface TestComponentWithGenericBuilderInterface { s()285 String s(); 286 i()287 int i(); 288 l()289 long l(); 290 f()291 float f(); 292 d()293 double d(); 294 295 interface SharedBuilder<B, C, M1, M2> { build()296 C build(); // Test resolving return type of build() 297 setM1(M1 m1)298 B setM1(M1 m1); // Test resolving return type & param of setter 299 setM2(M2 m2)300 SharedBuilder<B, C, M1, M2> setM2(M2 m2); // Test being overridden 301 doubleModule(DoubleModule doubleModule)302 void doubleModule(DoubleModule doubleModule); // Test being overridden 303 set(FloatModule floatModule)304 SharedBuilder<B, C, M1, M2> set(FloatModule floatModule); // Test return type is supertype. 305 } 306 307 @Component.Builder 308 interface Builder 309 extends TestComponentWithGenericBuilderInterface.SharedBuilder< 310 Builder, 311 TestComponentWithGenericBuilderInterface, 312 StringModule, 313 IntModuleIncludingDoubleAndFloat> { 314 @Override setM2(IntModuleIncludingDoubleAndFloat m2)315 Builder setM2(IntModuleIncludingDoubleAndFloat m2); // Test covariant overrides allowed 316 317 @Override doubleModule(DoubleModule module3)318 void doubleModule(DoubleModule module3); // Test simple overrides allowed 319 depComponent(DepComponent depComponent)320 void depComponent(DepComponent depComponent); 321 322 // Note we're missing M5 -- that's implicit. 323 } 324 } 325 326 @Component 327 interface DepComponent {} 328 329 @Singleton 330 @Component 331 interface ParentComponent { childAbstractClassBuilder()332 TestChildComponentWithBuilderAbstractClass.Builder childAbstractClassBuilder(); 333 childInterfaceBuilder()334 TestChildComponentWithBuilderInterface.Builder childInterfaceBuilder(); 335 middleBuilder()336 MiddleChild.Builder middleBuilder(); 337 otherBuilder()338 OtherMiddleChild.Builder otherBuilder(); 339 requiresMiddleChildBuilder()340 RequiresSubcomponentBuilder<MiddleChild.Builder> requiresMiddleChildBuilder(); 341 } 342 343 @Scope 344 @Retention(RUNTIME) 345 @interface MiddleScope {} 346 347 @MiddleScope 348 @Subcomponent(modules = StringModule.class) 349 interface MiddleChild { s()350 String s(); 351 grandchildBuilder()352 Grandchild.Builder grandchildBuilder(); 353 requiresGrandchildBuilder()354 RequiresSubcomponentBuilder<Grandchild.Builder> requiresGrandchildBuilder(); 355 356 @Subcomponent.Builder 357 interface Builder { build()358 MiddleChild build(); 359 set(StringModule stringModule)360 Builder set(StringModule stringModule); 361 } 362 } 363 364 static class RequiresSubcomponentBuilder<B> { 365 private final Provider<B> subcomponentBuilderProvider; 366 private final B subcomponentBuilder; 367 368 @Inject RequiresSubcomponentBuilder(Provider<B> subcomponentBuilderProvider, B subcomponentBuilder)369 RequiresSubcomponentBuilder(Provider<B> subcomponentBuilderProvider, B subcomponentBuilder) { 370 this.subcomponentBuilderProvider = subcomponentBuilderProvider; 371 this.subcomponentBuilder = subcomponentBuilder; 372 } 373 subcomponentBuilderProvider()374 Provider<B> subcomponentBuilderProvider() { 375 return subcomponentBuilderProvider; 376 } 377 subcomponentBuilder()378 B subcomponentBuilder() { 379 return subcomponentBuilder; 380 } 381 } 382 383 @MiddleScope 384 @Subcomponent(modules = {StringModule.class, LongModule.class}) 385 interface OtherMiddleChild { l()386 long l(); 387 s()388 String s(); 389 grandchildBuilder()390 Grandchild.Builder grandchildBuilder(); 391 392 @Subcomponent.Builder 393 interface Builder { build()394 OtherMiddleChild build(); 395 set(StringModule stringModule)396 Builder set(StringModule stringModule); 397 } 398 } 399 400 @Component(modules = StringModule.class) 401 @Singleton 402 interface ParentOfGenericComponent extends GenericParent<Grandchild.Builder> {} 403 404 @Subcomponent(modules = IntModuleIncludingDoubleAndFloat.class) 405 interface Grandchild { i()406 int i(); 407 s()408 String s(); 409 410 @Subcomponent.Builder 411 interface Builder { build()412 Grandchild build(); 413 set(IntModuleIncludingDoubleAndFloat intModule)414 Builder set(IntModuleIncludingDoubleAndFloat intModule); 415 } 416 } 417 418 interface GenericParent<B> { subcomponentBuilder()419 B subcomponentBuilder(); 420 } 421 422 @Module 423 static class ByteModule { 424 final byte b; 425 ByteModule(byte b)426 ByteModule(byte b) { 427 this.b = b; 428 } 429 430 @Provides b()431 byte b() { 432 return b; 433 } 434 } 435 436 @Module 437 static class DoubleModule { 438 @Provides d()439 double d() { 440 return 4.2d; 441 } 442 } 443 444 @Module 445 static class LongModule { 446 @Provides l()447 long l() { 448 return 6L; 449 } 450 } 451 452 @Module 453 static class FloatModule { 454 @Provides f()455 float f() { 456 return 5.5f; 457 } 458 } 459 460 @Module 461 static class StringModule { 462 final String string; 463 StringModule(String string)464 StringModule(String string) { 465 this.string = string; 466 } 467 468 @Provides string()469 String string() { 470 return string; 471 } 472 } 473 474 @Module(includes = {DoubleModule.class, FloatModule.class}) 475 static class IntModuleIncludingDoubleAndFloat { 476 final int integer; 477 IntModuleIncludingDoubleAndFloat(int integer)478 IntModuleIncludingDoubleAndFloat(int integer) { 479 this.integer = integer; 480 } 481 482 @Provides integer()483 int integer() { 484 return integer; 485 } 486 } 487 488 @Test interfaceBuilder()489 public void interfaceBuilder() { 490 TestComponentWithBuilderInterface.Builder builder = 491 DaggerBuilderTest_TestComponentWithBuilderInterface.builder(); 492 493 // Make sure things fail if we don't set our required modules. 494 try { 495 builder.build(); 496 fail(); 497 } catch (IllegalStateException expected) { 498 } 499 500 builder 501 .intModule(new IntModuleIncludingDoubleAndFloat(1)) 502 .stringModule(new StringModule("sam")) 503 .depComponent(new DepComponent() {}); 504 builder.doubleModule(new DoubleModule()); 505 // Don't set other modules -- make sure it works. 506 507 TestComponentWithBuilderInterface component = builder.build(); 508 assertThat(component.s()).isEqualTo("sam"); 509 assertThat(component.i()).isEqualTo(1); 510 assertThat(component.d()).isEqualTo(4.2d); 511 assertThat(component.f()).isEqualTo(5.5f); 512 assertThat(component.l()).isEqualTo(6L); 513 } 514 515 @Test abstractClassBuilder()516 public void abstractClassBuilder() { 517 TestComponentWithBuilderAbstractClass.Builder builder = 518 TestComponentWithBuilderAbstractClass.builder(); 519 520 // Make sure things fail if we don't set our required modules. 521 try { 522 builder.build(); 523 fail(); 524 } catch (IllegalStateException expected) { 525 } 526 527 builder 528 .intModule(new IntModuleIncludingDoubleAndFloat(1)) 529 .stringModule(new StringModule("sam")) 530 .depComponent(new DepComponent() {}); 531 builder.doubleModule(new DoubleModule()); 532 // Don't set other modules -- make sure it works. 533 534 TestComponentWithBuilderAbstractClass component = builder.build(); 535 assertThat(component.s()).isEqualTo("sam"); 536 assertThat(component.i()).isEqualTo(1); 537 assertThat(component.d()).isEqualTo(4.2d); 538 assertThat(component.f()).isEqualTo(5.5f); 539 assertThat(component.l()).isEqualTo(6L); 540 } 541 542 @Test interfaceGenericBuilder()543 public void interfaceGenericBuilder() { 544 TestComponentWithGenericBuilderInterface.Builder builder = 545 DaggerBuilderTest_TestComponentWithGenericBuilderInterface.builder(); 546 547 // Make sure things fail if we don't set our required modules. 548 try { 549 builder.build(); 550 fail(); 551 } catch (IllegalStateException expected) { 552 } 553 554 builder 555 .setM2(new IntModuleIncludingDoubleAndFloat(1)) 556 .setM1(new StringModule("sam")) 557 .depComponent(new DepComponent() {}); 558 builder.doubleModule(new DoubleModule()); 559 // Don't set other modules -- make sure it works. 560 561 TestComponentWithGenericBuilderInterface component = builder.build(); 562 assertThat(component.s()).isEqualTo("sam"); 563 assertThat(component.i()).isEqualTo(1); 564 assertThat(component.d()).isEqualTo(4.2d); 565 assertThat(component.f()).isEqualTo(5.5f); 566 assertThat(component.l()).isEqualTo(6L); 567 } 568 569 @Test abstractClassGenericBuilder()570 public void abstractClassGenericBuilder() { 571 TestComponentWithGenericBuilderAbstractClass.Builder builder = 572 DaggerBuilderTest_TestComponentWithGenericBuilderAbstractClass.builder(); 573 574 // Make sure things fail if we don't set our required modules. 575 try { 576 builder.build(); 577 fail(); 578 } catch (IllegalStateException expected) { 579 } 580 581 builder 582 .setM2(new IntModuleIncludingDoubleAndFloat(1)) 583 .setM1(new StringModule("sam")) 584 .depComponent(new DepComponent() {}); 585 builder.doubleModule(new DoubleModule()); 586 // Don't set other modules -- make sure it works. 587 588 TestComponentWithGenericBuilderAbstractClass component = builder.build(); 589 assertThat(component.s()).isEqualTo("sam"); 590 assertThat(component.i()).isEqualTo(1); 591 assertThat(component.d()).isEqualTo(4.2d); 592 assertThat(component.f()).isEqualTo(5.5f); 593 assertThat(component.l()).isEqualTo(6L); 594 } 595 596 @Test subcomponents_interface()597 public void subcomponents_interface() { 598 ParentComponent parent = DaggerBuilderTest_ParentComponent.create(); 599 TestChildComponentWithBuilderInterface.Builder builder1 = parent.childInterfaceBuilder(); 600 try { 601 builder1.build(); 602 fail(); 603 } catch (IllegalStateException expected) { 604 } 605 606 builder1 607 .setM2(new IntModuleIncludingDoubleAndFloat(1)) 608 .setM1(new StringModule("sam")) 609 .set(new ByteModule((byte) 7)); 610 builder1.set(new FloatModule()); 611 TestChildComponentWithBuilderInterface child1 = builder1.build(); 612 assertThat(child1.s()).isEqualTo("sam"); 613 assertThat(child1.i()).isEqualTo(1); 614 assertThat(child1.d()).isEqualTo(4.2d); 615 assertThat(child1.f()).isEqualTo(5.5f); 616 assertThat(child1.l()).isEqualTo(6L); 617 assertThat(child1.b()).isEqualTo((byte) 7); 618 } 619 620 @Test subcomponents_abstractclass()621 public void subcomponents_abstractclass() { 622 ParentComponent parent = DaggerBuilderTest_ParentComponent.create(); 623 TestChildComponentWithBuilderAbstractClass.Builder builder2 = 624 parent.childAbstractClassBuilder(); 625 try { 626 builder2.build(); 627 fail(); 628 } catch (IllegalStateException expected) { 629 } 630 631 builder2 632 .setM2(new IntModuleIncludingDoubleAndFloat(10)) 633 .setM1(new StringModule("tara")) 634 .set(new ByteModule((byte) 70)); 635 builder2.set(new FloatModule()); 636 TestChildComponentWithBuilderAbstractClass child2 = builder2.build(); 637 assertThat(child2.s()).isEqualTo("tara"); 638 assertThat(child2.i()).isEqualTo(10); 639 assertThat(child2.d()).isEqualTo(4.2d); 640 assertThat(child2.f()).isEqualTo(5.5f); 641 assertThat(child2.l()).isEqualTo(6L); 642 assertThat(child2.b()).isEqualTo((byte) 70); 643 } 644 645 @Test grandchildren()646 public void grandchildren() { 647 ParentComponent parent = DaggerBuilderTest_ParentComponent.create(); 648 MiddleChild middle1 = parent.middleBuilder().set(new StringModule("sam")).build(); 649 Grandchild grandchild1 = 650 middle1.grandchildBuilder().set(new IntModuleIncludingDoubleAndFloat(21)).build(); 651 Grandchild grandchild2 = 652 middle1.grandchildBuilder().set(new IntModuleIncludingDoubleAndFloat(22)).build(); 653 654 assertThat(middle1.s()).isEqualTo("sam"); 655 assertThat(grandchild1.i()).isEqualTo(21); 656 assertThat(grandchild1.s()).isEqualTo("sam"); 657 assertThat(grandchild2.i()).isEqualTo(22); 658 assertThat(grandchild2.s()).isEqualTo("sam"); 659 660 // Make sure grandchildren from newer children have no relation to the older ones. 661 MiddleChild middle2 = parent.middleBuilder().set(new StringModule("tara")).build(); 662 Grandchild grandchild3 = 663 middle2.grandchildBuilder().set(new IntModuleIncludingDoubleAndFloat(23)).build(); 664 Grandchild grandchild4 = 665 middle2.grandchildBuilder().set(new IntModuleIncludingDoubleAndFloat(24)).build(); 666 667 assertThat(middle2.s()).isEqualTo("tara"); 668 assertThat(grandchild3.i()).isEqualTo(23); 669 assertThat(grandchild3.s()).isEqualTo("tara"); 670 assertThat(grandchild4.i()).isEqualTo(24); 671 assertThat(grandchild4.s()).isEqualTo("tara"); 672 } 673 674 @Test diamondGrandchildren()675 public void diamondGrandchildren() { 676 ParentComponent parent = DaggerBuilderTest_ParentComponent.create(); 677 MiddleChild middle = parent.middleBuilder().set(new StringModule("sam")).build(); 678 OtherMiddleChild other = parent.otherBuilder().set(new StringModule("tara")).build(); 679 680 Grandchild middlegrand = 681 middle.grandchildBuilder().set(new IntModuleIncludingDoubleAndFloat(21)).build(); 682 Grandchild othergrand = 683 other.grandchildBuilder().set(new IntModuleIncludingDoubleAndFloat(22)).build(); 684 685 assertThat(middle.s()).isEqualTo("sam"); 686 assertThat(other.s()).isEqualTo("tara"); 687 assertThat(middlegrand.s()).isEqualTo("sam"); 688 assertThat(othergrand.s()).isEqualTo("tara"); 689 assertThat(middlegrand.i()).isEqualTo(21); 690 assertThat(othergrand.i()).isEqualTo(22); 691 } 692 693 @Test genericSubcomponentMethod()694 public void genericSubcomponentMethod() { 695 ParentOfGenericComponent parent = 696 DaggerBuilderTest_ParentOfGenericComponent.builder() 697 .stringModule(new StringModule("sam")) 698 .build(); 699 Grandchild.Builder builder = parent.subcomponentBuilder(); 700 Grandchild child = builder.set(new IntModuleIncludingDoubleAndFloat(21)).build(); 701 assertThat(child.s()).isEqualTo("sam"); 702 assertThat(child.i()).isEqualTo(21); 703 } 704 705 @Test requireSubcomponentBuilderProviders()706 public void requireSubcomponentBuilderProviders() { 707 ParentComponent parent = DaggerBuilderTest_ParentComponent.create(); 708 MiddleChild middle = 709 parent 710 .requiresMiddleChildBuilder() 711 .subcomponentBuilderProvider() 712 .get() 713 .set(new StringModule("sam")) 714 .build(); 715 Grandchild grandchild = 716 middle 717 .requiresGrandchildBuilder() 718 .subcomponentBuilderProvider() 719 .get() 720 .set(new IntModuleIncludingDoubleAndFloat(12)) 721 .build(); 722 assertThat(middle.s()).isEqualTo("sam"); 723 assertThat(grandchild.i()).isEqualTo(12); 724 assertThat(grandchild.s()).isEqualTo("sam"); 725 } 726 727 @Test requireSubcomponentBuilders()728 public void requireSubcomponentBuilders() { 729 ParentComponent parent = DaggerBuilderTest_ParentComponent.create(); 730 MiddleChild middle = 731 parent 732 .requiresMiddleChildBuilder() 733 .subcomponentBuilder() 734 .set(new StringModule("sam")) 735 .build(); 736 Grandchild grandchild = 737 middle 738 .requiresGrandchildBuilder() 739 .subcomponentBuilder() 740 .set(new IntModuleIncludingDoubleAndFloat(12)) 741 .build(); 742 assertThat(middle.s()).isEqualTo("sam"); 743 assertThat(grandchild.i()).isEqualTo(12); 744 assertThat(grandchild.s()).isEqualTo("sam"); 745 } 746 } 747