1 6 7 package com.hp.hpl.jena.util.test; 8 9 import junit.framework.*; 10 import java.io.* ; 11 12 import com.hp.hpl.jena.util.FileManager; 13 import com.hp.hpl.jena.util.LocationMapper; 14 15 import org.apache.commons.logging.*; 16 17 22 23 public class TestFileManager extends TestCase 24 { 25 static Log log = LogFactory.getLog(TestFileManager.class) ; 26 static final String testingDir = "testing/FileManager" ; 27 static final String filename = "fmgr-test-file" ; 28 static final String filenameNonExistent = "fmgr-test-file-1421" ; 29 static final String zipname = testingDir+"/fmgr-test.zip" ; 30 31 public TestFileManager( String name ) 32 { 33 super(name); 34 } 35 36 public static TestSuite suite() 37 { 38 return new TestSuite( TestFileManager.class ); 39 } 40 41 public void testFileManagerFileLocator() 42 { 43 FileManager fileManager = new FileManager() ; 44 fileManager.addLocatorFile() ; 45 InputStream in = fileManager.open(testingDir+"/"+filename) ; 46 assertNotNull(in) ; 47 closeInputStream(in) ; 48 } 49 50 public void testFileManagerFileLocatorWithDir() 51 { 52 FileManager fileManager = new FileManager() ; 53 fileManager.addLocatorFile(testingDir) ; 54 InputStream in = fileManager.open(filename) ; 55 assertNotNull(in) ; 56 closeInputStream(in) ; 57 } 58 59 60 public void testFileManagerNoFile() 61 { 62 FileManager fileManager = new FileManager() ; 63 fileManager.addLocatorFile() ; 64 InputStream in = fileManager.open(filenameNonExistent) ; 65 assertNull(in) ; 66 closeInputStream(in) ; 67 } 68 69 public void testFileManagerLocatorClassLoader() 70 { 71 FileManager fileManager = new FileManager() ; 72 fileManager.addLocatorClassLoader(fileManager.getClass().getClassLoader()) ; 73 InputStream in = fileManager.open("java/lang/String.class") ; 74 assertNotNull(in) ; 75 closeInputStream(in) ; 76 } 77 78 public void testFileManagerLocatorClassLoaderNotFound() 79 { 80 FileManager fileManager = new FileManager() ; 81 fileManager.addLocatorClassLoader(fileManager.getClass().getClassLoader()) ; 82 InputStream in = fileManager.open("not/java/lang/String.class") ; 83 assertNull(in) ; 84 closeInputStream(in) ; 85 } 86 87 public void testFileManagerLocatorZip() 88 { 89 FileManager fileManager = new FileManager() ; 90 try { 91 fileManager.addLocatorZip(zipname) ; 92 } catch (Exception ex) 93 { 94 fail("Failed to create a filemanager and add a zip locator") ; 95 } 96 InputStream in = fileManager.open(filename) ; 97 assertNotNull(in) ; 98 closeInputStream(in) ; 99 } 100 101 public void testFileManagerLocatorZipNonFound() 102 { 103 FileManager fileManager = new FileManager() ; 104 try { 105 fileManager.addLocatorZip(zipname) ; 106 } catch (Exception ex) 107 { 108 fail("Failed to create a filemanager and add a zip locator") ; 109 } 110 InputStream in = fileManager.open(filenameNonExistent) ; 111 assertNull(in) ; 112 closeInputStream(in) ; 113 } 114 115 116 public void testLocationMappingURLtoFileOpen() 117 { 118 LocationMapper locMap = new LocationMapper(TestLocationMapper.mapping) ; 119 FileManager fileManager = new FileManager(locMap) ; 120 fileManager.addLocatorFile() ; 121 InputStream in = fileManager.open("http://example.org/file") ; 122 assertNotNull(in) ; 123 closeInputStream(in) ; 124 } 125 126 public void testLocationMappingURLtoFileOpenNotFound() 127 { 128 LocationMapper locMap = new LocationMapper(TestLocationMapper.mapping) ; 129 FileManager fileManager = new FileManager(locMap) ; 130 fileManager.addLocatorClassLoader(fileManager.getClass().getClassLoader()) ; 131 InputStream in = fileManager.open("http://example.org/file") ; 132 assertNull(in) ; 133 closeInputStream(in) ; 134 } 135 136 137 138 139 140 141 155 156 158 private void closeInputStream(InputStream in) 159 { 160 try { 161 if ( in != null ) 162 in.close() ; 163 } 164 catch (Exception ex) {} 165 } 166 } 167 168 | Popular Tags |