001: // Copyright 2007 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.internal.services;
016:
017: import java.net.MalformedURLException;
018: import java.net.URL;
019: import java.util.Arrays;
020: import java.util.List;
021: import java.util.Set;
022:
023: import javax.servlet.ServletContext;
024:
025: import org.apache.tapestry.internal.test.InternalBaseTestCase;
026: import org.apache.tapestry.ioc.internal.util.CollectionFactory;
027: import org.apache.tapestry.services.Context;
028: import org.testng.annotations.Test;
029:
030: public class ContextImplTest extends InternalBaseTestCase {
031: @Test
032: public void get_resource_exists() throws Exception {
033: String path = "/foo";
034: URL url = getClass().getResource("ContextImplTest.class");
035:
036: ServletContext servletContext = newServletContext();
037:
038: expect(servletContext.getResource(path)).andReturn(url);
039:
040: replay();
041:
042: URL result = new ContextImpl(servletContext).getResource(path);
043:
044: assertSame(result, url);
045:
046: verify();
047: }
048:
049: @Test
050: public void get_resource_exception() throws Exception {
051: String path = "/foo";
052: Throwable t = new MalformedURLException("/foo is not a URL.");
053:
054: ServletContext servletContext = newServletContext();
055:
056: expect(servletContext.getResource(path)).andThrow(t);
057:
058: replay();
059:
060: try {
061: new ContextImpl(servletContext).getResource(path);
062: unreachable();
063: } catch (RuntimeException ex) {
064: assertEquals(ex.getMessage(),
065: "java.net.MalformedURLException: /foo is not a URL.");
066: assertSame(ex.getCause(), t);
067: }
068:
069: verify();
070: }
071:
072: @Test
073: public void get_resource_paths() throws Exception {
074: ServletContext servletContext = newServletContext();
075:
076: train_getResourcePaths(servletContext, "/foo",
077: "/foo/alpha.html", "/foo/beta/", "/foo/gamma.html");
078: train_getResourcePaths(servletContext, "/foo/beta/",
079: "/foo/beta/b.html", "/foo/beta/a.html", "/foo/beta/c/");
080: train_getResourcePaths(servletContext, "/foo/beta/c/",
081: "/foo/beta/c/c.html");
082:
083: replay();
084:
085: List<String> actual = new ContextImpl(servletContext)
086: .getResourcePaths("/foo");
087:
088: assertEquals(actual, Arrays.asList("/foo/alpha.html",
089: "/foo/beta/a.html", "/foo/beta/b.html",
090: "/foo/beta/c/c.html", "/foo/gamma.html"));
091:
092: verify();
093: }
094:
095: @Test
096: public void get_attribute() {
097: String name = "foo";
098: Object value = new Object();
099:
100: ServletContext servletContext = newServletContext();
101:
102: expect(servletContext.getAttribute(name)).andReturn(value);
103:
104: replay();
105:
106: Context context = new ContextImpl(servletContext);
107:
108: assertSame(context.getAttribute(name), value);
109:
110: verify();
111: }
112:
113: /** Tomcat 5.5.20 appears to sometimes return null if it can't find a match. */
114: @Test
115: public void ignore_null_from_get_resource_paths() throws Exception {
116: ServletContext servletContext = newServletContext();
117:
118: expect(servletContext.getResourcePaths("/foo")).andReturn(null);
119:
120: replay();
121:
122: List<String> actual = new ContextImpl(servletContext)
123: .getResourcePaths("/foo");
124:
125: assertTrue(actual.isEmpty());
126:
127: verify();
128:
129: }
130:
131: protected final ServletContext newServletContext() {
132: return newMock(ServletContext.class);
133: }
134:
135: protected final void train_getResourcePaths(ServletContext context,
136: String path, String... paths) {
137: Set<String> set = CollectionFactory
138: .newSet(Arrays.asList(paths));
139:
140: expect(context.getResourcePaths(path)).andReturn(set);
141: }
142: }
|