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: daigle $'
8
 *     '$Date: 2009-11-06 14:46:33 -0800 (Fri, 06 Nov 2009) $'
9
 * '$Revision: 5110 $'
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.InputStreamReader;
29
import java.io.Reader;
30
import java.io.StringReader;
31
import java.util.Calendar;
32
import java.util.Date;
33
import java.util.GregorianCalendar;
34
import java.util.SimpleTimeZone;
35
import java.util.TimeZone;
36

    
37
import edu.ucsb.nceas.MCTestCase;
38
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
39
import edu.ucsb.nceas.metacat.client.Metacat;
40
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
41
import edu.ucsb.nceas.metacat.client.MetacatException;
42
import edu.ucsb.nceas.metacat.client.MetacatFactory;
43
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
44
import edu.ucsb.nceas.metacat.properties.PropertyService;
45
import edu.ucsb.nceas.utilities.IOUtil;
46
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
47
import junit.framework.Test;
48
import junit.framework.TestSuite;
49
import java.io.File;
50

    
51
/**
52
 * A JUnit test for testing Metacat when Non Ascii Characters are inserted
53
 */
54
public class NonAsciiCharacterTest
55
    extends MCTestCase {
56

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

    
75
    private String prefix = "test";
76
    private String testdocument = "";
77

    
78
    private Metacat m;
79

    
80
    /**
81
     * These variables are for eml-2.0.1 only. For other eml versions,
82
     * this function might have to modified
83
     */
84

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

    
101
    private String testEmlCreatorBlock =
102
        "<creator scope=\"document\">                                       " +
103
        " <individualName>                                                  " +
104
        "    <surName>Smith</surName>                                       " +
105
        " </individualName>                                                 " +
106
        "</creator>                                                         ";
107

    
108
    private String testEmlContactBlock =
109
        "<contact scope=\"document\">                                       " +
110
        " <individualName>                                                  " +
111
        "    <surName>Jackson</surName>                                     " +
112
        " </individualName>                                                 " +
113
        "</contact>                                                         ";
114

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

    
125
        if (grantAccess) {
126
            accessBlock += "<allow>";
127
        }
128
        else {
129
            accessBlock += "<deny>";
130
        }
131

    
132
        accessBlock = accessBlock + "<principal>" + principal + "</principal>";
133

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

    
149
        if (grantAccess) {
150
            accessBlock += "</allow>";
151
        }
152
        else {
153
            accessBlock += "</deny>";
154
        }
155
        accessBlock += "</access>";
156

    
157
        return accessBlock;
158

    
159
    }
160

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

    
168
        String testDocument = "";
169

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

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

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

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

    
229
        return sQuery;
230
    }
231

    
232
    /**
233
     * Constructor to build the test
234
     *
235
     * @param name the name of the test method
236
     */
237
    public NonAsciiCharacterTest(String name) {
238
        super(name);
239
    }
240

    
241
    /**
242
     * Establish a testing framework by initializing appropriate objects
243
     */
244
    public void setUp() {
245
        try {
246
            System.err.println("Test Metacat: " + metacatUrl);
247
            m = MetacatFactory.createMetacatConnection(metacatUrl);
248
        }
249
        catch (MetacatInaccessibleException mie) {
250
            System.err.println("Metacat is: " + metacatUrl);
251
            fail("Metacat connection failed." + mie.getMessage());
252
        }
253
    }
254

    
255
    /**
256
     * Release any objects after tests are complete
257
     */
258
    public void tearDown() {
259
    }
260

    
261
    /**
262
     * Create a suite of tests to be run together
263
     */
264
    public static Test suite() {
265
        TestSuite suite = new TestSuite();
266
        suite.addTest(new NonAsciiCharacterTest("initialize"));
267
        // Test basic functions
268
        suite.addTest(new NonAsciiCharacterTest("invalidXMLCharacters201Test"));
269
        suite.addTest(new NonAsciiCharacterTest("invalidXMLCharacters210Test"));
270
        suite.addTest(new NonAsciiCharacterTest("symbolEncodedFormat201Test"));
271
        suite.addTest(new NonAsciiCharacterTest("symbolEncodedFormat210Test"));
272
        suite.addTest(new NonAsciiCharacterTest("quote201Test"));
273
        suite.addTest(new NonAsciiCharacterTest("quote210Test"));
274
        suite.addTest(new NonAsciiCharacterTest("numericCharacterReferenceFormat201Test"));
275
        suite.addTest(new NonAsciiCharacterTest("numericCharacterReferenceFormat210Test"));
276
        suite.addTest(new NonAsciiCharacterTest("nonLatinUnicodeCharacter201Test"));
277
        suite.addTest(new NonAsciiCharacterTest("nonLatinUnicodeCharacter210Test"));
278

    
279
        return suite;
280
    }
