1 | package org.armedbear.lisp; |
---|
2 | |
---|
3 | import java.net.MalformedURLException; |
---|
4 | import org.junit.Test; |
---|
5 | import static org.junit.Assert.*; |
---|
6 | import org.junit.runner.JUnitCore; |
---|
7 | |
---|
8 | |
---|
9 | import java.net.URL; |
---|
10 | import java.io.File; |
---|
11 | import java.io.FileWriter; |
---|
12 | import java.io.InputStream; |
---|
13 | import java.io.InputStreamReader; |
---|
14 | import java.io.IOException; |
---|
15 | |
---|
16 | public class PathnameTest |
---|
17 | { |
---|
18 | public static void main(final String args[]) { |
---|
19 | JUnitCore.main("org.armedbear.lisp.PathnameTest"); |
---|
20 | } |
---|
21 | |
---|
22 | @Test |
---|
23 | public void constructorURL() |
---|
24 | { |
---|
25 | URL url = null; |
---|
26 | try { |
---|
27 | url = new URL("file:///Users/evenson/work/abcl/build/classes/org/armedbear/lisp/boot.lisp"); |
---|
28 | } catch (MalformedURLException e) { |
---|
29 | System.out.println(e.getMessage()); |
---|
30 | } |
---|
31 | Pathname pathname = (Pathname)URLPathname.create(url); |
---|
32 | assertNotNull(pathname); |
---|
33 | assertNotNull(pathname.getNamestring()); |
---|
34 | assertNotNull(pathname.getName()); |
---|
35 | assertNotNull(pathname.getType()); |
---|
36 | assertNotNull(pathname.getDirectory()); |
---|
37 | } |
---|
38 | |
---|
39 | @Test |
---|
40 | public void getInputStream() throws IOException { |
---|
41 | File file = File.createTempFile("foo", ".lisp"); |
---|
42 | FileWriter output = new FileWriter(file); |
---|
43 | String contents = "(defun foo () 42)"; |
---|
44 | output.append(contents); |
---|
45 | output.close(); |
---|
46 | Pathname pathname = Pathname.makePathname(file); |
---|
47 | InputStream input = pathname.getInputStream(); |
---|
48 | InputStreamReader reader = new InputStreamReader(input); |
---|
49 | char[] buffer = new char[1024]; |
---|
50 | StringBuilder result = new StringBuilder(); |
---|
51 | int i; |
---|
52 | while((i = reader.read(buffer, 0, buffer.length)) != -1) { |
---|
53 | result.append(buffer, 0, i); |
---|
54 | } |
---|
55 | assertEquals(contents, result.toString()); |
---|
56 | input.close(); |
---|
57 | file.delete(); |
---|
58 | } |
---|
59 | |
---|
60 | @Test |
---|
61 | public void copyConstructor() { |
---|
62 | Pathname orig = (Pathname)Pathname.create("/a/b/c/d/e/foo.lisp"); |
---|
63 | Pathname copy = (Pathname)Pathname.create(orig.getNamestring()); |
---|
64 | assertTrue(orig.getNamestring().equals(copy.getNamestring())); |
---|
65 | } |
---|
66 | |
---|
67 | @Test |
---|
68 | public void mergePathnames1() { |
---|
69 | Pathname p = (Pathname)Pathname.create("a/b/c/d/foo.lisp"); |
---|
70 | Pathname d = (Pathname)Pathname.create("/foo/bar/there"); |
---|
71 | Pathname r = (Pathname)Pathname.mergePathnames(p, d); |
---|
72 | String s = r.getNamestring(); |
---|
73 | assertTrue(s.equals("/foo/bar/a/b/c/d/foo.lisp")); |
---|
74 | } |
---|
75 | |
---|
76 | @Test |
---|
77 | public void mergePathnames2() { |
---|
78 | Pathname p = (Pathname)Pathname.create("/a/b/c/d/foo.lisp"); |
---|
79 | Pathname d = (Pathname)Pathname.create("/foo/bar/there"); |
---|
80 | Pathname r = (Pathname)Pathname.mergePathnames(p, d); |
---|
81 | assertTrue(r.getNamestring().equals("/a/b/c/d/foo.lisp")); |
---|
82 | } |
---|
83 | |
---|
84 | @Test |
---|
85 | public void mergePathnames3() { |
---|
86 | LispObject args = Lisp.NIL; |
---|
87 | args = args.push(Keyword.TYPE); |
---|
88 | args = args.push(new SimpleString("abcl-tmp")); |
---|
89 | args = args.nreverse(); |
---|
90 | Pathname p = (Pathname)Pathname.makePathname(args); |
---|
91 | Pathname d = (Pathname)Pathname.create("/foo/bar.abcl"); |
---|
92 | Pathname r = (Pathname)Pathname.mergePathnames(p, d); |
---|
93 | assertTrue(r.getNamestring().equals("/foo/bar.abcl-tmp")); |
---|
94 | } |
---|
95 | |
---|
96 | // Currently we disallow construction of relative pathname JARs |
---|
97 | // @Test |
---|
98 | // public void mergePathnames4() { |
---|
99 | // Pathname p = (Pathname)Pathname.create("jar:file:foo.jar!/bar.abcl"); |
---|
100 | // Pathname d = (Pathname)Pathname.create("/a/b/c/"); |
---|
101 | // Pathname r = (Pathname)Pathname.mergePathnames(p, d); |
---|
102 | // String s = r.getNamestring(); |
---|
103 | // assertTrue(s.equals("jar:file:///a/b/c/foo.jar!/bar.abcl")); |
---|
104 | // } |
---|
105 | @Test |
---|
106 | public void constructorFileDirectory() { |
---|
107 | Pathname p = (Pathname)Pathname.create("file:///tmp/"); |
---|
108 | assertTrue(p.getNamestring().endsWith("/")); |
---|
109 | } |
---|
110 | @Test |
---|
111 | public void constructorFileWindowsDevice() { |
---|
112 | Pathname p = (Pathname)Pathname.create("file:c://tmp/"); |
---|
113 | LispObject device = p.getDevice(); |
---|
114 | if (Utilities.isPlatformWindows) { |
---|
115 | assert(device != Lisp.NIL); |
---|
116 | } |
---|
117 | } |
---|
118 | // Necessary for ASDF output translations to work |
---|
119 | @Test |
---|
120 | public void wildInferiorsJars() { |
---|
121 | String namestring = "jar:file:///**/*.jar!/**/*.*"; |
---|
122 | Pathname p = (Pathname)Pathname.create(namestring); |
---|
123 | String parsedNamestring = p.getNamestring(); |
---|
124 | assertTrue(parsedNamestring.equals(namestring)); |
---|
125 | } |
---|
126 | |
---|
127 | @Test |
---|
128 | public void equality() { |
---|
129 | Pathname p1 = (Pathname)Pathname.create("file:///tmp/"); |
---|
130 | Pathname p2 = (Pathname)Pathname.create("file:///tmp/"); |
---|
131 | boolean result = p1.equals(p2); |
---|
132 | assertTrue("Java equals() for Pathname", result); |
---|
133 | |
---|
134 | JarPathname p3 = (JarPathname)Pathname.create("jar:file:///abcl.jar!/tmp/"); |
---|
135 | JarPathname p4 = (JarPathname)Pathname.create("jar:file:///abcl.jar!/tmp/"); |
---|
136 | result = p3.equals(p4); |
---|
137 | assertTrue("Java equals() for PathnameJar", result); |
---|
138 | } |
---|
139 | } |
---|