Search in sources :

Example 1 with BaseMatcher

use of org.hamcrest.BaseMatcher in project sonarqube by SonarSource.

the class DefaultHttpDownloaderTest method openStream_network_errors.

@Test(timeout = 10000)
public void openStream_network_errors() throws IOException, URISyntaxException {
    // non routable address
    String url = "http://10.255.255.1";
    thrown.expect(SonarException.class);
    thrown.expect(hasCause(new BaseMatcher<Exception>() {

        @Override
        public boolean matches(Object ex) {
            return // Java 8
            ex instanceof NoRouteToHostException || ex instanceof SocketException || // Java 7 or before
            ex instanceof SocketTimeoutException;
        }

        @Override
        public void describeTo(Description arg0) {
        }
    }));
    DefaultHttpDownloader downloader = new DefaultHttpDownloader(new MapSettings(), 10, 50000);
    downloader.openStream(new URI(url));
}
Also used : SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) Description(org.hamcrest.Description) BaseMatcher(org.hamcrest.BaseMatcher) MapSettings(org.sonar.api.config.MapSettings) NoRouteToHostException(java.net.NoRouteToHostException) URI(java.net.URI) Test(org.junit.Test)

Example 2 with BaseMatcher

use of org.hamcrest.BaseMatcher in project neo4j by neo4j.

the class BoltMatchers method hasTransaction.

public static Matcher<BoltStateMachine> hasTransaction() {
    return new BaseMatcher<BoltStateMachine>() {

        @Override
        public boolean matches(final Object item) {
            final BoltStateMachine machine = (BoltStateMachine) item;
            final StatementProcessor statementProcessor = machine.statementProcessor();
            return statementProcessor != null && statementProcessor.hasTransaction();
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("no transaction");
        }
    };
}
Also used : Description(org.hamcrest.Description) BaseMatcher(org.hamcrest.BaseMatcher) BoltStateMachine(org.neo4j.bolt.v1.runtime.BoltStateMachine) StatementProcessor(org.neo4j.bolt.v1.runtime.StatementProcessor)

Example 3 with BaseMatcher

use of org.hamcrest.BaseMatcher in project byte-buddy by raphw.

the class AgentBuilderRedefinitionStrategyResubmissionStrategyTest method testRedefinition.

