KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > notification > TestContentNotification


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/notification/TestContentNotification.java,v $
3  * Date : $Date: 2006/03/27 14:52:58 $
4  * Version: $Revision: 1.2 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31  
32 package org.opencms.notification;
33
34 import org.opencms.file.CmsObject;
35 import org.opencms.file.CmsProperty;
36 import org.opencms.file.CmsPropertyDefinition;
37 import org.opencms.file.CmsResource;
38 import org.opencms.file.CmsUser;
39 import org.opencms.file.types.CmsResourceTypeXmlPage;
40 import org.opencms.i18n.CmsLocaleManager;
41 import org.opencms.security.I_CmsPrincipal;
42 import org.opencms.test.OpenCmsTestCase;
43 import org.opencms.test.OpenCmsTestProperties;
44 import org.opencms.util.CmsStringUtil;
45
46 import java.util.Calendar JavaDoc;
47 import java.util.Collection JavaDoc;
48 import java.util.Date JavaDoc;
49 import java.util.GregorianCalendar JavaDoc;
50 import java.util.HashMap JavaDoc;
51 import java.util.Iterator JavaDoc;
52 import java.util.TimeZone JavaDoc;
53
54 import junit.extensions.TestSetup;
55 import junit.framework.Test;
56 import junit.framework.TestSuite;
57
58 /**
59  * Unit test for the OpenCms content notification.<p>
60  *
61  * @author Jan Baudisch
62  * @version $Revision: 1.2 $
63  */

64 public class TestContentNotification extends OpenCmsTestCase {
65   
66     /**
67      * Default JUnit constructor.<p>
68      *
69      * @param arg0 JUnit parameters
70      */

71     public TestContentNotification(String JavaDoc arg0) {
72         super(arg0);
73     }
74     
75     /**
76      * Test suite for this test class.<p>
77      *
78      * @return the test suite
79      */

80     public static Test suite() {
81         OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH);
82         
83         TestSuite suite = new TestSuite();
84         suite.setName(TestContentNotification.class.getName());
85                 
86         suite.addTest(new TestContentNotification("testContentNotification"));
87                
88         TestSetup wrapper = new TestSetup(suite) {
89             
90             protected void setUp() {
91                 setupOpenCms("simpletest", "/sites/default/");
92             }
93             
94             protected void tearDown() {
95                 removeOpenCms();
96             }
97         };
98         
99         return wrapper;
100     }
101     
102     
103     /**
104      * Sets responsibles to a file and then tests the readResponsibleUsers method of CmsObject .<p>
105      *
106      * @throws Throwable if something goes wrong
107      */

