001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (license2)
004: * Initial Developer: H2 Group
005: */
006: package org.h2.test.jdbc;
007:
008: import java.math.BigDecimal;
009: import java.sql.Connection;
010: import java.sql.PreparedStatement;
011: import java.sql.ResultSet;
012: import java.sql.SQLException;
013:
014: import org.h2.test.TestBase;
015:
016: /**
017: * Tests a custom BigDecimal implementation, as well
018: * as direct modification of a byte in a byte array.
019: */
020: public class TestZloty extends TestBase {
021:
022: public void test() throws Exception {
023: testZloty();
024: testModifyBytes();
025: }
026:
027: private static class ZlotyBigDecimal extends BigDecimal {
028:
029: public ZlotyBigDecimal(String s) {
030: super (s);
031: }
032:
033: private static final long serialVersionUID = -8004563653683501484L;
034:
035: public int compareTo(BigDecimal bd) {
036: return -super .compareTo(bd);
037: }
038:
039: }
040:
041: private void testModifyBytes() throws Exception {
042: deleteDb("zloty");
043: Connection conn = getConnection("zloty");
044: conn.createStatement().execute(
045: "CREATE TABLE TEST(ID INT, DATA BINARY)");
046: PreparedStatement prep = conn
047: .prepareStatement("INSERT INTO TEST VALUES(?, ?)");
048: byte[] shared = new byte[1];
049: prep.setInt(1, 0);
050: prep.setBytes(2, shared);
051: prep.execute();
052: shared[0] = 1;
053: prep.setInt(1, 1);
054: prep.setBytes(2, shared);
055: prep.execute();
056: ResultSet rs = conn.createStatement().executeQuery(
057: "SELECT * FROM TEST ORDER BY ID");
058: rs.next();
059: check(rs.getInt(1), 0);
060: check(rs.getBytes(2)[0], 0);
061: rs.next();
062: check(rs.getInt(1), 1);
063: check(rs.getBytes(2)[0], 1);
064: rs.getBytes(2)[0] = 2;
065: check(rs.getBytes(2)[0], 1);
066: checkFalse(rs.next());
067: conn.close();
068: }
069:
070: /**
071: * H2 destroyer application ;->
072: *
073: * @author Maciej Wegorkiewicz
074: */
075: private void testZloty() throws Exception {
076: deleteDb("zloty");
077: Connection conn = getConnection("zloty");
078: conn.createStatement().execute(
079: "CREATE TABLE TEST(ID INT, AMOUNT DECIMAL)");
080: PreparedStatement prep = conn
081: .prepareStatement("INSERT INTO TEST VALUES(?, ?)");
082: prep.setInt(1, 1);
083: prep.setBigDecimal(2, new BigDecimal("10.0"));
084: prep.execute();
085: prep.setInt(1, 2);
086: try {
087: prep.setBigDecimal(2, new ZlotyBigDecimal("11.0"));
088: prep.execute();
089: error();
090: } catch (SQLException e) {
091: checkNotGeneralException(e);
092: }
093:
094: prep.setInt(1, 3);
095: try {
096: BigDecimal value = new BigDecimal("12.100000") {
097: private static final long serialVersionUID = -7909023971521750844L;
098:
099: public String toString() {
100: return "12,100000 EURO";
101: }
102: };
103: prep.setBigDecimal(2, value);
104: prep.execute();
105: error();
106: } catch (SQLException e) {
107: checkNotGeneralException(e);
108: }
109:
110: conn.close();
111: }
112:
113: }
|