2
package org.postgresql.test.jdbc2;
4
import junit.framework.*;
5
import org.postgresql.core.Encoding;
9
* Tests for the Encoding class.
11
* $Id: EncodingTest.java,v 1.4 2001/11/19 22:33:39 momjian Exp $
15
public class EncodingTest extends TestCase
18
public EncodingTest(String name)
23
public void testCreation() throws Exception
26
encoding = Encoding.getEncoding("UNICODE", null);
27
assertEquals("UTF", encoding.name().substring(0, 3).toUpperCase());
28
encoding = Encoding.getEncoding("SQL_ASCII", null);
29
assertTrue(encoding.name().toUpperCase().indexOf("ASCII") != -1);
30
assertEquals("When encoding is unknown the default encoding should be used",
31
Encoding.defaultEncoding(),
32
Encoding.getEncoding("UNKNOWN", null));
33
encoding = Encoding.getEncoding("SQL_ASCII", "utf-8");
34
assertTrue("Encoding passed in by the user should be preferred",
35
encoding.name().toUpperCase().indexOf("UTF") != -1);
38
public void testTransformations() throws Exception
40
Encoding encoding = Encoding.getEncoding("UNICODE", null);
41
assertEquals("ab", encoding.decode(new byte[] { 97, 98 }));
43
assertEquals(2, encoding.encode("ab").length);
44
assertEquals(97, encoding.encode("a")[0]);
45
assertEquals(98, encoding.encode("b")[0]);
47
encoding = Encoding.defaultEncoding();
48
assertEquals("a".getBytes()[0], encoding.encode("a")[0]);
49
assertEquals(new String(new byte[] { 97 }),
50
encoding.decode(new byte[] { 97 }));
53
public void testReader() throws Exception
55
Encoding encoding = Encoding.getEncoding("SQL_ASCII", null);
56
InputStream stream = new ByteArrayInputStream(new byte[] { 97, 98 });
57
Reader reader = encoding.getDecodingReader(stream);
58
assertEquals(97, reader.read());
59
assertEquals(98, reader.read());
60
assertEquals( -1, reader.read());