Package de.grey.ownsync.junit.utils

Source Code of de.grey.ownsync.junit.utils.OwnSyncTestCase

package de.grey.ownsync.junit.utils;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Properties;

import junit.framework.TestCase;

import org.apache.commons.io.FileUtils;

import de.grey.ownsync.OwnSyncStarter;
import de.grey.ownsync.config.OwnSyncConfiguration;
import de.grey.ownsync.exception.OwnSyncException;

/**
* @author wogy
*/

public class OwnSyncTestCase extends TestCase
{
    private static String dataFolderName = "JUnitTestData";
   
    private static String originalDataFolderName = "1Original";
    private static String testDataFolderName = "2Test";
    private static String expectedDataFolderName = "3Expected";
   
    private static String folderAName = "Folder_A";
    private static String folderBName = "Folder_B";
    private static String trashboxAName = folderAName+"_Trashbox";
    private static String trashboxBName = folderBName+"_Trashbox";
    private static String oldStateAName = folderAName+"_OldState.xml";
    private static String oldStateBName = folderBName+"_OldState.xml";
   
    private File rootfolder = new File("./../ownsync JUnits");
   
    public void internalTest(String folderName)
    {
        File folderOfOriginal = new File(rootfolder, dataFolderName+"/"+originalDataFolderName+"/"+folderName+"/");
        File folderOfCopy = new File(rootfolder, dataFolderName+"/"+testDataFolderName+"/"+folderName+"/");
        File folderOfResult = new File(rootfolder, dataFolderName+"/"+expectedDataFolderName+"/"+folderName+"/");
       
        cloneFilesystem(folderOfOriginal, folderOfCopy);
        OwnSyncConfiguration config = createOwnSyncConfiguration(folderOfCopy);
        startOwnSync(config);
       
        // Validate
        compareFilesystems(folderOfResult, folderOfCopy);
    }
   
    private void cloneFilesystem(File folderOfOriginal, File folderOfCopy)
    {
        try
        {
            FileUtils.cleanDirectory(folderOfCopy);
            FileUtils.copyDirectory(folderOfOriginal, folderOfCopy, true);
            EqualizeLastModifiedDate.fixLastModified(folderOfOriginal, folderOfCopy);
//            compareFilesystems(folderOfOriginal, folderOfCopy);
        }
        catch (IOException e)
        {
            e.printStackTrace();
            fail();
        }
    }
   
    private OwnSyncConfiguration createOwnSyncConfiguration(File folder)
    {
        try
        {
            Properties props = new Properties();
           
            props.put(OwnSyncConfiguration.SyncFolderLogFile, folder.getAbsolutePath()+"/global.log");
            props.put(OwnSyncConfiguration.TestModus, "true");
           
            props.put(OwnSyncConfiguration.SyncFolderA, folder.getAbsolutePath()+"/"+folderAName);
            props.put(OwnSyncConfiguration.SyncFolderStateA, folder.getAbsolutePath()+"/"+oldStateAName);
            props.put(OwnSyncConfiguration.WastbasketFolderA, folder.getAbsolutePath()+"/"+trashboxAName);
            props.put(OwnSyncConfiguration.KeepLastFileVersionA, "true");
            props.put(OwnSyncConfiguration.SyncExclusionRegExpPatternsA, "");

            props.put(OwnSyncConfiguration.SyncFolderB, folder.getAbsolutePath()+"/"+folderBName);
            props.put(OwnSyncConfiguration.SyncFolderStateB, folder.getAbsolutePath()+"/"+oldStateBName);
            props.put(OwnSyncConfiguration.WastbasketFolderB, folder.getAbsolutePath()+"/"+trashboxBName);
            props.put(OwnSyncConfiguration.KeepLastFileVersionB, "true");
            props.put(OwnSyncConfiguration.SyncExclusionRegExpPatternsB, "");
           
            return new OwnSyncConfiguration(props);
        }
        catch (OwnSyncException e)
        {
            e.printStackTrace();
            fail();
        }
        return null;
    }
   
    private void startOwnSync(OwnSyncConfiguration configuration)
    {
        try
        {
            OwnSyncStarter starter = new OwnSyncStarter(configuration);
            starter.sync();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            fail();
        }
    }
   
    private void compareFilesystems(File folderOfResult, File folderOfCopy)
    {
        compareFolder(new File(folderOfResult, "/"+folderAName), new File(folderOfCopy, "/"+folderAName), false);
        compareFolder(new File(folderOfResult, "/"+folderBName), new File(folderOfCopy, "/"+folderBName), false);
        compareFolder(new File(folderOfResult, "/"+trashboxAName), new File(folderOfCopy, "/"+trashboxAName), true);
        compareFolder(new File(folderOfResult, "/"+trashboxBName), new File(folderOfCopy, "/"+trashboxBName), true);
    }

    private void compareFolder(File folderResult, File folderCopy, boolean ignoreFolderDate)
    {
        // Compare the size of the directories
        compareFolderSize(folderResult, folderCopy, ignoreFolderDate);

        // Compare the files and dates of the directories
        compareFile(folderResult, folderCopy, ignoreFolderDate);
    }
   
    private void compareFolderSize(File folderResult, File folderCopy, boolean ignoreFolderDate)
    {
        if (!ignoreFolderDate)
            assertEquals(FileUtils.sizeOfDirectory(folderResult), FileUtils.sizeOfDirectory(folderCopy));
    }
   
    private void compareFile(File folderResult, File folderCopy, boolean ignoreFolderDate)
    {
        assertEquals(folderResult.isFile(), folderCopy.isFile());
        if (!ignoreFolderDate && folderResult.isDirectory() && folderCopy.isDirectory())
            assertEquals(folderResult.lastModified(), folderCopy.lastModified());
        if (folderResult.isFile() && folderCopy.isFile())
        {
            assertEquals(folderResult.length(), folderCopy.length());
        }
        else
        {
            File[] filesResult = folderResult.listFiles();
            File[] filesCopy = folderCopy.listFiles();
            assertEquals(filesResult.length, filesCopy.length);
           
            Arrays.sort(filesResult);
            Arrays.sort(filesResult);
           
            for (int i = 0; i < filesResult.length; i++)
            {
                compareFile(filesResult[i], filesCopy[i], ignoreFolderDate);
            }   
        }
    }
}
TOP

Related Classes of de.grey.ownsync.junit.utils.OwnSyncTestCase

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.