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
|
||||
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
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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