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.subcomponent.repeat; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static org.junit.Assert.fail; 21 22 import org.junit.Before; 23 import org.junit.Test; 24 import org.junit.runner.RunWith; 25 import org.junit.runners.JUnit4; 26 27 @RunWith(JUnit4.class) 28 public final class RepeatedModuleTest { 29 private ParentComponent parentComponent; 30 31 @Before initializeParentComponent()32 public void initializeParentComponent() { 33 this.parentComponent = DaggerParentComponent.builder().build(); 34 } 35 36 @Test repeatedModuleHasSameStateInSubcomponent()37 public void repeatedModuleHasSameStateInSubcomponent() { 38 SubcomponentWithRepeatedModule childComponent = 39 parentComponent.newChildComponentBuilder().build(); 40 assertThat(parentComponent.state()).isSameInstanceAs(childComponent.state()); 41 } 42 43 @Test repeatedModuleHasSameStateInGrandchildSubcomponent()44 public void repeatedModuleHasSameStateInGrandchildSubcomponent() { 45 SubcomponentWithoutRepeatedModule childComponent = 46 parentComponent.newChildComponentWithoutRepeatedModule(); 47 SubcomponentWithRepeatedModule grandchildComponent = 48 childComponent.newGrandchildBuilder().build(); 49 assertThat(parentComponent.state()).isSameInstanceAs(grandchildComponent.state()); 50 } 51 52 @Test repeatedModuleBuilderThrowsInSubcomponent()53 public void repeatedModuleBuilderThrowsInSubcomponent() { 54 SubcomponentWithRepeatedModule.Builder childComponentBuilder = 55 parentComponent.newChildComponentBuilder(); 56 try { 57 childComponentBuilder.repeatedModule(new RepeatedModule()); 58 fail(); 59 } catch (UnsupportedOperationException expected) { 60 assertThat(expected) 61 .hasMessageThat() 62 .isEqualTo( 63 "dagger.functional.subcomponent.repeat.RepeatedModule cannot be set " 64 + "because it is inherited from the enclosing component"); 65 } 66 } 67 68 @Test repeatedModuleBuilderThrowsInGrandchildSubcomponent()69 public void repeatedModuleBuilderThrowsInGrandchildSubcomponent() { 70 SubcomponentWithoutRepeatedModule childComponent = 71 parentComponent.newChildComponentWithoutRepeatedModule(); 72 OtherSubcomponentWithRepeatedModule.Builder grandchildComponentBuilder = 73 childComponent.newGrandchildBuilder(); 74 try { 75 grandchildComponentBuilder.repeatedModule(new RepeatedModule()); 76 fail(); 77 } catch (UnsupportedOperationException expected) { 78 assertThat(expected) 79 .hasMessageThat() 80 .isEqualTo( 81 "dagger.functional.subcomponent.repeat.RepeatedModule cannot be set " 82 + "because it is inherited from the enclosing component"); 83 } 84 } 85 } 86