470 |
26 Apr 05 |
enell |
1 |
import net.sf.basedb.core.Path; |
470 |
26 Apr 05 |
enell |
2 |
|
470 |
26 Apr 05 |
enell |
3 |
/* |
470 |
26 Apr 05 |
enell |
$Id$ |
470 |
26 Apr 05 |
enell |
5 |
|
3675 |
16 Aug 07 |
jari |
Copyright (C) 2005 Johan Enell, Nicklas Nordborg |
4889 |
06 Apr 09 |
nicklas |
Copyright (C) 2006 Jari Häkkinen, Nicklas Nordborg |
470 |
26 Apr 05 |
enell |
8 |
|
2304 |
22 May 06 |
jari |
This file is part of BASE - BioArray Software Environment. |
2304 |
22 May 06 |
jari |
Available at http://base.thep.lu.se/ |
470 |
26 Apr 05 |
enell |
11 |
|
470 |
26 Apr 05 |
enell |
BASE is free software; you can redistribute it and/or |
470 |
26 Apr 05 |
enell |
modify it under the terms of the GNU General Public License |
4480 |
05 Sep 08 |
jari |
as published by the Free Software Foundation; either version 3 |
470 |
26 Apr 05 |
enell |
of the License, or (at your option) any later version. |
470 |
26 Apr 05 |
enell |
16 |
|
470 |
26 Apr 05 |
enell |
BASE is distributed in the hope that it will be useful, |
470 |
26 Apr 05 |
enell |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
470 |
26 Apr 05 |
enell |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
470 |
26 Apr 05 |
enell |
GNU General Public License for more details. |
470 |
26 Apr 05 |
enell |
21 |
|
470 |
26 Apr 05 |
enell |
You should have received a copy of the GNU General Public License |
4514 |
11 Sep 08 |
jari |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
470 |
26 Apr 05 |
enell |
24 |
*/ |
470 |
26 Apr 05 |
enell |
25 |
|
470 |
26 Apr 05 |
enell |
26 |
public class TestPath |
470 |
26 Apr 05 |
enell |
27 |
{ |
470 |
26 Apr 05 |
enell |
28 |
|
470 |
26 Apr 05 |
enell |
29 |
public static void main(String[] args) |
470 |
26 Apr 05 |
enell |
30 |
{ |
470 |
26 Apr 05 |
enell |
31 |
TestUtil.checkArgs(args); |
470 |
26 Apr 05 |
enell |
32 |
TestUtil.begin(); |
470 |
26 Apr 05 |
enell |
33 |
ok = test_all(); |
470 |
26 Apr 05 |
enell |
34 |
TestUtil.stop(); |
470 |
26 Apr 05 |
enell |
35 |
} |
470 |
26 Apr 05 |
enell |
36 |
|
470 |
26 Apr 05 |
enell |
37 |
private static boolean ok = true; |
470 |
26 Apr 05 |
enell |
38 |
|
470 |
26 Apr 05 |
enell |
39 |
public static Boolean test_all() |
470 |
26 Apr 05 |
enell |
40 |
{ |
470 |
26 Apr 05 |
enell |
41 |
write("++Testing path"); |
3789 |
26 Sep 07 |
enell |
42 |
test_create("/filename", Path.Type.FILE, false); |
3789 |
26 Sep 07 |
enell |
43 |
test_create("/directory/filename", Path.Type.FILE, false); |
3789 |
26 Sep 07 |
enell |
44 |
test_create("/directory/subdirectory/filename", Path.Type.FILE, false); |
3789 |
26 Sep 07 |
enell |
45 |
test_create("~userlogin/filename", Path.Type.FILE, false); |
3789 |
26 Sep 07 |
enell |
46 |
test_create("~userlogin/directory/filename", Path.Type.FILE, false); |
3789 |
26 Sep 07 |
enell |
47 |
test_create("~userlogin/directory/subdirectory/filename", Path.Type.FILE, false); |
3789 |
26 Sep 07 |
enell |
48 |
test_create("~/filename", Path.Type.FILE, false); |
3789 |
26 Sep 07 |
enell |
49 |
test_create("~/directory/filename", Path.Type.FILE, false); |
3789 |
26 Sep 07 |
enell |
50 |
test_create("~/directory/subdirectory/filename", Path.Type.FILE, false); |
470 |
26 Apr 05 |
enell |
51 |
|
3789 |
26 Sep 07 |
enell |
52 |
test_create("/", Path.Type.DIRECTORY, false); |
3789 |
26 Sep 07 |
enell |
53 |
test_create("/directory", Path.Type.DIRECTORY, false); |
3789 |
26 Sep 07 |
enell |
54 |
test_create("/directory/subdirectory", Path.Type.DIRECTORY, false); |
3789 |
26 Sep 07 |
enell |
55 |
test_create("/directory/subdirectory/subsub", Path.Type.DIRECTORY, false); |
3789 |
26 Sep 07 |
enell |
56 |
test_create("~userlogin", Path.Type.DIRECTORY, false); |
3789 |
26 Sep 07 |
enell |
57 |
test_create("~userlogin/directory", Path.Type.DIRECTORY, false); |
3789 |
26 Sep 07 |
enell |
58 |
test_create("~userlogin/directory/subdirectory", Path.Type.DIRECTORY, false); |
3789 |
26 Sep 07 |
enell |
59 |
test_create("~userlogin/directory/subdirectory/subsub", Path.Type.DIRECTORY, false); |
3789 |
26 Sep 07 |
enell |
60 |
test_create("~", Path.Type.DIRECTORY, false); |
3789 |
26 Sep 07 |
enell |
61 |
test_create("~/directory", Path.Type.DIRECTORY, false); |
3789 |
26 Sep 07 |
enell |
62 |
test_create("~/directory/subdirectory", Path.Type.DIRECTORY, false); |
3789 |
26 Sep 07 |
enell |
63 |
test_create("~/directory/subdirectory/subsub", Path.Type.DIRECTORY, false); |
470 |
26 Apr 05 |
enell |
64 |
|
470 |
26 Apr 05 |
enell |
65 |
test_valid_name("abc", true); |
470 |
26 Apr 05 |
enell |
66 |
test_valid_name("~abc", false); |
470 |
26 Apr 05 |
enell |
67 |
test_valid_name("\\abc", false); |
470 |
26 Apr 05 |
enell |
68 |
test_valid_name("/abc", false); |
470 |
26 Apr 05 |
enell |
69 |
test_valid_name(":abc", false); |
470 |
26 Apr 05 |
enell |
70 |
test_valid_name(";abc", false); |
470 |
26 Apr 05 |
enell |
71 |
test_valid_name("*abc", false); |
470 |
26 Apr 05 |
enell |
72 |
test_valid_name("?abc", false); |
470 |
26 Apr 05 |
enell |
73 |
test_valid_name("<abc", false); |
470 |
26 Apr 05 |
enell |
74 |
test_valid_name(">abc", false); |
470 |
26 Apr 05 |
enell |
75 |
test_valid_name("|abc", false); |
2854 |
02 Nov 06 |
nicklas |
76 |
|
3789 |
26 Sep 07 |
enell |
77 |
test_create("/file<name>", Path.Type.FILE, true); |
3789 |
26 Sep 07 |
enell |
78 |
test_create("/dire<ctory>/filename", Path.Type.FILE, true); |
3789 |
26 Sep 07 |
enell |
79 |
test_create("/directory/file<name>", Path.Type.FILE, true); |
3789 |
26 Sep 07 |
enell |
80 |
test_create("/sub<dir>", Path.Type.DIRECTORY, true); |
3789 |
26 Sep 07 |
enell |
81 |
test_create("/directory/sub<dir>", Path.Type.DIRECTORY, true); |
3789 |
26 Sep 07 |
enell |
82 |
|
2854 |
02 Nov 06 |
nicklas |
83 |
test_replace_invalid("abc", ".", "abc"); |
2854 |
02 Nov 06 |
nicklas |
84 |
test_replace_invalid("~abc", ".", ".abc"); |
2854 |
02 Nov 06 |
nicklas |
85 |
test_replace_invalid("~:a*?bc<>", ".", ".a.bc."); |
470 |
26 Apr 05 |
enell |
86 |
|
470 |
26 Apr 05 |
enell |
87 |
write("++Testing path "+(ok ? "OK" : "Failed")+"\n"); |
470 |
26 Apr 05 |
enell |
88 |
return ok; |
470 |
26 Apr 05 |
enell |
89 |
} |
470 |
26 Apr 05 |
enell |
90 |
|
3789 |
26 Sep 07 |
enell |
91 |
static void test_create(String path, Path.Type type, boolean shouldFail) |
470 |
26 Apr 05 |
enell |
92 |
{ |
470 |
26 Apr 05 |
enell |
93 |
try |
470 |
26 Apr 05 |
enell |
94 |
{ |
470 |
26 Apr 05 |
enell |
95 |
Path p = new Path(path, type); |
681 |
31 May 05 |
nicklas |
96 |
String pp = p.toString(); |
681 |
31 May 05 |
nicklas |
97 |
if (!path.equals(pp)) |
681 |
31 May 05 |
nicklas |
98 |
{ |
681 |
31 May 05 |
nicklas |
99 |
throw new Exception("Original path ("+path+") is different from path: "+pp); |
681 |
31 May 05 |
nicklas |
100 |
} |
3789 |
26 Sep 07 |
enell |
101 |
if (shouldFail) |
3789 |
26 Sep 07 |
enell |
102 |
{ |
3789 |
26 Sep 07 |
enell |
103 |
write("--Path '"+path+"' FAILED"); |
3789 |
26 Sep 07 |
enell |
104 |
new Throwable("Expected test to fail but it didn't.").printStackTrace(); |
3789 |
26 Sep 07 |
enell |
105 |
ok = false; |
3789 |
26 Sep 07 |
enell |
106 |
} |
3789 |
26 Sep 07 |
enell |
107 |
else |
3789 |
26 Sep 07 |
enell |
108 |
{ |
3789 |
26 Sep 07 |
enell |
109 |
write("--Path " + p.getType() + ": '"+p+"' OK"); |
3789 |
26 Sep 07 |
enell |
110 |
} |
470 |
26 Apr 05 |
enell |
111 |
} |
816 |
23 Jun 05 |
nicklas |
112 |
catch (Throwable ex) |
470 |
26 Apr 05 |
enell |
113 |
{ |
3789 |
26 Sep 07 |
enell |
114 |
if (!shouldFail) |
3789 |
26 Sep 07 |
enell |
115 |
{ |
3789 |
26 Sep 07 |
enell |
116 |
write("--Path '"+path+"' FAILED"); |
3789 |
26 Sep 07 |
enell |
117 |
ex.printStackTrace(); |
3789 |
26 Sep 07 |
enell |
118 |
ok = false; |
3789 |
26 Sep 07 |
enell |
119 |
} |
3789 |
26 Sep 07 |
enell |
120 |
else |
3789 |
26 Sep 07 |
enell |
121 |
{ |
3789 |
26 Sep 07 |
enell |
122 |
write("--Path " + type + ": '"+path+"' OK"); |
3789 |
26 Sep 07 |
enell |
123 |
} |
470 |
26 Apr 05 |
enell |
124 |
} |
470 |
26 Apr 05 |
enell |
125 |
} |
470 |
26 Apr 05 |
enell |
126 |
|
470 |
26 Apr 05 |
enell |
127 |
static void test_valid_name(String name, boolean is_valid) |
470 |
26 Apr 05 |
enell |
128 |
{ |
470 |
26 Apr 05 |
enell |
129 |
try |
470 |
26 Apr 05 |
enell |
130 |
{ |
470 |
26 Apr 05 |
enell |
131 |
boolean name_is_valid = Path.isValidName(name); |
470 |
26 Apr 05 |
enell |
132 |
if (name_is_valid == is_valid) |
470 |
26 Apr 05 |
enell |
133 |
{ |
470 |
26 Apr 05 |
enell |
134 |
write("--Name '"+name+"' is "+(name_is_valid ? "valid" : "invalid")+" OK"); |
470 |
26 Apr 05 |
enell |
135 |
} |
470 |
26 Apr 05 |
enell |
136 |
else |
470 |
26 Apr 05 |
enell |
137 |
{ |
470 |
26 Apr 05 |
enell |
138 |
write("--Name '"+name+"' is "+(name_is_valid ? "valid" : "invalid")+" FAILED"); |
2854 |
02 Nov 06 |
nicklas |
139 |
ok = false; |
470 |
26 Apr 05 |
enell |
140 |
} |
470 |
26 Apr 05 |
enell |
141 |
} |
816 |
23 Jun 05 |
nicklas |
142 |
catch (Throwable ex) |
470 |
26 Apr 05 |
enell |
143 |
{ |
470 |
26 Apr 05 |
enell |
144 |
write("--Name '"+name+"' FAILED"); |
470 |
26 Apr 05 |
enell |
145 |
ex.printStackTrace(); |
470 |
26 Apr 05 |
enell |
146 |
ok = false; |
470 |
26 Apr 05 |
enell |
147 |
} |
470 |
26 Apr 05 |
enell |
148 |
} |
470 |
26 Apr 05 |
enell |
149 |
|
2854 |
02 Nov 06 |
nicklas |
150 |
static void test_replace_invalid(String invalidName, String replacement, String validName) |
2854 |
02 Nov 06 |
nicklas |
151 |
{ |
2854 |
02 Nov 06 |
nicklas |
152 |
try |
2854 |
02 Nov 06 |
nicklas |
153 |
{ |
2854 |
02 Nov 06 |
nicklas |
154 |
String fixedName = Path.makeSafeFilename(invalidName, replacement); |
2854 |
02 Nov 06 |
nicklas |
155 |
if (validName.equals(fixedName)) |
2854 |
02 Nov 06 |
nicklas |
156 |
{ |
2854 |
02 Nov 06 |
nicklas |
157 |
write("--Name '"+invalidName+"' changed to " +fixedName + ": OK"); |
2854 |
02 Nov 06 |
nicklas |
158 |
} |
2854 |
02 Nov 06 |
nicklas |
159 |
else |
2854 |
02 Nov 06 |
nicklas |
160 |
{ |
2854 |
02 Nov 06 |
nicklas |
161 |
write("--Name '"+invalidName+"' changed to " +fixedName + ": FAILED"); |
2854 |
02 Nov 06 |
nicklas |
162 |
ok = false; |
2854 |
02 Nov 06 |
nicklas |
163 |
} |
2854 |
02 Nov 06 |
nicklas |
164 |
} |
2854 |
02 Nov 06 |
nicklas |
165 |
catch (Throwable ex) |
2854 |
02 Nov 06 |
nicklas |
166 |
{ |
2854 |
02 Nov 06 |
nicklas |
167 |
write("--Replace invalid name '"+invalidName+"' FAILED"); |
2854 |
02 Nov 06 |
nicklas |
168 |
ex.printStackTrace(); |
2854 |
02 Nov 06 |
nicklas |
169 |
ok = false; |
2854 |
02 Nov 06 |
nicklas |
170 |
} |
2854 |
02 Nov 06 |
nicklas |
171 |
} |
2854 |
02 Nov 06 |
nicklas |
172 |
|
2854 |
02 Nov 06 |
nicklas |
173 |
|
470 |
26 Apr 05 |
enell |
174 |
static void write(String message) |
470 |
26 Apr 05 |
enell |
175 |
{ |
470 |
26 Apr 05 |
enell |
176 |
System.out.println(message); |
470 |
26 Apr 05 |
enell |
177 |
} |
470 |
26 Apr 05 |
enell |
178 |
} |