Project

General

Profile

« Previous | Next » 

Revision 5744

Added by berkley almost 14 years ago

a test to explore character encoding with utf8 chars

View differences:

test/edu/ucsb/nceas/metacattest/ReaderWriterTest.java
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.*;
29
import java.util.*;
30

  
31
import org.apache.commons.io.IOUtils;
32

  
33
import edu.ucsb.nceas.MCTestCase;
34
import edu.ucsb.nceas.metacat.client.MetacatFactory;
35
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
36
import junit.framework.Test;
37
import junit.framework.TestSuite;
38

  
39

  
40
/**
41
 * A unit test to see which readers/writers break a non-ascii char stream
42
 * @author berkley
43
 */
44
public class ReaderWriterTest extends MCTestCase 
45
{
46
    public static String testStr = "Checking characters like µ in doc";
47
    
48
    /**
49
     * Constructor to build the test
50
     *
51
     * @param name the name of the test method
52
     */
53
    public ReaderWriterTest(String name) {
54
        super(name);
55
    }
56

  
57
    /**
58
     * Establish a testing framework by initializing appropriate objects
59
     */
60
    public void setUp() 
61
    {
62
        
63
    }
64

  
65
    /**
66
     * Release any objects after tests are complete
67
     */
68
    public void tearDown() 
69
    {
70
    }
71

  
72
    /**
73
     * Create a suite of tests to be run together
74
     */
75
    public static Test suite() 
76
    {
77
        TestSuite suite = new TestSuite();
78
        suite.addTest(new ReaderWriterTest("initialize"));
79
        suite.addTest(new ReaderWriterTest("testStringReaderAndWriter"));
80
        suite.addTest(new ReaderWriterTest("testFileReaderAndWriter"));
81
        suite.addTest(new ReaderWriterTest("testBufferedReaderAndWriter"));
82
        suite.addTest(new ReaderWriterTest("charArrayReaderAndWriter"));
83
//        suite.addTest(new ReaderWriterTest("testBufferedReaderAndWriter"));
84
//        suite.addTest(new ReaderWriterTest("testBufferedReaderAndWriter"));
85
//        suite.addTest(new ReaderWriterTest("testBufferedReaderAndWriter"));
86
//        suite.addTest(new ReaderWriterTest("testBufferedReaderAndWriter"));
87
//        suite.addTest(new ReaderWriterTest("testBufferedReaderAndWriter"));
88
        
89
        return suite;
90
    }
91
    
92
    /**
93
     * test FileReader and FileWriter
94
     */
95
    public void charArrayReaderAndWriter()
96
    {
97
        try
98
        {
99
            CharArrayWriter caw = new CharArrayWriter();
100
            char[] c = new char[testStr.length()];
101
            testStr.getChars(0, testStr.length(), c, 0);
102
            caw.write(c);
103
            assertTrue(caw.toString().equals(testStr));
104
            
105
            CharArrayReader car = new CharArrayReader(c);
106
            String s = readReader(car);
107
            assertTrue(s.equals(testStr));
108
        }
109
        catch(Exception e)
110
        {
111
            fail("Unexpected error in testFileReaderAndWriter: " + e.getMessage());
112
        }
113
    }
114
    
115
    /**
116
     * test FileReader and FileWriter
117
     */
118
    public void testBufferedReaderAndWriter()
119
    {
120
        try
121
        {
122
            File tmp = getTempFile();
123
            FileWriter fw = new FileWriter(tmp);
124
            BufferedWriter bw = new BufferedWriter(fw);
125
            bw.write(testStr);
126
            bw.flush();
127
            bw.close();
128
            
129
            FileReader fr = new FileReader(tmp);
130
            BufferedReader br = new BufferedReader(fr);
131
            String s = readReader(br);
132
            assertTrue(s.equals(testStr));
133
        }
134
        catch(Exception e)
135
        {
136
            fail("Unexpected error in testFileReaderAndWriter: " + e.getMessage());
137
        }
138
     
139
    }
140
    
141
    /**
142
     * test FileReader and FileWriter
143
     */
144
    public void testFileReaderAndWriter()
145
    {
146
        try
147
        {
148
            File tmp = getTempFile();
149
            FileWriter fw = new FileWriter(tmp);
150
            fw.write(testStr);
151
            fw.flush();
152
            fw.close();
153
            
154
            FileReader fr = new FileReader(tmp);
155
            String s = readReader(fr);
156
            assertTrue(s.equals(testStr));
157
        }
158
        catch(Exception e)
159
        {
160
            fail("Unexpected error in testFileReaderAndWriter: " + e.getMessage());
161
        }
162
     
163
    }
164
    
165
    /**
166
     * test StringReader and StringWriter
167
     */
168
    public void testStringReaderAndWriter()
169
    {
170
        try
171
        {
172
            StringReader sr = new StringReader(testStr);
173
            String s = readReader(sr);
174
            assertTrue(s.equals(testStr));
175
            
176
            Writer testWriter = writeStringToWriter(testStr);
177
            assertTrue(testWriter.toString().equals(testStr));
178
            
179
            //tmp.delete();
180
        }
181
        catch(Exception e)
182
        {
183
            fail("Unexpected error in testStringReaderAndWriter");
184
        }
185
    }
186
    
187
    public void initialize()
188
    {
189
        assert(1 == 1);
190
    }
191
    
192
    private Writer writeStringToWriter(String s)
193
        throws IOException
194
    {
195
        StringWriter w = new StringWriter();
196
        w.write(s);
197
        return w;
198
    }
199
    
200
    private String readReader(Reader sr)
201
        throws IOException
202
    {
203
        char[] c = new char[1024];
204
        int numread = sr.read(c, 0, 1024);
205
        StringBuffer sb = new StringBuffer();
206
        while(numread != -1)
207
        {
208
            sb.append(c, 0, numread);
209
            numread = sr.read(c, 0, 1024);
210
        }
211
        return sb.toString();
212
    }
213
    
214
    private File getTempFile()
215
    {
216
        File f = new File("/tmp/ReaderWriterTest." + new Date().getTime() + ".tmp");
217
        return f;
218
    }
219
}
test/edu/ucsb/nceas/metacattest/NonAsciiCharacterTest.java
269 269
        TestSuite suite = new TestSuite();
