Skip to content
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

Remove ListUtil and Fields classes #1926

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -16,13 +16,13 @@
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.mockito.exceptions.base.MockitoException;
import org.mockito.internal.configuration.injection.filter.MockCandidateFilter;
import org.mockito.internal.configuration.injection.filter.NameBasedCandidateFilter;
import org.mockito.internal.configuration.injection.filter.TerminalMockCandidateFilter;
import org.mockito.internal.configuration.injection.filter.TypeBasedCandidateFilter;
import org.mockito.internal.util.collections.ListUtil;
import org.mockito.internal.util.reflection.FieldInitializationReport;
import org.mockito.internal.util.reflection.FieldInitializer;

Expand Down Expand Up @@ -67,13 +67,6 @@ public class PropertyAndSetterInjection extends MockInjectionStrategy {
new NameBasedCandidateFilter(
new TerminalMockCandidateFilter()));

private final ListUtil.Filter<Field> notFinalOrStatic = new ListUtil.Filter<Field>() {
public boolean isOut(Field object) {
return Modifier.isFinal(object.getModifiers()) || Modifier.isStatic(object.getModifiers());
}
};


public boolean processInjection(Field injectMocksField, Object injectMocksFieldOwner, Set<Object> mockCandidates) {
FieldInitializationReport report = initializeInjectMocksField(injectMocksField, injectMocksFieldOwner);

Expand Down Expand Up @@ -129,9 +122,11 @@ private boolean injectMockCandidatesOnFields(Set<Object> mocks,
}

private List<Field> orderedInstanceFieldsFrom(Class<?> awaitingInjectionClazz) {
List<Field> declaredFields = Arrays.asList(awaitingInjectionClazz.getDeclaredFields());
declaredFields = ListUtil.filter(declaredFields, notFinalOrStatic);

return sortSuperTypesLast(declaredFields);
return sortSuperTypesLast(
Arrays
.stream(awaitingInjectionClazz.getDeclaredFields())
.filter(object -> !Modifier.isFinal(object.getModifiers()) && !Modifier.isStatic(object.getModifiers()))
.collect(Collectors.toList())
);
}
}
Expand Up @@ -5,10 +5,10 @@
package org.mockito.internal.debugging;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

import org.mockito.Mockito;
import org.mockito.internal.util.collections.ListUtil;
import org.mockito.invocation.Invocation;
import org.mockito.stubbing.Stubbing;

Expand Down Expand Up @@ -37,11 +37,10 @@ public String printInvocations(Object mock) {
}
}

LinkedList<Stubbing> unused = ListUtil.filter(stubbings, new ListUtil.Filter<Stubbing>() {
public boolean isOut(Stubbing s) {
return s.wasUsed();
}
});
List<Stubbing> unused = stubbings
.stream()
.filter(s -> !s.wasUsed())
.collect(Collectors.toList());

