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: berkley $'
8
 *     '$Date: 2010-12-20 14:19:13 -0800 (Mon, 20 Dec 2010) $'
9
 * '$Revision: 5750 $'
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.IOUtil;
47
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
48
import junit.framework.Test;
49
import junit.framework.TestSuite;
50
import java.io.File;
51

    
52
import org.apache.commons.io.IOUtils;
53

    
54
/**
55
 * A JUnit test for testing Metacat when Non Ascii Characters are inserted
56
 */
57
public class NonAsciiCharacterTest
58
    extends MCTestCase {
59

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

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

    
81
    private Metacat m;
82

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

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

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

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

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

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

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

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

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

    
160
        return accessBlock;
161

    
162
    }
163

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

    
171
        String testDocument = "";
172

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

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

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

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

    
236
        return sQuery;
237
    }
238

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

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

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

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

    
286
        return suite;
287
    }
288

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

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

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

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

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

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

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

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

    
641
            // this tests reading the document back from disk
642
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
643
            
644
            // this tests searching for the document in the database
645
            Thread.sleep(3000);
646
            queryDocWhichHasTitle(newdocid  + ".1", testTitle, EML2_1_0, SUCCESS);
647
            
648
            deleteDocid(newdocid  + ".1", SUCCESS, false);
649
            
650
            m.logout();
651
        }
652
        catch (MetacatAuthException mae) {
653
            fail("Authorization failed:\n" + mae.getMessage());
654
        }
655
        catch (MetacatInaccessibleException mie) {
656
            fail("Metacat Inaccessible:\n" + mie.getMessage());
657
        }
658
        catch (Exception e) {
659
            fail("General exception:\n" + e.getMessage());
660
        }
661
    }
662

    
663
    /**
664
     * Insert a document into metacat. The expected result is passed as result
665
     */
666
    private String insertDocid(String docid, String docText, boolean result,
667
                               boolean expectMetacatException) {
668
        String response = null;
669
        try {
670
            response = m.insert(docid,
671
                                new StringReader(docText), null);
672
            System.err.println(response);
673
            if (result) {
674
                assertTrue( (response.indexOf("<success>") != -1));
675
                assertTrue(response.indexOf(docid) != -1);
676
            }
677
            else {
678
                assertTrue( (response.indexOf("<success>") == -1));
679
            }
680
        }
681
        catch (MetacatInaccessibleException mie) {
682
            fail("Metacat Inaccessible:\n" + mie.getMessage());
683
        }
684
        catch (InsufficientKarmaException ike) {
685
                fail("Insufficient karma:\n" + ike.getMessage());
686
        }
687
        catch (MetacatException me) {
688
            if (!expectMetacatException) {
689
                fail("Metacat Error:\n" + me.getMessage());
690
            }
691
        }
692
        catch (Exception e) {
693
            fail("General exception:\n" + e.getMessage());
694
        }
695
        return response;
696
    }
697

    
698
    /**
699
     * Insert a document into metacat. The expected result is passed as result
700
     */
701
    private String uploadDocid(String docid, String filePath, boolean result,
702
                               boolean expectedKarmaException) {
703
        String response = null;
704
        try {
705
            response = m.upload(docid, new File(filePath));
706
            if (result) {
707
                assertTrue( (response.indexOf("<success>") != -1));
708
                assertTrue(response.indexOf(docid) != -1);
709
            }
710
            else {
711
                assertTrue( (response.indexOf("<success>") == -1));
712
            }
713
            System.err.println("respose from metacat: " + response);
714
        }
715
        catch (MetacatInaccessibleException mie) {
716
            fail("Metacat Inaccessible:\n" + mie.getMessage());
717
        }
718
        catch (InsufficientKarmaException ike) {
719
            if (!expectedKarmaException) {
720
                fail("Insufficient karma:\n" + ike.getMessage());
721
            }
722
        }
723
        catch (MetacatException me) {
724
            if (result) {
725
                fail("Metacat Error:\n" + me.getMessage());
726
            } else {
727
                System.err.println("Metacat Error:\n" + me.getMessage());
728
            }
729
        }
730
        catch (Exception e) {
731
            fail("General exception:\n" + e.getMessage());
732
        }
733
        return response;
734
    }
