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.Status;
19  
20  import java.util.List;
21  /**
22   * JUnit test case for testing StatusDao implementation.
23   * @author  <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
24   * @version $Revision: 1.1 $
25   */
26  public class StatusDaoTest extends SpringTestCase{
27  
28     // Static -------------------------------------------------------------------
29  
30     /** Spring configuration files */
31     private String[] configLocations = new String[]{"classpath:/org/figure8/join/businessobjects/commons/persistence/spring.xml"};
32  
33  
34     // Attributes ---------------------------------------------------------------
35  
36     /** The StatusDao implementation to test */
37     protected StatusDao dao = null;
38  
39  
40     // Override of SpringTestCase -----------------------------------------------
41  
42     /** Retrieve Status dao after having initialized context */
43     public void setUp(){
44        super.setUp();
45        // Get dao.
46        dao = (StatusDao)context.getBean("statusDao");
47     }
48  
49  
50     // Public -------------------------------------------------------------------
51  
52     /** Test Status creation */
53     public void testStatusCreation(){
54        // Save a new status.
55        Status status = new Status("created", "Created status", Status.ASSEMBLY_TYPE);
56        status.setTerminal(true);
57        status.setCancelled(false);
58        int size = getStatusListSize();
59        dao.save(status);
60        // Assert there's one status.
61        assertEquals("Status is successfully created", size + 1, dao.findAll().size());
62     }
63  
64     /** Test Status retrieval */
65     public void testFindBy(){
66        // Save a bunch of new status.
67        Status status1 = new Status("key1", "label1", Status.BUILD_TYPE);
68        Status status2 = new Status("key2", "label2", Status.BUILD_TYPE);
69        Status status3 = new Status("key3", "label3", Status.ASSEMBLY_TYPE);
70        status2.setTerminal(true);
71        int size = getStatusListSize();
72        dao.save(status1);
73        dao.save(status2);
74        dao.save(status3);
75        // Assert there's three more status.
76        assertEquals("Status are successfully created", size + 3, dao.findAll().size());
77        // Retrieve status using identifier.
78        Status foundStatus = dao.getStatus(status3.getId());
79        assertNotNull("Found a Status", foundStatus);
80        assertEquals("Found the correct Status", "key3", foundStatus.getKey());
81        // Retrieve status using unique key.
82        foundStatus = dao.getStatus("key2");
83        assertNotNull("Found a Status", foundStatus);
84        assertEquals("Found the correct Status", "label2", foundStatus.getLabel());
85        assertTrue("Found the correct Status", foundStatus.isTerminal());
86        // Retrieve status using status type.
87        List result = dao.getStatusByType(Status.BUILD_TYPE);
88        assertNotNull("Found some Status", result);
89        assertEquals("Found the correct number of Status", 2, result.size());
90        for (int i=0; i<result.size(); i++){
91           foundStatus = (Status)result.get(i);
92           assertTrue("Found a correct Status", "key1".equals(foundStatus.getKey()) || "key2".equals(foundStatus.getKey()));
93        }
94     }
95  
96  
97     // Protected ----------------------------------------------------------------
98  
99     /** Test the findAll on status */
100    protected int getStatusListSize(){
101       return dao.findAll().size();
102    }
103 
104 
105    // Implementation of SpringTestCase -----------------------------------------
106 
107    /** @return configLocations inner field */
108    public String[] getConfigLocations(){
109       return configLocations;
110    }
111 }