Skip to content
Snippets Groups Projects
Commit 87c483f3 authored by Goik Martin's avatar Goik Martin
Browse files

Centralized database init

parent 5c24faad
No related branches found
No related tags found
No related merge requests found
......@@ -8,7 +8,7 @@ import java.util.ResourceBundle;
*
*/
public class DbProps {
private static final String BUNDLE_NAME = "de.hdm_stuttgart.mi.database";
private static final String BUNDLE_NAME = "de.hdm_stuttgart.mi.persistence.test.database";
private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle
.getBundle(BUNDLE_NAME);
......
package de.hdm_stuttgart.mi.persistence.test;
import java.sql.SQLException;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import de.hdm_stuttgart.mi.persistence.PersistenceHandler;
......@@ -14,6 +17,23 @@ public class ConnectionTest {
final ViewMockup viewMockup = new ViewMockup();
final PersistenceHandler ph = new PersistenceHandler(viewMockup);
/**
* Initialize database
*
*/
@BeforeClass
static public void init() {
try {
InitDatabase.initDatabase();
} catch (SQLException e) {
Assert.fail("Unable to initialize database:" + e.getLocalizedMessage());
return;
}
}
/**
* Opening and closing a connection
......
package de.hdm_stuttgart.mi.persistence.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.Assert;
import org.junit.Test;
import de.hdm_stuttgart.mi.persistence.DbProps;
import de.hdm_stuttgart.mi.persistence.PersistenceHandler;
/**
......@@ -25,7 +21,7 @@ public class DataInsertTest {
@Test ()
public void addDatasets() {
try {
initDatabase();
InitDatabase.initDatabase();
} catch (SQLException e) {
Assert.fail("Unable to initialize database:" + e.getLocalizedMessage());
return;
......@@ -41,7 +37,7 @@ public class DataInsertTest {
viewMockup.error);
try {
cleanDatabase();
InitDatabase.initDatabase();
} catch (SQLException e) {
System.err.println("Unable to clean database");
}
......@@ -50,44 +46,5 @@ public class DataInsertTest {
}
private void initDatabase() throws SQLException {
try (
final Connection conn = DriverManager.getConnection(
DbProps.getString("PersistenceHandler.jdbcUrl"),
DbProps.getString("PersistenceHandler.username"),
DbProps.getString("PersistenceHandler.password"));
final Statement stmt = conn.createStatement();
) {
stmt.executeUpdate("DROP TABLE IF EXISTS Person");
stmt.executeUpdate("CREATE TABLE Person (\n" +
"name CHAR(20) NOT NULL," +
"email CHAR(20) NOT NULL UNIQUE," +
"login CHAR(20) PRIMARY KEY," +
"password TEXT" +
");");
stmt.executeUpdate(
"INSERT INTO Person VALUES (\n" +
"'Master admin',\n" +
"'admin@company.com',\n" +
"'admin',\n" +
"'B5DllJLkC06vW7KwoTq/vLSWneEQirD3UQGVXNDEwIg=$ZzlQz/1l0yWV1vQokhDTI9j2vbFt5zFo/kXARI2wNlc='\n)"
);
}
}
private void cleanDatabase() throws SQLException {
try (
final Connection conn = DriverManager.getConnection(
DbProps.getString("PersistenceHandler.jdbcUrl"),
DbProps.getString("PersistenceHandler.username"),
DbProps.getString("PersistenceHandler.password"));
final Statement stmt = conn.createStatement();
) {
stmt.executeUpdate("DELETE FROM Person");
}
}
}
package de.hdm_stuttgart.mi.persistence.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import de.hdm_stuttgart.mi.persistence.DbProps;
/**
* Create database schema or erase entry records
*/
public class InitDatabase {
/**
* Erase if required and re-create application related tables.
*
* @throws SQLException
*/
static public void initDatabase() throws SQLException {
try (
final Connection conn = DriverManager.getConnection(
DbProps.getString("PersistenceHandler.jdbcUrl"),
DbProps.getString("PersistenceHandler.username"),
DbProps.getString("PersistenceHandler.password"));
final Statement stmt = conn.createStatement();
) {
stmt.executeUpdate("DROP TABLE IF EXISTS Person");
stmt.executeUpdate("CREATE TABLE Person (\n" +
"name CHAR(20) NOT NULL," +
"email CHAR(20) NOT NULL UNIQUE," +
"login CHAR(20) PRIMARY KEY," +
"password TEXT" +
");");
stmt.executeUpdate(
"INSERT INTO Person VALUES (\n" +
"'Master admin',\n" +
"'admin@company.com',\n" +
"'admin',\n" +
"'B5DllJLkC06vW7KwoTq/vLSWneEQirD3UQGVXNDEwIg=$ZzlQz/1l0yWV1vQokhDTI9j2vbFt5zFo/kXARI2wNlc='\n)"
);
}
}
/**
* Erase existing records.
*
* @throws SQLException
*/
static public void cleanDatabase() throws SQLException {
try (
final Connection conn = DriverManager.getConnection(
DbProps.getString("PersistenceHandler.jdbcUrl"),
DbProps.getString("PersistenceHandler.username"),
DbProps.getString("PersistenceHandler.password"));
final Statement stmt = conn.createStatement();
) {
stmt.executeUpdate("DELETE FROM Person");
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment