001: /*
002: * (c) Copyright 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: * All rights reserved.
004: * [See end of file]
005: */
006:
007: package com.hp.hpl.jena.util.test;
008:
009: import junit.framework.*;
010: import java.io.*;
011:
012: import com.hp.hpl.jena.rdf.model.Model;
013: import com.hp.hpl.jena.shared.NotFoundException;
014: import com.hp.hpl.jena.util.FileManager;
015: import com.hp.hpl.jena.util.LocationMapper;
016:
017: import org.apache.commons.logging.*;
018:
019: /** com.hp.hpl.jena.brql.util.test.TestFileManager
020: *
021: * @author Andy Seaborne
022: * @version $Id: TestFileManager.java,v 1.10 2008/01/02 12:08:35 andy_seaborne Exp $
023: */
024:
025: public class TestFileManager extends TestCase {
026: static Log log = LogFactory.getLog(TestFileManager.class);
027: static final String testingDir = "testing/FileManager";
028: static final String filename = "fmgr-test-file";
029: static final String filenameNonExistent = "fmgr-test-file-1421";
030: static final String fileModel = "foo.n3";
031: static final String zipname = testingDir + "/fmgr-test.zip";
032:
033: public TestFileManager(String name) {
034: super (name);
035: }
036:
037: public static TestSuite suite() {
038: return new TestSuite(TestFileManager.class);
039: }
040:
041: public void testFileManagerFileLocator() {
042: FileManager fileManager = new FileManager();
043: fileManager.addLocatorFile();
044: InputStream in = fileManager.open(testingDir + "/" + filename);
045: assertNotNull(in);
046: closeInputStream(in);
047: }
048:
049: public void testFileManagerFileLocatorWithDir() {
050: FileManager fileManager = new FileManager();
051: fileManager.addLocatorFile(testingDir);
052: InputStream in = fileManager.open(filename);
053: assertNotNull(in);
054: closeInputStream(in);
055: }
056:
057: public void testFileManagerNoFile() {
058: FileManager fileManager = new FileManager();
059: fileManager.addLocatorFile();
060: try {
061: // Tests either way round - exception or a null return.
062: InputStream in = fileManager.open(filenameNonExistent);
063: closeInputStream(in);
064: assertNull("Found non-existant file: "
065: + filenameNonExistent, in);
066: } catch (NotFoundException ex) {
067: }
068: }
069:
070: public void testFileManagerLocatorClassLoader() {
071: FileManager fileManager = new FileManager();
072: fileManager.addLocatorClassLoader(fileManager.getClass()
073: .getClassLoader());
074: InputStream in = fileManager.open("java/lang/String.class");
075: assertNotNull(in);
076: closeInputStream(in);
077: }
078:
079: public void testFileManagerLocatorClassLoaderNotFound() {
080: FileManager fileManager = new FileManager();
081: fileManager.addLocatorClassLoader(fileManager.getClass()
082: .getClassLoader());
083: try {
084: InputStream in = fileManager
085: .open("not/java/lang/String.class");
086: closeInputStream(in);
087: assertNull("Found non-existant class", in);
088: } catch (NotFoundException ex) {
089: }
090: }
091:
092: public void testFileManagerLocatorZip() {
093: FileManager fileManager = new FileManager();
094: try {
095: fileManager.addLocatorZip(zipname);
096: } catch (Exception ex) {
097: fail("Failed to create a filemanager and add a zip locator");
098: }
099: InputStream in = fileManager.open(filename);
100: assertNotNull(in);
101: closeInputStream(in);
102: }
103:
104: public void testFileManagerLocatorZipNonFound() {
105: FileManager fileManager = new FileManager();
106: try {
107: fileManager.addLocatorZip(zipname);
108: } catch (Exception ex) {
109: fail("Failed to create a filemanager and add a zip locator");
110: }
111: try {
112: InputStream in = fileManager.open(filenameNonExistent);
113: closeInputStream(in);
114: assertNull("Found non-existant zip file member", in);
115: } catch (NotFoundException ex) {
116: }
117: }
118:
119: public void testFileManagerClone() {
120: FileManager fileManager1 = new FileManager();
121: FileManager fileManager2 = new FileManager(fileManager1);
122:
123: // Should not affect fileManager2
124: fileManager1.addLocatorFile();
125: {
126: InputStream in = fileManager1.open(testingDir + "/"
127: + filename);
128: assertNotNull(in);
129: closeInputStream(in);
130: }
131: // Should not work.
132: try {
133: InputStream in = fileManager2.open(testingDir + "/"
134: + filename);
135: closeInputStream(in);
136: assertNull("Found file via wrong FileManager", in);
137: } catch (NotFoundException ex) {
138: }
139: }
140:
141: public void testLocationMappingURLtoFileOpen() {
142: LocationMapper locMap = new LocationMapper(
143: TestLocationMapper.mapping);
144: FileManager fileManager = new FileManager(locMap);
145: fileManager.addLocatorFile();
146: InputStream in = fileManager.open("http://example.org/file");
147: assertNotNull(in);
148: closeInputStream(in);
149: }
150:
151: public void testLocationMappingURLtoFileOpenNotFound() {
152: LocationMapper locMap = new LocationMapper(
153: TestLocationMapper.mapping);
154: FileManager fileManager = new FileManager(locMap);
155: fileManager.addLocatorClassLoader(fileManager.getClass()
156: .getClassLoader());
157: try {
158: InputStream in = fileManager
159: .open("http://example.org/file");
160: closeInputStream(in);
161: assertNull("Found nont-existant URL", null);
162: } catch (NotFoundException ex) {
163: }
164: }
165:
166: public void testCache1() {
167: FileManager fileManager = new FileManager();
168: fileManager.addLocatorFile(testingDir);
169: Model m1 = fileManager.loadModel(fileModel);
170: Model m2 = fileManager.loadModel(fileModel);
171: assertNotSame(m1, m2);
172: }
173:
174: public void testCache2() {
175: FileManager fileManager = FileManager.get();
176: fileManager.addLocatorFile(testingDir);
177: fileManager.setModelCaching(true);
178: Model m1 = fileManager.loadModel(fileModel);
179: Model m2 = fileManager.loadModel(fileModel);
180: assertSame(m1, m2);
181: }
182:
183: public void testCache3() {
184: FileManager fileManager = FileManager.get();
185: fileManager.addLocatorFile(testingDir);
186: fileManager.setModelCaching(true);
187: Model m1 = fileManager.loadModel(fileModel);
188: Model m2 = fileManager.loadModel(fileModel);
189: assertSame(m1, m2);
190:
191: fileManager.removeCacheModel(fileModel);
192: Model m3 = fileManager.loadModel(fileModel);
193: assertNotSame(m1, m3);
194:
195: fileManager.resetCache();
196: Model m4 = fileManager.loadModel(fileModel);
197: Model m5 = fileManager.loadModel(fileModel);
198:
199: assertSame(m4, m5);
200: assertNotSame(m1, m4);
201: assertNotSame(m3, m4);
202: }
203:
204: // public void testFileManagerLocatorURL()
205: // {
206: // FileManager fileManager = new FileManager() ;
207: // fileManager.addLocatorURL() ;
208: // InputStream in = fileManager.open("http:///www.bbc.co.uk/") ;
209: // //assertNotNull(in) ;
210: // // Proxies matter.
211: // if ( in == null )
212: // log.warn("Failed to contact http:///www.bbc.co.uk/: maybe due to proxy issues") ;
213: //
214: // try { if ( in != null ) in.close() ; }
215: // catch (Exception ex) {}
216: // }
217:
218: // -------- Helpers
219:
220: private void closeInputStream(InputStream in) {
221: try {
222: if (in != null)
223: in.close();
224: } catch (Exception ex) {
225: }
226: }
227: }
228:
229: /*
230: * (c) Copyright 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
231: * All rights reserved.
232: *
233: * Redistribution and use in source and binary forms, with or without
234: * modification, are permitted provided that the following conditions
235: * are met:
236: * 1. Redistributions of source code must retain the above copyright
237: * notice, this list of conditions and the following disclaimer.
238: * 2. Redistributions in binary form must reproduce the above copyright
239: * notice, this list of conditions and the following disclaimer in the
240: * documentation and/or other materials provided with the distribution.
241: * 3. The name of the author may not be used to endorse or promote products
242: * derived from this software without specific prior written permission.
243: *
244: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
245: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
246: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
247: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
248: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
249: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
250: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
251: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
253: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
254: */
|