KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > module > sitemesh > mapper > ConfigLoaderTest


1 package com.opensymphony.module.sitemesh.mapper;
2
3 import junit.framework.TestCase;
4
5 import java.io.BufferedWriter JavaDoc;
6 import java.io.File JavaDoc;
7 import java.io.FileWriter JavaDoc;
8
9 public class ConfigLoaderTest extends TestCase {
10
11     private ConfigLoader configLoader;
12     private File JavaDoc tempConfigFile;
13
14     protected void setUp() throws Exception JavaDoc {
15         super.setUp();
16
17         // create temp file
18
tempConfigFile = File.createTempFile("decorators-test", ".xml");
19         tempConfigFile.deleteOnExit();
20
21         // write to temp file
22
BufferedWriter JavaDoc out = new BufferedWriter JavaDoc(new FileWriter JavaDoc(tempConfigFile));
23         out.write("<decorators defaultdir=\"/decorators\">");
24
25         // new format test decorators
26

27         out.write(" <decorator name=\"default\" page=\"default.jsp\">");
28         out.write(" <pattern>/info/*</pattern>");
29         out.write(" <url-pattern> ");
30         out.write(" /test/*");
31         out.write(" </url-pattern>");
32         out.write(" <url-pattern> </url-pattern>");
33         out.write(" <url-pattern></url-pattern>");
34         out.write(" </decorator>");
35
36         out.write(" <decorator name=\"other\" page=\"/other.jsp\">");
37         out.write(" <pattern>/other/*</pattern>");
38         out.write(" </decorator>");
39
40         out.write(" <decorator name=\"uri\" page=\"uri.jsp\" webapp=\"someapp\">");
41         out.write(" <pattern>/uri/*</pattern>");
42         out.write(" </decorator>");
43
44         out.write(" <decorator name=\"rolebased\" page=\"rolebased.jsp\" role=\"developer\">");
45         out.write(" <pattern>/rolebased/*</pattern>");
46         out.write(" </decorator>");
47
48         // old format test decorator
49

50         out.write(" <decorator>");
51         out.write(" <decorator-name>old</decorator-name>");
52         out.write(" <resource>/decorators/old.jsp</resource>");
53         out.write(" </decorator>");
54
55         out.write(" <decorator-mapping>");
56         out.write(" <decorator-name>old</decorator-name>");
57         out.write(" <url-pattern>/old/*</url-pattern>");
58         out.write(" </decorator-mapping>");
59
60         out.write(" <decorator-mapping>");
61         out.write(" <decorator-name>old2</decorator-name>");
62         out.write(" <url-pattern></url-pattern>");
63         out.write(" </decorator-mapping>");
64
65         out.write("</decorators>");
66         out.close();
67         configLoader = new ConfigLoader(tempConfigFile);
68     }
69
70     protected void tearDown() throws Exception JavaDoc {
71         if (tempConfigFile != null) tempConfigFile.delete();
72         configLoader = null;
73     }
74
75     public void testMappedNames() throws Exception JavaDoc {
76         assertEquals(configLoader.getMappedName("/info/somepage.html"), "default");
77         assertEquals(configLoader.getMappedName("/test/somepage.html"), "default");
78
79         assertEquals(configLoader.getMappedName("/other/someotherpage.html"), "other");
80
81         assertEquals(configLoader.getMappedName("/uri/somepage.html"), "uri");
82
83         assertEquals(configLoader.getMappedName("/rolebased/someotherpage.html"), "rolebaseddeveloper");
84
85         assertEquals(configLoader.getMappedName("/old/someoldpage.html"), "old");
86     }
87
88     public void testDecoratorPresence() throws Exception JavaDoc {
89         assertNotNull(configLoader.getDecoratorByName("default"));
90         assertNotNull(configLoader.getDecoratorByName("other"));
91         assertNotNull(configLoader.getDecoratorByName("uri"));
92         assertNotNull(configLoader.getDecoratorByName("rolebaseddeveloper"));
93         assertNotNull(configLoader.getDecoratorByName("old"));
94     }
95
96     public void testDecorators() throws Exception JavaDoc {
97         assertEquals(configLoader.getDecoratorByName("default").getName(), "default");
98         assertEquals(configLoader.getDecoratorByName("default").getPage(), "/decorators/default.jsp");
99         assertNull(configLoader.getDecoratorByName("default").getURIPath());
100         assertNull(configLoader.getDecoratorByName("default").getRole());
101
102         assertEquals(configLoader.getDecoratorByName("other").getName(), "other");
103         assertEquals(configLoader.getDecoratorByName("other").getPage(), "/other.jsp"); // absolute path
104
assertNull(configLoader.getDecoratorByName("other").getURIPath());
105         assertNull(configLoader.getDecoratorByName("other").getRole());
106
107         assertEquals(configLoader.getDecoratorByName("uri").getName(), "uri");
108         assertEquals(configLoader.getDecoratorByName("uri").getPage(), "/decorators/uri.jsp");
109         assertEquals(configLoader.getDecoratorByName("uri").getURIPath(), "/someapp");
110         assertNull(configLoader.getDecoratorByName("uri").getRole());
111
112         assertEquals(configLoader.getDecoratorByName("rolebaseddeveloper").getName(), "rolebased");
113         assertEquals(configLoader.getDecoratorByName("rolebaseddeveloper").getPage(), "/decorators/rolebased.jsp");
114         assertNull(configLoader.getDecoratorByName("rolebaseddeveloper").getURIPath());
115         assertEquals(configLoader.getDecoratorByName("rolebaseddeveloper").getRole(), "developer");
116
117         assertEquals(configLoader.getDecoratorByName("old").getName(), "old");
118         assertEquals(configLoader.getDecoratorByName("old").getPage(), "/decorators/old.jsp");
119         assertNull(configLoader.getDecoratorByName("old").getURIPath());
120         assertNull(configLoader.getDecoratorByName("old").getRole());
121     }
122 }
Popular Tags