summaryrefslogtreecommitdiffstats
path: root/sangria-listbinder/src/test/java/com/tavianator/sangria/listbinder/ListBinderTest.java
blob: 4c7c86b947a77b89347b981582ec4c4148f90059 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/****************************************************************************
 * Sangria                                                                  *
 * Copyright (C) 2014 Tavian Barnes <tavianator@tavianator.com>             *
 *                                                                          *
 * Licensed under the Apache License, Version 2.0 (the "License");          *
 * you may not use this file except in compliance with the License.         *
 * You may obtain a copy of the License at                                  *
 *                                                                          *
 * http://www.apache.org/licenses/LICENSE-2.0                               *
 *                                                                          *
 * Unless required by applicable law or agreed to in writing, software      *
 * distributed under the License is distributed on an "AS IS" BASIS,        *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
 * See the License for the specific language governing permissions and      *
 * limitations under the License.                                           *
 ****************************************************************************/

package com.tavianator.sangria.listbinder;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.*;
import javax.inject.Provider;
import javax.inject.Qualifier;

import com.google.inject.AbstractModule;
import com.google.inject.CreationException;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.TypeLiteral;
import com.google.inject.name.Names;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import com.tavianator.sangria.core.TypeLiterals;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;

/**
 * Tests for {@link ListBinder}.
 *
 * @author Tavian Barnes (tavianator@tavianator.com)
 * @version 1.1
 * @since 1.1
 */
public class ListBinderTest {
    public @Rule ExpectedException thrown = ExpectedException.none();

    @Retention(RetentionPolicy.RUNTIME)
    @Qualifier
    private @interface Simple {
    }

    private static final TypeLiteral<List<String>> LIST_OF_STRINGS = TypeLiterals.listOf(String.class);
    private static final TypeLiteral<List<Provider<String>>> LIST_OF_STRING_PROVIDERS = TypeLiterals.listOf(TypeLiterals.providerOf(String.class));

    @Test
    public void testBasicLists() {
        Injector injector = Guice.createInjector(new AbstractModule() {
            @Override
            protected void configure() {
                ListBinder<String> listBinder = ListBinder.build(binder(), String.class)
                        .withDefaultPriority();
                listBinder.addBinding().toInstance("a");
                listBinder.addBinding().toInstance("b");
                listBinder.addBinding().toInstance("c");

                listBinder = ListBinder.build(binder(), String.class)
                        .annotatedWith(Simple.class)
                        .withDefaultPriority();
                listBinder.addBinding().toInstance("d");
                listBinder.addBinding().toInstance("e");
                listBinder.addBinding().toInstance("f");

                listBinder = ListBinder.build(binder(), String.class)
                        .annotatedWith(Names.named("name"))
                        .withDefaultPriority();
                listBinder.addBinding().toInstance("g");
                listBinder.addBinding().toInstance("h");
                listBinder.addBinding().toInstance("i");
            }
        });
        List<String> list = injector.getInstance(Key.get(LIST_OF_STRINGS));
        assertThat(list, contains("a", "b", "c"));

        List<Provider<String>> providers = injector.getInstance(Key.get(LIST_OF_STRING_PROVIDERS));
        assertThat(providers, hasSize(3));
        assertThat(providers.get(0).get(), equalTo("a"));
        assertThat(providers.get(1).get(), equalTo("b"));
        assertThat(providers.get(2).get(), equalTo("c"));

        list = injector.getInstance(Key.get(LIST_OF_STRINGS, Simple.class));
        assertThat(list, contains("d", "e", "f"));

        list = injector.getInstance(Key.get(LIST_OF_STRINGS, Names.named("name")));
        assertThat(list, contains("g", "h", "i"));
    }

    @Test
    public void testSplitBinders() {
        Injector injector = Guice.createInjector(new AbstractModule() {
            @Override
            protected void configure() {
                ListBinder<String> listBinder = ListBinder.build(binder(), String.class)
                        .withPriority(1);
                listBinder.addBinding().toInstance("c");
                listBinder.addBinding().toInstance("d");
            }
        }, new AbstractModule() {
            @Override
            protected void configure() {
                ListBinder<String> listBinder = ListBinder.build(binder(), String.class)
                        .withPriority(0);
                listBinder.addBinding().toInstance("a");
                listBinder.addBinding().toInstance("b");
            }
        });
        List<String> list = injector.getInstance(Key.get(LIST_OF_STRINGS));
        assertThat(list, contains("a", "b", "c", "d"));
    }

    @Test
    public void testConflictingDefaultPriorities() {
        thrown.expect(CreationException.class);
        thrown.expectMessage(containsString("2 errors"));
        thrown.expectMessage(containsString("1) Duplicate ListBinder<java.lang.String> with default priority"));
        thrown.expectMessage(containsString("2) Duplicate ListBinder<java.lang.String> with default priority"));
        thrown.expectMessage(containsString("at com.tavianator.sangria.listbinder.ListBinderTest"));

        Guice.createInjector(new AbstractModule() {
            @Override
            protected void configure() {
                ListBinder.build(binder(), String.class)
                        .withDefaultPriority();
            }
        }, new AbstractModule() {
            @Override
            protected void configure() {
                ListBinder.build(binder(), String.class)
                        .withDefaultPriority();
            }
        });
    }

    @Test
    public void testConflictingExplicitPriorities() {
        thrown.expect(CreationException.class);
        thrown.expectMessage(containsString("2 errors"));
        thrown.expectMessage(containsString("1) Duplicate ListBinder<java.lang.String> with priority [1]"));
        thrown.expectMessage(containsString("2) Duplicate ListBinder<java.lang.String> with priority [1]"));
        thrown.expectMessage(containsString("at com.tavianator.sangria.listbinder.ListBinderTest"));

        Guice.createInjector(new AbstractModule() {
            @Override
            protected void configure() {
                ListBinder.build(binder(), String.class)
                        .withPriority(1);
            }
        }, new AbstractModule() {
            @Override
            protected void configure() {
                ListBinder.build(binder(), String.class)
                        .withPriority(1);
            }
        });
    }

    @Test
    public void testConflictingDefaultAndExplicitPriorities() {
        thrown.expect(CreationException.class);
        thrown.expectMessage(containsString("2 errors"));
        thrown.expectMessage(containsString(") ListBinder<java.lang.String> with default priority conflicts with ListBinder with explicit priority"));
        thrown.expectMessage(containsString(") ListBinder<java.lang.String> with priority [1] conflicts with ListBinder with default priority"));
        thrown.expectMessage(containsString("at com.tavianator.sangria.listbinder.ListBinderTest"));

        Guice.createInjector(new AbstractModule() {
            @Override
            protected void configure() {
                ListBinder.build(binder(), String.class)
                        .withDefaultPriority();
            }
        }, new AbstractModule() {
            @Override
            protected void configure() {
                ListBinder.build(binder(), String.class)
                        .withPriority(1);
            }
        });
    }

    @Test
    public void testToString() {
        Guice.createInjector(new AbstractModule() {
            @Override
            protected void configure() {
                ListBinder<String> listBinder = ListBinder.build(binder(), String.class)
                        .annotatedWith(Names.named("name"))
                        .withDefaultPriority();
                assertThat(listBinder.toString(), equalTo("ListBinder<java.lang.String> annotated with @com.google.inject.name.Named(value=name) with default priority"));

                ListBinder<Object> objectListBinder = ListBinder.build(binder(), Object.class)
                        .withPriority(1, 2);
                assertThat(objectListBinder.toString(), equalTo("ListBinder<java.lang.Object> with priority [1, 2]"));
            }
        });
    }
}