Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2004 Regents of the University of California and the
4
 *              National Center for Ecological Analysis and Synthesis
5
 *  Purpose: To test the Access Controls in metacat by JUnit
6
 *
7
 *   '$Author: leinfelder $'
8
 *     '$Date: 2010-12-09 10:41:36 -0800 (Thu, 09 Dec 2010) $'
9
 * '$Revision: 5712 $'
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 if (emlVersion == EML2_1_0) {
174
			header = testEml_210_Header;
175
		} else { // if (emlVersion == EML2_1_1) {
176
			header = testEml_211_Header;
177
		}
178
        
179
        testDocument += header;
180
        
181
        // if this is an EML 2.1.0 or later document, the document level access is
182
        // before the dataset element.
183
        if (emlVersion == EML2_1_0 || emlVersion == EML2_1_1) {
184
        	testDocument += getAccessBlock("public", true, true, false, false, false);
185
        }
186
        testDocument += "<dataset scope=\"document\"><title>" + title + "</title>"
187
				+ testEmlCreatorBlock;
188

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

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

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

    
233
        return sQuery;
234
    }
235

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

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

    
259
    /**
260
     * Release any objects after tests are complete
261
     */
262
    public void tearDown() {
263
    }
264

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

    
283
        return suite;
284
    }
285

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

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

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

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

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

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

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

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

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

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

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

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

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

    
771
        return response;
772
    }
773

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

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

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

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

    
851
                if (!testDoc.equals(doc)) {
852
                    debug("doc    :" + doc);
853
                    debug("testDoc:" + testDoc);
854
                }
855

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

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

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

    
913
    }
914

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

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