if (unused.isEmpty()) {
return sb.toString();
Expand Down
Expand Up @@ -6,9 +6,8 @@

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

import org.mockito.internal.util.collections.ListUtil;
import org.mockito.internal.util.collections.ListUtil.Filter;
import org.mockito.internal.verification.api.InOrderContext;
import org.mockito.invocation.Invocation;
import org.mockito.invocation.Location;
Expand All @@ -20,12 +19,17 @@ private InvocationsFinder() {
}

public static List<Invocation> findInvocations(List<Invocation> invocations, MatchableInvocation wanted) {
return ListUtil.filter(invocations, new RemoveNotMatching(wanted));
return invocations
.stream()
.filter(wanted::matches)
.collect(Collectors.toList());
}

public static List<Invocation> findAllMatchingUnverifiedChunks(List<Invocation> invocations, MatchableInvocation wanted, InOrderContext orderingContext) {
List<Invocation> unverified = removeVerifiedInOrder(invocations, orderingContext);
return ListUtil.filter(unverified, new RemoveNotMatching(wanted));
return removeVerifiedInOrder(invocations, orderingContext)
.stream()
.filter(wanted::matches)
.collect(Collectors.toList());
}

/**
Expand Down Expand Up @@ -55,7 +59,7 @@ public static List<Invocation> findMatchingChunk(List<Invocation> invocations, M
}

private static List<Invocation> getFirstMatchingChunk(MatchableInvocation wanted, List<Invocation> unverified) {
List<Invocation> firstChunk = new LinkedList<Invocation>();
List<Invocation> firstChunk = new LinkedList<>();
for (Invocation invocation : unverified) {
if (wanted.matches(invocation)) {
firstChunk.add(invocation);
Expand Down Expand Up @@ -116,17 +120,20 @@ public static Location getLastLocation(List<Invocation> invocations) {
}

public static Invocation findPreviousVerifiedInOrder(List<Invocation> invocations, InOrderContext context) {
LinkedList<Invocation> verifiedOnly = ListUtil.filter(invocations, new RemoveUnverifiedInOrder(context));
List<Invocation> verifiedOnly = invocations
.stream()
.filter(context::isVerified)
.collect(Collectors.toList());

if (verifiedOnly.isEmpty()) {
return null;
} else {
return verifiedOnly.getLast();
return verifiedOnly.get(verifiedOnly.size() - 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be .reduce((first, second) -> second).orElse(null)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isnt the existing:

  • more readable
  • performant, getLast is O(1) for array and linked list

Sorry for the silly question.

}
}

private static List<Invocation> removeVerifiedInOrder(List<Invocation> invocations, InOrderContext orderingContext) {
List<Invocation> unverified = new LinkedList<Invocation>();
List<Invocation> unverified = new LinkedList<>();
for (Invocation i : invocations) {
if (orderingContext.isVerified(i)) {
unverified.clear();
Expand All @@ -138,37 +145,13 @@ private static List<Invocation> removeVerifiedInOrder(List<Invocation> invocatio
}

public static List<Location> getAllLocations(List<Invocation> invocations) {
List<Location> locations = new LinkedList<Location>();
List<Location> locations = new LinkedList<>();
for (Invocation invocation : invocations) {
locations.add(invocation.getLocation());
}
return locations;
}

private static class RemoveNotMatching implements Filter<Invocation> {
private final MatchableInvocation wanted;

private RemoveNotMatching(MatchableInvocation wanted) {
this.wanted = wanted;
}

public boolean isOut(Invocation invocation) {
return !wanted.matches(invocation);
}
}

private static class RemoveUnverifiedInOrder implements Filter<Invocation> {
private final InOrderContext orderingContext;

public RemoveUnverifiedInOrder(InOrderContext orderingContext) {
this.orderingContext = orderingContext;
}

public boolean isOut(Invocation invocation) {
return !orderingContext.isVerified(invocation);
}
}

/**
* i3 is unverified here:
*
Expand All @@ -185,7 +168,7 @@ public boolean isOut(Invocation invocation) {
*/
public static Invocation findFirstUnverifiedInOrder(InOrderContext context, List<Invocation> orderedInvocations) {
Invocation candidate = null;
for(Invocation i : orderedInvocations) {
for (Invocation i : orderedInvocations) {
if (!context.isVerified(i)) {
candidate = candidate != null ? candidate : i;
} else {
Expand Down
Expand Up @@ -5,9 +5,8 @@
package org.mockito.internal.invocation.finder;

import java.util.List;
import java.util.stream.Collectors;

import org.mockito.internal.util.collections.ListUtil;
import org.mockito.internal.util.collections.ListUtil.Filter;
import org.mockito.invocation.Invocation;

/**
Expand All @@ -18,13 +17,10 @@ public class VerifiableInvocationsFinder {
private VerifiableInvocationsFinder() {}

public static List<Invocation> find(List<?> mocks) {
List<Invocation> invocations = AllInvocationsFinder.find(mocks);
return ListUtil.filter(invocations, new RemoveIgnoredForVerification());
}

private static class RemoveIgnoredForVerification implements Filter<Invocation>{
public boolean isOut(Invocation invocation) {
return invocation.isIgnoredForVerification();
}
return AllInvocationsFinder
.find(mocks)
.stream()
.filter(invocation -> !invocation.isIgnoredForVerification())
.collect(Collectors.toList());
}
}
25 changes: 10 additions & 15 deletions src/main/java/org/mockito/internal/junit/UnusedStubbingsFinder.java
Expand Up @@ -4,18 +4,15 @@
*/
package org.mockito.internal.junit;

import static org.mockito.internal.util.collections.ListUtil.filter;

import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.mockito.internal.invocation.finder.AllInvocationsFinder;
import org.mockito.internal.stubbing.UnusedStubbingReporting;
import org.mockito.internal.util.collections.ListUtil.Filter;
import org.mockito.invocation.Invocation;
import org.mockito.stubbing.Stubbing;

Expand All @@ -29,15 +26,13 @@ public class UnusedStubbingsFinder {
* Stubbings explicitily marked as LENIENT are not included.
*/
public UnusedStubbings getUnusedStubbings(Iterable<Object> mocks) {
Set<Stubbing> stubbings = AllInvocationsFinder.findStubbings(mocks);

List<Stubbing> unused = filter(stubbings, new Filter<Stubbing>() {
public boolean isOut(Stubbing s) {
return !UnusedStubbingReporting.shouldBeReported(s);
}
});

return new UnusedStubbings(unused);
return new UnusedStubbings(
AllInvocationsFinder
.findStubbings(mocks)
.stream()
.filter(UnusedStubbingReporting::shouldBeReported)
.collect(Collectors.toList())
);
}

/**
Expand All @@ -55,7 +50,7 @@ public Collection<Invocation> getUnusedStubbingsByLocation(Iterable<Object> mock

//1st pass, collect all the locations of the stubbings that were used
//note that those are _not_ locations where the stubbings was used
Set<String> locationsOfUsedStubbings = new HashSet<String>();
Set<String> locationsOfUsedStubbings = new HashSet<>();
for (Stubbing s : stubbings) {
if (!UnusedStubbingReporting.shouldBeReported(s)) {
String location = s.getInvocation().getLocation().toString();
Expand All @@ -67,7 +62,7 @@ public Collection<Invocation> getUnusedStubbingsByLocation(Iterable<Object> mock
//If the location matches we assume the stubbing was used in at least one test method
//Also, using map to deduplicate reported unused stubbings
// if unused stubbing appear in the setup method / constructor we don't want to report it per each test case
Map<String, Invocation> out = new LinkedHashMap<String, Invocation>();
Map<String, Invocation> out = new LinkedHashMap<>();
for (Stubbing s : stubbings) {
String location = s.getInvocation().getLocation().toString();
if (!locationsOfUsedStubbings.contains(location)) {
Expand Down
43 changes: 0 additions & 43 deletions src/main/java/org/mockito/internal/util/collections/ListUtil.java

This file was deleted.