use of org.hamcrest.BaseMatcher in project gradle by gradle.
the class FileCollectionMatchers method sameCollection.
@Factory
public static <T extends FileCollection> Matcher<T> sameCollection(final FileCollection expected) {
return new BaseMatcher<T>() {
public boolean matches(Object o) {
FileCollection actual = (FileCollection) o;
List<? extends FileCollection> actualCollections = unpack(actual);
List<? extends FileCollection> expectedCollections = unpack(expected);
boolean equals = actualCollections.equals(expectedCollections);
if (!equals) {
System.out.println("expected: " + expectedCollections);
System.out.println("actual: " + actualCollections);
}
return equals;
}
private List<? extends FileCollection> unpack(FileCollection expected) {
if (expected instanceof UnionFileCollection) {
UnionFileCollection collection = (UnionFileCollection) expected;
return new ArrayList<FileCollection>(collection.getSources());
}
if (expected instanceof DefaultConfigurableFileCollection) {
DefaultConfigurableFileCollection collection = (DefaultConfigurableFileCollection) expected;
return new ArrayList<FileCollection>((Set) collection.getFrom());
}
if (expected instanceof CompositeFileCollection) {
CompositeFileCollection collection = (CompositeFileCollection) expected;
DefaultFileCollectionResolveContext context = new DefaultFileCollectionResolveContext(TestFiles.resolver());
collection.visitContents(context);
return context.resolveAsFileCollections();
}
throw new RuntimeException("Cannot get children of " + expected);
}
public void describeTo(Description description) {
description.appendText("same file collection as ").appendValue(expected);
}
};
}
use of org.hamcrest.BaseMatcher in project neo4j by neo4j.
the class OnlineBackupCommandTest method exitCode.
private Matcher<CommandFailed> exitCode(final int expectedCode) {
return new BaseMatcher<CommandFailed>() {
@Override
public void describeTo(Description description) {
description.appendText("expected exit code ").appendValue(expectedCode);
}
@Override
public void describeMismatch(Object o, Description description) {
if (o instanceof CommandFailed) {
CommandFailed e = (CommandFailed) o;
description.appendText("but it was ").appendValue(e.code());
} else {
description.appendText("but exception is not a CommandFailed exception");
}
}
@Override
public boolean matches(Object o) {
if (o instanceof CommandFailed) {
CommandFailed e = (CommandFailed) o;
return expectedCode == e.code();
}
return false;
}
};
}
use of org.hamcrest.BaseMatcher in project platform_frameworks_base by android.
the class MockUtils method checkUserRestrictions.
public static Bundle checkUserRestrictions(String... keys) {
final Bundle expected = DpmTestUtils.newRestrictions(Preconditions.checkNotNull(keys));
final Matcher<Bundle> m = new BaseMatcher<Bundle>() {
@Override
public boolean matches(Object item) {
if (item == null)
return false;
return UserRestrictionsUtils.areEqual((Bundle) item, expected);
}
@Override
public void describeTo(Description description) {
description.appendText("User restrictions=" + getRestrictionsAsString(expected));
}
};
return MockitoHamcrest.argThat(m);
}
use of org.hamcrest.BaseMatcher in project flink by apache.
the class CheckpointStateRestoreTest method testSetState.
/**
* Tests that on restore the task state is reset for each stateful task.
*/
@Test
public void testSetState() {
try {
final ChainedStateHandle<StreamStateHandle> serializedState = CheckpointCoordinatorTest.generateChainedStateHandle(new SerializableObject());
KeyGroupRange keyGroupRange = KeyGroupRange.of(0, 0);
List<SerializableObject> testStates = Collections.singletonList(new SerializableObject());
final KeyGroupsStateHandle serializedKeyGroupStates = CheckpointCoordinatorTest.generateKeyGroupState(keyGroupRange, testStates);
final JobID jid = new JobID();
final JobVertexID statefulId = new JobVertexID();
final JobVertexID statelessId = new JobVertexID();
Execution statefulExec1 = mockExecution();
Execution statefulExec2 = mockExecution();
Execution statefulExec3 = mockExecution();
Execution statelessExec1 = mockExecution();
Execution statelessExec2 = mockExecution();
ExecutionVertex stateful1 = mockExecutionVertex(statefulExec1, statefulId, 0, 3);
ExecutionVertex stateful2 = mockExecutionVertex(statefulExec2, statefulId, 1, 3);
ExecutionVertex stateful3 = mockExecutionVertex(statefulExec3, statefulId, 2, 3);
ExecutionVertex stateless1 = mockExecutionVertex(statelessExec1, statelessId, 0, 2);
ExecutionVertex stateless2 = mockExecutionVertex(statelessExec2, statelessId, 1, 2);
ExecutionJobVertex stateful = mockExecutionJobVertex(statefulId, new ExecutionVertex[] { stateful1, stateful2, stateful3 });
ExecutionJobVertex stateless = mockExecutionJobVertex(statelessId, new ExecutionVertex[] { stateless1, stateless2 });
Map<JobVertexID, ExecutionJobVertex> map = new HashMap<JobVertexID, ExecutionJobVertex>();
map.put(statefulId, stateful);
map.put(statelessId, stateless);
CheckpointCoordinator coord = new CheckpointCoordinator(jid, 200000L, 200000L, 0, Integer.MAX_VALUE, ExternalizedCheckpointSettings.none(), new ExecutionVertex[] { stateful1, stateful2, stateful3, stateless1, stateless2 }, new ExecutionVertex[] { stateful1, stateful2, stateful3, stateless1, stateless2 }, new ExecutionVertex[0], new StandaloneCheckpointIDCounter(), new StandaloneCompletedCheckpointStore(1), null, Executors.directExecutor());
// create ourselves a checkpoint with state
final long timestamp = 34623786L;
coord.triggerCheckpoint(timestamp, false);
PendingCheckpoint pending = coord.getPendingCheckpoints().values().iterator().next();
final long checkpointId = pending.getCheckpointId();
SubtaskState checkpointStateHandles = new SubtaskState(serializedState, null, null, serializedKeyGroupStates, null);
coord.receiveAcknowledgeMessage(new AcknowledgeCheckpoint(jid, statefulExec1.getAttemptId(), checkpointId, new CheckpointMetrics(), checkpointStateHandles));
coord.receiveAcknowledgeMessage(new AcknowledgeCheckpoint(jid, statefulExec2.getAttemptId(), checkpointId, new CheckpointMetrics(), checkpointStateHandles));
coord.receiveAcknowledgeMessage(new AcknowledgeCheckpoint(jid, statefulExec3.getAttemptId(), checkpointId, new CheckpointMetrics(), checkpointStateHandles));
coord.receiveAcknowledgeMessage(new AcknowledgeCheckpoint(jid, statelessExec1.getAttemptId(), checkpointId));
coord.receiveAcknowledgeMessage(new AcknowledgeCheckpoint(jid, statelessExec2.getAttemptId(), checkpointId));
assertEquals(1, coord.getNumberOfRetainedSuccessfulCheckpoints());
assertEquals(0, coord.getNumberOfPendingCheckpoints());
// let the coordinator inject the state
coord.restoreLatestCheckpointedState(map, true, false);
// verify that each stateful vertex got the state
final TaskStateHandles taskStateHandles = new TaskStateHandles(serializedState, Collections.<Collection<OperatorStateHandle>>singletonList(null), Collections.<Collection<OperatorStateHandle>>singletonList(null), Collections.singletonList(serializedKeyGroupStates), null);
BaseMatcher<TaskStateHandles> matcher = new BaseMatcher<TaskStateHandles>() {
@Override
public boolean matches(Object o) {
if (o instanceof TaskStateHandles) {
return o.equals(taskStateHandles);
}
return false;
}
@Override
public void describeTo(Description description) {
description.appendValue(taskStateHandles);
}
};
verify(statefulExec1, times(1)).setInitialState(Mockito.argThat(matcher));
verify(statefulExec2, times(1)).setInitialState(Mockito.argThat(matcher));
verify(statefulExec3, times(1)).setInitialState(Mockito.argThat(matcher));
verify(statelessExec1, times(0)).setInitialState(Mockito.<TaskStateHandles>any());
verify(statelessExec2, times(0)).setInitialState(Mockito.<TaskStateHandles>any());
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of org.hamcrest.BaseMatcher in project neo4j by neo4j.
the class BoltMatchers method canReset.
public static Matcher<BoltStateMachine> canReset() {
return new BaseMatcher<BoltStateMachine>() {
@Override
public boolean matches(final Object item) {
final BoltStateMachine machine = (BoltStateMachine) item;
final BoltResponseRecorder recorder = new BoltResponseRecorder();
try {
machine.reset(recorder);
return recorder.responseCount() == 1 && machine.state() == READY;
} catch (BoltConnectionFatality boltConnectionFatality) {
return false;
}
}
@Override
public void describeTo(Description description) {
description.appendText("can reset");
}
};
}
Aggregations