/** * Reports report reflection argument matchers for all arguments of the given method invocation. * An exception will be thrown if there were already matchers reported for the invocation. * * @param invocation the method invocation, not null */ private void createMatchers(Invocation invocation) { List<IArgumentMatcher> matchers = LastControl.pullMatchers(); if (matchers != null && !matchers.isEmpty()) { if (matchers.size() != invocation.getArguments().length) { throw new IllegalStateException("This mock control does not support mixing of no-argument matchers and per-argument matchers. " + "Either no matchers are defined and the reflection argument matcher is used by default or all matchers are defined explicitly (Eg by using refEq())."); } // put all matchers back since pull removes them for (IArgumentMatcher matcher : matchers) { LastControl.reportMatcher(matcher); } return; } Object[] arguments = invocation.getArguments(); if (arguments == null) { return; } for (Object argument : arguments) { LastControl.reportMatcher(new ReflectionArgumentMatcher<Object>(argument, modes)); } }
/** * Waypoint equals. * * @param waypoint the waypoint */ private Waypoint waypointEq(final Waypoint waypoint) { EasyMock.reportMatcher(new IArgumentMatcher() { @Override public boolean matches(Object object) { if (object == null || waypoint == null) { return waypoint == object; } Waypoint waypoint2 = (Waypoint) object; return waypoint.getId() == waypoint2.getId(); } @Override public void appendTo(StringBuffer buffer) { buffer.append("waypointEq("); buffer.append(waypoint); buffer.append(")"); } }); return null; }
/** * Track equals. * * @param track1 the track */ private Track trackEq(final Track track1) { EasyMock.reportMatcher(new IArgumentMatcher() { @Override public boolean matches(Object object) { if (object == null || track1 == null) { return track1 == object; } Track track2 = (Track) object; return track1.getName().equals(track2.getName()); } @Override public void appendTo(StringBuffer buffer) { buffer.append("trackEq("); buffer.append(track1); buffer.append(")"); } }); return null; }
public static LocationUpdatedRemoteEvent hasStatus(final Status status) { reportMatcher(new IArgumentMatcher() { public void appendTo(StringBuffer buffer) { buffer.append("hasStatus(\"" + status + "\")"); } public boolean matches(Object argument) { if (argument instanceof LocationUpdatedRemoteEvent) { LocationUpdatedRemoteEvent e = (LocationUpdatedRemoteEvent)argument; return status.equals(e.getLocationInfo().getStatus()); } else { return false; } } }); return null; }
private <T> T is(final RequestMatcher matcher) { reportMatcher(new IArgumentMatcher() { public boolean matches(Object argument) { try { matcher.match(null, serialize(argument)); } catch (IOException e) { throw new IllegalArgumentException("Can not match the request.",e); } return true; } public void appendTo(StringBuffer buffer) { } }); return null; }
public static Exception classEq(final Class<? extends Exception> in) { EasyMock.reportMatcher(new IArgumentMatcher() { @Override public void appendTo(StringBuffer buffer) { buffer.append("classEq("); buffer.append(in); buffer.append(")"); } @Override public boolean matches(Object arg) { return arg.getClass() == in; } }); return null; }
/** * Location equals. * * @param location the location */ private Location locationEq(final Location location) { EasyMock.reportMatcher(new IArgumentMatcher() { @Override public boolean matches(Object object) { if (object == null || location == null) { return location == object; } Location location2 = (Location) object; return location.hasAccuracy() == location2.hasAccuracy() && (!location.hasAccuracy() || location.getAccuracy() == location2.getAccuracy()) && location.hasAltitude() == location2.hasAltitude() && (!location.hasAltitude() || location.getAltitude() == location2.getAltitude()) && location.hasBearing() == location2.hasBearing() && (!location.hasBearing() || location.getBearing() == location2.getBearing()) && location.hasSpeed() == location2.hasSpeed() && (!location.hasSpeed() || location.getSpeed() == location2.getSpeed()) && location.getLatitude() == location2.getLatitude() && location.getLongitude() == location2.getLongitude() && location.getTime() == location2.getTime(); } @Override public void appendTo(StringBuffer buffer) { buffer.append("locationEq("); buffer.append(location); buffer.append(")"); } }); return null; }
private static com.amazonaws.regions.Region eqRegion(final com.amazonaws.regions.Region region) { EasyMock.reportMatcher(new IArgumentMatcher() { @Override public boolean matches(Object o) { com.amazonaws.regions.Region givenRegion = (com.amazonaws.regions.Region) o; return givenRegion.getName().equals(region.getName()); } @Override public void appendTo(StringBuffer stringBuffer) { stringBuffer.append("eqReqion for ").append(region.getName()); } }); return region; }
public static Location[] eqLoc(Location[] expected) { IArgumentMatcher matcher = new LocationsMatcher(expected); AndroidMock.reportMatcher(matcher); return null; }
/** * Reports an argument matcher. This method is needed to define custom argument * matchers. * * For example: * * {@code * AndroidMock.reportMatcher(new IntIsFortyTwo()); * AndroidMock.expect(mockObject.getString(null)).andReturn("hello world");} * * This example will expect a parameter for {@code mockObject.getString(int)} that matches the * conditions required by the {@code matches} method as defined by * {@link org.easymock.IArgumentMatcher#matches(Object)}. * * @param matcher the matcher whose {@code matches} method will be applied to the incoming * parameter to the mocked method. */ public static void reportMatcher(IArgumentMatcher matcher) { EasyMock.reportMatcher(matcher); }
/** * Reports an argument matcher. This method is needed to define own argument * matchers. For details, see the EasyMock documentation. * * @param matcher */ protected final void reportMatcher(final IArgumentMatcher matcher) { EasyMock.reportMatcher(matcher); }