@Test @DirtiesContext @SuppressWarnings("unchecked") public void testWrongPayload() { final Message<String> stringMessage = MessageBuilder.withPayload("just text").build(); try { requestChannel.send(stringMessage); fail(); } catch (MessageHandlingException e) { String message = e.getCause().getMessage(); assertTrue("Wrong message: " + message, message.contains("The payload must be of type JobLaunchRequest.")); } Message<JobExecution> executionMessage = (Message<JobExecution>) responseChannel.receive(1000); assertNull("JobExecution message received when no return address set", executionMessage); }
@Test public void testExceptionRaised() throws Exception { final Message<JobLaunchRequest> message = MessageBuilder.withPayload(new JobLaunchRequest(new JobSupport("testJob"), new JobParameters())).build(); final JobLauncher jobLauncher = mock(JobLauncher.class); when(jobLauncher.run(any(Job.class), any(JobParameters.class))) .thenThrow(new JobParametersInvalidException("This is a JobExecutionException.")); JobLaunchingGateway jobLaunchingGateway = new JobLaunchingGateway(jobLauncher); try { jobLaunchingGateway.handleMessage(message); } catch (MessageHandlingException e) { assertEquals("This is a JobExecutionException.", e.getCause().getMessage()); return; } fail("Expecting a MessageHandlingException to be thrown."); } }
@Test public void saveToSubDirWithEmptyStringExpression() throws Exception { try { this.inputChannelSaveToSubDirEmptyStringExpression.send(message); } catch (MessageHandlingException e) { Assert.assertEquals("Unable to resolve Destination Directory for the provided Expression '' ''.", e.getCause().getMessage()); return; } Assert.fail("Was expecting a MessageHandlingException to be thrown"); }
@Test public void saveToSubDirWithWrongExpression() throws Exception { try { this.inputChannelSaveToSubDirWrongExpression.send(message); } catch (MessageHandlingException e) { Assert.assertEquals( TestUtils.applySystemFileSeparator("Destination path [target/base-directory/sub-directory/foo.txt] does not point to a directory."), e.getCause().getMessage()); return; } Assert.fail("Was expecting a MessageHandlingException to be thrown"); }
@Test public void saveToSubDirAutoCreateOff() throws Exception { try { this.inputChannelSaveToSubDirAutoCreateOff.send(message); } catch (MessageHandlingException e) { Assert.assertEquals( TestUtils.applySystemFileSeparator("Destination directory [target/base-directory2/sub-directory2] does not exist."), e.getCause().getMessage()); return; } Assert.fail("Was expecting a MessageHandlingException to be thrown"); }
@Test //INT-2631 public void testFailOperationOnAbstractBean() { try { Message<?> message = MessageBuilder.withPayload("abstractService.convert('testString')").build(); this.input.send(message); fail("Expected BeanIsAbstractException"); } catch (MessageHandlingException e) { Throwable cause = e.getCause(); assertTrue("Expected BeanIsAbstractException, got " + cause.getClass() + ":" + cause.getMessage(), cause instanceof BeanIsAbstractException); assertTrue(cause.getMessage().contains("abstractService")); } }
@Test //INT-2567 public void testFailOperationOnNonManagedComponent() { try { Message<?> message = MessageBuilder.withPayload("def result = nonManagedService.convert('testString')") .build(); this.input.send(message); fail("Expected BeanCreationNotAllowedException"); } catch (MessageHandlingException e) { Throwable cause = e.getCause(); assertTrue("Expected BeanCreationNotAllowedException, got " + cause.getClass() + ":" + cause.getMessage(), cause instanceof BeanCreationNotAllowedException); assertTrue(cause.getMessage().contains("nonManagedService")); } }
@Test public void testNullCorrelationKey() throws Exception { final Message<?> message1 = MessageBuilder.withPayload("foo").build(); when(correlationStrategy.getCorrelationKey(isA(Message.class))).thenReturn(null); try { handler.handleMessage(message1); fail("Expected MessageHandlingException"); } catch (MessageHandlingException e) { Throwable cause = e.getCause(); boolean pass = cause instanceof IllegalStateException && cause.getMessage().toLowerCase().contains("null correlation"); if (!pass) { throw e; } } }
@Test public void testTargetBeanResolver() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( this.getClass().getSimpleName() + "-fail-context.xml", this.getClass()); MessageChannel beanResolveIn = context.getBean("beanResolveIn", MessageChannel.class); SomeBean payload = new SomeBean("foo"); assertEquals("foo", payload.getNested().getValue()); try { beanResolveIn.send(new GenericMessage<SomeBean>(payload)); fail("Expected SpEL Exception"); } catch (MessageHandlingException e) { assertTrue(e.getCause() instanceof SpelEvaluationException); } context.close(); }
@Test public void missingOutputChannelVerifiedAtRuntime() { Message<?> request = new GenericMessage<String>("test"); try { handler.handleMessage(request); fail("Expected exception"); } catch (MessageHandlingException e) { assertThat(e.getCause(), instanceOf(DestinationResolutionException.class)); } }
@Test public void saveToSubWithFileExpressionUnsupportedObjectType() throws Exception { final Integer unsupportedObject = Integer.valueOf(1234); final Message<File> messageWithFileHeader = MessageBuilder.fromMessage(message) .setHeader("subDirectory", unsupportedObject) .build(); try { this.inputChannelSaveToSubDirWithFile.send(messageWithFileHeader); } catch (MessageHandlingException e) { Assert.assertEquals("The provided Destination Directory expression" + " (headers['subDirectory']) must evaluate to type " + "java.io.File or String, not java.lang.Integer.", e.getCause().getMessage()); return; } Assert.fail("Was expecting a MessageHandlingException to be thrown"); }
@Test @DirtiesContext public void testWithMissingMessageHeader() throws Exception { User user1 = new User("First User", "my first password", "email1"); Message<User> user1Message = MessageBuilder.withPayload(user1).build(); this.channel.send(user1Message); Message<?> receive = this.startErrorsChannel.receive(10000); assertNotNull(receive); assertThat(receive, instanceOf(ErrorMessage.class)); MessageHandlingException exception = (MessageHandlingException) receive.getPayload(); String expectedMessage = "Unable to resolve Stored Procedure/Function name " + "for the provided Expression 'headers['my_stored_procedure']'."; String actualMessage = exception.getCause().getMessage(); Assert.assertEquals(expectedMessage, actualMessage); }
@Test public void testSecurityContextPropagation() { login("bob", "bobspassword", "ROLE_ADMIN", "ROLE_PRESIDENT"); this.queueChannel.send(new GenericMessage<String>("test")); Message<?> receive = this.securedChannelQueue.receive(10000); assertNotNull(receive); SecurityContextHolder.clearContext(); this.queueChannel.send(new GenericMessage<String>("test")); Message<?> errorMessage = this.errorChannel.receive(10000); assertNotNull(errorMessage); Object payload = errorMessage.getPayload(); assertThat(payload, instanceOf(MessageHandlingException.class)); assertThat(((MessageHandlingException) payload).getCause(), instanceOf(AuthenticationCredentialsNotFoundException.class)); }
@Test public void testSecurityContextPropagationQueueChannel() { login("bob", "bobspassword", "ROLE_ADMIN", "ROLE_PRESIDENT"); this.queueChannel.send(new GenericMessage<String>("test")); Message<?> receive = this.securedChannelQueue.receive(10000); assertNotNull(receive); SecurityContextHolder.clearContext(); this.queueChannel.send(new GenericMessage<String>("test")); Message<?> errorMessage = this.errorChannel.receive(10000); assertNotNull(errorMessage); Object payload = errorMessage.getPayload(); assertThat(payload, instanceOf(MessageHandlingException.class)); assertThat(((MessageHandlingException) payload).getCause(), instanceOf(AuthenticationCredentialsNotFoundException.class)); }
@Test public void saveToSubWithFileExpressionNull() throws Exception { final File directory = null; final Message<File> messageWithFileHeader = MessageBuilder.fromMessage(message) .setHeader("subDirectory", directory) .build(); try { this.inputChannelSaveToSubDirWithFile.send(messageWithFileHeader); } catch (MessageHandlingException e) { Assert.assertEquals("The provided Destination Directory expression " + "(headers['subDirectory']) must not evaluate to null.", e.getCause().getMessage()); return; } Assert.fail("Was expecting a MessageHandlingException to be thrown"); }
@Test public void testSecurityContextPropagationExecutorChannel() { login("bob", "bobspassword", "ROLE_ADMIN", "ROLE_PRESIDENT"); this.executorChannel.send(new GenericMessage<String>("test")); Message<?> receive = this.securedChannelQueue.receive(10000); assertNotNull(receive); SecurityContextHolder.clearContext(); this.executorChannel.send(new GenericMessage<String>("test")); Message<?> errorMessage = this.errorChannel.receive(10000); assertNotNull(errorMessage); Object payload = errorMessage.getPayload(); assertThat(payload, instanceOf(MessageHandlingException.class)); assertThat(((MessageHandlingException) payload).getCause(), instanceOf(AuthenticationCredentialsNotFoundException.class)); }
@Test public void testInt2720XmppUriVariables() throws Exception { willThrow(new WebServiceIOException("intentional")) .given(this.xmppConnection).sendStanza(Mockito.any(Stanza.class)); Message<?> message = MessageBuilder.withPayload("<spring/>").setHeader("to", "user").build(); try { this.inputXmpp.send(message); } catch (MessageHandlingException e) { // expected Class<?> causeType = e.getCause().getClass(); assertTrue(WebServiceIOException.class.equals(causeType)); // offline } ArgumentCaptor<Stanza> argument = ArgumentCaptor.forClass(Stanza.class); Mockito.verify(this.xmppConnection).sendStanza(argument.capture()); assertEquals("user@jabber.org", argument.getValue().getTo().toString()); assertEquals("xmpp:user@jabber.org", this.interceptor.getLastUri().toString()); }
@Test public void invalidServiceName() { RmiOutboundGateway gateway = new RmiOutboundGateway("rmi://localhost:1099/noSuchService"); boolean exceptionThrown = false; try { gateway.handleMessage(new GenericMessage<>("test")); } catch (MessageHandlingException e) { assertEquals(RemoteLookupFailureException.class, e.getCause().getClass()); exceptionThrown = true; } assertTrue(exceptionThrown); }
@Test public void invalidHost() { RmiOutboundGateway gateway = new RmiOutboundGateway("rmi://noSuchHost:1099/testRemoteHandler"); boolean exceptionThrown = false; try { gateway.handleMessage(new GenericMessage<>("test")); } catch (MessageHandlingException e) { assertEquals(RemoteLookupFailureException.class, e.getCause().getClass()); exceptionThrown = true; } assertTrue(exceptionThrown); }
@Test public void invalidUrl() { RmiOutboundGateway gateway = new RmiOutboundGateway("invalid"); boolean exceptionThrown = false; try { gateway.handleMessage(new GenericMessage<>("test")); } catch (MessageHandlingException e) { assertEquals(RemoteLookupFailureException.class, e.getCause().getClass()); exceptionThrown = true; } assertTrue(exceptionThrown); }