Skip to content

Commit 99d75d1

Browse files
cpovirkGoogle Java Core Libraries
authored andcommitted
Add tester classes for Java 8 APIs to the Android flavor.
RELNOTES=n/a PiperOrigin-RevId: 691799318
1 parent 6ace8bc commit 99d75d1

20 files changed

+2057
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (C) 2015 The Guava 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 com.google.common.collect.testing.testers;
18+
19+
import static com.google.common.collect.testing.features.CollectionFeature.KNOWN_ORDER;
20+
import static java.util.Arrays.asList;
21+
22+
import com.google.common.annotations.GwtCompatible;
23+
import com.google.common.collect.testing.AbstractCollectionTester;
24+
import com.google.common.collect.testing.Helpers;
25+
import com.google.common.collect.testing.features.CollectionFeature;
26+
import java.util.ArrayList;
27+
import java.util.List;
28+
import org.junit.Ignore;
29+
30+
/**
31+
* A generic JUnit test which tests {@code forEach} operations on a collection. Can't be invoked
32+
* directly; please see {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}.
33+
*
34+
* @author Louis Wasserman
35+
*/
36+
@GwtCompatible
37+
@Ignore("test runners must not instantiate and run this directly, only via suites we build")
38+
// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
39+
@SuppressWarnings("JUnit4ClassUsedInJUnit3")
40+
@IgnoreJRERequirement // We opt into library desugaring for our tests.
41+
public class CollectionForEachTester<E> extends AbstractCollectionTester<E> {
42+
@CollectionFeature.Require(absent = KNOWN_ORDER)
43+
public void testForEachUnknownOrder() {
44+
List<E> elements = new ArrayList<>();
45+
collection.forEach(elements::add);
46+
Helpers.assertEqualIgnoringOrder(asList(createSamplesArray()), elements);
47+
}
48+
49+
@CollectionFeature.Require(KNOWN_ORDER)
50+
public void testForEachKnownOrder() {
51+
List<E> elements = new ArrayList<>();
52+
collection.forEach(elements::add);
53+
List<E> expected = Helpers.copyToList(getOrderedElements());
54+
assertEquals("Different ordered iteration", expected, elements);
55+
}
56+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright (C) 2015 The Guava 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 com.google.common.collect.testing.testers;
18+
19+
import static com.google.common.collect.testing.features.CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION;
20+
import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ITERATOR_REMOVE;
21+
import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE;
22+
import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
23+
import static com.google.common.collect.testing.features.CollectionSize.ZERO;
24+
import static com.google.common.collect.testing.testers.ReflectionFreeAssertThrows.assertThrows;
25+
26+
import com.google.common.annotations.GwtCompatible;
27+
import com.google.common.collect.testing.AbstractCollectionTester;
28+
import com.google.common.collect.testing.features.CollectionFeature;
29+
import com.google.common.collect.testing.features.CollectionSize;
30+
import java.util.Collection;
31+
import java.util.ConcurrentModificationException;
32+
import java.util.Iterator;
33+
import java.util.function.Predicate;
34+
import org.junit.Ignore;
35+
36+
/**
37+
* A generic JUnit test which tests {@link Collection#removeIf}. Can't be invoked directly; please
38+
* see {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}.
39+
*
40+
* @author Louis Wasserman
41+
*/
42+
@GwtCompatible
43+
@Ignore("test runners must not instantiate and run this directly, only via suites we build")
44+
// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
45+
@SuppressWarnings("JUnit4ClassUsedInJUnit3")
46+
@IgnoreJRERequirement // We opt into library desugaring for our tests.
47+
public class CollectionRemoveIfTester<E> extends AbstractCollectionTester<E> {
48+
@CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
49+
public void testRemoveIf_alwaysFalse() {
50+
assertFalse("removeIf(x -> false) should return false", collection.removeIf(x -> false));
51+
expectUnchanged();
52+
}
53+
54+
@CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
55+
@CollectionSize.Require(absent = ZERO)
56+
public void testRemoveIf_sometimesTrue() {
57+
assertTrue(
58+
"removeIf(isEqual(present)) should return true",
59+
collection.removeIf(Predicate.isEqual(samples.e0())));
60+
expectMissing(samples.e0());
61+
}
62+
63+
@CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
64+
@CollectionSize.Require(absent = ZERO)
65+
public void testRemoveIf_allPresent() {
66+
assertTrue("removeIf(x -> true) should return true", collection.removeIf(x -> true));
67+
expectContents();
68+
}
69+
70+
@CollectionFeature.Require({SUPPORTS_ITERATOR_REMOVE, FAILS_FAST_ON_CONCURRENT_MODIFICATION})
71+
@CollectionSize.Require(SEVERAL)
72+
public void testRemoveIfSomeMatchesConcurrentWithIteration() {
73+
assertThrows(
74+
ConcurrentModificationException.class,
75+
() -> {
76+
Iterator<E> iterator = collection.iterator();
77+
assertTrue(collection.removeIf(Predicate.isEqual(samples.e0())));
78+
iterator.next();
79+
});
80+
}
81+
82+
@CollectionFeature.Require(absent = SUPPORTS_REMOVE)
83+
@CollectionSize.Require(ZERO)
84+
public void testRemoveIf_unsupportedEmptyCollection() {
85+
try {
86+
assertFalse(
87+
"removeIf(Predicate) should return false or throw " + "UnsupportedOperationException",
88+
collection.removeIf(
89+
x -> {
90+
throw new AssertionError("predicate should never be called");
91+
}));
92+
} catch (UnsupportedOperationException tolerated) {
93+
}
94+
expectUnchanged();
95+
}
96+
97+
@CollectionFeature.Require(absent = SUPPORTS_REMOVE)
98+
@CollectionSize.Require(absent = ZERO)
99+
public void testRemoveIf_alwaysTrueUnsupported() {
100+
assertThrows(UnsupportedOperationException.class, () -> collection.removeIf(x -> true));
101+
expectUnchanged();
102+
assertTrue(collection.contains(samples.e0()));
103+
}
104+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright (C) 2013 The Guava 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 com.google.common.collect.testing.testers;
18+
19+
import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
20+
import static com.google.common.collect.testing.features.CollectionFeature.KNOWN_ORDER;
21+
import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ADD;
22+
import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE;
23+
import static com.google.common.collect.testing.features.CollectionSize.ZERO;
24+
25+
import com.google.common.annotations.GwtCompatible;
26+
import com.google.common.annotations.GwtIncompatible;
27+
import com.google.common.annotations.J2ktIncompatible;
28+
import com.google.common.collect.testing.AbstractCollectionTester;
29+
import com.google.common.collect.testing.Helpers;
30+
import com.google.common.collect.testing.SpliteratorTester;
31+
import com.google.common.collect.testing.features.CollectionFeature;
32+
import com.google.common.collect.testing.features.CollectionSize;
33+
import java.lang.reflect.Method;
34+
import java.util.Spliterator;
35+
import org.junit.Ignore;
36+
37+
/**
38+
* A generic JUnit test which tests {@code spliterator} operations on a collection. Can't be invoked
39+
* directly; please see {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}.
40+
*
41+
* @author Louis Wasserman
42+
*/
43+
@GwtCompatible(emulated = true)
44+
@Ignore("test runners must not instantiate and run this directly, only via suites we build")
45+
// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
46+
@SuppressWarnings("JUnit4ClassUsedInJUnit3")
47+
@IgnoreJRERequirement // We opt into library desugaring for our tests.
48+
public class CollectionSpliteratorTester<E> extends AbstractCollectionTester<E> {
49+
50+
@CollectionFeature.Require(absent = KNOWN_ORDER)
51+
public void testSpliteratorUnknownOrder() {
52+
SpliteratorTester.of(collection::spliterator).expect(getSampleElements());
53+
}
54+
55+
@CollectionFeature.Require(KNOWN_ORDER)
56+
public void testSpliteratorKnownOrder() {
57+
SpliteratorTester.of(collection::spliterator).expect(getOrderedElements()).inOrder();
58+
}
59+
60+
@CollectionFeature.Require(ALLOWS_NULL_VALUES)
61+
@CollectionSize.Require(absent = ZERO)
62+
public void testSpliteratorNullable() {
63+
initCollectionWithNullElement();
64+
assertFalse(collection.spliterator().hasCharacteristics(Spliterator.NONNULL));
65+
}
66+
67+
@CollectionFeature.Require(SUPPORTS_ADD)
68+
public void testSpliteratorNotImmutable_collectionAllowsAdd() {
69+
// If add is supported, verify that IMMUTABLE is not reported.
70+
assertFalse(collection.spliterator().hasCharacteristics(Spliterator.IMMUTABLE));
71+
}
72+
73+
@CollectionFeature.Require(SUPPORTS_REMOVE)
74+
public void testSpliteratorNotImmutable_collectionAllowsRemove() {
75+
// If remove is supported, verify that IMMUTABLE is not reported.
76+
assertFalse(collection.spliterator().hasCharacteristics(Spliterator.IMMUTABLE));
77+
}
78+
79+
@J2ktIncompatible
80+
@GwtIncompatible // reflection
81+
public static Method getSpliteratorNotImmutableCollectionAllowsAddMethod() {
82+
return Helpers.getMethod(
83+
CollectionSpliteratorTester.class, "testSpliteratorNotImmutable_collectionAllowsAdd");
84+
}
85+
86+
@J2ktIncompatible
87+
@GwtIncompatible // reflection
88+
public static Method getSpliteratorNotImmutableCollectionAllowsRemoveMethod() {
89+
return Helpers.getMethod(
90+
CollectionSpliteratorTester.class, "testSpliteratorNotImmutable_collectionAllowsRemove");
91+
}
92+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright (C) 2013 The Guava 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 com.google.common.collect.testing.testers;
18+
19+
import static com.google.common.collect.testing.features.CollectionFeature.KNOWN_ORDER;
20+
import static java.util.Arrays.asList;
21+
22+
import com.google.common.annotations.GwtCompatible;
23+
import com.google.common.collect.testing.AbstractCollectionTester;
24+
import com.google.common.collect.testing.Helpers;
25+
import com.google.common.collect.testing.features.CollectionFeature;
26+
import org.junit.Ignore;
27+
28+
/**
29+
* A generic JUnit test which tests {@code stream} operations on a collection. Can't be invoked
30+
* directly; please see {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}.
31+
*
32+
* @author Louis Wasserman
33+
*/
34+
@GwtCompatible
35+
@Ignore("test runners must not instantiate and run this directly, only via suites we build")
36+
// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
37+
@SuppressWarnings("JUnit4ClassUsedInJUnit3")
38+
@IgnoreJRERequirement // We opt into library desugaring for our tests.
39+
public class CollectionStreamTester<E> extends AbstractCollectionTester<E> {
40+
/*
41+
* We're not really testing the implementation of Stream, only that we're getting a Stream
42+
* that corresponds to the expected elements.
43+
*/
44+
45+
@CollectionFeature.Require(absent = KNOWN_ORDER)
46+
public void testStreamToArrayUnknownOrder() {
47+
Helpers.assertEqualIgnoringOrder(getSampleElements(), asList(collection.stream().toArray()));
48+
}
49+
50+
@CollectionFeature.Require(KNOWN_ORDER)
51+
public void testStreamToArrayKnownOrder() {
52+
assertEquals(getOrderedElements(), asList(collection.stream().toArray()));
53+
}
54+
55+
public void testStreamCount() {
56+
assertEquals(getNumElements(), collection.stream().count());
57+
}
58+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2019 The Guava Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.common.collect.testing.testers;
16+
17+
import static java.lang.annotation.ElementType.CONSTRUCTOR;
18+
import static java.lang.annotation.ElementType.METHOD;
19+
import static java.lang.annotation.ElementType.TYPE;
20+
21+
import java.lang.annotation.Target;
22+
23+
/**
24+
* Disables Animal Sniffer's checking of compatibility with older versions of Java/Android.
25+
*
26+
* <p>Each package's copy of this annotation needs to be listed in our {@code pom.xml}.
27+
*/
28+
@Target({METHOD, CONSTRUCTOR, TYPE})
29+
@ElementTypesAreNonnullByDefault
30+
@interface IgnoreJRERequirement {}

0 commit comments

Comments
 (0)