/*
 * This file is part of the DITA Open Toolkit project.
 * See the accompanying license.txt file for applicable licenses.
 */
package org.dita.dost.platform;

import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
import static org.dita.dost.util.Constants.GEN_CONF_PROPERTIES;
import static org.junit.Assert.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.custommonkey.xmlunit.XMLUnit;

import org.xml.sax.InputSource;

import org.dita.dost.TestUtils;
import org.dita.dost.exception.DITAOTException;
import org.dita.dost.util.Constants;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class IntegratorTest {

    final File resourceDir = TestUtils.getResourceDir(IntegratorTest.class);
    private final File expDir = new File(resourceDir, "exp");
    private File tempDir;

    @Before
    public void setUp() throws IOException, DITAOTException {
        tempDir = TestUtils.createTempDir(getClass());
        TestUtils.copy(new File(resourceDir, "src"), tempDir);
    }

    @Test
    public void testIntegrator() {
        new Integrator();
    }

    @Test
    public void testExecute() throws Exception {
        final File libDir = new File(tempDir, "lib");
        if (!libDir.exists() && !libDir.mkdirs()) {
            throw new IOException("Failed to create directory " + libDir);
        }

        final Integrator i = new Integrator();
        i.setDitaDir(tempDir);
        i.setProperties(new File(tempDir, "integrator.properties"));
        i.execute();

        assertEquals(getProperties(new File(expDir, "lib" + File.separator + Integrator.class.getPackage().getName() + File.separator + Constants.GEN_CONF_PROPERTIES)),
                getProperties(new File(tempDir, "lib" + File.separator + Integrator.class.getPackage().getName() + File.separator + Constants.GEN_CONF_PROPERTIES)));
        TestUtils.resetXMLUnit();
        XMLUnit.setNormalizeWhitespace(true);
        XMLUnit.setIgnoreWhitespace(true);
        XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true);
        assertXMLEqual(new InputSource(new File(expDir, "build.xml").toURI().toString()),
                new InputSource(new File(tempDir, "build.xml").toURI().toString()));
        assertXMLEqual(new InputSource(new File(expDir, "catalog.xml").toURI().toString()),
                new InputSource(new File(tempDir, "catalog.xml").toURI().toString()));
        assertXMLEqual(new InputSource(new File(expDir, "xsl" + File.separator + "shell.xsl").toURI().toString()),
                new InputSource(new File(tempDir, "xsl" + File.separator + "shell.xsl").toURI().toString()));
        assertXMLEqual(new InputSource(new File(expDir, "xsl" + File.separator + "common" + File.separator + "allstrings.xml").toURI().toString()),
                new InputSource(new File(tempDir, "xsl" + File.separator + "common" + File.separator + "allstrings.xml").toURI().toString()));
        assertXMLEqual(new InputSource(new File(expDir, "plugins" + File.separator + "dummy" + File.separator + "xsl" + File.separator + "shell.xsl").toURI().toString()),
                new InputSource(new File(tempDir, "plugins" + File.separator + "dummy" + File.separator + "xsl" + File.separator + "shell.xsl").toURI().toString()));

    }

    private Properties getProperties(final File f) throws IOException {
        final Properties p = new Properties();
        InputStream in = null;
        try {
            in = new FileInputStream(f);
            p.load(in);
        } finally {
            if (in != null) {
                in.close();
            }
        }
        return p;
    }

    @After
    public void tearDown() throws IOException {
        TestUtils.forceDelete(tempDir);
    }

}

@@ -1628,21 +1628,41 @@
     
-assertEquals(
+final Properties expProperties = 
 getP
@@ -1803,16 +1803,320 @@
 ERTIES))
+;%0A        expProperties.setProperty(%22dita.plugin.base.dir%22, new File(tempDir, %22plugins%22 + File.separator + %22base%22).getAbsolutePath());%0A        expProperties.setProperty(%22dita.plugin.dummy.dir%22, new File(tempDir, %22plugins%22 + File.separator + %22dummy%22).getAbsolutePath());%0A        assertEquals(expProperties
 ,%0A      

Fix integration unit tests