BackupRestoreTest.java Source code

Java tutorial

Introduction

Here is the source code for BackupRestoreTest.java

Source

/*
This softtware use BSD licence (http://opensource.org/licenses/BSD-3-Clause)
    
Copyright (c) 2013, Martin Lebeda
All rights reserved.
    
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
    
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
    
3. Neither the name of the Martin Lebeda nor the names of its contributors may
be used to endorse or promote products derived from this software without
specific prior written permission.
    
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
 * @author <a href="mailto:martin.lebeda@gmail.com">Martin Lebeda</a>
 *         Date: 15.11.13
 */
public class BackupRestoreTest {

    private static final String REPO_TST = "repo.tst";
    private static final String RESTORE_TST = "restore.tst";
    private static final String ID_BCK_TST = "tst";
    private static final String DATA_TST = "data.tst";
    private List<String> dataContent;
    private List<String> argList;

    // Working Directory = C:\data\Projekty.pub\bas

    @After
    public void tearDown() throws Exception {
        File repo = new File(REPO_TST);
        if (repo.exists()) {
            FileUtils.deleteDirectory(repo);
        }

        File restore = new File(RESTORE_TST);
        if (restore.exists()) {
            FileUtils.deleteDirectory(restore);
        }

        FileUtils.forceDelete(Paths.get(DATA_TST, "datLink.txt").toFile());
        FileUtils.forceDelete(Paths.get(DATA_TST, "dir3").toFile());
    }

    @Before
    public void setUp() throws Exception {
        Files.createSymbolicLink(Paths.get(DATA_TST, "datLink.txt"), Paths.get("data2.txt"));
        Files.createSymbolicLink(Paths.get(DATA_TST, "dir3"), Paths.get("dir1"));

        argList = new ArrayList<>();
        argList.add(new File(DATA_TST).getAbsolutePath());

        dataContent = getContentDir(DATA_TST, true);
    }

    @Test
    public void testBaseBackupRestoreTo() throws Exception {
        checkRestoreTo(null, null, getRepoPatern(), argList);
    }

    @Test
    public void testDataPasswdBackupRestoreTo() throws Exception {
        checkRestoreTo("data", null, getRepoPaternEnc(), argList);
    }

    @Test
    public void testMetaPasswdBackupRestoreTo() throws Exception {
        checkRestoreTo("data", "meta", getRepoPaternEnc(), argList);
    }

    private static void checkRestoreTo(final String passwordData, final String passwordMeta,
            final Collection<String> repoPatern, List<String> argList) throws Exception {
        CipherProvider dataCipherProvider = new CipherProvider();
        dataCipherProvider.setPassword(passwordData);
        CipherProvider metaCipherProvider = new CipherProvider();
        metaCipherProvider.setPassword(passwordMeta);

        String repoPathStr = REPO_TST;
        Repository repository = new Repository(repoPathStr, dataCipherProvider, metaCipherProvider);

        VOConfig cfg = new VOConfig(REPO_TST, ID_BCK_TST, getExcl("*.excl"), getExcl("*Excl"), argList);

        BackupService backup = new BackupService(cfg, repository);
        final String bck = backup.run();

        // check repo content
        List<String> contentRepo = getContentDir(REPO_TST + File.separator + "data", false);
        printContent(contentRepo);
        checkCollectionIn(repoPatern, contentRepo);

        RestoreService restore = new RestoreService(bck, repository, argList, RESTORE_TST);
        restore.run();

        // check new dir content
        List<String> contentRestore = getContentDir(RESTORE_TST, false);
        // TODO Lebeda - do check with date - on source tst files must be dat prepared on setup test
        printContent(contentRestore);
        Assert.assertEquals(16, contentRestore.size());
        Assert.assertTrue(0 == CollectionUtils.countMatches(contentRestore, new Predicate() {
            @Override
            public boolean evaluate(final Object object) {
                String s = (String) object;
                return StringUtils.contains(s, "excl");
            }
        }));
        Assert.assertTrue(CollectionUtils.isSubCollection(getContentRestorePatern(), contentRestore));
    }

    private static void checkCollectionIn(final Collection<String> repoPatern, final List<String> contentRepo) {
        Set<String> contentRepoSet = new HashSet<>(contentRepo);
        for (String s : repoPatern) {
            Assert.assertTrue("Not found element: '" + s + "'", contentRepoSet.contains(s));
        }
    }

