/*
 * This file is part of the DITA Open Toolkit project.
 *
 * Copyright 2017 Jarno Elovirta
 *
 *  See the accompanying LICENSE file for applicable license.
 */

package org.dita.dost.module;

import org.dita.dost.TestUtils;
import org.dita.dost.TestUtils.TestLogger;
import org.dita.dost.store.StreamStore;
import org.dita.dost.util.Job;
import org.dita.dost.util.Job.FileInfo.Builder;
import org.dita.dost.util.XMLUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

import static java.net.URI.create;
import static org.dita.dost.util.Constants.OS_NAME;
import static org.dita.dost.util.Constants.OS_NAME_WINDOWS;
import static org.junit.Assert.assertEquals;

public class CleanPreprocessModuleTest {

    private CleanPreprocessModule module;
    private XMLUtils xmlUtils;
    private Job job;

    @Rule
    public TemporaryFolder temporaryFolder = new TemporaryFolder();

    @Before
    public void setUp() throws IOException {
        module = new CleanPreprocessModule();
        xmlUtils = new XMLUtils();
        final File tempDir = temporaryFolder.newFolder();
        job = new Job(tempDir, new StreamStore(tempDir, xmlUtils));
        module.setJob(job);
    }

    @Test
    public void getCommonBase_unix() {
        if (!OS_NAME.toLowerCase().contains(OS_NAME_WINDOWS)) {
            assertEquals(create("file:/foo/bar/"), module.getCommonBase(create("file:/foo/bar/a"), create("file:/foo/bar/b")));
            assertEquals(create("file:/foo/"), module.getCommonBase(create("file:/foo/a"), create("file:/foo/bar/b")));
            assertEquals(create("file:/foo/"), module.getCommonBase(create("file:/foo/bar/a"), create("file:/foo/b")));
            assertEquals(create("file:/foo/"), module.getCommonBase(create("file:/foo/bar/a"), create("file:/foo/baz/b")));
            assertEquals(create("file:/"), module.getCommonBase(create("file:/foo/a/b/c"), create("file:/bar/b/c/d")));
            assertEquals(null, module.getCommonBase(create("file:/foo/bar/a"), create("https://example.com/baz/b")));
        }
    }

    @Test
    public void getCommonBase_windows() {
        if (OS_NAME.toLowerCase().contains(OS_NAME_WINDOWS)) {
            assertEquals(create("file:/F:/bar/"), module.getCommonBase(create("file:/F:/bar/a"), create("file:/F:/bar/b")));
            assertEquals(create("file:/F:/"), module.getCommonBase(create("file:/F:/a"), create("file:/F:/bar/b")));
            assertEquals(create("file:/F:/"), module.getCommonBase(create("file:/F:/bar/a"), create("file:/F:/b")));
            assertEquals(create("file:/F:/"), module.getCommonBase(create("file:/F:/bar/a"), create("file:/F:/baz/b")));
            assertEquals(null, module.getCommonBase(create("file:/C:/a"), create("file:/D:/b")));
            assertEquals(null, module.getCommonBase(create("file:/f:/bar/a"), create("https://example.com/baz/b")));
        }
    }

    @Test
    public void getBaseDir() throws Exception {
        job.setInputDir(URI.create("file:/foo/bar/"));
        job.add(new Builder()
                .uri(create("map.ditamap"))
                .isInput(true)
                .result(create("file:/foo/bar/map.ditamap"))
                .build());
        job.add(new Builder()
                .uri(create("topics/topic.dita"))
                .result(create("file:/foo/bar/topics/topic.dita"))
                .build());
        job.add(new Builder()
                .uri(create("topics/null.dita"))
                .build());
        job.add(new Builder()
                .uri(create("topics/task.dita"))
                .result(create("file:/foo/bar/topics/task.dita"))
                .build());
        job.add(new Builder()
                .uri(create("common/topic.dita"))
                .result(create("file:/foo/bar/common/topic.dita"))
                .build());

        assertEquals(create("file:/foo/bar/"), module.getBaseDir());
    }

