/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2005 Bull S.A.
 * Contact: jonas-team@objectweb.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or 1any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas.jonasadmin.test.jonasserver;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.DifferenceListener;
import org.custommonkey.xmlunit.IgnoreTextAndAttributeValuesDifferenceListener;
import org.custommonkey.xmlunit.XMLTestCase;
import org.custommonkey.xmlunit.XMLUnit;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;


/**
 * Class for testing XML format
 * @author Paul Kemler
 *
 */
public class XMLTest extends XMLTestCase {

    /**
     * Constructor
     * @param name name of the test
     */
    public XMLTest(String name) {
        super(name);
    }

    /**
     * Test server.xml
     * @param file $JONAS_BASE/conf/server.xml
     * @param port port of the added connector
     * @throws Exception if an error occurs
     */
    public void testServerXml(File file, String port) throws Exception {
        String controlConnector = "<Connector " +
                "port=\"value\" " +
                "redirectPort=\"value\" " +
                "minSpareThreads=\"value\" " +
                "connectionTimeout=\"value\" " +
                "maxSpareThreads=\"value\" " +
                "maxThreads=\"value\" " +
                "maxHttpHeaderSize=\"value\">" +
                "</Connector>";
        String controlAddedConnector = "<Connector " +
                "enableLookups=\"value\" " +
                "port=\"value\" " +
                "redirectPort=\"value\" " +
                "acceptCount=\"value\">" +
                "</Connector>";
        String controlEngine = "<Engine " +
                "defaultHost=\"value\" " +
                "name=\"value\">" +
                "<Realm className=\"value\" " +
                "resourceName=\"value\"/>" +
                "<Valve className=\"value\" " +
                "fileDateFormat=\"value\" " +
                "suffix=\"value\"/>" +
                "<Host " +
                "appBase=\"value\" " +
                "autoDeploy=\"value\" " +
                "deployOnStartup=\"value\" " +
                "deployXML=\"false\" " +
                "liveDeploy=\"value\" " +
                "name=\"value\" " +
                "unpackWARs=\"value\">" +
                "</Host>" +
                "</Engine>";
        String controlXml = "<Server>" +
                "<Listener className=\"value\"/>" +
                "<Listener className=\"value\"/>" +
                "<Listener className=\"value\"/>" +
                "<GlobalNamingResources>" +
                "</GlobalNamingResources>" +
                "<Service " +
                "name=\"value\">" +
                controlConnector +
                controlAddedConnector +
                controlEngine +
                "</Service>" +
                "</Server>";
        Document testDocument = XMLUnit.buildTestDocument(new InputSource(new FileReader(file)));
        String testText = "";

        // Transform file into string
        BufferedReader read = new BufferedReader(new FileReader(file));
        String temp = read.readLine();
        while (temp != null) {
            testText += temp + "\n";
            temp = read.readLine();
        }
        read.close();

        // Verify if the new Connector exists
        assertXpathEvaluatesTo("1", "count(//Connector[@port='" + port + "'])", testDocument);

        // Verify the server.xml 'structure'
        XMLUnit.setIgnoreWhitespace(true);
        DifferenceListener diffListener = new IgnoreTextAndAttributeValuesDifferenceListener();
        Diff diff = new Diff(controlXml, testText);
        diff.overrideDifferenceListener(diffListener);

        assertTrue("The control structure:\n" + controlXml + "\nand the server.xml struture:\n" + testText + "\nare not similar: " + diff.toString(), diff.similar());

    }
}
