-
Notifications
You must be signed in to change notification settings - Fork 382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Java 10 Collectors APIs #9860
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e25559d
Tests for unmodifiable Collectors methods
niloc132 da3c491
Added missing impls, stuck on another branch
niloc132 62e0d87
Add extra imports added/removed upstream
niloc132 0ea22cf
Review feedback
niloc132 20a0add
Merge branch 'main' into java10-streams-test
niloc132 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
user/test/com/google/gwt/emultest/java10/util/stream/CollectorsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
/* | ||
* Copyright 2023 Google Inc. | ||
* | ||
* 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.google.gwt.emultest.java10.util.stream; | ||
|
||
import static com.google.gwt.emultest.java8.util.stream.CollectorsTest.applyItems; | ||
import static java.util.stream.Collectors.toUnmodifiableList; | ||
import static java.util.stream.Collectors.toUnmodifiableMap; | ||
import static java.util.stream.Collectors.toUnmodifiableSet; | ||
|
||
import com.google.gwt.emultest.java.util.EmulTestBase; | ||
|
||
import java.util.Collection; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.function.Function; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* Tests for java.util.stream.Collectors Java 10 API emulation. | ||
*/ | ||
public class CollectorsTest extends EmulTestBase { | ||
private static <T> boolean unmodifiableCollection(Collection<T> c, T existingSample, T newSample) { | ||
try { | ||
c.remove(existingSample); | ||
return false; | ||
} catch (UnsupportedOperationException ignore) { | ||
// expected | ||
} | ||
try { | ||
c.add(newSample); | ||
return false; | ||
} catch (UnsupportedOperationException ignore) { | ||
// expected | ||
} | ||
Iterator<T> itr = c.iterator(); | ||
itr.next(); | ||
try { | ||
itr.remove(); | ||
return false; | ||
} catch (UnsupportedOperationException e) { | ||
// expected | ||
} | ||
return true; | ||
} | ||
|
||
public void testToUnmodifiableList() { | ||
applyItems(List.of("a", "b"), toUnmodifiableList(), "a", "b", (expected, actual) -> { | ||
if (!expected.equals(actual)) { | ||
return false; | ||
} | ||
|
||
if (!unmodifiableCollection(actual, "a", "z")) { | ||
return false; | ||
} | ||
|
||
return true; | ||
}); | ||
|
||
// verify nulls fail | ||
try { | ||
Stream.of("a").map(ignore -> null).collect(toUnmodifiableList()); | ||
fail("Expected NPE"); | ||
} catch (NullPointerException ignore) { | ||
// expected | ||
} | ||
} | ||
|
||
public void testToUnmodifiableMap() { | ||
// verify simple cases copy all values and results are unmodifiable | ||
applyItems(Map.of("a", 0, "b", 1), toUnmodifiableMap(Function.identity(), | ||
k -> k.charAt(0) - 'a'), "a", "b", (expected, actual) -> { | ||
if (!expected.equals(actual)) { | ||
return false; | ||
} | ||
|
||
if (!unmodifiableMap(actual, "a", 0, "z", 100)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
}); | ||
|
||
// verify merge works with only one key (but this is just passing through to the toMap func | ||
// anyway...) | ||
applyItems(Map.of("a", 2), toUnmodifiableMap(Function.identity(), ignore -> 1, Integer::sum), | ||
"a", "a"); | ||
|
||
// verify nulls blow up for both keys and values | ||
try { | ||
Stream.of("a").collect(toUnmodifiableMap(obj -> null, Function.identity())); | ||
fail("Expected NPE"); | ||
} catch (NullPointerException ignore) { | ||
// expected | ||
} | ||
try { | ||
Stream.of("a").collect(toUnmodifiableMap(Function.identity(), obj -> null)); | ||
fail("Expected NPE"); | ||
} catch (Exception ignore) { | ||
// expected | ||
} | ||
} | ||
|
||
private <K, V> boolean unmodifiableMap(Map<K, V> a, K existingKey, V existingValue, K newKey, | ||
niloc132 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
V newValue) { | ||
if (!unmodifiableCollection(a.keySet(), existingKey, newKey)) { | ||
return false; | ||
} | ||
if (!unmodifiableCollection(a.values(), existingValue, newValue)) { | ||
return false; | ||
} | ||
|
||
try { | ||
a.put(newKey, newValue); | ||
return false; | ||
} catch (Exception ignore) { | ||
// expected | ||
} | ||
try { | ||
a.remove(existingKey); | ||
return false; | ||
} catch (Exception ignore) { | ||
// expected | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public void testToUnmodifiableSet() { | ||
applyItems(Set.of("a", "b"), toUnmodifiableSet(), "a", "b", (expected, actual) -> { | ||
if (!expected.equals(actual)) { | ||
return false; | ||
} | ||
if (!unmodifiableCollection(actual, "a", "z")) { | ||
return false; | ||
} | ||
return true; | ||
}); | ||
|
||
// verify nulls fail | ||
try { | ||
Stream.of("a").map(ignore -> null).collect(toUnmodifiableSet()); | ||
fail("Expected NPE"); | ||
} catch (NullPointerException ignore) { | ||
// expected | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It misses a lot of cases. Maybe we should refactor a bit in a follow up commit that introduces a helper class to check immutability of collections/sets, lists, maps. We now have many tests that need it:
CollectionsTest
,ListTest
,SetTest
,MapTest
,CollectorsTest
. Each doing it a bit differently and not covering everything.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call, I'll file a followup. At least for the
List
-verification method, the simplest answer might be to makeCollectionsTest.doTestModificationsToList
public and static, and lean on it from here.Note that I didn't work especially hard on fully validating every method, since the emulation depends on existing code that I assumed was well tested (or at least verified in real use) - at some point we have to assume that the levels below us are roughly sane...
#9882
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I had the thought that it might be good to just make the emulated immutable collection classes public and then simply do an instanceof check. If Collections.unmodifiableList/List.of and friends all use the same implementations, that would be fine I guess. Then we could make one test for the immutable implementations and all other tests check if that implementation has been used.