1 /**
2 * Copyright 2005-2006 the original author or authors.
3 *
4 * Licensed under the Gnu General Pubic License, Version 2.0 (the
5 * "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/gpl-license.php
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the Gnu General Public License for more details.
14 */
15 package org.figure8.join.businessobjects.commons.persistence;
16
17 import org.figure8.join.util.SpringTestCase;
18 import org.figure8.join.businessobjects.commons.Step;
19 /**
20 * JUnit test case for testing StepDao implementation.
21 * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
22 * @version $Revision: 1.3 $
23 */
24 public class StepDaoTest extends SpringTestCase{
25
26
27
28 /** Spring configuration files */
29 private String[] configLocations = new String[]{"classpath:/org/figure8/join/businessobjects/commons/persistence/spring.xml"};
30
31
32
33
34 /** The step dao implementation to test */
35 protected StepDao dao = null;
36
37
38
39
40 /** Retrieve Step dao after having initialized context */
41 public void setUp(){
42 super.setUp();
43
44 dao = (StepDao)context.getBean("stepDao");
45 }
46
47
48
49
50 /** Test Step creation */
51 public void testStepCreation(){
52
53 Step step = new Step("firstStep", 1);
54 int size = getStepListSize();
55 dao.save(step);
56
57 assertEquals("Step is successfully created", size + 1, dao.findAll().size());
58 }
59
60 /** Test the getByPosition() method */
61 public void testFindByPosition(){
62
63 Step step = new Step("secondStep", 2);
64 int size = getStepListSize();
65 dao.save(step);
66
67 assertEquals("Step is successfully created", size + 1, dao.findAll().size());
68
69 Step foundStep = dao.getStepByPosition(2);
70 assertNotNull("Found a step", foundStep);
71 assertEquals("Found a step with correct position", 2, foundStep.getPosition());
72 assertEquals("Found a step with correct label", "secondStep", foundStep.getLabel());
73 }
74
75 /**
76 * Test the pseudo definition of an integration cycle
77 * and the enabling of cascade feature on DAO.
78 */
79 public void testCycleCreation(){
80 int size = getStepListSize();
81
82 Step step3 = new Step("thirdStep", 3);
83 Step step4 = new Step("fourthStep", 4);
84 Step step5 = new Step("fithStep", 5);
85
86
87 step4.setPreviousStep(step3);
88 step5.setPreviousStep(step4);
89 dao.save(step3);
90
91 assertEquals("Steps are successfully created", size + 3, dao.findAll().size());
92
93 Step foundStep = dao.getStepByPosition(4);
94 assertNotNull("Found a step", foundStep);
95 assertEquals("Found a step with correct position", 4, foundStep.getPosition());
96 assertEquals("Found a step with correct next step", 5, foundStep.getNextStep().getPosition());
97 assertEquals("Found a step with correct previous step", 3, foundStep.getPreviousStep().getPosition());
98 }
99
100 /** Test step insertion into a cycle */
101 public void testStepInsertionInCycle(){
102 int size = getStepListSize();
103
104 Step step7 = new Step("seventhStep", 7);
105 Step step8 = new Step("eightStep", 8);
106 Step step9 = new Step("ninethStep", 9);
107
108
109 step8.setPreviousStep(step7);
110 step9.setPreviousStep(step8);
111 dao.save(step7);
112
113 assertEquals("Steps are successfully created", size + 3, dao.findAll().size());
114 size = getStepListSize();
115
116 Step step7a = new Step("seventhStepA", 8);
117 Step other = dao.getStepByPosition(8);
118 other.shiftPosition();
119 dao.save(other);
120 dao.save(step7a);
121 step7a.setPreviousStep(other.getPreviousStep());
122
123 other.setPreviousStep(step7a);
124 dao.save(other);
125
126 assertEquals("Step is successfully created", size + 1, dao.findAll().size());
127 assertEquals("Step has correct next step", "eightStep", step7a.getNextStep().getLabel());
128 assertEquals("Step has correct next step", 9, step7a.getNextStep().getPosition());
129 assertEquals("Step has correct previous step", "seventhStep", step7a.getPreviousStep().getLabel());
130 assertEquals("Step has correct previous step", 7, step7a.getPreviousStep().getPosition());
131 }
132
133 /** Test the removal of a step */
134 public void testStepRemoval(){
135
136 Step step = new Step("sixthStep", 6);
137 int size = getStepListSize();
138 dao.save(step);
139
140 assertEquals("Step is successfully created", size + 1, dao.findAll().size());
141
142 dao.remove(step);
143 assertEquals("Step is successfully removed", size, dao.findAll().size());
144 }
145
146
147
148
149 /** Test the findAll on step */
150 protected int getStepListSize(){
151 return dao.findAll().size();
152 }
153
154
155
156
157 /** @return configLocations inner field */
158 public String[] getConfigLocations(){
159 return configLocations;
160 }
161 }