Package

Source Code of ExporterTest

import com.directededge.Exporter;
import com.directededge.Item;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import static org.junit.Assert.*;
import org.xml.sax.SAXException;

public class ExporterTest
{
    public ExporterTest()
    {

    }

    @BeforeClass
    public static void setUpClass() throws Exception
    {

    }

    @AfterClass
    public static void tearDownClass() throws Exception
    {
        new File("test.xml").delete();
    }

    @Before
    public void setUp()
    {

    }

    @After
    public void tearDown()
    {

    }


    @Test
    public void exportFile() throws ParserConfigurationException, SAXException,
            IOException
    {
        export(new Exporter("test.xml"));
    }

    @Test
    public void exportStream() throws ParserConfigurationException, SAXException,
            IOException
    {
        export(new Exporter(new FileOutputStream("test.xml")));
    }

    private void export(Exporter exporter) throws ParserConfigurationException,
            SAXException, IOException
    {
        Item first = new Item(exporter.getDatabase(), "first");
        Item second = new Item(exporter.getDatabase(), "second");
        first.linkTo(second);
        first.addTag("tag1");
        second.addTag("tag2");
        first.setProperty("testkey", "testvalue");
        exporter.export(first);
        exporter.export(second);
        exporter.finish();

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(new File("test.xml"));

        NodeList itemNodes = doc.getElementsByTagName("item");

        assertEquals(itemNodes.getLength(), 2);

        assertEquals(itemNodes.item(0).getAttributes().getNamedItem("id").getTextContent(),
                     "first");
        assertEquals(itemNodes.item(1).getAttributes().getNamedItem("id").getTextContent(),
                     "second");
    }
}
TOP

Related Classes of ExporterTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.