• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.google.common.graph;
2 
3 import static com.google.common.truth.Truth.assertThat;
4 import static org.junit.Assert.assertThrows;
5 
6 import com.google.common.collect.ImmutableSet;
7 import java.util.HashSet;
8 import java.util.Set;
9 import org.junit.Before;
10 import org.junit.Test;
11 import org.junit.runner.RunWith;
12 import org.junit.runners.JUnit4;
13 
14 @RunWith(JUnit4.class)
15 public final class InvalidatableSetTest {
16   Set<Integer> wrappedSet;
17   Set<Integer> copyOfWrappedSet;
18   InvalidatableSet<Integer> setToTest;
19 
20   @Before
createSets()21   public void createSets() {
22     wrappedSet = new HashSet<>();
23     wrappedSet.add(1);
24     wrappedSet.add(2);
25     wrappedSet.add(3);
26 
27     copyOfWrappedSet = ImmutableSet.copyOf(wrappedSet);
28     setToTest =
29         InvalidatableSet.of(wrappedSet, () -> wrappedSet.contains(1), () -> 1 + "is not present");
30   }
31 
32   @Test
33   @SuppressWarnings("TruthSelfEquals")
testEquals()34   public void testEquals() {
35     // sanity check on construction of copyOfWrappedSet
36     assertThat(wrappedSet).isEqualTo(copyOfWrappedSet);
37 
38     // test that setToTest is still valid
39     assertThat(setToTest).isEqualTo(wrappedSet);
40     assertThat(setToTest).isEqualTo(copyOfWrappedSet);
41 
42     // invalidate setToTest
43     wrappedSet.remove(1);
44     // sanity check on update of wrappedSet
45     assertThat(wrappedSet).isNotEqualTo(copyOfWrappedSet);
46 
47     ImmutableSet<Integer> copyOfModifiedSet = ImmutableSet.copyOf(wrappedSet); // {2,3}
48     // sanity check on construction of copyOfModifiedSet
49     assertThat(wrappedSet).isEqualTo(copyOfModifiedSet);
50 
51     // setToTest should throw when it calls equals(), or equals is called on it, except for itself
52     assertThat(setToTest).isEqualTo(setToTest);
53     assertThrows(IllegalStateException.class, () -> setToTest.equals(wrappedSet));
54     assertThrows(IllegalStateException.class, () -> setToTest.equals(copyOfWrappedSet));
55     assertThrows(IllegalStateException.class, () -> setToTest.equals(copyOfModifiedSet));
56     assertThrows(IllegalStateException.class, () -> wrappedSet.equals(setToTest));
57     assertThrows(IllegalStateException.class, () -> copyOfWrappedSet.equals(setToTest));
58     assertThrows(IllegalStateException.class, () -> copyOfModifiedSet.equals(setToTest));
59   }
60 }
61