Refine search
@Test public void testCountStarWithTimeFilterUsingStringLiteralsInvalid() throws Exception { // Strings are implicitly cast to timestamps. Test an invalid string. // This error message isn't ideal but it is at least better than silently ignoring the problem. expectedException.expect(RuntimeException.class); expectedException.expectMessage("Error while applying rule ReduceExpressionsRule"); expectedException.expectCause( ThrowableMessageMatcher.hasMessage(CoreMatchers.containsString("Illegal TIMESTAMP constant")) ); testQuery( "SELECT COUNT(*) FROM druid.foo\n" + "WHERE __time >= 'z2000-01-01 00:00:00' AND __time < '2001-01-01 00:00:00'\n", ImmutableList.of(), ImmutableList.of() ); }
@Factory public static <T extends Throwable> Matcher<T> hasMessage(final Matcher<String> matcher) { return new ThrowableMessageMatcher<T>(matcher); } }
expectedException.expect(ExecutionException.class); expectedException.expectCause(CoreMatchers.<IllegalStateException>instanceOf(IllegalStateException.class)); expectedException.expectCause( ThrowableMessageMatcher.hasMessage(CoreMatchers.containsString("concurrent 'eval' calls not permitted")) );
@Test public void noContentInPrologue() throws Exception { thrown.expect(IllegalArgumentException.class); thrown.expect(new ThrowableMessageMatcher<Throwable>(containsString("Improper section ordering: METADATA section must come before the content section"))); RerunnableChangeParser parser = new RerunnableChangeParser(); String fileContent = "\n" + "prologueContentBeforeSections" + "//// METADATA\n" + ""; parser.value(mock(ChangeType.class), null, fileContent, objectName, "schema", null); }
@Test public void usesChildFirstThenParentLookupAndFails() throws Exception { ClassLoader parent = Thread.currentThread().getContextClassLoader(); final ClassLoaderLookupPolicy lookupPolicy = mock(ClassLoaderLookupPolicy.class); when(lookupPolicy.getClassLookupStrategy(TEST_CLASS_NAME)).thenReturn(CHILD_FIRST); expected.expect(CompositeClassNotFoundException.class); expected.expectMessage(startsWith("Cannot load class '" + TEST_CLASS_NAME + "': [")); FineGrainedControlClassLoader ext = buildFineGrainedControlClassLoader(parent, lookupPolicy); expected.expect(expressionMatches((e) -> ((CompositeClassNotFoundException) e).getExceptions(), contains(expressionMatches((e) -> ((TestClassNotFoundException) e).getClassLoader(), is((ClassLoader) ext)), hasMessage(is(TEST_CLASS_NAME))))); invokeTestClassMethod(ext); }
/** * Adds {@code matcher} to the list of requirements for the message returned * from any thrown exception. */ public void expectMessage(Matcher<String> matcher) { expect(hasMessage(matcher)); }
@Test public void configFailsOnInitialize() throws Exception { final Lifecycle connProvider = mock(Lifecycle.class, withSettings().extraInterfaces(ConnectionProvider.class)); final String expectedExceptionMessage = "Init failed!"; doThrow(new RuntimeException(expectedExceptionMessage)).when(connProvider).initialise(); when(connectionProviderResolver.resolve(any())).thenReturn(new Pair<>(connProvider, mock(ResolverSetResult.class))); expected.expectCause(hasMessage(is(InitialisationException.class.getName() + ": " + expectedExceptionMessage))); try { provider.get(event); } finally { verify(connProvider).initialise(); verify(connProvider, never()).start(); verify(connProvider, never()).stop(); verify(connProvider).dispose(); } }
expectedException.expect(ExecutionException.class); expectedException.expectCause(CoreMatchers.instanceOf(ParseException.class)); expectedException.expectCause( ThrowableMessageMatcher.hasMessage( CoreMatchers.containsString("[Unable to parse value[foo] for field[met1]")
@Test public void noMultipleMetadataSections1() throws Exception { thrown.expect(IllegalArgumentException.class); thrown.expect(new ThrowableMessageMatcher<Throwable>(containsString("found these extra sections instances: [METADATA"))); RerunnableChangeParser parser = new RerunnableChangeParser(); String fileContent = "\n" + "//// METADATA\n" + "//// DROP\n" + "//// METADATA\n" + ""; parser.value(mock(ChangeType.class), null, fileContent, objectName, "schema", null); }
@Test public void usesParentFirstAndChildLookupAndFails() throws Exception { ClassLoader parent = Thread.currentThread().getContextClassLoader(); final ClassLoaderLookupPolicy lookupPolicy = mock(ClassLoaderLookupPolicy.class); when(lookupPolicy.getClassLookupStrategy(TEST_CLASS_NAME)).thenReturn(PARENT_FIRST); expected.expect(CompositeClassNotFoundException.class); expected.expectMessage(startsWith("Cannot load class '" + TEST_CLASS_NAME + "': [")); FineGrainedControlClassLoader ext = buildFineGrainedControlClassLoader(parent, lookupPolicy); expected.expect(expressionMatches((e) -> ((CompositeClassNotFoundException) e).getExceptions(), contains(hasMessage(is(TEST_CLASS_NAME)), expressionMatches((e) -> ((TestClassNotFoundException) e).getClassLoader(), is((ClassLoader) ext))))); invokeTestClassMethod(ext); }
/** * Verify that your code throws an exception whose message is matched * by a Hamcrest matcher. * <pre> @Test * public void throwsExceptionWhoseMessageCompliesWithMatcher() { * thrown.expectMessage(startsWith("What")); * throw new NullPointerException("What happened?"); * }</pre> * * @deprecated use {@code org.hamcrest.junit.ExpectedException.expectMessage()} */ @Deprecated public void expectMessage(Matcher<String> matcher) { expect(hasMessage(matcher)); }
@Test public void shouldThrowIfDirectory() throws Exception { // Given: final File dir = TMP.newFolder(); // Expect: expectedException.expect(KsqlException.class); expectedException.expectMessage("Failed to read file: " + dir.toString()); expectedException.expectCause(hasMessage(containsString("Is a directory"))); // When: cmd.execute(ImmutableList.of(dir.toString()), terminal); } }
@Override @Description("When the ProactorProcessingStrategy is configured and a transaction is active processing fails with an error") public void tx() throws Exception { flow = flowBuilder.get().processors(cpuLightProcessor, cpuIntensiveProcessor, blockingProcessor).build(); flow.initialise(); flow.start(); TransactionCoordination.getInstance().bindTransaction(new TestTransaction(muleContext)); expectedException.expect(MessagingException.class); expectedException.expectCause(instanceOf(DefaultMuleException.class)); expectedException.expectCause(hasMessage(equalTo(TRANSACTIONAL_ERROR_MESSAGE))); processFlow(testEvent()); }
@Test public void noChangeSectionAllowed2() throws Exception { thrown.expect(IllegalArgumentException.class); thrown.expect(new ThrowableMessageMatcher<Throwable>(containsString("found these disallowed sections"))); RerunnableChangeParser parser = new RerunnableChangeParser(); String fileContent = "\n" + "//// CHANGE name=abc"; parser.value(mock(ChangeType.class), null, fileContent, objectName, "schema", null); }
/** * Adds {@code matcher} to the list of requirements for the message returned * from any thrown exception. */ public void expectMessage(Matcher<String> matcher) { expect(hasMessage(matcher)); }
@Factory public static <T extends Throwable> Matcher<T> hasMessage(final Matcher<String> matcher) { return new ThrowableMessageMatcher<T>(matcher); } }
@Test public void shouldFailOnECDSA384SigningWhenProvidedPrivateKeyIsNull() throws Exception { exception.expect(SignatureGenerationException.class); exception.expectMessage("The Token's Signature couldn't be generated when signing using the Algorithm: SHA384withECDSA"); exception.expectCause(isA(IllegalStateException.class)); exception.expectCause(hasMessage(is("The given Private Key is null."))); ECDSAKeyProvider provider = mock(ECDSAKeyProvider.class); when(provider.getPrivateKey()).thenReturn(null); Algorithm algorithm = Algorithm.ECDSA384(provider); algorithm.sign(new byte[0], new byte[0]); }
@Override @Description("When the ProactorProcessingStrategy is configured and a transaction is active processing fails with an error") public void tx() throws Exception { flow = flowBuilder.get().processors(cpuLightProcessor, cpuIntensiveProcessor, blockingProcessor).build(); flow.initialise(); flow.start(); TransactionCoordination.getInstance().bindTransaction(new TestTransaction(muleContext)); expectedException.expect(MessagingException.class); expectedException.expectCause(instanceOf(DefaultMuleException.class)); expectedException.expectCause(hasMessage(equalTo(TRANSACTIONAL_ERROR_MESSAGE))); processFlow(testEvent()); }
@Test public void noMultipleMetadataSections2() throws Exception { thrown.expect(IllegalArgumentException.class); thrown.expect(new ThrowableMessageMatcher<Throwable>(containsString("found these extra sections instances: [METADATA"))); RerunnableChangeParser parser = new RerunnableChangeParser(); String fileContent = "\n" + "//// METADATA\n" + "//// METADATA\n" + ""; parser.value(mock(ChangeType.class), null, fileContent, objectName, "schema", null); }
/** * Adds {@code matcher} to the list of requirements for the message returned * from any thrown exception. */ public void expectMessage(Matcher<String> matcher) { expect(hasMessage(matcher)); }