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.ResourceVersion;
19  import org.figure8.join.businessobjects.environment.VersionedResource;
20  import org.figure8.join.businessobjects.environment.VersionedResourceType;
21  import org.figure8.join.businessobjects.environment.VersionedResourceUpdate;
22  
23  import java.util.List;
24  import java.util.Calendar;
25  /**
26   * JUnit test case for testing VersionedResourceUpdateDao implementation.
27   * @author  <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
28   * @version $Revision: 1.1 $
29   */
30  public class VersionedResourceUpdateDaoTest extends SpringTestCase{
31  
32     // Static -------------------------------------------------------------------
33  
34     /** Spring configuration files */
35     private String[] configLocations = new String[]{"classpath:/org/figure8/join/businessobjects/environment/persistence/spring.xml"};
36  
37  
38     // Attributes ---------------------------------------------------------------
39  
40     /** The resource dao we need */
41     private ResourceDao resourceDao = null;
42     /** The resource type dao we need */
43     private ResourceTypeDao resourceTypeDao = null;
44     /** The resource version dao we need */
45     private ResourceVersionDao resourceVersionDao = null;
46     /** The resource update dao we test */
47     private VersionedResourceUpdateDao dao = null;
48  
49  
50     // Override of SpringTestCase -----------------------------------------------
51  
52     /** Retrieve the daos we need after having initialized context */
53     public void setUp(){
54        super.setUp();
55        // Get the required daos.
56        resourceDao = (ResourceDao)context.getBean("resourceDao");
57        resourceTypeDao = (ResourceTypeDao)context.getBean("resourceTypeDao");
58        resourceVersionDao = (ResourceVersionDao)context.getBean("resourceVersionDao");
59        dao = (VersionedResourceUpdateDao)context.getBean("resourceUpdateDao");
60     }
61  
62  
63     // Public -------------------------------------------------------------------
64  
65     /** Test the creation of resource udpate */
66     public void testResourceUpdateCreation(){
67        // Save objects we need.
68        VersionedResourceType resType = new VersionedResourceType("resUpdTypeKey", "resUpdTypeLabel");
69        resourceTypeDao.save(resType);
70        VersionedResource res = new VersionedResource("resUpdKey", resType);
71        resourceDao.save(res);
72        ResourceVersion version = new ResourceVersion("verUpdName", "verUpdDescription", resType);
73        resourceVersionDao.save(version);
74        // Now create and save a resource update.
75        VersionedResourceUpdate update = new VersionedResourceUpdate(version, res);
76        int size = getResourceUpdateListSize();
77        dao.save(update);
78        // Assert there's one more resource update.
79        assertEquals("ResourceUpdate is successfully created", size + 1, dao.findAll().size());
80  
81        // Remove and assert.
82        dao.remove(update);
83        assertEquals("ResourceUpdate is successfully removed", size, dao.findAll().size());
84     }
85  
86     /** Test the retrieval of resource updates */
87     public void testResourceUpdateRetrieval(){
88        // Save objects we need.
89        VersionedResourceType resType = new VersionedResourceType("resUpdRetTypeKey", "resUpdRetTypeLabel");
90        resourceTypeDao.save(resType);
91        VersionedResource res = new VersionedResource("resUpdRetKey", resType);
92        resourceDao.save(res);
93        ResourceVersion version1 = new ResourceVersion("verUpdRetName1", "verUpdRetDescription", resType);
94        ResourceVersion version2 = new ResourceVersion("verUpdRetName2", "verUpdRetDescription", resType);
95        resourceVersionDao.save(version1);
96        resourceVersionDao.save(version2);
97        // Now save a bunch of updates for resource.
98        int size = getResourceUpdateListSize();
99        VersionedResourceUpdate update1 = new VersionedResourceUpdate(version1, res);
100       dao.save(update1);
101       // Wait here to avoid end date == start date.
102       Object semaphore = new Object();
103       try {synchronized (semaphore) {semaphore.wait(1000);}}
104       catch (Exception e) {}
105       // Now close current update and create a new one.
106       update1.close();
107       dao.save(update1);
108       VersionedResourceUpdate update2 = new VersionedResourceUpdate(version2, res);
109       dao.save(update2);
110       // Assert there's two more resource updates.
111       assertEquals("ResourceUpdates are successfully created", size + 2, dao.findAll().size());
112 
113       // Retrieve updates of the day.
114       Calendar startDate = Calendar.getInstance();
115       startDate.set(Calendar.HOUR_OF_DAY, 0);
116       startDate.set(Calendar.MINUTE, 1);
117       Calendar endDate = Calendar.getInstance();
118       endDate.set(Calendar.HOUR_OF_DAY, 23);
119       endDate.set(Calendar.MINUTE, 59);
120       List updates = dao.getVersionedResourceUpdatesForResource(res, startDate.getTime(), endDate.getTime());
121       // Assert there's 2 updates done.
122       assertEquals("Two ResourceUpdates done this day", 2, updates.size());
123 
124       // Retrieve updates from now till midnight (only one)
125       updates = dao.getVersionedResourceUpdatesForResource(res, Calendar.getInstance().getTime(), endDate.getTime());
126       assertEquals("One ResourceUpdate from now to midnight", 1, updates.size());
127       assertEquals("Correct ResourceUpdate from now to midnight", update2.getId(), ((VersionedResourceUpdate)updates.get(0)).getId());
128 
129       // Retrieve update active at a special
130       Calendar sd = Calendar.getInstance();
131       sd.setTime(update1.getStartDate());
132       sd.add(Calendar.MILLISECOND, 500);
133       VersionedResourceUpdate foundUpdate = dao.getVersionedResourceUpdateForResource(res, sd.getTime());
134       assertNotNull("One ResourceUpdate active some seconds ago", foundUpdate);
135       assertEquals("Correct ResourceUpdate active some seconds ago", update1.getId(), foundUpdate.getId());
136    }
137 
138 
139    // Protected ----------------------------------------------------------------
140 
141    /** Test the findAll() on dao */
142    protected int getResourceUpdateListSize(){
143       return dao.findAll().size();
144    }
145 
146 
147    // Implementation of SpringTestCase -----------------------------------------
148 
149    /** @return configLocations inner field */
150    public String[] getConfigLocations(){
151       return configLocations;
152    }
153 }