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.reporting.persistence;
16  
17  import org.figure8.join.util.SpringTestCase;
18  import org.figure8.join.businessobjects.reporting.Message;
19  import org.figure8.join.businessobjects.commons.persistence.ReleaseDao;
20  import org.figure8.join.businessobjects.commons.Release;
21  
22  import java.util.Date;
23  import java.util.Calendar;
24  import java.util.List;
25  /**
26   * JUnit test case for testing MessageDao implementation.
27   * @author  <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
28   * @version $Revision: 1.1 $
29   */
30  public class MessageDaoTest extends SpringTestCase{
31  
32     // Static -------------------------------------------------------------------
33  
34     /** Spring configuration files */
35     private String[] configLocations = new String[]{"classpath:/org/figure8/join/businessobjects/reporting/persistence/spring.xml"};
36  
37  
38     // Attributes ---------------------------------------------------------------
39  
40     /** The needed ReleaseDao impl */
41     protected ReleaseDao releaseDao = null;
42     /** The MessageDao implementation to test */
43     protected MessageDao dao = null;
44  
45  
46     // Override of SpringTestCase -----------------------------------------------
47  
48     /** Retrieve needed daos after having initialized context */
49     public void setUp(){
50        super.setUp();
51        // Get needed dao.
52        dao = (MessageDao)context.getBean("messageDao");
53        releaseDao = (ReleaseDao)context.getBean("releaseDao");
54     }
55  
56  
57     // Public -------------------------------------------------------------------
58  
59     /** Test Message creation. */
60     public void testMessageCreation(){
61        // Save a new message.
62        Message message = new Message("title", "content", new Date(), new Date());
63        int size = getMessageListSize();
64        dao.save(message);
65        // Assert there's one message.
66        assertEquals("Message is successfully created", size + 1, dao.findAll().size());
67     }
68  
69     /** Test the getOpenMessages() on dao. */
70     public void testFindOpenMessages(){
71        Calendar pubCal = Calendar.getInstance();
72        Calendar expCal = Calendar.getInstance();
73        expCal.add(Calendar.DAY_OF_YEAR, 1);
74        // Save a bunch of new messages and required items.
75        Release release = new Release("1.0", 1, 0, new Date());
76        Message message1 = new Message("title1", "content1", pubCal.getTime(), expCal.getTime());
77        Message message2 = new Message("title2", "content2", pubCal.getTime(), expCal.getTime());
78        Message message3 = new Message("title3", "content3", expCal.getTime(), expCal.getTime());
79        message2.setRelease(release);
80        message3.setRelease(release);
81        int size = getMessageListSize();
82        releaseDao.save(release);
83        dao.save(message1);
84        dao.save(message2);
85        dao.save(message3);
86        // Assert there's three more message.
87        assertEquals("Messages are successfully created", size + 3, dao.findAll().size());
88        // Retrieve open messages.
89        List messages = dao.getOpenMessages();
90        assertEquals("Found 2 open messages", 2, messages.size());
91        for (int i=0; i<messages.size(); i++){
92           Message foundMsg = (Message)messages.get(i);
93           assertTrue("Found the correct open messages", "title1".equals(foundMsg.getTitle()) || "title2".equals(foundMsg.getTitle()));
94        }
95        // Retrieve open messages for release.
96        messages = dao.getOpenMessagesForRelease(release);
97        assertEquals("Found 1 open message for release", 1, messages.size());
98        Message foundMsg = (Message)messages.get(0);
99        dao.initialize(foundMsg.getRelease());
100       assertEquals("Found the correct open message for release", "title2", foundMsg.getTitle());
101       assertEquals("Correct message has correct release", "1.0", foundMsg.getRelease().getName());
102    }
103 
104    /** Test the getMessages() on dao. */
105    public void testFindMessages(){
106       Calendar pubCal = Calendar.getInstance();
107       Calendar expCal = Calendar.getInstance();
108       pubCal.add(Calendar.DAY_OF_YEAR, -4);
109       expCal.add(Calendar.DAY_OF_YEAR, -2);
110       // Save a bunch of new messages and required items.
111       Release release = new Release("2.0", 2, 0, new Date());
112       Message message1 = new Message("title11", "content11", pubCal.getTime(), expCal.getTime());
113       Message message2 = new Message("title12", "content12", pubCal.getTime(), expCal.getTime());
114       Message message3 = new Message("title13", "content13", new Date(), new Date());
115       message2.setRelease(release);
116       message3.setRelease(release);
117       int size = getMessageListSize();
118       releaseDao.save(release);
119       dao.save(message1);
120       dao.save(message2);
121       dao.save(message3);
122       // Assert there's three more message.
123       assertEquals("Messages are successfully created", size + 3, dao.findAll().size());
124       // Retrieve messages using dates.
125       Calendar sCal = Calendar.getInstance();
126       sCal.add(Calendar.DAY_OF_YEAR, -3);
127       List messages = dao.getMessages(sCal.getTime(), expCal.getTime());
128       assertEquals("Found 2 messages for specified dates", 2, messages.size());
129       for (int i=0; i<messages.size(); i++){
130          Message foundMsg = (Message)messages.get(i);
131          assertTrue("Found the correct messages for specified dates", "title11".equals(foundMsg.getTitle()) || "title12".equals(foundMsg.getTitle()));
132       }
133       // Retrieve open messages for release.
134       messages = dao.getMessagesForRelease(release, sCal.getTime(), expCal.getTime());
135       assertEquals("Found 1 message for specified dates and release", 1, messages.size());
136       Message foundMsg = (Message)messages.get(0);
137       dao.initialize(foundMsg.getRelease());
138       assertEquals("Found the correct message for specified dates and release", "title12", foundMsg.getTitle());
139       assertEquals("Correct message has correct release", "2.0", foundMsg.getRelease().getName());
140    }
141 
142 
143    // Protected ----------------------------------------------------------------
144 
145    /** Test the findAll on message */
146    protected int getMessageListSize(){
147       return dao.findAll().size();
148    }
149 
150 
151    // Implementation of SpringTestCase -----------------------------------------
152 
153    /** @return configLocations inner field */
154    public String[] getConfigLocations(){
155       return configLocations;
156    }
157 }