File: EncodingTest.java

package info (click to toggle)
libpgjava 8.4-701-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,532 kB
  • ctags: 4,162
  • sloc: java: 33,948; xml: 3,158; makefile: 14; sh: 10
file content (68 lines) | stat: -rw-r--r-- 2,208 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*-------------------------------------------------------------------------
*
* Copyright (c) 2004-2008, PostgreSQL Global Development Group
*
* IDENTIFICATION
*   $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/EncodingTest.java,v 1.11 2008/02/19 06:12:24 jurka Exp $
*
*-------------------------------------------------------------------------
*/

package org.postgresql.test.jdbc2;

import junit.framework.*;
import org.postgresql.core.Encoding;
import java.io.*;
import java.util.Locale;

/*
 * Tests for the Encoding class.
 *
 */


public class EncodingTest extends TestCase
{

    public EncodingTest(String name)
    {
        super(name);
    }

    public void testCreation() throws Exception
    {
        Encoding encoding;
        encoding = Encoding.getDatabaseEncoding("UNICODE");
        assertEquals("UTF", encoding.name().substring(0, 3).toUpperCase(Locale.US));
        encoding = Encoding.getDatabaseEncoding("SQL_ASCII");
        assertTrue(encoding.name().toUpperCase(Locale.US).indexOf("ASCII") != -1);
        assertEquals("When encoding is unknown the default encoding should be used",
                     Encoding.defaultEncoding(),
                     Encoding.getDatabaseEncoding("UNKNOWN"));
    }

    public void testTransformations() throws Exception
    {
        Encoding encoding = Encoding.getDatabaseEncoding("UNICODE");
        assertEquals("ab", encoding.decode(new byte[] { 97, 98 }));

        assertEquals(2, encoding.encode("ab").length);
        assertEquals(97, encoding.encode("a")[0]);
        assertEquals(98, encoding.encode("b")[0]);

        encoding = Encoding.defaultEncoding();
        assertEquals("a".getBytes()[0], encoding.encode("a")[0]);
        assertEquals(new String(new byte[] { 97 }),
                     encoding.decode(new byte[] { 97 }));
    }

    public void testReader() throws Exception
    {
        Encoding encoding = Encoding.getDatabaseEncoding("SQL_ASCII");
        InputStream stream = new ByteArrayInputStream(new byte[] { 97, 98 });
        Reader reader = encoding.getDecodingReader(stream);
        assertEquals(97, reader.read());
        assertEquals(98, reader.read());
        assertEquals( -1, reader.read());
    }
}