001: // ========================================================================
002: // Copyright 1997-2005 Mort Bay Consulting Pty. Ltd.
003: // ------------------------------------------------------------------------
004: // Licensed under the Apache License, Version 2.0 (the "License");
005: // you may not use this file except in compliance with the License.
006: // You may obtain a copy of the License at
007: // http://www.apache.org/licenses/LICENSE-2.0
008: // Unless required by applicable law or agreed to in writing, software
009: // distributed under the License is distributed on an "AS IS" BASIS,
010: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011: // See the License for the specific language governing permissions and
012: // limitations under the License.
013: // ========================================================================
014:
015: package org.mortbay.resource;
016:
017: import java.io.File;
018: import java.io.FilePermission;
019: import java.io.InputStream;
020: import java.net.URL;
021: import java.util.jar.JarInputStream;
022:
023: import junit.framework.TestSuite;
024:
025: import org.mortbay.util.IO;
026:
027: public class ResourceTest extends junit.framework.TestCase {
028:
029: public static String __userDir = System
030: .getProperty("user.dir", ".");
031: public static URL __userURL = null;
032: private static String __relDir = "";
033: private static File tmpFile;
034:
035: private static final boolean DIR = true;
036: private static final boolean EXISTS = true;
037:
038: class Data {
039: Resource resource;
040: String test;
041: boolean exists;
042: boolean dir;
043: String content;
044:
045: Data(Data data, String path, boolean exists, boolean dir)
046: throws Exception {
047: this .test = data.resource + "+" + path;
048: resource = data.resource.addPath(path);
049: this .exists = exists;
050: this .dir = dir;
051: }
052:
053: Data(Data data, String path, boolean exists, boolean dir,
054: String content) throws Exception {
055: this .test = data.resource + "+" + path;
056: resource = data.resource.addPath(path);
057: this .exists = exists;
058: this .dir = dir;
059: this .content = content;
060: }
061:
062: Data(URL url, boolean exists, boolean dir) throws Exception {
063: this .test = url.toString();
064: this .exists = exists;
065: this .dir = dir;
066: resource = Resource.newResource(url);
067: }
068:
069: Data(String url, boolean exists, boolean dir) throws Exception {
070: this .test = url;
071: this .exists = exists;
072: this .dir = dir;
073: resource = Resource.newResource(url);
074: }
075:
076: Data(String url, boolean exists, boolean dir, String content)
077: throws Exception {
078: this .test = url;
079: this .exists = exists;
080: this .dir = dir;
081: this .content = content;
082: resource = Resource.newResource(url);
083: }
084: }
085:
086: public static Data[] data;
087:
088: public ResourceTest(String name) {
089: super (name);
090: }
091:
092: /* ------------------------------------------------------------ */
093: public static void main(String[] args) {
094: junit.textui.TestRunner.run(suite());
095: }
096:
097: /* ------------------------------------------------------------ */
098: public static junit.framework.Test suite() {
099: return new TestSuite(ResourceTest.class);
100: }
101:
102: /* ------------------------------------------------------------ */
103: protected void setUp() throws Exception {
104: if (data != null)
105: return;
106:
107: File file = new File(__userDir);
108: file = new File(file.getCanonicalPath());
109: __userURL = file.toURL();
110: if (__userURL.toString().endsWith("/modules/jetty/")
111: || __userURL.toString().endsWith("/modules/jetty")) {
112: __userURL = new URL(__userURL.toString()
113: + "src/test/java/org/mortbay/resource/");
114: FilePermission perm = (FilePermission) __userURL
115: .openConnection().getPermission();
116: __userDir = new File(perm.getName()).getCanonicalPath()
117: + File.separatorChar;
118: __relDir = "src/test/java/org/mortbay/resource/".replace(
119: '/', File.separatorChar);
120: } else {
121: __userURL = new URL(
122: __userURL.toString()
123: + "modules/jetty/src/test/java/org/mortbay/resource/");
124: FilePermission perm = (FilePermission) __userURL
125: .openConnection().getPermission();
126: __userDir = new File(perm.getName()).getCanonicalPath()
127: + File.separatorChar;
128: __relDir = "modules/jetty/src/test/java/org/mortbay/resource/"
129: .replace('/', File.separatorChar);
130: }
131:
132: System.err.println("User Dir=" + __userDir);
133: System.err.println("Rel Dir=" + __relDir);
134: System.err.println("User URL=" + __userURL);
135:
136: tmpFile = File.createTempFile("test", null).getCanonicalFile();
137: tmpFile.deleteOnExit();
138:
139: data = new Data[50];
140: int i = 0;
141:
142: data[i++] = new Data(tmpFile.toString(), EXISTS, !DIR);
143:
144: int rt = i;
145: data[i++] = new Data(__userURL, EXISTS, DIR);
146: data[i++] = new Data(__userDir, EXISTS, DIR);
147: data[i++] = new Data(__relDir, EXISTS, DIR);
148: data[i++] = new Data(__userURL + "ResourceTest.java", EXISTS,
149: !DIR);
150: data[i++] = new Data(__userDir + "ResourceTest.java", EXISTS,
151: !DIR);
152: data[i++] = new Data(__relDir + "ResourceTest.java", EXISTS,
153: !DIR);
154: data[i++] = new Data(__userURL + "NoName.txt", !EXISTS, !DIR);
155: data[i++] = new Data(__userDir + "NoName.txt", !EXISTS, !DIR);
156: data[i++] = new Data(__relDir + "NoName.txt", !EXISTS, !DIR);
157:
158: data[i++] = new Data(data[rt], "ResourceTest.java", EXISTS,
159: !DIR);
160: data[i++] = new Data(data[rt], "/ResourceTest.java", EXISTS,
161: !DIR);
162: data[i++] = new Data(data[rt], "NoName.txt", !EXISTS, !DIR);
163: data[i++] = new Data(data[rt], "/NoName.txt", !EXISTS, !DIR);
164:
165: int td = i;
166: data[i++] = new Data(data[rt], "TestData", EXISTS, DIR);
167: data[i++] = new Data(data[rt], "TestData/", EXISTS, DIR);
168: data[i++] = new Data(data[td], "alphabet.txt", EXISTS, !DIR,
169: "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
170:
171: data[i++] = new Data("jar:file:/somejar.jar!/content/",
172: !EXISTS, DIR);
173: data[i++] = new Data("jar:file:/somejar.jar!/", !EXISTS, DIR);
174:
175: int tj = i;
176: data[i++] = new Data(
177: "jar:" + __userURL + "TestData/test.zip!/", EXISTS, DIR);
178: data[i++] = new Data(data[tj], "Unkown", !EXISTS, !DIR);
179: data[i++] = new Data(data[tj], "/Unkown/", !EXISTS, DIR);
180:
181: data[i++] = new Data(data[tj], "subdir", EXISTS, DIR);
182: data[i++] = new Data(data[tj], "/subdir/", EXISTS, DIR);
183: data[i++] = new Data(data[tj], "alphabet", EXISTS, !DIR,
184: "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
185: data[i++] = new Data(data[tj], "/subdir/alphabet", EXISTS,
186: !DIR, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
187:
188: Resource base = Resource.newResource(__userDir);
189: Resource dir0 = base.addPath("TestData");
190: assertTrue(dir0.isDirectory());
191: assertTrue(dir0.toString().endsWith("/"));
192: assertTrue(dir0.getAlias() == null);
193: Resource dir1 = base.addPath("TestData/");
194: assertTrue(dir1.isDirectory());
195: assertTrue(dir1.toString().endsWith("/"));
196: assertTrue(dir1.getAlias() == null);
197:
198: }
199:
200: /* ------------------------------------------------------------ */
201: protected void tearDown() throws Exception {
202: }
203:
204: /* ------------------------------------------------------------ */
205: public void testResourceExists() {
206: for (int i = 0; i < data.length; i++) {
207: if (data[i] == null)
208: continue;
209:
210: assertEquals("" + i + ":" + data[i].test, data[i].exists,
211: data[i].resource.exists());
212: }
213: }
214:
215: /* ------------------------------------------------------------ */
216: public void testResourceDir() {
217: for (int i = 0; i < data.length; i++) {
218: if (data[i] == null)
219: continue;
220:
221: assertEquals("" + i + ":" + data[i].test, data[i].dir,
222: data[i].resource.isDirectory());
223: }
224: }
225:
226: /* ------------------------------------------------------------ */
227: public void testResourceContent() throws Exception {
228: for (int i = 0; i < data.length; i++) {
229: if (data[i] == null || data[i].content == null)
230: continue;
231:
232: InputStream in = data[i].resource.getInputStream();
233: String c = IO.toString(in);
234: assertTrue("" + i + ":" + data[i].test, c
235: .startsWith(data[i].content));
236: }
237: }
238:
239: public void testJarFile() throws Exception {
240:
241: String s = "jar:" + __userURL + "TestData/test.zip!/subdir/";
242: Resource r = Resource.newResource(s);
243: InputStream is = r.getInputStream();
244: JarInputStream jin = new JarInputStream(is);
245: assertNotNull(is);
246: assertNotNull(jin);
247:
248: }
249:
250: /**
251: * Test a class path resource for existence.
252: */
253: public void testClassPathResourceClassRelative() {
254: final String classPathName = "Resource.class";
255:
256: Resource resource = Resource
257: .newClassPathResource(classPathName);
258:
259: assertTrue(resource != null);
260:
261: // A class path cannot be a directory
262: assertFalse("Class path cannot be a directory.", resource
263: .isDirectory());
264:
265: // A class path must exist
266: assertTrue("Class path resource does not exist.", resource
267: .exists());
268: }
269:
270: /**
271: * Test a class path resource for existence.
272: */
273: public void testClassPathResourceClassAbsolute() {
274: final String classPathName = "/org/mortbay/resource/Resource.class";
275:
276: Resource resource = Resource
277: .newClassPathResource(classPathName);
278:
279: assertTrue(resource != null);
280:
281: // A class path cannot be a directory
282: assertFalse("Class path cannot be a directory.", resource
283: .isDirectory());
284:
285: // A class path must exist
286: assertTrue("Class path resource does not exist.", resource
287: .exists());
288: }
289:
290: /**
291: * Test a class path resource for directories.
292: */
293: public void testClassPathResourceDirectory() throws Exception {
294: final String classPathName = "/";
295:
296: Resource resource = Resource
297: .newClassPathResource(classPathName);
298:
299: assertTrue(resource != null);
300:
301: // A class path must be a directory
302: assertTrue("Class path must be a directory.", resource
303: .isDirectory());
304:
305: assertTrue("Class path returned file must be a directory.",
306: resource.getFile().isDirectory());
307:
308: // A class path must exist
309: assertTrue("Class path resource does not exist.", resource
310: .exists());
311: }
312:
313: /**
314: * Test a class path resource for a file.
315: */
316: public void testClassPathResourceFile() throws Exception {
317: final String fileName = "fakeRequests.txt";
318: final String classPathName = "/" + fileName;
319:
320: // Will locate a resource in the class path
321: Resource resource = Resource
322: .newClassPathResource(classPathName);
323:
324: assertTrue(resource != null);
325:
326: // A class path cannot be a directory
327: assertFalse("Class path must be a directory.", resource
328: .isDirectory());
329:
330: assertTrue(resource != null);
331:
332: File file = resource.getFile();
333:
334: assertTrue("File returned from class path should not be null.",
335: file != null);
336: assertEquals("File name from class path is not equal.",
337: fileName, file.getName());
338: assertTrue("File returned from class path should be a file.",
339: file.isFile());
340:
341: // A class path must exist
342: assertTrue("Class path resource does not exist.", resource
343: .exists());
344: }
345:
346: }
|