KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > staticexport > TestCmsStaticExportManager


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/staticexport/TestCmsStaticExportManager.java,v $
3  * Date : $Date: 2006/03/27 14:52:51 $
4  * Version: $Revision: 1.11 $
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.staticexport;
33
34 import org.opencms.file.CmsObject;
35 import org.opencms.file.CmsProperty;
36 import org.opencms.file.CmsPropertyDefinition;
37 import org.opencms.main.OpenCms;
38 import org.opencms.test.OpenCmsTestCase;
39 import org.opencms.test.OpenCmsTestProperties;
40 import org.opencms.util.CmsStringUtil;
41
42 import java.util.regex.Pattern JavaDoc;
43
44 import junit.extensions.TestSetup;
45 import junit.framework.Test;
46 import junit.framework.TestSuite;
47
48 /**
49  * Tests for static export manager.<p>
50  *
51  * @author Alexander Kandzior
52  *
53  * @version $Revision: 1.11 $
54  *
55  * @version $Revision: 1.11 $
56  *
57  * @since 6.0.0
58  */

59 public class TestCmsStaticExportManager extends OpenCmsTestCase {
60
61     /**
62      * Default JUnit constructor.<p>
63      *
64      * @param arg0 JUnit parameters
65      */

66     public TestCmsStaticExportManager(String JavaDoc arg0) {
67
68         super(arg0);
69     }
70
71     /**
72      * Test suite for this test class.<p>
73      *
74      * @return the test suite
75      */

76     public static Test suite() {
77
78         OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH);
79
80         TestSuite suite = new TestSuite();
81         suite.setName(TestCmsStaticExportManager.class.getName());
82
83         suite.addTest(new TestCmsStaticExportManager("testExportJspLinkGeneration"));
84         suite.addTest(new TestCmsStaticExportManager("testDefaultSuffixLinkGeneration"));
85
86         TestSetup wrapper = new TestSetup(suite) {
87
88             protected void setUp() {
89
90                 setupOpenCms("simpletest", "/sites/default/");
91             }
92
93             protected void tearDown() {
94
95                 removeOpenCms();
96             }
97
98         };
99
100         return wrapper;
101     }
102
103     /**
104      * Tests the link generation for statically exported files by default suffix.<p>
105      *
106      * @throws Exception if the test fails
107      */

108     public void testDefaultSuffixLinkGeneration() throws Exception JavaDoc {
109
110         CmsObject cms = getCmsObject();
111         echo("Testing default suffix statix export link generation");
112
113         String JavaDoc folder = "/folder1/subfolder11/subsubfolder111/";
114         String JavaDoc vfsName1 = folder + "image.gif";
115         String JavaDoc vfsName2 = folder + "xml.xml";
116         // make sure "export" is not set for the folder
117
CmsProperty exportProp = cms.readPropertyObject(vfsName1, CmsPropertyDefinition.PROPERTY_EXPORT, true);
118         assertTrue(exportProp.isNullProperty());
119
120         // make sure static export on default is disabled
121
OpenCms.getStaticExportManager().setDefault(CmsStringUtil.FALSE);
122         String JavaDoc rfsPrefix = OpenCms.getStaticExportManager().getRfsPrefix(cms.getRequestContext().getSiteRoot() + folder);
123         String JavaDoc expected1, expected2;
124
125         cms.getRequestContext().setCurrentProject(cms.readProject("Online"));
126
127         echo("Testing default export based on file suffix");
128
129         assertTrue(OpenCms.getStaticExportManager().isSuffixExportable(vfsName1));
130         expected1 = rfsPrefix + cms.getRequestContext().getSiteRoot() + vfsName1;
131         checkLinkWithoutParameters(cms, vfsName1, expected1);
132         checkLinkWithParameters(cms, vfsName1, expected1);
133         assertEquals(expected1, OpenCms.getLinkManager().substituteLink(cms, vfsName1));
134
135         assertFalse(OpenCms.getStaticExportManager().isSuffixExportable(vfsName2));
136         expected1 = rfsPrefix + cms.getRequestContext().getSiteRoot() + vfsName2;
137         checkLinkWithoutParameters(cms, vfsName2, expected1);
138         checkLinkWithParameters(cms, vfsName2, expected1);
139         expected2 = OpenCms.getStaticExportManager().getVfsPrefix() + vfsName2;
140         assertEquals(expected2, OpenCms.getLinkManager().substituteLink(cms, vfsName2));
141
142         echo("Testing default export based on file suffix with 'exportname' property set");
143
144         cms.getRequestContext().setCurrentProject(cms.readProject("Offline"));
145
146         // set "exportname" property to JSP
147
cms.lockResource(folder);
148         cms.writePropertyObject(folder, new CmsProperty(CmsPropertyDefinition.PROPERTY_EXPORTNAME, "testfolder", null));
149         cms.unlockResource(folder);
150         // publish the changes
151
cms.publishProject();
152
153         cms.getRequestContext().setCurrentProject(cms.readProject("Online"));
154
155         assertTrue(OpenCms.getStaticExportManager().isSuffixExportable(vfsName1));
156         expected1 = rfsPrefix + "/testfolder/image.gif";
157         checkLinkWithoutParameters(cms, vfsName1, expected1);
158         checkLinkWithParameters(cms, vfsName1, expected1);
159         assertEquals(expected1, OpenCms.getLinkManager().substituteLink(cms, vfsName1));
160
161         assertFalse(OpenCms.getStaticExportManager().isSuffixExportable(vfsName2));
162         expected1 = rfsPrefix + "/testfolder/xml.xml";
163         checkLinkWithoutParameters(cms, vfsName2, expected1);
164         checkLinkWithParameters(cms, vfsName2, expected1);
165         expected2 = OpenCms.getStaticExportManager().getVfsPrefix() + vfsName2;
166         assertEquals(expected2, OpenCms.getLinkManager().substituteLink(cms, vfsName2));
167     }
168
169     /**
170      * Tests the link generation for statically exported JSP files.<p>
171      *
172      * @throws Exception if the test fails
173      */

