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