Feature make mysql testdb as testexecutionlistener (#557)
* make CIMySqlTestDatabase as TestExecutionListener for spring Signed-off-by: Michael Hirsch <michael.hirsch@bosch-si.com> * re-order MySqlTestDatabase to the end of the execution listeners Signed-off-by: Michael Hirsch <michael.hirsch@bosch-si.com> * remove property to disable autocommit Signed-off-by: Michael Hirsch <michael.hirsch@bosch-si.com> * commit connection when dropping creating schema Signed-off-by: Michael Hirsch <michael.hirsch@bosch-si.com> * re-name test execution listener for mysql and write javadoc Signed-off-by: Michael Hirsch <michael.hirsch@bosch-si.com>
This commit is contained in:
@@ -10,7 +10,6 @@
|
|||||||
### JPA / Datasource - START
|
### JPA / Datasource - START
|
||||||
spring.jpa.database=H2
|
spring.jpa.database=H2
|
||||||
spring.jpa.show-sql=false
|
spring.jpa.show-sql=false
|
||||||
spring.datasource.tomcat.defaultAutoCommit=false
|
|
||||||
# Logging
|
# Logging
|
||||||
spring.datasource.eclipselink.logging.logger=JavaLogger
|
spring.datasource.eclipselink.logging.logger=JavaLogger
|
||||||
spring.jpa.properties.eclipselink.logging.level=off
|
spring.jpa.properties.eclipselink.logging.level=off
|
||||||
|
|||||||
@@ -56,9 +56,7 @@ import org.eclipse.hawkbit.security.ExcludePathAwareShallowETagFilter;
|
|||||||
import org.eclipse.hawkbit.security.SystemSecurityContext;
|
import org.eclipse.hawkbit.security.SystemSecurityContext;
|
||||||
import org.eclipse.hawkbit.tenancy.TenantAware;
|
import org.eclipse.hawkbit.tenancy.TenantAware;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.AfterClass;
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.BeforeClass;
|
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.rules.TestWatcher;
|
import org.junit.rules.TestWatcher;
|
||||||
import org.junit.runner.Description;
|
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
|
// Cleaning repository will fire "delete" events. We won't count them to the
|
||||||
// test execution. So, the order execution between EventVerifier and Cleanup is
|
// test execution. So, the order execution between EventVerifier and Cleanup is
|
||||||
// important!
|
// important!
|
||||||
@TestExecutionListeners(inheritListeners = true, listeners = { EventVerifier.class,
|
@TestExecutionListeners(inheritListeners = true, listeners = { EventVerifier.class, CleanupTestExecutionListener.class,
|
||||||
CleanupTestExecutionListener.class }, mergeMode = MergeMode.MERGE_WITH_DEFAULTS)
|
MySqlTestDatabase.class }, mergeMode = MergeMode.MERGE_WITH_DEFAULTS)
|
||||||
public abstract class AbstractIntegrationTest implements EnvironmentAware {
|
public abstract class AbstractIntegrationTest implements EnvironmentAware {
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(AbstractIntegrationTest.class);
|
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/**",
|
"/{tenant}/controller/v1/{controllerId}/softwaremodules/{softwareModuleId}/artifacts/**",
|
||||||
"/api/v1/downloadserver/**"));
|
"/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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,14 +16,17 @@ import java.sql.SQLException;
|
|||||||
import org.apache.commons.lang3.RandomStringUtils;
|
import org.apache.commons.lang3.RandomStringUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
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 schemaName;
|
||||||
private String uri;
|
private String uri;
|
||||||
private final String username;
|
private final String username;
|
||||||
@@ -32,7 +35,7 @@ public class CIMySqlTestDatabase implements Testdatabase {
|
|||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
*/
|
*/
|
||||||
public CIMySqlTestDatabase() {
|
public MySqlTestDatabase() {
|
||||||
this.username = System.getProperty("spring.datasource.username");
|
this.username = System.getProperty("spring.datasource.username");
|
||||||
this.password = System.getProperty("spring.datasource.password");
|
this.password = System.getProperty("spring.datasource.password");
|
||||||
this.uri = System.getProperty("spring.datasource.url");
|
this.uri = System.getProperty("spring.datasource.url");
|
||||||
@@ -47,8 +50,21 @@ public class CIMySqlTestDatabase implements Testdatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void before() {
|
public void beforeTestClass(final TestContext testContext) throws Exception {
|
||||||
createSchema();
|
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() {
|
private void createSchema() {
|
||||||
@@ -56,6 +72,8 @@ public class CIMySqlTestDatabase implements Testdatabase {
|
|||||||
try (PreparedStatement statement = connection.prepareStatement("CREATE SCHEMA " + schemaName + ";")) {
|
try (PreparedStatement statement = connection.prepareStatement("CREATE SCHEMA " + schemaName + ";")) {
|
||||||
statement.execute();
|
statement.execute();
|
||||||
LOG.info("Schema {} created on uri {}", schemaName, uri);
|
LOG.info("Schema {} created on uri {}", schemaName, uri);
|
||||||
|
} finally {
|
||||||
|
connection.commit();
|
||||||
}
|
}
|
||||||
} catch (final SQLException e) {
|
} catch (final SQLException e) {
|
||||||
LOG.error("Schema creation failed!", e);
|
LOG.error("Schema creation failed!", e);
|
||||||
@@ -63,24 +81,16 @@ public class CIMySqlTestDatabase implements Testdatabase {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void after() {
|
|
||||||
dropSchema();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void dropSchema() {
|
private void dropSchema() {
|
||||||
try (Connection connection = DriverManager.getConnection(uri, username, password)) {
|
try (Connection connection = DriverManager.getConnection(uri, username, password)) {
|
||||||
try (PreparedStatement statement = connection.prepareStatement("DROP SCHEMA " + schemaName + ";")) {
|
try (PreparedStatement statement = connection.prepareStatement("DROP SCHEMA " + schemaName + ";")) {
|
||||||
statement.execute();
|
statement.execute();
|
||||||
LOG.info("Schema {} dropped on uri {}", schemaName, uri);
|
LOG.info("Schema {} dropped on uri {}", schemaName, uri);
|
||||||
|
} finally {
|
||||||
|
connection.commit();
|
||||||
}
|
}
|
||||||
} catch (final SQLException e) {
|
} catch (final SQLException e) {
|
||||||
LOG.error("Schema drop failed!", e);
|
LOG.error("Schema drop failed!", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getUri() {
|
|
||||||
return uri;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -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();
|
|
||||||
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user