fr.mael.microrss.dao.CategoryDaoTest.java Source code

Java tutorial

Introduction

Here is the source code for fr.mael.microrss.dao.CategoryDaoTest.java

Source

/*
   Copyright  2013 Mael Le Guvel
   This work is free. You can redistribute it and/or modify it under the
   terms of the Do What The Fuck You Want To Public License, Version 2,
   as published by Sam Hocevar. See the COPYING file for more details.
*/
package fr.mael.microrss.dao;

import java.util.Collection;
import java.util.List;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import fr.mael.microrss.domain.Category;
import fr.mael.microrss.domain.User;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/fr/mael/microrss/conf/application-context-test.xml" })
@Transactional
public class CategoryDaoTest {

    @Autowired
    private CategoryDao categoryDao;

    @Test
    public void testList() {
        User user = new User();
        user.setId(17);
        List<Category> categories = categoryDao.forUser(user);
        Assert.assertEquals(new Integer(5), new Integer(categories.size()));
        Category cat1 = getCategory(1, categories);
        Assert.assertEquals(new Integer(1), new Integer(cat1.getUserFeeds().size()));
        Assert.assertEquals(new Integer(1), cat1.getUserFeeds().iterator().next().getFeed().getId());
        Category cat2 = getCategory(2, categories);
        Assert.assertEquals(new Integer(0), new Integer(cat2.getUserFeeds().size()));
        Category cat3 = getCategory(3, categories);
        Assert.assertEquals(new Integer(2), new Integer(cat3.getUserFeeds().size()));
        Category cat4 = getCategory(2, categories);
        Assert.assertEquals(new Integer(0), new Integer(cat4.getUserFeeds().size()));
        Category cat5 = getCategory(2, categories);
        Assert.assertEquals(new Integer(0), new Integer(cat5.getUserFeeds().size()));
    }

    @Test
    public void testList2() {
        User user = new User();
        user.setId(18);
        List<Category> categories = categoryDao.forUser(user);
        Assert.assertEquals(new Integer(1), new Integer(categories.size()));
        Category cat6 = getCategory(6, categories);
        Assert.assertEquals(new Integer(1), new Integer(cat6.getUserFeeds().size()));
        Assert.assertEquals(new Integer(3), cat6.getUserFeeds().iterator().next().getFeed().getId());
    }

    @Test
    public void testCollapse() {
        Category cat1 = categoryDao.get(1);
        cat1 = categoryDao.collapse(cat1, true);
        Assert.assertTrue(cat1.getCollapsed());
        Category cat2 = categoryDao.get(1);
        Assert.assertTrue(cat2.getCollapsed());
    }

    private Category getCategory(Integer id, Collection<Category> categories) {
        for (Category cat : categories) {
            if (cat.getId().equals(id)) {
                return cat;
            }
        }
        return null;
    }
}