diff --git a/dspace-api/pom.xml b/dspace-api/pom.xml index 8a8ddebc48..5ebdaa41d5 100644 --- a/dspace-api/pom.xml +++ b/dspace-api/pom.xml @@ -111,6 +111,7 @@ ${project.build.directory}/testing/dspace + ${basedir}/src/test/data/dspaceFolder ${project.build.directory}/testing/dspace.cfg.woven ${project.build.directory}/testing/dspace/etc/h2/database_schema.sql true diff --git a/dspace-api/src/main/java/org/dspace/content/authority/DSpaceControlledVocabulary.java b/dspace-api/src/main/java/org/dspace/content/authority/DSpaceControlledVocabulary.java index 37562cbc1c..29bfd64c93 100644 --- a/dspace-api/src/main/java/org/dspace/content/authority/DSpaceControlledVocabulary.java +++ b/dspace-api/src/main/java/org/dspace/content/authority/DSpaceControlledVocabulary.java @@ -55,7 +55,7 @@ import org.dspace.core.SelfNamedPlugin; public class DSpaceControlledVocabulary extends SelfNamedPlugin implements ChoiceAuthority { - private static Logger log = Logger.getLogger(DSpaceControlledVocabulary.class); + private static Logger log = Logger.getLogger(DSpaceControlledVocabulary.class); private static String xpathTemplate = "//node[contains(translate(@label,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz'),'%s')]"; private static String idTemplate = "//node[@id = '%s']"; private static String pluginNames[] = null; @@ -85,22 +85,24 @@ public class DSpaceControlledVocabulary extends SelfNamedPlugin implements Choic { if (pluginNames == null) { - class xmlFilter implements java.io.FilenameFilter + class xmlFilter implements java.io.FilenameFilter { - public boolean accept(File dir, String name) + @Override + public boolean accept(File dir, String name) { - return name.endsWith(".xml"); - } - } - String vocabulariesPath = ConfigurationManager.getProperty("dspace.dir") + "/config/controlled-vocabularies/"; - String[] xmlFiles = (new File(vocabulariesPath)).list(new xmlFilter()); - List names = new ArrayList(); - for (String filename : xmlFiles) + return name.endsWith(".xml"); + } + } + String vocabulariesPath = ConfigurationManager.getProperty("dspace.dir") + + "/config/controlled-vocabularies/"; + String[] xmlFiles = (new File(vocabulariesPath)).list(new xmlFilter()); + List names = new ArrayList(); + for (String filename : xmlFiles) { - names.add((new File(filename)).getName().replace(".xml","")); - } - pluginNames = names.toArray(new String[names.size()]); - log.info("Got plugin names = "+Arrays.deepToString(pluginNames)); + names.add((new File(filename)).getName().replace(".xml", "")); + } + pluginNames = names.toArray(new String[names.size()]); + log.info("Got plugin names = " + Arrays.deepToString(pluginNames)); } } @@ -154,6 +156,7 @@ public class DSpaceControlledVocabulary extends SelfNamedPlugin implements Choic } } + @Override public Choices getMatches(String field, String text, int collection, int start, int limit, String locale) { init(); @@ -162,47 +165,54 @@ public class DSpaceControlledVocabulary extends SelfNamedPlugin implements Choic XPath xpath = XPathFactory.newInstance().newXPath(); Choice[] choices; try { - NodeList results = (NodeList)xpath.evaluate(xpathExpression, vocabulary, XPathConstants.NODESET); - String[] authorities = new String[results.getLength()]; - String[] values = new String[results.getLength()]; - String[] labels = new String[results.getLength()]; - for (int i=0; i 0) + values[i] = node.getAttributes().getNamedItem("label").getNodeValue(); + } + Node idAttr = node.getAttributes().getNamedItem("id"); + if (null != idAttr) // 'id' is optional + authorities[i] = idAttr.getNodeValue(); + } + int resultCount = labels.length - start; + if ((limit > 0) && (resultCount > limit)) // limit = 0 means no limit + resultCount = limit; + choices = new Choice[resultCount]; + if (resultCount > 0) { - for (int i=0; i + + + + + + diff --git a/dspace-api/src/test/java/org/dspace/MockConfigurationManager.java b/dspace-api/src/test/java/org/dspace/MockConfigurationManager.java new file mode 100644 index 0000000000..acda5e7f08 --- /dev/null +++ b/dspace-api/src/test/java/org/dspace/MockConfigurationManager.java @@ -0,0 +1,55 @@ +/** + * 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.util.Properties; +import mockit.Mock; +import mockit.MockClass; +import org.dspace.core.ConfigurationManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Dummy ConfigurationManager with a setter instead of external storage for + * values. Call {@link setProperty} to create configuration. + * + *

Please note that this implementation is incomplete!

+ * + * @author mwood + */ +@MockClass(realClass=ConfigurationManager.class) +public class MockConfigurationManager { + private static final Properties properties = new Properties(); + private static final Logger log = LoggerFactory.getLogger(MockConfigurationManager.class); + + /** + * Set a value in the configuration map. + * + * @param key name of the configuration datum. + * @param value value to be assigned to the name. + */ + public static void setProperty(String key, String value) + { + log.info("setProperty({}, {});", key, value); + properties.setProperty(key, value); + } + + /** + * Fetch a value from the map. + * + * @param key name of the configuration property desired. + * @return value bound to that name, or null if not set. + */ + @Mock + public static String getProperty(String key) + { + log.info("getProperty({});", key); + return properties.getProperty(key); + } +} diff --git a/dspace-api/src/test/java/org/dspace/content/authority/DSpaceControlledVocabularyTest.java b/dspace-api/src/test/java/org/dspace/content/authority/DSpaceControlledVocabularyTest.java new file mode 100644 index 0000000000..3de7182669 --- /dev/null +++ b/dspace-api/src/test/java/org/dspace/content/authority/DSpaceControlledVocabularyTest.java @@ -0,0 +1,140 @@ +/** + * 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.content.authority; + +import java.io.IOException; +import mockit.UsingMocksAndStubs; +import org.dspace.MockConfigurationManager; +import org.dspace.core.PluginManager; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import org.junit.*; + +/** + * Unit tests for DSpaceControlledVocabulary. + * + * @author mwood + */ +@UsingMocksAndStubs(value=MockConfigurationManager.class) +public class DSpaceControlledVocabularyTest +{ + public DSpaceControlledVocabularyTest() + { + } + + @BeforeClass + public static void setUpClass() + throws Exception + { + } + + @AfterClass + public static void tearDownClass() + throws Exception + { + } + + @Before + public void setUp() + { + } + + @After + public void tearDown() + { + } + + /** + * Test of getPluginNames method, of class DSpaceControlledVocabulary. + */ +/* + @Test + public void testGetPluginNames() + { + System.out.println("getPluginNames"); + String[] expResult = null; + String[] result = DSpaceControlledVocabulary.getPluginNames(); + 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 getMatches method, of class DSpaceControlledVocabulary. + */ + @Test + public void testGetMatches() throws IOException, ClassNotFoundException + { + System.out.println("getMatches"); + + // Set up the PluginManager + final String PLUGIN_INTERFACE = "org.dspace.content.authority.ChoiceAuthority"; + final String PLUGIN_NAME = "org.dspace.content.authority.DSpaceControlledVocabulary"; + + MockConfigurationManager.setProperty("dspace.dir", + System.getProperty("dspace.dir.static")); + MockConfigurationManager.setProperty( + "plugin.selfnamed." + PLUGIN_INTERFACE, PLUGIN_NAME); + + // Ensure that 'id' attribute is optional + String field = null; // not used + String text = "north 40"; + int collection = 0; + int start = 0; + int limit = 0; + String locale = null; + DSpaceControlledVocabulary instance = (DSpaceControlledVocabulary) + PluginManager.getNamedPlugin(Class.forName(PLUGIN_INTERFACE), "farm"); + assertNotNull(instance); + Choices result = instance.getMatches(field, text, collection, start, + limit, locale); + assertEquals("the farm::north 40", result.values[0].value); + } + + /** + * Test of getBestMatch method, of class DSpaceControlledVocabulary. + */ +/* + @Test + public void testGetBestMatch() + { + System.out.println("getBestMatch"); + String field = ""; + String text = ""; + int collection = 0; + String locale = ""; + DSpaceControlledVocabulary instance = new DSpaceControlledVocabulary(); + Choices expResult = null; + Choices result = instance.getBestMatch(field, text, collection, locale); + 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 getLabel method, of class DSpaceControlledVocabulary. + */ +/* + @Test + public void testGetLabel() + { + System.out.println("getLabel"); + String field = ""; + String key = ""; + String locale = ""; + DSpaceControlledVocabulary instance = new DSpaceControlledVocabulary(); + String expResult = ""; + String result = instance.getLabel(field, key, locale); + assertEquals(expResult, result); + // TODO review the generated test code and remove the default call to fail. + fail("The test case is a prototype."); + } +*/ +}