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.staticprovides;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 import static com.google.common.truth.Truth.assertWithMessage;
21 
22 import com.google.common.collect.ImmutableSet;
23 import java.lang.reflect.Field;
24 import java.lang.reflect.Method;
25 import java.util.Arrays;
26 import java.util.Collection;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.junit.runners.Parameterized;
30 import org.junit.runners.Parameterized.Parameter;
31 import org.junit.runners.Parameterized.Parameters;
32 
33 @RunWith(Parameterized.class)
34 public class StaticProvidesTest {
35   @Parameters
components()36   public static Collection<Object[]> components() {
37     return Arrays.asList(new Object[][] {
38         {DaggerStaticTestComponent.create()},
39         {DaggerStaticTestComponentWithBuilder.builder().build()},
40         {DaggerStaticTestComponentWithBuilder.builder()
41           .allStaticModule(new AllStaticModule())
42           .someStaticModule(new SomeStaticModule())
43           .build()}});
44   }
45 
46   @Parameter
47   public StaticTestComponent component;
48 
setMultibinding()49   @Test public void setMultibinding() {
50     assertThat(component.getMultiboundStrings()).isEqualTo(ImmutableSet.of(
51         AllStaticModule.class + ".contributeString",
52         SomeStaticModule.class + ".contributeStringFromAStaticMethod",
53         SomeStaticModule.class + ".contributeStringFromAnInstanceMethod"));
54   }
55 
allStaticProvidesModules_noFieldInComponentBuilder()56   @Test public void allStaticProvidesModules_noFieldInComponentBuilder() {
57     for (Field field : DaggerStaticTestComponent.Builder.class.getDeclaredFields()) {
58       assertWithMessage(field.getName())
59           .that(field.getType()).isNotEqualTo(AllStaticModule.class);
60     }
61   }
62 
allStaticProvidesModules_deprecatedMethodInComponentBuilder()63   @Test public void allStaticProvidesModules_deprecatedMethodInComponentBuilder() {
64     for (Method method : DaggerStaticTestComponent.Builder.class.getDeclaredMethods()) {
65       if (Arrays.asList(method.getParameterTypes()).contains(AllStaticModule.class)) {
66         assertWithMessage(method.getName())
67             .that(method.isAnnotationPresent(Deprecated.class))
68             .isTrue();
69       }
70     }
71   }
72 }
73