/* This file is part of VoltDB.
 * Copyright (C) 2008-2015 VoltDB Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

package org.voltdb.regressionsuites;

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

import org.voltdb.DefaultSnapshotDataTarget;


public class SaveRestoreBase extends RegressionSuite {
    protected static final String TMPDIR = "/tmp/" + System.getProperty("user.name");
    protected static final String TESTNONCE = "testnonce";
    protected static final String JAR_NAME = "sysproc-threesites.jar";

    public SaveRestoreBase(String s) {
        super(s);
    }

    @Override
    public void setUp() throws Exception
    {
        setUp(TESTNONCE);
    }

    public void setUp(String nonce) throws Exception
    {
        File tempDir = new File(TMPDIR);
        if (!tempDir.exists()) {
            assertTrue(tempDir.mkdirs());
        }
        deleteTestFiles(nonce);
        super.setUp();
        DefaultSnapshotDataTarget.m_simulateFullDiskWritingChunk = false;
        DefaultSnapshotDataTarget.m_simulateFullDiskWritingHeader = false;
        org.voltdb.sysprocs.SnapshotRegistry.clear();
    }

    @Override
    public void tearDown() throws Exception
    {
        tearDown(TESTNONCE);
    }

    public void tearDown(String nonce) throws Exception
    {
        super.tearDown();
        deleteTestFiles(nonce);
        System.gc();
        System.runFinalization();
    }

    private void deleteRecursively(File f) {
        if (f.isDirectory()) {
            for (File f2 : f.listFiles()) {
                deleteRecursively(f2);
            }
            boolean deleted = f.delete();
            if (!deleted) {
                if (!f.exists()) return;
                System.err.println("Couldn't delete " + f.getPath());
                System.err.println("Remaining files are:");
                for (File f2 : f.listFiles()) {
                    System.err.println("    " + f2.getPath());
                }
                //Recurse until stack overflow trying to delete, y not rite?
                deleteRecursively(f);
            }
        } else  {
            boolean deleted = f.delete();
            if (!deleted) {
                if (!f.exists()) return;
                System.err.println("Couldn't delete " + f.getPath());
            }
            assertTrue(deleted);
        }
    }

    protected void deleteTestFiles(final String nonce)
    {
        FilenameFilter cleaner = new FilenameFilter()
        {
            @Override
            public boolean accept(File dir, String file)
            {
                return file.startsWith(nonce) ||
                file.endsWith(".vpt") ||
                file.endsWith(".digest") ||
                file.endsWith(".tsv") ||
                file.endsWith(".csv") ||
                file.endsWith(".incomplete") ||
                new File(dir, file).isDirectory();
            }
        };

        File tmp_dir = new File(TMPDIR);
        File[] tmp_files = tmp_dir.listFiles(cleaner);
        for (File tmp_file : tmp_files)
        {
            deleteRecursively(tmp_file);
        }
    }
}

@@ -1220,52 +1220,188 @@
 er;%0A
-%0Aimport org.voltdb.DefaultSnapshotDataTarget
+import java.util.ArrayList;%0Aimport java.util.List;%0A%0Aimport org.voltdb.DefaultSnapshotDataTarget;%0Aimport org.voltdb.types.GeographyPointValue;%0Aimport org.voltdb.types.GeographyValue
 ;%0A%0A%0A
@@ -4258,11 +4258,939 @@
  %7D%0A    %7D
+%0A%0A    static protected GeographyPointValue getPoint(int index) %7B%0A        double lat = index %25 90;%0A        double lng = (index * 2) %25 180;%0A        if ((index & 0x1) != 0) %7B%0A            lat *= -1.0;%0A        %7D%0A%0A        if ((index & 0x2) != 0) %7B%0A            lng *= -1.0;%0A        %7D%0A%0A        return new GeographyPointValue(lng, lat);%0A    %7D%0A%0A    static protected GeographyValue getGeography(int index) %7B%0A        List%3CGeographyPointValue%3E loop = new ArrayList%3CGeographyPointValue%3E();%0A        for (int i = 0; i %3C 3; ++i) %7B%0A            loop.add(getPoint(index + i));%0A        %7D%0A        loop.add(loop.get(0));%0A%0A        List%3CList%3CGeographyPointValue%3E%3E loops = new ArrayList%3C%3E();%0A        loops.add(loop);%0A%0A        // It would be nice to test multiple loops here, but to do it right%0A        // would require that the inner loops don't cross the outer one,%0A        // which is not trivial to do.%0A%0A        return new GeographyValue(loops);%0A    %7D
 %0A%7D%0A

Update test to work with new master commits