/* $HeadURL$
* $Id$
*
* Copyright (c) 2006-2010 by Public Library of Science
* http://plos.org
* http://ambraproject.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.ambraproject.service.xml;
import org.ambraproject.ApplicationException;
import org.ambraproject.action.BaseTest;
import org.apache.commons.configuration.BaseConfiguration;
import org.apache.commons.configuration.Configuration;
import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.XMLUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import static org.testng.Assert.assertTrue;
/**
* @author Dragisa Krsmanovic
* @author Joe Osowski
*/
public class XMLServiceTest extends BaseTest {
private static final String OBJINFO_NAMESPACES =
"xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" "
+ "xmlns:fo=\"http://www.w3.org/1999/XSL/Format\" "
+ "xmlns:fn=\"http://www.w3.org/2005/xpath-functions\" "
+ "xmlns:xlink=\"http://www.w3.org/1999/xlink\" "
+ "xmlns:util=\"http://dtd.nlm.nih.gov/xsl/util\" "
+ "xmlns:mml=\"http://www.w3.org/1998/Math/MathML\"";
@Autowired
@Qualifier("secondaryObjectService")
protected XMLServiceImpl secondaryObjectService;
@Autowired
@Qualifier("viewNLMService")
protected XMLServiceImpl viewNLMService;
@BeforeClass
protected void setUp() throws Exception {
DocumentBuilderFactory documentBuilderfactory =
DocumentBuilderFactory.newInstance("org.apache.xerces.jaxp.DocumentBuilderFactoryImpl", getClass().getClassLoader());
documentBuilderfactory.setNamespaceAware(true);
documentBuilderfactory.setValidating(false);
documentBuilderfactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
Map<String, String> xmlFactoryProperties = new HashMap<String, String>();
xmlFactoryProperties.put("javax.xml.transform.TransformerFactory","net.sf.saxon.TransformerFactoryImpl");
xmlFactoryProperties.put("javax.xml.transform.Transformer","net.sf.saxon.Controller");
XMLUnit.setControlDocumentBuilderFactory(documentBuilderfactory);
XMLUnit.setTestDocumentBuilderFactory(documentBuilderfactory);
XMLUnit.setIgnoreComments(true);
XMLUnit.setIgnoreWhitespace(true);
XMLUnit.setIgnoreAttributeOrder(true);
XMLUnit.setTransformerFactory("net.sf.saxon.TransformerFactoryImpl");
XMLUnit.setXSLTVersion("2.0");
Configuration configiration = new BaseConfiguration();
configiration.setProperty("ambra.platform.appContext", "test-context");
}
@DataProvider(name = "objInfoSamples")
public String[][] createObjInfoSamples() {
return new String[][]{
{"<p>Hello World</p>", "<p>Hello World</p>"},
{"<sc>hello world</sc>", "<span " + OBJINFO_NAMESPACES + " class=\"small-caps\">hello world</span>"},
{"<bold>Hello World</bold>", "<strong " + OBJINFO_NAMESPACES + ">Hello World</strong>"},
};
}
@Test(dataProvider = "objInfoSamples")
public void testObjInfoTransformation(String source, String expected)
throws URISyntaxException, ApplicationException, SAXException, IOException,
ParserConfigurationException {
final DocumentBuilder builder = viewNLMService.createDocBuilder();
Document doc = builder.parse(new InputSource(new StringReader(source)));
String result = secondaryObjectService.getTransformedDocument(doc);
Diff diff = new Diff(expected, result);
assertTrue(diff.identical(), diff.toString());
}
private String getFileAsString(String resultFilename) throws IOException {
BufferedReader reader = new BufferedReader(
new InputStreamReader(getClass().getResourceAsStream(resultFilename), "UTF-8"));
StringBuilder expected = new StringBuilder();
String line = reader.readLine();
while (line != null) {
expected.append(line);
expected.append('\n');
line = reader.readLine();
}
return expected.toString();
}
private InputStream getFileAsStream(String resultFilename) throws IOException {
return getClass().getResourceAsStream(resultFilename);
}
}