Migrated MySQL schema creation to test package. Small doc improvements

on testdatafactory.

Signed-off-by: Kai Zimmermann <kai.zimmermann@bosch-si.com>
This commit is contained in:
Kai Zimmermann
2016-05-31 12:28:24 +02:00
parent b1a2a583e9
commit 91fd94b356
9 changed files with 52 additions and 184 deletions

View File

@@ -13,8 +13,6 @@ import javax.persistence.PersistenceContext;
import org.eclipse.hawkbit.cache.TenantAwareCacheManager;
import org.eclipse.hawkbit.repository.util.AbstractIntegrationTest;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.data.mongodb.gridfs.GridFsOperations;
@@ -79,26 +77,4 @@ public abstract class AbstractJpaIntegrationTest extends AbstractIntegrationTest
@Autowired
protected TenantAwareCacheManager cacheManager;
private static CIMySqlTestDatabase tesdatabase;
@BeforeClass
public static void beforeClass() {
createTestdatabaseAndStart();
}
private static 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();
}
}
}

View File

@@ -13,11 +13,8 @@ import javax.persistence.PersistenceContext;
import org.eclipse.hawkbit.cache.TenantAwareCacheManager;
import org.eclipse.hawkbit.repository.util.AbstractIntegrationTestWithMongoDB;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.data.mongodb.gridfs.GridFsOperations;
@SpringApplicationConfiguration(classes = { org.eclipse.hawkbit.RepositoryApplicationConfiguration.class,
TestConfiguration.class })
@@ -68,9 +65,6 @@ public abstract class AbstractJpaIntegrationTestWithMongoDB extends AbstractInte
@Autowired
protected TargetInfoRepository targetInfoRepository;
@Autowired
protected GridFsOperations operations;
@Autowired
protected RolloutGroupRepository rolloutGroupRepository;
@@ -80,25 +74,4 @@ public abstract class AbstractJpaIntegrationTestWithMongoDB extends AbstractInte
@Autowired
protected TenantAwareCacheManager cacheManager;
private static CIMySqlTestDatabase tesdatabase;
@BeforeClass
public static void beforeClass() {
createTestdatabaseAndStart();
}
private static 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();
}
}
}

View File

@@ -1,90 +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.jpa;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
*
*/
public class CIMySqlTestDatabase implements Testdatabase {
private final static Logger LOG = LoggerFactory.getLogger(CIMySqlTestDatabase.class);
private String schemaName;
private String uri;
private final String username;
private final String password;
public CIMySqlTestDatabase() {
this.username = System.getProperty("spring.datasource.username");
this.password = System.getProperty("spring.datasource.password");
this.uri = System.getProperty("spring.datasource.url");
createSchemaUri();
initSystemProperties();
}
private final void initSystemProperties() {
System.setProperty("spring.datasource.driverClassName", getDriverClassName());
System.setProperty("spring.jpa.database", "MYSQL");
}
private void createSchemaUri() {
schemaName = "SP" + RandomStringUtils.randomAlphanumeric(10);
this.uri = this.uri.substring(0, uri.lastIndexOf("/") + 1);
System.setProperty("spring.datasource.url", uri + schemaName);
}
@Override
public void before() {
createSchema();
}
private void createSchema() {
try (Connection connection = DriverManager.getConnection(uri, username, password)) {
connection.prepareStatement("CREATE SCHEMA " + schemaName + ";").execute();
LOG.info("Schema {} created on uri {}", schemaName, uri);
} catch (final SQLException e) {
e.printStackTrace();
}
}
@Override
public void after() {
dropSchema();
}
private void dropSchema() {
try (Connection connection = DriverManager.getConnection(uri, username, password)) {
connection.prepareStatement("DROP SCHEMA " + schemaName + ";").execute();
LOG.info("Schema {} dropped on uri {}", schemaName, uri);
} catch (final SQLException e) {
e.printStackTrace();
}
}
@Override
public String getDriverClassName() {
return "org.mariadb.jdbc.Driver";
}
@Override
public String getUri() {
return uri;
}
}

View File

@@ -1,118 +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.jpa;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.UUID;
import org.h2.tools.Server;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
*
*/
public class LocalH2TestDatabase implements Testdatabase {
private final static Logger LOG = LoggerFactory.getLogger(LocalH2TestDatabase.class);
private final int port;
private Server h2server;
private boolean dbStarted;
private String uri;
public LocalH2TestDatabase(final int port) {
super();
this.port = port;
createUri();
initSystemProperties();
}
private final void initSystemProperties() {
System.setProperty("spring.datasource.driverClassName", getDriverClassName());
System.setProperty("spring.datasource.username", "");
System.setProperty("spring.datasource.password", "");
System.setProperty("hawkbit.server.database", "H2");
}
private void dropAllObjects() {
try (Connection connection = DriverManager.getConnection(uri)) {
connection.prepareCall("DROP ALL OBJECTS;").execute();
} catch (final SQLException e) {
e.printStackTrace();
}
}
@Override
public void before() {
try {
startDatabase();
} catch (ClassNotFoundException | SQLException | IOException e) {
e.printStackTrace();
}
}
@Override
public void after() {
try {
stopDatabase();
} catch (ClassNotFoundException | SQLException | IOException e) {
e.printStackTrace();
}
}
private void startDatabase() throws SQLException, ClassNotFoundException, IOException {
if (dbStarted) {
return;
}
// Start H2 database for OpenFire
h2server = Server
.createTcpServer(
new String[] { "-tcpPort", String.valueOf(port), "-tcpAllowOthers", "-tcpShutdownForce" })
.start();
dbStarted = true;
LOG.info("H2 Database started on port {} and uri {}", port, uri);
dropAllObjects();
}
private final void createUri() {
this.uri = "jdbc:h2:tcp://localhost:" + port + "/mem:SP" + UUID.randomUUID().toString() + ";MVCC=TRUE;"
+ "DB_CLOSE_DELAY=-1";
System.setProperty("spring.datasource.url", uri);
}
private void stopDatabase() throws SQLException, ClassNotFoundException, IOException {
if (!dbStarted) {
return;
}
h2server.stop();
h2server = null;
dbStarted = false;
try {
Thread.sleep(1000);
} catch (final InterruptedException e) {
}
}
@Override
public String getDriverClassName() {
return "org.h2.Driver";
}
@Override
public String getUri() {
return uri;
}
}

View File

@@ -1,25 +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.jpa;
/**
*
*
*/
public interface Testdatabase {
void before();
void after();
public String getUri();
String getDriverClassName();
}