108     public void testContentNotification() throws Throwable JavaDoc {
109         
110         echo("Testing OpenCms content notification");
111         CmsObject cms = getCmsObject();
112         // initialize calendars
113

114         GregorianCalendar JavaDoc today = new GregorianCalendar JavaDoc(TimeZone.getDefault(), CmsLocaleManager.getDefaultLocale());
115         today.setTimeInMillis(cms.getRequestContext().getRequestTime());
116         GregorianCalendar JavaDoc inFiveDays = (GregorianCalendar JavaDoc)today.clone();
117         inFiveDays.add(Calendar.DAY_OF_YEAR, 5);
118         GregorianCalendar JavaDoc inEightDays = (GregorianCalendar JavaDoc)today.clone();
119         inEightDays.add(Calendar.DAY_OF_YEAR, 8);
120         GregorianCalendar JavaDoc oneDayBefore = (GregorianCalendar JavaDoc)today.clone();
121         oneDayBefore.add(Calendar.DAY_OF_YEAR, -1);
122         
123
124         echo("yesterday: " + oneDayBefore.getTimeInMillis());
125         echo("today: " + today.getTimeInMillis());
126         echo("in five days: " + inFiveDays.getTimeInMillis());
127         echo("inEightDays: " + inEightDays.getTimeInMillis());
128         
129         // create three users, two of them belonging to a group
130
CmsUser fry = cms.createUser("fry", "password", "First test user", new HashMap JavaDoc());
131          
132         // create a number of resources
133
String JavaDoc folder = "folder1/";
134         String JavaDoc expired01 = "folder1/expired01.html";
135         CmsResource expired = cms.createResource(expired01, CmsResourceTypeXmlPage.getStaticTypeId());
136         cms.chacc(expired01, I_CmsPrincipal.PRINCIPAL_USER, fry.getName(), "+s");
137         cms.setDateExpired(expired01, inFiveDays.getTimeInMillis(), false);
138         
139         String JavaDoc expired02 = "folder1/expired02.html";
140         cms.createResource(expired02, CmsResourceTypeXmlPage.getStaticTypeId());
141         cms.chacc(expired02, I_CmsPrincipal.PRINCIPAL_USER, fry.getName(), "+s");
142         cms.setDateExpired(expired02, inEightDays.getTimeInMillis(), false);
143         
144         String JavaDoc expired03 = "folder1/expired03.html";
145         cms.createResource(expired03, CmsResourceTypeXmlPage.getStaticTypeId());
146         cms.chacc(expired03, I_CmsPrincipal.PRINCIPAL_USER, fry.getName(), "+s");
147         cms.setDateExpired(expired03, oneDayBefore.getTimeInMillis(), false);
148         
149         String JavaDoc released01 = "folder1/released01.html";
150         CmsResource released = cms.createResource(released01, CmsResourceTypeXmlPage.getStaticTypeId());
151         cms.chacc(released01, I_CmsPrincipal.PRINCIPAL_USER, fry.getName(), "+s");
152         cms.setDateReleased(released01, inFiveDays.getTimeInMillis(), false);
153         
154         String JavaDoc released02Name = "folder1/released02.html";
155         cms.createResource(released02Name, CmsResourceTypeXmlPage.getStaticTypeId());
156         cms.chacc(released02Name, I_CmsPrincipal.PRINCIPAL_USER, fry.getName(), "+s");
157         cms.setDateReleased(released02Name, inEightDays.getTimeInMillis(), false);
158         
159         String JavaDoc released03 = "folder1/released03.html";
160         cms.createResource(released03, CmsResourceTypeXmlPage.getStaticTypeId());
161         cms.chacc(released03, I_CmsPrincipal.PRINCIPAL_USER, fry.getName(), "+s");
162         cms.setDateReleased(released03, oneDayBefore.getTimeInMillis(), false);
163         
164         cms.lockResource(folder);
165         cms.writePropertyObject(folder, new CmsProperty(
166             CmsPropertyDefinition.PROPERTY_ENABLE_NOTIFICATION, CmsStringUtil.TRUE, CmsStringUtil.TRUE));
167         cms.unlockResource(folder);
168         cms.publishProject();
169         Iterator JavaDoc notifications = new CmsNotificationCandidates(cms).getContentNotifications().iterator();
170         // there should be exactly one notification
171
while (notifications.hasNext()) {
172             CmsContentNotification notification = (CmsContentNotification)notifications.next();
173             assertTrue(notification.getResponsible().equals(fry)); // fry should be notified;
174
Collection JavaDoc notificationCauses = notification.getNotificationCauses();
175             assertTrue(notificationCauses.contains(new CmsExtendedNotificationCause(expired,
176                 CmsExtendedNotificationCause.RESOURCE_EXPIRES, new Date JavaDoc(expired.getDateExpired()))));
177             assertTrue(notificationCauses.contains(new CmsExtendedNotificationCause(released,
178                 CmsExtendedNotificationCause.RESOURCE_RELEASE, new Date JavaDoc(released.getDateReleased()))));
179             // there should be no other resources contained in the notification
180
assertEquals(notificationCauses.size(), 2);
181         }
182     }
183 }
Popular Tags