aboutsummaryrefslogtreecommitdiff
path: root/javatests/dagger/internal/SetFactoryTest.java
blob: 5e109ddb6de124f3292e86b09846638209665533 (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
/*
 * Copyright (C) 2014 The Dagger Authors.
 *
 * 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 dagger.internal;

import static com.google.common.truth.Truth.assertThat;

import com.google.common.collect.ImmutableSet;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
@SuppressWarnings("unchecked")
public class SetFactoryTest {
  @Rule public ExpectedException thrown = ExpectedException.none();

  @Test
  public void providerReturnsNull() {
    Factory<Set<Integer>> factory =
        SetFactory.<Integer>builder(0, 1).addCollectionProvider(() -> null).build();
    thrown.expect(NullPointerException.class);
    factory.get();
  }

  @Test
  public void providerReturnsNullSet() {
    Factory<Set<Integer>> factory =
        SetFactory.<Integer>builder(1, 0).addProvider(() -> null).build();
    thrown.expect(NullPointerException.class);
    factory.get();
  }

  @Test
  public void providerReturnsSetWithNullElement() {
    Set<Integer> set = new LinkedHashSet<>(Arrays.asList(1, null, 3));
    Factory<Set<Integer>> factory =
        SetFactory.<Integer>builder(0, 1).addCollectionProvider(() -> set).build();
    thrown.expect(NullPointerException.class);
    factory.get();
  }

  @Test
  public void invokesProvidersEveryTime() {
    Factory<Set<Integer>> factory =
        SetFactory.<Integer>builder(2, 2)
            .addProvider(incrementingIntegerProvider(0))
            .addProvider(incrementingIntegerProvider(10))
            .addCollectionProvider(incrementingIntegerSetProvider(20))
            .addCollectionProvider(incrementingIntegerSetProvider(30))
            .build();
    assertThat(factory.get()).containsExactly(0, 10, 20, 21, 30, 31);
    assertThat(factory.get()).containsExactly(1, 11, 22, 23, 32, 33);
    assertThat(factory.get()).containsExactly(2, 12, 24, 25, 34, 35);
  }

  private static Provider<Integer> incrementingIntegerProvider(int seed) {
    final AtomicInteger value = new AtomicInteger(seed);
    return value::getAndIncrement;
  }

  private static Provider<Set<Integer>> incrementingIntegerSetProvider(int seed) {
    final AtomicInteger value = new AtomicInteger(seed);
    return () -> ImmutableSet.of(value.getAndIncrement(), value.getAndIncrement());
  }
}