Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,47 @@
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import net.sf.saxon.jaxp.SaxonTransformerFactory;
import net.sf.saxon.s9api.Processor;
import org.apache.commons.io.IOUtils;
import org.dspace.xoai.services.impl.resources.functions.LogMissingFn;
import org.dspace.xoai.services.impl.resources.functions.ShortestIdFn;
import org.dspace.xoai.services.impl.resources.functions.StringXSLFunction;

public abstract class AbstractXSLTest {
/**
* The backend URL fn:getProperty('dspace.server.url') answers with. It is deliberately on another host
* than the repository url of the test fixtures, so a stylesheet that derives the server from the UI
* url instead of asking the configuration fails its tests.
*/
protected static final String TEST_SERVER_URL = "https://api.example.org/server";

private static final Map<String, String> DEFAULT_PROPERTIES = Map.of("dspace.server.url", TEST_SERVER_URL);

/** what the stub getProperty answers during the current transformation */
private static volatile Map<String, String> properties = DEFAULT_PROPERTIES;

// Requires usage of Saxon as OAI-PMH uses some XSLT 2 functions
private static final TransformerFactory factory = TransformerFactory
.newInstance("net.sf.saxon.TransformerFactoryImpl", null);

static {
// In production DSpaceResourceResolver registers the real extension functions. getProperty reads
// the DSpace configuration, which a unit test does not start, so it is replaced by a stub.
Processor processor = ((SaxonTransformerFactory) factory).getProcessor();
processor.registerExtensionFunction(new StubGetPropertyFn());
// Real functions that need no DSpace services (used by elg.xsl).
processor.registerExtensionFunction(new LogMissingFn());
processor.registerExtensionFunction(new ShortestIdFn());
}

protected TransformBuilder apply(String xslLocation) throws Exception {
return new TransformBuilder(xslLocation);
}
Expand All @@ -42,17 +71,49 @@ private InputStream print(InputStream resourceAsStream) throws Exception {

public static class TransformBuilder {
private final Transformer transformer;
private final Map<String, String> overrides = new HashMap<>();

public TransformBuilder(String xslLocation) throws Exception {
this.transformer = factory.newTransformer(
new StreamSource(new File("../dspace/config/crosswalks/oai/metadataFormats", xslLocation)));
}

/**
* Set a configuration property for this transformation only; null leaves it unset.
*/
public TransformBuilder withProperty(String key, String value) {
this.overrides.put(key, value);
return this;
}

public String to(InputStream input) throws Exception {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
this.transformer.transform(new StreamSource(input), new StreamResult(outputStream));
outputStream.close();
return outputStream.toString();
Map<String, String> merged = new HashMap<>(DEFAULT_PROPERTIES);
merged.putAll(overrides);
properties = merged;
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
this.transformer.transform(new StreamSource(input), new StreamResult(outputStream));
outputStream.close();
return outputStream.toString();
} finally {
properties = DEFAULT_PROPERTIES;
}
}
}

/**
* fn:getProperty for the tests: same name and namespace as GetPropertyFn, answers from
* {@link #properties} instead of the DSpace configuration.
*/
private static class StubGetPropertyFn extends StringXSLFunction {
@Override
protected String getFnName() {
return "getProperty";
}

@Override
protected String getStringResult(String param) {
return properties.get(param);
}
}
}
Loading
Loading