KickJava   Java API By Example, From Geeks To Geeks.

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


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.MalformedURLException JavaDoc;
18 import java.net.URL JavaDoc;
19 import java.util.Locale JavaDoc;
20
21 import javax.servlet.ServletContext JavaDoc;
22
23 import org.apache.hivemind.test.HiveMindTestCase;
24 import org.easymock.MockControl;
25
26 /**
27  * Test for {@link org.apache.hivemind.util.LocalizedResourceFinder}.
28  *
29  * @author Howard M. Lewis Ship
30  * @since 1.1
31  */

32 public class TestLocalizedContextResourceFinder extends HiveMindTestCase
33 {
34     public void testFound() throws Exception JavaDoc
35     {
36         MockControl control = newControl(ServletContext JavaDoc.class);
37         ServletContext JavaDoc sc = (ServletContext JavaDoc) control.getMock();
38
39         sc.getResource("/foo/bar/baz_en_US.html");
40         control.setReturnValue(null);
41
42         sc.getResource("/foo/bar/baz_en.html");
43         control.setReturnValue(new URL JavaDoc("http://foo.com"));
44
45         replayControls();
46
47         LocalizedContextResourceFinder f = new LocalizedContextResourceFinder(sc);
48
49         LocalizedResource lr = f.resolve("/foo/bar/baz.html", Locale.US);
50
51         assertEquals("/foo/bar/baz_en.html", lr.getResourcePath());
52         assertEquals(Locale.ENGLISH, lr.getResourceLocale());
53
54         verifyControls();
55
56     }
57
58     public void testNotFound() throws Exception JavaDoc
59     {
60         MockControl control = newControl(ServletContext JavaDoc.class);
61         ServletContext JavaDoc sc = (ServletContext JavaDoc) control.getMock();
62
63         sc.getResource("/foo/bar/baz_en.html");
64         control.setReturnValue(null);
65
66         sc.getResource("/foo/bar/baz.html");
67         control.setReturnValue(null);
68
69         replayControls();
70
71         LocalizedContextResourceFinder f = new LocalizedContextResourceFinder(sc);
72
73         assertNull(f.resolve("/foo/bar/baz.html", Locale.ENGLISH));
74
75         verifyControls();
76     }
77
78     public void testNotFoundException() throws Exception JavaDoc
79     {
80         MockControl control = newControl(ServletContext JavaDoc.class);
81         ServletContext JavaDoc sc = (ServletContext JavaDoc) control.getMock();
82
83         sc.getResource("/foo/bar/baz_en.html");
84         control.setThrowable(new MalformedURLException JavaDoc());
85
86         sc.getResource("/foo/bar/baz.html");
87         control.setThrowable(new MalformedURLException JavaDoc());
88
89         replayControls();
90
91         LocalizedContextResourceFinder f = new LocalizedContextResourceFinder(sc);
92
93         assertNull(f.resolve("/foo/bar/baz.html", Locale.ENGLISH));
94
95         verifyControls();
96     }
97
98     public void testExtensionlessResource() throws Exception JavaDoc
99     {
100         MockControl control = newControl(ServletContext JavaDoc.class);
101         ServletContext JavaDoc sc = (ServletContext JavaDoc) control.getMock();
102
103         sc.getResource("/foo/bar/baz");
104         control.setReturnValue(new URL JavaDoc("http://foo.com"));
105
106         replayControls();
107
108         LocalizedContextResourceFinder f = new LocalizedContextResourceFinder(sc);
109
110         LocalizedResource lr = f.resolve("/foo/bar/baz", null);
111
112         assertEquals("/foo/bar/baz", lr.getResourcePath());
113         assertNull(lr.getResourceLocale());
114
115         verifyControls();
116     }
117 }
Popular Tags