package org.axonframework.boot;

import org.axonframework.boot.autoconfig.AxonServerAutoConfiguration;
import org.axonframework.eventhandling.saga.repository.SagaStore;
import org.axonframework.eventhandling.saga.repository.jdbc.JdbcSagaStore;
import org.axonframework.eventhandling.tokenstore.TokenStore;
import org.axonframework.eventhandling.tokenstore.jdbc.JdbcTokenStore;
import org.axonframework.eventsourcing.eventstore.EventStorageEngine;
import org.axonframework.eventsourcing.eventstore.jdbc.JdbcEventStorageEngine;
import org.junit.*;
import org.junit.runner.*;
import org.ops4j.pax.exam.Configuration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import javax.sql.DataSource;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

/**
 * Tests JDBC auto-configuration.
 *
 * @author Milan Savic
 */
@ContextConfiguration(classes = JdbcAutoConfigurationTest.Context.class)
@EnableAutoConfiguration(exclude = {
        JpaRepositoriesAutoConfiguration.class,
        HibernateJpaAutoConfiguration.class,
        AxonServerAutoConfiguration.class})
@RunWith(SpringRunner.class)
public class JdbcAutoConfigurationTest {

    @Autowired
    private ApplicationContext applicationContext;

    @Test
    public void testContextInitialization() {
        assertNotNull(applicationContext);

        assertTrue(applicationContext.getBean(EventStorageEngine.class) instanceof JdbcEventStorageEngine);
        assertTrue(applicationContext.getBean(TokenStore.class) instanceof JdbcTokenStore);
        assertTrue(applicationContext.getBean(SagaStore.class) instanceof JdbcSagaStore);
    }

    @Configuration
    public static class Context {

        @Bean
        public DataSource dataSource() throws SQLException {
            DatabaseMetaData databaseMetaData = mock(DatabaseMetaData.class);
            when(databaseMetaData.getDatabaseProductName()).thenReturn("H2");
            Connection connection = mock(Connection.class);
            when(connection.getMetaData()).thenReturn(databaseMetaData);
            DataSource dataSource = mock(DataSource.class);
            when(dataSource.getConnection()).thenReturn(connection);
            return dataSource;
        }
    }
}

@@ -1,8 +1,617 @@
+/*%0A * Copyright (c) 2010-2018. Axon Framework%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *     http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A%0A
 package 
@@ -1145,17 +1145,20 @@
 g.junit.
-*
+Test
 ;%0Aimport
@@ -1175,17 +1175,23 @@
 .runner.
-*
+RunWith
 ;%0Aimport
@@ -1640,16 +1640,140 @@
 n.Bean;%0A
+import org.springframework.context.annotation.EnableMBeanExport;%0Aimport org.springframework.jmx.support.RegistrationPolicy;%0A
 import o
@@ -1888,16 +1888,45 @@
 unner;%0A%0A
+import javax.sql.DataSource;%0A
 import j
@@ -2013,70 +2013,136 @@
 on;%0A
+%0A
 import 
-javax.sql.DataSource;%0A%0Aimport static org.junit.Assert.*
+static org.junit.Assert.assertNotNull;%0Aimport static org.junit.Assert.assertTrue;%0Aimport static org.mockito.Mockito.mock
 ;%0Aim
@@ -2173,17 +2173,20 @@
 Mockito.
-*
+when
 ;%0A%0A/**%0A 
@@ -2524,16 +2524,86 @@
 .class)%0A
+@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)%0A
 public c

Set test to ignore failed MBean registrations