/* $Id: QedeqBoFactoryTest.java,v 1.13 2005/10/13 03:37:54 m31 Exp $
 *
 * This file is part of the project "Hilbert II" - http://www.qedeq.org
 *
 * Copyright 2000-2005,  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.bo.control;

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

import javax.xml.parsers.ParserConfigurationException;

import org.qedeq.kernel.base.module.Author;
import org.qedeq.kernel.base.module.Qedeq;
import org.qedeq.kernel.log.Trace;
import org.qedeq.kernel.test.DynamicGetter;
import org.qedeq.kernel.test.QedeqTestCase;
import org.qedeq.kernel.xml.handler.module.QedeqHandler;
import org.qedeq.kernel.xml.mapper.Context2XPath;
import org.qedeq.kernel.xml.parser.SaxDefaultHandler;
import org.qedeq.kernel.xml.parser.SaxParser;
import org.qedeq.kernel.xml.tracker.SimpleXPath;
import org.qedeq.kernel.xml.tracker.XPathLocationFinder;
import org.xml.sax.SAXException;

/**
 * For testing qedeq generation.
 *
 * @version $Revision: 1.13 $
 * @author Michael Meyling
 */
public class QedeqBoFactoryTest extends QedeqTestCase {

    private Qedeq ok;

    private Qedeq error;

    private static File okFile = new File("data/qedeq_sample2_error.xml");

    private static File errorFile = new File("data/qedeq_sample2_error.xml");

/*        
    private final void initializeTrace() {
        final File traceFile = new File(TRACE_FILE_PATH);
        IoUtility.createNecessaryDirectories(traceFile);
        try {
            Trace.setPrintStream(new PrintStream(new FileOutputStream(traceFile)));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
*/        

    protected void setUp() throws Exception {
        ok = (Qedeq) QedeqProxy.createProxy(createQedeqFromFile(okFile));
        ok.getHeader().getAuthorList().get(0);
        assertTrue(DynamicGetter.get(ok, "getHeader().getAuthorList().get(0)") instanceof Author);
//TODO        ok = (Qedeq) QedeqProxy.createProxy(ok);
        error = (Qedeq) QedeqProxy.createProxy(createQedeqFromFile(errorFile));
//TODO        error = createQedeqFromFile(errorFile);
    }

    protected void tearDown() throws Exception {
        super.tearDown();
        ok = null;
        error = null;
    }

    private final Qedeq createQedeqFromFile(final File file) throws ParserConfigurationException,
            SAXException, IOException {
        SaxDefaultHandler handler = new SaxDefaultHandler();
        QedeqHandler simple = new QedeqHandler(handler);
        handler.setBasisDocumentHandler(simple);
        SaxParser parser = new SaxParser(handler);
        parser.parse(file);
        return simple.getQedeq();
    }

    /**
     * Class under test for QedeqBo create(String, Qedeq).
     * 
     * @throws IOException  Module creation failed due to IO error.
     * @throws SAXException Module parsing failed.
     * @throws ParserConfigurationException Parser configuration problem.
     */
    public final void testCreateStringQedeq1() throws IOException, ParserConfigurationException, SAXException {
        final String method = "testCreateStringQedeq()";
        try {
            QedeqBoFactoryAssert.createQedeq(errorFile.getCanonicalPath(), error);
            fail("IllegalModuleDataException expected");
        } catch (IllegalModuleDataException e) {
            System.err.println(e);
            System.err.println(e.getContext());
            final String file = Context2XPath.getFileName(e.getContext());
            String xpath = Context2XPath.getXPath(e.getContext());
            final SimpleXPath find = XPathLocationFinder.getXPathLocation(file, xpath);
            System.out.println("found: " + find.getStartLocation());
            System.out.println("found: " + find.getEndLocation());
            assertEquals(217, find.getStartLocation().getLine());
            assertEquals(9, find.getStartLocation().getColumn());
            assertEquals(261, find.getEndLocation().getLine());
            assertEquals(16, find.getEndLocation().getColumn());
            Trace.trace(this, method, e);
        }
    }

    /**
     * Class under test for QedeqBo createQedeq(String, Qedeq).
     * 
     * @throws Exception    Unexpected failure of module creation. 
     */
    public final void testCreateStringQedeq2() throws Exception {
        final File qedeqFile = getQedeqFile("project/qedeq_basic_concept.xml");
        QedeqBoFactoryAssert.createQedeq(qedeqFile.getAbsolutePath(), createQedeqFromFile(qedeqFile));
    }

    /**
     * Class under test for QedeqBo createQedeq(String, Qedeq).
     * 
     * @throws Exception    Unexpected failure of module creation. 
     */
    public final void testCreateStringQedeq3() throws Exception {
        final File qedeqFile = getQedeqFile("math/qedeq_sample1.xml");
        QedeqBoFactoryAssert.createQedeq(qedeqFile.getAbsolutePath(), createQedeqFromFile(qedeqFile));
    }
    
    /**
     * Get qedeq file.
     * 
     * @param   relativePath    File path relative to documentation directory. 
     * 
     * @return  Path to file.
     * @throws  IOException IO-Failure.
     */
    private final File getQedeqFile(final String relativePath) throws IOException {
        File docDir = new File("../QedeqDoc");
        // test if we are in the normal development enviroment, 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");
            if (!docDir.exists()) {
                throw new IOException("unknown source directory for qedeq modules");
            }
        }
        final File qedeqFile = new File(docDir, relativePath);
        return qedeqFile;
    }

}
