01: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
02: * This code is licensed under the GPL 2.0 license, availible at the root
03: * application directory.
04: */
05: package org.geoserver.template;
06:
07: import junit.framework.TestCase;
08: import org.geoserver.platform.GeoServerResourceLoader;
09: import org.springframework.web.context.support.GenericWebApplicationContext;
10: import org.vfny.geoserver.global.GeoserverDataDirectory;
11: import java.io.File;
12: import java.io.IOException;
13:
14: public class GeoServerTemplateLoaderTest extends TestCase {
15: public void test() throws Exception {
16: File data = File.createTempFile(getName(), "template");
17: data.delete();
18: data.mkdir();
19:
20: System
21: .setProperty("GEOSERVER_DATA_DIR", data
22: .getAbsolutePath());
23:
24: File templates = new File(data, "templates");
25: templates.mkdir();
26:
27: File featureTypes = new File(data, "featureTypes");
28: featureTypes.mkdir();
29:
30: File featureType1 = new File(featureTypes, "ft1");
31: featureType1.mkdir();
32:
33: File featureType2 = new File(featureTypes, "ft2");
34: featureType2.mkdir();
35:
36: try {
37: GeoServerResourceLoader loader = new GeoServerResourceLoader(
38: data);
39: GenericWebApplicationContext context = new GenericWebApplicationContext();
40: context.getBeanFactory().registerSingleton(
41: "resourceLoader", loader);
42:
43: GeoserverDataDirectory.init(context);
44:
45: GeoServerTemplateLoader templateLoader = new GeoServerTemplateLoader(
46: getClass());
47:
48: //test a path relative to templates
49: File expected = new File(templates, "1.ftl");
50: expected.createNewFile();
51:
52: File actual = (File) templateLoader
53: .findTemplateSource("1.ftl");
54: assertEquals(expected.getCanonicalPath(), actual
55: .getCanonicalPath());
56:
57: //test a path relative to featureTypes
58: expected = new File(featureType1, "2.ftl");
59: expected.createNewFile();
60:
61: actual = (File) templateLoader
62: .findTemplateSource("ft1/2.ftl");
63: assertEquals(expected.getCanonicalPath(), actual
64: .getCanonicalPath());
65:
66: actual = (File) templateLoader.findTemplateSource("2.ftl");
67: assertNull(actual);
68:
69: // Removed this for the moment, I need to setup a mock catalog in
70: // order to test again feature type specific template loading
71: // templateLoader.setFeatureType("ft1");
72: // actual = (File) templateLoader.findTemplateSource("2.ftl");
73: // assertEquals(expected.getCanonicalPath(),
74: // actual.getCanonicalPath());
75:
76: //test loading relative to class
77: Object source = templateLoader
78: .findTemplateSource("FeatureSimple.ftl");
79: assertNotNull(source);
80: assertFalse(source instanceof File);
81: templateLoader.getReader(source, "UTF-8");
82: } finally {
83: delete(data);
84: }
85: }
86:
87: void delete(File file) throws IOException {
88: if (file.isDirectory()) {
89: File[] files = file.listFiles();
90:
91: for (int i = 0; i < files.length; i++) {
92: delete(files[i]);
93: }
94: }
95:
96: file.delete();
97: }
98: }
|