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     // Static -------------------------------------------------------------------
27  
28     /** Spring configuration files */
29     private String[] configLocations = new String[]{"classpath:/org/figure8/join/businessobjects/commons/persistence/spring.xml"};
30  
31  
32     // Attributes ---------------------------------------------------------------
33  
34     /** The step dao implementation to test */
35     protected StepDao dao = null;
36  
37  
38     // Override of SpringTestCase -----------------------------------------------
39  
40     /** Retrieve Step dao after having initialized context */
41     public void setUp(){
42        super.setUp();
43        // Get step dao.
44        dao = (StepDao)context.getBean("stepDao");
45     }
46  
47  
48     // Public -------------------------------------------------------------------
49  
50     /** Test Step creation */
51     public void testStepCreation(){
52        // Save a new step.
53        Step step = new Step("firstStep", 1);
54        int size = getStepListSize();
55        dao.save(step);
56        // Assert there's one step.
57        assertEquals("Step is successfully created", size + 1, dao.findAll().size());
58     }
59  
60     /** Test the getByPosition() method */
61     public void testFindByPosition(){
62        // Save a new step.
63        Step step = new Step("secondStep", 2);
64        int size = getStepListSize();
65        dao.save(step);
66        // Assert there's one step.
67        assertEquals("Step is successfully created", size + 1, dao.findAll().size());
68        // Retrieve step using position.
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        // Save some associated steps.
82        Step step3 = new Step("thirdStep", 3);
83        Step step4 = new Step("fourthStep", 4);
84        Step step5 = new Step("fithStep", 5);
85        //step3.setNextStep(step4);
86        //step4.setNextStep(step5);
87        step4.setPreviousStep(step3);
88        step5.setPreviousStep(step4);
89        dao.save(step3);
90        // Assert there's three more steps.
91        assertEquals("Steps are successfully created", size + 3, dao.findAll().size());
92        // Retrieve a step using its position.
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       // Save some associated steps.
104       Step step7 = new Step("seventhStep", 7);
105       Step step8 = new Step("eightStep", 8);
106       Step step9 = new Step("ninethStep", 9);
107       //step7.setNextStep(step8);
108       //step8.setNextStep(step9);
109       step8.setPreviousStep(step7);
110       step9.setPreviousStep(step8);
111       dao.save(step7);
112       // Assert there's three more steps.
113       assertEquals("Steps are successfully created", size + 3, dao.findAll().size());
114       size = getStepListSize();
115       // Insert step 7a before step 8.
116       Step step7a = new Step("seventhStepA", 8);
117       Step other = dao.getStepByPosition(8);
118       other.shiftPosition();
119       dao.save(other);      // 1/3
120       dao.save(step7a);     // 2/3
121       step7a.setPreviousStep(other.getPreviousStep());
122       //step7a.setNextStep(other);
123       other.setPreviousStep(step7a);
124       dao.save(other);      // 3/3
125       // Assert.
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       // Save a new step.
136       Step step = new Step("sixthStep", 6);
137       int size = getStepListSize();
138       dao.save(step);
139       // Assert there's one step.
140       assertEquals("Step is successfully created", size + 1, dao.findAll().size());
141       // Remove and assert.
142       dao.remove(step);
143       assertEquals("Step is successfully removed", size, dao.findAll().size());
144    }
145 
146 
147    // Protected ----------------------------------------------------------------
148 
149    /** Test the findAll on step */
150    protected int getStepListSize(){
151       return dao.findAll().size();
152    }
153 
154 
155    // Implementation of SpringTestCase -----------------------------------------
156 
157    /** @return configLocations inner field */
158    public String[] getConfigLocations(){
159       return configLocations;
160    }
161 }