Search in sources :

Example 11 with BaseMatcher

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);
        }
    };
}
Also used : UnionFileCollection(org.gradle.api.internal.file.UnionFileCollection) Description(org.hamcrest.Description) BaseMatcher(org.hamcrest.BaseMatcher) ArrayList(java.util.ArrayList) DefaultConfigurableFileCollection(org.gradle.api.internal.file.collections.DefaultConfigurableFileCollection) DefaultFileCollectionResolveContext(org.gradle.api.internal.file.collections.DefaultFileCollectionResolveContext) CompositeFileCollection(org.gradle.api.internal.file.CompositeFileCollection) CompositeFileCollection(org.gradle.api.internal.file.CompositeFileCollection) UnionFileCollection(org.gradle.api.internal.file.UnionFileCollection) DefaultConfigurableFileCollection(org.gradle.api.internal.file.collections.DefaultConfigurableFileCollection) Factory(org.hamcrest.Factory)

Example 12 with BaseMatcher

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;
        }
    };
}
Also used : Description(org.hamcrest.Description) BaseMatcher(org.hamcrest.BaseMatcher) CommandFailed(org.neo4j.commandline.admin.CommandFailed)

Example 13 with BaseMatcher

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);
}
Also used : Description(org.hamcrest.Description) BaseMatcher(org.hamcrest.BaseMatcher) Bundle(android.os.Bundle)

Example 14 with BaseMatcher

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());
    }
}
Also used : Description(org.hamcrest.Description) HashMap(java.util.HashMap) JobVertexID(org.apache.flink.runtime.jobgraph.JobVertexID) KeyGroupRange(org.apache.flink.runtime.state.KeyGroupRange) KeyGroupsStateHandle(org.apache.flink.runtime.state.KeyGroupsStateHandle) ExecutionVertex(org.apache.flink.runtime.executiongraph.ExecutionVertex) SerializableObject(org.apache.flink.runtime.util.SerializableObject) StreamStateHandle(org.apache.flink.runtime.state.StreamStateHandle) Execution(org.apache.flink.runtime.executiongraph.Execution) ExecutionJobVertex(org.apache.flink.runtime.executiongraph.ExecutionJobVertex) TaskStateHandles(org.apache.flink.runtime.state.TaskStateHandles) AcknowledgeCheckpoint(org.apache.flink.runtime.messages.checkpoint.AcknowledgeCheckpoint) BaseMatcher(org.hamcrest.BaseMatcher) SerializableObject(org.apache.flink.runtime.util.SerializableObject) OperatorStateHandle(org.apache.flink.runtime.state.OperatorStateHandle) JobID(org.apache.flink.api.common.JobID) Test(org.junit.Test)

Example 15 with BaseMatcher

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");
        }
    };
}
Also used : Description(org.hamcrest.Description) BaseMatcher(org.hamcrest.BaseMatcher) BoltStateMachine(org.neo4j.bolt.v1.runtime.BoltStateMachine) BoltConnectionFatality(org.neo4j.bolt.v1.runtime.BoltConnectionFatality)

Aggregations

BaseMatcher (org.hamcrest.BaseMatcher)25 Description (org.hamcrest.Description)25 Test (org.junit.Test)6 Bundle (android.os.Bundle)4 List (java.util.List)3 BoltStateMachine (org.neo4j.bolt.v1.runtime.BoltStateMachine)3 HttpMessage (org.parosproxy.paros.network.HttpMessage)3 PipelineSelections (com.thoughtworks.go.server.domain.user.PipelineSelections)2 URI (java.net.URI)2 StatementProcessor (org.neo4j.bolt.v1.runtime.StatementProcessor)2 Uri (android.net.Uri)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 CreatedRadiator (com.paulhammant.buildradiator.model.CreatedRadiator)1 IOException (java.io.IOException)1 ClassDefinition (java.lang.instrument.ClassDefinition)1 NoRouteToHostException (java.net.NoRouteToHostException)1 SocketException (java.net.SocketException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1