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.businessfacades.reporting;
16  
17  import org.figure8.join.businessobjects.commons.Release;
18  import org.figure8.join.businessobjects.commons.persistence.ReleaseDao;
19  import org.figure8.join.businessobjects.reporting.Message;
20  import org.figure8.join.util.SpringTestCase;
21  
22  import java.util.Date;
23  import java.util.Calendar;
24  /**
25   * JUnit test case for testing the ReportingManager implementation.
26   * @author  <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
27   * @version $Revision: 1.2 $
28   */
29  public class ReportingManagerTest extends SpringTestCase{
30  
31     // Static -------------------------------------------------------------------
32  
33     /** Spring configuration files */
34     private String[] configLocations = new String[]{
35        "classpath:/org/figure8/join/businessfacades/reporting/spring.xml",
36        "classpath:/org/figure8/join/businessobjects/reporting/persistence/spring.xml"};
37  
38  
39     // Attributes ---------------------------------------------------------------
40  
41     /** The needed ReleaseDao impl */
42     protected ReleaseDao releaseDao = null;
43  
44     /** The ReportingManager implementation instance to test */
45     protected ReportingManager reportingManager = null;
46  
47  
48     // Override of SpringTestCase -----------------------------------------------
49  
50     /** Retrieve the needed daos and manager after having initialized context */
51     public void setUp(){
52        super.setUp();
53        // Get the needed daos ...
54        releaseDao = (ReleaseDao)context.getBean("releaseDao");
55        // ... and the required manager.
56        reportingManager = (ReportingManager)context.getBean("reportingManager");
57     }
58  
59  
60     // Public -------------------------------------------------------------------
61  
62     /** Test the messages related methods. */
63     public void testMessage(){
64        // Create required item for message.
65        Release release = new Release("1.0", 1, 0, new Date());
66        try {releaseDao.save(release);}
67        catch (Exception e){
68           fail("No exception should be thrown");
69        }
70        // Save a bunch of new messages.
71        Calendar pubCal = Calendar.getInstance();
72        Calendar expCal = Calendar.getInstance();
73        pubCal.add(Calendar.DAY_OF_YEAR, -4);
74        expCal.add(Calendar.DAY_OF_YEAR, 1);
75        Message message1 = new Message("title1", "content1", pubCal.getTime(), expCal.getTime());
76        Message message2 = new Message("title2", "content2", pubCal.getTime(), expCal.getTime());
77        message2.setRelease(release);
78        reportingManager.saveMessage(message1);
79        reportingManager.saveMessage(message2);
80        // Assert there's 2 open messages created.
81        assertEquals("Messages are successfully created", 2, reportingManager.getOpenMessages(null).size());
82        assertEquals("Messages are successfully created", 1, reportingManager.getOpenMessages(release).size());
83  
84        // Change expiry date.
85        expCal.add(Calendar.DAY_OF_YEAR, -3);
86        Message message3 = new Message("title3", "content3", pubCal.getTime(), expCal.getTime());
87        Message message4 = new Message("title4", "content4", pubCal.getTime(), expCal.getTime());
88        message4.setRelease(release);
89        reportingManager.saveMessage(message3);
90        reportingManager.saveMessage(message4);
91  
92        // Assert we can retrieve these messages.
93        Calendar sCal = Calendar.getInstance();
94        sCal.add(Calendar.DAY_OF_YEAR, -3);
95        assertEquals("Messages are successfully retrieved", 4, reportingManager.getMessages(sCal.getTime(),
96                                                                                   expCal.getTime(), null).size());
97        assertEquals("Messages are successfully retrieved", 2, reportingManager.getMessages(sCal.getTime(),
98                                                                                   expCal.getTime(), release).size());
99        // Assert there's still 2 open messages created.
100       assertEquals("Messages are successfully created", 2, reportingManager.getOpenMessages(null).size());
101       assertEquals("Messages are successfully created", 1, reportingManager.getOpenMessages(release).size());
102 
103       // Try retrieving a message by its id.
104       Message foundMessage = reportingManager.getMessage(message1.getId());
105       assertNotNull("Found a message using its id", foundMessage);
106       assertEquals("Found the correct message", message1.getTitle(), foundMessage.getTitle());
107       assertEquals("Found the correct message", message1.getContent(), foundMessage.getContent());
108    }
109    
110    
111    // Implementation of SpringTestCase -----------------------------------------
112 
113    /** @return configLocations inner field */
114    public String[] getConfigLocations(){
115       return configLocations;
116    }
117 }