com.leverno.ysbos.item.dao.ItemStoreTest.java Source code

Java tutorial

Introduction

Here is the source code for com.leverno.ysbos.item.dao.ItemStoreTest.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.item.dao;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.leverno.ysbos.common.Predicate;
import com.leverno.ysbos.item.dao.ItemStore;
import com.leverno.ysbos.item.dao.NoBackupPredicate;
import com.leverno.ysbos.item.domain.Item;

import junit.framework.TestCase;

import org.apache.commons.io.FileUtils;

public class ItemStoreTest extends TestCase {

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

    public void setUp() throws Exception {
        super.setUp();

        createTestStructure();
    }

    public void tearDown() throws Exception {
        FileUtils.deleteDirectory(new File(rootFolder));

        clearItemStore();

        super.tearDown();
    }

    private void clearItemStore() {
        ItemStore.getInstance()._clear();
    }

    public void testAddItem() {
        //Given
        ItemStore itemStore = ItemStore.getInstance();
        itemStore._clear();

        assertNull(itemStore.getItem(fileOne));

        //When
        Item original = Item.createAndStoreItem(fileOne.getAbsolutePath());

        //Then
        Item lookedup = itemStore.getItem(fileOne);
        assertNotNull(lookedup);
        assertEquals(original, lookedup);
        assertEquals(fileOne.getAbsolutePath(), lookedup.getOriginalPath());
    }

    public void testAddAll() {
        //Given
        ItemStore itemStore = ItemStore.getInstance();

        assertNull(itemStore.getItem(fileOne));

        //When
        List<Item> list = new ArrayList<Item>();
        list.add(Item.createItem(fileOne.getAbsolutePath()));
        list.add(Item.createItem(fileTwo.getAbsolutePath()));

        //And
        ItemStore.getInstance().addAll(list);

        //Then
        List<Item> stored = itemStore.findAll();
        assertNotNull(stored);
        assertEquals(2, stored.size());
    }

    public void testAddDuplicate() {
        //Given
        ItemStore itemStore = ItemStore.getInstance();

        assertNotNull(itemStore.findAll());
        assertEquals(0, itemStore.findAll().size());

        //When
        Item.createAndStoreItem(fileOne.getAbsolutePath());
        Item duplicate = Item.createAndStoreItem(fileOne.getAbsolutePath());

        //Then
        assertNull(duplicate);
        assertEquals(1, itemStore.findAll().size());
    }

    public void testFindItem() {
        //Given
        ItemStore itemStore = ItemStore.getInstance();

        Item.createAndStoreItem(fileOne.getAbsolutePath());
        Item.createAndStoreItem(fileTwo.getAbsolutePath());

        //When
        Collection<Item> items = itemStore.findAll();

        //Then
        assertNotNull(items);
        assertEquals(2, items.size());
    }

    public void testFindWithPredicate() {
        //Given
        ItemStore itemStore = ItemStore.getInstance();

        Item.createAndStoreItem(fileOne.getAbsolutePath());
        Item.createAndStoreItem(fileTwo.getAbsolutePath());

        //When
        final String filePath = fileOne.getAbsolutePath();

        List<Item> items = itemStore.findAll(new Predicate<Item>() {

            @Override
            public boolean apply(Item each) {
                return filePath.equals(each.getOriginalPath());
            }
        });

        //Then
        assertNotNull(items);
        assertEquals(1, items.size());
        assertEquals(fileOne.getAbsolutePath(), items.get(0).getOriginalPath());
    }

    public void testNoBackupPredicate() {
        //Given
        Item itemOne = Item.createAndStoreItem(fileOne.getAbsolutePath());
        Item.createAndStoreItem(fileTwo.getAbsolutePath());

        List<Item> items = ItemStore.getInstance().findAll(new NoBackupPredicate());
        assertEquals(2, items.size());

        //When
        itemOne.setReference("some reference to indicate backup");

        //Then
        items = ItemStore.getInstance().findAll(new NoBackupPredicate());
        assertEquals(1, items.size());
    }

    public void testRemoveItem() {
        //Given
        Item item = Item.createAndStoreItem(fileOne.getAbsolutePath());
        assertEquals(item, ItemStore.getInstance().getItem(fileOne));

        //When
        ItemStore.getInstance().remove(item);

        //Then
        assertNull(ItemStore.getInstance().getItem(fileOne));
    }

    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();
        }
    }
}