Search in sources :

Example 1 with ApplicationMessageEvent

use of nl.nn.adapterframework.lifecycle.ApplicationMessageEvent in project iaf by ibissource.

the class DatabaseClassLoaderTest method mockDatabase.

private void mockDatabase(boolean throwException) throws Exception {
    // Mock a FixedQuerySender
    FixedQuerySender fq = mock(FixedQuerySender.class);
    doReturn(new GenericDbmsSupport()).when(fq).getDbmsSupport();
    Connection conn = mock(Connection.class);
    doReturn(conn).when(fq).getConnection();
    PreparedStatement stmt = mock(PreparedStatement.class);
    doReturn(stmt).when(conn).prepareStatement(anyString());
    ResultSet rs = mock(ResultSet.class);
    doReturn(!throwException).when(rs).next();
    doReturn("dummy").when(rs).getString(anyInt());
    URL file = this.getClass().getResource(JAR_FILE);
    doReturn(Misc.streamToBytes(file.openStream())).when(rs).getBytes(anyInt());
    doReturn(rs).when(stmt).executeQuery();
    doReturn(fq).when(ibisContext).createBeanAutowireByName(FixedQuerySender.class);
    // IbisContext.log is a void method
    @SuppressWarnings("rawtypes") Answer answer = new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            String message = invocation.getArgument(0);
            MessageKeeperLevel level = invocation.getArgument(1);
            Exception exception = invocation.getArgument(2);
            new ApplicationMessageEvent(spy(ApplicationContext.class), message, level, exception);
            return null;
        }
    };
    // Mock the IbisContext's log method which uses getApplicationContext which in turn creates a
    // new ApplicationContext if non exists. This functionality should be removed sometime in the future.
    // During testing, the IbisContext never initialises and thus there is no ApplicationContext. The
    // creation of the ApplicationContext during the test phase causes IllegalStateExceptions
    // In turn this causes the actual thing we want to test to never be 'hit', aka the log message.
    doAnswer(answer).when(ibisContext).log(anyString(), any(MessageKeeperLevel.class), any(Exception.class));
}
Also used : MessageKeeperLevel(nl.nn.adapterframework.util.MessageKeeper.MessageKeeperLevel) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) GenericDbmsSupport(nl.nn.adapterframework.jdbc.dbms.GenericDbmsSupport) URL(java.net.URL) ClassLoaderException(nl.nn.adapterframework.configuration.ClassLoaderException) Answer(org.mockito.stubbing.Answer) ApplicationContext(org.springframework.context.ApplicationContext) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ResultSet(java.sql.ResultSet) ApplicationMessageEvent(nl.nn.adapterframework.lifecycle.ApplicationMessageEvent) FixedQuerySender(nl.nn.adapterframework.jdbc.FixedQuerySender)

Example 2 with ApplicationMessageEvent

use of nl.nn.adapterframework.lifecycle.ApplicationMessageEvent in project iaf by ibissource.

the class ApplicationMessageEventTest method testGlobalMessage.

@Test
public void testGlobalMessage() {
    TestConfiguration configuration = new TestConfiguration();
    configuration.setVersion("13-2342");
    configuration.publishEvent(new ApplicationMessageEvent(configuration, "test 123"));
    MessageKeeper globalKeeper = configuration.getGlobalMessageKeeper();
    assertEquals(2, globalKeeper.size());
    assertEquals("INFO", globalKeeper.getMessage(1).getMessageLevel());
    assertEquals("Application [TestConfiguration] test 123", globalKeeper.getMessage(1).getMessageText());
    MessageKeeper configKeeper = configuration.getMessageKeeper();
    assertEquals(1, configKeeper.size());
    assertEquals("INFO", configKeeper.getMessage(0).getMessageLevel());
    assertThat(configKeeper.getMessage(0).getMessageText(), Matchers.containsString("Configuration [TestConfiguration] configured in "));
}
Also used : TestConfiguration(nl.nn.adapterframework.testutil.TestConfiguration) MessageKeeper(nl.nn.adapterframework.util.MessageKeeper) ApplicationMessageEvent(nl.nn.adapterframework.lifecycle.ApplicationMessageEvent) Test(org.junit.Test)

Aggregations

ApplicationMessageEvent (nl.nn.adapterframework.lifecycle.ApplicationMessageEvent)2 URL (java.net.URL)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 ClassLoaderException (nl.nn.adapterframework.configuration.ClassLoaderException)1 FixedQuerySender (nl.nn.adapterframework.jdbc.FixedQuerySender)1 GenericDbmsSupport (nl.nn.adapterframework.jdbc.dbms.GenericDbmsSupport)1 TestConfiguration (nl.nn.adapterframework.testutil.TestConfiguration)1 MessageKeeper (nl.nn.adapterframework.util.MessageKeeper)1 MessageKeeperLevel (nl.nn.adapterframework.util.MessageKeeper.MessageKeeperLevel)1 Test (org.junit.Test)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 Answer (org.mockito.stubbing.Answer)1 ApplicationContext (org.springframework.context.ApplicationContext)1