281

    
282
    /**
283
     * Run an initial test that always passes to check that the test
284
     * harness is working.
285
     */
286
    public void initialize() {
287
        assertTrue(1 == 1);
288
    }
289

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

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

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

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

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

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

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

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

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

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

    
729
    /**
730
     * Update a document in metacat. The expected result is passed as result
731
     */
732
    private String updateDocid(String docid, String docText, boolean result,
733
                               boolean expectedKarmaFailure) {
734
        String response = null;
735
        try {
736
            response = m.update(docid,
737
                                new StringReader(testdocument), null);
738

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

    
767
        return response;
768
    }
769

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

    
805
    /**
806
     * Read a document from metacat. The expected result is passed as result
807
     */
808
    private void readDocid(String docid, boolean result,
809
                           boolean expextedKarmaFailure) {
810
        try {
811
            Reader r = new InputStreamReader(m.read(docid));
812
            String response = IOUtil.getAsString(r, true);
813

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

    
835
    /**
836
     * Read a document from metacat and check if it is equal to a given string.
837
     * The expected result is passed as result
838
     */
839
    private void readDocidWhichEqualsDoc(String docid, String testDoc,
840
                                         boolean result,
841
                                         boolean expectedKarmaFailure) {
842
        try {
843
            Reader r = new InputStreamReader(m.read(docid));
844
            String doc = IOUtil.getAsString(r, true);
845
            if (result) {
846

    
847
                if (!testDoc.equals(doc)) {
848
                    debug("doc    :" + doc);
849
                    debug("testDoc:" + testDoc);
850
                }
851

    
852
                assertTrue(testDoc.equals(doc));
853
            }
854
            else {
855
                assertTrue(doc.indexOf("<error>") != -1);
856
            }
857
        }
858
        catch (MetacatInaccessibleException mie) {
859
            fail("Metacat Inaccessible:\n" + mie.getMessage());
860
        }
861
        catch (InsufficientKarmaException ike) {
862
            if (!expectedKarmaFailure) {
863
                fail("Insufficient karma:\n" + ike.getMessage());
864
            }
865
        }
866
        catch (MetacatException me) {
867
            fail("Metacat Error:\n" + me.getMessage());
868
        }
869
        catch (Exception e) {
870
            fail("General exception:\n" + e.getMessage());
871
        }
872

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

    
896
                assertTrue(queryResult.contains(testTitle));
897
            }
898
            else {
899
                assertTrue(queryResult.indexOf("<error>") != -1);
900
            }
901
        }
902
        catch (MetacatInaccessibleException mie) {
903
            fail("Metacat Inaccessible:\n" + mie.getMessage());
904
        }
905
        catch (Exception e) {
906
            fail("General exception:\n" + e.getMessage());
907
        }
908

    
909
    }
910

    
911
    /**
912
     * Create a hopefully unique docid for testing insert and update. Does
913
     * not include the 'revision' part of the id.
914
     *
915
     * @return a String docid based on the current date and time
916
     */
917
    private String generateDocid() {
918
        StringBuffer docid = new StringBuffer(prefix);
919
        docid.append(".");
920

    
921
        // Create a calendar to get the date formatted properly
922
        String[] ids = TimeZone.getAvailableIDs( -8 * 60 * 60 * 1000);
923
        SimpleTimeZone pdt = new SimpleTimeZone( -8 * 60 * 60 * 1000, ids[0]);
924
        pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
925
        pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY,
926
                       2 * 60 * 60 * 1000);
927
        Calendar calendar = new GregorianCalendar(pdt);
928
        Date trialTime = new Date();
929
        calendar.setTime(trialTime);
930
        docid.append(calendar.get(Calendar.YEAR));
931
        docid.append(calendar.get(Calendar.DAY_OF_YEAR));
932
        docid.append(calendar.get(Calendar.HOUR_OF_DAY));
933
        docid.append(calendar.get(Calendar.MINUTE));
934
        docid.append(calendar.get(Calendar.SECOND));
935
   	    //sometimes this number is not unique, so we append a random number
936
    	int random = (new Double(Math.random()*100)).intValue();
937
    	docid.append(random);
938
        return docid.toString();
939
    }
940
}
(11-11/22)