@Test public void testSchemaEvolutionToIncompatibleType() throws Exception { byte[] originalIntData = buildAvroData(new Schema.Parser().parse( getAvroSchema("int_to_string_field", "\"int\"")), "int_to_string_field", 100); DecoderTestColumnHandle stringColumnReadingIntData = new DecoderTestColumnHandle(0, "row0", VARCHAR, "int_to_string_field", null, null, false, false, false); String changedTypeSchema = getAvroSchema("int_to_string_field", "\"string\""); assertThatThrownBy(() -> decodeRow(originalIntData, ImmutableSet.of(stringColumnReadingIntData), ImmutableMap.of(DATA_SCHEMA, changedTypeSchema))) .isInstanceOf(PrestoException.class) .hasCauseExactlyInstanceOf(AvroTypeException.class) .hasStackTraceContaining("Found int, expecting string") .hasMessageMatching("Decoding Avro record failed."); }
@Test public void unwrap() { RuntimeException re = new RuntimeException("my runtime"); assertThat(ExceptionUtil.unwrap(re)).isSameAs(re); ExecutionException ee = new ExecutionException(re); assertThat(ExceptionUtil.unwrap(ee)).isSameAs(re); InvocationTargetException ie = new InvocationTargetException(re); assertThat(ExceptionUtil.unwrap(ie)).isSameAs(re); Exception e = new Exception("my exception"); ExecutionException ee2 = new ExecutionException(e); try{ ExceptionUtil.unwrapAndUnchecked(ee2); }catch (Throwable t) { assertThat(t).isInstanceOf(UncheckedException.class).hasCauseExactlyInstanceOf(Exception.class); } }
@Test public void testWithFileWithInvalidMissingDir() throws Exception { String inputFileString = "readonly-dir/missing-dir/test.timestamp"; File inputFile = new File(inputFileString); File readOnlyDir = inputFile.getParentFile().getParentFile(); Files.createDirectory(readOnlyDir.toPath()); assertThat(readOnlyDir.setReadOnly()).isTrue(); assertThatThrownBy(() -> runAndVerifyCliForFile(inputFileString)) .isInstanceOf(RuntimeException.class) .hasCauseExactlyInstanceOf(AccessDeniedException.class); }
@Test public void shouldMapInterruptedExceptionToNcleIfLeadingStatusChanges() { Callable<Void> delegate = () -> { throw new InterruptedException(TEST_MESSAGE); }; assertThatThrownBy(() -> loseLeadershipDuringCallToProxyFor(delegate)) .isInstanceOf(NotCurrentLeaderException.class) .hasMessage("received an interrupt due to leader election.") .hasCauseExactlyInstanceOf(InterruptedException.class) .hasStackTraceContaining(TEST_MESSAGE); }
@Test public void testXsltPayloadWithBadTransformerFactoryClassName() throws IOException { XsltPayloadTransformer transformer = new XsltPayloadTransformer(getXslResourceThatOutputsText(), "foo.bar.Baz"); transformer.setBeanFactory(Mockito.mock(BeanFactory.class)); assertThatThrownBy(transformer::afterPropertiesSet) .isExactlyInstanceOf(IllegalStateException.class) .hasCauseExactlyInstanceOf(ClassNotFoundException.class); }
@Test public void testMessageAsArgumentLambda() { LambdaMessageProcessor lmp = new LambdaMessageProcessor( (GenericTransformer<Message<?>, Message<?>>) source -> messageTransformer(source), null); lmp.setBeanFactory(mock(BeanFactory.class)); GenericMessage<String> testMessage = new GenericMessage<>("foo"); assertThatThrownBy(() -> lmp.processMessage(testMessage)).hasCauseExactlyInstanceOf(ClassCastException.class); }
@Test public void testDisabledBeansOverride() { assertThatThrownBy( () -> this.integrationFlowContext .registration(f -> f.channel(c -> c.direct("doNotOverrideChannel"))) .register()) .isExactlyInstanceOf(BeanCreationException.class) .hasCauseExactlyInstanceOf(BeanDefinitionOverrideException.class) .hasMessageContaining("Invalid bean definition with name 'doNotOverrideChannel'"); }
@Test public void testTimeout() throws Exception { ReplyingKafkaTemplate<Integer, String, String> template = createTemplate(A_REPLY); try { template.setReplyTimeout(1); ProducerRecord<Integer, String> record = new ProducerRecord<>(A_REQUEST, "fiz"); record.headers().add(new RecordHeader(KafkaHeaders.REPLY_TOPIC, A_REPLY.getBytes())); RequestReplyFuture<Integer, String, String> future = template.sendAndReceive(record); future.getSendFuture().get(10, TimeUnit.SECONDS); // send ok try { future.get(30, TimeUnit.SECONDS); fail("Expected Exception"); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw e; } catch (ExecutionException e) { assertThat(e).hasCauseExactlyInstanceOf(KafkaException.class).hasMessageContaining("Reply timed out"); } } finally { template.stop(); } }
@Test public void testMailboxExceptionCause() { MailboxException mbe = new MailboxException(EXCEPTION_MESSAGE, EXCEPTION_CAUSE); assertThat(mbe).hasMessage(EXCEPTION_MESSAGE).hasCauseExactlyInstanceOf(Exception.class); assertThat(mbe.getCause()).hasMessage(CAUSE_MESSAGE); }
@Test public void getSession_ConfigWithInvalidEndpointGiven_ShouldThrowExceptionProperly() { // Arrange props.setProperty(DatabaseConfig.CONTACT_POINTS, INVALID_ADDRESS); config = new DatabaseConfig(props); manager = Mockito.spy(new ClusterManager(config)); // Act Assert assertThatThrownBy( () -> { manager.getSession(); }) .isInstanceOf(ConnectionException.class) .hasCauseExactlyInstanceOf(IllegalArgumentException.class); // Assert verify(cluster, never()).connect(); }
@Test public void handle_MultiPartitionOperationsGiven_ShouldThrowMultiPartitionException() { // Arrange configureBehavior(); mutations = prepareMultiPartitionPuts(); // Act Assert assertThatThrownBy( () -> { batch.handle(mutations); }) .isInstanceOf(RetriableExecutionException.class) .hasCauseExactlyInstanceOf(MultiPartitionException.class); }
@Test public void testSchemaEvolutionToIncompatibleType() throws Exception { byte[] originalIntData = buildAvroData(new Schema.Parser().parse( getAvroSchema("int_to_string_field", "\"int\"")), "int_to_string_field", 100); DecoderTestColumnHandle stringColumnReadingIntData = new DecoderTestColumnHandle(0, "row0", VARCHAR, "int_to_string_field", null, null, false, false, false); String changedTypeSchema = getAvroSchema("int_to_string_field", "\"string\""); assertThatThrownBy(() -> decodeRow(originalIntData, ImmutableSet.of(stringColumnReadingIntData), ImmutableMap.of(DATA_SCHEMA, changedTypeSchema))) .isInstanceOf(PrestoException.class) .hasCauseExactlyInstanceOf(AvroTypeException.class) .hasStackTraceContaining("Found int, expecting string") .hasMessageMatching("Decoding Avro record failed."); }
@Test public void testSchemaEvolutionToIncompatibleType() throws Exception { byte[] originalIntData = buildAvroData(new Schema.Parser().parse( getAvroSchema("int_to_string_field", "\"int\"")), "int_to_string_field", 100); DecoderTestColumnHandle stringColumnReadingIntData = new DecoderTestColumnHandle(0, "row0", VARCHAR, "int_to_string_field", null, null, false, false, false); String changedTypeSchema = getAvroSchema("int_to_string_field", "\"string\""); assertThatThrownBy(() -> decodeRow(originalIntData, ImmutableSet.of(stringColumnReadingIntData), ImmutableMap.of(DATA_SCHEMA, changedTypeSchema))) .isInstanceOf(PrestoException.class) .hasCauseExactlyInstanceOf(AvroTypeException.class) .hasStackTraceContaining("Found int, expecting string") .hasMessageMatching("Decoding Avro record failed."); }
@Test public void handle_MultiTableOperationsGiven_ShouldThrowMultiPartitionException() { // Arrange configureBehavior(); mutations = prepareConditionalPuts(); mutations.get(1).forNamespace(ANY_KEYSPACE_NAME).forTable(ANOTHER_TABLE_NAME); // Act Assert assertThatThrownBy( () -> { batch.handle(mutations); }) .isInstanceOf(RetriableExecutionException.class) .hasCauseExactlyInstanceOf(MultiPartitionException.class); }
.hasCauseExactlyInstanceOf(IllegalArgumentException.class) .hasMessageContaining("Queue with name 'foo' does not exist");
@Test public void testFailOnErrorEnable() { EC2InstanceScanner scanner = getProjector().createBuilder(AWSScannerBuilder.class).withAccountId("111222333444") .withFailOnError(true).build(EC2InstanceScanner.class); try { Assertions.assertThat(scanner.isFailOnError()).isTrue(); scanner.maybeThrow(new RuntimeException("foo")); } catch (RuntimeException e) { Assertions.assertThat(e).hasMessageContaining("foo"); } try { Assertions.assertThat(scanner.isFailOnError()).isTrue(); scanner.maybeThrow(new IOException("foo")); } catch (RuntimeException e) { Assertions.assertThat(e).hasMessageContaining("foo"); Assertions.assertThat(e).isInstanceOf(MercatorException.class).hasCauseExactlyInstanceOf(IOException.class); } }
@Test public void testUploadInputStream() throws IOException { Expression actualKeyExpression = TestUtils.getPropertyValue(this.s3MessageHandler, "keyExpression", Expression.class); this.s3MessageHandler.setKeyExpression(null); InputStream payload = new StringInputStream("a"); Message<?> message = MessageBuilder.withPayload(payload) .setHeader("s3Command", S3MessageHandler.Command.UPLOAD.name()) .setHeader("key", "myStream") .build(); assertThatThrownBy(() -> this.s3SendChannel.send(message)) .hasCauseExactlyInstanceOf(IllegalStateException.class) .hasMessageContaining("Specify a 'keyExpression' for non-java.io.File payloads"); this.s3MessageHandler.setKeyExpression(actualKeyExpression); this.s3SendChannel.send(message); ArgumentCaptor<PutObjectRequest> putObjectRequestArgumentCaptor = ArgumentCaptor.forClass(PutObjectRequest.class); verify(this.amazonS3, atLeastOnce()).putObject(putObjectRequestArgumentCaptor.capture()); PutObjectRequest putObjectRequest = putObjectRequestArgumentCaptor.getValue(); assertThat(putObjectRequest.getBucketName()).isEqualTo(S3_BUCKET_NAME); assertThat(putObjectRequest.getKey()).isEqualTo("myStream"); assertThat(putObjectRequest.getFile()).isNull(); assertThat(putObjectRequest.getInputStream()).isNotNull(); ObjectMetadata metadata = putObjectRequest.getMetadata(); assertThat(metadata.getContentMD5()).isEqualTo(Md5Utils.md5AsBase64(payload)); assertThat(metadata.getContentLength()).isEqualTo(1); assertThat(metadata.getContentType()).isEqualTo(MediaType.APPLICATION_JSON_VALUE); assertThat(metadata.getContentDisposition()).isEqualTo("test.json"); }
.hasCauseExactlyInstanceOf(WrongEpochException.class));
service2.run(); } catch (final Exception e) { assertThat(e.getCause()).hasCauseExactlyInstanceOf(PeerDiscoveryServiceException.class); assertThat(e.getCause()) .hasMessageStartingWith(