001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.taskdefs.optional;
020:
021: import java.io.File;
022: import java.io.FileNotFoundException;
023: import java.net.URL;
024: import javax.xml.parsers.DocumentBuilder;
025: import javax.xml.parsers.DocumentBuilderFactory;
026: import junit.framework.TestCase;
027: import org.apache.tools.ant.taskdefs.XSLTLiaison;
028: import org.apache.tools.ant.util.FileUtils;
029: import org.w3c.dom.Document;
030:
031: /**
032: * Abtract testcase for XSLTLiaison.
033: * Override createLiaison for each XSLTLiaison.
034: *
035: * <a href="sbailliez@apache.org">Stephane Bailliez</a>
036: */
037: public abstract class AbstractXSLTLiaisonTest extends TestCase {
038:
039: private static final FileUtils FILE_UTILS = FileUtils
040: .getFileUtils();
041:
042: protected XSLTLiaison liaison;
043:
044: protected AbstractXSLTLiaisonTest(String name) {
045: super (name);
046: }
047:
048: protected void setUp() throws Exception {
049: liaison = createLiaison();
050: }
051:
052: // to override
053: protected abstract XSLTLiaison createLiaison() throws Exception;
054:
055: /** load the file from the caller classloader that loaded this class */
056: protected File getFile(String name) throws FileNotFoundException {
057: URL url = getClass().getResource(name);
058: if (url == null) {
059: throw new FileNotFoundException("Unable to load '" + name
060: + "' from classpath");
061: }
062: return new File(FILE_UTILS.fromURI(url.toExternalForm()));
063: }
064:
065: /** keep it simple stupid */
066: public void testTransform() throws Exception {
067: File xsl = getFile("/taskdefs/optional/xsltliaison-in.xsl");
068: liaison.setStylesheet(xsl);
069: liaison.addParam("param", "value");
070: File in = getFile("/taskdefs/optional/xsltliaison-in.xml");
071: File out = new File("xsltliaison.tmp");
072: out.deleteOnExit(); // just to be sure
073: try {
074: liaison.transform(in, out);
075: } finally {
076: out.delete();
077: }
078: }
079:
080: public void testEncoding() throws Exception {
081: File xsl = getFile("/taskdefs/optional/xsltliaison-encoding-in.xsl");
082: liaison.setStylesheet(xsl);
083: File in = getFile("/taskdefs/optional/xsltliaison-encoding-in.xml");
084: File out = new File("xsltliaison-encoding.tmp");
085: out.deleteOnExit(); // just to be sure
086: try {
087: liaison.transform(in, out);
088: Document doc = parseXML(out);
089: assertEquals("root", doc.getDocumentElement().getNodeName());
090: assertEquals("message", doc.getDocumentElement()
091: .getFirstChild().getNodeName());
092: assertEquals("\u00E9\u00E0\u00E8\u00EF\u00F9", doc
093: .getDocumentElement().getFirstChild()
094: .getFirstChild().getNodeValue());
095: } finally {
096: out.delete();
097: }
098: }
099:
100: public Document parseXML(File file) throws Exception {
101: DocumentBuilderFactory dbfactory = DocumentBuilderFactory
102: .newInstance();
103: DocumentBuilder dbuilder = dbfactory.newDocumentBuilder();
104: return dbuilder.parse(file);
105: }
106: }
|