Skip to content

Commit

Permalink
Merge branch '2023.06.x'
Browse files Browse the repository at this point in the history
* 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
yagee-de committed Jul 4, 2024
2 parents 99d5e55 + 92868d2 commit c50df19
Show file tree
Hide file tree
Showing 22 changed files with 653 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,21 @@
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.IdClass;
import jakarta.persistence.Index;
import jakarta.persistence.Table;

@Entity
@IdClass(MCRObjectIDPK.class)
@Table(name = "MCRObject")
@Table(name = "MCRObject",
indexes = {
@Index(name = "MCRObject_state", columnList = "state"),
@Index(name = "MCRObject_createdate", columnList = "createdate"),
@Index(name = "MCRObject_modifydate", columnList = "modifydate"),
@Index(name = "MCRObject_createdby", columnList = "createdby"),
@Index(name = "MCRObject_modifiedby", columnList = "modifiedby"),
@Index(name = "MCRObject_deletedby", columnList = "deletedby"),
@Index(name = "MCRObject_deletedate", columnList = "deletedate")
})
public class MCRObjectInfoEntity implements MCRObjectInfo {

private MCRObjectID id;
Expand Down
13 changes: 6 additions & 7 deletions mycore-base/src/test/java/org/mycore/common/MCRJPATestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,18 +159,18 @@ private void doSchemaOperation(Function<String, String> schemaFunction) {

protected static Optional<String> getDefaultSchema() {
return Optional.ofNullable(MCREntityManagerProvider
.getEntityManagerFactory()
.getProperties()
.get("hibernate.default_schema"))
.getEntityManagerFactory()
.getProperties()
.get("hibernate.default_schema"))
.map(Object::toString)
.map(schema -> quoteSchema() ? '"' + schema + '"' : schema);
}

protected static boolean quoteSchema() {
return Optional.ofNullable(MCREntityManagerProvider
.getEntityManagerFactory()
.getProperties()
.get("hibernate.globally_quoted_identifiers"))
.getEntityManagerFactory()
.getProperties()
.get("hibernate.globally_quoted_identifiers"))
.map(Object::toString)
.map(Boolean::parseBoolean)
.orElse(Boolean.FALSE);
Expand Down Expand Up @@ -276,5 +276,4 @@ protected static void executeUpdate(String sql, Consumer<Integer> consumer) {
}
});
}

}
16 changes: 8 additions & 8 deletions mycore-base/src/test/java/org/mycore/common/MCRTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@

package org.mycore.common;

import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.rules.TemporaryFolder;
import org.mycore.common.config.MCRConfigurationException;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
Expand All @@ -32,14 +40,6 @@
import java.util.Objects;
import java.util.Optional;

import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.rules.TemporaryFolder;
import org.mycore.common.config.MCRConfigurationException;

@MCRTestConfiguration(properties = {
@MCRTestProperty(key = "MCR.Metadata.Type.test", string = "true")
})
Expand Down
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);
}

}
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());
}
}
}
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());
}
}
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>
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>
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Index;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.MapKeyColumn;
import jakarta.persistence.NamedQueries;
Expand All @@ -52,7 +53,15 @@
@NamedQueries({
@NamedQuery(name = "mcrjob.classes",
query = "select DISTINCT(o.action) from MCRJob o") })
@Table(name = "MCRJob")
@Table(name = "MCRJob",
indexes = {
@Index(name = "MCRJob_status", columnList = "status"),
@Index(name = "MCRJob_added", columnList = "added"),
@Index(name = "MCRJob_start", columnList = "start"),
@Index(name = "MCRJob_finished", columnList = "finished"),
@Index(name = "MCRJob_action", columnList = "action"),
@Index(name = "MCRJob_tries", columnList = "tries")
})
public class MCRJob implements Cloneable {
private Long id;

Expand Down
Loading

0 comments on commit c50df19

Please sign in to comment.