01: /*
02: * Created on May 23, 2003
03: *
04: */
05: package org.axiondb.types;
06:
07: import java.io.ByteArrayInputStream;
08: import java.io.IOException;
09: import java.io.InputStream;
10: import java.io.OutputStream;
11: import java.io.Reader;
12: import java.io.UnsupportedEncodingException;
13: import java.sql.SQLException;
14:
15: import junit.framework.Test;
16: import junit.framework.TestCase;
17: import junit.framework.TestSuite;
18:
19: import org.axiondb.AxionException;
20:
21: /**
22: * @author mdelagrange
23: *
24: */
25: public class TestClobSource extends TestCase {
26:
27: /**
28: * Constructor for TestClobSource.
29: * @param arg0
30: */
31: public TestClobSource(String arg0) {
32: super (arg0);
33: }
34:
35: public static Test suite() {
36: return new TestSuite(TestClobSource.class);
37: }
38:
39: public void testUtf8GetCharacterStream() throws Exception {
40: ClobSource source = new ClobSource(new TestLobSource());
41: Reader reader = source.getCharacterStream();
42: char theChar = (char) reader.read();
43: assertEquals("should be the right character", '\uFFFA', theChar);
44: assertTrue("stream should be exhausted", reader.read() == -1);
45: }
46:
47: public void testUtf8GetAsciiStream() throws SQLException {
48: ClobSource source = new ClobSource(new TestLobSource());
49: InputStream stream = source.getAsciiStream();
50: try {
51: stream.read();
52: fail("Expected IOException reading a non-ascii stream");
53: } catch (IOException e) {
54: assertEquals(
55: "Could not convert stream from UTF-8 to US-ASCII",
56: e.getMessage());
57: }
58:
59: try {
60: stream.close();
61: } catch (IOException e1) {
62: }
63: }
64:
65: public class TestLobSource implements LobSource {
66: public String lob = String.valueOf('\uFFFA');
67:
68: public long length() throws AxionException {
69: try {
70: return lob.getBytes("UTF8").length;
71: } catch (UnsupportedEncodingException e) {
72: throw new AxionException(e);
73: }
74: }
75:
76: public void truncate(long length) throws AxionException {
77: throw new AxionException();
78: }
79:
80: public InputStream getInputStream() throws AxionException {
81: try {
82: return new ByteArrayInputStream(lob.getBytes("UTF8"));
83: } catch (UnsupportedEncodingException e) {
84: throw new AxionException(e.toString());
85: }
86: }
87:
88: public OutputStream setOutputStream(long pos)
89: throws AxionException {
90: throw new AxionException();
91: }
92: }
93:
94: }
|