KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > util > TestContextResource


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// 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 package org.apache.hivemind.util;
16
17 import java.net.URL JavaDoc;
18 import java.util.Locale JavaDoc;
19
20 import javax.servlet.ServletContext JavaDoc;
21
22 import org.apache.hivemind.Resource;
23 import org.apache.hivemind.test.HiveMindTestCase;
24
25 /**
26  * Tests for {@link org.apache.hivemind.util.ContextResource}.
27  *
28  * @author Howard M. Lewis Ship
29  * @since 1.1
30  */

31 public class TestContextResource extends HiveMindTestCase
32 {
33     private ServletContext JavaDoc newContext()
34     {
35         return (ServletContext JavaDoc) newMock(ServletContext JavaDoc.class);
36     }
37
38     public void testConstructor()
39     {
40         ServletContext JavaDoc context = newContext();
41
42         replayControls();
43
44         ContextResource r = new ContextResource(context, "/foo/bar/baz_en.html", Locale.ENGLISH);
45
46         assertEquals("context:/foo/bar/baz_en.html", r.toString());
47
48         assertEquals("/foo/bar/baz_en.html", r.getPath());
49
50         assertEquals("baz_en.html", r.getName());
51
52         assertEquals(Locale.ENGLISH, r.getLocale());
53
54         verifyControls();
55     }
56
57     public void testLocalizationExists() throws Exception JavaDoc
58     {
59         ServletContext JavaDoc context = newContext();
60
61         context.getResource("/foo/bar/baz_en.html");
62         setReturnValue(context, new URL JavaDoc("http://foo.com"));
63
64         replayControls();
65
66         ContextResource r1 = new ContextResource(context, "/foo/bar/baz.html");
67
68         Resource r2 = r1.getLocalization(Locale.ENGLISH);
69
70         assertEquals("/foo/bar/baz_en.html", r2.getPath());
71         assertEquals(Locale.ENGLISH, r2.getLocale());
72
73         verifyControls();
74     }
75
76     public void testLocalizationSame() throws Exception JavaDoc
77     {
78         ServletContext JavaDoc context = newContext();
79
80         context.getResource("/foo/bar/baz_en.html");
81         setReturnValue(context, null);
82
83         context.getResource("/foo/bar/baz.html");
84         setReturnValue(context, new URL JavaDoc("http://foo.com"));
85
86         replayControls();
87
88         ContextResource r1 = new ContextResource(context, "/foo/bar/baz.html");
89
90         Resource r2 = r1.getLocalization(Locale.ENGLISH);
91
92         assertSame(r2, r1);
93
94         verifyControls();
95     }
96
97     public void testLocalizationMissing() throws Exception JavaDoc
98     {
99         ServletContext JavaDoc context = newContext();
100
101         context.getResource("/foo/bar/baz_en.html");
102         setReturnValue(context, null);
103
104         context.getResource("/foo/bar/baz.html");
105         setReturnValue(context, null);
106
107         replayControls();
108
109         ContextResource r1 = new ContextResource(context, "/foo/bar/baz.html");
110
111         assertNull(r1.getLocalization(Locale.ENGLISH));
112
113         verifyControls();
114     }
115
116     public void testGetRelativeResource()
117     {
118         ServletContext JavaDoc context = newContext();
119
120         replayControls();
121
122         ContextResource r1 = new ContextResource(context, "/foo/bar/baz.html");
123         Resource r2 = r1.getRelativeResource("baz.gif");
124
125         assertEquals("/foo/bar/baz.gif", r2.getPath());
126
127         verifyControls();
128     }
129 }
Popular Tags