@Test
@SuppressWarnings("unchecked")
public void testRedefinition() throws Exception {
    when(instrumentation.isModifiableClass(Foo.class)).thenReturn(true);
    when(redefinitionBatchAllocator.batch(Mockito.any(List.class))).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            return Collections.singleton(invocationOnMock.getArgumentAt(0, List.class));
        }
    });
    when(rawMatcher.matches(new TypeDescription.ForLoadedType(Foo.class), Foo.class.getClassLoader(), JavaModule.ofType(Foo.class), Foo.class, Foo.class.getProtectionDomain())).thenReturn(true);
    when(matcher.matches(error)).thenReturn(true);
    when(resubmissionScheduler.isAlive()).thenReturn(true);
    ClassFileLocator classFileLocator = mock(ClassFileLocator.class);
    when(locationStrategy.classFileLocator(Foo.class.getClassLoader(), JavaModule.ofType(Foo.class))).thenReturn(classFileLocator);
    when(classFileLocator.locate(Foo.class.getName())).thenReturn(new ClassFileLocator.Resolution.Explicit(new byte[] { 1, 2, 3 }));
    AgentBuilder.RedefinitionStrategy.ResubmissionStrategy.Installation installation = new AgentBuilder.RedefinitionStrategy.ResubmissionStrategy.Enabled(resubmissionScheduler, matcher).install(instrumentation, locationStrategy, listener, installationListener, circularityLock, rawMatcher, AgentBuilder.RedefinitionStrategy.REDEFINITION, redefinitionBatchAllocator, redefinitionListener);
    installation.getInstallationListener().onInstall(instrumentation, classFileTransformer);
    installation.getListener().onError(Foo.class.getName(), Foo.class.getClassLoader(), JavaModule.ofType(Foo.class), false, error);
    ArgumentCaptor<Runnable> argumentCaptor = ArgumentCaptor.forClass(Runnable.class);
    verify(resubmissionScheduler).isAlive();
    verify(resubmissionScheduler).schedule(argumentCaptor.capture());
    argumentCaptor.getValue().run();
    verifyNoMoreInteractions(resubmissionScheduler);
    verify(instrumentation).isModifiableClass(Foo.class);
    verify(instrumentation).redefineClasses(Mockito.argThat(new BaseMatcher<ClassDefinition[]>() {

        @Override
        public boolean matches(Object o) {
            return ((ClassDefinition) o).getDefinitionClass() == Foo.class && Arrays.equals(((ClassDefinition) o).getDefinitionClassFile(), new byte[] { 1, 2, 3 });
        }

        @Override
        public void describeTo(Description description) {
        }
    }));
    verifyNoMoreInteractions(instrumentation);
    verify(rawMatcher).matches(new TypeDescription.ForLoadedType(Foo.class), Foo.class.getClassLoader(), JavaModule.ofType(Foo.class), Foo.class, Foo.class.getProtectionDomain());
    verifyNoMoreInteractions(rawMatcher);
    verify(redefinitionBatchAllocator).batch(Collections.<Class<?>>singletonList(Foo.class));
    verifyNoMoreInteractions(redefinitionBatchAllocator);
    verify(listener).onError(Foo.class.getName(), Foo.class.getClassLoader(), JavaModule.ofType(Foo.class), false, error);
    verifyNoMoreInteractions(listener);
    verify(matcher).matches(error);
    verifyNoMoreInteractions(matcher);
}
Also used : Description(org.hamcrest.Description) TypeDescription(net.bytebuddy.description.type.TypeDescription) ClassDefinition(java.lang.instrument.ClassDefinition) ClassFileLocator(net.bytebuddy.dynamic.ClassFileLocator) BaseMatcher(org.hamcrest.BaseMatcher) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TypeDescription(net.bytebuddy.description.type.TypeDescription) List(java.util.List) Test(org.junit.Test)

Example 4 with BaseMatcher

use of org.hamcrest.BaseMatcher in project gs-spring-security-3.2 by rwinch.

the class SecurityTests method inboxShowsOnlyRobsMessages.

@Test
public void inboxShowsOnlyRobsMessages() throws Exception {
    RequestBuilder request = get("/").with(user(rob).roles("USER"));
    mvc.perform(request).andExpect(model().attribute("messages", new BaseMatcher<List<Message>>() {

        @Override
        public boolean matches(Object other) {
            @SuppressWarnings("unchecked") List<Message> messages = (List<Message>) other;
            return messages.size() == 1 && messages.get(0).getId() == 100;
        }

        @Override
        public void describeTo(Description d) {
        }
    }));
}
Also used : Description(org.hamcrest.Description) RequestBuilder(org.springframework.test.web.servlet.RequestBuilder) BaseMatcher(org.hamcrest.BaseMatcher) Message(sample.data.Message) List(java.util.List) Test(org.junit.Test)

Example 5 with BaseMatcher

use of org.hamcrest.BaseMatcher in project gocd by gocd.

the class GoConfigServiceTest method isAPipelineSelectionsInstanceWith.

private Matcher<PipelineSelections> isAPipelineSelectionsInstanceWith(final boolean isBlacklist, final String... pipelineSelectionsInInstance) {
    return new BaseMatcher<PipelineSelections>() {

        public boolean matches(Object o) {
            PipelineSelections pipelineSelections = (PipelineSelections) o;
            assertThat(pipelineSelections.isBlacklist(), is(isBlacklist));
            List<String> expectedSelectionsAsList = Arrays.asList(pipelineSelectionsInInstance);
            assertEquals(pipelineSelections.getSelections(), ListUtil.join(expectedSelectionsAsList, ","));
            return true;
        }

        public void describeTo(Description description) {
        }
    };
}
Also used : PipelineSelections(com.thoughtworks.go.server.domain.user.PipelineSelections) Description(org.hamcrest.Description) BaseMatcher(org.hamcrest.BaseMatcher) StringContains.containsString(org.hamcrest.core.StringContains.containsString)

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