174     public void testExportJspLinkGeneration() throws Exception JavaDoc {
175
176         CmsObject cms = getCmsObject();
177         echo("Testing the link generation for exported JSP pages");
178
179         // fist setup a little test scenario with the imported data
180
String JavaDoc folder = "/types/";
181         String JavaDoc vfsName = folder + "jsp.jsp";
182         CmsProperty exportProp = new CmsProperty(CmsPropertyDefinition.PROPERTY_EXPORT, CmsStringUtil.TRUE, null);
183         cms.lockResource(folder);
184         cms.writePropertyObject(folder, exportProp);
185         cms.unlockResource(folder);
186         cms.lockResource(vfsName);
187         cms.writePropertyObject(vfsName, exportProp);
188         cms.unlockResource(vfsName);
189         // publish the changes
190
cms.publishProject();
191
192         String JavaDoc rfsPrefix = OpenCms.getStaticExportManager().getRfsPrefix(cms.getRequestContext().getSiteRoot() + folder);
193         String JavaDoc expected;
194
195         echo("Testing basic export name generating functions for a JSP");
196
197         expected = rfsPrefix + cms.getRequestContext().getSiteRoot() + vfsName + ".html";
198         checkLinkWithoutParameters(cms, vfsName, expected);
199         checkLinkWithParameters(cms, vfsName, expected);
200
201         echo("Testing export name generating functions for a JSP with 'exportname' property set");
202
203         // set "exportname" property to JSP
204
cms.lockResource(folder);
205         cms.writePropertyObject(folder, new CmsProperty(CmsPropertyDefinition.PROPERTY_EXPORTNAME, "myfolder", null));
206         cms.unlockResource(folder);
207         // publish the changes
208
cms.publishProject();
209
210         expected = rfsPrefix + "/myfolder/jsp.jsp.html";
211         checkLinkWithoutParameters(cms, vfsName, expected);
212         checkLinkWithParameters(cms, vfsName, expected);
213
214         echo("Testing export name generating functions for a JSP with 'exportname' property AND 'exportsuffix' property set");
215
216         // set "exportsuffix" property to JSP
217
cms.lockResource(vfsName);
218         cms.writePropertyObject(vfsName, new CmsProperty(CmsPropertyDefinition.PROPERTY_EXPORTSUFFIX, ".txt", null));
219         cms.unlockResource(vfsName);
220         // publish the changes
221
cms.publishProject();
222
223         expected = rfsPrefix + "/myfolder/jsp.jsp.txt";
224         checkLinkWithoutParameters(cms, vfsName, expected);
225         checkLinkWithParameters(cms, vfsName, expected);
226
227         echo("Testing export name generating functions for a JSP with only 'exportsuffix' property set");
228
229         // remove "exportname" property from JSP
230
cms.lockResource(folder);
231         cms.writePropertyObject(folder, new CmsProperty(
232             CmsPropertyDefinition.PROPERTY_EXPORTNAME,
233             CmsProperty.DELETE_VALUE,
234             CmsProperty.DELETE_VALUE));
235         cms.unlockResource(folder);
236         cms.lockResource(vfsName);
237         cms.writePropertyObject(vfsName, new CmsProperty(CmsPropertyDefinition.PROPERTY_EXPORTSUFFIX, ".pdf", null));
238         cms.unlockResource(vfsName);
239         // publish the changes
240
cms.publishProject();
241
242         expected = rfsPrefix + cms.getRequestContext().getSiteRoot() + vfsName + ".pdf";
243         checkLinkWithoutParameters(cms, vfsName, expected);
244         checkLinkWithParameters(cms, vfsName, expected);
245     }
246
247     /**
248      * Checks a link that has no parameters.<p>
249      *
250      * @param cms the cms context
251      * @param vfsName the vfsName of the resource to test the link for
252      * @param expected the previous generated link
253      */

254     private void checkLinkWithoutParameters(CmsObject cms, String JavaDoc vfsName, String JavaDoc expected) {
255
256         // check JSP without parameters
257
String JavaDoc rfsName = OpenCms.getStaticExportManager().getRfsName(cms, vfsName);
258         System.out.println("RFS name: " + rfsName + " VFS name: " + vfsName);
259         assertEquals(expected, rfsName);
260         assertEquals(vfsName, OpenCms.getStaticExportManager().getVfsName(cms, rfsName));
261     }
262
263     /**
264      * Checks a link that has parameters.<p>
265      *
266      * @param cms the cms context
267      * @param vfsName the vfsName of the resource to test the link for
268      * @param expected the previous generated link
269      */

270     private void checkLinkWithParameters(CmsObject cms, String JavaDoc vfsName, String JavaDoc expected) {
271
272         // check JSP WITH parameters
273
String JavaDoc rfsName = OpenCms.getStaticExportManager().getRfsName(cms, vfsName, "?a=b&c=d");
274         System.out.println("RFS name: " + rfsName + " VFS name: " + vfsName);
275         String JavaDoc extension = expected.substring(expected.lastIndexOf('.'));
276         Pattern JavaDoc pattern = Pattern.compile("^"
277             + CmsStringUtil.escapePattern(expected + "_")
278             + "\\d*"
279             + CmsStringUtil.escapePattern(extension)
280             + "$");
281         assertTrue(pattern.matcher(rfsName).matches());
282         assertEquals(vfsName, OpenCms.getStaticExportManager().getVfsName(cms, rfsName));
283     }
284 }
285
Popular Tags