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