KickJava   Java API By Example, From Geeks To Geeks.

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


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 hivemind.test.FrameworkTestCase;
18
19 import java.io.File JavaDoc;
20 import java.net.URL JavaDoc;
21 import java.util.Locale JavaDoc;
22
23 import org.apache.hivemind.Resource;
24
25 /**
26  * Tests for {@link org.apache.hivemind.util.FileResource} and
27  * {@link org.apache.hivemind.util.LocalizedFileResourceFinder}.
28  *
29  * @author Howard Lewis Ship
30  */

31 public class TestFileResource extends FrameworkTestCase
32 {
33
34     private static final String JavaDoc DIR = "/test-data/TestFileResource";
35     private static final String JavaDoc BASEFILE = DIR + "/localizable.properties";
36
37     public void testFindLocalization()
38         throws Exception JavaDoc
39     {
40         String JavaDoc path = getFrameworkPath(BASEFILE);
41
42         // validate that the test is running properly
43

44         File JavaDoc file = new File JavaDoc(path);
45
46         if (!file.exists())
47             throw new IllegalArgumentException JavaDoc("Configuration error: base path (" + path + ") evaluates to "
48                     + file.getCanonicalPath()
49                     + ", which does not exist. Set the correct working directory or FRAMEWORK_DIR system property.");
50
51         LocalizedFileResourceFinder f = new LocalizedFileResourceFinder();
52
53         String JavaDoc enPath = getFrameworkPath(DIR + "/localizable_en.properties");
54
55         assertEquals(enPath, f.findLocalizedPath(path, Locale.ENGLISH));
56
57         String JavaDoc frPath = getFrameworkPath(DIR + "/localizable_fr.properties");
58
59         assertEquals(frPath, f.findLocalizedPath(path, Locale.FRANCE));
60     }
61
62     public void testNoLocalization()
63     {
64         String JavaDoc path = getFrameworkPath(BASEFILE);
65         LocalizedFileResourceFinder f = new LocalizedFileResourceFinder();
66
67         assertEquals(path, f.findLocalizedPath(path, Locale.GERMAN));
68     }
69
70     public void testExisting()
71         throws Exception JavaDoc
72     {
73         String JavaDoc path = getFrameworkPath(BASEFILE);
74
75         Resource r = new FileResource(path);
76
77         File JavaDoc file = new File JavaDoc(path);
78         URL JavaDoc expected = file.toURL();
79
80         assertEquals(expected, r.getResourceURL());
81     }
82
83     public void testMissing()
84         throws Exception JavaDoc
85     {
86         String JavaDoc path = "file-does-not-exist.xml";
87
88         Resource r = new FileResource(path);
89
90         assertEquals(null, r.getResourceURL());
91     }
92
93     public void testCreateWithLocale()
94         throws Exception JavaDoc
95     {
96         String JavaDoc path = getFrameworkPath(DIR + "/localizable_fr.properties");
97
98         Resource r = new FileResource(path, Locale.CANADA_FRENCH);
99
100         assertEquals(path, r.getPath());
101         assertEquals(Locale.CANADA_FRENCH, r.getLocale());
102     }
103
104     public void testGetLocalization()
105     {
106         String JavaDoc path = getFrameworkPath(BASEFILE);
107
108         Resource base = new FileResource(path);
109         Resource localized = base.getLocalization(Locale.FRANCE);
110
111         String JavaDoc expected = getFrameworkPath(DIR + "/localizable_fr.properties");
112
113         assertEquals(expected, localized.getPath());
114     }
115
116     public void testLocalizationMissing()
117     {
118         String JavaDoc path = getFrameworkPath(BASEFILE);
119
120         Resource base = new FileResource(path);
121         Resource localized = base.getLocalization(Locale.CHINA);
122
123         assertSame(base, localized);
124     }
125
126     public void testToString()
127     {
128         String JavaDoc path = getFrameworkPath(BASEFILE);
129
130         Resource r = new FileResource(path);
131
132         assertEquals(path, r.toString());
133     }
134
135     public void testExtensionLess()
136     {
137         String JavaDoc path = getFrameworkPath(DIR);
138         // Remove the ./ at the beginning to remove any dot from the path
139
if (path.startsWith("./")) path = path.substring(2);
140
141         Resource r = new FileResource(path);
142         Resource localized = r.getLocalization(Locale.CHINA);
143
144         assertSame(r, localized);
145     }
146 }
147
Popular Tags