From 3f3349046dbe495d2dd8f828ee637d593cd3c92a Mon Sep 17 00:00:00 2001 From: Zeroday BYTE Date: Thu, 17 Jul 2025 02:06:29 +0700 Subject: [PATCH 1/5] Update DSpaceControlledVocabulary.java (cherry picked from commit e94f0a9cb3d43c1ed7af44683202737f598681d5) (cherry picked from commit 73326f277b6014a20d81e91e2e8b80a933c055a1) --- .../authority/DSpaceControlledVocabulary.java | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) 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 4e30559e1c92..ff91a58c6f36 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 @@ -178,13 +178,21 @@ protected String buildString(Node node) { public Choices getMatches(String text, int start, int limit, String locale) { init(locale); log.debug("Getting matches for '" + text + "'"); - String xpathExpression = ""; String[] textHierarchy = text.split(hierarchyDelimiter, -1); + StringBuilder xpathExpressionBuilder = new StringBuilder(); for (int i = 0; i < textHierarchy.length; i++) { - xpathExpression += - String.format(xpathTemplate, textHierarchy[i].replaceAll("'", "'").toLowerCase()); + xpathExpressionBuilder.append(String.format(xpathTemplate, "$var" + i)); } + String xpathExpression = xpathExpressionBuilder.toString(); XPath xpath = XPathFactory.newInstance().newXPath(); + xpath.setXPathVariableResolver(variableName -> { + String varName = variableName.getLocalPart(); + if (varName.startsWith("var")) { + int index = Integer.parseInt(varName.substring(3)); + return textHierarchy[index].toLowerCase(); + } + throw new IllegalArgumentException("Unexpected variable: " + varName); + }); int total = 0; List choices = new ArrayList(); try { @@ -203,13 +211,21 @@ public Choices getMatches(String text, int start, int limit, String locale) { public Choices getBestMatch(String text, String locale) { init(locale); log.debug("Getting best matches for '" + text + "'"); - String xpathExpression = ""; String[] textHierarchy = text.split(hierarchyDelimiter, -1); + StringBuilder xpathExpressionBuilder = new StringBuilder(); for (int i = 0; i < textHierarchy.length; i++) { - xpathExpression += - String.format(valueTemplate, textHierarchy[i].replaceAll("'", "'")); + xpathExpressionBuilder.append(String.format(valueTemplate, "$var" + i)); } + String xpathExpression = xpathExpressionBuilder.toString(); XPath xpath = XPathFactory.newInstance().newXPath(); + xpath.setXPathVariableResolver(variableName -> { + String varName = variableName.getLocalPart(); + if (varName.startsWith("var")) { + int index = Integer.parseInt(varName.substring(3)); + return textHierarchy[index]; + } + throw new IllegalArgumentException("Unexpected variable: " + varName); + }); List choices = new ArrayList(); try { NodeList results = (NodeList) xpath.evaluate(xpathExpression, vocabulary, XPathConstants.NODESET); From 76a616f679034e5cb7ad01b2fd4aa51d7b22654b Mon Sep 17 00:00:00 2001 From: "Mark H. Wood" Date: Thu, 24 Jul 2025 08:32:50 -0400 Subject: [PATCH 2/5] 'No match' should be test failure, not index error. (cherry picked from commit c781ba278093f7c80b8b68c601309fe90a9b85f0) (cherry picked from commit 072d13e26a45836e8c9f8b070762eba012f23a62) --- .../content/authority/DSpaceControlledVocabularyTest.java | 6 ++++++ 1 file changed, 6 insertions(+) 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 index 43bd20cc1553..5eb56428f7db 100644 --- a/dspace-api/src/test/java/org/dspace/content/authority/DSpaceControlledVocabularyTest.java +++ b/dspace-api/src/test/java/org/dspace/content/authority/DSpaceControlledVocabularyTest.java @@ -8,6 +8,7 @@ package org.dspace.content.authority; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; import java.io.IOException; @@ -86,6 +87,7 @@ public void testGetMatches() throws IOException, ClassNotFoundException { CoreServiceFactory.getInstance().getPluginService().getNamedPlugin(Class.forName(PLUGIN_INTERFACE), "farm"); assertNotNull(instance); Choices result = instance.getMatches(text, start, limit, locale); + assertNotEquals("At least one match expected", 0, result.values.length); assertEquals("north 40", result.values[0].value); } @@ -110,6 +112,7 @@ public void testGetMatchesNoLocale() throws ClassNotFoundException { "countries"); assertNotNull(instance); Choices result = instance.getMatches(labelPart, start, limit, null); + assertNotEquals("At least one match expected", 0, result.values.length); assertEquals(idValue, result.values[0].value); assertEquals("Algeria", result.values[0].label); } @@ -132,6 +135,7 @@ public void testGetBestMatchIdValueNoLocale() throws ClassNotFoundException { "countries"); assertNotNull(instance); Choices result = instance.getBestMatch(idValue, null); + assertNotEquals("At least one match expected", 0, result.values.length); assertEquals(idValue, result.values[0].value); assertEquals("Algeria", result.values[0].label); } @@ -157,6 +161,7 @@ public void testGetMatchesGermanLocale() throws ClassNotFoundException { "countries"); assertNotNull(instance); Choices result = instance.getMatches(labelPart, start, limit, "de"); + assertNotEquals("At least one match expected", 0, result.values.length); assertEquals(idValue, result.values[0].value); assertEquals("Algerien", result.values[0].label); } @@ -179,6 +184,7 @@ public void testGetBestMatchIdValueGermanLocale() throws ClassNotFoundException "countries"); assertNotNull(instance); Choices result = instance.getBestMatch(idValue, "de"); + assertNotEquals("At least one match expected", 0, result.values.length); assertEquals(idValue, result.values[0].value); assertEquals("Algerien", result.values[0].label); } From 682a118d6e26b9dcf00dc253d5d68fbd29164df2 Mon Sep 17 00:00:00 2001 From: "Mark H. Wood" Date: Thu, 24 Jul 2025 11:51:53 -0400 Subject: [PATCH 3/5] Variables in XPath expressions should not be quoted. Documentation cleanup. Clean up many IDE warnings. (cherry picked from commit 7deaf1cca5c35b721726be1fa7b8a0aa86647938) (cherry picked from commit 01c533ae0b29da87a7d8a6c3071da953030059bd) --- .../authority/DSpaceControlledVocabulary.java | 66 +++++++++---------- .../DSpaceControlledVocabularyTest.java | 14 ++-- 2 files changed, 41 insertions(+), 39 deletions(-) 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 ff91a58c6f36..ccfb15679054 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 @@ -35,23 +35,25 @@ * from {@code ${dspace.dir}/config/controlled-vocabularies/*.xml} and turns * them into autocompleting authorities. * - * Configuration: This MUST be configured as a self-named plugin, e.g.: {@code - * plugin.selfnamed.org.dspace.content.authority.ChoiceAuthority = \ + *

Configuration: This MUST be configured as a self-named plugin, e.g.: {@code + * plugin.selfnamed.org.dspace.content.authority.ChoiceAuthority = * org.dspace.content.authority.DSpaceControlledVocabulary * } * - * It AUTOMATICALLY configures a plugin instance for each XML file in the + *

It AUTOMATICALLY configures a plugin instance for each XML file in the * controlled vocabularies directory. The name of the plugin is the basename of * the file; e.g., {@code ${dspace.dir}/config/controlled-vocabularies/nsi.xml} * would generate a plugin called "nsi". * - * Each configured plugin comes with three configuration options: {@code - * vocabulary.plugin._plugin_.hierarchy.store = - * # Store entire hierarchy along with selected value. Default: TRUE - * vocabulary.plugin._plugin_.hierarchy.suggest = - * # Display entire hierarchy in the suggestion list. Default: TRUE - * vocabulary.plugin._plugin_.delimiter = "" - * # Delimiter to use when building hierarchy strings. Default: "::" + *

Each configured plugin comes with three configuration options: + *

    + *
  • {@code vocabulary.plugin._plugin_.hierarchy.store = + * # Store entire hierarchy along with selected value. Default: TRUE}
  • + *
  • {@code vocabulary.plugin._plugin_.hierarchy.suggest = + * # Display entire hierarchy in the suggestion list. Default: TRUE}
  • + *
  • {@code vocabulary.plugin._plugin_.delimiter = "" + * # Delimiter to use when building hierarchy strings. Default: "::"}
  • + *
* } * * @author Michael B. Klein @@ -59,12 +61,13 @@ public class DSpaceControlledVocabulary extends SelfNamedPlugin implements HierarchicalAuthority { - private static Logger log = org.apache.logging.log4j.LogManager.getLogger(DSpaceControlledVocabulary.class); + private static final Logger log = org.apache.logging.log4j.LogManager.getLogger(); protected static String xpathTemplate = "//node[contains(translate(@label,'ABCDEFGHIJKLMNOPQRSTUVWXYZ'," + - "'abcdefghijklmnopqrstuvwxyz'),'%s')]"; - protected static String idTemplate = "//node[@id = '%s']"; - protected static String labelTemplate = "//node[@label = '%s']"; - protected static String idParentTemplate = "//node[@id = '%s']/parent::isComposedBy/parent::node"; + "'abcdefghijklmnopqrstuvwxyz'),%s)]"; + protected static String idTemplate = "//node[@id = %s]"; + protected static String idTemplateQuoted = "//node[@id = '%s']"; + protected static String labelTemplate = "//node[@label = %s]"; + protected static String idParentTemplate = "//node[@id = %s]/parent::isComposedBy/parent::node"; protected static String rootTemplate = "/node"; protected static String idAttribute = "id"; protected static String labelAttribute = "label"; @@ -110,7 +113,7 @@ public boolean accept(File dir, String name) { File.separator + "config" + File.separator + "controlled-vocabularies"; String[] xmlFiles = (new File(vocabulariesPath)).list(new xmlFilter()); - List names = new ArrayList(); + List names = new ArrayList<>(); for (String filename : xmlFiles) { names.add((new File(filename)).getName().replace(".xml", "")); } @@ -193,8 +196,8 @@ public Choices getMatches(String text, int start, int limit, String locale) { } throw new IllegalArgumentException("Unexpected variable: " + varName); }); - int total = 0; - List choices = new ArrayList(); + int total; + List choices; try { NodeList results = (NodeList) xpath.evaluate(xpathExpression, vocabulary, XPathConstants.NODESET); total = results.getLength(); @@ -210,7 +213,7 @@ public Choices getMatches(String text, int start, int limit, String locale) { @Override public Choices getBestMatch(String text, String locale) { init(locale); - log.debug("Getting best matches for '" + text + "'"); + log.debug("Getting best matches for {}'", text); String[] textHierarchy = text.split(hierarchyDelimiter, -1); StringBuilder xpathExpressionBuilder = new StringBuilder(); for (int i = 0; i < textHierarchy.length; i++) { @@ -226,7 +229,7 @@ public Choices getBestMatch(String text, String locale) { } throw new IllegalArgumentException("Unexpected variable: " + varName); }); - List choices = new ArrayList(); + List choices; try { NodeList results = (NodeList) xpath.evaluate(xpathExpression, vocabulary, XPathConstants.NODESET); choices = getChoicesFromNodeList(results, 0, 1); @@ -298,15 +301,12 @@ public Integer getPreloadLevel() { } private boolean isRootElement(Node node) { - if (node != null && node.getOwnerDocument().getDocumentElement().equals(node)) { - return true; - } - return false; + return node != null && node.getOwnerDocument().getDocumentElement().equals(node); } private Node getNode(String key, String locale) throws XPathExpressionException { init(locale); - String xpathExpression = String.format(idTemplate, key); + String xpathExpression = String.format(idTemplateQuoted, key); Node node = getNodeFromXPath(xpathExpression); return node; } @@ -318,7 +318,7 @@ private Node getNodeFromXPath(String xpathExpression) throws XPathExpressionExce } private List getChoicesFromNodeList(NodeList results, int start, int limit) { - List choices = new ArrayList(); + List choices = new ArrayList<>(); for (int i = 0; i < results.getLength(); i++) { if (i < start) { continue; @@ -337,17 +337,17 @@ private List getChoicesFromNodeList(NodeList results, int start, int lim private Map addOtherInformation(String parentCurr, String noteCurr, List childrenCurr, String authorityCurr) { - Map extras = new HashMap(); + Map extras = new HashMap<>(); if (StringUtils.isNotBlank(parentCurr)) { extras.put("parent", parentCurr); } if (StringUtils.isNotBlank(noteCurr)) { extras.put("note", noteCurr); } - if (childrenCurr.size() > 0) { - extras.put("hasChildren", "true"); - } else { + if (childrenCurr.isEmpty()) { extras.put("hasChildren", "false"); + } else { + extras.put("hasChildren", "true"); } extras.put("id", authorityCurr); return extras; @@ -402,7 +402,7 @@ private String getNote(Node node) { } private List getChildren(Node node) { - List children = new ArrayList(); + List children = new ArrayList<>(); NodeList childNodes = node.getChildNodes(); for (int ci = 0; ci < childNodes.getLength(); ci++) { Node firstChild = childNodes.item(ci); @@ -425,7 +425,7 @@ private List getChildren(Node node) { private boolean isSelectable(Node node) { Node selectableAttr = node.getAttributes().getNamedItem("selectable"); if (null != selectableAttr) { - return Boolean.valueOf(selectableAttr.getNodeValue()); + return Boolean.parseBoolean(selectableAttr.getNodeValue()); } else { // Default is true return true; } @@ -452,7 +452,7 @@ private String getAuthority(Node node) { } private Choices getChoicesByXpath(String xpathExpression, int start, int limit) { - List choices = new ArrayList(); + List choices = new ArrayList<>(); XPath xpath = XPathFactory.newInstance().newXPath(); try { Node parentNode = (Node) xpath.evaluate(xpathExpression, vocabulary, XPathConstants.NODE); 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 index 5eb56428f7db..7012556ceb52 100644 --- a/dspace-api/src/test/java/org/dspace/content/authority/DSpaceControlledVocabularyTest.java +++ b/dspace-api/src/test/java/org/dspace/content/authority/DSpaceControlledVocabularyTest.java @@ -141,9 +141,10 @@ public void testGetBestMatchIdValueNoLocale() throws ClassNotFoundException { } /** - * Test of getMatches method of class - * DSpaceControlledVocabulary using a localized controlled vocabulary with valid locale parameter (localized - * label returned) + * Test of getMatches method of class DSpaceControlledVocabulary + * using a localized controlled vocabulary with valid locale parameter + * (localized label returned). + * @throws java.lang.ClassNotFoundException if class under test cannot be found. */ @Test public void testGetMatchesGermanLocale() throws ClassNotFoundException { @@ -167,9 +168,10 @@ public void testGetMatchesGermanLocale() throws ClassNotFoundException { } /** - * Test of getBestMatch method of class - * DSpaceControlledVocabulary using a localized controlled vocabulary with valid locale parameter (localized - * label returned) + * Test of getBestMatch method of class DSpaceControlledVocabulary + * using a localized controlled vocabulary with valid locale parameter + * (localized label returned). + * @throws java.lang.ClassNotFoundException if class under test cannot be found. */ @Test public void testGetBestMatchIdValueGermanLocale() throws ClassNotFoundException { From 538001ed5af132d912583f640481d63af113d9fc Mon Sep 17 00:00:00 2001 From: "Mark H. Wood" Date: Thu, 24 Jul 2025 14:01:57 -0400 Subject: [PATCH 4/5] Correct some assumptions about what should be quoted. (cherry picked from commit dac494191c5079912cdc3db1f6af70f1cfdd7c43) (cherry picked from commit 068eb40e621f5ccbc022f028131e9a3034bf2a1c) --- .../dspace/content/authority/DSpaceControlledVocabulary.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 ccfb15679054..b6e1bc213d3a 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 @@ -67,7 +67,7 @@ public class DSpaceControlledVocabulary extends SelfNamedPlugin implements Hiera protected static String idTemplate = "//node[@id = %s]"; protected static String idTemplateQuoted = "//node[@id = '%s']"; protected static String labelTemplate = "//node[@label = %s]"; - protected static String idParentTemplate = "//node[@id = %s]/parent::isComposedBy/parent::node"; + protected static String idParentTemplate = "//node[@id = '%s']/parent::isComposedBy/parent::node"; protected static String rootTemplate = "/node"; protected static String idAttribute = "id"; protected static String labelAttribute = "label"; @@ -277,7 +277,7 @@ public Choices getTopChoices(String authorityName, int start, int limit, String @Override public Choices getChoicesByParent(String authorityName, String parentId, int start, int limit, String locale) { init(locale); - String xpathExpression = String.format(idTemplate, parentId); + String xpathExpression = String.format(idTemplateQuoted, parentId); return getChoicesByXpath(xpathExpression, start, limit); } From 15aae0170f153cca994c611ce2d9bd5ee046b90e Mon Sep 17 00:00:00 2001 From: Juraj Roka <95219754+jr-rk@users.noreply.github.com> Date: Thu, 26 Nov 2026 08:39:02 +0100 Subject: [PATCH 5/5] MENDELU/Fix dspace-customers 1083 V4: bind vocabulary node id as an XPath variable Port of dtq-dev-93 PR 1650 (commit f08abc4357). It needs the label search fix from upstream DSpace PR 11078 (PR 11075 ported to dspace-9_x), which is cherry-picked in the four commits before this one. --- .../authority/DSpaceControlledVocabulary.java | 62 +++++++++++++++---- .../config/controlled-vocabularies/farm.xml | 6 +- .../DSpaceControlledVocabularyTest.java | 33 ++++++++++ 3 files changed, 88 insertions(+), 13 deletions(-) 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 b6e1bc213d3a..10e7f0fdfcef 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 @@ -65,9 +65,9 @@ public class DSpaceControlledVocabulary extends SelfNamedPlugin implements Hiera protected static String xpathTemplate = "//node[contains(translate(@label,'ABCDEFGHIJKLMNOPQRSTUVWXYZ'," + "'abcdefghijklmnopqrstuvwxyz'),%s)]"; protected static String idTemplate = "//node[@id = %s]"; - protected static String idTemplateQuoted = "//node[@id = '%s']"; + protected static String idTemplateQuoted = "//node[@id = $nodeId]"; protected static String labelTemplate = "//node[@label = %s]"; - protected static String idParentTemplate = "//node[@id = '%s']/parent::isComposedBy/parent::node"; + protected static String idParentTemplate = "//node[@id = $nodeId]/parent::isComposedBy/parent::node"; protected static String rootTemplate = "/node"; protected static String idAttribute = "id"; protected static String labelAttribute = "label"; @@ -277,16 +277,14 @@ public Choices getTopChoices(String authorityName, int start, int limit, String @Override public Choices getChoicesByParent(String authorityName, String parentId, int start, int limit, String locale) { init(locale); - String xpathExpression = String.format(idTemplateQuoted, parentId); - return getChoicesByXpath(xpathExpression, start, limit); + return getChoicesByXpathWithId(idTemplateQuoted, parentId, start, limit); } @Override public Choice getParentChoice(String authorityName, String childId, String locale) { init(locale); try { - String xpathExpression = String.format(idParentTemplate, childId); - Choice choice = createChoiceFromNode(getNodeFromXPath(xpathExpression)); + Choice choice = createChoiceFromNode(getNodeByIdFromXPath(idParentTemplate, childId)); return choice; } catch (XPathExpressionException e) { log.error(e.getMessage(), e); @@ -306,15 +304,18 @@ private boolean isRootElement(Node node) { private Node getNode(String key, String locale) throws XPathExpressionException { init(locale); - String xpathExpression = String.format(idTemplateQuoted, key); - Node node = getNodeFromXPath(xpathExpression); - return node; + return getNodeByIdFromXPath(idTemplateQuoted, key); } - private Node getNodeFromXPath(String xpathExpression) throws XPathExpressionException { + private Node getNodeByIdFromXPath(String xpathTemplate, String nodeId) throws XPathExpressionException { XPath xpath = XPathFactory.newInstance().newXPath(); - Node node = (Node) xpath.evaluate(xpathExpression, vocabulary, XPathConstants.NODE); - return node; + xpath.setXPathVariableResolver(variableName -> { + if ("nodeId".equals(variableName.getLocalPart())) { + return nodeId; + } + throw new IllegalArgumentException("Unexpected variable: " + variableName); + }); + return (Node) xpath.evaluate(xpathTemplate, vocabulary, XPathConstants.NODE); } private List getChoicesFromNodeList(NodeList results, int start, int limit) { @@ -482,6 +483,43 @@ private Choices getChoicesByXpath(String xpathExpression, int start, int limit) return new Choices(false); } + private Choices getChoicesByXpathWithId(String xpathTemplate, String nodeId, int start, int limit) { + List choices = new ArrayList<>(); + XPath xpath = XPathFactory.newInstance().newXPath(); + xpath.setXPathVariableResolver(variableName -> { + if ("nodeId".equals(variableName.getLocalPart())) { + return nodeId; + } + throw new IllegalArgumentException("Unexpected variable: " + variableName); + }); + try { + Node parentNode = (Node) xpath.evaluate(xpathTemplate, vocabulary, XPathConstants.NODE); + int count = 0; + if (parentNode != null) { + NodeList childNodes = (NodeList) xpath.evaluate(".//isComposedBy", parentNode, XPathConstants.NODE); + if (null != childNodes) { + for (int i = 0; i < childNodes.getLength(); i++) { + Node childNode = childNodes.item(i); + if (childNode != null && "node".equals(childNode.getNodeName())) { + if (count < start || choices.size() >= limit) { + count++; + continue; + } + count++; + choices.add(createChoiceFromNode(childNode)); + } + } + } + return new Choices(choices.toArray(new Choice[choices.size()]), start, count, + Choices.CF_AMBIGUOUS, false); + } + } catch (XPathExpressionException e) { + log.warn(e.getMessage(), e); + return new Choices(true); + } + return new Choices(false); + } + private Choice createChoiceFromNode(Node node) { if (node != null && !isRootElement(node)) { Choice choice = new Choice(getAuthority(node), getLabel(node), getValue(node), diff --git a/dspace-api/src/test/data/dspaceFolder/config/controlled-vocabularies/farm.xml b/dspace-api/src/test/data/dspaceFolder/config/controlled-vocabularies/farm.xml index c77d1c5ff291..e4c1469b6fd7 100644 --- a/dspace-api/src/test/data/dspaceFolder/config/controlled-vocabularies/farm.xml +++ b/dspace-api/src/test/data/dspaceFolder/config/controlled-vocabularies/farm.xml @@ -2,6 +2,10 @@ - + + + + + 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 index 7012556ceb52..8b9128f32671 100644 --- a/dspace-api/src/test/java/org/dspace/content/authority/DSpaceControlledVocabularyTest.java +++ b/dspace-api/src/test/java/org/dspace/content/authority/DSpaceControlledVocabularyTest.java @@ -10,6 +10,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import java.io.IOException; @@ -91,6 +92,38 @@ public void testGetMatches() throws IOException, ClassNotFoundException { assertEquals("north 40", result.values[0].value); } + /** + * Node ids (entryID, parent, child) must be compared as plain strings, not spliced into the XPath expression. + * @throws java.lang.ClassNotFoundException passed through. + */ + @Test + public void testNodeIdIsNotEvaluatedAsXPath() throws ClassNotFoundException { + DSpaceControlledVocabulary instance = (DSpaceControlledVocabulary) + CoreServiceFactory.getInstance().getPluginService() + .getNamedPlugin(Class.forName("org.dspace.content.authority.ChoiceAuthority"), "farm"); + assertNotNull(instance); + + Choice choice = instance.getChoice("s40", null); + assertNotNull(choice); + assertEquals("south 40", choice.label); + + // Spliced into the expression this would select //node[@id = 'x' or @label='south 40'] + assertNull(instance.getChoice("x' or @label='south 40", null)); + // ... and here the root node, returning its children + Choices children = instance.getChoicesByParent("farm", "x' or @label='the farm", 0, 10, null); + assertEquals(0, children.values.length); + + children = instance.getChoicesByParent("farm", "s40", 0, 10, null); + assertEquals(1, children.values.length); + assertEquals("pond", children.values[0].authority); + + Choice parent = instance.getParentChoice("farm", "pond", null); + assertNotNull(parent); + assertEquals("s40", parent.authority); + // ... and here the parent of 'pond' + assertNull(instance.getParentChoice("farm", "x' or @id='pond", null)); + } + /** * Test of getMatches method of class * DSpaceControlledVocabulary using a localized controlled vocabulary with no locale (fallback to default)