package org.schoellerfamily.gedbrowser.writer.test;

import static org.junit.Assert.assertTrue;

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

import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.schoellerfamily.gedbrowser.datamodel.Root;
import org.schoellerfamily.gedbrowser.reader.AbstractGedLine;
import org.schoellerfamily.gedbrowser.reader.GedObjectCreator;
import org.schoellerfamily.gedbrowser.reader.testreader.TestResourceReader;
import org.schoellerfamily.gedbrowser.writer.GedWriter;

/**
 * @author Dick Schoeller
 */
public class GedWriterTest {
    /**
     * The file name to use in the test.
     */
    private static final String FILE_NAME =
            "/var/lib/gedbrowser/mini-schoeller.ged";
// Tests can be done with these others.
//            "/var/lib/gedbrowser/schoeller.ged";
//            "gl120368.ged";

    /** Logger. */
    private final transient Log logger = LogFactory.getLog(getClass());

    /** */
    private Root root;

    /** */
    private String filename;

    /**
     * @throws IOException if there are problems reading the data file
     */
    @Before
    public void setUp() throws IOException {
        final AbstractGedLine top = readFileTestSource();
        final GedObjectCreator g2g = new GedObjectCreator();
        root = g2g.create(top);
        logger.info("dbName: " + root.getDbName());
        filename = root.getDbName() + ".ged";
        root.setFilename("/tmp/" + filename);
        cleanTemp(filename);
    }

    /**
     * @throws IOException if file IO fails
     */
    @Test
    public void test() throws IOException {
        final GedWriter writer = new GedWriter(root);
        writer.write();
        final GedWriter writer1 = new GedWriter(root);
        writer1.write();
        final GedWriter writer2 = new GedWriter(root);
        writer2.write();
        assertSuccess();
    }

    /**
     * @throws IOException if there are problems reading or writing files
     */
    private void assertSuccess() throws IOException {
        logger.info("originalFilename: " + FILE_NAME);
        logger.info("filename: " + filename);
        final File original = new File(FILE_NAME);
        final File result = new File("/tmp/" + filename);
        assertTrue("File content should match",
                FileUtils.contentEquals(original, result));
        final File backup1 = new File("/tmp/" + filename + ".1");
        assertTrue("Backup should exist", backup1.exists());
        final File backup2 = new File("/tmp/" + filename + ".2");
        assertTrue("Backup should exist", backup2.exists());
    }

    /** */
    @After
    public void cleanUp() {
        cleanTemp(filename);
    }

    /**
     * Clean up the files that might be sitting in the temp directory.
     *
     * @param baseFilename the filename that we'll be doing stuff with
     */
    private void cleanTemp(final String baseFilename) {
        final File folder = new File("/tmp");
        final File[] files = folder.listFiles(new FilenameFilter() {
            /**
             * {@inheritDoc}
             */
            @Override
            public boolean accept(final File dir, final String name) {
                return name.matches(baseFilename + ".*");
            }
        });
        for (final File delFile : files) {
            if (!delFile.delete()) {
                logger.error("Can't delete file " + delFile.getName());
            }
        }
    }

    /**
     * Read data for tests available to prepare data for tests.
     *
     * @return a populated GedLine parse tree.
     * @throws IOException because reader might throw.
     */
    private static AbstractGedLine readFileTestSource() throws IOException {
        return TestResourceReader.readFileTestSource(
                "", FILE_NAME);
    }
}

@@ -368,16 +368,49 @@
 t.Test;%0A
+import org.junit.runner.RunWith;%0A
 import o
@@ -580,16 +580,85 @@
 reator;%0A
+import org.schoellerfamily.gedbrowser.reader.test.TestConfiguration;%0A
 import o
@@ -785,346 +785,699 @@
 er;%0A
-%0A/**%0A * @author Dick Schoeller%0A */%0Apublic class GedWriterTest %7B%0A    /**%0A     * The file name to use in the test.%0A     */%0A    private static final String FILE_NAME =%0A            %22/var/lib/gedbrowser/mini-schoeller.ged%22;%0A// Tests can be done with these others.%0A//            %22/var/lib/gedbrowser/schoeller.ged%22;%0A//
+import org.springframework.beans.factory.annotation.Autowired;%0Aimport org.springframework.beans.factory.annotation.Value;%0Aimport org.springframework.test.context.ContextConfiguration;%0Aimport org.springframework.test.context.junit4.SpringJUnit4ClassRunner;%0A%0A/**%0A * @author Dick Schoeller%0A */%0A@RunWith(SpringJUnit4ClassRunner.class)%0A@ContextConfiguration(classes = %7B TestConfiguration.class %7D)%0Apublic class GedWriterTest %7B%0A    /** */%0A    @Autowired%0A    private transient GedObjectCreator g2g;%0A%0A    /** */%0A    @Value(%22$%7Bgedbrowser.home:/var/lib/gedbrowser%7D%22)%0A    private transient String gedbrowserHome;%0A%0A    /**%0A     * The file name to use in the test.%0A
      
+*/%0A
     
-   %22gl120368.ged%22
+private String inputFilename
 ;%0A%0A 
@@ -1799,115 +1799,118 @@
     
-f
 in
-al AbstractGedLine top = readFileTestSource();%0A        final GedObjectCreator g2g = new GedObjectCreator
+putFilename = gedbrowserHome + %22/mini-schoeller.ged%22;%0A        final AbstractGedLine top = readFileTestSource
 ();%0A
@@ -2692,25 +2692,29 @@
 me: %22 + 
-FILE_NAME
+inputFilename
 );%0A     
@@ -2793,25 +2793,29 @@
 ew File(
-FILE_NAME
+inputFilename
 );%0A     
@@ -4280,23 +4280,16 @@
 private 
-static 
 Abstract
@@ -4416,17 +4416,21 @@
 %22%22, 
-FILE_NAME
+inputFilename
 );%0A 

make tests work on travis