org.sansdemeure.zenindex.indexer.odt.TestHTMLConverter.java Source code

Java tutorial

Introduction

Here is the source code for org.sansdemeure.zenindex.indexer.odt.TestHTMLConverter.java

Source

/*
 * This file is part of zen-index.
    
zen-index is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
zen-index is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with zen-index.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.sansdemeure.zenindex.indexer.odt;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.sansdemeure.zenindex.config.FreeMarkerConfig;
import org.sansdemeure.zenindex.indexer.odt.OdtHTMLConverterHandler;
import org.sansdemeure.zenindex.util.FileUtil;
import org.sansdemeure.zenindex.util.ODTResource;
import org.sansdemeure.zenindex.util.TestAppender;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.xml.sax.SAXException;

/**
 * @author mcourcy
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = FreeMarkerConfig.class)
public class TestHTMLConverter {

    @Test
    public void test() throws ParserConfigurationException, SAXException, IOException {
        TestAppender testAppender = new TestAppender();
        File testDir = FileUtil.prepareEmptyDirectory(TestHTMLConverter.class);
        FileUtil.copyFromResources("docs/1992/Sandokai_with_2comments.odt", testDir, "Sandokai.odt");
        File odt = new File(testDir, "Sandokai.odt");
        try (ODTResource odtRessource = new ODTResource(odt)) {
            InputStream in = odtRessource.openContentXML();
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();
            OdtHTMLConverterHandler htmlConverter = new OdtHTMLConverterHandler();
            saxParser.parse(in, htmlConverter);
            testAppender.verify("paragraphs were created");
            testAppender.verify("spans were created");
            testAppender.verify("2 annotations were created");
            testAppender.verify("All paragraphs were closed");
            testAppender.verify("All spans were closed");
        }

    }

}