1   package com.neidetcher.petunia.service;
2   
3   import junit.framework.TestCase;
4   
5   import org.springframework.beans.factory.BeanFactory;
6   import org.springframework.beans.factory.xml.XmlBeanFactory;
7   import org.springframework.core.io.ClassPathResource;
8   import org.springframework.core.io.Resource;
9   
10  public class CategoryDaoTest extends TestCase
11  {
12  
13     CategoryDao categoryDao;
14  
15     @Override
16     public void setUp()
17     {
18        categoryDao = (CategoryDao) getBean("CategoryDao");
19     }
20  
21     public void testSave_insert()
22     {
23        Category category = new CategoryImpl();
24        category.setDescription("recursive, paren heavy evil");
25        category.setName("lisp");
26        category.setParentCategoryId(1);
27  
28        Category categoryResult = categoryDao.save(category);
29        assertNotNull(categoryResult);
30     }
31  
32     public void testSave_update()
33     {
34        Category category = new CategoryImpl();
35        category.setDescription("programmers regular expression crap");
36        category.setName("perl");
37        category.setParentCategoryId(1);
38  
39        Category categoryResult = categoryDao.save(category);
40        assertNotNull(categoryResult);
41  
42        categoryResult.setParentCategoryId(null);
43  
44        Category categoryResult2 = categoryDao.save(categoryResult);
45        assertNotNull(categoryResult2);
46        assertEquals(categoryResult.getId(), categoryResult2.getId());
47     }
48  
49     public void testFindById()
50     {
51        Category categoryResult = categoryDao.find(1);
52        assertNotNull(categoryResult);
53     }
54  
55     protected Object getBean(String name)
56     {
57        Resource resource = new ClassPathResource("applicationContext.xml");
58        BeanFactory factory = new XmlBeanFactory(resource);
59        return factory.getBean(name);
60     }
61  }