-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 2023.06.x: update maven-processor-plugin for Maven 4 MCR-3092 XSL test capabilities (#2144) MCR-3133 Add fallback value to xpath based classification mapping (#2190) MCR-3131 align behaviour of command with normal repository operations MCR-3104 missing database indexes (#2154)
- Loading branch information
Showing
22 changed files
with
653 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
mycore-base/src/test/java/org/mycore/common/util/MCRTestCaseClassificationUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* This file is part of *** M y C o R e *** | ||
* See http://www.mycore.de/ for details. | ||
* | ||
* MyCoRe is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* MyCoRe is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with MyCoRe. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package org.mycore.common.util; | ||
|
||
import org.jdom2.Document; | ||
import org.jdom2.input.SAXBuilder; | ||
import org.mycore.datamodel.classifications2.MCRCategory; | ||
import org.mycore.datamodel.classifications2.MCRCategoryDAOFactory; | ||
import org.mycore.datamodel.classifications2.utils.MCRXMLTransformer; | ||
|
||
public class MCRTestCaseClassificationUtil { | ||
/** | ||
* Adds a classification represented by the given XML file from classpath to the system. | ||
* | ||
* @param resourcePath the XML classpath file containing the classification | ||
*/ | ||
public static void addClassification(String resourcePath) throws Exception { | ||
Document classification = new SAXBuilder() | ||
.build(MCRTestCaseClassificationUtil.class.getResourceAsStream(resourcePath)); | ||
MCRCategory category = MCRXMLTransformer.getCategory(classification); | ||
MCRCategoryDAOFactory.getInstance().addCategory(null, category); | ||
} | ||
|
||
} |
136 changes: 136 additions & 0 deletions
136
mycore-base/src/test/java/org/mycore/common/util/MCRTestCaseXSLTUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/* | ||
* This file is part of *** M y C o R e *** | ||
* See http://www.mycore.de/ for details. | ||
* | ||
* MyCoRe is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* MyCoRe is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with MyCoRe. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package org.mycore.common.util; | ||
|
||
import net.sf.saxon.jaxp.TransformerImpl; | ||
import net.sf.saxon.s9api.Message; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.jdom2.Content; | ||
import org.jdom2.Document; | ||
import org.jdom2.Element; | ||
import org.jdom2.transform.JDOMResult; | ||
import org.jdom2.transform.JDOMSource; | ||
import org.mycore.common.xml.MCRURIResolver; | ||
|
||
import javax.xml.transform.Source; | ||
import javax.xml.transform.Transformer; | ||
import javax.xml.transform.TransformerException; | ||
import javax.xml.transform.TransformerFactory; | ||
import javax.xml.transform.stream.StreamSource; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Provides utility methods for testing XSLT functions. The general process for XSL testing is as follows: | ||
* <ol> | ||
* <li>create a test XSL file containing match templates calling functions/templates to test</li> | ||
* <li>call prepareTestDocument with the rootName matching the test template (and possible XML content)</li> | ||
* <li>call transform with the test document and evaluate the result document</li> | ||
* </ol> | ||
*/ | ||
public class MCRTestCaseXSLTUtil { | ||
private static final Logger LOGGER = LogManager.getLogger(); | ||
private static final String ERROR_XTMM9000 = "XTMM9000"; | ||
private static final String ERROR_XTMM9001 = "XTMM9001"; | ||
|
||
/** | ||
* Returns an XML document with a root element with the given name | ||
* | ||
* @param rootName the root element name of the document | ||
* @return an XML document with a root element with the given name | ||
*/ | ||
public static Document prepareTestDocument(String rootName) { | ||
return new Document().addContent(new Element(rootName)); | ||
} | ||
|
||
/** | ||
* Returns an XML document with the given name as root element and the content of the given XML tree as | ||
* child element. The resulting document can be used for calling XSLT test files using template matching. | ||
* | ||
* @param rootName the name used as root element name | ||
* @param xml the XML tree | ||
* @return an XML document with the name as root element and the content of the given XML tree as children | ||
*/ | ||
public static Document prepareTestDocument(String rootName, Element xml) { | ||
return prepareTestDocument(rootName, Collections.singletonList(xml)); | ||
} | ||
|
||
/** | ||
* Returns an XML document with the given name as root element and the content of the given XML tree as | ||
* child elements. The resulting document can be used for calling XSLT test files using template matching. | ||
* | ||
* @param rootName the name used as root element name | ||
* @param xml the XML tree | ||
* @return an XML document with the name as root element and the content of the given XML tree as children | ||
*/ | ||
public static Document prepareTestDocument(String rootName, List<? extends Content> xml) { | ||
final Element root = new Element(rootName); | ||
|
||
for (Content xmlContent : xml) { | ||
root.addContent(xmlContent.detach()); | ||
} | ||
|
||
return new Document(root); | ||
} | ||
|
||
/** | ||
* Transforms the XML document using the given XSL stylesheet from classpath with the given parameters. | ||
* | ||
* @param xml the XML document to parse | ||
* @param xsl the XSL stylesheet file used for parsing | ||
* @param parameters the XSL transformation parameters | ||
*/ | ||
public static Document transform(Document xml, String xsl, Map<String, Object> parameters) | ||
throws TransformerException { | ||
Source xslt = new StreamSource(MCRTestCaseXSLTUtil.class.getResourceAsStream(xsl)); | ||
JDOMResult result = new JDOMResult(); | ||
|
||
TransformerFactory factory = TransformerFactory.newInstance(); | ||
factory.setURIResolver(MCRURIResolver.instance()); | ||
|
||
Transformer transformer = factory.newTransformer(xslt); | ||
parameters.forEach(transformer::setParameter); | ||
|
||
if (transformer instanceof TransformerImpl transformerImpl) { | ||
transformerImpl.getUnderlyingXsltTransformer().setMessageHandler(MCRTestCaseXSLTUtil::log); | ||
} | ||
|
||
transformer.transform(new JDOMSource(xml), result); | ||
|
||
return new Document(result.getResult()); | ||
} | ||
|
||
/** | ||
* Logs an XSL message to the system logger. | ||
* | ||
* @param message the message to log | ||
*/ | ||
private static void log(Message message) { | ||
// error codes from https://www.w3.org/2005/xqt-errors/: | ||
// XTMM9000, XTMM9001 are messages; other codes are warnings/errors | ||
if (StringUtils.equalsAny(message.getErrorCode().getLocalName(), ERROR_XTMM9000, ERROR_XTMM9001)) { | ||
LOGGER.info(message.getContent()); | ||
} else { | ||
LOGGER.error(message.getContent()); | ||
} | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
mycore-base/src/test/java/org/mycore/frontend/xsl/MCRXSLClassificationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* This file is part of *** M y C o R e *** | ||
* See http://www.mycore.de/ for details. | ||
* | ||
* MyCoRe is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* MyCoRe is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with MyCoRe. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package org.mycore.frontend.xsl; | ||
|
||
import org.jdom2.Document; | ||
import org.jdom2.Element; | ||
import org.junit.Test; | ||
import org.mycore.common.MCRJPATestCase; | ||
import org.mycore.common.util.MCRTestCaseClassificationUtil; | ||
|
||
import java.util.Map; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.mycore.common.util.MCRTestCaseXSLTUtil.prepareTestDocument; | ||
import static org.mycore.common.util.MCRTestCaseXSLTUtil.transform; | ||
|
||
public class MCRXSLClassificationTest extends MCRJPATestCase { | ||
private static final String XSL = "/xslt/functions/classificationTest.xsl"; | ||
|
||
@Test | ||
public void testCurrentLabelText() throws Exception { | ||
MCRTestCaseClassificationUtil.addClassification("/classification/TestClassification.xml"); | ||
final Document testDoc = prepareTestDocument("test-current-label-text"); | ||
|
||
// we have a current lang label (de) and a default lang label (en) entry | ||
Element result = transform(testDoc, XSL, | ||
Map.of("classid", "TestClassification", "categid", "junit_1")) | ||
.getRootElement(); | ||
assertEquals("junit_1 (de)", result.getText()); | ||
|
||
result = transform(testDoc, XSL, | ||
Map.of("classid", "TestClassification", "categid", "junit_1", "CurrentLang", "en")) | ||
.getRootElement(); | ||
assertEquals("junit_1 (en)", result.getText()); | ||
|
||
// we do not have a current lang label (de) entry, but default lang label (en) entry | ||
result = transform(testDoc, XSL, | ||
Map.of("classid", "TestClassification", "categid", "junit_2")) | ||
.getRootElement(); | ||
assertEquals("junit_2 (en)", result.getText()); | ||
|
||
// we neither have current lang nor default lang label | ||
result = transform(testDoc, XSL, | ||
Map.of("classid", "TestClassification", "categid", "junit_3")) | ||
.getRootElement(); | ||
assertEquals("??junit_3@de??", result.getText()); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
mycore-base/src/test/resources/classification/TestClassification.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<mycoreclass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="MCRClassification.xsd" ID="TestClassification"> | ||
<categories> | ||
<category ID="junit_1"> | ||
<label xml:lang="de" text="junit_1 (de)"/> | ||
<label xml:lang="en" text="junit_1 (en)"/> | ||
</category> | ||
<category ID="junit_2"> | ||
<label xml:lang="en" text="junit_2 (en)"/> | ||
</category> | ||
<category ID="junit_3"> | ||
</category> | ||
</categories> | ||
</mycoreclass> |
22 changes: 22 additions & 0 deletions
22
mycore-base/src/test/resources/xslt/functions/classificationTest.xsl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<xsl:stylesheet version="3.0" | ||
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" | ||
xmlns:xs="http://www.w3.org/2001/XMLSchema" | ||
xmlns:mcrclass="http://www.mycore.de/xslt/classification" | ||
exclude-result-prefixes="#all"> | ||
<xsl:include href="resource:xslt/functions/classification.xsl"/> | ||
|
||
<xsl:param name="classid" as="xs:string"/> | ||
<xsl:param name="categid" as="xs:string"/> | ||
<xsl:param name="CurrentLang" as="xs:string?" select="'de'"/> | ||
<xsl:param name="DefaultLang" as="xs:string?" select="'en'"/> | ||
|
||
|
||
<xsl:template match="test-current-label-text"> | ||
<xsl:variable name="class" select="mcrclass:category($classid, $categid)"/> | ||
<result> | ||
<xsl:value-of select="mcrclass:current-label-text($class)"/> | ||
</result> | ||
</xsl:template> | ||
|
||
</xsl:stylesheet> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.