Project

General

Profile

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: 2010-12-21 14:26:06 -0800 (Tue, 21 Dec 2010) $'
9
 * '$Revision: 5752 $'
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 NonAsciiCharacterTest
59
    extends MCTestCase {
60

    
61
    private static String metacatUrl;
62
    private static String username;
63
	private static String password;
64
	private static String anotheruser;
65
	private static String anotherpassword;
66
	static {
67
		try {
68
		    metacatUrl = PropertyService.getProperty("test.metacatUrl");
69
			username = PropertyService.getProperty("test.mcUser");
70
			password = PropertyService.getProperty("test.mcPassword");
71
			anotheruser = PropertyService.getProperty("test.mcAnotherUser");
72
			anotherpassword = PropertyService.getProperty("test.mcAnotherPassword");
73
		} catch (PropertyNotFoundException pnfe) {
74
			System.err.println("Could not get property in static block: " 
75
					+ pnfe.getMessage());
76
		}
77
	}
78

    
79
    private String prefix = "test";
80
    private String testdocument = "";
81

    
82
    private Metacat m;
83

    
84
    /**
85
     * These variables are for eml-2.0.1 only. For other eml versions,
86
     * this function might have to modified
87
     */
88

    
89
    private String testEml_201_Header =
90
        "<?xml version=\"1.0\"?><eml:eml" +
91
        " xmlns:eml=\"eml://ecoinformatics.org/eml-2.0.1\"" +
92
        " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
93
        " packageId=\"eml.1.1\" system=\"knb\"" +
94
        " xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.0.1 eml.xsd\"" +
95
        " scope=\"system\">";
96
    
97
    private String testEml_210_Header =
98
        "<?xml version=\"1.0\"?><eml:eml" +
99
        " xmlns:eml=\"eml://ecoinformatics.org/eml-2.1.0\"" +
100
        " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
101
        " packageId=\"eml.1.1\" system=\"knb\"" +
102
        " xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.1.0 eml.xsd\"" +
103
        " scope=\"system\">";
104

    
105
    private String testEmlCreatorBlock =
106
        "<creator scope=\"document\">                                       " +
107
        " <individualName>                                                  " +
108
        "    <surName>Smith</surName>                                       " +
109
        " </individualName>                                                 " +
110
        "</creator>                                                         ";
111

    
112
    private String testEmlContactBlock =
113
        "<contact scope=\"document\">                                       " +
114
        " <individualName>                                                  " +
115
        "    <surName>Jackson</surName>                                     " +
116
        " </individualName>                                                 " +
117
        "</contact>                                                         ";
118

    
119
    /**
120
     * This function returns an access block based on the params passed
121
     */
122
    protected String getAccessBlock(String principal, boolean grantAccess,
123
                                  boolean read, boolean write,
124
                                  boolean changePermission, boolean all) {
125
        String accessBlock = "<access " +
126
            "authSystem=\"ldap://ldap.ecoinformatics.org:389/dc=ecoinformatics,dc=org\"" +
127
            " order=\"allowFirst\" scope=\"document\">";
128

    
129
        if (grantAccess) {
130
            accessBlock += "<allow>";
131
        }
132
        else {
133
            accessBlock += "<deny>";
134
        }
135

    
136
        accessBlock = accessBlock + "<principal>" + principal + "</principal>";
137

    
138
        if (all) {
139
            accessBlock += "<permission>all</permission>";
140
        }
141
        else {
142
            if (read) {
143
                accessBlock += "<permission>read</permission>";
144
            }
145
            if (write) {
146
                accessBlock += "<permission>write</permission>";
147
            }
148
            if (changePermission) {
149
                accessBlock += "<permission>changePermission</permission>";
150
            }
151
        }
152

    
153
        if (grantAccess) {
154
            accessBlock += "</allow>";
155
        }
156
        else {
157
            accessBlock += "</deny>";
158
        }
159
        accessBlock += "</access>";
160

    
161
        return accessBlock;
162

    
163
    }
164

    
165
    /**
166
     * This function returns a valid eml document with no access rules
167
     * This function is for eml-2.0.1 only. For other eml versions,
168
     * this function might have to modified
169
     */
170
    private String getTestEmlDoc(String title, String emlVersion) {
171

    
172
        String testDocument = "";
173

    
174
        String header;
175
		if (emlVersion == EML2_0_1) {
176
			header = testEml_201_Header;
177
		} else if (emlVersion == EML2_1_0) {
178
			header = testEml_210_Header;
179
		} else { // if (emlVersion == EML2_1_1) {
180
			header = testEml_211_Header;
181
		}
182
        
183
        testDocument += header;
184
        
185
        // if this is an EML 2.1.0 or later document, the document level access is
186
        // before the dataset element.
187
        if (emlVersion == EML2_1_0 || emlVersion == EML2_1_1) {
188
        	testDocument += getAccessBlock("public", true, true, false, false, false);
189
        }
190
        testDocument += "<dataset scope=\"document\"><title>" + title + "</title>"
191
				+ testEmlCreatorBlock;
192

    
193
        testDocument += testEmlContactBlock;
194
        
195
        // if this is an EML 2.0.1 or earlier document, the document level access is
196
        // inside the dataset element.
197
        if (emlVersion != EML2_1_0) {
198
        	testDocument += getAccessBlock("public", true, true, false, false, false);
199
        }
200
        testDocument += "</dataset>";
201
        testDocument += "</eml:eml>";
202

    
203
        return testDocument;
204
    }
205
    
206
    /**
207
     * Returns an xml squery that searches for the doc id in the
208
     * title of documents. This function is for eml-2.0.1+ only. For 
209
     * other eml versions, this function might have to modified.
210
     */
211
    private String getTestEmlQuery(String titlePart, String emlVersion) {
212

    
213
    	String docType;
214
    	if (emlVersion.equals(EML2_0_1)) {
215
    		docType = "eml://ecoinformatics.org/eml-2.0.1";
216
    	} else if (emlVersion.equals(EML2_1_0)) {
217
    		docType = "eml://ecoinformatics.org/eml-2.1.0";
218
    	} else { //if (emlVersion.equals(EML2_1_1)) {
219
    		docType = "eml://ecoinformatics.org/eml-2.1.1";
220
    	}
221
    	
222
        String sQuery = "";
223
        sQuery = 
224
        	"<pathquery version=\"1.0\">" +
225
        		"<meta_file_id>unspecified</meta_file_id>" +
226
        		"<querytitle>unspecified</querytitle>" + 
227
        		"<returnfield>dataset/title</returnfield>" +
228
        		"<returndoctype>" + docType + "</returndoctype>" +
229
        		"<querygroup operator=\"UNION\">" +
230
        			"<queryterm casesensitive=\"false\" searchmode=\"contains\">" +
231
        				"<value>" + titlePart + "</value>" +
232
        				"<pathexpr>dataset/title</pathexpr>" +
233
        			"</queryterm>" +
234
        		"</querygroup>" +
235
        	"</pathquery>";
236

    
237
        return sQuery;
238
    }
239

    
240
    /**
241
     * Constructor to build the test
242
     *
243
     * @param name the name of the test method
244
     */
245
    public NonAsciiCharacterTest(String name) {
246
        super(name);
247
    }
248

    
249
    /**
250
     * Establish a testing framework by initializing appropriate objects
251
     */
252
    public void setUp() {
253
        try {
254
            System.err.println("Test Metacat: " + metacatUrl);
255
            m = MetacatFactory.createMetacatConnection(metacatUrl);
256
        }
257
        catch (MetacatInaccessibleException mie) {
258
            System.err.println("Metacat is: " + metacatUrl);
259
            fail("Metacat connection failed." + mie.getMessage());
260
        }
261
    }
262

    
263
    /**
264
     * Release any objects after tests are complete
265
     */
266
    public void tearDown() {
267
    }
268

    
269
    /**
270
     * Create a suite of tests to be run together
271
     */
272
    public static Test suite() {
273
        TestSuite suite = new TestSuite();
274
        suite.addTest(new NonAsciiCharacterTest("initialize"));
275
        // Test basic functions
276
        /*suite.addTest(new NonAsciiCharacterTest("invalidXMLCharacters201Test"));
277
        suite.addTest(new NonAsciiCharacterTest("invalidXMLCharacters210Test"));
278
        suite.addTest(new NonAsciiCharacterTest("symbolEncodedFormat201Test"));
279
        suite.addTest(new NonAsciiCharacterTest("symbolEncodedFormat210Test"));
280
        suite.addTest(new NonAsciiCharacterTest("quote201Test"));
281
        suite.addTest(new NonAsciiCharacterTest("quote210Test"));
282
        suite.addTest(new NonAsciiCharacterTest("numericCharacterReferenceFormat201Test"));
283
        suite.addTest(new NonAsciiCharacterTest("numericCharacterReferenceFormat210Test"));*/
284
        //suite.addTest(new NonAsciiCharacterTest("nonLatinUnicodeCharacter201Test"));
285
        //suite.addTest(new NonAsciiCharacterTest("nonLatinUnicodeCharacter210Test"));
286
        suite.addTest(new NonAsciiCharacterTest("unicodeCharacterTest"));
287

    
288

    
289
        return suite;
290
    }
291

    
292
    /**
293
     * Run an initial test that always passes to check that the test
294
     * harness is working.
295
     */
296
    public void initialize() {
297
        assertTrue(1 == 1);
298
    }
299

    
300
    /**
301
     * Test inserting an EML 2.0.1 document with > & <
302
     * should fail because this means an invalid xml document is being inserted
303
     */
304
    public void invalidXMLCharacters201Test() {
305
    	debug("\nRunning: invalidXMLCharacters201Test");
306
        try {
307
            String newdocid = generateDocid();
308
            m.login(username, password);
309
            testdocument = getTestEmlDoc("Checking > & < in doc: " + newdocid, EML2_0_1);
310
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
311
            m.logout();
312
        }
313
        catch (MetacatAuthException mae) {
314
            fail("Authorization failed:\n" + mae.getMessage());
315
        }
316
        catch (MetacatInaccessibleException mie) {
317
            fail("Metacat Inaccessible:\n" + mie.getMessage());
318
        }
319
        catch (Exception e) {
320
            fail("General exception:\n" + e.getMessage());
321
        }
322
    }
323
    
324
    /**
325
     * Test inserting an EML 2.1.0 document with > & <
326
     * should fail because this means an invalid xml document is being inserted
327
     */
328
    public void invalidXMLCharacters210Test() {
329
    	debug("\nRunning: invalidXMLCharacters210Test");
330
        try {
331
            String newdocid = generateDocid();
332
            m.login(username, password);
333
            testdocument = getTestEmlDoc("Checking > & < in doc: " + newdocid, EML2_1_0);
334
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
335
            m.logout();
336
        }
337
        catch (MetacatAuthException mae) {
338
            fail("Authorization failed:\n" + mae.getMessage());
339
        }
340
        catch (MetacatInaccessibleException mie) {
341
            fail("Metacat Inaccessible:\n" + mie.getMessage());
342
        }
343
        catch (Exception e) {
344
            fail("General exception:\n" + e.getMessage());
345
        }
346
    }
347

    
348
    /**
349
     * Test inserting and reading an EML 2.0.1 document with &gt; &amp; &lt;
350
     * Read should succeed since the same document should be read 
351
     * back from disk that was submitted. Query should succeed as 
352
     * well because the characters in this test are not changed in
353
     * the database.
354
     */
355
    public void symbolEncodedFormat201Test() {
356
    	debug("\nRunning: symbolEncodedFormat201Test");
357
        try {
358
            String newdocid = generateDocid();
359
            m.login(username, password);
360
            
361
            String testTitle = 
362
            	"Checking &gt; &lt; &quot; &apos; &amp; in doc: " + newdocid  + ".1";
363
            testdocument = getTestEmlDoc(testTitle, EML2_0_1);
364
            insertDocid(newdocid  + ".1", testdocument, SUCCESS, false);
365
            
366
            // this tests reading the document back from disk
367
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
368
            
369
            // this tests searching for the document in the database
370
            Thread.sleep(3000);
371
            queryDocWhichHasTitle(newdocid  + ".1", testTitle, EML2_0_1, SUCCESS);
372
            
373
            deleteDocid(newdocid  + ".1", SUCCESS, false);
374
            
375
            m.logout();
376
        }
377
        catch (MetacatAuthException mae) {
378
            fail("Authorization failed:\n" + mae.getMessage());
379
        }
380
        catch (MetacatInaccessibleException mie) {
381
            fail("Metacat Inaccessible:\n" + mie.getMessage());
382
        }
383
        catch (Exception e) {
384
            fail("General exception:\n" + e.getMessage());
385
        }
386
    }
387
    
388
    /**
389
     * Test inserting and reading an EML 2.1.0 document with &gt; &amp; &lt;
390
     * Read should succeed since the same document should be read 
391
     * back from disk that was submitted. Query should succeed as 
392
     * well because the characters in this test are not changed in
393
     * the database.
394
     */
395
    public void symbolEncodedFormat210Test() {
396
    	debug("\nRunning: symbolEncodedFormat210Test");
397
        try {
398
            String newdocid = generateDocid();
399
            m.login(username, password);
400
            
401
            String testTitle = 
402
            	"Checking &gt; &lt; &quot; &apos; &amp; in doc: " + newdocid + ".1";
403
            testdocument = getTestEmlDoc(testTitle, EML2_1_0);
404
            insertDocid(newdocid  + ".1", testdocument, SUCCESS, false);
405
            
406
            // this tests reading the document back from disk
407
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
408
            
409
            // this tests searching for the document in the database
410
            Thread.sleep(3000);
411
            queryDocWhichHasTitle(newdocid  + ".1", testTitle, EML2_1_0, SUCCESS);
412
            
413
            deleteDocid(newdocid  + ".1", SUCCESS, false);
414
            
415
            m.logout();
416
        }
417
        catch (MetacatAuthException mae) {
418
            fail("Authorization failed:\n" + mae.getMessage());
419
        }
420
        catch (MetacatInaccessibleException mie) {
421
            fail("Metacat Inaccessible:\n" + mie.getMessage());
422
        }
423
        catch (Exception e) {
424
            fail("General exception:\n" + e.getMessage());
425
        }
426
    }
427

    
428
    /**
429
     * Test inserting and reading an EML 2.0.1 document with single quote and double quote.
430
     * Read should succeed since the same document should be read back from disk 
431
     * that was submitted.  Query should succeed because we look for the converted
432
     * character (&apos; and &quot;).
433
     */
434
    public void quote201Test() {
435
    	debug("\nRunning: quote201Test");
436
        try {
437
            String newdocid = generateDocid();
438
            m.login(username, password);
439
            
440
            String testTitle = "Checking ' ` \" in doc: " + newdocid  + ".1";
441
            String convertedTestTitle = "Checking &apos; ` &quot; in doc: " + newdocid  + ".1";
442
            
443
            testdocument = getTestEmlDoc(testTitle, EML2_0_1);
444
            insertDocid(newdocid + ".1", testdocument, SUCCESS, true);
445
            
446
            // this tests reading the document back from disk
447
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
448
            
449
            // this tests searching for the document in the database
450
            Thread.sleep(3000);
451
            queryDocWhichHasTitle(newdocid  + ".1", convertedTestTitle, EML2_0_1, SUCCESS);
452
            
453
            deleteDocid(newdocid  + ".1", SUCCESS, false);
454
            
455
            m.logout();
456
        }
457
        catch (MetacatAuthException mae) {
458
            fail("Authorization failed:\n" + mae.getMessage());
459
        }
460
        catch (MetacatInaccessibleException mie) {
461
            fail("Metacat Inaccessible:\n" + mie.getMessage());
462
        }
463
        catch (Exception e) {
464
            fail("General exception:\n" + e.getMessage());
465
        }
466
    }
467
    
468
    /**
469
     * Test inserting and reading an EML 2.1.0 document with single quote and double quote.
470
     * Read should succeed since the same document should be read back from disk 
471
     * that was submitted.  Query shoud succeed because we look for the converted
472
     * character (&apos; and &quot;).
473
     */
474
    public void quote210Test() {
475
    	debug("\nRunning: quote210Test");
476
        try {
477
            String newdocid = generateDocid();
478
            m.login(username, password);
479
            
480
            String testTitle = "Checking ' ` \" in doc: " + newdocid  + ".1";
481
            String convertedTestTitle = "Checking &apos; ` &quot; in doc: " + newdocid  + ".1";
482
            
483
            testdocument = getTestEmlDoc(testTitle, EML2_1_0);
484
            insertDocid(newdocid + ".1", testdocument, SUCCESS, true);
485
            
486
            // this tests reading the document back from disk
487
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
488
            
489
            // this tests searching for the document in the database
490
            Thread.sleep(3000);
491
            queryDocWhichHasTitle(newdocid  + ".1", convertedTestTitle, EML2_1_0, SUCCESS);
492
            
493
            deleteDocid(newdocid  + ".1", SUCCESS, false);
494
            
495
            m.logout();
496
        }
497
        catch (MetacatAuthException mae) {
498
            fail("Authorization failed:\n" + mae.getMessage());
499
        }
500
        catch (MetacatInaccessibleException mie) {
501
            fail("Metacat Inaccessible:\n" + mie.getMessage());
502
        }
503
        catch (Exception e) {
504
            fail("General exception:\n" + e.getMessage());
505
        }
506
    }
507

    
508
    /**
509
     * Test inserting and reading an EML 2.0.1 document with the code representation 
510
     * of a micro sign (&#181). Read should succeed since the same document should be 
511
     * read back from disk that was submitted.  Query should succeed because we look 
512
     * for the converted character (µ).
513
     */
514
    public void numericCharacterReferenceFormat201Test() {
515
    	debug("\nRunning: numericCharacterReferenceFormat201Test");
516
        try {
517
            String newdocid = generateDocid();
518
            m.login(username, password);
519
            
520
            String testTitle = "Checking &#181; in doc: " + newdocid  + ".1";
521
            String convertedTestTitle = "Checking µ in doc: " + newdocid  + ".1";
522
            
523
            testdocument = getTestEmlDoc(testTitle, EML2_0_1);
524
            insertDocid(newdocid + ".1", testdocument, SUCCESS, true);
525

    
526
            // this tests reading the document back from disk
527
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
528
            
529
            // this tests searching for the document in the database
530
            Thread.sleep(3000);
531
            queryDocWhichHasTitle(newdocid  + ".1", convertedTestTitle, EML2_0_1, SUCCESS);
532
            
533
            deleteDocid(newdocid  + ".1", SUCCESS, false);
534
  
535
            m.logout();
536
        }
537
        catch (MetacatAuthException mae) {
538
            fail("Authorization failed:\n" + mae.getMessage());
539
        }
540
        catch (MetacatInaccessibleException mie) {
541
            fail("Metacat Inaccessible:\n" + mie.getMessage());
542
        }
543
        catch (Exception e) {
544
            fail("General exception:\n" + e.getMessage());
545
        }
546
    }
547

    
548
    /**
549
     * Test inserting and reading an EML 2.1.0 document with the code representation 
550
     * of a micro sign (&#181). Read should succeed since the same document should be 
551
     * read back from disk that was submitted.  Query should succeed because we look 
552
     * for the converted character (µ).
553
     */
554
    public void numericCharacterReferenceFormat210Test() {
555
    	debug("\nRunning: numericCharacterReferenceFormat210Test");
556
        try {
557
            String newdocid = generateDocid();
558
            m.login(username, password);
559
            
560
            String testTitle = "Checking &#181; in doc: " + newdocid  + ".1";
561
            String convertedTestTitle = "Checking µ in doc: " + newdocid  + ".1";
562
            
563
            testdocument = getTestEmlDoc(testTitle, EML2_1_0);
564
            insertDocid(newdocid + ".1", testdocument, SUCCESS, true);
565
            
566
            // this tests reading the document back from disk
567
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
568
            
569
            // this tests searching for the document in the database
570
            Thread.sleep(3000);
571
            queryDocWhichHasTitle(newdocid  + ".1", convertedTestTitle, EML2_1_0, SUCCESS);
572
            
573
            deleteDocid(newdocid  + ".1", SUCCESS, false);
574
            
575
            m.logout();
576
        }
577
        catch (MetacatAuthException mae) {
578
            fail("Authorization failed:\n" + mae.getMessage());
579
        }
580
        catch (MetacatInaccessibleException mie) {
581
            fail("Metacat Inaccessible:\n" + mie.getMessage());
582
        }
583
        catch (Exception e) {
584
            fail("General exception:\n" + e.getMessage());
585
        }
586
    }
587

    
588
    /**
589
     * Test inserting and reading an EML 2.0.1 document with the micro sign (µ). 
590
     * Read should succeed since the same document should be read back from disk 
591
     * that was submitted.  Query should succeed because we look for the same 
592
     * character (µ).
593
     */
594
    public void nonLatinUnicodeCharacter201Test() {
595
    	debug("\nRunning: nonLatinUnicodeCharacter201Test");
596
        try {
597
            String newdocid = generateDocid();
598
            m.login(username, password);
599
            
600
            String testTitle = "Checking characters like µ in doc: " + newdocid  + ".1";
601
            
602
            testdocument = getTestEmlDoc(testTitle, EML2_0_1);
603
            
604
            debug("original test document:	" + testdocument);
605
            
606
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
607
            
608
            // this tests reading the document back from disk
609
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
610
            
611
            // this tests searching for the document in the database
612
            Thread.sleep(3000);
613
            queryDocWhichHasTitle(newdocid  + ".1", testTitle, EML2_0_1, SUCCESS);
614
            
615
            //deleteDocid(newdocid  + ".1", SUCCESS, false);
616
            
617
            m.logout();
618
        }
619
        catch (MetacatAuthException mae) {
620
            fail("Authorization failed:\n" + mae.getMessage());
621
        }
622
        catch (MetacatInaccessibleException mie) {
623
            fail("Metacat Inaccessible:\n" + mie.getMessage());
624
        }
625
        catch (Exception e) {
626
            fail("General exception:\n" + e.getMessage());
627
        }
628
    }
629
    
630
    /**
631
     * Test inserting and reading an EML 2.1.0 document with the micro sign (µ). 
632
     * Read should succeed since the same document should be read back from disk 
633
     * that was submitted.  Query should succeed because we look for the same 
634
     * character (µ).
635
     */
636
    public void nonLatinUnicodeCharacter210Test() {
637
    	debug("\nRunning: nonLatinUnicodeCharacter210Test");
638
        try {
639
            String newdocid = generateDocid();
640
            m.login(username, password);
641
            
642
            String testTitle = "Checking characters like µ in doc: " + newdocid  + ".1";
643
            
644
            testdocument = getTestEmlDoc(testTitle, EML2_1_0);
645
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
646

    
647
            // this tests reading the document back from disk
648
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
649
            
650
            // this tests searching for the document in the database
651
            Thread.sleep(3000);
652
            queryDocWhichHasTitle(newdocid  + ".1", testTitle, EML2_1_0, SUCCESS);
653
            
654
            deleteDocid(newdocid  + ".1", SUCCESS, false);
655
            
656
            m.logout();
657
        }
658
        catch (MetacatAuthException mae) {
659
            fail("Authorization failed:\n" + mae.getMessage());
660
        }
661
        catch (MetacatInaccessibleException mie) {
662
            fail("Metacat Inaccessible:\n" + mie.getMessage());
663
        }
664
        catch (Exception e) {
665
            fail("General exception:\n" + e.getMessage());
666
        }
667
    }
668
    
669
    /**
670
     * Test inserting and reading an EML 2.1.0 with Chinese
671
     */
672
    public void unicodeCharacterTest() {
673
    	debug("\nRunning: unicodeCharacterTest");
674
        try {
675
            
676
        	String filePath = "test/clienttestfiles/unicodeEML.xml";
677
            String testTitle = "測試中的數據包 (Test Chinese data package) _DOCID_";
678
            String newdocid = generateDocid() + ".1";
679
            testdocument = FileUtil.readFileToString(filePath, "UTF-8");
680
            
681
            // include the docid
682
            testdocument = testdocument.replaceAll("_DOCID_", newdocid);
683
            testTitle = testTitle.replaceAll("_DOCID_", newdocid);
684
            
685
            // login
686
            m.login(username, password);
687
            
688
            // insert
689
            insertDocid(newdocid, testdocument, SUCCESS, false);
690

    
691
            // this tests reading the document back from disk
692
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
693
            
694
            // this tests searching for the document in the database
695
            Thread.sleep(3000);
696
            queryDocWhichHasTitle(testTitle, testTitle, EML2_1_0, SUCCESS);
697
            
698
            // clean up
699
            //deleteDocid(newdocid, SUCCESS, false);
700
            
701
            m.logout();
702
        }
703
        catch (MetacatAuthException mae) {
704
            fail("Authorization failed:\n" + mae.getMessage());
705
        }
706
        catch (MetacatInaccessibleException mie) {
707
            fail("Metacat Inaccessible:\n" + mie.getMessage());
708
        }
709
        catch (Exception e) {
710
            fail("General exception:\n" + e.getMessage());
711
        }
712
    }
713

    
714
    /**
715
     * Insert a document into metacat. The expected result is passed as result
716
     */
717
    private String insertDocid(String docid, String docText, boolean result,
718
                               boolean expectMetacatException) {
719
        String response = null;
720
        try {
721
        	
722
        	debug("doctext: " + docText);
723
        	
724
            response = m.insert(docid,
725
                                new StringReader(docText), null);
726
            System.err.println(response);
727
            if (result) {
728
                assertTrue( (response.indexOf("<success>") != -1));
729
                assertTrue(response.indexOf(docid) != -1);
730
            }
731
            else {
732
                assertTrue( (response.indexOf("<success>") == -1));
733
            }
734
        }
735
        catch (MetacatInaccessibleException mie) {
736
            fail("Metacat Inaccessible:\n" + mie.getMessage());
737
        }
738
        catch (InsufficientKarmaException ike) {
739
                fail("Insufficient karma:\n" + ike.getMessage());
740
        }
741
        catch (MetacatException me) {
742
            if (!expectMetacatException) {
743
                fail("Metacat Error:\n" + me.getMessage());
744
            }
745
        }
746
        catch (Exception e) {
747
            fail("General exception:\n" + e.getMessage());
748
        }
749
        return response;
750
    }
751

    
752
    /**
753
     * Insert a document into metacat. The expected result is passed as result
754
     */
755
    private String uploadDocid(String docid, String filePath, boolean result,
756
                               boolean expectedKarmaException) {
757
        String response = null;
758
        try {
759
            response = m.upload(docid, new File(filePath));
760
            if (result) {
761
                assertTrue( (response.indexOf("<success>") != -1));
762
                assertTrue(response.indexOf(docid) != -1);
763
            }
764
            else {
765
                assertTrue( (response.indexOf("<success>") == -1));
766
            }
767
            System.err.println("respose from metacat: " + response);
768
        }
769
        catch (MetacatInaccessibleException mie) {
770
            fail("Metacat Inaccessible:\n" + mie.getMessage());
771
        }
772
        catch (InsufficientKarmaException ike) {
773
            if (!expectedKarmaException) {
774
                fail("Insufficient karma:\n" + ike.getMessage());
775
            }
776
        }
777
        catch (MetacatException me) {
778
            if (result) {
779
                fail("Metacat Error:\n" + me.getMessage());
780
            } else {
781
                System.err.println("Metacat Error:\n" + me.getMessage());
782
            }
783
        }
784
        catch (Exception e) {
785
            fail("General exception:\n" + e.getMessage());
786
        }
787
        return response;
788
    }
789

    
790
    /**
791
     * Update a document in metacat. The expected result is passed as result
792
     */
793
    private String updateDocid(String docid, String docText, boolean result,
794
                               boolean expectedKarmaFailure) {
795
        String response = null;
796
        try {
797
            response = m.update(docid,
798
                                new StringReader(testdocument), null);
799

    
800
            if (result) {
801
                assertTrue( (response.indexOf("<success>") != -1));
802
                assertTrue(response.indexOf(docid) != -1);
803
            }
804
            else {
805
                assertTrue( (response.indexOf("<success>") == -1));
806
            }
807
            System.err.println(response);
808
        }
809
        catch (MetacatInaccessibleException mie) {
810
            fail("Metacat Inaccessible:\n" + mie.getMessage());
811
        }
812
        catch (InsufficientKarmaException ike) {
813
            if (!expectedKarmaFailure) {
814
                fail("Insufficient karma:\n" + ike.getMessage());
815
            }
816
        }
817
        catch (MetacatException me) {
818
            if (result) {
819
                fail("Metacat Error:\n" + me.getMessage());
820
            } else {
821
                System.err.println("Metacat Error:\n" + me.getMessage());
822
            }
823
        }
824
        catch (Exception e) {
825
            fail("General exception:\n" + e.getMessage());
826
        }
827

    
828
        return response;
829
    }
830

    
831
    /**
832
     * Delete a document from metacat. The expected result is passed as result
833
     */
834
    private void deleteDocid(String docid, boolean result,
835
                             boolean expextedKarmaFailure) {
836
        try {
837
            String response = m.delete(docid);
838
            if (result) {
839
                assertTrue(response.indexOf("<success>") != -1);
840
            }
841
            else {
842
                assertTrue(response.indexOf("<success>") == -1);
843
            }
844
            System.err.println(response);
845
        }
846
        catch (MetacatInaccessibleException mie) {
847
            fail("Metacat Inaccessible:\n" + mie.getMessage());
848
        }
849
        catch (InsufficientKarmaException ike) {
850
            if(!expextedKarmaFailure){
851
                fail("Insufficient karma:\n" + ike.getMessage());
852
            }
853
        }
854
        catch (MetacatException me) {
855
            if (result) {
856
                fail("Metacat Error:\n" + me.getMessage());
857
            } else {
858
                System.err.println("Metacat Error:\n" + me.getMessage());
859
            }
860
        }
861
        catch (Exception e) {
862
            fail("General exception:\n" + e.getMessage());
863
        }
864
    }
865

    
866
    /**
867
     * Read a document from metacat. The expected result is passed as result
868
     */
869
    private void readDocid(String docid, boolean result,
870
                           boolean expextedKarmaFailure) {
871
        try {
872
            Reader r = new InputStreamReader(m.read(docid));
873
            String response = IOUtil.getAsString(r, true);
874

    
875
            if (!result) {
876
                assertTrue(response.indexOf("<success>") == -1);
877
            }
878
            // System.err.println(response);
879
        }
880
        catch (MetacatInaccessibleException mie) {
881
            fail("Metacat Inaccessible:\n" + mie.getMessage());
882
        }
883
        catch (InsufficientKarmaException ike) {
884
            if (!expextedKarmaFailure) {
885
                fail("Insufficient karma:\n" + ike.getMessage());
886
            }
887
        }
888
        catch (MetacatException me) {
889
            fail("Metacat Error:\n" + me.getMessage());
890
        }
891
        catch (Exception e) {
892
            fail("General exception:\n" + e.getMessage());
893
        }
894
    }
895

    
896
    /**
897
     * Read a document from metacat and check if it is equal to a given string.
898
     * The expected result is passed as result
899
     */
900
    private void readDocidWhichEqualsDoc(String docid, String testDoc,
901
                                         boolean result,
902
                                         boolean expectedKarmaFailure) {
903
        try {
904
            Reader r = new InputStreamReader(m.read(docid), "UTF-8");
905
            //InputStream is = m.read(docid);
906
            String doc = IOUtil.getAsString(r, true);
907
            //String doc = IOUtils.toString(is);
908
            
909
            if (result) {
910

    
911
                if (!testDoc.equals(doc)) {
912
                    debug("doc    :" + doc);
913
                    debug("testDoc:" + testDoc);
914
                }
915

    
916
                assertTrue(testDoc.equals(doc));
917
            }
918
            else {
919
                assertTrue(doc.indexOf("<error>") != -1);
920
            }
921
        }
922
        catch (MetacatInaccessibleException mie) {
923
            fail("Metacat Inaccessible:\n" + mie.getMessage());
924
        }
925
        catch (InsufficientKarmaException ike) {
926
            if (!expectedKarmaFailure) {
927
                fail("Insufficient karma:\n" + ike.getMessage());
928
            }
929
        }
930
        catch (MetacatException me) {
931
            fail("Metacat Error:\n" + me.getMessage());
932
        }
933
        catch (Exception e) {
934
            fail("General exception:\n" + e.getMessage());
935
        }
936

    
937
    }
938
    
939
    /**
940
     * Query a document by looking for a part of the title in the title element.
941
     * Then check if the testTitle exists in the doc.
942
     * @param titlePart the part of the title of the doc to look for
943
     * @param testTitle the title containing special characters
944
     * @param result are we expecting SUCCESS or FAILURE
945
     * @param expextedKarmaFailure
946
     */
947
    private void queryDocWhichHasTitle(String titlePart, String testTitle,
948
                                         String emlVersion, boolean result) {
949
        try {
950
            String sQuery = getTestEmlQuery(titlePart, emlVersion);
951
        	Reader queryReader = new StringReader(sQuery);
952
            Reader resultReader = m.query(queryReader);
953
            String queryResult = IOUtil.getAsString(resultReader, true);
954
            if (result) {
955
                if (!queryResult.contains(testTitle)) {
956
                    debug("queryResult: " + queryResult);
957
                    debug("does not contain title: " + testTitle);
958
                }
959

    
960
                assertTrue(queryResult.contains(testTitle));
961
            }
962
            else {
963
                assertTrue(queryResult.indexOf("<error>") != -1);
964
            }
965
        }
966
        catch (MetacatInaccessibleException mie) {
967
            fail("Metacat Inaccessible:\n" + mie.getMessage());
968
        }
969
        catch (Exception e) {
970
            fail("General exception:\n" + e.getMessage());
971
        }
972

    
973
    }
974

    
975
    /**
976
     * Create a hopefully unique docid for testing insert and update. Does
977
     * not include the 'revision' part of the id.
978
     *
979
     * @return a String docid based on the current date and time
980
     */
981
    private String generateDocid() {
982
        StringBuffer docid = new StringBuffer(prefix);
983
        docid.append(".");
984

    
985
        // Create a calendar to get the date formatted properly
986
        String[] ids = TimeZone.getAvailableIDs( -8 * 60 * 60 * 1000);
987
        SimpleTimeZone pdt = new SimpleTimeZone( -8 * 60 * 60 * 1000, ids[0]);
988
        pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
989
        pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY,
990
                       2 * 60 * 60 * 1000);
991
        Calendar calendar = new GregorianCalendar(pdt);
992
        Date trialTime = new Date();
993
        calendar.setTime(trialTime);
994
        docid.append(calendar.get(Calendar.YEAR));
995
        docid.append(calendar.get(Calendar.DAY_OF_YEAR));
996
        docid.append(calendar.get(Calendar.HOUR_OF_DAY));
997
        docid.append(calendar.get(Calendar.MINUTE));
998
        docid.append(calendar.get(Calendar.SECOND));
999
   	    //sometimes this number is not unique, so we append a random number
1000
    	int random = (new Double(Math.random()*100)).intValue();
1001
    	docid.append(random);
1002
        return docid.toString();
1003
    }
1004
}
(11-11/23)