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