735

    
736
    /**
737
     * Update a document in metacat. The expected result is passed as result
738
     */
739
    private String updateDocid(String docid, String docText, boolean result,
740
                               boolean expectedKarmaFailure) {
741
        String response = null;
742
        try {
743
            response = m.update(docid,
744
                                new StringReader(testdocument), null);
745

    
746
            if (result) {
747
                assertTrue( (response.indexOf("<success>") != -1));
748
                assertTrue(response.indexOf(docid) != -1);
749
            }
750
            else {
751
                assertTrue( (response.indexOf("<success>") == -1));
752
            }
753
            System.err.println(response);
754
        }
755
        catch (MetacatInaccessibleException mie) {
756
            fail("Metacat Inaccessible:\n" + mie.getMessage());
757
        }
758
        catch (InsufficientKarmaException ike) {
759
            if (!expectedKarmaFailure) {
760
                fail("Insufficient karma:\n" + ike.getMessage());
761
            }
762
        }
763
        catch (MetacatException me) {
764
            if (result) {
765
                fail("Metacat Error:\n" + me.getMessage());
766
            } else {
767
                System.err.println("Metacat Error:\n" + me.getMessage());
768
            }
769
        }
770
        catch (Exception e) {
771
            fail("General exception:\n" + e.getMessage());
772
        }
773

    
774
        return response;
775
    }
776

    
777
    /**
778
     * Delete a document from metacat. The expected result is passed as result
779
     */
780
    private void deleteDocid(String docid, boolean result,
781
                             boolean expextedKarmaFailure) {
782
        try {
783
            String response = m.delete(docid);
784
            if (result) {
785
                assertTrue(response.indexOf("<success>") != -1);
786
            }
787
            else {
788
                assertTrue(response.indexOf("<success>") == -1);
789
            }
790
            System.err.println(response);
791
        }
792
        catch (MetacatInaccessibleException mie) {
793
            fail("Metacat Inaccessible:\n" + mie.getMessage());
794
        }
795
        catch (InsufficientKarmaException ike) {
796
            if(!expextedKarmaFailure){
797
                fail("Insufficient karma:\n" + ike.getMessage());
798
            }
799
        }
800
        catch (MetacatException me) {
801
            if (result) {
802
                fail("Metacat Error:\n" + me.getMessage());
803
            } else {
804
                System.err.println("Metacat Error:\n" + me.getMessage());
805
            }
806
        }
807
        catch (Exception e) {
808
            fail("General exception:\n" + e.getMessage());
809
        }
810
    }
811

    
812
    /**
813
     * Read a document from metacat. The expected result is passed as result
814
     */
815
    private void readDocid(String docid, boolean result,
816
                           boolean expextedKarmaFailure) {
817
        try {
818
            Reader r = new InputStreamReader(m.read(docid));
819
            String response = IOUtil.getAsString(r, true);
820

    
821
            if (!result) {
822
                assertTrue(response.indexOf("<success>") == -1);
823
            }
824
            // System.err.println(response);
825
        }
826
        catch (MetacatInaccessibleException mie) {
827
            fail("Metacat Inaccessible:\n" + mie.getMessage());
828
        }
829
        catch (InsufficientKarmaException ike) {
830
            if (!expextedKarmaFailure) {
831
                fail("Insufficient karma:\n" + ike.getMessage());
832
            }
833
        }
834
        catch (MetacatException me) {
835
            fail("Metacat Error:\n" + me.getMessage());
836
        }
837
        catch (Exception e) {
838
            fail("General exception:\n" + e.getMessage());
839
        }
840
    }
841

    
842
    /**
843
     * Read a document from metacat and check if it is equal to a given string.
844
     * The expected result is passed as result
845
     */
