KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > file > TestReadResource


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/file/TestReadResource.java,v $
3  * Date : $Date: 2005/06/27 23:22:09 $
4  * Version: $Revision: 1.8 $
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.file;
33
34 import org.opencms.main.CmsException;
35 import org.opencms.test.OpenCmsTestCase;
36 import org.opencms.test.OpenCmsTestProperties;
37
38 import junit.extensions.TestSetup;
39 import junit.framework.Test;
40 import junit.framework.TestSuite;
41
42 /**
43  * Unit test for the "readFileHeader" method of the CmsObject to test the release and expiration date.<p>
44  *
45  * @author Michael Emmerich
46  * @version $Revision: 1.8 $
47  */

48 public class TestReadResource extends OpenCmsTestCase {
49   
50     /**
51      * Default JUnit constructor.<p>
52      *
53      * @param arg0 JUnit parameters
54      */

55     public TestReadResource(String JavaDoc arg0) {
56         super(arg0);
57     }
58     
59     /**
60      * Test suite for this test class.<p>
61      *
62      * @return the test suite
63      */

64     public static Test suite() {
65         OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH);
66         
67         TestSuite suite = new TestSuite();
68         suite.setName(TestReadResource.class.getName());
69         
70         suite.addTest(new TestReadResource("testReadBeforeReleaseDate"));
71         suite.addTest(new TestReadResource("testReadInValidTimeRange"));
72         suite.addTest(new TestReadResource("testReadAfterExpirationDate"));
73         suite.addTest(new TestReadResource("testReadBeforeReleaseDateIgnore"));
74         suite.addTest(new TestReadResource("testReadInValidTimeRangeIgnore"));
75         suite.addTest(new TestReadResource("testReadAfterExpirationDateIgnore"));
76         
77         TestSetup wrapper = new TestSetup(suite) {
78
79             protected void setUp() {
80                 setupOpenCms("simpletest", "/sites/default/");
81             }
82             
83             protected void tearDown() {
84                 removeOpenCms();
85             }
86         };
87         
88         return wrapper;
89     }
90     
91     /**
92      * Test readResource of a file before its release date.<p>
93      *
94      * @param tc the OpenCmsTestCase
95      * @param cms the CmsObject
96      * @param resource1 the resource to touch
97      * @param filter the filter to use
98      * @throws Throwable if something goes wrong
99      */

100     public static void readBeforeReleaseDate(OpenCmsTestCase tc, CmsObject cms, String JavaDoc resource1, CmsResourceFilter filter) throws Throwable JavaDoc {
101               
102         tc.storeResources(cms, resource1);
103       
104         // preperation, modify the release date
105
CmsFile preperationRes = cms.readFile(resource1, CmsResourceFilter.ALL);
106         // set the release date to one hour in the future
107
preperationRes .setDateReleased(System.currentTimeMillis() + (60 * 60 *1000));
108         
109         cms.lockResource(resource1);
110         cms.writeFile(preperationRes);
111         cms.unlockResource(resource1);
112         
113         // now try to access the resource
114
try {
115             cms.readResource(resource1, filter);
116             if (!filter.includeDeleted()) {
117                 // the file could be read, despite the release date set in the future
118
fail("Resource "+ resource1+ " could be read before release date");
119             }
120         } catch (CmsException e) {
121             if (filter.includeDeleted()) {
122                 fail("Resource "+ resource1+ " could not be read");
123             }
124         }
125     }
126
127     /**
128      * Test readResource of a file after its expirationrelease date.<p>
129      *
130      * @param tc the OpenCmsTestCase
131      * @param cms the CmsObject
132      * @param resource1 the resource to touch
133      * @param filter the filter to use
134      * @throws Throwable if something goes wrong
135      */

136     public static void readAfterExpirationDate(OpenCmsTestCase tc, CmsObject cms, String JavaDoc resource1, CmsResourceFilter filter) throws Throwable JavaDoc {
137               
138         tc.storeResources(cms, resource1);
139       
140         // preperation, modify the expiration date
141
CmsFile preperationRes = cms.readFile(resource1, CmsResourceFilter.ALL);
142         // set the expiration date to one hour in the past
143
preperationRes.setDateExpired(System.currentTimeMillis() - (60 * 60 *1000));
144
145         cms.lockResource(resource1);
146         cms.writeFile(preperationRes);
147         cms.unlockResource(resource1);
148         
149         // now try to access the resource
150
try {
151             cms.readResource(resource1, filter);
152             if (!filter.includeDeleted()) {
153                 // the file could be read, despite the expiration date was set to the past
154
fail("Resource "+ resource1+ " could be read after the expiration date");
155             }
156         } catch (CmsException e) {
157             if (filter.includeDeleted()) {
158                 fail("Resource "+ resource1+ " could not be read");
159             }
160         }
161     }
162   
163     /**
164      * Test readResource of a file in its valid time range.<p>
165      *
166      * @param tc the OpenCmsTestCase
167      * @param cms the CmsObject
168      * @param resource1 the resource to touch
169      * @param filter the filter to use
170      * @throws Throwable if something goes wrong
171      */

