1+ package com .iluwatar .servicelayer .common ;
2+
3+ import com .iluwatar .servicelayer .hibernate .HibernateUtil ;
4+
5+ import org .junit .After ;
6+ import org .junit .Before ;
7+ import org .junit .Test ;
8+
9+ import java .util .List ;
10+ import java .util .concurrent .atomic .AtomicInteger ;
11+ import java .util .function .Function ;
12+
13+ import static org .junit .Assert .assertEquals ;
14+ import static org .junit .Assert .assertNotNull ;
15+ import static org .junit .Assert .assertNull ;
16+
17+ /**
18+ * Date: 12/28/15 - 10:53 PM
19+ *
20+ * @author Jeroen Meulemeester
21+ */
22+ public abstract class BaseDaoTest <E extends BaseEntity , D extends DaoBaseImpl <E >> {
23+
24+ /**
25+ * The number of entities stored before each test
26+ */
27+ private static final int INITIAL_COUNT = 5 ;
28+
29+ /**
30+ * The unique id generator, shared between all entities
31+ */
32+ private static final AtomicInteger ID_GENERATOR = new AtomicInteger ();
33+
34+ /**
35+ * Factory, used to create new entity instances with the given name
36+ */
37+ private final Function <String , E > factory ;
38+
39+ /**
40+ * The tested data access object
41+ */
42+ private final D dao ;
43+
44+ /**
45+ * Create a new test using the given factory and dao
46+ *
47+ * @param factory The factory, used to create new entity instances with the given name
48+ * @param dao The tested data access object
49+ */
50+ public BaseDaoTest (final Function <String , E > factory , final D dao ) {
51+ this .factory = factory ;
52+ this .dao = dao ;
53+ }
54+
55+ @ Before
56+ public void setUp () throws Exception {
57+ for (int i = 0 ; i < INITIAL_COUNT ; i ++) {
58+ final String className = dao .persistentClass .getSimpleName ();
59+ final String entityName = String .format ("%s%d" , className , ID_GENERATOR .incrementAndGet ());
60+ this .dao .persist (this .factory .apply (entityName ));
61+ }
62+ }
63+
64+ @ After
65+ public void tearDown () throws Exception {
66+ HibernateUtil .dropSession ();
67+ }
68+
69+ protected final D getDao () {
70+ return this .dao ;
71+ }
72+
73+ @ Test
74+ public void testFind () throws Exception {
75+ final List <E > all = this .dao .findAll ();
76+ for (final E entity : all ) {
77+ final E byId = this .dao .find (entity .getId ());
78+ assertNotNull (byId );
79+ assertEquals (byId .getId (), byId .getId ());
80+ }
81+ }
82+
83+ @ Test
84+ public void testDelete () throws Exception {
85+ final List <E > originalEntities = this .dao .findAll ();
86+ this .dao .delete (originalEntities .get (1 ));
87+ this .dao .delete (originalEntities .get (2 ));
88+
89+ final List <E > entitiesLeft = this .dao .findAll ();
90+ assertNotNull (entitiesLeft );
91+ assertEquals (INITIAL_COUNT - 2 , entitiesLeft .size ());
92+ }
93+
94+ @ Test
95+ public void testFindAll () throws Exception {
96+ final List <E > all = this .dao .findAll ();
97+ assertNotNull (all );
98+ assertEquals (INITIAL_COUNT , all .size ());
99+ }
100+
101+ @ Test
102+ public void testSetId () throws Exception {
103+ final E entity = this .factory .apply ("name" );
104+ assertNull (entity .getId ());
105+
106+ final Long expectedId = Long .valueOf (1 );
107+ entity .setId (expectedId );
108+ assertEquals (expectedId , entity .getId ());
109+ }
110+
111+ @ Test
112+ public void testSetName () throws Exception {
113+ final E entity = this .factory .apply ("name" );
114+ assertEquals ("name" , entity .getName ());
115+ assertEquals ("name" , entity .toString ());
116+
117+ final String expectedName = "new name" ;
118+ entity .setName (expectedName );
119+ assertEquals (expectedName , entity .getName ());
120+ assertEquals (expectedName , entity .toString ());
121+ }
122+
123+ }
0 commit comments