net.sourceforge.jaulp.xsl.transform.XsltTransformerUtilsTest.java Source code

Java tutorial

Introduction

Here is the source code for net.sourceforge.jaulp.xsl.transform.XsltTransformerUtilsTest.java

Source

/**
 * Copyright (C) 2007 Asterios Raptis
 *
 * 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 net.sourceforge.jaulp.xsl.transform;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;

import javax.xml.transform.Source;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.stream.StreamSource;

import net.sourceforge.jaulp.file.read.ReadFileUtils;
import net.sourceforge.jaulp.file.search.PathFinder;
import net.sourceforge.jaulp.io.StreamUtils;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.testng.AssertJUnit;
import org.testng.annotations.Test;

public class XsltTransformerUtilsTest {
    private String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><birthdates>\r\n" + "    <birthdate>\r\n"
            + "        <id>1</id>\r\n" + "        <date>19:07</date>\r\n" + "    </birthdate>\r\n"
            + "    <birthdate>\r\n" + "        <id>2</id>\r\n" + "        <date>13:48</date>\r\n"
            + "    </birthdate>\r\n" + "    <birthdate>\r\n" + "        <id>3</id>\r\n"
            + "        <date>08:40</date>\r\n" + "    </birthdate>\r\n" + "</birthdates>\r\n";
    /** The Constant logger. */
    protected static final Logger logger = Logger.getLogger(XsltTransformerUtilsTest.class);

    @Test(enabled = false)
    public void testTransformStringStringOutputStream() {
        throw new RuntimeException("Test not implemented");
    }

    @Test(enabled = false)
    public void testTransformFileFileOutputStream() {
        throw new RuntimeException("Test not implemented");
    }

    @Test(enabled = true)
    public void testTransformSourceSourceOutputStream() throws TransformerException, IOException {
        final File resDestDir = PathFinder.getSrcTestResourcesDir();
        String[] dirsAndFilename = { "net", "sourceforge", "jaulp", "xsl", "transform", "birthdates.xml" };
        File xmlFile = PathFinder.getRelativePath(resDestDir, dirsAndFilename);
        File xsltFile = PathFinder.getRelativePathTo(resDestDir, "\\.", "net.sourceforge.jaulp.xsl.transform",
                "functions.xsl");
        InputStream is = StreamUtils.getInputStream(xsltFile);
        Source xsltSource = new StreamSource(is);

        InputStream xmlIs = StreamUtils.getInputStream(xmlFile);
        File outputFile = PathFinder.getRelativePathTo(resDestDir, "\\.", "net.sourceforge.jaulp.xsl.transform",
                "data_02_output.xml");
        OutputStream output = StreamUtils.getOutputStream(outputFile, true);
        Source xmlSource = new StreamSource(xmlIs);
        XsltTransformerUtils.transform(xmlSource, xsltSource, output);
        String actual = ReadFileUtils.readFromFile(outputFile);
        actual = StringUtils.remove(actual, '\r');
        actual = StringUtils.remove(actual, '\n');
        expected = StringUtils.remove(expected, '\r');
        expected = StringUtils.remove(expected, '\n');
        AssertJUnit.assertTrue("", expected.equals(actual));
    }

    @Test(enabled = false)
    public void testGetTransformerSource() {
        throw new RuntimeException("Test not implemented");
    }

    @Test
    public void testGetTransformerFile() throws TransformerConfigurationException,
            TransformerFactoryConfigurationError, TransformerException, IOException {
        final File resDestDir = PathFinder.getSrcTestResourcesDir();
        String[] dirsAndFilename = { "net", "sourceforge", "jaulp", "xsl", "transform", "birthdates.xml" };
        File xmlFile = PathFinder.getRelativePathTo(resDestDir, Arrays.asList(dirsAndFilename));
        File xsltFile = PathFinder.getRelativePathTo(resDestDir, "\\.", "net.sourceforge.jaulp.xsl.transform",
                "functions.xsl");
        File outputFile = PathFinder.getRelativePathTo(resDestDir, "\\.", "net.sourceforge.jaulp.xsl.transform",
                "output.xml");
        XsltTransformerUtils.transform(xmlFile, xsltFile, new FileOutputStream(outputFile));
        String actual = ReadFileUtils.readFromFile(outputFile);
        actual = StringUtils.remove(actual, '\r');
        actual = StringUtils.remove(actual, '\n');
        expected = StringUtils.remove(expected, '\r');
        expected = StringUtils.remove(expected, '\n');
        AssertJUnit.assertTrue("", expected.equals(actual));
    }

    @Test(enabled = false)
    public void testGetTransformerString() {
        throw new RuntimeException("Test not implemented");
    }

}