~ubuntu-branches/ubuntu/wily/libpgjava/wily

« back to all changes in this revision

Viewing changes to src/interfaces/jdbc/org/postgresql/test/jdbc2/EncodingTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Gybas
  • Date: 2002-02-06 23:43:06 UTC
  • Revision ID: james.westby@ubuntu.com-20020206234306-hsg7suqr8q56qg40
Tags: upstream-7.2
ImportĀ upstreamĀ versionĀ 7.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
package org.postgresql.test.jdbc2;
 
3
 
 
4
import junit.framework.*;
 
5
import org.postgresql.core.Encoding;
 
6
import java.io.*;
 
7
 
 
8
/*
 
9
 * Tests for the Encoding class.
 
10
 *
 
11
 * $Id: EncodingTest.java,v 1.4 2001/11/19 22:33:39 momjian Exp $
 
12
 */
 
13
 
 
14
 
 
15
public class EncodingTest extends TestCase
 
16
{
 
17
 
 
18
        public EncodingTest(String name)
 
19
        {
 
20
                super(name);
 
21
        }
 
22
 
 
23
        public void testCreation() throws Exception
 
24
        {
 
25
                Encoding encoding;
 
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);
 
36
        }
 
37
 
 
38
        public void testTransformations() throws Exception
 
39
        {
 
40
                Encoding encoding = Encoding.getEncoding("UNICODE", null);
 
41
                assertEquals("ab", encoding.decode(new byte[] { 97, 98 }));
 
42
 
 
43
                assertEquals(2, encoding.encode("ab").length);
 
44
                assertEquals(97, encoding.encode("a")[0]);
 
45
                assertEquals(98, encoding.encode("b")[0]);
 
46
 
 
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 }));
 
51
        }
 
52
 
 
53
        public void testReader() throws Exception
 
54
        {
 
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());
 
61
        }
 
62
}