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.environment.persistence;
16  
17  import org.figure8.join.util.SpringTestCase;
18  import org.figure8.join.businessobjects.environment.VersionedResourceType;
19  import org.figure8.join.businessobjects.environment.ResourceVersion;
20  
21  import java.util.List;
22  /**
23   * JUnit test case for testing ResourceVersionDao implementation.
24   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
25   * @version $Revision: 1.2 $
26   */
27  public class ResourceVersionDaoTest extends SpringTestCase{
28  
29     // Static -------------------------------------------------------------------
30  
31     /** Spring configuration files */
32     private String[] configLocations = new String[]{
33        "classpath:/org/figure8/join/businessobjects/environment/persistence/spring.xml"};
34  
35  
36     // Attributes ---------------------------------------------------------------
37  
38     /** The resource type dao we need */
39     protected ResourceTypeDao typeDao = null;
40     /** The dao implementation to test */
41     protected ResourceVersionDao dao = null;
42  
43  
44     // Override of SpringTestCase -----------------------------------------------
45  
46     /** Retrieve the resource type and version daos after having initialized context */
47     public void setUp(){
48        super.setUp();
49        // Get daos.
50        dao = (ResourceVersionDao)context.getBean("resourceVersionDao");
51        typeDao = (ResourceTypeDao)context.getBean("resourceTypeDao");
52     }
53  
54  
55     // Public -------------------------------------------------------------------
56  
57     /** Test the creation of a ResourceVersion */
58     public void testResourceVersionCreation(){
59        // Save a new VersionedResourceType.
60        VersionedResourceType resType = new VersionedResourceType("resTypeKey", "resTypeLabel");
61        typeDao.save(resType);
62        // Save a new ResourceVersion.
63        ResourceVersion version = new ResourceVersion("versionName", "versionDescription", resType);
64        int size = getResourceVersionListSize();
65        dao.save(version);
66        // Assert there's one more resource version.
67        assertEquals("ResourceVersion is successfully created", size + 1, dao.findAll().size());
68  
69        // Remove and assert.
70        dao.remove(version);
71        assertEquals("ResourceVersion is successfully removed", size, dao.findAll().size());
72     }
73  
74     /** Test the retrieval of ResourceVersions */
75     public void testResourceVersionRetrieval(){
76        // Save a bunch of new resource types.
77        VersionedResourceType type1 = new VersionedResourceType("resTypeKey1", "resTypeLabel1");
78        VersionedResourceType type2 = new VersionedResourceType("resTypeKey2", "resTypeLabel2");
79        typeDao.save(type1);
80        typeDao.save(type2);
81        // Now save a bunch of new resource versions.
82        ResourceVersion ver1 = new ResourceVersion("version1", "description1", type1);
83        ResourceVersion ver2 = new ResourceVersion("version2", "description2", type1);
84        ResourceVersion ver3 = new ResourceVersion("version3", "description3", type2);
85        ResourceVersion ver4 = new ResourceVersion("version4", "description4", type2);
86        int size = getResourceVersionListSize();
87        dao.save(ver1);
88        dao.save(ver2);
89        dao.save(ver3);
90        dao.save(ver4);
91        // Assert there's four more resource versions.
92        assertEquals("ResourceVersions are successfully created", size + 4, dao.findAll().size());
93  
94        // Retrieve a version using its name.
95        ResourceVersion foundVersion = dao.getResourceVersion("version1");
96        assertNotNull("Found a ResourceVersion", foundVersion);
97        assertEquals("Found the correct ResourceVersion", "version1", foundVersion.getName());
98        assertEquals("Found the correct ResourceVersion", "description1", foundVersion.getDescription());
99        assertNotNull("Found a creation date", foundVersion.getCreationDate());
100 
101       // Retrieve versions using their type.
102       List versions = dao.getResourceVersionsByType(type1);
103       assertNotNull("Found a list of ResouceVersions", versions);
104       assertEquals("Found the correct number of ResouceVersions", 2, versions.size());
105       for (int i=0; i<versions.size(); i++){
106          foundVersion = (ResourceVersion)versions.get(i);
107          assertTrue("Found a correct ResourceVersion", "version1".equals(foundVersion.getName())
108                                                          || "version2".equals(foundVersion.getName()));
109       }
110       versions = dao.getResourceVersionsByType(type2);
111       assertNotNull("Found a list of ResouceVersions", versions);
112       assertEquals("Found the correct number of ResouceVersions", 2, versions.size());
113       for (int i=0; i<versions.size(); i++){
114          foundVersion = (ResourceVersion)versions.get(i);
115          assertTrue("Found a correct ResourceVersion", "version3".equals(foundVersion.getName())
116                                                          || "version4".equals(foundVersion.getName()));
117       }
118    }
119 
120 
121    // Protected ----------------------------------------------------------------
122 
123    /** Test the findAll() on dao */
124    protected int getResourceVersionListSize(){
125       return dao.findAll().size();
126    }
127 
128 
129    // Implementation of SpringTestCase -----------------------------------------
130 
131    /** @return configLocations inner field */
132    public String[] getConfigLocations(){
133       return configLocations;
134    }
135 }