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: 2008-11-04 16:02:07 -0800 (Tue, 04 Nov 2008) $'
9
 * '$Revision: 4509 $'
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.Reader;
29
import java.io.StringReader;
30
import java.util.Calendar;
31
import java.util.Date;
32
import java.util.GregorianCalendar;
33
import java.util.SimpleTimeZone;
34
import java.util.TimeZone;
35

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

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

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

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

    
77
    private Metacat m;
78

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

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

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

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

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

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

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

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

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

    
156
        return accessBlock;
157

    
158
    }
159

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

    
167
        String testDocument = "";
168

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

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

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

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

    
228
        return sQuery;
229
    }
230

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

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

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

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

    
278
        return suite;
279
    }
280

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

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

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

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

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

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

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

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

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

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

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

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

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

    
766
        return response;
767
    }
768

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

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

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

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

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

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

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

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

    
908
    }
909

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

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