001: // Copyright 2006 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.ioc.internal.util;
016:
017: import java.io.BufferedInputStream;
018: import java.io.InputStream;
019: import java.io.InputStreamReader;
020: import java.io.Reader;
021: import java.net.URL;
022: import java.util.Locale;
023:
024: import org.apache.tapestry.ioc.Resource;
025: import org.testng.Assert;
026: import org.testng.annotations.Test;
027:
028: public class ClasspathResourceTest extends Assert {
029: private static final String RESOURCE_TXT_CONTENT = "content from resource.txt";
030:
031: private static final String FOLDER = "org/apache/tapestry/ioc/internal/util";
032:
033: private static final String PATH = FOLDER + "/resource.txt";
034:
035: @Test
036: public void get_resource_URL() throws Exception {
037: Resource r = new ClasspathResource(PATH);
038:
039: assertEquals(content(r), RESOURCE_TXT_CONTENT);
040: }
041:
042: @Test
043: public void relative_to_root_resource() throws Exception {
044: Resource r = new ClasspathResource("").forFile(PATH);
045:
046: assertEquals(content(r), RESOURCE_TXT_CONTENT);
047: }
048:
049: @Test
050: public void relative_to_root_resource_using_leading_slash()
051: throws Exception {
052: Resource r = new ClasspathResource("/").forFile(PATH);
053:
054: assertEquals(content(r), RESOURCE_TXT_CONTENT);
055: }
056:
057: @Test
058: public void leading_slash_on_path_relative_to_root_doesnt_matter()
059: throws Exception {
060: Resource r = new ClasspathResource("/").forFile("/" + PATH);
061:
062: assertEquals(content(r), RESOURCE_TXT_CONTENT);
063: }
064:
065: @Test
066: public void path_and_file() {
067: Resource r = new ClasspathResource(PATH);
068:
069: assertEquals(r.getFolder(), FOLDER);
070: assertEquals(r.getFile(), "resource.txt");
071:
072: }
073:
074: @Test
075: public void for_file_in_same_folder() throws Exception {
076: Resource r = new ClasspathResource(PATH);
077:
078: Resource n = r.forFile("same-folder.txt");
079:
080: assertEquals(content(n), "content from same-folder resource");
081: }
082:
083: @Test
084: public void for_file_single_dot() throws Exception {
085: Resource r = new ClasspathResource(PATH);
086:
087: Resource n = r.forFile("./same-folder.txt");
088:
089: assertEquals(content(n), "content from same-folder resource");
090: }
091:
092: @Test
093: public void multiple_slashes_treated_as_single_slash()
094: throws Exception {
095: Resource r = new ClasspathResource(PATH);
096:
097: Resource n = r.forFile("././/.///same-folder.txt");
098:
099: assertEquals(content(n), "content from same-folder resource");
100: }
101:
102: @Test
103: public void for_file_in_subfolder() throws Exception {
104: Resource r = new ClasspathResource(PATH);
105:
106: Resource n = r.forFile("sub/sub-folder.txt");
107:
108: assertEquals(content(n), "content from sub-folder resource");
109: }
110:
111: @Test
112: public void for_file_same_resource() throws Exception {
113: Resource r = new ClasspathResource(PATH);
114:
115: assertSame(r.forFile("../util/resource.txt"), r);
116: }
117:
118: @Test
119: public void for_file_in_parent_folder() throws Exception {
120: Resource r = new ClasspathResource(PATH);
121:
122: Resource n = r.forFile("../parent-folder.txt");
123:
124: assertEquals(content(n), "content from parent-folder resource");
125: }
126:
127: @Test
128: public void to_string() throws Exception {
129: Resource r = new ClasspathResource(PATH);
130:
131: assertEquals(r.toString(), "classpath:" + PATH);
132: }
133:
134: @Test
135: public void get_URL_for_missing_resource() throws Exception {
136: Resource r = new ClasspathResource(FOLDER
137: + "/missing-resource.txt");
138:
139: assertNull(r.toURL());
140: }
141:
142: @Test
143: public void localization_of_resource() throws Exception {
144: Resource r = new ClasspathResource(PATH);
145:
146: Resource l = r.forLocale(Locale.FRENCH);
147:
148: assertEquals(content(l), "french content");
149: }
150:
151: @Test
152: public void localization_to_closest_match() throws Exception {
153: Resource r = new ClasspathResource(PATH);
154:
155: Resource l = r.forLocale(Locale.CANADA_FRENCH);
156:
157: assertEquals(content(l), "french content");
158: }
159:
160: @Test
161: public void localization_to_base_resource() throws Exception {
162: Resource r = new ClasspathResource(PATH);
163:
164: Resource l = r.forLocale(Locale.JAPANESE);
165:
166: assertSame(l, r);
167: }
168:
169: @Test
170: public void with_extension_same_extension() {
171: Resource r = new ClasspathResource(PATH);
172:
173: assertSame(r.withExtension("txt"), r);
174: }
175:
176: @Test
177: public void with_extension() throws Exception {
178: Resource r = new ClasspathResource(PATH);
179: Resource e = r.withExtension("ext");
180:
181: assertEquals(content(e), "ext content");
182: }
183:
184: @Test
185: public void with_extension_adds_extension() throws Exception {
186: Resource r = new ClasspathResource(FOLDER + "/resource");
187: Resource e = r.withExtension("ext");
188:
189: assertEquals(content(e), "ext content");
190: }
191:
192: @Test
193: public void with_extension_missing_resource_is_null() {
194: Resource r = new ClasspathResource(PATH);
195: Resource e = r.withExtension("does-not-exist");
196:
197: assertNull(e.toURL());
198: }
199:
200: @Test
201: public void localization_of_missing_resource() throws Exception {
202: Resource r = new ClasspathResource(FOLDER
203: + "/missing-resource.txt");
204:
205: assertNull(r.forLocale(Locale.FRENCH));
206: }
207:
208: private String content(Resource resource) throws Exception {
209: return content(resource.toURL());
210: }
211:
212: private String content(URL url) throws Exception {
213: InputStream is = new BufferedInputStream(url.openStream());
214: Reader r = new InputStreamReader(is);
215:
216: StringBuilder builder = new StringBuilder();
217: char[] buffer = new char[2000];
218:
219: while (true) {
220: int length = r.read(buffer, 0, buffer.length);
221:
222: if (length < 0)
223: break;
224:
225: builder.append(buffer, 0, length);
226: }
227:
228: r.close();
229:
230: return builder.toString().trim();
231: }
232: }
|