001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: TestResourceFinderClasspath.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.resources;
009:
010: import com.uwyn.rife.resources.ResourceFinder;
011: import com.uwyn.rife.resources.ResourceFinderClasspath;
012: import com.uwyn.rife.resources.exceptions.ResourceFinderErrorException;
013: import com.uwyn.rife.tools.ExceptionUtils;
014: import com.uwyn.rife.tools.FileUtils;
015: import com.uwyn.rife.tools.InnerClassException;
016: import com.uwyn.rife.tools.InputStreamUser;
017: import com.uwyn.rife.tools.exceptions.FileUtilsErrorException;
018: import java.io.InputStream;
019: import java.net.MalformedURLException;
020: import java.net.URL;
021: import junit.framework.TestCase;
022:
023: public class TestResourceFinderClasspath extends TestCase {
024: public TestResourceFinderClasspath(String name) {
025: super (name);
026: }
027:
028: public void testInstantiation() {
029: ResourceFinder resource_finder1 = ResourceFinderClasspath
030: .getInstance();
031: ResourceFinder resource_finder2 = ResourceFinderClasspath
032: .getInstance();
033: assertNotNull(resource_finder1);
034: assertNotNull(resource_finder2);
035: assertSame(resource_finder1, resource_finder2);
036: }
037:
038: public void testGetUnknownResource() {
039: ResourceFinder resource_finder = ResourceFinderClasspath
040: .getInstance();
041: assertNull(resource_finder
042: .getResource("this/resource/doesnt/exist.txt"));
043: }
044:
045: public void testGetResourceByName() {
046: ResourceFinder resource_finder = ResourceFinderClasspath
047: .getInstance();
048: assertNotNull(resource_finder.getResource("resources/test.txt"));
049: }
050:
051: public void testGetUnknownStreamByName() {
052: ResourceFinder resource_finder = ResourceFinderClasspath
053: .getInstance();
054:
055: try {
056: resource_finder.useStream("this/resource/doesnt/exist.txt",
057: new InputStreamUser() {
058: public Object useInputStream(InputStream stream)
059: throws InnerClassException {
060: assertNull(stream);
061:
062: return null;
063: }
064: });
065: } catch (ResourceFinderErrorException e) {
066: assertFalse(ExceptionUtils.getExceptionStackTrace(e), false);
067: }
068: }
069:
070: public void testGetUnknownStreamByResource() {
071: ResourceFinder resource_finder = ResourceFinderClasspath
072: .getInstance();
073:
074: try {
075: resource_finder.useStream(new URL(
076: "file://this/resource/doesnt/exist.txt"),
077: new InputStreamUser() {
078: public Object useInputStream(InputStream stream)
079: throws InnerClassException {
080: assertNull(stream);
081:
082: return null;
083: }
084: });
085: } catch (ResourceFinderErrorException e) {
086: assertFalse(ExceptionUtils.getExceptionStackTrace(e), false);
087: } catch (MalformedURLException e) {
088: assertFalse(ExceptionUtils.getExceptionStackTrace(e), false);
089: }
090: }
091:
092: public void testGetStreamByName() {
093: ResourceFinder resource_finder = ResourceFinderClasspath
094: .getInstance();
095:
096: try {
097: resource_finder.useStream("resources/test.txt",
098: new InputStreamUser() {
099: public Object useInputStream(InputStream stream)
100: throws InnerClassException {
101: assertNotNull(stream);
102: try {
103: assertEquals(
104: "This just contains some text to\n"
105: + "verify if\n"
106: + "\n"
107: + "resources can be found\n"
108: + "\n" + "and\n"
109: + "read\n" + "\n"
110: + "correctly.\n",
111: FileUtils.readString(stream));
112: } catch (FileUtilsErrorException e) {
113: assertFalse(ExceptionUtils
114: .getExceptionStackTrace(e),
115: false);
116: }
117: return null;
118: }
119: });
120: } catch (ResourceFinderErrorException e) {
121: assertFalse(ExceptionUtils.getExceptionStackTrace(e), false);
122: }
123: }
124:
125: public void testGetStreamByResource() {
126: ResourceFinder resource_finder = ResourceFinderClasspath
127: .getInstance();
128:
129: URL resource = resource_finder
130: .getResource("resources/test.txt");
131: try {
132: resource_finder.useStream(resource, new InputStreamUser() {
133: public Object useInputStream(InputStream stream)
134: throws InnerClassException {
135: assertNotNull(stream);
136: try {
137: assertEquals(
138: "This just contains some text to\n"
139: + "verify if\n" + "\n"
140: + "resources can be found\n"
141: + "\n" + "and\n" + "read\n"
142: + "\n" + "correctly.\n",
143: FileUtils.readString(stream));
144: } catch (FileUtilsErrorException e) {
145: assertFalse(ExceptionUtils
146: .getExceptionStackTrace(e), false);
147: }
148: return null;
149: }
150: });
151: } catch (ResourceFinderErrorException e) {
152: assertFalse(ExceptionUtils.getExceptionStackTrace(e), false);
153: }
154: }
155:
156: public void testGetUnknownContentByName() {
157: ResourceFinder resource_finder = ResourceFinderClasspath
158: .getInstance();
159:
160: try {
161: String content = resource_finder
162: .getContent("this/resource/doesnt/exist.txt");
163: assertNull(content);
164: } catch (ResourceFinderErrorException e) {
165: assertFalse(ExceptionUtils.getExceptionStackTrace(e), false);
166: }
167: }
168:
169: public void testGetUnknownContentByResource() {
170: ResourceFinder resource_finder = ResourceFinderClasspath
171: .getInstance();
172:
173: try {
174: String content = resource_finder.getContent(new URL(
175: "file://this/resource/doesnt/exist.txt"));
176: assertNull(content);
177: } catch (ResourceFinderErrorException e) {
178: assertFalse(ExceptionUtils.getExceptionStackTrace(e), false);
179: } catch (MalformedURLException e) {
180: assertFalse(ExceptionUtils.getExceptionStackTrace(e), false);
181: }
182: }
183:
184: public void testGetContentByName() {
185: ResourceFinder resource_finder = ResourceFinderClasspath
186: .getInstance();
187:
188: try {
189: String content = resource_finder
190: .getContent("resources/test.txt");
191: assertNotNull(content);
192: assertEquals(content, "This just contains some text to\n"
193: + "verify if\n" + "\n" + "resources can be found\n"
194: + "\n" + "and\n" + "read\n" + "\n" + "correctly.\n");
195: } catch (ResourceFinderErrorException e) {
196: assertFalse(ExceptionUtils.getExceptionStackTrace(e), false);
197: }
198: }
199:
200: public void testGetContentByResource() {
201: ResourceFinder resource_finder = ResourceFinderClasspath
202: .getInstance();
203:
204: URL resource = resource_finder
205: .getResource("resources/test.txt");
206: try {
207: String content = resource_finder.getContent(resource);
208: assertNotNull(content);
209: assertEquals(content, "This just contains some text to\n"
210: + "verify if\n" + "\n" + "resources can be found\n"
211: + "\n" + "and\n" + "read\n" + "\n" + "correctly.\n");
212: } catch (ResourceFinderErrorException e) {
213: assertFalse(ExceptionUtils.getExceptionStackTrace(e), false);
214: }
215: }
216:
217: public void testGetContentByNameAndEncoding() {
218: ResourceFinder resource_finder = ResourceFinderClasspath
219: .getInstance();
220:
221: try {
222: String content = resource_finder.getContent(
223: "resources/test-utf8.txt", "UTF-8");
224: assertNotNull(content);
225: assertEquals(
226: content,
227: "This just contains some text to\n"
228: + "verify if\n"
229: + "\n"
230: + "resources can be found\n"
231: + "\n"
232: + "and\n"
233: + "read\n"
234: + "\n"
235: + "correctly.\n"
236: + "Here are some encoding-specific chars : ¡¢£¤¥¦§¨©ª«¬®.\n");
237: } catch (ResourceFinderErrorException e) {
238: assertFalse(ExceptionUtils.getExceptionStackTrace(e), false);
239: }
240: }
241:
242: public void testGetContentByResourceAndEncoding() {
243: ResourceFinder resource_finder = ResourceFinderClasspath
244: .getInstance();
245:
246: URL resource = resource_finder
247: .getResource("resources/test-utf8.txt");
248: try {
249: String content = resource_finder.getContent(resource,
250: "UTF-8");
251: assertNotNull(content);
252: assertEquals(
253: content,
254: "This just contains some text to\n"
255: + "verify if\n"
256: + "\n"
257: + "resources can be found\n"
258: + "\n"
259: + "and\n"
260: + "read\n"
261: + "\n"
262: + "correctly.\n"
263: + "Here are some encoding-specific chars : ¡¢£¤¥¦§¨©ª«¬®.\n");
264: } catch (ResourceFinderErrorException e) {
265: assertFalse(ExceptionUtils.getExceptionStackTrace(e), false);
266: }
267: }
268:
269: public void testGetUnknownModificationTimeByName() {
270: ResourceFinder resource_finder = ResourceFinderClasspath
271: .getInstance();
272:
273: try {
274: long time = resource_finder
275: .getModificationTime("this/resource/doesnt/exist.txt");
276: assertEquals(-1, time);
277: } catch (ResourceFinderErrorException e) {
278: assertFalse(ExceptionUtils.getExceptionStackTrace(e), false);
279: }
280: }
281:
282: public void testGetUnknownModificationTimeByResource() {
283: ResourceFinder resource_finder = ResourceFinderClasspath
284: .getInstance();
285:
286: try {
287: long time = resource_finder.getModificationTime(new URL(
288: "file://this/resource/doesnt/exist.txt"));
289: assertEquals(-1, time);
290: } catch (ResourceFinderErrorException e) {
291: assertFalse(ExceptionUtils.getExceptionStackTrace(e), false);
292: } catch (MalformedURLException e) {
293: assertFalse(ExceptionUtils.getExceptionStackTrace(e), false);
294: }
295: }
296:
297: public void testGetModificationTimeByName() {
298: ResourceFinder resource_finder = ResourceFinderClasspath
299: .getInstance();
300:
301: try {
302: long time = resource_finder
303: .getModificationTime("resources/test.txt");
304: assertTrue(time != -1);
305: } catch (ResourceFinderErrorException e) {
306: assertFalse(ExceptionUtils.getExceptionStackTrace(e), false);
307: }
308: }
309:
310: public void testGetModificationTimeByResource() {
311: ResourceFinder resource_finder = ResourceFinderClasspath
312: .getInstance();
313:
314: URL resource = resource_finder
315: .getResource("resources/test.txt");
316: try {
317: long time = resource_finder.getModificationTime(resource);
318: assertTrue(time != -1);
319: } catch (ResourceFinderErrorException e) {
320: assertFalse(ExceptionUtils.getExceptionStackTrace(e), false);
321: }
322: }
323: }
|