KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > test > jdbc > TestZloty


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.test.jdbc;
6
7 import java.math.BigDecimal JavaDoc;
8 import java.sql.Connection JavaDoc;
9 import java.sql.PreparedStatement JavaDoc;
10 import java.sql.ResultSet JavaDoc;
11 import java.sql.SQLException JavaDoc;
12
13 import org.h2.test.TestBase;
14
15 public class TestZloty extends TestBase {
16
17     public void test() throws Exception JavaDoc {
18         testZloty();
19         testModifyBytes();
20     }
21     
22     private static class ZlotyBigDecimal extends BigDecimal JavaDoc {
23
24         public ZlotyBigDecimal(String JavaDoc s) {
25             super(s);
26         }
27
28         private static final long serialVersionUID = -8004563653683501484L;
29         
30         public int compareTo(BigDecimal JavaDoc bd) {
31             return -super.compareTo(bd);
32         }
33         
34     }
35     
36     private void testModifyBytes() throws Exception JavaDoc {
37         deleteDb("zloty");
38         Connection JavaDoc conn = getConnection("zloty");
39         conn.createStatement().execute("CREATE TABLE TEST(ID INT, DATA BINARY)");
40         PreparedStatement JavaDoc prep = conn.prepareStatement("INSERT INTO TEST VALUES(?, ?)");
41         byte[] shared = new byte[1];
42         prep.setInt(1, 0);
43         prep.setBytes(2, shared);
44         prep.execute();
45         shared[0] = 1;
46         prep.setInt(1, 1);
47         prep.setBytes(2, shared);
48         prep.execute();
49         ResultSet JavaDoc rs = conn.createStatement().executeQuery("SELECT * FROM TEST ORDER BY ID");
50         rs.next();
51         check(rs.getInt(1), 0);
52         check(rs.getBytes(2)[0], 0);
53         rs.next();
54         check(rs.getInt(1), 1);
55         check(rs.getBytes(2)[0], 1);
56         rs.getBytes(2)[0] = 2;
57         check(rs.getBytes(2)[0], 1);
58         checkFalse(rs.next());
59         conn.close();
60     }
61
62     /**
63      * H2 destroyer application ;->
64      * @author Maciej Wegorkiewicz
65      */

66     private void testZloty() throws Exception JavaDoc {
67         deleteDb("zloty");
68         Connection JavaDoc conn = getConnection("zloty");
69         conn.createStatement().execute("CREATE TABLE TEST(ID INT, AMOUNT DECIMAL)");
70         PreparedStatement JavaDoc prep = conn.prepareStatement("INSERT INTO TEST VALUES(?, ?)");
71         prep.setInt(1, 1);
72         prep.setBigDecimal(2, new BigDecimal JavaDoc("10.0"));
73         prep.execute();
74         prep.setInt(1, 2);
75         try {
76             prep.setBigDecimal(2, new ZlotyBigDecimal("11.0"));
77             prep.execute();
78             error("unexpected success");
79         } catch(SQLException JavaDoc e) {
80             checkNotGeneralException(e);
81         }
82
83         prep.setInt(1, 3);
84         try {
85             BigDecimal JavaDoc crappyVal=new BigDecimal JavaDoc("12.100000") {
86                 private static final long serialVersionUID = -7909023971521750844L;
87                 public String JavaDoc toString() { return "12,100000 EURO"; }
88             };
89             prep.setBigDecimal(2, crappyVal);
90             prep.execute();
91             error("unexpected success");
92         } catch(SQLException JavaDoc e) {
93             checkNotGeneralException(e);
94         }
95         
96         conn.close();
97     }
98
99 }
100
Popular Tags