1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Copyright: 2004 Regents of the University of California and the
|
4
|
* National Center for Ecological Analysis and Synthesis
|
5
|
* Purpose: To test the Access Controls in metacat by JUnit
|
6
|
*
|
7
|
* '$Author: leinfelder $'
|
8
|
* '$Date: 2011-12-16 09:37:02 -0800 (Fri, 16 Dec 2011) $'
|
9
|
* '$Revision: 6796 $'
|
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;
|
27
|
|
28
|
import java.io.InputStream;
|
29
|
import java.io.InputStreamReader;
|
30
|
import java.io.Reader;
|
31
|
import java.io.StringReader;
|
32
|
import java.util.Calendar;
|
33
|
import java.util.Date;
|
34
|
import java.util.GregorianCalendar;
|
35
|
import java.util.SimpleTimeZone;
|
36
|
import java.util.TimeZone;
|
37
|
|
38
|
import edu.ucsb.nceas.MCTestCase;
|
39
|
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
|
40
|
import edu.ucsb.nceas.metacat.client.Metacat;
|
41
|
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
|
42
|
import edu.ucsb.nceas.metacat.client.MetacatException;
|
43
|
import edu.ucsb.nceas.metacat.client.MetacatFactory;
|
44
|
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
|
45
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
46
|
import edu.ucsb.nceas.utilities.FileUtil;
|
47
|
import edu.ucsb.nceas.utilities.IOUtil;
|
48
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
49
|
import junit.framework.Test;
|
50
|
import junit.framework.TestSuite;
|
51
|
import java.io.File;
|
52
|
|
53
|
import org.apache.commons.io.IOUtils;
|
54
|
|
55
|
/**
|
56
|
* A JUnit test for testing Metacat when Non Ascii Characters are inserted
|
57
|
*/
|
58
|
public class InternationalizationTest
|
59
|
extends MCTestCase {
|
60
|
|
61
|
/**
|
62
|
* Returns an xml squery that searches for the doc id in the
|
63
|
* title of documents. This function is for eml-2.0.1+ only. For
|
64
|
* other eml versions, this function might have to modified.
|
65
|
* NOTE: this override includes the "value" i18n element for eml 2.1.1
|
66
|
*/
|
67
|
@Override
|
68
|
protected String getTestEmlQuery(String titlePart, String emlVersion) {
|
69
|
|
70
|
String docType;
|
71
|
if (emlVersion.equals(EML2_0_1)) {
|
72
|
docType = "eml://ecoinformatics.org/eml-2.0.1";
|
73
|
} else if (emlVersion.equals(EML2_1_0)) {
|
74
|
docType = "eml://ecoinformatics.org/eml-2.1.0";
|
75
|
} else { //if (emlVersion.equals(EML2_1_1)) {
|
76
|
docType = "eml://ecoinformatics.org/eml-2.1.1";
|
77
|
}
|
78
|
|
79
|
String sQuery = "";
|
80
|
sQuery =
|
81
|
"<pathquery version=\"1.0\">" +
|
82
|
"<meta_file_id>unspecified</meta_file_id>" +
|
83
|
"<querytitle>unspecified</querytitle>" +
|
84
|
"<returnfield>dataset/title</returnfield>" +
|
85
|
"<returnfield>dataset/title/value</returnfield>" +
|
86
|
"<returndoctype>" + docType + "</returndoctype>" +
|
87
|
"<querygroup operator=\"UNION\">" +
|
88
|
"<queryterm casesensitive=\"false\" searchmode=\"contains\">" +
|
89
|
"<value>" + titlePart + "</value>" +
|
90
|
"<pathexpr>dataset/title</pathexpr>" +
|
91
|
"</queryterm>" +
|
92
|
"<queryterm casesensitive=\"false\" searchmode=\"contains\">" +
|
93
|
"<value>" + titlePart + "</value>" +
|
94
|
"<pathexpr>dataset/title/value</pathexpr>" +
|
95
|
"</queryterm>" +
|
96
|
"</querygroup>" +
|
97
|
"</pathquery>";
|
98
|
|
99
|
return sQuery;
|
100
|
}
|
101
|
|
102
|
/**
|
103
|
* Constructor to build the test
|
104
|
*
|
105
|
* @param name the name of the test method
|
106
|
*/
|
107
|
public InternationalizationTest(String name) {
|
108
|
super(name);
|
109
|
}
|
110
|
|
111
|
/**
|
112
|
* Establish a testing framework by initializing appropriate objects
|
113
|
*/
|
114
|
public void setUp() {
|
115
|
try {
|
116
|
System.err.println("Test Metacat: " + metacatUrl);
|
117
|
m = MetacatFactory.createMetacatConnection(metacatUrl);
|
118
|
}
|
119
|
catch (MetacatInaccessibleException mie) {
|
120
|
System.err.println("Metacat is: " + metacatUrl);
|
121
|
fail("Metacat connection failed." + mie.getMessage());
|
122
|
}
|
123
|
}
|
124
|
|
125
|
/**
|
126
|
* Release any objects after tests are complete
|
127
|
*/
|
128
|
public void tearDown() {
|
129
|
}
|
130
|
|
131
|
/**
|
132
|
* Create a suite of tests to be run together
|
133
|
*/
|
134
|
public static Test suite() {
|
135
|
TestSuite suite = new TestSuite();
|
136
|
suite.addTest(new InternationalizationTest("initialize"));
|
137
|
// Test basic functions
|
138
|
//suite.addTest(new InternationalizationTest("unicodeCharacterTest"));
|
139
|
suite.addTest(new InternationalizationTest("translation211Test"));
|
140
|
return suite;
|
141
|
}
|
142
|
|
143
|
/**
|
144
|
* Run an initial test that always passes to check that the test
|
145
|
* harness is working.
|
146
|
*/
|
147
|
public void initialize() {
|
148
|
assertTrue(1 == 1);
|
149
|
}
|
150
|
|
151
|
/**
|
152
|
* Test inserting and reading an EML 2.1.0 document with the code representation
|
153
|
* of a micro sign (µ). Read should succeed since the same document should be
|
154
|
* read back from disk that was submitted. Query should succeed because we look
|
155
|
* for the converted character (µ).
|
156
|
*/
|
157
|
public void numericCharacterReferenceFormat210Test() {
|
158
|
debug("\nRunning: numericCharacterReferenceFormat210Test");
|
159
|
try {
|
160
|
String newdocid = generateDocid();
|
161
|
m.login(username, password);
|
162
|
|
163
|
String testTitle = "Checking µ in doc: " + newdocid + ".1";
|
164
|
String convertedTestTitle = "Checking µ in doc: " + newdocid + ".1";
|
165
|
|
166
|
testdocument = getTestEmlDoc(testTitle, EML2_1_0);
|
167
|
insertDocid(newdocid + ".1", testdocument, SUCCESS, true);
|
168
|
|
169
|
// this tests reading the document back from disk
|
170
|
readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
|
171
|
|
172
|
// this tests searching for the document in the database
|
173
|
Thread.sleep(3000);
|
174
|
queryDocWhichHasTitle(newdocid + ".1", convertedTestTitle, EML2_1_0, SUCCESS);
|
175
|
|
176
|
deleteDocid(newdocid + ".1", SUCCESS, false);
|
177
|
|
178
|
m.logout();
|
179
|
}
|
180
|
catch (MetacatAuthException mae) {
|
181
|
fail("Authorization failed:\n" + mae.getMessage());
|
182
|
}
|
183
|
catch (MetacatInaccessibleException mie) {
|
184
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
185
|
}
|
186
|
catch (Exception e) {
|
187
|
fail("General exception:\n" + e.getMessage());
|
188
|
}
|
189
|
}
|
190
|
|
191
|
/**
|
192
|
* Test inserting and reading an EML 2.1.1 document with the multiple title translations.
|
193
|
* Read should succeed since the same document should be read back from disk
|
194
|
* that was submitted. Query should succeed because we look for the same original and translation
|
195
|
*/
|
196
|
public void translation211Test() {
|
197
|
debug("\nRunning: translation211Test");
|
198
|
try {
|
199
|
String newdocid = generateDocid();
|
200
|
newdocid += ".1";
|
201
|
m.login(username, password);
|
202
|
|
203
|
String title_en_US = "Translation for document: " + newdocid;
|
204
|
String title_zh_TW = "翻譯的文件: " + newdocid;
|
205
|
|
206
|
String mixedTitle =
|
207
|
title_en_US +
|
208
|
"<value xml:lang=\"zh-TW\">" +
|
209
|
title_zh_TW +
|
210
|
"</value>";
|
211
|
|
212
|
testdocument = getTestEmlDoc(mixedTitle, EML2_1_1);
|
213
|
insertDocid(newdocid, testdocument, SUCCESS, false);
|
214
|
|
215
|
// this tests reading the document back from disk
|
216
|
readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
|
217
|
|
218
|
// this tests searching for the document in the database
|
219
|
Thread.sleep(3000);
|
220
|
queryDocWhichHasTitle(newdocid, title_en_US, EML2_1_1, SUCCESS);
|
221
|
queryDocWhichHasTitle(newdocid, title_zh_TW, EML2_1_1, SUCCESS);
|
222
|
|
223
|
deleteDocid(newdocid, SUCCESS, false);
|
224
|
|
225
|
m.logout();
|
226
|
}
|
227
|
catch (MetacatAuthException mae) {
|
228
|
fail("Authorization failed:\n" + mae.getMessage());
|
229
|
}
|
230
|
catch (MetacatInaccessibleException mie) {
|
231
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
232
|
}
|
233
|
catch (Exception e) {
|
234
|
fail("General exception:\n" + e.getMessage());
|
235
|
}
|
236
|
}
|
237
|
|
238
|
/**
|
239
|
* Test inserting and reading an EML 2.1.0 with Chinese
|
240
|
*/
|
241
|
public void unicodeCharacterTest() {
|
242
|
debug("\nRunning: unicodeCharacterTest");
|
243
|
try {
|
244
|
|
245
|
String filePath = "test/clienttestfiles/unicodeEML.xml";
|
246
|
String testTitle = "測試中的數據包 (Test Chinese data package) _DOCID_";
|
247
|
String newdocid = generateDocid() + ".1";
|
248
|
testdocument = FileUtil.readFileToString(filePath, "UTF-8");
|
249
|
|
250
|
// include the docid
|
251
|
testdocument = testdocument.replaceAll("_DOCID_", newdocid);
|
252
|
testTitle = testTitle.replaceAll("_DOCID_", newdocid);
|
253
|
|
254
|
// login
|
255
|
m.login(username, password);
|
256
|
|
257
|
// insert
|
258
|
insertDocid(newdocid, testdocument, SUCCESS, false);
|
259
|
|
260
|
// this tests reading the document back from disk
|
261
|
readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
|
262
|
|
263
|
// this tests searching for the document in the database
|
264
|
Thread.sleep(3000);
|
265
|
queryDocWhichHasTitle(testTitle, testTitle, EML2_1_0, SUCCESS);
|
266
|
|
267
|
// clean up
|
268
|
//deleteDocid(newdocid, SUCCESS, false);
|
269
|
|
270
|
m.logout();
|
271
|
}
|
272
|
catch (MetacatAuthException mae) {
|
273
|
fail("Authorization failed:\n" + mae.getMessage());
|
274
|
}
|
275
|
catch (MetacatInaccessibleException mie) {
|
276
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
277
|
}
|
278
|
catch (Exception e) {
|
279
|
fail("General exception:\n" + e.getMessage());
|
280
|
}
|
281
|
}
|
282
|
|
283
|
/**
|
284
|
* Insert a document into metacat. The expected result is passed as result
|
285
|
*/
|
286
|
private String insertDocid(String docid, String docText, boolean result,
|
287
|
boolean expectMetacatException) {
|
288
|
String response = null;
|
289
|
try {
|
290
|
|
291
|
debug("doctext: " + docText);
|
292
|
|
293
|
response = m.insert(docid,
|
294
|
new StringReader(docText), null);
|
295
|
System.err.println(response);
|
296
|
if (result) {
|
297
|
assertTrue( (response.indexOf("<success>") != -1));
|
298
|
assertTrue(response.indexOf(docid) != -1);
|
299
|
}
|
300
|
else {
|
301
|
assertTrue( (response.indexOf("<success>") == -1));
|
302
|
}
|
303
|
}
|
304
|
catch (MetacatInaccessibleException mie) {
|
305
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
306
|
}
|
307
|
catch (InsufficientKarmaException ike) {
|
308
|
fail("Insufficient karma:\n" + ike.getMessage());
|
309
|
}
|
310
|
catch (MetacatException me) {
|
311
|
if (!expectMetacatException) {
|
312
|
fail("Metacat Error:\n" + me.getMessage());
|
313
|
}
|
314
|
}
|
315
|
catch (Exception e) {
|
316
|
fail("General exception:\n" + e.getMessage());
|
317
|
}
|
318
|
return response;
|
319
|
}
|
320
|
|
321
|
/**
|
322
|
* Insert a document into metacat. The expected result is passed as result
|
323
|
*/
|
324
|
private String uploadDocid(String docid, String filePath, boolean result,
|
325
|
boolean expectedKarmaException) {
|
326
|
String response = null;
|
327
|
try {
|
328
|
response = m.upload(docid, new File(filePath));
|
329
|
if (result) {
|
330
|
assertTrue( (response.indexOf("<success>") != -1));
|
331
|
assertTrue(response.indexOf(docid) != -1);
|
332
|
}
|
333
|
else {
|
334
|
assertTrue( (response.indexOf("<success>") == -1));
|
335
|
}
|
336
|
System.err.println("respose from metacat: " + response);
|
337
|
}
|
338
|
catch (MetacatInaccessibleException mie) {
|
339
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
340
|
}
|
341
|
catch (InsufficientKarmaException ike) {
|
342
|
if (!expectedKarmaException) {
|
343
|
fail("Insufficient karma:\n" + ike.getMessage());
|
344
|
}
|
345
|
}
|
346
|
catch (MetacatException me) {
|
347
|
if (result) {
|
348
|
fail("Metacat Error:\n" + me.getMessage());
|
349
|
} else {
|
350
|
System.err.println("Metacat Error:\n" + me.getMessage());
|
351
|
}
|
352
|
}
|
353
|
catch (Exception e) {
|
354
|
fail("General exception:\n" + e.getMessage());
|
355
|
}
|
356
|
return response;
|
357
|
}
|
358
|
|
359
|
/**
|
360
|
* Update a document in metacat. The expected result is passed as result
|
361
|
*/
|
362
|
private String updateDocid(String docid, String docText, boolean result,
|
363
|
boolean expectedKarmaFailure) {
|
364
|
String response = null;
|
365
|
try {
|
366
|
response = m.update(docid,
|
367
|
new StringReader(testdocument), null);
|
368
|
|
369
|
if (result) {
|
370
|
assertTrue( (response.indexOf("<success>") != -1));
|
371
|
assertTrue(response.indexOf(docid) != -1);
|
372
|
}
|
373
|
else {
|
374
|
assertTrue( (response.indexOf("<success>") == -1));
|
375
|
}
|
376
|
System.err.println(response);
|
377
|
}
|
378
|
catch (MetacatInaccessibleException mie) {
|
379
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
380
|
}
|
381
|
catch (InsufficientKarmaException ike) {
|
382
|
if (!expectedKarmaFailure) {
|
383
|
fail("Insufficient karma:\n" + ike.getMessage());
|
384
|
}
|
385
|
}
|
386
|
catch (MetacatException me) {
|
387
|
if (result) {
|
388
|
fail("Metacat Error:\n" + me.getMessage());
|
389
|
} else {
|
390
|
System.err.println("Metacat Error:\n" + me.getMessage());
|
391
|
}
|
392
|
}
|
393
|
catch (Exception e) {
|
394
|
fail("General exception:\n" + e.getMessage());
|
395
|
}
|
396
|
|
397
|
return response;
|
398
|
}
|
399
|
|
400
|
/**
|
401
|
* Delete a document from metacat. The expected result is passed as result
|
402
|
*/
|
403
|
private void deleteDocid(String docid, boolean result,
|
404
|
boolean expextedKarmaFailure) {
|
405
|
try {
|
406
|
String response = m.delete(docid);
|
407
|
if (result) {
|
408
|
assertTrue(response.indexOf("<success>") != -1);
|
409
|
}
|
410
|
else {
|
411
|
assertTrue(response.indexOf("<success>") == -1);
|
412
|
}
|
413
|
System.err.println(response);
|
414
|
}
|
415
|
catch (MetacatInaccessibleException mie) {
|
416
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
417
|
}
|
418
|
catch (InsufficientKarmaException ike) {
|
419
|
if(!expextedKarmaFailure){
|
420
|
fail("Insufficient karma:\n" + ike.getMessage());
|
421
|
}
|
422
|
}
|
423
|
catch (MetacatException me) {
|
424
|
if (result) {
|
425
|
fail("Metacat Error:\n" + me.getMessage());
|
426
|
} else {
|
427
|
System.err.println("Metacat Error:\n" + me.getMessage());
|
428
|
}
|
429
|
}
|
430
|
catch (Exception e) {
|
431
|
fail("General exception:\n" + e.getMessage());
|
432
|
}
|
433
|
}
|
434
|
|
435
|
/**
|
436
|
* Read a document from metacat. The expected result is passed as result
|
437
|
*/
|
438
|
private void readDocid(String docid, boolean result,
|
439
|
boolean expextedKarmaFailure) {
|
440
|
try {
|
441
|
Reader r = new InputStreamReader(m.read(docid));
|
442
|
String response = IOUtil.getAsString(r, true);
|
443
|
|
444
|
if (!result) {
|
445
|
assertTrue(response.indexOf("<success>") == -1);
|
446
|
}
|
447
|
// System.err.println(response);
|
448
|
}
|
449
|
catch (MetacatInaccessibleException mie) {
|
450
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
451
|
}
|
452
|
catch (InsufficientKarmaException ike) {
|
453
|
if (!expextedKarmaFailure) {
|
454
|
fail("Insufficient karma:\n" + ike.getMessage());
|
455
|
}
|
456
|
}
|
457
|
catch (MetacatException me) {
|
458
|
fail("Metacat Error:\n" + me.getMessage());
|
459
|
}
|
460
|
catch (Exception e) {
|
461
|
fail("General exception:\n" + e.getMessage());
|
462
|
}
|
463
|
}
|
464
|
|
465
|
/**
|
466
|
* Read a document from metacat and check if it is equal to a given string.
|
467
|
* The expected result is passed as result
|
468
|
*/
|
469
|
private void readDocidWhichEqualsDoc(String docid, String testDoc,
|
470
|
boolean result,
|
471
|
boolean expectedKarmaFailure) {
|
472
|
try {
|
473
|
Reader r = new InputStreamReader(m.read(docid), "UTF-8");
|
474
|
//InputStream is = m.read(docid);
|
475
|
String doc = IOUtil.getAsString(r, true);
|
476
|
//String doc = IOUtils.toString(is);
|
477
|
|
478
|
if (result) {
|
479
|
|
480
|
if (!testDoc.equals(doc)) {
|
481
|
debug("doc :" + doc);
|
482
|
debug("testDoc:" + testDoc);
|
483
|
}
|
484
|
|
485
|
assertTrue(testDoc.equals(doc));
|
486
|
}
|
487
|
else {
|
488
|
assertTrue(doc.indexOf("<error>") != -1);
|
489
|
}
|
490
|
}
|
491
|
catch (MetacatInaccessibleException mie) {
|
492
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
493
|
}
|
494
|
catch (InsufficientKarmaException ike) {
|
495
|
if (!expectedKarmaFailure) {
|
496
|
fail("Insufficient karma:\n" + ike.getMessage());
|
497
|
}
|
498
|
}
|
499
|
catch (MetacatException me) {
|
500
|
fail("Metacat Error:\n" + me.getMessage());
|
501
|
}
|
502
|
catch (Exception e) {
|
503
|
fail("General exception:\n" + e.getMessage());
|
504
|
}
|
505
|
|
506
|
}
|
507
|
|
508
|
/**
|
509
|
* Create a hopefully unique docid for testing insert and update. Does
|
510
|
* not include the 'revision' part of the id.
|
511
|
*
|
512
|
* @return a String docid based on the current date and time
|
513
|
*/
|
514
|
private String generateDocid() {
|
515
|
StringBuffer docid = new StringBuffer(prefix);
|
516
|
docid.append(".");
|
517
|
|
518
|
// Create a calendar to get the date formatted properly
|
519
|
String[] ids = TimeZone.getAvailableIDs( -8 * 60 * 60 * 1000);
|
520
|
SimpleTimeZone pdt = new SimpleTimeZone( -8 * 60 * 60 * 1000, ids[0]);
|
521
|
pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
|
522
|
pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY,
|
523
|
2 * 60 * 60 * 1000);
|
524
|
Calendar calendar = new GregorianCalendar(pdt);
|
525
|
Date trialTime = new Date();
|
526
|
calendar.setTime(trialTime);
|
527
|
docid.append(calendar.get(Calendar.YEAR));
|
528
|
docid.append(calendar.get(Calendar.DAY_OF_YEAR));
|
529
|
docid.append(calendar.get(Calendar.HOUR_OF_DAY));
|
530
|
docid.append(calendar.get(Calendar.MINUTE));
|
531
|
docid.append(calendar.get(Calendar.SECOND));
|
532
|
//sometimes this number is not unique, so we append a random number
|
533
|
int random = (new Double(Math.random()*100)).intValue();
|
534
|
docid.append(random);
|
535
|
return docid.toString();
|
536
|
}
|
537
|
}
|