01: package com.opensymphony.module.sitemesh.mapper;
02:
03: import junit.framework.TestCase;
04:
05: public class PathMapperTest extends TestCase {
06:
07: private PathMapper pathMapper;
08:
09: protected void setUp() throws Exception {
10: super .setUp();
11:
12: pathMapper = new PathMapper();
13:
14: // exact matches come first
15: pathMapper.put("exact1", "/myexactfile.html");
16: pathMapper.put("exact2", "/mydir/myexactfile.html");
17: pathMapper.put("exact3", "/mydir/myexactfile.jsp");
18: pathMapper.put("exact4", "/mydir/dodo");
19:
20: // then the complex matches
21: pathMapper.put("complex1", "/mydir/*");
22: pathMapper.put("complex2", "/mydir/otherdir/*.jsp");
23: pathMapper.put("complex3", "/otherdir/*.??p");
24: pathMapper.put("complex4", "*.xml");
25: pathMapper.put("complex5", "/*/admin/*.??ml");
26: pathMapper.put("complex6", "/*/complexx/a*b.x?tml");
27:
28: // if all the rest fails, use the default matches
29: pathMapper.put("default", "*");
30: }
31:
32: public void testHardening() throws Exception {
33: PathMapper bad = new PathMapper();
34: bad.put(null, null);
35: assertNull(bad.get(null));
36: assertNull(bad.get(""));
37: assertNull(bad.get("/somenonexistingpath"));
38: }
39:
40: public void testFindExactKey() throws Exception {
41: assertEquals("exact1", pathMapper.get("/myexactfile.html"));
42: assertEquals("exact2", pathMapper
43: .get("/mydir/myexactfile.html"));
44: assertEquals("exact3", pathMapper.get("/mydir/myexactfile.jsp"));
45: assertEquals("exact4", pathMapper.get("/mydir/dodo"));
46: }
47:
48: public void testFindComplexKey() throws Exception {
49: assertEquals("complex1", pathMapper.get("/mydir/"));
50: assertEquals("complex1", pathMapper.get("/mydir/test1.xml"));
51: assertEquals("complex1", pathMapper.get("/mydir/test321.jsp"));
52: assertEquals("complex1", pathMapper.get("/mydir/otherdir"));
53:
54: assertEquals("complex2", pathMapper
55: .get("/mydir/otherdir/test321.jsp"));
56:
57: assertEquals("complex3", pathMapper.get("/otherdir/test2.jsp"));
58: assertEquals("complex3", pathMapper.get("/otherdir/test2.bpp"));
59:
60: assertEquals("complex4", pathMapper
61: .get("/somedir/one/two/some/deep/file/test.xml"));
62: assertEquals("complex4", pathMapper.get("/somedir/321.jsp.xml"));
63:
64: assertEquals("complex5", pathMapper
65: .get("/mydir/otherdir/admin/myfile.html"));
66: assertEquals("complex5", pathMapper
67: .get("/mydir/somedir/admin/text.html"));
68:
69: assertEquals("complex6", pathMapper
70: .get("/mydir/complexx/a-some-test-b.xctml"));
71: assertEquals("complex6", pathMapper
72: .get("/mydir/complexx/a b.xhtml"));
73: assertEquals("complex6", pathMapper
74: .get("/mydir/complexx/a___b.xhtml"));
75: }
76:
77: public void testFindDefaultKey() throws Exception {
78: assertEquals("default", pathMapper.get(null));
79: assertEquals("default", pathMapper.get("/"));
80: assertEquals("default", pathMapper.get("/*"));
81: assertEquals("default", pathMapper.get("*"));
82: assertEquals("default", pathMapper.get("blah.txt"));
83: assertEquals("default", pathMapper
84: .get("somefilewithoutextension"));
85: assertEquals("default", pathMapper
86: .get("/file_with_underscores-and-dashes.test"));
87: assertEquals("default", pathMapper
88: .get("/tuuuu*/file.with.dots.test.txt"));
89: }
90: }
|