001    package com.khubla.pragmatach.contrib.testsupport.dao;
002    
003    import java.io.Serializable;
004    import java.util.List;
005    
006    import org.testng.Assert;
007    import org.testng.annotations.BeforeClass;
008    import org.testng.annotations.Test;
009    
010    import com.khubla.pragmatach.framework.application.Application;
011    import com.khubla.pragmatach.framework.configuration.PropertiesFileConfigurationImpl;
012    import com.khubla.pragmatach.framework.dao.DAO;
013    import com.khubla.pragmatach.framework.dao.Pager;
014    import com.khubla.pragmatach.framework.scanner.AnnotationScanner;
015    
016    /**
017     * @author tome
018     */
019    public abstract class AbstractDAOTest<T, I extends Serializable> {
020       /**
021        * get the DAO
022        */
023       public abstract DAO<T, I> getDAO();
024    
025       /**
026        * get id
027        */
028       public abstract I getId(T t);
029    
030       /**
031        * get an instance
032        */
033       public abstract T getInstance();
034    
035       /**
036        * for the class
037        */
038       @BeforeClass
039       public void setupClass() {
040          /*
041           * run the scanner
042           */
043          try {
044             AnnotationScanner.scan(null);
045          } catch (final Exception e) {
046             e.printStackTrace();
047             Assert.fail();
048          }
049          /*
050           * set the config
051           */
052          Application.setConfiguration(new PropertiesFileConfigurationImpl());
053       }
054    
055       /**
056        * basic CRUD tests on the DAO
057        */
058       @Test(enabled = true)
059       public void testCRUD() {
060          try {
061             /*
062              * get DAO
063              */
064             final DAO<T, I> dao = this.getDAO();
065             Assert.assertNotNull(dao);
066             /*
067              * get an instance
068              */
069             final T t = this.getInstance();
070             Assert.assertNotNull(t);
071             /*
072              * there are no rows
073              */
074             long count = dao.count();
075             Assert.assertTrue(0 == count);
076             /*
077              * get all
078              */
079             List<T> all = dao.getAll();
080             Assert.assertNotNull(all);
081             Assert.assertTrue(all.size() == 0);
082             /*
083              * save the instance
084              */
085             dao.save(t);
086             final I id = this.getId(t);
087             Assert.assertNotNull(id);
088             /*
089              * there's 1 row
090              */
091             count = dao.count();
092             Assert.assertTrue(1 == count);
093             /*
094              * get it back
095              */
096             final T retrieved = dao.findById(id);
097             final I retrievedId = this.getId(retrieved);
098             Assert.assertNotNull(retrieved);
099             Assert.assertNotNull(retrievedId);
100             /*
101              * get a list of rows
102              */
103             all = dao.getAll();
104             Assert.assertNotNull(all);
105             Assert.assertTrue(all.size() == 1);
106             /*
107              * delete it
108              */
109             dao.delete(t);
110             /*
111              * no rows
112              */
113             count = dao.count();
114             Assert.assertTrue(0 == count);
115             /*
116              * it's gone
117              */
118             final T retrieved2 = dao.findById(id);
119             Assert.assertNull(retrieved2);
120             /*
121              * nothing in list
122              */
123             all = dao.getAll();
124             Assert.assertNotNull(all);
125             Assert.assertTrue(all.size() == 0);
126          } catch (final Exception e) {
127             e.printStackTrace();
128             Assert.fail();
129          }
130       }
131    
132       /**
133        * test that we can get pager
134        */
135       @Test(enabled = true)
136       public void testPager() {
137          try {
138             /*
139              * get DAO
140              */
141             final DAO<T, I> dao = this.getDAO();
142             Assert.assertNotNull(dao);
143             /*
144              * get pager
145              */
146             final Pager<T> pager = dao.getPager(10);
147             Assert.assertNotNull(pager);
148          } catch (final Exception e) {
149             e.printStackTrace();
150             Assert.fail();
151          }
152       }
153    }