KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > util > test > TestFileManager


1 /*
2  * (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP
3  * All rights reserved.
4  * [See end of file]
5  */

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 /** com.hp.hpl.jena.brql.util.test.TestFileManager
18  *
19  * @author Andy Seaborne
20  * @version $Id: TestFileManager.java,v 1.4 2005/03/08 13:12:47 andy_seaborne Exp $
21  */

22
23 public class TestFileManager extends TestCase
24 {
25     static Log log = LogFactory.getLog(TestFileManager.class) ;
26     static final String JavaDoc testingDir = "testing/FileManager" ;
27     static final String JavaDoc filename = "fmgr-test-file" ;
28     static final String JavaDoc filenameNonExistent = "fmgr-test-file-1421" ;
29     static final String JavaDoc zipname = testingDir+"/fmgr-test.zip" ;
30     
31     public TestFileManager( String JavaDoc 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 JavaDoc 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 JavaDoc 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 // public void testFileManagerLocatorURL()
142
// {
143
// FileManager fileManager = new FileManager() ;
144
// fileManager.addLocatorURL() ;
145
// InputStream in = fileManager.open("http:///www.bbc.co.uk/") ;
146
// //assertNotNull(in) ;
147
// // Proxies matter.
148
// if ( in == null )
149
// log.warn("Failed to contact http:///www.bbc.co.uk/: maybe due to proxy issues") ;
150
//
151
// try { if ( in != null ) in.close() ; }
152
// catch (Exception ex) {}
153
// }
154

155     
156     // -------- Helpers
157

158     private void closeInputStream(InputStream in)
159     {
160       try {
161           if ( in != null )
162               in.close() ;
163       }
164       catch (Exception JavaDoc ex) {}
165     }
166 }
167
168 /*
169  * (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP
170  * All rights reserved.
171  *
172  * Redistribution and use in source and binary forms, with or without
173  * modification, are permitted provided that the following conditions
174  * are met:
175  * 1. Redistributions of source code must retain the above copyright
176  * notice, this list of conditions and the following disclaimer.
177  * 2. Redistributions in binary form must reproduce the above copyright
178  * notice, this list of conditions and the following disclaimer in the
179  * documentation and/or other materials provided with the distribution.
180  * 3. The name of the author may not be used to endorse or promote products
181  * derived from this software without specific prior written permission.
182  *
183  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
184  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
185  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
186  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
187  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
188  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
189  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
190  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
191  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
192  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
193  */
Popular Tags