KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/file/TestTouch.java,v $
3  * Date : $Date: 2006/09/21 09:34:47 $
4  * Version: $Revision: 1.20 $
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.test.OpenCmsTestCase;
35 import org.opencms.test.OpenCmsTestProperties;
36 import org.opencms.test.OpenCmsTestResourceFilter;
37
38 import java.util.Iterator JavaDoc;
39 import java.util.List JavaDoc;
40
41 import junit.extensions.TestSetup;
42 import junit.framework.Test;
43 import junit.framework.TestSuite;
44
45 /**
46  * Unit test for the "touch" method of the CmsObject.<p>
47  *
48  * @author Michael Emmerich
49  * @version $Revision: 1.20 $
50  */

51 public class TestTouch extends OpenCmsTestCase {
52   
53     /**
54      * Default JUnit constructor.<p>
55      *
56      * @param arg0 JUnit parameters
57      */

58     public TestTouch(String JavaDoc arg0) {
59         super(arg0);
60     }
61     
62     /**
63      * Test suite for this test class.<p>
64      *
65      * @return the test suite
66      */

67     public static Test suite() {
68         OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH);
69         
70         TestSuite suite = new TestSuite();
71         suite.setName(TestTouch.class.getName());
72         
73         suite.addTest(new TestTouch("testTouchFile"));
74         suite.addTest(new TestTouch("testTouchFolder"));
75         suite.addTest(new TestTouch("testTouchFolderRecursive"));
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 the touch method to touch a single resource.<p>
93      * @param tc the OpenCmsTestCase
94      * @param cms the CmsObject
95      * @param resource1 the resource to touch
96      * @throws Throwable if something goes wrong
97      */

98     public static void touchResource(OpenCmsTestCase tc, CmsObject cms, String JavaDoc resource1) throws Throwable JavaDoc {
99        
100         tc.storeResources(cms, resource1);
101
102         long timestamp = System.currentTimeMillis();
103         cms.lockResource(resource1);
104         cms.setDateLastModified(resource1, timestamp, false);
105         cms.unlockResource(resource1);
106
107         // now evaluate the result
108
tc.assertFilter(cms, resource1, OpenCmsTestResourceFilter.FILTER_TOUCH);
109         // project must be current project
110
tc.assertProject(cms, resource1, cms.getRequestContext().currentProject());
111         // state must be "changed"
112
tc.assertState(cms, resource1, tc.getPreCalculatedState(resource1));
113         // date last modified must be the date set in the tough operation
114
tc.assertDateLastModified(cms, resource1, timestamp);
115         // the user last modified must be the current user
116
tc.assertUserLastModified(cms, resource1, cms.getRequestContext().currentUser());
117     }
118     
119     /**
120      * Test the touch method to touch a single folder.<p>
121      * @param tc the OpenCmsTestCase
122      * @param cms the CmsObject
123      * @param resource1 the resource to touch
124      * @throws Throwable if something goes wrong
125      */

126     public static void touchResources(OpenCmsTestCase tc, CmsObject cms, String JavaDoc resource1) throws Throwable JavaDoc {
127
128         tc.storeResources(cms, resource1);
129          
130         long timestamp = System.currentTimeMillis();
131         cms.lockResource(resource1);
132         cms.setDateLastModified(resource1, timestamp, false);
133         cms.unlockResource(resource1);
134
135         // now evaluate the result
136
tc.assertFilter(cms, resource1, OpenCmsTestResourceFilter.FILTER_TOUCH);
137         // project must be current project
138
tc.assertProject(cms, resource1, cms.getRequestContext().currentProject());
139         // state must be "changed"
140
tc.assertState(cms, resource1, tc.getPreCalculatedState(resource1));
141         // date last modified must be the date set in the tough operation
142
tc.assertDateLastModified(cms, resource1, timestamp);
143         // the user last modified must be the current user
144
tc.assertUserLastModified(cms, resource1, cms.getRequestContext().currentUser());
145         
146         // evaluate all subresources
147
List JavaDoc subresources = cms.readResources(resource1, CmsResourceFilter.ALL);
148         
149         // iterate through the subresources
150
Iterator JavaDoc i = subresources.iterator();
151         while (i.hasNext()) {
152             CmsResource res = (CmsResource)i.next();
153             String JavaDoc resName = cms.getSitePath(res);
154             // now evaluate the result
155
tc.assertFilter(cms, resName, OpenCmsTestResourceFilter.FILTER_EQUAL);
156         }
157     }
158     
159     /**
160      * Test the touch method to touch a complete subtree.<p>
161      * @param tc the OpenCmsTestCase
162      * @param cms the CmsObject
163      * @param resource1 the resource to touch
164      * @throws Throwable if something goes wrong
165      */

166     public static void touchResourcesRecursive(OpenCmsTestCase tc, CmsObject cms, String JavaDoc resource1) throws Throwable JavaDoc {
167             
168         tc.storeResources(cms, resource1);
169         
170         long timestamp = System.currentTimeMillis();
171         cms.lockResource(resource1);
172         cms.setDateLastModified(resource1, timestamp, true);
173         cms.unlockResource(resource1);
174
175         // now evaluate the result
176
tc.assertFilter(cms, resource1, OpenCmsTestResourceFilter.FILTER_TOUCH);
177         // project must be current project
178
tc.assertProject(cms, resource1, cms.getRequestContext().currentProject());
179         // state must be "changed"
180
tc.assertState(cms, resource1, tc.getPreCalculatedState(resource1));
181         // date last modified must be the date set in the tough operation
182
tc.assertDateLastModified(cms, resource1, timestamp);
183         // the user last modified must be the current user
184
tc.assertUserLastModified(cms, resource1, cms.getRequestContext().currentUser());
185         
186         // evaluate all subresources
187
List JavaDoc subresources = cms.readResources(resource1, CmsResourceFilter.ALL);
188         
189         // iterate through the subresources
190
Iterator JavaDoc i = subresources.iterator();
191         while (i.hasNext()) {
192             CmsResource res = (CmsResource)i.next();
193             String JavaDoc resName = cms.getSitePath(res);
194             // now evaluate the result
195
tc.assertFilter(cms, resName, OpenCmsTestResourceFilter.FILTER_TOUCH);
196             // project must be current project
197
tc.assertProject(cms, resName, cms.getRequestContext().currentProject());
198             // state must be "changed"
199
tc.assertState(cms, resName, tc.getPreCalculatedState(resName));
200             // date last modified must be the date set in the tough operation
201
tc.assertDateLastModified(cms, resName, timestamp);
202             // the user last modified must be the current user
203
tc.assertUserLastModified(cms, resName, cms.getRequestContext().currentUser());
204         }
205     }
206     
207     /**
208      * Test the touch method on a file.<p>
209      *
210      * @throws Throwable if something goes wrong
211      */

212     public void testTouchFile() throws Throwable JavaDoc {
213
214         CmsObject cms = getCmsObject();
215         echo("Testing touch on file");
216         touchResource(this, cms, "/index.html");
217     }
218     
219     /**
220      * Test the touch method on a folder.<p>
221      *
222      * @throws Throwable if something goes wrong
223      */

224     public void testTouchFolder() throws Throwable JavaDoc {
225         
226         CmsObject cms = getCmsObject();
227         echo("Testing touch on a folder (without recursion)");
228         touchResources(this, cms, "/folder1/");
229     }
230     
231     /**
232      * Test the touch method on a folder and recusivly on all resources in the folder.<p>
233      *
234      * @throws Throwable if something goes wrong
235      */

236     public void testTouchFolderRecursive() throws Throwable JavaDoc {
237         
238         CmsObject cms = getCmsObject();
239         echo("Testing touch on a folder (_with_ recursion)");
240         touchResourcesRecursive(this, cms, "/folder2/");
241     }
242 }
243
Popular Tags