Create new AbstractDSpaceTest to initialize Kernel separate from Database. Some tests ONLY need the Kernel for configs, and not the full DB.

This commit is contained in:
Tim Donohue
2015-10-15 13:24:53 -05:00
parent 44a6baa0e8
commit ed25cc0afe
11 changed files with 152 additions and 150 deletions

View File

@@ -0,0 +1,107 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace;
import java.io.IOException;
import java.net.URL;
import java.sql.SQLException;
import java.util.Properties;
import java.util.TimeZone;
import org.apache.log4j.Logger;
import org.dspace.app.util.MockUtil;
import org.dspace.servicemanager.DSpaceKernelImpl;
import org.dspace.servicemanager.DSpaceKernelInit;
import org.junit.AfterClass;
import static org.junit.Assert.fail;
import org.junit.BeforeClass;
/**
* DSpace Unit Tests need to initialize the DSpace Kernel / Service Mgr
* in order to have access to configurations, etc. This Abstract class only
* initializes the Kernel (without full in-memory DB initialization).
* <P>
* Tests which just need the Kernel (or configs) can extend this class.
* <P>
* Tests which also need an in-memory DB should extend AbstractUnitTest or AbstractIntegrationTest
*
* @see AbstractUnitTest
* @see AbstractIntegrationTest
* @author Tim
*/
public class AbstractDSpaceTest
{
/** log4j category */
private static final Logger log = Logger.getLogger(AbstractDSpaceTest.class);
/**
* Test properties. These configure our general test environment
*/
protected static Properties testProps;
/**
* DSpace Kernel. Must be started to initialize ConfigurationService and
* any other services.
*/
protected static DSpaceKernelImpl kernelImpl;
/**
* This method will be run before the first test as per @BeforeClass. It will
* initialize shared resources required for all tests of this class.
*
* This method loads our test properties to initialize our test environment,
* and then starts the DSpace Kernel (which allows access to services).
*/
@BeforeClass
public static void initKernel()
{
try
{
//set a standard time zone for the tests
TimeZone.setDefault(TimeZone.getTimeZone("Europe/Dublin"));
//load the properties of the tests
testProps = new Properties();
URL properties = AbstractUnitTest.class.getClassLoader()
.getResource("test-config.properties");
testProps.load(properties.openStream());
// Initialise the service manager kernel
kernelImpl = DSpaceKernelInit.getKernel(null);
if (!kernelImpl.isRunning())
{
kernelImpl.start(System.getProperty("dspace.dir")); // init the kernel
}
// Initialize mock Util class (allows Util.getSourceVersion() to work in Unit tests)
new MockUtil();
}
catch (IOException ex)
{
log.error("Error initializing tests", ex);
fail("Error initializing tests: " + ex.getMessage());
}
}
/**
* This method will be run after all tests finish as per @AfterClass. It
* will clean resources initialized by the @BeforeClass methods.
*/
@AfterClass
public static void destroyKernel() throws SQLException {
//we clear the properties
testProps.clear();
testProps = null;
//Also clear out the kernel & nullify (so JUnit will clean it up)
if (kernelImpl != null) {
kernelImpl.destroy();
}
kernelImpl = null;
}
}

View File

