1935 |
07 Feb 06 |
nicklas |
1 |
/* |
1935 |
07 Feb 06 |
nicklas |
$Id$ |
1935 |
07 Feb 06 |
nicklas |
3 |
|
3675 |
16 Aug 07 |
jari |
Copyright (C) 2005 Johan Enell, Nicklas Nordborg |
4889 |
06 Apr 09 |
nicklas |
Copyright (C) 2006 Johan Enell, Jari Häkkinen, Nicklas Nordborg |
3675 |
16 Aug 07 |
jari |
Copyright (C) 2007 Nicklas Nordborg |
1935 |
07 Feb 06 |
nicklas |
7 |
|
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/ |
1935 |
07 Feb 06 |
nicklas |
10 |
|
1935 |
07 Feb 06 |
nicklas |
BASE is free software; you can redistribute it and/or |
1935 |
07 Feb 06 |
nicklas |
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 |
1935 |
07 Feb 06 |
nicklas |
of the License, or (at your option) any later version. |
1935 |
07 Feb 06 |
nicklas |
15 |
|
1935 |
07 Feb 06 |
nicklas |
BASE is distributed in the hope that it will be useful, |
1935 |
07 Feb 06 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
1935 |
07 Feb 06 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
1935 |
07 Feb 06 |
nicklas |
GNU General Public License for more details. |
1935 |
07 Feb 06 |
nicklas |
20 |
|
1935 |
07 Feb 06 |
nicklas |
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/>. |
1935 |
07 Feb 06 |
nicklas |
23 |
*/ |
2689 |
02 Oct 06 |
nicklas |
24 |
import net.sf.basedb.core.AbsoluteProgressReporter; |
496 |
29 Apr 05 |
enell |
25 |
import net.sf.basedb.core.BaseException; |
1935 |
07 Feb 06 |
nicklas |
26 |
import net.sf.basedb.core.Application; |
496 |
29 Apr 05 |
enell |
27 |
import net.sf.basedb.core.DbControl; |
496 |
29 Apr 05 |
enell |
28 |
import net.sf.basedb.core.Directory; |
496 |
29 Apr 05 |
enell |
29 |
import net.sf.basedb.core.File; |
5360 |
11 Jun 10 |
nicklas |
30 |
import net.sf.basedb.core.FileServer; |
496 |
29 Apr 05 |
enell |
31 |
import net.sf.basedb.core.Item; |
5340 |
10 May 10 |
nicklas |
32 |
import net.sf.basedb.core.ItemProxy; |
5360 |
11 Jun 10 |
nicklas |
33 |
import net.sf.basedb.core.ItemQuery; |
5325 |
29 Apr 10 |
nicklas |
34 |
import net.sf.basedb.core.Path; |
496 |
29 Apr 05 |
enell |
35 |
import net.sf.basedb.core.Permission; |
1418 |
07 Oct 05 |
nicklas |
36 |
import net.sf.basedb.core.ItemResultList; |
2689 |
02 Oct 06 |
nicklas |
37 |
import net.sf.basedb.core.SimpleAbsoluteProgressReporter; |
496 |
29 Apr 05 |
enell |
38 |
import net.sf.basedb.core.SystemItems; |
5360 |
11 Jun 10 |
nicklas |
39 |
import net.sf.basedb.core.query.Expressions; |
5360 |
11 Jun 10 |
nicklas |
40 |
import net.sf.basedb.core.query.Hql; |
5360 |
11 Jun 10 |
nicklas |
41 |
import net.sf.basedb.core.query.Restrictions; |
2681 |
29 Sep 06 |
nicklas |
42 |
import net.sf.basedb.util.ConsoleProgressReporter; |
5325 |
29 Apr 10 |
nicklas |
43 |
import net.sf.basedb.util.FileUtil; |
3606 |
27 Jul 07 |
nicklas |
44 |
import net.sf.basedb.util.InputStreamSplitter; |
2681 |
29 Sep 06 |
nicklas |
45 |
import net.sf.basedb.util.zip.ZipUnpacker; |
496 |
29 Apr 05 |
enell |
46 |
|
5325 |
29 Apr 10 |
nicklas |
47 |
import java.io.ByteArrayOutputStream; |
1935 |
07 Feb 06 |
nicklas |
48 |
import java.io.FileInputStream; |
1935 |
07 Feb 06 |
nicklas |
49 |
import java.io.IOException; |
1935 |
07 Feb 06 |
nicklas |
50 |
import java.io.OutputStream; |
1935 |
07 Feb 06 |
nicklas |
51 |
import java.io.InputStream; |
5325 |
29 Apr 10 |
nicklas |
52 |
import java.net.URL; |
4520 |
15 Sep 08 |
nicklas |
53 |
import java.nio.charset.Charset; |
5325 |
29 Apr 10 |
nicklas |
54 |
import java.util.Arrays; |
1935 |
07 Feb 06 |
nicklas |
55 |
import java.util.Date; |
5360 |
11 Jun 10 |
nicklas |
56 |
import java.util.List; |
5340 |
10 May 10 |
nicklas |
57 |
import java.util.Set; |
2681 |
29 Sep 06 |
nicklas |
58 |
import java.util.zip.ZipInputStream; |
496 |
29 Apr 05 |
enell |
59 |
|
496 |
29 Apr 05 |
enell |
60 |
public class TestFile |
496 |
29 Apr 05 |
enell |
61 |
{ |
496 |
29 Apr 05 |
enell |
62 |
private static boolean ok = true; |
496 |
29 Apr 05 |
enell |
63 |
|
496 |
29 Apr 05 |
enell |
64 |
public static void main(String[] args) |
496 |
29 Apr 05 |
enell |
65 |
{ |
496 |
29 Apr 05 |
enell |
66 |
TestUtil.checkArgs(args); |
496 |
29 Apr 05 |
enell |
67 |
TestUtil.begin(); |
496 |
29 Apr 05 |
enell |
68 |
ok = test_all(); |
496 |
29 Apr 05 |
enell |
69 |
TestUtil.stop(); |
496 |
29 Apr 05 |
enell |
70 |
} |
496 |
29 Apr 05 |
enell |
71 |
|
496 |
29 Apr 05 |
enell |
72 |
static boolean test_all() |
496 |
29 Apr 05 |
enell |
73 |
{ |
496 |
29 Apr 05 |
enell |
74 |
write("++Testing file"); |
496 |
29 Apr 05 |
enell |
75 |
write_header(); |
496 |
29 Apr 05 |
enell |
// Standard tests: create, load, list |
3719 |
12 Sep 07 |
nicklas |
77 |
int id1 = test_create("data/test.upload.txt", true, true); |
3719 |
12 Sep 07 |
nicklas |
78 |
int id2 = test_create("data/test.upload.txt", false, false); |
5362 |
16 Jun 10 |
nicklas |
79 |
int baseTracServer = TestFileServer.test_create("base.thep.lu.se", "BASE trac site", |
7982 |
14 Jun 21 |
nicklas |
80 |
"base", "base", "data/base.thep.lu.se.crt", null, null); |
7982 |
14 Jun 21 |
nicklas |
81 |
int extId1 = test_create("https://base.thep.lu.se/robots.txt", false, false); |
7982 |
14 Jun 21 |
nicklas |
82 |
int extId3 = test_create("https://base.thep.lu.se/login", false, false); |
657 |
27 May 05 |
enell |
83 |
test_load(id1); |
537 |
10 May 05 |
enell |
84 |
test_list(); |
657 |
27 May 05 |
enell |
85 |
|
657 |
27 May 05 |
enell |
// Extra test: update |
3719 |
12 Sep 07 |
nicklas |
87 |
test_update("data/test.upload.png", id2); |
496 |
29 Apr 05 |
enell |
88 |
|
2681 |
29 Sep 06 |
nicklas |
// Extra test: unpack zip file |
2681 |
29 Sep 06 |
nicklas |
90 |
int dirId = TestDirectory.test_create(true, "unpackzip"); |
2959 |
27 Nov 06 |
nicklas |
91 |
test_unpack_zip("data/test.unpack.zip", dirId); |
2681 |
29 Sep 06 |
nicklas |
92 |
|
5325 |
29 Apr 10 |
nicklas |
93 |
test_download(id1, "data/test.upload.txt"); |
5325 |
29 Apr 10 |
nicklas |
94 |
test_download(extId1, "data/test.download.robots.txt"); |
5360 |
11 Jun 10 |
nicklas |
95 |
test_download(extId3, null); |
5325 |
29 Apr 10 |
nicklas |
96 |
|
1529 |
27 Oct 05 |
nicklas |
97 |
if (TestUtil.waitBeforeDelete()) TestUtil.waitForEnter(); |
2681 |
29 Sep 06 |
nicklas |
98 |
TestDirectory.test_delete(dirId, true); |
2681 |
29 Sep 06 |
nicklas |
99 |
|
496 |
29 Apr 05 |
enell |
// Standard test: Delete |
657 |
27 May 05 |
enell |
101 |
test_delete(id1); |
657 |
27 May 05 |
enell |
102 |
test_delete(id2); |
5325 |
29 Apr 10 |
nicklas |
103 |
test_delete(extId1); |
5360 |
11 Jun 10 |
nicklas |
104 |
test_delete(extId3); |
5360 |
11 Jun 10 |
nicklas |
105 |
TestFileServer.test_delete(baseTracServer); |
5360 |
11 Jun 10 |
nicklas |
106 |
|
496 |
29 Apr 05 |
enell |
107 |
write("++Testing file "+(ok ? "OK" : "Failed")+"\n"); |
496 |
29 Apr 05 |
enell |
108 |
return ok; |
496 |
29 Apr 05 |
enell |
109 |
} |
496 |
29 Apr 05 |
enell |
110 |
|
3719 |
12 Sep 07 |
nicklas |
111 |
static int test_create(String filename, boolean compress, boolean setAll) |
657 |
27 May 05 |
enell |
112 |
{ |
496 |
29 Apr 05 |
enell |
113 |
if (!TestUtil.hasPermission(Permission.CREATE, Item.FILE)) return 0; |
496 |
29 Apr 05 |
enell |
114 |
int id = 0; |
496 |
29 Apr 05 |
enell |
115 |
DbControl dc = null; |
657 |
27 May 05 |
enell |
116 |
InputStream in = null; |
496 |
29 Apr 05 |
enell |
117 |
try |
496 |
29 Apr 05 |
enell |
118 |
{ |
496 |
29 Apr 05 |
enell |
119 |
dc = TestUtil.getDbControl(); |
659 |
27 May 05 |
nicklas |
120 |
File file = File.getNew(dc, Directory.getById(dc, SystemItems.getId(Directory.ROOT))); |
5325 |
29 Apr 10 |
nicklas |
121 |
boolean external = false; |
5325 |
29 Apr 10 |
nicklas |
122 |
if (filename.startsWith("http://") || filename.startsWith("https://")) |
5325 |
29 Apr 10 |
nicklas |
123 |
{ |
5325 |
29 Apr 10 |
nicklas |
124 |
URL url = new URL(filename); |
5325 |
29 Apr 10 |
nicklas |
125 |
file.setName(Path.makeSafeFilename(url.getPath(), "_")+Application.generateRandomId(2)); |
5360 |
11 Jun 10 |
nicklas |
126 |
|
5360 |
11 Jun 10 |
nicklas |
127 |
ItemQuery<FileServer> serverQuery = FileServer.getQuery(); |
5360 |
11 Jun 10 |
nicklas |
128 |
serverQuery.restrict(Restrictions.eq(Hql.property("name"), Expressions.string(url.getHost()))); |
5360 |
11 Jun 10 |
nicklas |
129 |
List<FileServer> fs = serverQuery.list(dc); |
5360 |
11 Jun 10 |
nicklas |
130 |
if (fs.size() > 0) file.setFileServer(fs.get(0)); |
5333 |
30 Apr 10 |
nicklas |
131 |
file.setUrl(filename, true); |
5325 |
29 Apr 10 |
nicklas |
132 |
external = true; |
5325 |
29 Apr 10 |
nicklas |
133 |
} |
5325 |
29 Apr 10 |
nicklas |
134 |
else |
5325 |
29 Apr 10 |
nicklas |
135 |
{ |
5325 |
29 Apr 10 |
nicklas |
136 |
file.setName(Path.makeSafeFilename(filename, "_")+Application.generateRandomId(2)); |
5325 |
29 Apr 10 |
nicklas |
137 |
} |
657 |
27 May 05 |
enell |
138 |
if (setAll) |
657 |
27 May 05 |
enell |
139 |
{ |
657 |
27 May 05 |
enell |
140 |
file.setDescription("Added at "+new Date()); |
2691 |
03 Oct 06 |
nicklas |
141 |
file.setMimeTypeAuto("text/plain", null); |
4520 |
15 Sep 08 |
nicklas |
142 |
file.setCharacterSet(Charset.defaultCharset().name()); |
657 |
27 May 05 |
enell |
143 |
} |
627 |
20 May 05 |
nicklas |
144 |
dc.saveItem(file); |
5325 |
29 Apr 10 |
nicklas |
145 |
if (!external) |
1139 |
26 Aug 05 |
nicklas |
146 |
{ |
5325 |
29 Apr 10 |
nicklas |
147 |
java.io.File fileToUpload = new java.io.File(filename); |
5325 |
29 Apr 10 |
nicklas |
148 |
if (fileToUpload.exists()) |
3606 |
27 Jul 07 |
nicklas |
149 |
{ |
5325 |
29 Apr 10 |
nicklas |
150 |
in = new FileInputStream(fileToUpload); |
5325 |
29 Apr 10 |
nicklas |
151 |
file.upload(in, true, compress); |
5325 |
29 Apr 10 |
nicklas |
152 |
if (file.getSize() != fileToUpload.length()) |
5325 |
29 Apr 10 |
nicklas |
153 |
{ |
5325 |
29 Apr 10 |
nicklas |
154 |
throw new BaseException("Upload incomplete: original size=" + fileToUpload.length()+ |
5325 |
29 Apr 10 |
nicklas |
155 |
"; uploaded="+file.getSize()); |
5325 |
29 Apr 10 |
nicklas |
156 |
} |
3606 |
27 Jul 07 |
nicklas |
157 |
} |
1139 |
26 Aug 05 |
nicklas |
158 |
} |
496 |
29 Apr 05 |
enell |
159 |
dc.commit(); |
496 |
29 Apr 05 |
enell |
160 |
id = file.getId(); |
806 |
22 Jun 05 |
nicklas |
161 |
dc = TestUtil.getDbControl(); |
5060 |
19 Aug 09 |
nicklas |
162 |
dc.reattachItem(file, false); |
496 |
29 Apr 05 |
enell |
163 |
write_item(0, file); |
496 |
29 Apr 05 |
enell |
164 |
write("--Create file OK"); |
496 |
29 Apr 05 |
enell |
165 |
} |
816 |
23 Jun 05 |
nicklas |
166 |
catch (Throwable ex) |
496 |
29 Apr 05 |
enell |
167 |
{ |
496 |
29 Apr 05 |
enell |
168 |
write("--Create file FAILED"); |
496 |
29 Apr 05 |
enell |
169 |
ex.printStackTrace(); |
496 |
29 Apr 05 |
enell |
170 |
ok = false; |
496 |
29 Apr 05 |
enell |
171 |
} |
496 |
29 Apr 05 |
enell |
172 |
finally |
496 |
29 Apr 05 |
enell |
173 |
{ |
496 |
29 Apr 05 |
enell |
174 |
if (dc != null) dc.close(); |
657 |
27 May 05 |
enell |
175 |
if (in != null) |
657 |
27 May 05 |
enell |
176 |
{ |
657 |
27 May 05 |
enell |
177 |
try |
657 |
27 May 05 |
enell |
178 |
{ |
657 |
27 May 05 |
enell |
179 |
in.close(); |
657 |
27 May 05 |
enell |
180 |
} |
657 |
27 May 05 |
enell |
181 |
catch (IOException e) |
657 |
27 May 05 |
enell |
182 |
{ |
657 |
27 May 05 |
enell |
183 |
e.printStackTrace(); |
657 |
27 May 05 |
enell |
184 |
} |
657 |
27 May 05 |
enell |
185 |
} |
496 |
29 Apr 05 |
enell |
186 |
} |
496 |
29 Apr 05 |
enell |
187 |
return id; |
496 |
29 Apr 05 |
enell |
188 |
} |
496 |
29 Apr 05 |
enell |
189 |
|
537 |
10 May 05 |
enell |
190 |
static void test_load(int id) |
537 |
10 May 05 |
enell |
191 |
{ |
537 |
10 May 05 |
enell |
192 |
if (id == 0) return; |
537 |
10 May 05 |
enell |
193 |
DbControl dc = null; |
537 |
10 May 05 |
enell |
194 |
try |
537 |
10 May 05 |
enell |
195 |
{ |
537 |
10 May 05 |
enell |
196 |
dc = TestUtil.getDbControl(); |
537 |
10 May 05 |
enell |
197 |
File fi = File.getById(dc, id); |
537 |
10 May 05 |
enell |
198 |
write_item(0, fi); |
537 |
10 May 05 |
enell |
199 |
write("--Load file OK"); |
537 |
10 May 05 |
enell |
200 |
} |
816 |
23 Jun 05 |
nicklas |
201 |
catch (Throwable ex) |
537 |
10 May 05 |
enell |
202 |
{ |
537 |
10 May 05 |
enell |
203 |
write("--Load file FAILED"); |
537 |
10 May 05 |
enell |
204 |
ex.printStackTrace(); |
537 |
10 May 05 |
enell |
205 |
ok = false; |
537 |
10 May 05 |
enell |
206 |
} |
537 |
10 May 05 |
enell |
207 |
finally |
537 |
10 May 05 |
enell |
208 |
{ |
537 |
10 May 05 |
enell |
209 |
if (dc != null) dc.close(); |
537 |
10 May 05 |
enell |
210 |
} |
537 |
10 May 05 |
enell |
211 |
} |
537 |
10 May 05 |
enell |
212 |
|
537 |
10 May 05 |
enell |
213 |
static void test_list() |
537 |
10 May 05 |
enell |
214 |
{ |
537 |
10 May 05 |
enell |
215 |
DbControl dc = null; |
537 |
10 May 05 |
enell |
216 |
try |
537 |
10 May 05 |
enell |
217 |
{ |
537 |
10 May 05 |
enell |
218 |
dc = TestUtil.getDbControl(); |
1418 |
07 Oct 05 |
nicklas |
219 |
ItemResultList<File> l = File.getQuery().list(dc); |
537 |
10 May 05 |
enell |
220 |
for (int i = 0; i<l.size(); i++) |
537 |
10 May 05 |
enell |
221 |
{ |
537 |
10 May 05 |
enell |
222 |
write_item(i, l.get(i)); |
537 |
10 May 05 |
enell |
223 |
} |
537 |
10 May 05 |
enell |
224 |
write("--List files OK ("+l.size()+")"); |
537 |
10 May 05 |
enell |
225 |
} |
816 |
23 Jun 05 |
nicklas |
226 |
catch (Throwable ex) |
537 |
10 May 05 |
enell |
227 |
{ |
537 |
10 May 05 |
enell |
228 |
write("--List files FAILED"); |
537 |
10 May 05 |
enell |
229 |
ex.printStackTrace(); |
537 |
10 May 05 |
enell |
230 |
ok = false; |
537 |
10 May 05 |
enell |
231 |
} |
537 |
10 May 05 |
enell |
232 |
finally |
537 |
10 May 05 |
enell |
233 |
{ |
537 |
10 May 05 |
enell |
234 |
if (dc != null) dc.close(); |
537 |
10 May 05 |
enell |
235 |
} |
537 |
10 May 05 |
enell |
236 |
} |
537 |
10 May 05 |
enell |
237 |
|
6520 |
18 Aug 14 |
nicklas |
238 |
static boolean test_list(String path, int expectedResults) |
6520 |
18 Aug 14 |
nicklas |
239 |
{ |
6520 |
18 Aug 14 |
nicklas |
240 |
DbControl dc = null; |
6520 |
18 Aug 14 |
nicklas |
241 |
try |
6520 |
18 Aug 14 |
nicklas |
242 |
{ |
6520 |
18 Aug 14 |
nicklas |
243 |
dc = TestUtil.getDbControl(); |
6520 |
18 Aug 14 |
nicklas |
244 |
Path p = new Path(path, Path.Type.DIRECTORY); |
6520 |
18 Aug 14 |
nicklas |
245 |
Directory d = Directory.getByPath(dc, p); |
6520 |
18 Aug 14 |
nicklas |
246 |
ItemResultList<File> files = d.getFiles().list(dc); |
6520 |
18 Aug 14 |
nicklas |
247 |
|
6520 |
18 Aug 14 |
nicklas |
248 |
for (int i = 0; i<files.size(); i++) |
6520 |
18 Aug 14 |
nicklas |
249 |
{ |
6520 |
18 Aug 14 |
nicklas |
250 |
write_item(i, files.get(i)); |
6520 |
18 Aug 14 |
nicklas |
251 |
} |
6520 |
18 Aug 14 |
nicklas |
252 |
if (expectedResults >=0 && expectedResults != files.size()) |
6520 |
18 Aug 14 |
nicklas |
253 |
{ |
6520 |
18 Aug 14 |
nicklas |
254 |
throw new BaseException("Expected " + expectedResults + " results, " + |
6520 |
18 Aug 14 |
nicklas |
255 |
"not " + files.size()); |
6520 |
18 Aug 14 |
nicklas |
256 |
} |
6520 |
18 Aug 14 |
nicklas |
257 |
write("--List files OK ("+files.size()+")"); |
6520 |
18 Aug 14 |
nicklas |
258 |
} |
6520 |
18 Aug 14 |
nicklas |
259 |
catch (Throwable ex) |
6520 |
18 Aug 14 |
nicklas |
260 |
{ |
6520 |
18 Aug 14 |
nicklas |
261 |
write("--List files FAILED"); |
6520 |
18 Aug 14 |
nicklas |
262 |
ex.printStackTrace(); |
6520 |
18 Aug 14 |
nicklas |
263 |
ok = false; |
6520 |
18 Aug 14 |
nicklas |
264 |
return false; |
6520 |
18 Aug 14 |
nicklas |
265 |
} |
6520 |
18 Aug 14 |
nicklas |
266 |
finally |
6520 |
18 Aug 14 |
nicklas |
267 |
{ |
6520 |
18 Aug 14 |
nicklas |
268 |
if (dc != null) dc.close(); |
6520 |
18 Aug 14 |
nicklas |
269 |
} |
6520 |
18 Aug 14 |
nicklas |
270 |
return true; |
6520 |
18 Aug 14 |
nicklas |
271 |
} |
6520 |
18 Aug 14 |
nicklas |
272 |
|
6520 |
18 Aug 14 |
nicklas |
273 |
|
537 |
10 May 05 |
enell |
274 |
static void test_delete(int id) |
537 |
10 May 05 |
enell |
275 |
{ |
537 |
10 May 05 |
enell |
276 |
if (id == 0) return; |
537 |
10 May 05 |
enell |
277 |
DbControl dc = null; |
2205 |
28 Apr 06 |
enell |
278 |
File fi = null; |
537 |
10 May 05 |
enell |
279 |
try |
537 |
10 May 05 |
enell |
280 |
{ |
537 |
10 May 05 |
enell |
281 |
dc = TestUtil.getDbControl(); |
2205 |
28 Apr 06 |
enell |
282 |
fi = File.getById(dc, id); |
537 |
10 May 05 |
enell |
283 |
dc.deleteItem(fi); |
5340 |
10 May 10 |
nicklas |
284 |
Set<ItemProxy> using = fi.getUsingItems(); |
5340 |
10 May 10 |
nicklas |
285 |
if (using.size() > 0) |
5340 |
10 May 10 |
nicklas |
286 |
{ |
5340 |
10 May 10 |
nicklas |
287 |
throw new BaseException(fi + " is used by " + using); |
5340 |
10 May 10 |
nicklas |
288 |
} |
537 |
10 May 05 |
enell |
289 |
dc.commit(); |
2205 |
28 Apr 06 |
enell |
290 |
write("--Delete file "+fi+" OK"); |
537 |
10 May 05 |
enell |
291 |
} |
816 |
23 Jun 05 |
nicklas |
292 |
catch (Throwable ex) |
537 |
10 May 05 |
enell |
293 |
{ |
2205 |
28 Apr 06 |
enell |
294 |
write("--Delete file "+fi+"FAILED"); |
537 |
10 May 05 |
enell |
295 |
ex.printStackTrace(); |
537 |
10 May 05 |
enell |
296 |
ok = false; |
537 |
10 May 05 |
enell |
297 |
} |
537 |
10 May 05 |
enell |
298 |
finally |
537 |
10 May 05 |
enell |
299 |
{ |
537 |
10 May 05 |
enell |
300 |
if (dc != null) dc.close(); |
537 |
10 May 05 |
enell |
301 |
} |
537 |
10 May 05 |
enell |
302 |
|
537 |
10 May 05 |
enell |
303 |
} |
537 |
10 May 05 |
enell |
304 |
|
4033 |
04 Dec 07 |
martin |
305 |
/** |
4033 |
04 Dec 07 |
martin |
@since 2.5 |
4033 |
04 Dec 07 |
martin |
307 |
*/ |
4033 |
04 Dec 07 |
martin |
308 |
static void test_rename(int id, String newName) |
4033 |
04 Dec 07 |
martin |
309 |
{ |
4033 |
04 Dec 07 |
martin |
310 |
if (id == 0) return; |
4033 |
04 Dec 07 |
martin |
311 |
DbControl dc = null; |
4033 |
04 Dec 07 |
martin |
312 |
File file = null; |
4033 |
04 Dec 07 |
martin |
313 |
String oldName = null; |
4033 |
04 Dec 07 |
martin |
314 |
try |
4033 |
04 Dec 07 |
martin |
315 |
{ |
4033 |
04 Dec 07 |
martin |
316 |
dc = TestUtil.getDbControl(); |
4033 |
04 Dec 07 |
martin |
317 |
file = File.getById(dc, id); |
4033 |
04 Dec 07 |
martin |
318 |
oldName = file.getName(); |
4033 |
04 Dec 07 |
martin |
319 |
file.setName(newName); |
4033 |
04 Dec 07 |
martin |
320 |
dc.commit(); |
4033 |
04 Dec 07 |
martin |
321 |
write("--Rename fileId: " + file.getId() + " from " + oldName + " to " + newName + " OK"); |
4033 |
04 Dec 07 |
martin |
322 |
} |
4033 |
04 Dec 07 |
martin |
323 |
catch (Throwable ex) |
4033 |
04 Dec 07 |
martin |
324 |
{ |
4033 |
04 Dec 07 |
martin |
325 |
write("--Rename file: " + file + " FAILED"); |
4033 |
04 Dec 07 |
martin |
326 |
ex.printStackTrace(); |
4033 |
04 Dec 07 |
martin |
327 |
ok = false; |
4033 |
04 Dec 07 |
martin |
328 |
} |
4033 |
04 Dec 07 |
martin |
329 |
finally |
4033 |
04 Dec 07 |
martin |
330 |
{ |
4033 |
04 Dec 07 |
martin |
331 |
if (dc != null) dc.close(); |
4033 |
04 Dec 07 |
martin |
332 |
} |
4033 |
04 Dec 07 |
martin |
333 |
} |
4033 |
04 Dec 07 |
martin |
334 |
|
496 |
29 Apr 05 |
enell |
335 |
static void write_header() |
496 |
29 Apr 05 |
enell |
336 |
{ |
496 |
29 Apr 05 |
enell |
337 |
if (!TestUtil.getSilent()) |
496 |
29 Apr 05 |
enell |
338 |
{ |
7548 |
10 Dec 18 |
nicklas |
339 |
write(" \tID \tName \tDescription\tFiletype\tUsed\tLocation\tSize \tCompressed size\tMime type\tCharacter set\tPath"); |
7548 |
10 Dec 18 |
nicklas |
340 |
write("-- \t-- \t--------- \t-----------\t--------\t----\t--------\t--------\t---------------\t---------\t-------------\t----"); |
496 |
29 Apr 05 |
enell |
341 |
} |
496 |
29 Apr 05 |
enell |
342 |
} |
496 |
29 Apr 05 |
enell |
343 |
static void write_item(int i, File fi) |
496 |
29 Apr 05 |
enell |
344 |
throws BaseException |
496 |
29 Apr 05 |
enell |
345 |
{ |
496 |
29 Apr 05 |
enell |
346 |
if (!TestUtil.getSilent()) System.out.println(i+":\t"+fi.getId()+"\t"+fi.getName()+"\t"+fi.getDescription()+"\t"+ |
7548 |
10 Dec 18 |
nicklas |
347 |
fi.getItemSubtype()+"\t"+fi.isUsed()+"\t"+fi.getLocation()+"\t"+ |
4520 |
15 Sep 08 |
nicklas |
348 |
fi.getSize()+"\t" + fi.getCompressedSize() + "\t"+fi.getMimeType()+"\t"+ |
4520 |
15 Sep 08 |
nicklas |
349 |
fi.getCharacterSet()+"\t"+fi.getPath()); |
496 |
29 Apr 05 |
enell |
350 |
} |
496 |
29 Apr 05 |
enell |
351 |
static void write(String message) |
496 |
29 Apr 05 |
enell |
352 |
{ |
496 |
29 Apr 05 |
enell |
353 |
System.out.println(message); |
496 |
29 Apr 05 |
enell |
354 |
} |
806 |
22 Jun 05 |
nicklas |
355 |
|
806 |
22 Jun 05 |
nicklas |
356 |
static void test_update(String filename, int fileid) |
806 |
22 Jun 05 |
nicklas |
357 |
{ |
977 |
21 Jul 05 |
enell |
358 |
if (fileid == 0) return; |
806 |
22 Jun 05 |
nicklas |
359 |
DbControl dc = null; |
806 |
22 Jun 05 |
nicklas |
360 |
InputStream in = null; |
806 |
22 Jun 05 |
nicklas |
361 |
try |
806 |
22 Jun 05 |
nicklas |
362 |
{ |
806 |
22 Jun 05 |
nicklas |
363 |
dc = TestUtil.getDbControl(); |
806 |
22 Jun 05 |
nicklas |
364 |
File file = File.getById(dc, fileid); |
806 |
22 Jun 05 |
nicklas |
365 |
in = new FileInputStream(filename); |
2959 |
27 Nov 06 |
nicklas |
366 |
file.setName(filename.replace("/", "_")+Application.generateRandomId(2)); |
902 |
08 Jul 05 |
nicklas |
367 |
file.upload(in, false); |
806 |
22 Jun 05 |
nicklas |
368 |
dc.commit(); |
806 |
22 Jun 05 |
nicklas |
369 |
dc = TestUtil.getDbControl(); |
5060 |
19 Aug 09 |
nicklas |
370 |
dc.reattachItem(file, false); |
806 |
22 Jun 05 |
nicklas |
371 |
write_item(0, file); |
806 |
22 Jun 05 |
nicklas |
372 |
write("--Update file OK"); |
806 |
22 Jun 05 |
nicklas |
373 |
} |
816 |
23 Jun 05 |
nicklas |
374 |
catch (Throwable ex) |
806 |
22 Jun 05 |
nicklas |
375 |
{ |
806 |
22 Jun 05 |
nicklas |
376 |
write("--Update file FAILED"); |
806 |
22 Jun 05 |
nicklas |
377 |
ex.printStackTrace(); |
806 |
22 Jun 05 |
nicklas |
378 |
ok = false; |
806 |
22 Jun 05 |
nicklas |
379 |
} |
806 |
22 Jun 05 |
nicklas |
380 |
finally |
806 |
22 Jun 05 |
nicklas |
381 |
{ |
806 |
22 Jun 05 |
nicklas |
382 |
if (dc != null) dc.close(); |
806 |
22 Jun 05 |
nicklas |
383 |
if (in != null) |
806 |
22 Jun 05 |
nicklas |
384 |
{ |
806 |
22 Jun 05 |
nicklas |
385 |
try |
806 |
22 Jun 05 |
nicklas |
386 |
{ |
806 |
22 Jun 05 |
nicklas |
387 |
in.close(); |
806 |
22 Jun 05 |
nicklas |
388 |
} |
806 |
22 Jun 05 |
nicklas |
389 |
catch (IOException e) |
806 |
22 Jun 05 |
nicklas |
390 |
{ |
806 |
22 Jun 05 |
nicklas |
391 |
e.printStackTrace(); |
806 |
22 Jun 05 |
nicklas |
392 |
} |
806 |
22 Jun 05 |
nicklas |
393 |
} |
806 |
22 Jun 05 |
nicklas |
394 |
} |
806 |
22 Jun 05 |
nicklas |
395 |
} |
2681 |
29 Sep 06 |
nicklas |
396 |
|
2681 |
29 Sep 06 |
nicklas |
397 |
static void test_unpack_zip(String filename, int directoryId) |
2681 |
29 Sep 06 |
nicklas |
398 |
{ |
2681 |
29 Sep 06 |
nicklas |
399 |
if (!TestUtil.hasPermission(Permission.CREATE, Item.FILE) || directoryId == 0) return; |
2681 |
29 Sep 06 |
nicklas |
400 |
DbControl dc = null; |
2681 |
29 Sep 06 |
nicklas |
401 |
ZipInputStream zipFile = null; |
2681 |
29 Sep 06 |
nicklas |
402 |
try |
2681 |
29 Sep 06 |
nicklas |
403 |
{ |
2681 |
29 Sep 06 |
nicklas |
404 |
dc = TestUtil.getDbControl(); |
2681 |
29 Sep 06 |
nicklas |
405 |
Directory dir = Directory.getById(dc, directoryId); |
2681 |
29 Sep 06 |
nicklas |
406 |
|
3606 |
27 Jul 07 |
nicklas |
407 |
File keepZip = File.getNew(dc, dir); |
3606 |
27 Jul 07 |
nicklas |
408 |
keepZip.setName(filename.replace("/", "_")+Application.generateRandomId(2)); |
3606 |
27 Jul 07 |
nicklas |
409 |
dc.saveItem(keepZip); |
3606 |
27 Jul 07 |
nicklas |
410 |
OutputStream keepZipStream = keepZip.getUploadStream(false); |
3606 |
27 Jul 07 |
nicklas |
411 |
java.io.File fileToUpload = new java.io.File(filename); |
3606 |
27 Jul 07 |
nicklas |
412 |
InputStream upload = new FileInputStream(fileToUpload); |
3606 |
27 Jul 07 |
nicklas |
413 |
InputStreamSplitter splitter = new InputStreamSplitter(upload, true, true, keepZipStream); |
3606 |
27 Jul 07 |
nicklas |
414 |
|
2681 |
29 Sep 06 |
nicklas |
415 |
ZipUnpacker unpacker = new ZipUnpacker(dc); |
3606 |
27 Jul 07 |
nicklas |
416 |
zipFile = new ZipInputStream(splitter); |
2689 |
02 Oct 06 |
nicklas |
417 |
AbsoluteProgressReporter progress = TestUtil.getSilent() ? |
3606 |
27 Jul 07 |
nicklas |
418 |
null : new SimpleAbsoluteProgressReporter(new ConsoleProgressReporter(true), fileToUpload.length()); |
5758 |
23 Sep 11 |
nicklas |
419 |
unpacker.unpackZipFile(dir, zipFile, keepZip, true, progress); |
2681 |
29 Sep 06 |
nicklas |
420 |
if (progress != null) progress.append("\n"); |
3606 |
27 Jul 07 |
nicklas |
421 |
|
2681 |
29 Sep 06 |
nicklas |
422 |
zipFile.close(); |
3606 |
27 Jul 07 |
nicklas |
423 |
keepZipStream.close(); |
3606 |
27 Jul 07 |
nicklas |
424 |
if (splitter.getNumRead() != fileToUpload.length()) |
3606 |
27 Jul 07 |
nicklas |
425 |
{ |
3606 |
27 Jul 07 |
nicklas |
426 |
throw new BaseException("Incomplete file upload; original size=" + fileToUpload.length()+ |
3606 |
27 Jul 07 |
nicklas |
427 |
"; uploaded size=" + splitter.getNumRead()); |
3606 |
27 Jul 07 |
nicklas |
428 |
} |
3606 |
27 Jul 07 |
nicklas |
429 |
if (keepZip.getSize() != fileToUpload.length()) |
3606 |
27 Jul 07 |
nicklas |
430 |
{ |
3606 |
27 Jul 07 |
nicklas |
431 |
throw new BaseException("Incomplete file upload; original size=" + fileToUpload.length()+ |
3606 |
27 Jul 07 |
nicklas |
432 |
"; uploaded size=" + keepZip.getSize()); |
3606 |
27 Jul 07 |
nicklas |
433 |
} |
2681 |
29 Sep 06 |
nicklas |
434 |
dc.commit(); |
2681 |
29 Sep 06 |
nicklas |
435 |
write("--Unpack zip file OK"); |
2681 |
29 Sep 06 |
nicklas |
436 |
} |
2681 |
29 Sep 06 |
nicklas |
437 |
catch (Throwable ex) |
2681 |
29 Sep 06 |
nicklas |
438 |
{ |
2681 |
29 Sep 06 |
nicklas |
439 |
write("--Unpack zip file FAILED"); |
2681 |
29 Sep 06 |
nicklas |
440 |
ex.printStackTrace(); |
2681 |
29 Sep 06 |
nicklas |
441 |
ok = false; |
2681 |
29 Sep 06 |
nicklas |
442 |
} |
2681 |
29 Sep 06 |
nicklas |
443 |
finally |
2681 |
29 Sep 06 |
nicklas |
444 |
{ |
2681 |
29 Sep 06 |
nicklas |
445 |
if (dc != null) dc.close(); |
2681 |
29 Sep 06 |
nicklas |
446 |
if (zipFile != null) |
2681 |
29 Sep 06 |
nicklas |
447 |
{ |
2681 |
29 Sep 06 |
nicklas |
448 |
try |
2681 |
29 Sep 06 |
nicklas |
449 |
{ |
2681 |
29 Sep 06 |
nicklas |
450 |
zipFile.close(); |
2681 |
29 Sep 06 |
nicklas |
451 |
} |
2681 |
29 Sep 06 |
nicklas |
452 |
catch (IOException e) |
2681 |
29 Sep 06 |
nicklas |
453 |
{ |
2681 |
29 Sep 06 |
nicklas |
454 |
e.printStackTrace(); |
2681 |
29 Sep 06 |
nicklas |
455 |
} |
2681 |
29 Sep 06 |
nicklas |
456 |
} |
2681 |
29 Sep 06 |
nicklas |
457 |
} |
2681 |
29 Sep 06 |
nicklas |
458 |
} |
2681 |
29 Sep 06 |
nicklas |
459 |
|
5325 |
29 Apr 10 |
nicklas |
460 |
static void test_download(int fileId, String referenceFile) |
5325 |
29 Apr 10 |
nicklas |
461 |
{ |
5325 |
29 Apr 10 |
nicklas |
462 |
if (fileId == 0) return; |
5325 |
29 Apr 10 |
nicklas |
463 |
DbControl dc = null; |
5325 |
29 Apr 10 |
nicklas |
464 |
InputStream in = null; |
5325 |
29 Apr 10 |
nicklas |
465 |
try |
5325 |
29 Apr 10 |
nicklas |
466 |
{ |
5325 |
29 Apr 10 |
nicklas |
467 |
dc = TestUtil.getDbControl(); |
5325 |
29 Apr 10 |
nicklas |
468 |
File file = File.getById(dc, fileId); |
5325 |
29 Apr 10 |
nicklas |
469 |
ByteArrayOutputStream download = new ByteArrayOutputStream(); |
5325 |
29 Apr 10 |
nicklas |
470 |
file.download(download, 0); |
5325 |
29 Apr 10 |
nicklas |
471 |
dc.commit(); |
5325 |
29 Apr 10 |
nicklas |
472 |
|
5325 |
29 Apr 10 |
nicklas |
473 |
if (referenceFile != null) |
5325 |
29 Apr 10 |
nicklas |
474 |
{ |
5325 |
29 Apr 10 |
nicklas |
// Read the check-file into a byte array |
5325 |
29 Apr 10 |
nicklas |
476 |
java.io.File checkFile = new java.io.File(referenceFile); |
5325 |
29 Apr 10 |
nicklas |
477 |
ByteArrayOutputStream check = new ByteArrayOutputStream((int)checkFile.length()); |
5325 |
29 Apr 10 |
nicklas |
478 |
FileUtil.copy(new FileInputStream(checkFile), check); |
5325 |
29 Apr 10 |
nicklas |
479 |
if (!Arrays.equals(download.toByteArray(), check.toByteArray())) |
5325 |
29 Apr 10 |
nicklas |
480 |
{ |
5325 |
29 Apr 10 |
nicklas |
481 |
throw new IOException("Downloaded file data (" + file.getName() + |
5325 |
29 Apr 10 |
nicklas |
482 |
") is not equal to reference file: " + referenceFile); |
5325 |
29 Apr 10 |
nicklas |
483 |
} |
5325 |
29 Apr 10 |
nicklas |
484 |
} |
5325 |
29 Apr 10 |
nicklas |
485 |
write("--Download file [" + file.getName() + "] OK"); |
5325 |
29 Apr 10 |
nicklas |
486 |
} |
5325 |
29 Apr 10 |
nicklas |
487 |
catch (Throwable ex) |
5325 |
29 Apr 10 |
nicklas |
488 |
{ |
5325 |
29 Apr 10 |
nicklas |
489 |
write("--Download file [" + referenceFile + "] FAILED"); |
5325 |
29 Apr 10 |
nicklas |
490 |
ex.printStackTrace(); |
5325 |
29 Apr 10 |
nicklas |
491 |
ok = false; |
5325 |
29 Apr 10 |
nicklas |
492 |
} |
5325 |
29 Apr 10 |
nicklas |
493 |
finally |
5325 |
29 Apr 10 |
nicklas |
494 |
{ |
5325 |
29 Apr 10 |
nicklas |
495 |
if (dc != null) dc.close(); |
5325 |
29 Apr 10 |
nicklas |
496 |
if (in != null) |
5325 |
29 Apr 10 |
nicklas |
497 |
{ |
5325 |
29 Apr 10 |
nicklas |
498 |
try |
5325 |
29 Apr 10 |
nicklas |
499 |
{ |
5325 |
29 Apr 10 |
nicklas |
500 |
in.close(); |
5325 |
29 Apr 10 |
nicklas |
501 |
} |
5325 |
29 Apr 10 |
nicklas |
502 |
catch (IOException e) |
5325 |
29 Apr 10 |
nicklas |
503 |
{ |
5325 |
29 Apr 10 |
nicklas |
504 |
e.printStackTrace(); |
5325 |
29 Apr 10 |
nicklas |
505 |
} |
5325 |
29 Apr 10 |
nicklas |
506 |
} |
5325 |
29 Apr 10 |
nicklas |
507 |
} |
5325 |
29 Apr 10 |
nicklas |
508 |
} |
5325 |
29 Apr 10 |
nicklas |
509 |
|
496 |
29 Apr 05 |
enell |
510 |
} |