diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/hawkbit-jpa-defaults.properties b/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/hawkbit-jpa-defaults.properties index 73c7fbccf..47910214b 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/hawkbit-jpa-defaults.properties +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/hawkbit-jpa-defaults.properties @@ -10,7 +10,6 @@ ### JPA / Datasource - START spring.jpa.database=H2 spring.jpa.show-sql=false -spring.datasource.tomcat.defaultAutoCommit=false # Logging spring.datasource.eclipselink.logging.logger=JavaLogger spring.jpa.properties.eclipselink.logging.level=off diff --git a/hawkbit-repository/hawkbit-repository-test/src/main/java/org/eclipse/hawkbit/repository/test/util/AbstractIntegrationTest.java b/hawkbit-repository/hawkbit-repository-test/src/main/java/org/eclipse/hawkbit/repository/test/util/AbstractIntegrationTest.java index fea130c9b..51354226b 100644 --- a/hawkbit-repository/hawkbit-repository-test/src/main/java/org/eclipse/hawkbit/repository/test/util/AbstractIntegrationTest.java +++ b/hawkbit-repository/hawkbit-repository-test/src/main/java/org/eclipse/hawkbit/repository/test/util/AbstractIntegrationTest.java @@ -56,9 +56,7 @@ import org.eclipse.hawkbit.security.ExcludePathAwareShallowETagFilter; import org.eclipse.hawkbit.security.SystemSecurityContext; import org.eclipse.hawkbit.tenancy.TenantAware; import org.junit.After; -import org.junit.AfterClass; import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Rule; import org.junit.rules.TestWatcher; import org.junit.runner.Description; @@ -102,8 +100,8 @@ import org.springframework.web.context.WebApplicationContext; // Cleaning repository will fire "delete" events. We won't count them to the // test execution. So, the order execution between EventVerifier and Cleanup is // important! -@TestExecutionListeners(inheritListeners = true, listeners = { EventVerifier.class, - CleanupTestExecutionListener.class }, mergeMode = MergeMode.MERGE_WITH_DEFAULTS) +@TestExecutionListeners(inheritListeners = true, listeners = { EventVerifier.class, CleanupTestExecutionListener.class, + MySqlTestDatabase.class }, mergeMode = MergeMode.MERGE_WITH_DEFAULTS) public abstract class AbstractIntegrationTest implements EnvironmentAware { private static final Logger LOG = LoggerFactory.getLogger(AbstractIntegrationTest.class); @@ -317,25 +315,4 @@ public abstract class AbstractIntegrationTest implements EnvironmentAware { "/{tenant}/controller/v1/{controllerId}/softwaremodules/{softwareModuleId}/artifacts/**", "/api/v1/downloadserver/**")); } - - private static CIMySqlTestDatabase tesdatabase; - - @BeforeClass - public static void beforeClass() { - createTestdatabaseAndStart(); - } - - private static synchronized void createTestdatabaseAndStart() { - if ("MYSQL".equals(System.getProperty("spring.jpa.database"))) { - tesdatabase = new CIMySqlTestDatabase(); - tesdatabase.before(); - } - } - - @AfterClass - public static void afterClass() { - if (tesdatabase != null) { - tesdatabase.after(); - } - } } diff --git a/hawkbit-repository/hawkbit-repository-test/src/main/java/org/eclipse/hawkbit/repository/test/util/CIMySqlTestDatabase.java b/hawkbit-repository/hawkbit-repository-test/src/main/java/org/eclipse/hawkbit/repository/test/util/MySqlTestDatabase.java similarity index 68% rename from hawkbit-repository/hawkbit-repository-test/src/main/java/org/eclipse/hawkbit/repository/test/util/CIMySqlTestDatabase.java rename to hawkbit-repository/hawkbit-repository-test/src/main/java/org/eclipse/hawkbit/repository/test/util/MySqlTestDatabase.java index cd2054aa3..a6741e225 100644 --- a/hawkbit-repository/hawkbit-repository-test/src/main/java/org/eclipse/hawkbit/repository/test/util/CIMySqlTestDatabase.java +++ b/hawkbit-repository/hawkbit-repository-test/src/main/java/org/eclipse/hawkbit/repository/test/util/MySqlTestDatabase.java @@ -16,14 +16,17 @@ import java.sql.SQLException; import org.apache.commons.lang3.RandomStringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.test.context.TestContext; +import org.springframework.test.context.TestExecutionListener; +import org.springframework.test.context.support.AbstractTestExecutionListener; /** - * {@link Testdatabase} implementation for MySQL. - * + * A {@link TestExecutionListener} for creating and dropping MySql schemas if + * tests are setup with MySql. */ -public class CIMySqlTestDatabase implements Testdatabase { +public class MySqlTestDatabase extends AbstractTestExecutionListener { - private static final Logger LOG = LoggerFactory.getLogger(CIMySqlTestDatabase.class); + private static final Logger LOG = LoggerFactory.getLogger(MySqlTestDatabase.class); private String schemaName; private String uri; private final String username; @@ -32,7 +35,7 @@ public class CIMySqlTestDatabase implements Testdatabase { /** * Constructor. */ - public CIMySqlTestDatabase() { + public MySqlTestDatabase() { this.username = System.getProperty("spring.datasource.username"); this.password = System.getProperty("spring.datasource.password"); this.uri = System.getProperty("spring.datasource.url"); @@ -47,8 +50,21 @@ public class CIMySqlTestDatabase implements Testdatabase { } @Override - public void before() { - createSchema(); + public void beforeTestClass(final TestContext testContext) throws Exception { + if (isRunningWithMySql()) { + createSchema(); + } + } + + @Override + public void afterTestClass(final TestContext testContext) throws Exception { + if (isRunningWithMySql()) { + dropSchema(); + } + } + + private boolean isRunningWithMySql() { + return "MYSQL".equals(System.getProperty("spring.jpa.database")); } private void createSchema() { @@ -56,6 +72,8 @@ public class CIMySqlTestDatabase implements Testdatabase { try (PreparedStatement statement = connection.prepareStatement("CREATE SCHEMA " + schemaName + ";")) { statement.execute(); LOG.info("Schema {} created on uri {}", schemaName, uri); + } finally { + connection.commit(); } } catch (final SQLException e) { LOG.error("Schema creation failed!", e); @@ -63,24 +81,16 @@ public class CIMySqlTestDatabase implements Testdatabase { } - @Override - public void after() { - dropSchema(); - } - private void dropSchema() { try (Connection connection = DriverManager.getConnection(uri, username, password)) { try (PreparedStatement statement = connection.prepareStatement("DROP SCHEMA " + schemaName + ";")) { statement.execute(); LOG.info("Schema {} dropped on uri {}", schemaName, uri); + } finally { + connection.commit(); } } catch (final SQLException e) { LOG.error("Schema drop failed!", e); } } - - @Override - public String getUri() { - return uri; - } } diff --git a/hawkbit-repository/hawkbit-repository-test/src/main/java/org/eclipse/hawkbit/repository/test/util/Testdatabase.java b/hawkbit-repository/hawkbit-repository-test/src/main/java/org/eclipse/hawkbit/repository/test/util/Testdatabase.java deleted file mode 100644 index 8a10558f3..000000000 --- a/hawkbit-repository/hawkbit-repository-test/src/main/java/org/eclipse/hawkbit/repository/test/util/Testdatabase.java +++ /dev/null @@ -1,23 +0,0 @@ -/** - * Copyright (c) 2015 Bosch Software Innovations GmbH and others. - * - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - */ -package org.eclipse.hawkbit.repository.test.util; - -/** - * - * - */ -public interface Testdatabase { - - void before(); - - void after(); - - public String getUri(); - -}