@@ -9,14 +9,9 @@ package org.dspace;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import java.io.IOException;
import java.net.URL;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Properties;
import java.util.TimeZone;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.dspace.app.util.MockUtil;
import org.dspace.authorize.AuthorizeException; import org.dspace.authorize.AuthorizeException;
import org.dspace.authorize.factory.AuthorizeServiceFactory; import org.dspace.authorize.factory.AuthorizeServiceFactory;
import org.dspace.authorize.service.AuthorizeService; import org.dspace.authorize.service.AuthorizeService;
@@ -26,36 +21,28 @@ import org.dspace.discovery.MockIndexEventConsumer;
import org.dspace.eperson.EPerson; import org.dspace.eperson.EPerson;
import org.dspace.eperson.factory.EPersonServiceFactory; import org.dspace.eperson.factory.EPersonServiceFactory;
import org.dspace.eperson.service.EPersonService; import org.dspace.eperson.service.EPersonService;
import org.dspace.servicemanager.DSpaceKernelImpl;
import org.dspace.servicemanager.DSpaceKernelInit;
import org.dspace.storage.rdbms.DatabaseUtils; import org.dspace.storage.rdbms.DatabaseUtils;
import org.junit.After; import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
/** /**
* This is the base class for Unit Tests. It contains some generic mocks and * This is the base class for most Unit Tests. It contains some generic mocks and
* utilities that are needed by most of the unit tests developed for DSpace. * utilities that are needed by most of the unit tests developed for DSpace.
* <P>
* NOTE: This base class also performs in-memory (H2) database initialization.
* If your tests don't need that, you may wish to just use AbstractDSpaceTest.
* *
* @see AbstractDSpaceTest
* @author pvillega * @author pvillega
*/ */
public class AbstractUnitTest public class AbstractUnitTest extends AbstractDSpaceTest
{ {
/** log4j category */ /** log4j category */
private static final Logger log = Logger.getLogger(AbstractUnitTest.class); private static final Logger log = Logger.getLogger(AbstractUnitTest.class);
//Below there are static variables shared by all the instances of the class
/**
* Test properties.
*/
protected static Properties testProps;
//Below there are variables used in each test
/** /**
* Context mock object to use in the tests. * Context mock object to use in the tests.
*/ */
@@ -66,72 +53,43 @@ public class AbstractUnitTest
*/ */
protected EPerson eperson; protected EPerson eperson;
protected static DSpaceKernelImpl kernelImpl; /**
* This service is used by the majority of DSO-based Unit tests, which
* is why it is initialized here.
*/
protected AuthorizeService authorizeService = AuthorizeServiceFactory.getInstance().getAuthorizeService(); protected AuthorizeService authorizeService = AuthorizeServiceFactory.getInstance().getAuthorizeService();
/** /**
* This method will be run before the first test as per @BeforeClass. It will * This method will be run before the first test as per @BeforeClass. It will
* initialize shared resources required for all tests of this class. * initialize shared resources required for all tests of this class.
* * <p>
* Due to the way Maven works, unit tests can't be run from a POM package, * NOTE: Per JUnit, "The @BeforeClass methods of superclasses will be run before those the current class."
* which forbids us to run the tests from the Assembly and Configuration * http://junit.org/apidocs/org/junit/BeforeClass.html
* package. On the other hand we need a structure of folders to run the tests, * <p>
* like "solr", "report", etc. This will be provided by a JAR assembly * This method builds on the initialization in AbstractDSpaceTest, and
* built out of files from various modules -- see the dspace-parent POM. * initializes the in-memory database for tests that need it.
*
* This method will load a few properties for derived test classes.
*
* The ConfigurationManager will be initialized to load the test
* "dspace.cfg".
*/ */
@BeforeClass @BeforeClass
public static void initOnce() public static void initDatabase()
{ {
// Clear our old flyway object. Because this DB is in-memory, its
// data is lost when the last connection is closed. So, we need
// to (re)start Flyway from scratch for each Unit Test class.
DatabaseUtils.clearFlywayDBCache();
try try
{ {
//set a standard time zone for the tests // Update/Initialize the database to latest version (via Flyway)
TimeZone.setDefault(TimeZone.getTimeZone("Europe/Dublin")); DatabaseUtils.updateDatabase();
//load the properties of the tests
testProps = new Properties();
URL properties = AbstractUnitTest.class.getClassLoader()
.getResource("test-config.properties");
testProps.load(properties.openStream());
// Initialise the service manager kernel
kernelImpl = DSpaceKernelInit.getKernel(null);
if (!kernelImpl.isRunning())
{
kernelImpl.start(System.getProperty("dspace.dir")); // init the kernel
}
// Clear our old flyway object. Because this DB is in-memory, its
// data is lost when the last connection is closed. So, we need
// to (re)start Flyway from scratch for each Unit Test class.
DatabaseUtils.clearFlywayDBCache();
try
{
// Update/Initialize the database to latest version (via Flyway)
DatabaseUtils.updateDatabase();
}
catch(SQLException se)
{
log.error("Error initializing database", se);
fail("Error initializing database: " + se.getMessage());
}
// Initialize mock indexer (which does nothing, since Solr isn't running)
new MockIndexEventConsumer();
// Initialize mock Util class
new MockUtil();
}
catch (IOException ex)
{
log.error("Error initializing tests", ex);
fail("Error initializing tests: " + ex.getMessage());
} }
catch(SQLException se)
{
log.error("Error initializing database", se);
fail("Error initializing database: " + se.getMessage());
}
// Initialize mock indexer (which does nothing, since Solr isn't running)
new MockIndexEventConsumer();
} }
/** /**
@@ -203,23 +161,6 @@ public class AbstractUnitTest
} }
} }
/**
* This method will be run after all tests finish as per @AfterClass. It will
* clean resources initialized by the @BeforeClass methods.
*
*/
@AfterClass
public static void destroyOnce() throws SQLException {
//we clear the properties
testProps.clear();
testProps = null;
//Also clear out the kernel & nullify (so JUnit will clean it up)
if (kernelImpl!=null)
kernelImpl.destroy();
kernelImpl = null;
}
/** /**
* Utility method to cleanup a created Context object (to save memory). * Utility method to cleanup a created Context object (to save memory).
* This can also be used by individual tests to cleanup context objects they create. * This can also be used by individual tests to cleanup context objects they create.

View File

@@ -11,7 +11,6 @@ import org.apache.commons.lang.time.DateUtils;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import java.util.*; import java.util.*;
import org.dspace.AbstractUnitTest;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@@ -22,7 +21,7 @@ import static org.hamcrest.CoreMatchers.*;
* *
* @author pvillega * @author pvillega
*/ */
public class DCDateTest extends AbstractUnitTest public class DCDateTest
{ {
/** log4j category */ /** log4j category */
private static Logger log = Logger.getLogger(DCDateTest.class); private static Logger log = Logger.getLogger(DCDateTest.class);
@@ -45,9 +44,7 @@ public class DCDateTest extends AbstractUnitTest
* but no execution order is guaranteed * but no execution order is guaranteed
*/ */
@Before @Before
@Override
public void init() { public void init() {
super.init();
TimeZone.setDefault(TimeZone.getTimeZone("GMT-8")); TimeZone.setDefault(TimeZone.getTimeZone("GMT-8"));
} }
@@ -59,11 +56,9 @@ public class DCDateTest extends AbstractUnitTest
* but no execution order is guaranteed * but no execution order is guaranteed
*/ */
@After @After
@Override
public void destroy() { public void destroy() {
dc = null; dc = null;
c = null; c = null;
super.destroy();
} }
/** /**

View File

@@ -8,7 +8,6 @@
package org.dspace.content; package org.dspace.content;
import java.util.Locale; import java.util.Locale;
import org.dspace.AbstractUnitTest;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@@ -20,7 +19,7 @@ import static org.hamcrest.CoreMatchers.*;
* Tests DCLanguageTest class * Tests DCLanguageTest class
* @author pvillega * @author pvillega
*/ */
public class DCLanguageTest extends AbstractUnitTest public class DCLanguageTest
{ {
/** /**
@@ -37,10 +36,8 @@ public class DCLanguageTest extends AbstractUnitTest
* but no execution order is guaranteed * but no execution order is guaranteed
*/ */
@Before @Before
@Override
public void init() public void init()
{ {
super.init();
dc = new DCLanguage(""); dc = new DCLanguage("");
} }
@@ -52,11 +49,9 @@ public class DCLanguageTest extends AbstractUnitTest
* but no execution order is guaranteed * but no execution order is guaranteed
*/ */
@After @After
@Override
public void destroy() public void destroy()
{ {
dc = null; dc = null;
super.destroy();
} }
/** /**

View File

@@ -7,7 +7,6 @@
*/ */
package org.dspace.content; package org.dspace.content;
import org.dspace.AbstractUnitTest;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@@ -20,7 +19,7 @@ import static org.hamcrest.CoreMatchers.*;
* Tests DCPersonName class * Tests DCPersonName class
* @author pvillega * @author pvillega
*/ */
public class DCPersonNameTest extends AbstractUnitTest public class DCPersonNameTest
{ {
/** /**
@@ -37,10 +36,8 @@ public class DCPersonNameTest extends AbstractUnitTest
* but no execution order is guaranteed * but no execution order is guaranteed
*/ */
@Before @Before
@Override
public void init() public void init()
{ {
super.init();
dc = new DCPersonName(""); dc = new DCPersonName("");
} }
@@ -52,11 +49,9 @@ public class DCPersonNameTest extends AbstractUnitTest
* but no execution order is guaranteed * but no execution order is guaranteed
*/ */
@After @After
@Override
public void destroy() public void destroy()
{ {
dc = null; dc = null;
super.destroy();
} }
/** /**

View File

@@ -7,7 +7,6 @@
*/ */
package org.dspace.content; package org.dspace.content;
import org.dspace.AbstractUnitTest;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@@ -19,7 +18,7 @@ import static org.hamcrest.CoreMatchers.*;
* Tests DCSeriesNumber class * Tests DCSeriesNumber class
* @author pvillega * @author pvillega
*/ */
public class DCSeriesNumberTest extends AbstractUnitTest public class DCSeriesNumberTest
{ {
/** /**
@@ -36,10 +35,8 @@ public class DCSeriesNumberTest extends AbstractUnitTest
* but no execution order is guaranteed * but no execution order is guaranteed
*/ */
@Before @Before
@Override
public void init() public void init()
{ {
super.init();
dc = new DCSeriesNumber(); dc = new DCSeriesNumber();
} }
@@ -51,11 +48,9 @@ public class DCSeriesNumberTest extends AbstractUnitTest
* but no execution order is guaranteed * but no execution order is guaranteed
*/ */
@After @After
@Override
public void destroy() public void destroy()
{ {
dc = null; dc = null;
super.destroy();
} }
/** /**

View File

@@ -7,7 +7,6 @@
*/ */
package org.dspace.content; package org.dspace.content;
import org.dspace.AbstractUnitTest;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@@ -18,38 +17,12 @@ import org.junit.*;
* no tests have to be done, the class is created for coberture purposes * no tests have to be done, the class is created for coberture purposes
* @author pvillega * @author pvillega
*/ */
public class NonUniqueMetadataExceptionTest extends AbstractUnitTest public class NonUniqueMetadataExceptionTest
{ {
/** log4j category */ /** log4j category */
private static final Logger log = Logger.getLogger(NonUniqueMetadataExceptionTest.class); private static final Logger log = Logger.getLogger(NonUniqueMetadataExceptionTest.class);
/**
* This method will be run before every test as per @Before. It will
* initialize resources required for the tests.
*
* Other methods can be annotated with @Before here or in subclasses
* but no execution order is guaranteed
*/
@Before
@Override
public void init() {
super.init();
}
/**
* This method will be run after every test as per @After. It will
* clean resources initialized by the @Before methods.
*
* Other methods can be annotated with @After here or in subclasses
* but no execution order is guaranteed
*/
@After
@Override
public void destroy() {
super.destroy();
}
/** /**
* Dummy test to avoid initialization errors * Dummy test to avoid initialization errors
*/ */

View File

@@ -10,7 +10,7 @@ package org.dspace.content.authority;
import java.io.IOException; import java.io.IOException;
import org.dspace.content.Collection; import org.dspace.content.Collection;
import org.dspace.AbstractUnitTest; import org.dspace.AbstractDSpaceTest;
import org.dspace.core.factory.CoreServiceFactory; import org.dspace.core.factory.CoreServiceFactory;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
@@ -21,7 +21,7 @@ import org.junit.*;
* *
* @author mwood * @author mwood
*/ */
public class DSpaceControlledVocabularyTest extends AbstractUnitTest public class DSpaceControlledVocabularyTest extends AbstractDSpaceTest
{ {
public DSpaceControlledVocabularyTest() public DSpaceControlledVocabularyTest()
{ {

View File

@@ -7,7 +7,7 @@
*/ */
package org.dspace.core; package org.dspace.core;
import org.dspace.AbstractUnitTest; import org.dspace.AbstractDSpaceTest;
import org.dspace.core.factory.CoreServiceFactory; import org.dspace.core.factory.CoreServiceFactory;
import org.dspace.core.service.PluginService; import org.dspace.core.service.PluginService;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@@ -26,7 +26,7 @@ import org.junit.Test;
* *
* @author Tim Donohue * @author Tim Donohue
*/ */
public class PluginServiceTest extends AbstractUnitTest public class PluginServiceTest extends AbstractDSpaceTest
{ {
// Get our enabled pluginService // Get our enabled pluginService
private PluginService pluginService = CoreServiceFactory.getInstance().getPluginService(); private PluginService pluginService = CoreServiceFactory.getInstance().getPluginService();

View File

@@ -10,7 +10,7 @@ package org.dspace.eperson;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import org.apache.commons.codec.DecoderException; import org.apache.commons.codec.DecoderException;
import org.dspace.AbstractUnitTest; import org.dspace.AbstractDSpaceTest;
import org.junit.*; import org.junit.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@@ -18,7 +18,7 @@ import static org.junit.Assert.*;
* *
* @author mwood * @author mwood
*/ */
public class PasswordHashTest extends AbstractUnitTest public class PasswordHashTest extends AbstractDSpaceTest
{ {
public PasswordHashTest() public PasswordHashTest()
{ {

View File

@@ -9,6 +9,7 @@ package org.dspace.statistics.util;
import mockit.Mock; import mockit.Mock;
import mockit.MockUp; import mockit.MockUp;
import org.dspace.AbstractDSpaceTest;
import org.dspace.statistics.SolrLoggerServiceImpl; import org.dspace.statistics.SolrLoggerServiceImpl;
import org.junit.Test; import org.junit.Test;
@@ -18,7 +19,7 @@ import static org.junit.Assert.assertTrue;
/** /**
* @author mwood * @author mwood
*/ */
public class SpiderDetectorTest public class SpiderDetectorTest extends AbstractDSpaceTest
{ {
private static final String NOT_A_BOT_ADDRESS = "192.168.0.1"; private static final String NOT_A_BOT_ADDRESS = "192.168.0.1";