[DS-3209] First hack at a unit test suite for Handle IdentiferProviders.

This commit is contained in:
Mark H. Wood
2016-08-08 11:58:02 -04:00
parent f6bfc491d3
commit 83498162c8
3 changed files with 371 additions and 6 deletions

View File

@@ -0,0 +1,348 @@
/**
* 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.identifier;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;
import org.dspace.AbstractDSpaceTest;
import org.dspace.kernel.ServiceManager;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import static org.junit.Assert.*;
/**
* Test the HandleIdentifierProvider.
*
* @author mwood
*/
public class HandleIdentifierProviderTest
extends AbstractDSpaceTest
{
/** A name for our testing bean definition. */
private static final String BEAN_NAME = "test-HandleIdentifierProvider";
/** DSpace service manager. */
private static ServiceManager serviceManager;
/** Spring application context. */
private static GenericApplicationContext applicationContext;
public HandleIdentifierProviderTest()
{
}
@BeforeClass
public static void setUpClass()
{
serviceManager = kernelImpl.getServiceManager();
// We need to define a Bean for the System Under Test so that we can
// get Spring to do autowiring on it. There are several conflicting
// definitions of Handle provider beans, so to test them all we'd have
// to reconfigure between tests. Instead, create a new definition just
// for testing so that we know we have the one that we want.
applicationContext
= (GenericApplicationContext) serviceManager.getServiceByName(
ApplicationContext.class.getName(),
ApplicationContext.class);
GenericBeanDefinition bd = new GenericBeanDefinition();
bd.setBeanClass(HandleIdentifierProvider.class);
applicationContext.registerBeanDefinition(BEAN_NAME, bd); // Now our SUT is a Bean.
}
@AfterClass
public static void tearDownClass()
{
// Clean up dynamic bean definitions from setup.
applicationContext.removeBeanDefinition(BEAN_NAME);
}
@Before
public void setUp()
{
}
@After
public void tearDown()
{
}
/**
* Test of supports(Class) method, of class HandleIdentifierProvider.
*/
/*
@Test
public void testSupports_Class()
{
System.out.println("supports(Class)");
Class<? extends Identifier> identifier = null;
HandleIdentifierProvider instance = new HandleIdentifierProvider();
boolean expResult = false;
boolean result = instance.supports(identifier);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of supports(String) method, of class HandleIdentifierProvider.
* Read a property list of identifiers and ask an instance of the provider
* whether it supports them. Properties are "identifier = true/false",
* where the value indicates whether the identifier should be supported.
* The list is a .properties on the class path.
*/
@Test
public void testSupports_String()
{
System.out.println("supports(String)");
// We have to get Spring to instantiate the provider as a Bean, because
// the bean class has autowired fields.
HandleIdentifierProvider instance // Make one to test.
= applicationContext.getBean(BEAN_NAME, HandleIdentifierProvider.class);
// Load the test cases
Properties forms = new Properties();
try {
forms.load(this.getClass().getResourceAsStream("handle-forms.properties"));
} catch (IOException e) {
System.err.format("Could not load handle-forms.properties: %s%n", e.getMessage());
return;
}
// Test each case
for (Map.Entry<Object, Object> entry : forms.entrySet())
{
String identifier = (String)entry.getKey();
boolean expResult = Boolean.parseBoolean((String)entry.getValue());
boolean result = instance.supports(identifier);
String message = expResult ?
"This provider should support " + identifier :
"This provider should not support " + identifier;
assertEquals(message, expResult, result);
}
}
/**
* Test of register method, of class HandleIdentifierProvider.
*/
/*
@Test
public void testRegister_Context_DSpaceObject()
{
System.out.println("register");
Context context = null;
DSpaceObject dso = null;
HandleIdentifierProvider instance = new HandleIdentifierProvider();
String expResult = "";
String result = instance.register(context, dso);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of register method, of class HandleIdentifierProvider.
*/
/*
@Test
public void testRegister_3args()
{
System.out.println("register");
Context context = null;
DSpaceObject dso = null;
String identifier = "";
HandleIdentifierProvider instance = new HandleIdentifierProvider();
instance.register(context, dso, identifier);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of reserve method, of class HandleIdentifierProvider.
*/
/*
@Test
public void testReserve()
{
System.out.println("reserve");
Context context = null;
DSpaceObject dso = null;
String identifier = "";
HandleIdentifierProvider instance = new HandleIdentifierProvider();
instance.reserve(context, dso, identifier);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of mint method, of class HandleIdentifierProvider.
*/
/*
@Test
public void testMint()
{
System.out.println("mint");
Context context = null;
DSpaceObject dso = null;
HandleIdentifierProvider instance = new HandleIdentifierProvider();
String expResult = "";
String result = instance.mint(context, dso);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of resolve method, of class HandleIdentifierProvider.
*/
/*
@Test
public void testResolve()
{
System.out.println("resolve");
Context context = null;
String identifier = "";
String[] attributes = null;
HandleIdentifierProvider instance = new HandleIdentifierProvider();
DSpaceObject expResult = null;
DSpaceObject result = instance.resolve(context, identifier, attributes);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of lookup method, of class HandleIdentifierProvider.
* @throws java.lang.Exception passed through.
*/
/*
@Test
public void testLookup()
throws Exception
{
System.out.println("lookup");
Context context = null;
DSpaceObject dso = null;
HandleIdentifierProvider instance = new HandleIdentifierProvider();
String expResult = "";
String result = instance.lookup(context, dso);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of delete method, of class HandleIdentifierProvider.
* @throws java.lang.Exception passed through.
*/
/*
@Test
public void testDelete_3args()
throws Exception
{
System.out.println("delete");
Context context = null;
DSpaceObject dso = null;
String identifier = "";
HandleIdentifierProvider instance = new HandleIdentifierProvider();
instance.delete(context, dso, identifier);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of delete method, of class HandleIdentifierProvider.
* @throws java.lang.Exception passed through.
*/
/*
@Test
public void testDelete_Context_DSpaceObject()
throws Exception
{
System.out.println("delete");
Context context = null;
DSpaceObject dso = null;
HandleIdentifierProvider instance = new HandleIdentifierProvider();
instance.delete(context, dso);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of retrieveHandleOutOfUrl method, of class HandleIdentifierProvider.
* @throws java.lang.Exception passed through.
*/
/*
@Test
public void testRetrieveHandleOutOfUrl()
throws Exception
{
System.out.println("retrieveHandleOutOfUrl");
String url = "";
String expResult = "";
String result = HandleIdentifierProvider.retrieveHandleOutOfUrl(url);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of getPrefix method, of class HandleIdentifierProvider.
*/
/*
@Test
public void testGetPrefix()
{
System.out.println("getPrefix");
String expResult = "";
String result = HandleIdentifierProvider.getPrefix();
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of populateHandleMetadata method, of class HandleIdentifierProvider.
* @throws java.lang.Exception passed through.
*/
/*
@Test
public void testPopulateHandleMetadata()
throws Exception
{
System.out.println("populateHandleMetadata");
Context context = null;
Item item = null;
String handle = "";
HandleIdentifierProvider instance = new HandleIdentifierProvider();
instance.populateHandleMetadata(context, item, handle);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
}

View File

@@ -0,0 +1,5 @@
# Key is the identifier to be tested; value is "true" or "false": should this
# identifier be supported by the provider under test?
"http://dx.doi.org/10.14279/depositonce-5383" = false
"123456789/1" = true

View File

@@ -25,8 +25,10 @@ import org.springframework.beans.BeansException;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
/**
* This is the Spring implementation of the service manager.
@@ -37,12 +39,12 @@ public final class SpringServiceManager implements ServiceManagerSystem {
private static Logger log = LoggerFactory.getLogger(SpringServiceManager.class);
private ClassPathXmlApplicationContext applicationContext;
private GenericApplicationContext applicationContext;
/**
* @return the parent core Spring {@link ApplicationContext}
*/
public ClassPathXmlApplicationContext getApplicationContext() {
public GenericApplicationContext getApplicationContext() {
return applicationContext;
}
/**
@@ -203,15 +205,24 @@ public final class SpringServiceManager implements ServiceManagerSystem {
}
}
}
String[] allPaths = pathList.toArray(new String[pathList.size()]);
applicationContext = new ClassPathXmlApplicationContext(allPaths, false);
// Make sure that the spring files from the config directoy can override the spring files from our jars
applicationContext = new GenericApplicationContext();
// Make sure that the Spring files from the configuration directory can
// override the Spring files from our JARs.
applicationContext.setAllowBeanDefinitionOverriding(true);
applicationContext.setAllowCircularReferences(true);
//applicationContext.registerShutdownHook(); // this interferes with the kernel shutdown hook
// add the config interceptors (partially done in the xml)
applicationContext.addBeanFactoryPostProcessor( new DSpaceBeanFactoryPostProcessor(parent, configurationService, testMode) );
// Load all of those XML bean definition files.
BeanDefinitionReader bdr = new XmlBeanDefinitionReader(applicationContext);
String[] allPaths = pathList.toArray(new String[pathList.size()]);
bdr.loadBeanDefinitions(allPaths);
// Resolve bean relationships, instantiate singletons, etc.
applicationContext.refresh();
if (developmentMode) {
log.warn("Spring Service Manager is running in developmentMode, services will be loaded on demand only");
// TODO find a way to set this sucker to super duper lazy mode? it is currently not actually doing it
@@ -219,6 +230,7 @@ public final class SpringServiceManager implements ServiceManagerSystem {
applicationContext.getBeanFactory().preInstantiateSingletons();
applicationContext.getBeanFactory().freezeConfiguration();
}
long totalTime = System.currentTimeMillis() - startTime;
log.info("Spring Service Manager started up in "+totalTime+" ms with "+applicationContext.getBeanDefinitionCount()+" services...");
}