/* $Id: GenerateLatexTest.java,v 1.12 2006/10/20 20:22:13 m31 Exp $
 *
 * This file is part of the project "Hilbert II" - http://www.qedeq.org
 *
 * Copyright 2000-2006,  Michael Meyling <mime@qedeq.org>.
 *
 * "Hilbert II" 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 2 of the License, or (at your option) any later version.
 *
 * This program 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.
 */

package org.qedeq.kernel.latex;

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

import org.qedeq.kernel.bo.module.ModuleDataException;
import org.qedeq.kernel.rel.test.text.Xml2Latex;
import org.qedeq.kernel.test.QedeqTestCase;
import org.qedeq.kernel.utility.IoUtility;
import org.qedeq.kernel.xml.mapper.XmlFilePositionException;

/**
 * Test generating LaTeX files for all known samples.
 * FIXME mime 20060711: tested documents must also be tested within
 * {@link org.qedeq.kernel.bo.control.QedeqBoFactoryTest}!!!
 * 
 * @version $Revision: 1.12 $
 * @author Michael Meyling
 */
public final class GenerateLatexTest extends QedeqTestCase {

    /**
     * Start main process.
     * 
     * @throws IOException
     * @throws XmlFilePositionException 
     */
    public void testGeneration() throws IOException, XmlFilePositionException {
        File docDir = new File("../QedeqDoc");
        File genDir = new File("../QedeqGenerated");
        // test if we are in the normal development environment, where a project with name
        //  "../QedeqDoc" exists, otherwise we assume to run within the release directory
        //  structure where the docs are in the directory ../doc
        if (!docDir.exists()) {
            docDir = new File("../doc");
            genDir = new File("../doc");
            if (!docDir.exists()) {
                throw new IOException("unknown source directory for qedeq modules");
            }
        }
        generate(docDir, "math/qedeq_set_theory_v1.xml", "en", genDir);
        generate(docDir, "math/qedeq_set_theory_v1.xml", "de", genDir);
        generate(docDir, "math/qedeq_logic_v1.xml", "en", genDir);
        generate(docDir, "math/qedeq_logic_v1.xml", "de", genDir);
        generate(docDir, "project/qedeq_basic_concept.xml", "en", genDir);
        generate(docDir, "project/qedeq_basic_concept.xml", "de", genDir);
        generate(docDir, "project/qedeq_logic_language.xml", "en", genDir);
        generate(docDir, "project/qedeq_logic_language.xml", "de", genDir);
        generate(docDir, "math/qedeq_sample1.xml", "en", genDir);
        generate(docDir, "math/qedeq_sample1.xml", "de", genDir);
        try {
            generate(new File("."), "data/qedeq_sample2_error.xml", "de",
                new File("."));
            fail("IllegalModuleDataException expected");
        } catch (XmlFilePositionException e) {
            assertTrue(e.getCause() instanceof ModuleDataException);
            assertEquals(217, e.getContext().getLine());
            assertEquals(9, e.getContext().getColumn());
        }
    }

    /**
     * Call the generation of one LaTeX file and copy XML source to same destination directory.
     * @param   dir         Start directory.
     * @param   xml         Relative path to XML file. Must not be <code>null</code>.
     * @param   language    Generate text in this language. Can be <code>null</code>.
     * @param   destinationDirectory Directory path for LaTeX file. Must not be <code>null</code>.
     * @throws  IOException File IO failed.
     * @throws  XmlFilePositionException File data is invalid.
     */
    private static void generate(final File dir, final String xml, final String language,
            final File destinationDirectory) throws IOException, XmlFilePositionException {
        final File xmlFile = new File(dir, xml);
        final File texFile = new File(Xml2Latex.generate(xmlFile.getAbsolutePath(), null, language, 
            "1"));
        final File texCopy = new File(destinationDirectory, new File(new File(xml).getParent(), texFile.getName()).getPath());
        final File xmlCopy = new File(destinationDirectory, xml);
        IoUtility.copyFile(xmlFile, xmlCopy);
        IoUtility.copyFile(texFile, texCopy);
    }
}