    @Test
    public void getBaseDirExternal() throws Exception {
        job.setInputDir(URI.create("file:/foo/bar/"));
        job.add(new Builder()
                .uri(create("map.ditamap"))
                .isInput(true)
                .result(create("file:/foo/bar/map.ditamap"))
                .build());
        job.add(new Builder()
                .uri(create("topics/topic.dita"))
                .result(create("https://example.com/topics/bar/topics/topic.dita"))
                .build());

        assertEquals(create("file:/foo/bar/"), module.getBaseDir());
    }

    @Test
    public void getBaseDirSubdir() throws Exception {
        job.setInputDir(URI.create("file:/foo/bar/maps/"));
        job.add(new Builder()
                .uri(create("maps/map.ditamap"))
                .isInput(true)
                .result(create("file:/foo/bar/maps/map.ditamap"))
                .build());
        job.add(new Builder()
                .uri(create("topics/topic.dita"))
                .result(create("file:/foo/bar/topics/topic.dita"))
                .build());

        assertEquals(create("file:/foo/bar/"), module.getBaseDir());
    }

    @Test
    public void getBaseDirSupdir() throws Exception {
        job.setInputDir(URI.create("file:/foo/bar/maps/"));
        job.add(new Builder()
                .uri(create("maps/map.ditamap"))
                .isInput(true)
                .result(create("file:/foo/bar/maps/map.ditamap"))
                .build());
        job.add(new Builder()
                .uri(create("topics/topic.dita"))
                .result(create("file:/foo/bar/topic.dita"))
                .build());

        assertEquals(create("file:/foo/bar/"), module.getBaseDir());
    }

    @Test(expected = RuntimeException.class)
    public void RewriteRule_WhenStylesheetNotFound_ShouldThrowException() throws Exception {
        module.setJob(job);
        module.setXmlUtils(xmlUtils);
        final TestLogger logger = new TestUtils.TestLogger(false);
        module.setLogger(logger);
        final Map<String, String> input = new HashMap<>();
        input.put("result.rewrite-rule.xsl", "abc.xsl");

        module.execute(input);
    }

    @Test
    public void RewriteRule() throws Exception {
        job.setInputDir(URI.create("file:/foo/bar/"));
        job.add(new Builder()
                .uri(create("map.ditamap"))
                .isInput(true)
                .result(create("file:/foo/bar/map.ditamap"))
                .build());
        job.add(new Builder()
                .uri(create("topics/topic.dita"))
                .result(create("file:/foo/bar/topics/topic.dita"))
                .build());
        module.setJob(job);
        module.setXmlUtils(xmlUtils);
        final TestLogger logger = new TestUtils.TestLogger(false);
        module.setLogger(logger);
        final Map<String, String> input = new HashMap<>();
        input.put("use-result-filename", "true");

        module.execute(input);
    }
}
@@ -6210,804 +6210,8 @@
 );%0A%0A
-        module.execute(input);%0A    %7D%0A%0A    @Test%0A    public void RewriteRule() throws Exception %7B%0A        job.setInputDir(URI.create(%22file:/foo/bar/%22));%0A        job.add(new Builder()%0A                .uri(create(%22map.ditamap%22))%0A                .isInput(true)%0A                .result(create(%22file:/foo/bar/map.ditamap%22))%0A                .build());%0A        job.add(new Builder()%0A                .uri(create(%22topics/topic.dita%22))%0A                .result(create(%22file:/foo/bar/topics/topic.dita%22))%0A                .build());%0A        module.setJob(job);%0A        module.setXmlUtils(xmlUtils);%0A        final TestLogger logger = new TestUtils.TestLogger(false);%0A        module.setLogger(logger);%0A        final Map%3CString, String%3E input = new HashMap%3C%3E();%0A        input.put(%22use-result-filename%22, %22true%22);%0A%0A
     

Remove debug code from test