fulcrum.xml.ParserTest.java Source code

Java tutorial

Introduction

Here is the source code for fulcrum.xml.ParserTest.java

Source

/**
 * Copyright (c)2012, Rational Developers; Ryan McGuinness
 * All rights reserved
 * Site: http://www.rationaldevelopers.com/fulcrum-xml
 * License: http://www.rationaldevelopers.com/fulcrum-xml/license.txt
 * Read Me: http://www.rationaldevelopers.com/fulcrum-xml/readme.txt
 *.
 */
package fulcrum.xml;

import java.io.InputStream;

import junit.framework.Assert;

import nu.xom.Builder;
import nu.xom.Document;

import org.apache.commons.vfs2.FileContent;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemManager;
import org.apache.commons.vfs2.VFS;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * @author Rational Developers
 * @author Ryan McGuinness
 *
 */
public class ParserTest {
    private static final Logger LOGGER = LoggerFactory.getLogger(ParserTest.class);

    private static InputStream getInputStream(String location) {
        InputStream is = null;
        try {
            FileSystemManager fsManager = VFS.getManager();
            FileObject fileObj = fsManager.resolveFile(location);
            if (fileObj != null && fileObj.exists() && fileObj.isReadable()) {
                FileContent content = fileObj.getContent();
                is = content.getInputStream();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return is;
    }

    @Test
    public void testInitializeVFS() {
        LOGGER.info("Starting Loading");
        InputStream simple = getInputStream(SIMPLE);
        Assert.assertNotNull(simple);
        InputStream complex = getInputStream(COMPLEX);
        Assert.assertNotNull(complex);
    }

    static final String SIMPLE = "res:fulcrum/xml/input-simple.xml";
    static final String COMPLEX = "res:fulcrum/xml/input-medium.xml";

    private static final void getStartLine(String testName, String fileName) {
        LOGGER.info("Starting: " + testName + " - " + fileName);
    }

    private static final void getEndLine(String testName, String fileName, long s) {
        long e = System.currentTimeMillis();
        LOGGER.info("Ending: " + testName + " - " + fileName);
        LOGGER.info("Elapsed Time: " + (e - s) + "(ms)");
    }

    private static void testFulcrumXml(String fileName) {
        long s = System.currentTimeMillis();
        getStartLine("Fulcrum", fileName);
        fulcrum.xml.Document d = null;
        try {
            d = new fulcrum.xml.Document(fileName);
            Assert.assertNotNull(d);
        } catch (ParseException pe) {
            pe.printStackTrace();
        } finally {
            d = null;
        }
        getEndLine("Fulcrum", fileName, s);
    }

    @Test
    public void testSimple() {
        testFulcrumXml(SIMPLE);
        LOGGER.info("Finished Fulcrum Simple");
    }

    @Test
    public void testComplex() {
        testFulcrumXml(COMPLEX);
        LOGGER.info("Finished Fulcrum Complex");
    }

    private void testXOM(String fileName) {
        long s = System.currentTimeMillis();
        getStartLine("XOM", fileName);
        Builder parser = new Builder();
        try {
            Document d = parser.build(getInputStream(fileName));
            Assert.assertNotNull(d);
        } catch (Exception e) {
            e.printStackTrace();
        }
        getEndLine("XOM", fileName, s);
    }

    @Test
    public void testXOMSimple() {
        testXOM(SIMPLE);
        LOGGER.info("Finished XOM Simple");
    }

    @Test
    public void testXOMComplex() {
        testXOM(COMPLEX);
        LOGGER.info("Finished XOM Complex");
    }
}