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.*;
29
import java.nio.charset.Charset;
30
import java.util.*;
31

    
32
import org.apache.commons.io.IOUtils;
33
import org.apache.commons.io.input.XmlStreamReader;
34

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

    
41

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

    
59
    /**
60
     * Establish a testing framework by initializing appropriate objects
61
     */
62
    public void setUp() 
63
    {
64
        System.out.println("default charset is: " + Charset.defaultCharset().displayName());
65
    }
66

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

    
74
    /**
75
     * Create a suite of tests to be run together
76
     */
77
    public static Test suite() 
78
    {
79
        TestSuite suite = new TestSuite();
80
//        suite.addTest(new ReaderWriterTest("initialize"));
81
//        suite.addTest(new ReaderWriterTest("testStringReaderAndWriter"));
82
//        suite.addTest(new ReaderWriterTest("testFileReaderAndWriter"));
83
//        suite.addTest(new ReaderWriterTest("testBufferedReaderAndWriter"));
84
//        suite.addTest(new ReaderWriterTest("charArrayReaderAndWriter"));
85
        suite.addTest(new ReaderWriterTest("testInputStreamReaderAndWriter"));
86
//        suite.addTest(new ReaderWriterTest("testBufferedReaderAndWriter"));
87
//        suite.addTest(new ReaderWriterTest("testBufferedReaderAndWriter"));
88
//        suite.addTest(new ReaderWriterTest("testBufferedReaderAndWriter"));
89
//        suite.addTest(new ReaderWriterTest("testBufferedReaderAndWriter"));
90
        
91
        return suite;
92
    }
93
    
94
    /**
95
     * test InputStreamReader and InputStreamWriter
96
     */
97
    public void testInputStreamReaderAndWriter()
98
    {
99
        try
100
        {
101
            File tmpFile = getTempFile();
102
            FileOutputStream fos = new FileOutputStream(tmpFile);
103
            fos.write(testStr.getBytes());
104
            fos.flush();
105
            fos.close();
106
            
107
            FileInputStream fis = new FileInputStream(tmpFile);
108
            InputStreamReader isr = new InputStreamReader(fis);
109
            String s = readReader(isr);
110
            System.out.println("s: " + s);
111
            assertTrue(s.equals(testStr));
112
        }
113
        catch(Exception e)
114
        {
115
            fail("Unexpected error in testInputStreamReaderAndWriter: " + e.getMessage());
116
        }
117
    }
118
    
119
    /**
120
     * test FileReader and FileWriter
121
     */
122
    public void charArrayReaderAndWriter()
123
    {
124
        try
125
        {
126
            CharArrayWriter caw = new CharArrayWriter();
127
            char[] c = new char[testStr.length()];
128
            testStr.getChars(0, testStr.length(), c, 0);
129
            caw.write(c);
130
            assertTrue(caw.toString().equals(testStr));
131
            
132
            CharArrayReader car = new CharArrayReader(c);
133
            String s = readReader(car);
134
            assertTrue(s.equals(testStr));
135
        }
136
        catch(Exception e)
137
        {
138
            fail("Unexpected error in charArrayReaderAndWriter: " + e.getMessage());
139
        }
140
    }
141
    
142
    /**
143
     * test FileReader and FileWriter
144
     */
145
    public void testBufferedReaderAndWriter()
146
    {
147
        try
148
        {
149
            File tmp = getTempFile();
150
            FileWriter fw = new FileWriter(tmp);
151
            BufferedWriter bw = new BufferedWriter(fw);
152
            bw.write(testStr);
153
            bw.flush();
154
            bw.close();
155
            
156
            FileReader fr = new FileReader(tmp);
157
            BufferedReader br = new BufferedReader(fr);
158
            String s = readReader(br);
159
            assertTrue(s.equals(testStr));
160
        }
161
        catch(Exception e)
162
        {
163
            fail("Unexpected error in testBufferedReaderAndWriter: " + e.getMessage());
164
        }
165
     
166
    }
167
    
168
    /**
169
     * test FileReader and FileWriter
170
     */
171
    public void testFileReaderAndWriter()
172
    {
173
        try
174
        {
175
            File tmp = getTempFile();
176
            FileWriter fw = new FileWriter(tmp);
177
            fw.write(testStr);
178
            fw.flush();
179
            fw.close();
180
            
181
            FileReader fr = new FileReader(tmp);
182
            String s = readReader(fr);
183
            assertTrue(s.equals(testStr));
184
        }
185
        catch(Exception e)
186
        {
187
            fail("Unexpected error in testFileReaderAndWriter: " + e.getMessage());
188
        }
189
     
190
    }
191
    
192
    /**
193
     * test StringReader and StringWriter
194
     */
195
    public void testStringReaderAndWriter()
196
    {
197
        try
198
        {
199
            StringReader sr = new StringReader(testStr);
200
            String s = readReader(sr);
201
            assertTrue(s.equals(testStr));
202
            
203
            Writer testWriter = writeStringToWriter(testStr);
204
            assertTrue(testWriter.toString().equals(testStr));
205
            
206
            //tmp.delete();
207
        }
208
        catch(Exception e)
209
        {
210
            fail("Unexpected error in testStringReaderAndWriter");
211
        }
212
    }
213
    
214
    /**
215
     * test Commons IO detection
216
     */
217
    public void testXMLEncodingDectection()
218
    {
219
        try
220
        {
221
        	System.out.println("default charset:" + Charset.defaultCharset().displayName());
222
        	String sampleXML = "<?xml version='1.0' encoding='UTF-8'?><test>my content 你</test>";
223
        	// get bytes using different encoding - shouldn't matter what we use for the prolog
224
        	XmlStreamReader xsr = 
225
        		new XmlStreamReader(
226
        				new BufferedInputStream(new ByteArrayInputStream(sampleXML.getBytes("ISO-8859-1"))));
227
        	
228
        	System.out.println("detected encoding:" + xsr.getEncoding());
229
        	
230
        	// read the string [again] using the detected encoding
231
        	// NOTE: XmlStreamReader consumes the entire stream and does not suport reset()
232
        	// Besides, we'd have the wrong bytes anyway
233
        	String result = IOUtils.toString(new ByteArrayInputStream(sampleXML.getBytes(xsr.getEncoding())));
234
            System.out.println(result);
235
        	
236
        	assertTrue(result.equals(sampleXML));
237
        }
238
        catch(Exception e)
239
        {
240
        	e.printStackTrace();
241
            fail("Unexpected error in testXMLEncodingDectection: " + e.getMessage());
242
        }
243
     
244
    }
245
    
246
    public void initialize()
247
    {
248
        assert(1 == 1);
249
    }
250
    
251
    private Writer writeStringToWriter(String s)
252
        throws IOException
253
    {
254
        StringWriter w = new StringWriter();
255
        w.write(s);
256
        return w;
257
    }
258
    
259
    private String readReader(Reader sr)
260
        throws IOException
261
    {
262
        char[] c = new char[1024];
263
        int numread = sr.read(c, 0, 1024);
264
        StringBuffer sb = new StringBuffer();
265
        while(numread != -1)
266
        {
267
            sb.append(c, 0, numread);
268
            numread = sr.read(c, 0, 1024);
269
        }
270
        return sb.toString();
271
    }
272
    
273
    private File getTempFile()
274
    {
275
        File f = new File("/tmp/ReaderWriterTest." + new Date().getTime() + ".tmp");
276
        return f;
277
    }
278
}
(24-24/30)