001: // jTDS JDBC Driver for Microsoft SQL Server and Sybase
002: // Copyright (C) 2004 The jTDS Project
003: //
004: // This library is free software; you can redistribute it and/or
005: // modify it under the terms of the GNU Lesser General Public
006: // License as published by the Free Software Foundation; either
007: // version 2.1 of the License, or (at your option) any later version.
008: //
009: // This library is distributed in the hope that it will be useful,
010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: // Lesser General Public License for more details.
013: //
014: // You should have received a copy of the GNU Lesser General Public
015: // License along with this library; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: //
018: package net.sourceforge.jtds.test;
019:
020: import java.sql.*;
021: import java.util.Arrays;
022:
023: import net.sourceforge.jtds.jdbc.Messages;
024:
025: /**
026: * @version 1.0
027: */
028: public class EncodingTest extends TestBase {
029:
030: public EncodingTest(String name) {
031: super (name);
032: }
033:
034: /**
035: * Test for bug [101956] updateBytes converted to hex in varchar
036: * <p/>
037: * NB Test assumes server using iso_1 character set.
038: */
039: public void testBytesToVarchar() throws Exception {
040: Statement stmt = con.createStatement();
041: stmt
042: .execute("CREATE TABLE #test (id INT PRIMARY KEY, data VARCHAR(255) NULL)");
043: stmt.close();
044:
045: PreparedStatement pstmt = con
046: .prepareStatement("INSERT INTO #test (id, data) VALUES (?, ?)");
047: pstmt.setInt(1, 1);
048: pstmt.setBytes(2, "This is a test".getBytes("ISO-8859-1"));
049:
050: assertEquals(pstmt.executeUpdate(), 1);
051: pstmt.setInt(1, 2);
052: pstmt.setBytes(2, null);
053:
054: assertEquals(pstmt.executeUpdate(), 1);
055: pstmt.close();
056:
057: stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY,
058: ResultSet.CONCUR_UPDATABLE);
059: ResultSet rs = stmt.executeQuery("SELECT * FROM #test");
060:
061: while (rs.next()) {
062: if (rs.getInt(1) == 2) {
063: rs.updateBytes(2, "This is a another test"
064: .getBytes("ISO-8859-1"));
065: rs.updateRow();
066: }
067: }
068:
069: rs.close();
070: stmt.close();
071:
072: stmt = con.createStatement();
073: rs = stmt
074: .executeQuery("SELECT id, data FROM #test ORDER BY id");
075:
076: assertTrue(rs.next());
077: assertEquals("This is a test", rs.getString("data"));
078: assertTrue(rs.next());
079: assertEquals("This is a another test", rs.getString("data"));
080:
081: rs.close();
082: stmt.close();
083:
084: }
085:
086: /**
087: * Test for bug [1293415] Charset iso_1 is broken for Sybase.
088: * <p/>
089: * NB Test assumes server using iso_1 character set.
090: */
091: public void testSybaseISO_1() throws Exception {
092: if (!"2".equals(props.getProperty(Messages
093: .get(net.sourceforge.jtds.jdbc.Driver.SERVERTYPE)))) {
094: // Only test for Sybase
095: return;
096: }
097:
098: byte[] test = new byte[224];
099: for (int i = 0; i < test.length; i++) {
100: test[i] = (byte) (i + 32);
101: }
102:
103: Statement stmt = con.createStatement();
104: stmt
105: .execute("CREATE TABLE #test (id INT PRIMARY KEY, data VARCHAR(255) NULL)");
106: stmt.close();
107:
108: PreparedStatement pstmt = con
109: .prepareStatement("INSERT INTO #test (id, data) VALUES (?, ?)");
110: pstmt.setInt(1, 1);
111: pstmt.setBytes(2, test);
112:
113: assertEquals(pstmt.executeUpdate(), 1);
114: pstmt.close();
115:
116: stmt = con.createStatement();
117: ResultSet rs = stmt.executeQuery("SELECT id, data FROM #test");
118:
119: assertTrue(rs.next());
120: byte[] result = rs.getBytes("data");
121: assertEquals(test.length, result.length);
122: for (int i = 0; i < result.length; i++) {
123: assertEquals(String.valueOf(i), (byte) ((char) (i + 32)),
124: result[i]);
125: }
126: assertTrue(Arrays.equals(test, result));
127: assertFalse(rs.next());
128:
129: rs.close();
130: stmt.close();
131: }
132:
133: public static void main(String[] args) {
134: junit.textui.TestRunner.run(EncodingTest.class);
135: }
136: }
|