Make LoginService tests use single database and drop and recreate tables

This commit is contained in:
Jan Bartel 2014-07-18 16:52:38 +10:00
parent e9a77b6ac7
commit d580c3279b
5 changed files with 72 additions and 40 deletions

View File

@ -53,28 +53,41 @@ import org.junit.Test;
*/
public class DataSourceLoginServiceTest
{
public static final String _content = "This is some protected content";
private static File _docRoot;
private static File _dbRoot;
private static HttpClient _client;
private static String __realm = "DSRealm";
private static URI _baseUri;
private static final int __cacheInterval = 200;
private static DatabaseLoginServiceTestServer _testServer;
@BeforeClass
public static void setUp() throws Exception
{
_docRoot = MavenTestingUtils.getTargetTestingDir(DataSourceLoginServiceTest.class.getSimpleName());
FS.ensureEmpty(_docRoot);
_docRoot = MavenTestingUtils.getTargetTestingDir("loginservice-test");
FS.ensureDirExists(_docRoot);
File content = new File(_docRoot,"input.txt");
FileOutputStream out = new FileOutputStream(content);
out.write(_content.getBytes("utf-8"));
out.close();
File scriptFile = MavenTestingUtils.getTestResourceFile("createdb.sql");
_dbRoot = DatabaseLoginServiceTestServer.createDB(scriptFile,"dstest");
//clear previous runs
File scriptFile = MavenTestingUtils.getTestResourceFile("droptables.sql");
int result = DatabaseLoginServiceTestServer.runscript(scriptFile);
//ignore result as derby spits errors for dropping tables that dont exist
//create afresh
scriptFile = MavenTestingUtils.getTestResourceFile("createdb.sql");
result = DatabaseLoginServiceTestServer.runscript(scriptFile);
assertThat("runScript result",result, is(0));
_testServer = new DatabaseLoginServiceTestServer();
_testServer.setResourceBase(_docRoot.getAbsolutePath());
@ -115,7 +128,8 @@ public class DataSourceLoginServiceTest
//create a datasource
EmbeddedDataSource ds = new EmbeddedDataSource();
ds.setDatabaseName(_dbRoot.getAbsolutePath());
File db = new File (DatabaseLoginServiceTestServer.getDbRoot(), "loginservice");
ds.setDatabaseName(db.getAbsolutePath());
org.eclipse.jetty.plus.jndi.Resource binding = new org.eclipse.jetty.plus.jndi.Resource(null, "dstest",
ds);
assertThat("Created binding for dstest", binding, notNullValue());
@ -157,9 +171,10 @@ public class DataSourceLoginServiceTest
protected void changePassword (String user, String newpwd) throws Exception
{
Loader.loadClass(this.getClass(), "org.apache.derby.jdbc.EmbeddedDriver").newInstance();
try (Connection connection = DriverManager.getConnection("jdbc:derby:dstest", "", ""))
try (Connection connection = DriverManager.getConnection(DatabaseLoginServiceTestServer.__dbURL, "", "");
Statement stmt = connection.createStatement())
{
Statement stmt = connection.createStatement();
connection.setAutoCommit(true);
stmt.executeUpdate("update users set pwd='"+newpwd+"' where username='"+user+"'");
}

View File

@ -19,9 +19,6 @@
package org.eclipse.jetty;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
@ -65,6 +62,7 @@ import org.eclipse.jetty.util.security.Constraint;
*/
public class DatabaseLoginServiceTestServer
{
protected static String __dbURL = "jdbc:derby:loginservice;create=true";
protected Server _server;
protected static String _protocol;
protected static URI _baseUri;
@ -74,30 +72,30 @@ public class DatabaseLoginServiceTestServer
private static File commonDerbySystemHome;
protected static String _requestContent;
protected static File createDB(File scriptFile, String dbName) throws Exception
protected static File _dbRoot;
static
{
if(commonDerbySystemHome == null)
{
commonDerbySystemHome = MavenTestingUtils.getTargetTestingDir("derby-system-common");
FS.ensureEmpty(commonDerbySystemHome);
System.setProperty("derby.system.home", commonDerbySystemHome.getAbsolutePath());
_dbRoot = new File(MavenTestingUtils.getTargetTestingDir("loginservice-test"), "derby");
FS.ensureDirExists(_dbRoot);
System.setProperty("derby.system.home", _dbRoot.getAbsolutePath());
}
String dbUrl = "jdbc:derby:directory:" + dbName + ";create=true";
public static File getDbRoot ()
{
return _dbRoot;
}
public static int runscript (File scriptFile) throws Exception
{
//System.err.println("Running script:"+scriptFile.getAbsolutePath());
try (FileInputStream fileStream = new FileInputStream(scriptFile))
{
Loader.loadClass(fileStream.getClass(), "org.apache.derby.jdbc.EmbeddedDriver").newInstance();
Connection connection = DriverManager.getConnection(dbUrl, "", "");
OutputStream out = new ByteArrayOutputStream();
int result = ij.runScript(connection, fileStream, "UTF-8", out, "UTF-8");
assertThat("runScript result",result, is(0));
File dbRoot = new File(commonDerbySystemHome, dbName);
assertThat("exists: " + dbRoot, dbRoot.exists(), is(true));
return dbRoot;
Connection connection = DriverManager.getConnection(__dbURL, "", "");
ByteArrayOutputStream out = new ByteArrayOutputStream();
return ij.runScript(connection, fileStream, "UTF-8", out, "UTF-8");
}
}

View File

@ -18,8 +18,10 @@
package org.eclipse.jetty;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.io.File;
import java.io.FileInputStream;
@ -49,6 +51,8 @@ import org.junit.Test;
public class JdbcLoginServiceTest
{
private static String _content =
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. In quis felis nunc. "+
"Quisque suscipit mauris et ante auctor ornare rhoncus lacus aliquet. Pellentesque "+
@ -69,21 +73,30 @@ public class JdbcLoginServiceTest
private static URI _baseUri;
private static DatabaseLoginServiceTestServer _testServer;
@BeforeClass
public static void setUp() throws Exception
{
_docRoot = MavenTestingUtils.getTargetTestingDir(JdbcLoginServiceTest.class.getSimpleName());
FS.ensureEmpty(_docRoot);
_docRoot = MavenTestingUtils.getTargetTestingDir("loginservice-test");
FS.ensureDirExists(_docRoot);
File content = new File(_docRoot,"input.txt");
try (FileOutputStream out = new FileOutputStream(content))
{
out.write(_content.getBytes("utf-8"));
}
File scriptFile = MavenTestingUtils.getTestResourceFile("createdb.sql");
DatabaseLoginServiceTestServer.createDB(scriptFile,"jdbcrealm");
//drop any tables that might have existed
File scriptFile = MavenTestingUtils.getTestResourceFile("droptables.sql");
int result = DatabaseLoginServiceTestServer.runscript(scriptFile);
//ignore result, if the tables dont already exist, derby spits out an error
//create the tables afresh
scriptFile = MavenTestingUtils.getTestResourceFile("createdb.sql");
result = DatabaseLoginServiceTestServer.runscript(scriptFile);
assertThat("runScript result",result, is(0));
File jdbcRealmFile = MavenTestingUtils.getTestResourceFile("jdbcrealm.properties");

View File

@ -0,0 +1,6 @@
DROP TABLE roles;
DROP TABLE users;
DROP TABLE user_roles;

View File

@ -1,5 +1,5 @@
jdbcdriver = org.apache.derby.jdbc.EmbeddedDriver
url = jdbc:derby:jdbcrealm
url = jdbc:derby:loginservice
username =
password =
usertable = users