172     public static void readInValidTimeRange(OpenCmsTestCase tc, CmsObject cms, String JavaDoc resource1, CmsResourceFilter filter) throws Throwable JavaDoc {
173               
174         tc.storeResources(cms, resource1);
175       
176         // preperation, modify the expiration date
177
CmsFile preperationRes = cms.readFile(resource1, CmsResourceFilter.ALL);
178         // set the release date to one hour in the future
179
preperationRes.setDateReleased(System.currentTimeMillis()- (60 * 60 *1000));
180         // set the expiration date to one hour in the past
181
preperationRes.setDateExpired(System.currentTimeMillis() + (60 * 60 *1000));
182         
183         cms.lockResource(resource1);
184         cms.writeFile(preperationRes);
185         cms.unlockResource(resource1);
186         
187         // now try to access the resource
188
try {
189             cms.readResource(resource1, filter);
190         } catch (CmsException e) {
191                 fail("Resource "+ resource1+ " could not be read");
192         }
193     }
194     
195     /**
196      * Test readResource of a file before its release date.<p>
197      *
198      * @throws Throwable if something goes wrong
199      */

200     public void testReadBeforeReleaseDate() throws Throwable JavaDoc {
201         
202         CmsObject cms = getCmsObject();
203         echo("Testing readFileHeader of a file before the release date");
204         readBeforeReleaseDate(this, cms, "/folder1/page1.html", CmsResourceFilter.DEFAULT);
205     }
206     
207     /**
208      * Test readFileHeader of a file after its expiration date.<p>
209      *
210      * @throws Throwable if something goes wrong
211      */

212     public void testReadAfterExpirationDate() throws Throwable JavaDoc {
213         
214         CmsObject cms = getCmsObject();
215         echo("Testing readFileHeader of a file after the expiration date");
216         readAfterExpirationDate(this, cms, "/folder1/page2.html", CmsResourceFilter.DEFAULT);
217     }
218
219     /**
220      * Test readFileHeader of a file in its valid time range.<p>
221      *
222      * @throws Throwable if something goes wrong
223      */

224     public void testReadInValidTimeRange() throws Throwable JavaDoc {
225         
226         CmsObject cms = getCmsObject();
227         echo("Testing readFileHeader of a file in its valid time range");
228         readInValidTimeRange(this, cms, "/folder1/page3.html", CmsResourceFilter.DEFAULT);
229     }
230     
231     /**
232      * Test readFileHeader of a file before its release date.<p>
233      * The valid time range will be ignored.
234      *
235      * @throws Throwable if something goes wrong
236      */

237     public void testReadBeforeReleaseDateIgnore() throws Throwable JavaDoc {
238         
239         CmsObject cms = getCmsObject();
240         echo("Testing readFileHeader of a file before the release date, ignoring valid timerange");
241         readBeforeReleaseDate(this, cms, "/folder1/page1.html", CmsResourceFilter.ALL);
242     }
243     
244     /**
245      * Test readFileHeader of a file after its expiration date.<p>
246      * The valid time range will be ignored.
247      *
248      * @throws Throwable if something goes wrong
249      */

250     public void testReadAfterExpirationDateIgnore() throws Throwable JavaDoc {
251         
252         CmsObject cms = getCmsObject();
253         echo("Testing readFileHeader of a file after the expiration date, ignoring valid timerange");
254         readAfterExpirationDate(this, cms, "/folder1/page2.html", CmsResourceFilter.ALL);
255     }
256
257     /**
258      * Test readFileHeader of a file in its valid time range.<p>
259      * The valid time range will be ignored.
260      *
261      * @throws Throwable if something goes wrong
262      */

263     public void testReadInValidTimeRangeIgnore() throws Throwable JavaDoc {
264         
265         CmsObject cms = getCmsObject();
266         echo("Testing readFileHeader of a file in its valid time range, ignoring valid timerange");
267         readInValidTimeRange(this, cms, "/folder1/page3.html", CmsResourceFilter.ALL);
268     }
269     
270 }
271
Popular Tags