RepositoryEncCompatibilityTest.java Source code

Java tutorial

Introduction

Here is the source code for RepositoryEncCompatibilityTest.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.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
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 RepositoryEncCompatibilityTest {

    private static final String REPO_TST = "encCmpRepo.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> argList;

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

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

    @Before
    public void setUp() throws Exception {
        argList = new ArrayList<>();
        argList.add(new File(DATA_TST).getAbsolutePath());

    }

    @Test
    public void testMetaPasswdBackupRestoreTo() throws Exception {
        checkRestoreTo("datapass", "metapass", argList);
    }

    private static void checkRestoreTo(final String passwordData, final String passwordMeta, 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);

        RestoreService restore = new RestoreService("encCmpRepo_2013-12-17_08-45-57", repository, argList,
                RESTORE_TST);
        restore.run();

        // check new dir content
        List<String> contentRestore = getContentDir(RESTORE_TST, true);
        printContent(contentRestore);
        Assert.assertTrue(contentRestore.size() == 6);
        Assert.assertTrue(CollectionUtils.isSubCollection(getContentRestorePatern(), contentRestore));
    }

    private static Collection<String> getContentRestorePatern() {
        final List<String> result = new ArrayList<>();
        result.add("restore.tst;true;0");
        result.add("home;true;0");
        result.add("martin;true;0");
        result.add("bar;true;0");
        result.add("data.tst;true;0");
        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;
    }
}