270 270
        suite.addTest(new NonAsciiCharacterTest("initialize"));
271 271
        // Test basic functions
272
        suite.addTest(new NonAsciiCharacterTest("invalidXMLCharacters201Test"));
272
        /*suite.addTest(new NonAsciiCharacterTest("invalidXMLCharacters201Test"));
273 273
        suite.addTest(new NonAsciiCharacterTest("invalidXMLCharacters210Test"));
274 274
        suite.addTest(new NonAsciiCharacterTest("symbolEncodedFormat201Test"));
275 275
        suite.addTest(new NonAsciiCharacterTest("symbolEncodedFormat210Test"));
276 276
        suite.addTest(new NonAsciiCharacterTest("quote201Test"));
277 277
        suite.addTest(new NonAsciiCharacterTest("quote210Test"));
278 278
        suite.addTest(new NonAsciiCharacterTest("numericCharacterReferenceFormat201Test"));
279
        suite.addTest(new NonAsciiCharacterTest("numericCharacterReferenceFormat210Test"));
279
        suite.addTest(new NonAsciiCharacterTest("numericCharacterReferenceFormat210Test"));*/
280 280
        suite.addTest(new NonAsciiCharacterTest("nonLatinUnicodeCharacter201Test"));
281
        suite.addTest(new NonAsciiCharacterTest("nonLatinUnicodeCharacter210Test"));
281
        //suite.addTest(new NonAsciiCharacterTest("nonLatinUnicodeCharacter210Test"));
282 282

  
283 283
        return suite;
284 284
    }
......
503 503
     * Test inserting and reading an EML 2.0.1 document with the code representation 
504 504
     * of a micro sign (&#181). Read should succeed since the same document should be 
505 505
     * read back from disk that was submitted.  Query should succeed because we look 
506
     * for the converted character (µ).
506
     * for the converted character (µ).
507 507
     */
508 508
    public void numericCharacterReferenceFormat201Test() {
509 509
    	debug("\nRunning: numericCharacterReferenceFormat201Test");
......
512 512
            m.login(username, password);
513 513
            
514 514
            String testTitle = "Checking µ in doc: " + newdocid  + ".1";
515
            String convertedTestTitle = "Checking µ in doc: " + newdocid  + ".1";
515
            String convertedTestTitle = "Checking µ in doc: " + newdocid  + ".1";
516 516
            
517 517
            testdocument = getTestEmlDoc(testTitle, EML2_0_1);
518 518
            insertDocid(newdocid + ".1", testdocument, SUCCESS, true);
......
543 543
     * Test inserting and reading an EML 2.1.0 document with the code representation 
544 544
     * of a micro sign (&#181). Read should succeed since the same document should be 
545 545
     * read back from disk that was submitted.  Query should succeed because we look 
546
     * for the converted character (µ).
546
     * for the converted character (µ).
547 547
     */
548 548
    public void numericCharacterReferenceFormat210Test() {
549 549
    	debug("\nRunning: numericCharacterReferenceFormat210Test");
......
552 552
            m.login(username, password);
553 553
            
554 554
            String testTitle = "Checking µ in doc: " + newdocid  + ".1";
555
            String convertedTestTitle = "Checking µ in doc: " + newdocid  + ".1";
555
            String convertedTestTitle = "Checking µ in doc: " + newdocid  + ".1";
556 556
            
557 557
            testdocument = getTestEmlDoc(testTitle, EML2_1_0);
558 558
            insertDocid(newdocid + ".1", testdocument, SUCCESS, true);
......
580 580
    }
581 581

  
582 582
    /**
583
     * Test inserting and reading an EML 2.0.1 document with the micro sign (µ). 
583
     * Test inserting and reading an EML 2.0.1 document with the micro sign (µ). 
584 584
     * Read should succeed since the same document should be read back from disk 
585 585
     * that was submitted.  Query should succeed because we look for the same 
586
     * character (µ).
586
     * character (µ).
587 587
     */
588 588
    public void nonLatinUnicodeCharacter201Test() {
589 589
    	debug("\nRunning: nonLatinUnicodeCharacter201Test");
......
619 619
    }
620 620
    
621 621
    /**
622
     * Test inserting and reading an EML 2.1.0 document with the micro sign (µ). 
622
     * Test inserting and reading an EML 2.1.0 document with the micro sign (µ). 
623 623
     * Read should succeed since the same document should be read back from disk 
624 624
     * that was submitted.  Query should succeed because we look for the same 
625
     * character (µ).
625
     * character (µ).
626 626
     */
627 627
    public void nonLatinUnicodeCharacter210Test() {
628 628
    	debug("\nRunning: nonLatinUnicodeCharacter210Test");
......
630 630
            String newdocid = generateDocid();
631 631
            m.login(username, password);
632 632
            
633
            String testTitle = "Checking characters like µ in doc: " + newdocid  + ".1";
633
            String testTitle = "Checking characters like µ in doc: " + newdocid  + ".1";
634 634
            
635 635
            testdocument = getTestEmlDoc(testTitle, EML2_1_0);
636 636
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);

Also available in: Unified diff