com.leverno.ysbos.harvest.HarvesterTest.java Source code

Java tutorial

Introduction

Here is the source code for com.leverno.ysbos.harvest.HarvesterTest.java

Source

/**
 *     Copyright (C) 2012  Werner van Rensburg
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.leverno.ysbos.harvest;

import java.io.File;
import java.io.IOException;
import java.nio.file.NotDirectoryException;
import java.util.Collection;

import junit.framework.TestCase;
import org.apache.commons.io.FileUtils;

public class HarvesterTest extends TestCase {

    private String rootFolder;
    private String subFolder;
    private File fileOne;
    private File fileTwo;

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        createTestStructure();
    }

    @Override
    protected void tearDown() throws Exception {
        FileUtils.deleteDirectory(new File(rootFolder));

        super.tearDown();
    }

    public void testGetFilesFor() throws Exception {
        FileHarvester harvester = new FileHarvester();
        Collection<File> files = harvester.getFilesFor(rootFolder);

        assertNotNull(files);
        assertEquals(2, files.size());
        assertTrue(files.contains(fileOne));
        assertTrue(files.contains(fileTwo));
    }

    public void testNoDirectory() {
        FileHarvester harvester = new FileHarvester();

        //non existing path
        try {
            harvester.getFilesFor("/not/a/directory");
            fail();
        } catch (NotDirectoryException e) {
        }

        //path to a file node
        try {
            harvester.getFilesFor(fileTwo.getAbsolutePath());
            fail();
        } catch (NotDirectoryException e) {
        }
    }

    private void createTestStructure() {
        rootFolder = new String("/tmp/archiverTest/");
        subFolder = String.format("%s/sub", rootFolder);
        fileOne = new File(String.format("%s/one", rootFolder));
        fileTwo = new File(String.format("%s/two", subFolder));

        try {
            FileUtils.forceMkdir(new File(subFolder));
            FileUtils.touch(fileOne);
            FileUtils.touch(fileTwo);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}