Project

General

Profile

« Previous | Next » 

Revision 5752

use detected XML encoding when reading/writing files
use UTF-8 as default when performing queries in the DB (assume DB is using UTF-8)
remove as many PrintWriters (uses system default character encoding only) as possible and construct OutputStreamWriters where explicit encoding can be given.
http://bugzilla.ecoinformatics.org/show_bug.cgi?id=2495

View differences:

NonAsciiCharacterTest.java
43 43
import edu.ucsb.nceas.metacat.client.MetacatFactory;
44 44
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
45 45
import edu.ucsb.nceas.metacat.properties.PropertyService;
46
import edu.ucsb.nceas.utilities.FileUtil;
46 47
import edu.ucsb.nceas.utilities.IOUtil;
47 48
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
48 49
import junit.framework.Test;
......
204 205
    
205 206
    /**
206 207
     * Returns an xml squery that searches for the doc id in the
207
     * title of documents. This function is for eml-2.0.1 only. For 
208
     * title of documents. This function is for eml-2.0.1+ only. For 
208 209
     * other eml versions, this function might have to modified.
209 210
     */
210
    private String getTestEmlQuery(String docid, String emlVersion) {
211
    private String getTestEmlQuery(String titlePart, String emlVersion) {
211 212

  
212 213
    	String docType;
213 214
    	if (emlVersion.equals(EML2_0_1)) {
......
227 228
        		"<returndoctype>" + docType + "</returndoctype>" +
228 229
        		"<querygroup operator=\"UNION\">" +
229 230
        			"<queryterm casesensitive=\"false\" searchmode=\"contains\">" +
230
        				"<value>" + docid + "</value>" +
231
        				"<value>" + titlePart + "</value>" +
231 232
        				"<pathexpr>dataset/title</pathexpr>" +
232 233
        			"</queryterm>" +
233 234
        		"</querygroup>" +
......
280 281
        suite.addTest(new NonAsciiCharacterTest("quote210Test"));
281 282
        suite.addTest(new NonAsciiCharacterTest("numericCharacterReferenceFormat201Test"));
282 283
        suite.addTest(new NonAsciiCharacterTest("numericCharacterReferenceFormat210Test"));*/
283
        suite.addTest(new NonAsciiCharacterTest("nonLatinUnicodeCharacter201Test"));
284
        //suite.addTest(new NonAsciiCharacterTest("nonLatinUnicodeCharacter201Test"));
284 285
        //suite.addTest(new NonAsciiCharacterTest("nonLatinUnicodeCharacter210Test"));
286
        suite.addTest(new NonAsciiCharacterTest("unicodeCharacterTest"));
285 287

  
288

  
286 289
        return suite;
287 290
    }
288 291

  
......
506 509
     * Test inserting and reading an EML 2.0.1 document with the code representation 
507 510
     * of a micro sign (&#181). Read should succeed since the same document should be 
508 511
     * read back from disk that was submitted.  Query should succeed because we look 
509
     * for the converted character (µ).
512
     * for the converted character (µ).
510 513
     */
511 514
    public void numericCharacterReferenceFormat201Test() {
512 515
    	debug("\nRunning: numericCharacterReferenceFormat201Test");
......
515 518
            m.login(username, password);
516 519
            
517 520
            String testTitle = "Checking &#181; in doc: " + newdocid  + ".1";
518
            String convertedTestTitle = "Checking µ in doc: " + newdocid  + ".1";
521
            String convertedTestTitle = "Checking µ in doc: " + newdocid  + ".1";
519 522
            
520 523
            testdocument = getTestEmlDoc(testTitle, EML2_0_1);
521 524
            insertDocid(newdocid + ".1", testdocument, SUCCESS, true);
......
546 549
     * Test inserting and reading an EML 2.1.0 document with the code representation 
547 550
     * of a micro sign (&#181). Read should succeed since the same document should be 
548 551
     * read back from disk that was submitted.  Query should succeed because we look 
549
     * for the converted character (µ).
552
     * for the converted character (µ).
550 553
     */
551 554
    public void numericCharacterReferenceFormat210Test() {
552 555
    	debug("\nRunning: numericCharacterReferenceFormat210Test");
......
555 558
            m.login(username, password);
556 559
            
557 560
            String testTitle = "Checking &#181; in doc: " + newdocid  + ".1";
558
            String convertedTestTitle = "Checking µ in doc: " + newdocid  + ".1";
561
            String convertedTestTitle = "Checking µ in doc: " + newdocid  + ".1";
559 562
            
560 563
            testdocument = getTestEmlDoc(testTitle, EML2_1_0);
561 564
            insertDocid(newdocid + ".1", testdocument, SUCCESS, true);
......
583 586
    }
584 587

  
585 588
    /**
586
     * Test inserting and reading an EML 2.0.1 document with the micro sign (µ). 
589
     * Test inserting and reading an EML 2.0.1 document with the micro sign (µ). 
587 590
     * Read should succeed since the same document should be read back from disk 
588 591
     * that was submitted.  Query should succeed because we look for the same 
589
     * character (µ).
592
     * character (µ).
590 593
     */
591 594
    public void nonLatinUnicodeCharacter201Test() {
592 595
    	debug("\nRunning: nonLatinUnicodeCharacter201Test");
......
597 600
            String testTitle = "Checking characters like µ in doc: " + newdocid  + ".1";
598 601
            
599 602
            testdocument = getTestEmlDoc(testTitle, EML2_0_1);
603
            
604
            debug("original test document:	" + testdocument);
605
            
600 606
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
601 607
            
602 608
            // this tests reading the document back from disk
......
633 639
            String newdocid = generateDocid();
634 640
            m.login(username, password);
635 641
            
636
            String testTitle = "Checking characters like µ in doc: " + newdocid  + ".1";
642
            String testTitle = "Checking characters like µ in doc: " + newdocid  + ".1";
637 643
            
638 644
            testdocument = getTestEmlDoc(testTitle, EML2_1_0);
639 645
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
......
659 665
            fail("General exception:\n" + e.getMessage());
660 666
        }
661 667
    }
668
    
669
    /**
670
     * Test inserting and reading an EML 2.1.0 with Chinese
671
     */
672
    public void unicodeCharacterTest() {
673
    	debug("\nRunning: unicodeCharacterTest");
674
        try {
675
            
676
        	String filePath = "test/clienttestfiles/unicodeEML.xml";
677
            String testTitle = "測試中的數據包 (Test Chinese data package) _DOCID_";
678
            String newdocid = generateDocid() + ".1";
679
            testdocument = FileUtil.readFileToString(filePath, "UTF-8");
680
            
681
            // include the docid
682
            testdocument = testdocument.replaceAll("_DOCID_", newdocid);
683
            testTitle = testTitle.replaceAll("_DOCID_", newdocid);
684
            
685
            // login
686
            m.login(username, password);
687
            
688
            // insert
689
            insertDocid(newdocid, testdocument, SUCCESS, false);
662 690

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

  
663 714
    /**
664 715
     * Insert a document into metacat. The expected result is passed as result
665 716
     */
......
667 718
                               boolean expectMetacatException) {
668 719
        String response = null;
669 720
        try {
721
        	
722
        	debug("doctext: " + docText);
723
        	
670 724
            response = m.insert(docid,
671 725
                                new StringReader(docText), null);
672 726
            System.err.println(response);
......
883 937
    }
884 938
    
885 939
    /**
886
     * Query a document by looking for it's doc id in the title element.
940
     * Query a document by looking for a part of the title in the title element.
887 941
     * Then check if the testTitle exists in the doc.
888
     * @param docId the id of the doc to look for
942
     * @param titlePart the part of the title of the doc to look for
889 943
     * @param testTitle the title containing special characters
890 944
     * @param result are we expecting SUCCESS or FAILURE
891 945
     * @param expextedKarmaFailure
892 946
     */
893
    private void queryDocWhichHasTitle(String docId, String testTitle,
947
    private void queryDocWhichHasTitle(String titlePart, String testTitle,
894 948
                                         String emlVersion, boolean result) {
895 949
        try {
896
            String sQuery = getTestEmlQuery(docId, emlVersion);
950
            String sQuery = getTestEmlQuery(titlePart, emlVersion);
897 951
        	Reader queryReader = new StringReader(sQuery);
898 952
            Reader resultReader = m.query(queryReader);
899 953
            String queryResult = IOUtil.getAsString(resultReader, true);

Also available in: Unified diff