    private static Collection<String> getContentRestorePatern() {
        final List<String> result = new ArrayList<>();
        // TODO Lebeda - uncomment after check with date
        //        result.add("data.tst;true;0");
        //        result.add("data.tst.zip;1384892367389;false;244;06e09bb6af7dd09204c58eb9f500efa9");
        //        result.add("data1.txt;1384887301019;false;20;23e0f4f35f51fc106b9072a87ecebe26");
        //        result.add("data2.txt;1384887315773;false;20;a815a7d70235d2114b4c5c289a325859");
        //        result.add("dir1;true;0");
        //        result.add("data3.txt;1384887351364;false;16;142f908013f97fc2ef8964387c9b67df");

        result.add("data.tst.zip;false;244;06e09bb6af7dd09204c58eb9f500efa9");
        result.add("data1.txt;false;20;23e0f4f35f51fc106b9072a87ecebe26");
        result.add("data2.txt;false;20;a815a7d70235d2114b4c5c289a325859");
        result.add("dir1;true;0");
        result.add("data3.txt;false;16;142f908013f97fc2ef8964387c9b67df");

        return result;
    }

    private static List<String> getExcl(final String... excl) {
        final List<String> result = new ArrayList<>();
        for (String ex : excl) {
            result.add(ex);
        }
        return result;
    }

    private static Collection<String> getRepoPatern() {
        List<String> result = new ArrayList<>();

        //        result.add("data;true;0");
        //        result.add("0;true;0");
        //        result.add("6;true;0");
        //        result.add("06e09bb6af7dd09204c58eb9f500efa9_244;false;244;06e09bb6af7dd09204c58eb9f500efa9");
        //        result.add("1;true;0");
        //        result.add("4;true;0");
        //        result.add("142f908013f97fc2ef8964387c9b67df_16.bz2;false;52;e887cd782b802330da0330968791523e");
        //        result.add("2;true;0");
        //        result.add("3;true;0");
        //        result.add("23e0f4f35f51fc106b9072a87ecebe26_20.bz2;false;52;ce56bb92dc5446ec721f9078b5603780");
        //        result.add("a;true;0");
        //        result.add("8;true;0");
        //        result.add("a815a7d70235d2114b4c5c289a325859_20.bz2;false;53;e26a53c0976ba07e271b0546f456e23e");
        //
        //        result.add("06e09bb6af7dd09204c58eb9f500efa9_244.md5;false;32;77d88f5b3b65c9cb2ae78d7f9516ed25");
        //        result.add("142f908013f97fc2ef8964387c9b67df_16.bz2.md5;false;32;90d6a76f43f91e8ccac980dc956d6252");
        //        result.add("23e0f4f35f51fc106b9072a87ecebe26_20.bz2.md5;false;32;e3998b0cdeb172eb5b5fba712c0864f7");
        //        result.add("a815a7d70235d2114b4c5c289a325859_20.bz2.md5;false;32;8e5597dfcb102c5d70ec6b82c43ee8ea");

        return result;
    }

    private static Collection<String> getRepoPaternEnc() {
        List<String> result = new ArrayList<>();

        return result;
    }

    // only for development
    private static void printContent(List<String> contentRepo) {
        for (String s : contentRepo) {
            System.out.println("result.add(\"" + s + "\");");
        }
    }

    private static List<String> getContentDir(String tstPath, boolean checkTime) throws Exception {
        List<String> result = new ArrayList<>();

        for (File file : FileUtils.listFilesAndDirs(new File(tstPath), FileFilterUtils.trueFileFilter(),
                FileFilterUtils.trueFileFilter())) {

            List<String> loc = new ArrayList<>();
            loc.add(file.getName());
            if (checkTime && !file.isDirectory()) {
                loc.add(String.valueOf(file.lastModified()));
            }
            loc.add(String.valueOf(file.isDirectory()));
            loc.add(String.valueOf((file.isDirectory() ? 0 : file.length())));
            if (!file.isDirectory()) {
                loc.add(Repository.getMd5Sum(file.getAbsolutePath()));
            }
            result.add(StringUtils.join(loc, ";"));
        }

        return result;
    }
}