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,29 +53,42 @@ import org.junit.Test;
*/ */
public class DataSourceLoginServiceTest public class DataSourceLoginServiceTest
{ {
public static final String _content = "This is some protected content"; public static final String _content = "This is some protected content";
private static File _docRoot; private static File _docRoot;
private static File _dbRoot;
private static HttpClient _client; private static HttpClient _client;
private static String __realm = "DSRealm"; private static String __realm = "DSRealm";
private static URI _baseUri; private static URI _baseUri;
private static final int __cacheInterval = 200; private static final int __cacheInterval = 200;
private static DatabaseLoginServiceTestServer _testServer; private static DatabaseLoginServiceTestServer _testServer;
@BeforeClass @BeforeClass
public static void setUp() throws Exception 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"); File content = new File(_docRoot,"input.txt");
FileOutputStream out = new FileOutputStream(content); FileOutputStream out = new FileOutputStream(content);
out.write(_content.getBytes("utf-8")); out.write(_content.getBytes("utf-8"));
out.close(); 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 = new DatabaseLoginServiceTestServer();
_testServer.setResourceBase(_docRoot.getAbsolutePath()); _testServer.setResourceBase(_docRoot.getAbsolutePath());
_testServer.setLoginService(configureLoginService()); _testServer.setLoginService(configureLoginService());
@ -115,7 +128,8 @@ public class DataSourceLoginServiceTest
//create a datasource //create a datasource
EmbeddedDataSource ds = new EmbeddedDataSource(); 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", org.eclipse.jetty.plus.jndi.Resource binding = new org.eclipse.jetty.plus.jndi.Resource(null, "dstest",
ds); ds);
assertThat("Created binding for dstest", binding, notNullValue()); assertThat("Created binding for dstest", binding, notNullValue());
@ -157,9 +171,10 @@ public class DataSourceLoginServiceTest
protected void changePassword (String user, String newpwd) throws Exception protected void changePassword (String user, String newpwd) throws Exception
{ {
Loader.loadClass(this.getClass(), "org.apache.derby.jdbc.EmbeddedDriver").newInstance(); 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+"'"); stmt.executeUpdate("update users set pwd='"+newpwd+"' where username='"+user+"'");
} }

View File

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

View File

@ -18,8 +18,10 @@
package org.eclipse.jetty; 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.File;
import java.io.FileInputStream; import java.io.FileInputStream;
@ -49,6 +51,8 @@ import org.junit.Test;
public class JdbcLoginServiceTest public class JdbcLoginServiceTest
{ {
private static String _content = private static String _content =
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. In quis felis nunc. "+ "Lorem ipsum dolor sit amet, consectetur adipiscing elit. In quis felis nunc. "+
"Quisque suscipit mauris et ante auctor ornare rhoncus lacus aliquet. Pellentesque "+ "Quisque suscipit mauris et ante auctor ornare rhoncus lacus aliquet. Pellentesque "+
@ -68,22 +72,31 @@ public class JdbcLoginServiceTest
private static String __realm = "JdbcRealm"; private static String __realm = "JdbcRealm";
private static URI _baseUri; private static URI _baseUri;
private static DatabaseLoginServiceTestServer _testServer; private static DatabaseLoginServiceTestServer _testServer;
@BeforeClass @BeforeClass
public static void setUp() throws Exception public static void setUp() throws Exception
{ {
_docRoot = MavenTestingUtils.getTargetTestingDir(JdbcLoginServiceTest.class.getSimpleName()); _docRoot = MavenTestingUtils.getTargetTestingDir("loginservice-test");
FS.ensureEmpty(_docRoot); FS.ensureDirExists(_docRoot);
File content = new File(_docRoot,"input.txt"); File content = new File(_docRoot,"input.txt");
try (FileOutputStream out = new FileOutputStream(content)) try (FileOutputStream out = new FileOutputStream(content))
{ {
out.write(_content.getBytes("utf-8")); out.write(_content.getBytes("utf-8"));
} }
//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
File scriptFile = MavenTestingUtils.getTestResourceFile("createdb.sql"); //create the tables afresh
DatabaseLoginServiceTestServer.createDB(scriptFile,"jdbcrealm"); scriptFile = MavenTestingUtils.getTestResourceFile("createdb.sql");
result = DatabaseLoginServiceTestServer.runscript(scriptFile);
assertThat("runScript result",result, is(0));
File jdbcRealmFile = MavenTestingUtils.getTestResourceFile("jdbcrealm.properties"); 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 jdbcdriver = org.apache.derby.jdbc.EmbeddedDriver
url = jdbc:derby:jdbcrealm url = jdbc:derby:loginservice
username = username =
password = password =
usertable = users usertable = users