01: //========================================================================
02: //$Id$
03: //Copyright 2006 Mort Bay Consulting Pty. Ltd.
04: //------------------------------------------------------------------------
05: //Licensed under the Apache License, Version 2.0 (the "License");
06: //you may not use this file except in compliance with the License.
07: //You may obtain a copy of the License at
08: //http://www.apache.org/licenses/LICENSE-2.0
09: //Unless required by applicable law or agreed to in writing, software
10: //distributed under the License is distributed on an "AS IS" BASIS,
11: //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: //See the License for the specific language governing permissions and
13: //limitations under the License.
14: //========================================================================
15:
16: package org.mortbay.jetty.handler;
17:
18: import java.io.File;
19: import java.io.IOException;
20: import java.net.MalformedURLException;
21: import java.util.ArrayList;
22: import java.util.Collections;
23: import java.util.List;
24:
25: import junit.framework.TestCase;
26:
27: import org.mortbay.resource.Resource;
28:
29: /**
30: * @version $Revision$
31: */
32: public class ContextHandlerTest extends TestCase {
33: public void testGetResourcePathsWhenSuppliedPathEndsInSlash()
34: throws Exception {
35: checkResourcePathsForExampleWebApp("/WEB-INF/");
36: }
37:
38: public void testGetResourcePathsWhenSuppliedPathDoesNotEndInSlash()
39: throws Exception {
40: checkResourcePathsForExampleWebApp("/WEB-INF");
41: }
42:
43: private void checkResourcePathsForExampleWebApp(String root)
44: throws IOException, MalformedURLException {
45: File testDirectory = setupTestDirectory();
46:
47: ContextHandler handler = new ContextHandler();
48:
49: assertTrue("Not a directory " + testDirectory, testDirectory
50: .isDirectory());
51: handler.setBaseResource(Resource.newResource(testDirectory
52: .toURL()));
53:
54: List paths = new ArrayList(handler.getResourcePaths(root));
55: assertEquals(2, paths.size());
56:
57: Collections.sort(paths);
58: assertEquals("/WEB-INF/jsp/", paths.get(0));
59: assertEquals("/WEB-INF/web.xml", paths.get(1));
60: }
61:
62: private File setupTestDirectory() throws IOException {
63: File root = new File(System.getProperty("basedir",
64: "modules/jetty"), "target/" + getClass().getName());
65: root.mkdir();
66:
67: File webInf = new File(root, "WEB-INF");
68: webInf.mkdir();
69:
70: new File(webInf, "jsp").mkdir();
71: new File(webInf, "web.xml").createNewFile();
72:
73: return root;
74: }
75: }
|