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: TestResourceFinderDirectories.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.resources;
009:
010: import com.uwyn.rife.config.Config;
011: import com.uwyn.rife.resources.ResourceFinder;
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.File;
019: import java.io.InputStream;
020: import java.net.MalformedURLException;
021: import java.net.URL;
022: import junit.framework.TestCase;
023:
024: public class TestResourceFinderDirectories extends TestCase {
025: public TestResourceFinderDirectories(String name) {
026: super (name);
027: }
028:
029: public void testInstantiation() {
030: ResourceFinder resource_finder1 = new ResourceFinderDirectories(
031: new File[] { new File(Config.getRepInstance()
032: .getString("RIFE_HOME")
033: + File.separator + "config") });
034: ResourceFinder resource_finder2 = new ResourceFinderDirectories(
035: new File[] { new File(Config.getRepInstance()
036: .getString("RIFE_HOME")
037: + File.separator + "config") });
038: assertNotNull(resource_finder1);
039: assertNotNull(resource_finder2);
040: assertNotSame(resource_finder1, resource_finder2);
041: }
042:
043: public void testGetUnknownResource() {
044: ResourceFinder resource_finder = new ResourceFinderDirectories(
045: new File[] {
046: new File(Config.getRepInstance().getString(
047: "RIFE_HOME")
048: + File.separator + "templates"),
049: new File(Config.getRepInstance().getString(
050: "RIFE_HOME")
051: + File.separator + "config") });
052:
053: assertNull(resource_finder
054: .getResource("this/resource/doesnt/exist.txt"));
055: }
056:
057: public void testGetResourceByName() {
058: ResourceFinder resource_finder = new ResourceFinderDirectories(
059: new File[] {
060: new File(Config.getRepInstance().getString(
061: "RIFE_HOME")
062: + File.separator + "templates"),
063: new File(Config.getRepInstance().getString(
064: "RIFE_HOME")
065: + File.separator + "config") });
066:
067: assertNotNull(resource_finder.getResource("resources/test.txt"));
068: }
069:
070: public void testGetUnknownStreamByName() {
071: ResourceFinder resource_finder = new ResourceFinderDirectories(
072: new File[] {
073: new File(Config.getRepInstance().getString(
074: "RIFE_HOME")
075: + File.separator + "templates"),
076: new File(Config.getRepInstance().getString(
077: "RIFE_HOME")
078: + File.separator + "config") });
079:
080: try {
081: resource_finder.useStream("this/resource/doesnt/exist.txt",
082: new InputStreamUser() {
083: public Object useInputStream(InputStream stream)
084: throws InnerClassException {
085: assertNull(stream);
086:
087: return null;
088: }
089: });
090: } catch (ResourceFinderErrorException e) {
091: assertFalse(ExceptionUtils.getExceptionStackTrace(e), false);
092: }
093: }
094:
095: public void testGetUnknownStreamByResource() {
096: ResourceFinder resource_finder = new ResourceFinderDirectories(
097: new File[] {
098: new File(Config.getRepInstance().getString(
099: "RIFE_HOME")
100: + File.separator + "templates"),
101: new File(Config.getRepInstance().getString(
102: "RIFE_HOME")
103: + File.separator + "config") });
104:
105: try {
106: resource_finder.useStream(new URL(
107: "file://this/resource/doesnt/exist.txt"),
108: new InputStreamUser() {
109: public Object useInputStream(InputStream stream)
110: throws InnerClassException {
111: assertNull(stream);
112:
113: return null;
114: }
115: });
116: } catch (ResourceFinderErrorException e) {
117: assertFalse(ExceptionUtils.getExceptionStackTrace(e), false);
118: } catch (MalformedURLException e) {
119: assertFalse(ExceptionUtils.getExceptionStackTrace(e), false);
120: }
121: }
122:
123: public void testGetStreamByName() {
124: ResourceFinder resource_finder = new ResourceFinderDirectories(
125: new File[] {
126: new File(Config.getRepInstance().getString(
127: "RIFE_HOME")
128: + File.separator + "templates"),
129: new File(Config.getRepInstance().getString(
130: "RIFE_HOME")
131: + File.separator + "config") });
132:
133: try {
134: resource_finder.useStream("resources/test.txt",
135: new InputStreamUser() {
136: public Object useInputStream(InputStream stream)
137: throws InnerClassException {
138: assertNotNull(stream);
139: try {
140: assertEquals(
141: "This just contains some text to\n"
142: + "verify if\n"
143: + "\n"
144: + "resources can be found\n"
145: + "\n" + "and\n"
146: + "read\n" + "\n"
147: + "correctly.\n",
148: FileUtils.readString(stream));
149: } catch (FileUtilsErrorException e) {
150: assertFalse(ExceptionUtils
151: .getExceptionStackTrace(e),
152: false);
153: }
154: return null;
155: }
156: });
157: } catch (ResourceFinderErrorException e) {
158: assertFalse(ExceptionUtils.getExceptionStackTrace(e), false);
159: }
160: }
161:
162: public void testGetStreamByRkesource() {
163: ResourceFinder resource_finder = new ResourceFinderDirectories(
164: new File[] {
165: new File(Config.getRepInstance().getString(
166: "RIFE_HOME")
167: + File.separator + "templates"),
168: new File(Config.getRepInstance().getString(
169: "RIFE_HOME")
170: + File.separator + "config") });
171:
172: URL resource = resource_finder
173: .getResource("resources/test.txt");
174: try {
175: resource_finder.useStream(resource, new InputStreamUser() {
176: public Object useInputStream(InputStream stream)
177: throws InnerClassException {
178: assertNotNull(stream);
179: try {
180: assertEquals(
181: "This just contains some text to\n"
182: + "verify if\n" + "\n"
183: + "resources can be found\n"
184: + "\n" + "and\n" + "read\n"
185: + "\n" + "correctly.\n",
186: FileUtils.readString(stream));
187: } catch (FileUtilsErrorException e) {
188: assertFalse(ExceptionUtils
189: .getExceptionStackTrace(e), false);
190: }
191: return null;
192: }
193: });
194: } catch (ResourceFinderErrorException e) {
195: assertFalse(ExceptionUtils.getExceptionStackTrace(e), false);
196: }
197: }
198:
199: public void testGetUnknownContentByName() {
200: ResourceFinder resource_finder = new ResourceFinderDirectories(
201: new File[] {
202: new File(Config.getRepInstance().getString(
203: "RIFE_HOME")
204: + File.separator + "templates"),
205: new File(Config.getRepInstance().getString(
206: "RIFE_HOME")
207: + File.separator + "config") });
208:
209: try {
210: String content = resource_finder
211: .getContent("this/resource/doesnt/exist.txt");
212: assertNull(content);
213: } catch (ResourceFinderErrorException e) {
214: assertFalse(ExceptionUtils.getExceptionStackTrace(e), false);
215: }
216: }
217:
218: public void testGetUnknownContentByResource() {
219: ResourceFinder resource_finder = new ResourceFinderDirectories(
220: new File[] {
221: new File(Config.getRepInstance().getString(
222: "RIFE_HOME")
223: + File.separator + "templates"),
224: new File(Config.getRepInstance().getString(
225: "RIFE_HOME")
226: + File.separator + "config") });
227:
228: try {
229: String content = resource_finder.getContent(new URL(
230: "file://this/resource/doesnt/exist.txt"));
231: assertNull(content);
232: } catch (ResourceFinderErrorException e) {
233: assertFalse(ExceptionUtils.getExceptionStackTrace(e), false);
234: } catch (MalformedURLException e) {
235: assertFalse(ExceptionUtils.getExceptionStackTrace(e), false);
236: }
237: }
238:
239: public void testGetContentByName() {
240: ResourceFinder resource_finder = new ResourceFinderDirectories(
241: new File[] {
242: new File(Config.getRepInstance().getString(
243: "RIFE_HOME")
244: + File.separator + "templates"),
245: new File(Config.getRepInstance().getString(
246: "RIFE_HOME")
247: + File.separator + "config") });
248:
249: try {
250: String content = resource_finder
251: .getContent("resources/test.txt");
252: assertNotNull(content);
253: assertEquals(content, "This just contains some text to\n"
254: + "verify if\n" + "\n" + "resources can be found\n"
255: + "\n" + "and\n" + "read\n" + "\n" + "correctly.\n");
256: } catch (ResourceFinderErrorException e) {
257: assertFalse(ExceptionUtils.getExceptionStackTrace(e), false);
258: }
259: }
260:
261: public void testGetContentByResource() {
262: ResourceFinder resource_finder = new ResourceFinderDirectories(
263: new File[] {
264: new File(Config.getRepInstance().getString(
265: "RIFE_HOME")
266: + File.separator + "templates"),
267: new File(Config.getRepInstance().getString(
268: "RIFE_HOME")
269: + File.separator + "config") });
270:
271: URL resource = resource_finder
272: .getResource("resources/test.txt");
273: try {
274: String content = resource_finder.getContent(resource);
275: assertNotNull(content);
276: assertEquals(content, "This just contains some text to\n"
277: + "verify if\n" + "\n" + "resources can be found\n"
278: + "\n" + "and\n" + "read\n" + "\n" + "correctly.\n");
279: } catch (ResourceFinderErrorException e) {
280: assertFalse(ExceptionUtils.getExceptionStackTrace(e), false);
281: }
282: }
283:
284: public void testGetContentByNameAndEncoding() {
285: ResourceFinder resource_finder = new ResourceFinderDirectories(
286: new File[] {
287: new File(Config.getRepInstance().getString(
288: "RIFE_HOME")
289: + File.separator + "templates"),
290: new File(Config.getRepInstance().getString(
291: "RIFE_HOME")
292: + File.separator + "config") });
293:
294: try {
295: String content = resource_finder.getContent(
296: "resources/test-utf8.txt", "UTF-8");
297: assertNotNull(content);
298: assertEquals(
299: content,
300: "This just contains some text to\n"
301: + "verify if\n"
302: + "\n"
303: + "resources can be found\n"
304: + "\n"
305: + "and\n"
306: + "read\n"
307: + "\n"
308: + "correctly.\n"
309: + "Here are some encoding-specific chars : ¡¢£¤¥¦§¨©ª«¬®.\n");
310: } catch (ResourceFinderErrorException e) {
311: assertFalse(ExceptionUtils.getExceptionStackTrace(e), false);
312: }
313: }
314:
315: public void testGetContentByResourceAndEncoding() {
316: ResourceFinder resource_finder = new ResourceFinderDirectories(
317: new File[] {
318: new File(Config.getRepInstance().getString(
319: "RIFE_HOME")
320: + File.separator + "templates"),
321: new File(Config.getRepInstance().getString(
322: "RIFE_HOME")
323: + File.separator + "config") });
324:
325: URL resource = resource_finder
326: .getResource("resources/test-utf8.txt");
327: try {
328: String content = resource_finder.getContent(resource,
329: "UTF-8");
330: assertNotNull(content);
331: assertEquals(
332: content,
333: "This just contains some text to\n"
334: + "verify if\n"
335: + "\n"
336: + "resources can be found\n"
337: + "\n"
338: + "and\n"
339: + "read\n"
340: + "\n"
341: + "correctly.\n"
342: + "Here are some encoding-specific chars : ¡¢£¤¥¦§¨©ª«¬®.\n");
343: } catch (ResourceFinderErrorException e) {
344: assertFalse(ExceptionUtils.getExceptionStackTrace(e), false);
345: }
346: }
347:
348: public void testGetUnknownModificationTimeByName() {
349: ResourceFinder resource_finder = new ResourceFinderDirectories(
350: new File[] {
351: new File(Config.getRepInstance().getString(
352: "RIFE_HOME")
353: + File.separator + "templates"),
354: new File(Config.getRepInstance().getString(
355: "RIFE_HOME")
356: + File.separator + "config") });
357:
358: try {
359: long time = resource_finder
360: .getModificationTime("this/resource/doesnt/exist.txt");
361: assertEquals(-1, time);
362: } catch (ResourceFinderErrorException e) {
363: assertFalse(ExceptionUtils.getExceptionStackTrace(e), false);
364: }
365: }
366:
367: public void testGetUnknownModificationTimeByResource() {
368: ResourceFinder resource_finder = new ResourceFinderDirectories(
369: new File[] {
370: new File(Config.getRepInstance().getString(
371: "RIFE_HOME")
372: + File.separator + "templates"),
373: new File(Config.getRepInstance().getString(
374: "RIFE_HOME")
375: + File.separator + "config") });
376:
377: try {
378: long time = resource_finder.getModificationTime(new URL(
379: "file://this/resource/doesnt/exist.txt"));
380: assertEquals(-1, time);
381: } catch (ResourceFinderErrorException e) {
382: assertFalse(ExceptionUtils.getExceptionStackTrace(e), false);
383: } catch (MalformedURLException e) {
384: assertFalse(ExceptionUtils.getExceptionStackTrace(e), false);
385: }
386: }
387:
388: public void testGetModificationTimeByName() {
389: ResourceFinder resource_finder = new ResourceFinderDirectories(
390: new File[] {
391: new File(Config.getRepInstance().getString(
392: "RIFE_HOME")
393: + File.separator + "templates"),
394: new File(Config.getRepInstance().getString(
395: "RIFE_HOME")
396: + File.separator + "config") });
397:
398: try {
399: long time = resource_finder
400: .getModificationTime("resources/test.txt");
401: assertTrue(time != -1);
402: } catch (ResourceFinderErrorException e) {
403: assertFalse(ExceptionUtils.getExceptionStackTrace(e), false);
404: }
405: }
406:
407: public void testGetModificationTimeByResource() {
408: ResourceFinder resource_finder = new ResourceFinderDirectories(
409: new File[] {
410: new File(Config.getRepInstance().getString(
411: "RIFE_HOME")
412: + File.separator + "templates"),
413: new File(Config.getRepInstance().getString(
414: "RIFE_HOME")
415: + File.separator + "config") });
416:
417: URL resource = resource_finder
418: .getResource("resources/test.txt");
419: try {
420: long time = resource_finder.getModificationTime(resource);
421: assertTrue(time != -1);
422: } catch (ResourceFinderErrorException e) {
423: assertFalse(ExceptionUtils.getExceptionStackTrace(e), false);
424: }
425: }
426: }
|