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