846
    private void readDocidWhichEqualsDoc(String docid, String testDoc,
847
                                         boolean result,
848
                                         boolean expectedKarmaFailure) {
849
        try {
850
            Reader r = new InputStreamReader(m.read(docid), "UTF-8");
851
            //InputStream is = m.read(docid);
852
            String doc = IOUtil.getAsString(r, true);
853
            //String doc = IOUtils.toString(is);
854
            
855
            if (result) {
856

    
857
                if (!testDoc.equals(doc)) {
858
                    debug("doc    :" + doc);
859
                    debug("testDoc:" + testDoc);
860
                }
861

    
862
                assertTrue(testDoc.equals(doc));
863
            }
864
            else {
865
                assertTrue(doc.indexOf("<error>") != -1);
866
            }
867
        }
868
        catch (MetacatInaccessibleException mie) {
869
            fail("Metacat Inaccessible:\n" + mie.getMessage());
870
        }
871
        catch (InsufficientKarmaException ike) {
872
            if (!expectedKarmaFailure) {
873
                fail("Insufficient karma:\n" + ike.getMessage());
874
            }
875
        }
876
        catch (MetacatException me) {
877
            fail("Metacat Error:\n" + me.getMessage());
878
        }
879
        catch (Exception e) {
880
            fail("General exception:\n" + e.getMessage());
881
        }
882

    
883
    }
884
    
885
    /**
886
     * Query a document by looking for it's doc id in the title element.
887
     * Then check if the testTitle exists in the doc.
888
     * @param docId the id of the doc to look for
889
     * @param testTitle the title containing special characters
890
     * @param result are we expecting SUCCESS or FAILURE
891
     * @param expextedKarmaFailure
892
     */
893
    private void queryDocWhichHasTitle(String docId, String testTitle,
894
                                         String emlVersion, boolean result) {
895
        try {
896
            String sQuery = getTestEmlQuery(docId, emlVersion);
897
        	Reader queryReader = new StringReader(sQuery);
898
            Reader resultReader = m.query(queryReader);
899
            String queryResult = IOUtil.getAsString(resultReader, true);
900
            if (result) {
901
                if (!queryResult.contains(testTitle)) {
902
                    debug("queryResult: " + queryResult);
903
                    debug("does not contain title: " + testTitle);
904
                }
905

    
906
                assertTrue(queryResult.contains(testTitle));
907
            }
908
            else {
909
                assertTrue(queryResult.indexOf("<error>") != -1);
910
            }
911
        }
912
        catch (MetacatInaccessibleException mie) {
913
            fail("Metacat Inaccessible:\n" + mie.getMessage());
914
        }
915
        catch (Exception e) {
916
            fail("General exception:\n" + e.getMessage());
917
        }
918

    
919
    }
920

    
921
    /**
922
     * Create a hopefully unique docid for testing insert and update. Does
923
     * not include the 'revision' part of the id.
924
     *
925
     * @return a String docid based on the current date and time
926
     */
927
    private String generateDocid() {
928
        StringBuffer docid = new StringBuffer(prefix);
929
        docid.append(".");
930

    
931
        // Create a calendar to get the date formatted properly
932
        String[] ids = TimeZone.getAvailableIDs( -8 * 60 * 60 * 1000);
933
        SimpleTimeZone pdt = new SimpleTimeZone( -8 * 60 * 60 * 1000, ids[0]);
934
        pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
935
        pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY,
936
                       2 * 60 * 60 * 1000);
937
        Calendar calendar = new GregorianCalendar(pdt);
938
        Date trialTime = new Date();
939
        calendar.setTime(trialTime);
940
        docid.append(calendar.get(Calendar.YEAR));
941
        docid.append(calendar.get(Calendar.DAY_OF_YEAR));
942
        docid.append(calendar.get(Calendar.HOUR_OF_DAY));
943
        docid.append(calendar.get(Calendar.MINUTE));
944
        docid.append(calendar.get(Calendar.SECOND));
945
   	    //sometimes this number is not unique, so we append a random number
946
    	int random = (new Double(Math.random()*100)).intValue();
947
    	docid.append(random);
948
        return docid.toString();
949
    }
950
}
(11-11/23)