1 |
1783
|
jones
|
/**
|
2 |
|
|
* '$RCSfile$'
|
3 |
|
|
* Copyright: 2003 Regents of the University of California and the
|
4 |
|
|
* National Center for Ecological Analysis and Synthesis
|
5 |
|
|
* Purpose: To test the MetaCatURL class by JUnit
|
6 |
|
|
*
|
7 |
|
|
* '$Author$'
|
8 |
|
|
* '$Date$'
|
9 |
|
|
* '$Revision$'
|
10 |
|
|
*
|
11 |
|
|
* This program is free software; you can redistribute it and/or modify
|
12 |
|
|
* it under the terms of the GNU General Public License as published by
|
13 |
|
|
* the Free Software Foundation; either version 2 of the License, or
|
14 |
|
|
* (at your option) any later version.
|
15 |
|
|
*
|
16 |
|
|
* This program is distributed in the hope that it will be useful,
|
17 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
18 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
19 |
|
|
* GNU General Public License for more details.
|
20 |
|
|
*
|
21 |
|
|
* You should have received a copy of the GNU General Public License
|
22 |
|
|
* along with this program; if not, write to the Free Software
|
23 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
24 |
|
|
*/
|
25 |
|
|
|
26 |
|
|
package edu.ucsb.nceas.metacattest.client;
|
27 |
|
|
|
28 |
2242
|
sgarg
|
import java.io.File;
|
29 |
1784
|
jones
|
import java.io.FileReader;
|
30 |
3212
|
tao
|
import java.io.FileWriter;
|
31 |
1784
|
jones
|
import java.io.IOException;
|
32 |
|
|
import java.io.Reader;
|
33 |
1789
|
jones
|
import java.io.StringReader;
|
34 |
3212
|
tao
|
import java.io.StringWriter;
|
35 |
1791
|
jones
|
import java.util.Calendar;
|
36 |
2242
|
sgarg
|
import java.util.Date;
|
37 |
1791
|
jones
|
import java.util.GregorianCalendar;
|
38 |
|
|
import java.util.SimpleTimeZone;
|
39 |
|
|
import java.util.TimeZone;
|
40 |
1784
|
jones
|
|
41 |
3172
|
tao
|
import edu.ucsb.nceas.metacat.DocumentImpl;
|
42 |
3780
|
daigle
|
import edu.ucsb.nceas.metacat.MetaCatUtil;
|
43 |
3172
|
tao
|
import edu.ucsb.nceas.metacat.client.DocumentNotFoundException;
|
44 |
2242
|
sgarg
|
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
|
45 |
|
|
import edu.ucsb.nceas.metacat.client.Metacat;
|
46 |
|
|
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
|
47 |
|
|
import edu.ucsb.nceas.metacat.client.MetacatException;
|
48 |
|
|
import edu.ucsb.nceas.metacat.client.MetacatFactory;
|
49 |
|
|
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
|
50 |
|
|
import edu.ucsb.nceas.utilities.IOUtil;
|
51 |
1783
|
jones
|
import junit.framework.Test;
|
52 |
|
|
import junit.framework.TestCase;
|
53 |
|
|
import junit.framework.TestSuite;
|
54 |
2265
|
sgarg
|
import java.io.FileInputStream;
|
55 |
1783
|
jones
|
|
56 |
3172
|
tao
|
import org.apache.log4j.Logger;
|
57 |
|
|
|
58 |
1783
|
jones
|
/**
|
59 |
|
|
* A JUnit test for testing Step class processing
|
60 |
|
|
*/
|
61 |
|
|
public class MetacatClientTest extends TestCase
|
62 |
|
|
{
|
63 |
3780
|
daigle
|
private String metacatUrl = MetaCatUtil.getOption("metacatUrl");
|
64 |
1800
|
tao
|
private String wrongMetacatUrl=
|
65 |
|
|
"http://somepalce.somewhere.com/some/servlet/metacat";
|
66 |
3780
|
daigle
|
private String username = MetaCatUtil.getOption("mcuser");
|
67 |
|
|
private String password = MetaCatUtil.getOption("mcpassword");
|
68 |
|
|
private String anotheruser = MetaCatUtil.getOption("mcanotheruser");
|
69 |
|
|
private String anotherpassword = MetaCatUtil.getOption("mcanotherpassword");
|
70 |
1783
|
jones
|
private String failpass = "uidfnkj43987yfdn";
|
71 |
1791
|
jones
|
private String prefix = "test";
|
72 |
|
|
private String newdocid = null;
|
73 |
1784
|
jones
|
private String testfile = "test/jones.204.22.xml";
|
74 |
2242
|
sgarg
|
private String onlinetestdatafile = "test/onlineDataFile1";
|
75 |
1787
|
tao
|
private String queryFile = "test/query.xml";
|
76 |
1784
|
jones
|
private String testdocument = "";
|
77 |
1783
|
jones
|
private Metacat m;
|
78 |
3172
|
tao
|
private String spatialTestFile = "test/spatialEml.xml";
|
79 |
|
|
private static final int TIME = 30;
|
80 |
3212
|
tao
|
private static final String DOCID = "docid";
|
81 |
|
|
private static final String DATAID = "dataid";
|
82 |
3172
|
tao
|
|
83 |
1783
|
jones
|
/**
|
84 |
|
|
* Constructor to build the test
|
85 |
|
|
*
|
86 |
|
|
* @param name the name of the test method
|
87 |
|
|
*/
|
88 |
|
|
public MetacatClientTest(String name)
|
89 |
|
|
{
|
90 |
|
|
super(name);
|
91 |
1791
|
jones
|
newdocid = generateDocid();
|
92 |
1783
|
jones
|
}
|
93 |
|
|
|
94 |
|
|
/**
|
95 |
|
|
* Establish a testing framework by initializing appropriate objects
|
96 |
|
|
*/
|
97 |
|
|
public void setUp()
|
98 |
|
|
{
|
99 |
|
|
try {
|
100 |
1784
|
jones
|
FileReader fr = new FileReader(testfile);
|
101 |
1788
|
jones
|
testdocument = IOUtil.getAsString(fr, true);
|
102 |
1784
|
jones
|
} catch (IOException ioe) {
|
103 |
|
|
fail("Can't read test data to run the test: " + testfile);
|
104 |
|
|
}
|
105 |
|
|
|
106 |
|
|
try {
|
107 |
1828
|
jones
|
System.err.println("Test Metacat: " + metacatUrl);
|
108 |
1783
|
jones
|
m = MetacatFactory.createMetacatConnection(metacatUrl);
|
109 |
|
|
} catch (MetacatInaccessibleException mie) {
|
110 |
1822
|
jones
|
System.err.println("Metacat is: " + metacatUrl);
|
111 |
1783
|
jones
|
fail("Metacat connection failed." + mie.getMessage());
|
112 |
|
|
}
|
113 |
|
|
}
|
114 |
2242
|
sgarg
|
|
115 |
1783
|
jones
|
/**
|
116 |
|
|
* Release any objects after tests are complete
|
117 |
|
|
*/
|
118 |
|
|
public void tearDown()
|
119 |
|
|
{
|
120 |
|
|
}
|
121 |
2242
|
sgarg
|
|
122 |
1783
|
jones
|
/**
|
123 |
|
|
* Create a suite of tests to be run together
|
124 |
|
|
*/
|
125 |
|
|
public static Test suite()
|
126 |
|
|
{
|
127 |
2242
|
sgarg
|
TestSuite suite = new TestSuite();
|
128 |
2265
|
sgarg
|
suite.addTest(new MetacatClientTest("initialize"));
|
129 |
2987
|
sgarg
|
suite.addTest(new MetacatClientTest("invalidLogin"));
|
130 |
|
|
suite.addTest(new MetacatClientTest("logoutAndInvalidInsert"));
|
131 |
2265
|
sgarg
|
suite.addTest(new MetacatClientTest("login"));
|
132 |
|
|
suite.addTest(new MetacatClientTest("insert"));
|
133 |
2987
|
sgarg
|
suite.addTest(new MetacatClientTest("getNewestDocRevision"));
|
134 |
2981
|
jones
|
suite.addTest(new MetacatClientTest("getLastDocid"));
|
135 |
2987
|
sgarg
|
suite.addTest(new MetacatClientTest("upload"));
|
136 |
|
|
suite.addTest(new MetacatClientTest("upload_stream"));
|
137 |
|
|
suite.addTest(new MetacatClientTest("invalidRead"));
|
138 |
|
|
suite.addTest(new MetacatClientTest("read"));
|
139 |
|
|
suite.addTest(new MetacatClientTest("query"));
|
140 |
3465
|
tao
|
suite.addTest(new MetacatClientTest("queryWithQformat"));
|
141 |
2987
|
sgarg
|
suite.addTest(new MetacatClientTest("invalidUpdate"));
|
142 |
|
|
suite.addTest(new MetacatClientTest("update"));
|
143 |
|
|
suite.addTest(new MetacatClientTest("invalidDelete"));
|
144 |
|
|
suite.addTest(new MetacatClientTest("delete"));
|
145 |
|
|
suite.addTest(new MetacatClientTest("inaccessibleMetacat"));
|
146 |
|
|
suite.addTest(new MetacatClientTest("reuseSession"));
|
147 |
|
|
suite.addTest(new MetacatClientTest("reuseInvalidSession"));
|
148 |
3172
|
tao
|
//suite.addTest(new MetacatClientTest("insertSpatialDocs"));
|
149 |
2265
|
sgarg
|
return suite;
|
150 |
|
|
}
|
151 |
2242
|
sgarg
|
|
152 |
1783
|
jones
|
/**
|
153 |
|
|
* Run an initial test that always passes to check that the test
|
154 |
|
|
* harness is working.
|
155 |
|
|
*/
|
156 |
|
|
public void initialize()
|
157 |
|
|
{
|
158 |
|
|
assertTrue(1 == 1);
|
159 |
|
|
}
|
160 |
2242
|
sgarg
|
|
161 |
1783
|
jones
|
/**
|
162 |
|
|
* Test the login() function with valid credentials
|
163 |
|
|
*/
|
164 |
|
|
public void login()
|
165 |
|
|
{
|
166 |
|
|
// Try a valid login
|
167 |
|
|
try {
|
168 |
1822
|
jones
|
String response = m.login(username, password);
|
169 |
2981
|
jones
|
//System.err.println("Login response: " + response);
|
170 |
1822
|
jones
|
assertTrue(response != null);
|
171 |
|
|
assertTrue(response.indexOf("<login>") != -1);
|
172 |
|
|
String sessionId = m.getSessionId();
|
173 |
2981
|
jones
|
//System.err.println("Session ID: " + m.getSessionId());
|
174 |
1825
|
jones
|
assertTrue(sessionId != null);
|
175 |
|
|
assertTrue(response.indexOf(m.getSessionId()) != -1);
|
176 |
1783
|
jones
|
} catch (MetacatAuthException mae) {
|
177 |
|
|
fail("Authorization failed:\n" + mae.getMessage());
|
178 |
|
|
} catch (MetacatInaccessibleException mie) {
|
179 |
|
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
180 |
|
|
}
|
181 |
|
|
}
|
182 |
|
|
|
183 |
|
|
/**
|
184 |
|
|
* Test the login() function with INVALID credentials
|
185 |
|
|
*/
|
186 |
|
|
public void invalidLogin()
|
187 |
|
|
{
|
188 |
|
|
// Try an invalid login
|
189 |
|
|
try {
|
190 |
|
|
m.login(username, failpass);
|
191 |
|
|
fail("Authorization should have failed.");
|
192 |
|
|
} catch (MetacatAuthException mae) {
|
193 |
|
|
assertTrue(1 == 1);
|
194 |
|
|
} catch (MetacatInaccessibleException mie) {
|
195 |
|
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
196 |
|
|
}
|
197 |
|
|
}
|
198 |
2242
|
sgarg
|
|
199 |
1800
|
tao
|
/**
|
200 |
|
|
* Test the logout() function. When logout, user will be public, it couldn't
|
201 |
|
|
* insert a document.
|
202 |
|
|
*/
|
203 |
|
|
public void logoutAndInvalidInsert()
|
204 |
|
|
{
|
205 |
|
|
try {
|
206 |
|
|
String identifier = newdocid + ".1";
|
207 |
|
|
m.login(username, password);
|
208 |
|
|
m.logout();
|
209 |
2242
|
sgarg
|
String response = m.insert(identifier,
|
210 |
1800
|
tao
|
new StringReader(testdocument), null);
|
211 |
2981
|
jones
|
//System.err.println("Response in logout: "+response);
|
212 |
1800
|
tao
|
assertTrue(response.indexOf("<success>") == -1);
|
213 |
|
|
} catch (MetacatAuthException mae) {
|
214 |
|
|
fail("Authorization failed:\n" + mae.getMessage());
|
215 |
|
|
} catch (MetacatInaccessibleException mie) {
|
216 |
|
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
217 |
|
|
} catch (InsufficientKarmaException ike) {
|
218 |
|
|
fail("Insufficient karma:\n" + ike.getMessage());
|
219 |
|
|
} catch (MetacatException me) {
|
220 |
2265
|
sgarg
|
if(me.getMessage().
|
221 |
|
|
indexOf("Permission denied for user public inser") == -1){
|
222 |
|
|
fail("Metacat Error:\n" + me.getMessage());
|
223 |
|
|
}
|
224 |
1800
|
tao
|
} catch (Exception e) {
|
225 |
|
|
fail("General exception:\n" + e.getMessage());
|
226 |
|
|
}
|
227 |
|
|
}
|
228 |
1784
|
jones
|
|
229 |
|
|
/**
|
230 |
|
|
* Test the read() function with a known document
|
231 |
|
|
*/
|
232 |
|
|
public void read()
|
233 |
|
|
{
|
234 |
|
|
try {
|
235 |
|
|
m.login(username, password);
|
236 |
3212
|
tao
|
String docid = readIdFromFile(DOCID);
|
237 |
|
|
Reader r = m.read(docid+".1");
|
238 |
1788
|
jones
|
String doc = IOUtil.getAsString(r, true);
|
239 |
1800
|
tao
|
doc = doc +"\n";
|
240 |
2981
|
jones
|
//System.err.println(doc);
|
241 |
1784
|
jones
|
assertTrue(doc.equals(testdocument));
|
242 |
|
|
} catch (MetacatAuthException mae) {
|
243 |
|
|
fail("Authorization failed:\n" + mae.getMessage());
|
244 |
|
|
} catch (MetacatInaccessibleException mie) {
|
245 |
|
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
246 |
|
|
} catch (Exception e) {
|
247 |
|
|
fail("General exception:\n" + e.getMessage());
|
248 |
|
|
}
|
249 |
|
|
}
|
250 |
2242
|
sgarg
|
|
251 |
1787
|
tao
|
/**
|
252 |
1800
|
tao
|
* A user try to read a document which it doesn't have read permission
|
253 |
|
|
*/
|
254 |
|
|
public void invalidRead()
|
255 |
|
|
{
|
256 |
|
|
try {
|
257 |
|
|
m.login(anotheruser, anotherpassword);
|
258 |
3212
|
tao
|
String docid = readIdFromFile(DOCID);
|
259 |
|
|
Reader r = m.read(docid+".1");
|
260 |
1800
|
tao
|
String doc = IOUtil.getAsString(r, true);
|
261 |
|
|
assertTrue(doc.indexOf("<error>") != -1);
|
262 |
2981
|
jones
|
//System.err.println(doc);
|
263 |
1800
|
tao
|
} catch (MetacatAuthException mae) {
|
264 |
|
|
fail("Authorization failed:\n" + mae.getMessage());
|
265 |
|
|
} catch (MetacatInaccessibleException mie) {
|
266 |
|
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
267 |
|
|
} catch (InsufficientKarmaException ike) {
|
268 |
|
|
assertTrue(1 == 1);
|
269 |
|
|
} catch (MetacatException e) {
|
270 |
|
|
fail("Metacat exception:\n" + e.getMessage());
|
271 |
3172
|
tao
|
} catch (IOException ioe) {
|
272 |
|
|
fail("IO exception:\n" + ioe.getMessage());
|
273 |
|
|
} catch (DocumentNotFoundException ne) {
|
274 |
|
|
fail("DocumentNotFound exception:\n" + ne.getMessage());
|
275 |
|
|
|
276 |
3212
|
tao
|
} catch(Exception e) {
|
277 |
|
|
fail("Exception:\n" + e.getMessage());
|
278 |
1800
|
tao
|
}
|
279 |
|
|
}
|
280 |
2242
|
sgarg
|
|
281 |
1800
|
tao
|
/**
|
282 |
1787
|
tao
|
* Test the query() function with a known document
|
283 |
|
|
*/
|
284 |
|
|
public void query()
|
285 |
|
|
{
|
286 |
|
|
try {
|
287 |
1828
|
jones
|
m.login(username,password);
|
288 |
1787
|
tao
|
FileReader fr = new FileReader(queryFile);
|
289 |
|
|
Reader r = m.query(fr);
|
290 |
2981
|
jones
|
//System.err.println("Starting query...");
|
291 |
1788
|
jones
|
String result = IOUtil.getAsString(r, true);
|
292 |
3465
|
tao
|
System.err.println("Query result:\n" + result);
|
293 |
3212
|
tao
|
String docid = readIdFromFile(DOCID);
|
294 |
|
|
assertTrue(result.indexOf(docid+".1")!=-1);
|
295 |
3465
|
tao
|
assertTrue(result.indexOf("<?xml")!=-1);
|
296 |
1828
|
jones
|
} catch (MetacatAuthException mae) {
|
297 |
|
|
fail("Authorization failed:\n" + mae.getMessage());
|
298 |
1787
|
tao
|
} catch (MetacatInaccessibleException mie) {
|
299 |
|
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
300 |
|
|
} catch (Exception e) {
|
301 |
|
|
fail("General exception:\n" + e.getMessage());
|
302 |
|
|
}
|
303 |
|
|
}
|
304 |
3465
|
tao
|
/**
|
305 |
|
|
* Test the query() function with a known document
|
306 |
|
|
*/
|
307 |
|
|
public void queryWithQformat()
|
308 |
|
|
{
|
309 |
|
|
try {
|
310 |
|
|
m.login(username,password);
|
311 |
|
|
FileReader fr = new FileReader(queryFile);
|
312 |
|
|
String qformat = "knb";
|
313 |
|
|
Reader r = m.query(fr, qformat);
|
314 |
|
|
//System.err.println("Starting query...");
|
315 |
|
|
String result = IOUtil.getAsString(r, true);
|
316 |
|
|
System.err.println("Query result:\n" + result);
|
317 |
|
|
String docid = readIdFromFile(DOCID);
|
318 |
|
|
assertTrue(result.indexOf(docid+".1")!=-1);
|
319 |
|
|
assertTrue(result.indexOf("<html>")!=-1);
|
320 |
|
|
} catch (MetacatAuthException mae) {
|
321 |
|
|
fail("Authorization failed:\n" + mae.getMessage());
|
322 |
|
|
} catch (MetacatInaccessibleException mie) {
|
323 |
|
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
324 |
|
|
} catch (Exception e) {
|
325 |
|
|
fail("General exception:\n" + e.getMessage());
|
326 |
|
|
}
|
327 |
|
|
}
|
328 |
1789
|
jones
|
|
329 |
|
|
/**
|
330 |
|
|
* Test the insert() function with a known document
|
331 |
|
|
*/
|
332 |
|
|
public void insert()
|
333 |
|
|
{
|
334 |
|
|
try {
|
335 |
1791
|
jones
|
String identifier = newdocid + ".1";
|
336 |
3212
|
tao
|
//write newdocid into file for persistance
|
337 |
|
|
writeIdToFile(DOCID, newdocid);
|
338 |
1789
|
jones
|
m.login(username, password);
|
339 |
2242
|
sgarg
|
String response = m.insert(identifier,
|
340 |
1789
|
jones
|
new StringReader(testdocument), null);
|
341 |
|
|
assertTrue(response.indexOf("<success>") != -1);
|
342 |
1791
|
jones
|
assertTrue(response.indexOf(identifier) != -1);
|
343 |
2981
|
jones
|
//System.err.println(response);
|
344 |
1789
|
jones
|
|
345 |
|
|
} catch (MetacatAuthException mae) {
|
346 |
|
|
fail("Authorization failed:\n" + mae.getMessage());
|
347 |
|
|
} catch (MetacatInaccessibleException mie) {
|
348 |
|
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
349 |
|
|
} catch (InsufficientKarmaException ike) {
|
350 |
1800
|
tao
|
assertTrue(1 == 1);
|
351 |
1789
|
jones
|
fail("Insufficient karma:\n" + ike.getMessage());
|
352 |
|
|
} catch (MetacatException me) {
|
353 |
|
|
fail("Metacat Error:\n" + me.getMessage());
|
354 |
|
|
} catch (Exception e) {
|
355 |
|
|
fail("General exception:\n" + e.getMessage());
|
356 |
|
|
}
|
357 |
|
|
}
|
358 |
1791
|
jones
|
|
359 |
|
|
/**
|
360 |
3779
|
tao
|
* Test the upload() function with a data file.
|
361 |
|
|
* 1. Insert version 1 successfully.
|
362 |
|
|
* 2. Update version 2 successfully
|
363 |
|
|
* 3. Update version 2 again and should be failed.
|
364 |
2242
|
sgarg
|
*/
|
365 |
|
|
public void upload()
|
366 |
|
|
{
|
367 |
|
|
try {
|
368 |
|
|
newdocid = generateDocid();
|
369 |
|
|
String identifier = newdocid + ".1";
|
370 |
3212
|
tao
|
writeIdToFile(DATAID, newdocid);
|
371 |
2242
|
sgarg
|
m.login(username, password);
|
372 |
|
|
String response = m.upload(identifier,
|
373 |
|
|
new File(onlinetestdatafile));
|
374 |
|
|
assertTrue(response.indexOf("<success>") != -1);
|
375 |
|
|
assertTrue(response.indexOf(identifier) != -1);
|
376 |
3425
|
tao
|
identifier = newdocid +".2";
|
377 |
|
|
response = m.upload(identifier,
|
378 |
|
|
new File(onlinetestdatafile));
|
379 |
|
|
assertTrue(response.indexOf("<success>") != -1);
|
380 |
|
|
assertTrue(response.indexOf(identifier) != -1);
|
381 |
2981
|
jones
|
//System.err.println(response);
|
382 |
3779
|
tao
|
//upload the same identifier again. it should return an error
|
383 |
|
|
try
|
384 |
|
|
{
|
385 |
|
|
response = m.upload(identifier,
|
386 |
|
|
new File(onlinetestdatafile));
|
387 |
|
|
fail("Metacat shouldn't successfully upload the same identifier twice "+response);
|
388 |
|
|
}
|
389 |
|
|
catch(Exception ee)
|
390 |
|
|
{
|
391 |
|
|
assertTrue(ee.getMessage().indexOf("<error>") != -1);
|
392 |
|
|
}
|
393 |
2242
|
sgarg
|
|
394 |
|
|
} catch (MetacatAuthException mae) {
|
395 |
|
|
fail("Authorization failed:\n" + mae.getMessage());
|
396 |
|
|
} catch (MetacatInaccessibleException mie) {
|
397 |
|
|
mie.printStackTrace();
|
398 |
|
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
399 |
|
|
} catch (InsufficientKarmaException ike) {
|
400 |
|
|
assertTrue(1 == 1);
|
401 |
|
|
fail("Insufficient karma:\n" + ike.getMessage());
|
402 |
|
|
} catch (MetacatException me) {
|
403 |
|
|
fail("Metacat Error:\n" + me.getMessage());
|
404 |
|
|
} catch (Exception e) {
|
405 |
|
|
fail("General exception:\n" + e.getMessage());
|
406 |
|
|
}
|
407 |
|
|
}
|
408 |
3779
|
tao
|
|
409 |
2242
|
sgarg
|
|
410 |
3779
|
tao
|
|
411 |
2242
|
sgarg
|
/**
|
412 |
2265
|
sgarg
|
* Test the upload() function by passing an InputStream
|
413 |
|
|
*/
|
414 |
|
|
public void upload_stream()
|
415 |
|
|
{
|
416 |
|
|
try {
|
417 |
|
|
newdocid = generateDocid();
|
418 |
|
|
String identifier = newdocid + ".1";
|
419 |
|
|
m.login(username, password);
|
420 |
|
|
File testFile = new File(onlinetestdatafile);
|
421 |
|
|
String response = m.upload(identifier, "onlineDataFile1",
|
422 |
|
|
new FileInputStream(testFile),
|
423 |
|
|
(int) testFile.length());
|
424 |
|
|
|
425 |
|
|
assertTrue(response.indexOf("<success>") != -1);
|
426 |
|
|
assertTrue(response.indexOf(identifier) != -1);
|
427 |
3425
|
tao
|
identifier = newdocid + ".2";
|
428 |
|
|
response = m.upload(identifier, "onlineDataFile1",
|
429 |
|
|
new FileInputStream(testFile),
|
430 |
|
|
(int) testFile.length());
|
431 |
|
|
|
432 |
|
|
assertTrue(response.indexOf("<success>") != -1);
|
433 |
|
|
assertTrue(response.indexOf(identifier) != -1);
|
434 |
2981
|
jones
|
//System.err.println(response);
|
435 |
2265
|
sgarg
|
|
436 |
|
|
} catch (MetacatAuthException mae) {
|
437 |
|
|
fail("Authorization failed:\n" + mae.getMessage());
|
438 |
|
|
} catch (MetacatInaccessibleException mie) {
|
439 |
|
|
mie.printStackTrace();
|
440 |
|
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
441 |
|
|
} catch (InsufficientKarmaException ike) {
|
442 |
|
|
assertTrue(1 == 1);
|
443 |
|
|
fail("Insufficient karma:\n" + ike.getMessage());
|
444 |
|
|
} catch (MetacatException me) {
|
445 |
|
|
fail("Metacat Error:\n" + me.getMessage());
|
446 |
|
|
} catch (Exception e) {
|
447 |
|
|
fail("General exception:\n" + e.getMessage());
|
448 |
|
|
}
|
449 |
|
|
}
|
450 |
|
|
|
451 |
|
|
/**
|
452 |
1800
|
tao
|
* Test the invalidUpdate() function. A user try to update a document
|
453 |
|
|
* which it doesn't have permission
|
454 |
|
|
*/
|
455 |
|
|
public void invalidUpdate()
|
456 |
|
|
{
|
457 |
|
|
try {
|
458 |
3212
|
tao
|
String docid = readIdFromFile(DOCID);
|
459 |
|
|
String identifier = docid + ".2";
|
460 |
1800
|
tao
|
m.login(anotheruser, anotherpassword);
|
461 |
2242
|
sgarg
|
String response = m.update(identifier,
|
462 |
1800
|
tao
|
new StringReader(testdocument), null);
|
463 |
|
|
assertTrue(response.indexOf("<success>") == -1);
|
464 |
2242
|
sgarg
|
|
465 |
1800
|
tao
|
} catch (MetacatAuthException mae) {
|
466 |
|
|
fail("Authorization failed:\n" + mae.getMessage());
|
467 |
|
|
} catch (MetacatInaccessibleException mie) {
|
468 |
|
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
469 |
|
|
} catch (InsufficientKarmaException ike) {
|
470 |
|
|
assertTrue(1 == 1);
|
471 |
|
|
} catch (MetacatException me) {
|
472 |
|
|
fail("Metacat Error:\n" + me.getMessage());
|
473 |
|
|
} catch (Exception e) {
|
474 |
|
|
fail("General exception:\n" + e.getMessage());
|
475 |
|
|
}
|
476 |
|
|
}
|
477 |
2242
|
sgarg
|
|
478 |
1800
|
tao
|
/**
|
479 |
1795
|
jones
|
* Test the update() function with a known document
|
480 |
|
|
*/
|
481 |
|
|
public void update()
|
482 |
|
|
{
|
483 |
|
|
try {
|
484 |
3212
|
tao
|
String docid = readIdFromFile(DOCID);
|
485 |
|
|
String identifier = docid + ".2";
|
486 |
1795
|
jones
|
m.login(username, password);
|
487 |
2242
|
sgarg
|
String response = m.update(identifier,
|
488 |
1795
|
jones
|
new StringReader(testdocument), null);
|
489 |
|
|
assertTrue(response.indexOf("<success>") != -1);
|
490 |
|
|
assertTrue(response.indexOf(identifier) != -1);
|
491 |
2981
|
jones
|
//System.err.println(response);
|
492 |
1795
|
jones
|
|
493 |
|
|
} catch (MetacatAuthException mae) {
|
494 |
|
|
fail("Authorization failed:\n" + mae.getMessage());
|
495 |
|
|
} catch (MetacatInaccessibleException mie) {
|
496 |
|
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
497 |
|
|
} catch (InsufficientKarmaException ike) {
|
498 |
|
|
fail("Insufficient karma:\n" + ike.getMessage());
|
499 |
|
|
} catch (MetacatException me) {
|
500 |
|
|
fail("Metacat Error:\n" + me.getMessage());
|
501 |
|
|
} catch (Exception e) {
|
502 |
|
|
fail("General exception:\n" + e.getMessage());
|
503 |
|
|
}
|
504 |
|
|
}
|
505 |
2242
|
sgarg
|
|
506 |
1800
|
tao
|
/**
|
507 |
|
|
* A user try delete a document which it doesn't have permission
|
508 |
|
|
*/
|
509 |
|
|
public void invalidDelete()
|
510 |
|
|
{
|
511 |
|
|
try {
|
512 |
|
|
String identifier = newdocid + ".2";
|
513 |
|
|
m.login(anotheruser, anotherpassword);
|
514 |
|
|
String response = m.delete(identifier);
|
515 |
|
|
assertTrue(response.indexOf("<success>") == -1);
|
516 |
2981
|
jones
|
//System.err.println(response);
|
517 |
1795
|
jones
|
|
518 |
1800
|
tao
|
} catch (MetacatAuthException mae) {
|
519 |
|
|
fail("Authorization failed:\n" + mae.getMessage());
|
520 |
|
|
} catch (MetacatInaccessibleException mie) {
|
521 |
|
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
522 |
|
|
} catch (InsufficientKarmaException ike) {
|
523 |
|
|
assertTrue(1 == 1);
|
524 |
|
|
} catch (MetacatException me) {
|
525 |
|
|
assertTrue(1 == 1);
|
526 |
|
|
} catch (Exception e) {
|
527 |
|
|
fail("General exception:\n" + e.getMessage());
|
528 |
|
|
}
|
529 |
|
|
}
|
530 |
2242
|
sgarg
|
|
531 |
1795
|
jones
|
/**
|
532 |
|
|
* Test the delete() function with a known document
|
533 |
|
|
*/
|
534 |
|
|
public void delete()
|
535 |
|
|
{
|
536 |
|
|
try {
|
537 |
3212
|
tao
|
Thread.sleep(10000);
|
538 |
|
|
String docid = readIdFromFile(DOCID);
|
539 |
|
|
String identifier = docid + ".2";
|
540 |
1795
|
jones
|
m.login(username, password);
|
541 |
|
|
String response = m.delete(identifier);
|
542 |
|
|
assertTrue(response.indexOf("<success>") != -1);
|
543 |
3212
|
tao
|
// delete the docid persistence file.
|
544 |
|
|
deleteFile(DOCID);
|
545 |
|
|
deleteFile(DATAID);
|
546 |
2981
|
jones
|
//System.err.println(response);
|
547 |
1795
|
jones
|
|
548 |
|
|
} catch (MetacatAuthException mae) {
|
549 |
|
|
fail("Authorization failed:\n" + mae.getMessage());
|
550 |
|
|
} catch (MetacatInaccessibleException mie) {
|
551 |
|
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
552 |
|
|
} catch (InsufficientKarmaException ike) {
|
553 |
|
|
fail("Insufficient karma:\n" + ike.getMessage());
|
554 |
|
|
} catch (MetacatException me) {
|
555 |
|
|
fail("Metacat Error:\n" + me.getMessage());
|
556 |
|
|
} catch (Exception e) {
|
557 |
|
|
fail("General exception:\n" + e.getMessage());
|
558 |
|
|
}
|
559 |
|
|
}
|
560 |
2242
|
sgarg
|
|
561 |
1800
|
tao
|
/**
|
562 |
|
|
* Test the to connect a wrong metacat url. Create a new metacat with a
|
563 |
|
|
* inaccessible url. Then try to login it. If get a MetacatInaccessible
|
564 |
|
|
* exception, this is right.
|
565 |
|
|
*/
|
566 |
1826
|
jones
|
public void inaccessibleMetacat()
|
567 |
1800
|
tao
|
{
|
568 |
|
|
Metacat mWrong = null;
|
569 |
|
|
try {
|
570 |
|
|
mWrong = MetacatFactory.createMetacatConnection(wrongMetacatUrl);
|
571 |
|
|
} catch (MetacatInaccessibleException mie) {
|
572 |
|
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
573 |
|
|
}
|
574 |
2242
|
sgarg
|
|
575 |
1800
|
tao
|
try {
|
576 |
|
|
mWrong.login(username, password);
|
577 |
|
|
} catch (MetacatInaccessibleException mie) {
|
578 |
|
|
assertTrue(1 == 1);
|
579 |
|
|
} catch (MetacatAuthException mae) {
|
580 |
|
|
fail("Authorization failed:\n" + mae.getMessage());
|
581 |
|
|
}
|
582 |
|
|
}
|
583 |
1795
|
jones
|
|
584 |
|
|
/**
|
585 |
1826
|
jones
|
* Try to perform an action that requires a session to be valid without
|
586 |
|
|
* having logged in first by setting the sessionId from a previous
|
587 |
|
|
* session. Then insert a document.
|
588 |
|
|
*/
|
589 |
|
|
public void reuseSession()
|
590 |
|
|
{
|
591 |
|
|
String oldSessionId = "";
|
592 |
|
|
try {
|
593 |
|
|
Metacat mtemp = MetacatFactory.createMetacatConnection(metacatUrl);
|
594 |
|
|
String response = mtemp.login(username, password);
|
595 |
|
|
oldSessionId = mtemp.getSessionId();
|
596 |
2981
|
jones
|
//System.err.println("SessionId (mtemp): " + oldSessionId);
|
597 |
1826
|
jones
|
} catch (MetacatAuthException mae) {
|
598 |
|
|
fail("Authorization failed:\n" + mae.getMessage());
|
599 |
|
|
} catch (MetacatInaccessibleException mie) {
|
600 |
|
|
System.err.println("Metacat is: " + metacatUrl);
|
601 |
|
|
fail("Metacat connection failed." + mie.getMessage());
|
602 |
|
|
}
|
603 |
|
|
|
604 |
|
|
try {
|
605 |
|
|
String identifier = generateDocid() + ".1";
|
606 |
|
|
Metacat m2 = null;
|
607 |
|
|
try {
|
608 |
|
|
m2 = MetacatFactory.createMetacatConnection(metacatUrl);
|
609 |
3588
|
tao
|
m2.logout();
|
610 |
1826
|
jones
|
} catch (MetacatInaccessibleException mie) {
|
611 |
|
|
System.err.println("Metacat is: " + metacatUrl);
|
612 |
|
|
fail("Metacat connection failed." + mie.getMessage());
|
613 |
|
|
}
|
614 |
1828
|
jones
|
System.err.println("SessionId (m2): " + m2.getSessionId());
|
615 |
1826
|
jones
|
m2.setSessionId(oldSessionId);
|
616 |
1828
|
jones
|
System.err.println("SessionId (m2 after set): " + m2.getSessionId());
|
617 |
2242
|
sgarg
|
String response = m2.insert(identifier,
|
618 |
1826
|
jones
|
new StringReader(testdocument), null);
|
619 |
|
|
System.err.println("Reuse Insert response: " + response);
|
620 |
|
|
assertTrue(response.indexOf("<success>") != -1);
|
621 |
|
|
} catch (MetacatInaccessibleException mie) {
|
622 |
|
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
623 |
|
|
} catch (InsufficientKarmaException ike) {
|
624 |
|
|
fail("Insufficient karma:\n" + ike.getMessage());
|
625 |
|
|
} catch (MetacatException me) {
|
626 |
|
|
fail("Metacat Error:\n" + me.getMessage());
|
627 |
|
|
} catch (Exception e) {
|
628 |
|
|
fail("General exception:\n" + e.getMessage());
|
629 |
|
|
}
|
630 |
|
|
}
|
631 |
|
|
|
632 |
|
|
/**
|
633 |
1828
|
jones
|
* Try to perform an action that requires a session to be valid without
|
634 |
2242
|
sgarg
|
* having logged in first, but use an invalid session identifier.
|
635 |
1829
|
jones
|
* Then insert a document, which should fail.
|
636 |
1828
|
jones
|
*/
|
637 |
|
|
public void reuseInvalidSession()
|
638 |
|
|
{
|
639 |
1829
|
jones
|
System.err.println("Starting resuseInvalidSession test...");
|
640 |
1828
|
jones
|
String oldSessionId = "foobar";
|
641 |
|
|
try {
|
642 |
|
|
String identifier = generateDocid() + ".1";
|
643 |
1829
|
jones
|
Metacat m3 = null;
|
644 |
1828
|
jones
|
try {
|
645 |
1829
|
jones
|
m3 = MetacatFactory.createMetacatConnection(metacatUrl);
|
646 |
3588
|
tao
|
m3.logout();
|
647 |
1828
|
jones
|
} catch (MetacatInaccessibleException mie) {
|
648 |
|
|
System.err.println("Metacat is: " + metacatUrl);
|
649 |
|
|
fail("Metacat connection failed." + mie.getMessage());
|
650 |
|
|
}
|
651 |
1829
|
jones
|
System.err.println("SessionId (m3): " + m3.getSessionId());
|
652 |
|
|
m3.setSessionId(oldSessionId);
|
653 |
|
|
System.err.println("SessionId (m3 after set): " + m3.getSessionId());
|
654 |
|
|
System.err.println("Performing resuseInvalidSession insert: " +
|
655 |
|
|
identifier);
|
656 |
2242
|
sgarg
|
String response = m3.insert(identifier,
|
657 |
1828
|
jones
|
new StringReader(testdocument), null);
|
658 |
1829
|
jones
|
System.err.println("ReuseInvalid Insert response: " + response);
|
659 |
1828
|
jones
|
assertTrue(response.indexOf("<success>") == -1);
|
660 |
|
|
} catch (MetacatInaccessibleException mie) {
|
661 |
|
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
662 |
|
|
} catch (InsufficientKarmaException ike) {
|
663 |
|
|
fail("Insufficient karma:\n" + ike.getMessage());
|
664 |
|
|
} catch (MetacatException me) {
|
665 |
2265
|
sgarg
|
if(me.getMessage().
|
666 |
|
|
indexOf("Permission denied for user public inser") == -1){
|
667 |
|
|
fail("Metacat Error:\n" + me.getMessage());
|
668 |
|
|
}
|
669 |
1828
|
jones
|
} catch (Exception e) {
|
670 |
|
|
fail("General exception:\n" + e.getMessage());
|
671 |
|
|
}
|
672 |
|
|
}
|
673 |
2338
|
tao
|
|
674 |
2981
|
jones
|
/**
|
675 |
|
|
* Get the most recent document id for a given scope and be sure
|
676 |
|
|
* that it matches the one we last inserted. Assumes this test is run
|
677 |
|
|
* immediately following a successful insert() test.
|
678 |
|
|
*/
|
679 |
|
|
public void getLastDocid()
|
680 |
|
|
{
|
681 |
|
|
try {
|
682 |
|
|
Metacat m3 = null;
|
683 |
|
|
try {
|
684 |
|
|
m3 = MetacatFactory.createMetacatConnection(metacatUrl);
|
685 |
|
|
} catch (MetacatInaccessibleException mie) {
|
686 |
|
|
System.err.println("Metacat is: " + metacatUrl);
|
687 |
|
|
fail("Metacat connection failed." + mie.getMessage());
|
688 |
|
|
}
|
689 |
|
|
String lastId = m3.getLastDocid(prefix);
|
690 |
2992
|
berkley
|
System.err.println("Last Id: " + lastId);
|
691 |
3212
|
tao
|
//get docid from file
|
692 |
|
|
String docid = readIdFromFile(DOCID);
|
693 |
|
|
assertTrue(lastId.equals(docid + ".1"));
|
694 |
2981
|
jones
|
} catch (MetacatException me) {
|
695 |
|
|
fail("Metacat Error:\n" + me.getMessage());
|
696 |
|
|
} catch (Exception e) {
|
697 |
|
|
fail("General exception:\n" + e.getMessage());
|
698 |
|
|
}
|
699 |
|
|
}
|
700 |
|
|
|
701 |
|
|
/**
|
702 |
2338
|
tao
|
* Try to perform an action that requires a session to be valid without
|
703 |
|
|
* having logged in first, but use an invalid session identifier.
|
704 |
|
|
* Then insert a document, which should fail.
|
705 |
|
|
*/
|
706 |
|
|
public void getNewestDocRevision()
|
707 |
|
|
{
|
708 |
2981
|
jones
|
//System.err.println("Starting getNewestDocRevision test...");
|
709 |
2338
|
tao
|
try {
|
710 |
|
|
|
711 |
|
|
Metacat m3 = null;
|
712 |
|
|
try {
|
713 |
|
|
m3 = MetacatFactory.createMetacatConnection(metacatUrl);
|
714 |
|
|
} catch (MetacatInaccessibleException mie) {
|
715 |
|
|
System.err.println("Metacat is: " + metacatUrl);
|
716 |
|
|
fail("Metacat connection failed." + mie.getMessage());
|
717 |
|
|
}
|
718 |
3212
|
tao
|
// get docid from file
|
719 |
|
|
String docid = readIdFromFile(DOCID);
|
720 |
|
|
int revision = m3.getNewestDocRevision(docid);
|
721 |
2981
|
jones
|
//System.err.println("Newest revision number is: " + revision);
|
722 |
2338
|
tao
|
assertTrue(revision == 1);
|
723 |
|
|
} catch (MetacatException me) {
|
724 |
|
|
if(me.getMessage().
|
725 |
|
|
indexOf("Permission denied for user public inser") == -1){
|
726 |
|
|
fail("Metacat Error:\n" + me.getMessage());
|
727 |
|
|
}
|
728 |
|
|
} catch (Exception e) {
|
729 |
|
|
fail("General exception:\n" + e.getMessage());
|
730 |
|
|
}
|
731 |
|
|
}
|
732 |
3172
|
tao
|
|
733 |
|
|
/**
|
734 |
|
|
* Try to insert a bunch of eml documents which contain spatial information
|
735 |
|
|
* This test is used to try to find a bug in spatial part of metacat
|
736 |
|
|
*/
|
737 |
|
|
public void insertSpatialDocs() throws IOException
|
738 |
|
|
{
|
739 |
|
|
FileReader fr = new FileReader(spatialTestFile);
|
740 |
|
|
String spatialtestdocument = IOUtil.getAsString(fr, true);
|
741 |
|
|
System.out.println("the eml is "+spatialtestdocument);
|
742 |
|
|
for (int i=0; i<TIME; i++)
|
743 |
|
|
{
|
744 |
|
|
try {
|
745 |
|
|
newdocid = generateDocid();
|
746 |
|
|
String identifier = newdocid + ".1";
|
747 |
|
|
System.out.println("the docid is "+identifier);
|
748 |
|
|
m.login(username, password);
|
749 |
|
|
String response = m.insert(identifier,
|
750 |
|
|
new StringReader(spatialtestdocument), null);
|
751 |
|
|
assertTrue(response.indexOf("<success>") != -1);
|
752 |
|
|
assertTrue(response.indexOf(identifier) != -1);
|
753 |
|
|
Thread.sleep(8000);
|
754 |
|
|
identifier = newdocid +".2";
|
755 |
|
|
response = m.update(identifier,
|
756 |
|
|
new StringReader(spatialtestdocument), null);
|
757 |
|
|
assertTrue(response.indexOf("<success>") != -1);
|
758 |
3186
|
tao
|
Thread.sleep(6000);
|
759 |
|
|
String response2 = m.delete(identifier);
|
760 |
|
|
assertTrue(response2.indexOf("<success>") != -1);
|
761 |
3172
|
tao
|
//System.err.println(response);
|
762 |
|
|
|
763 |
|
|
} catch (MetacatAuthException mae) {
|
764 |
|
|
fail("Authorization failed:\n" + mae.getMessage());
|
765 |
|
|
} catch (MetacatInaccessibleException mie) {
|
766 |
|
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
767 |
|
|
} catch (InsufficientKarmaException ike) {
|
768 |
|
|
assertTrue(1 == 1);
|
769 |
|
|
fail("Insufficient karma:\n" + ike.getMessage());
|
770 |
|
|
} catch (MetacatException me) {
|
771 |
|
|
fail("Metacat Error:\n" + me.getMessage());
|
772 |
|
|
} catch (Exception e) {
|
773 |
|
|
fail("General exception:\n" + e.getMessage());
|
774 |
|
|
}
|
775 |
|
|
}
|
776 |
|
|
}
|
777 |
2338
|
tao
|
|
778 |
|
|
/**
|
779 |
1791
|
jones
|
* Create a hopefully unique docid for testing insert and update. Does
|
780 |
|
|
* not include the 'revision' part of the id.
|
781 |
|
|
*
|
782 |
|
|
* @return a String docid based on the current date and time
|
783 |
|
|
*/
|
784 |
|
|
private String generateDocid()
|
785 |
|
|
{
|
786 |
2988
|
sgarg
|
StringBuffer docid = new StringBuffer(prefix);
|
787 |
|
|
docid.append(".");
|
788 |
|
|
|
789 |
1791
|
jones
|
// Create a calendar to get the date formatted properly
|
790 |
|
|
String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
|
791 |
|
|
SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
|
792 |
|
|
pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2*60*60*1000);
|
793 |
|
|
pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*60*60*1000);
|
794 |
|
|
Calendar calendar = new GregorianCalendar(pdt);
|
795 |
|
|
Date trialTime = new Date();
|
796 |
|
|
calendar.setTime(trialTime);
|
797 |
2988
|
sgarg
|
|
798 |
|
|
int time = 0;
|
799 |
|
|
|
800 |
|
|
docid.append(calendar.get(Calendar.YEAR));
|
801 |
|
|
|
802 |
|
|
time = calendar.get(Calendar.DAY_OF_YEAR);
|
803 |
|
|
if(time < 10){
|
804 |
|
|
docid.append("0");
|
805 |
|
|
docid.append("0");
|
806 |
|
|
docid.append(time);
|
807 |
|
|
} else if(time < 100) {
|
808 |
|
|
docid.append("0");
|
809 |
|
|
docid.append(time);
|
810 |
|
|
} else {
|
811 |
|
|
docid.append(time);
|
812 |
|
|
}
|
813 |
|
|
|
814 |
|
|
time = calendar.get(Calendar.HOUR_OF_DAY);
|
815 |
|
|
if(time < 10){
|
816 |
|
|
docid.append("0");
|
817 |
|
|
docid.append(time);
|
818 |
|
|
} else {
|
819 |
|
|
docid.append(time);
|
820 |
|
|
}
|
821 |
|
|
|
822 |
|
|
time = calendar.get(Calendar.MINUTE);
|
823 |
|
|
if(time < 10){
|
824 |
|
|
docid.append("0");
|
825 |
|
|
docid.append(time);
|
826 |
|
|
} else {
|
827 |
|
|
docid.append(time);
|
828 |
|
|
}
|
829 |
|
|
|
830 |
|
|
time = calendar.get(Calendar.SECOND);
|
831 |
|
|
if(time < 10){
|
832 |
|
|
docid.append("0");
|
833 |
|
|
docid.append(time);
|
834 |
|
|
} else {
|
835 |
|
|
docid.append(time);
|
836 |
|
|
}
|
837 |
3187
|
tao
|
|
838 |
|
|
//sometimes this number is not unique, so we append a random number
|
839 |
|
|
int random = (new Double(Math.random()*100)).intValue();
|
840 |
|
|
docid.append(random);
|
841 |
|
|
|
842 |
2988
|
sgarg
|
return docid.toString();
|
843 |
1791
|
jones
|
}
|
844 |
3212
|
tao
|
|
845 |
|
|
/*
|
846 |
|
|
* Write id to a file for persistance
|
847 |
|
|
*/
|
848 |
|
|
private void writeIdToFile(String fileName, String id) throws Exception
|
849 |
|
|
{
|
850 |
|
|
File file = new File(fileName);
|
851 |
|
|
StringReader reader = new StringReader(id);
|
852 |
|
|
FileWriter writer = new FileWriter(file);
|
853 |
|
|
char [] buffer = new char[1024];
|
854 |
|
|
int c = reader.read(buffer);
|
855 |
|
|
while (c != -1)
|
856 |
|
|
{
|
857 |
|
|
writer.write(buffer, 0, c);
|
858 |
|
|
c = reader.read(buffer);
|
859 |
|
|
}
|
860 |
|
|
writer.close();
|
861 |
|
|
reader.close();
|
862 |
|
|
}
|
863 |
|
|
|
864 |
|
|
/*
|
865 |
|
|
* Read id from a given file
|
866 |
|
|
*/
|
867 |
|
|
private String readIdFromFile(String fileName) throws Exception
|
868 |
|
|
{
|
869 |
|
|
File file = new File(fileName);
|
870 |
|
|
FileReader reader = new FileReader(file);
|
871 |
|
|
StringWriter writer = new StringWriter();
|
872 |
|
|
char [] buffer = new char[1024];
|
873 |
|
|
int c = reader.read(buffer);
|
874 |
|
|
while (c != -1)
|
875 |
|
|
{
|
876 |
|
|
writer.write(buffer, 0, c);
|
877 |
|
|
c = reader.read(buffer);
|
878 |
|
|
}
|
879 |
|
|
reader.close();
|
880 |
|
|
return writer.toString();
|
881 |
|
|
}
|
882 |
|
|
|
883 |
|
|
/*
|
884 |
|
|
* Delete the given file
|
885 |
|
|
*/
|
886 |
|
|
private void deleteFile(String fileName) throws Exception
|
887 |
|
|
{
|
888 |
|
|
File file = new File(fileName);
|
889 |
|
|
file.delete();
|
890 |
|
|
}
|
891 |
1783
|
jones
|
}
|