3 import static org.junit.Assert.*;
4 import static org.junit.Assert.assertEquals;
6 import org.junit.After;
7 import org.junit.Before;
10 import java.math.BigDecimal;
12 import java.util.Properties;
13 import java.util.TimeZone;
20 static final String
url = PROPERTIES.getProperty(
"default_db_connection_url");
21 static final String
user = PROPERTIES.getProperty(
"default_super_user");
22 static final String
password = PROPERTIES.getProperty(
"default_user_password");
24 static final Integer[]
ia = {100, null, 200, 300, 400, 500};
25 static final Float[]
fa = {null, null, 1.0001f, 12.541f, null};
26 static final BigDecimal[]
da = {BigDecimal.valueOf(1.100),
28 BigDecimal.valueOf(2.15),
29 BigDecimal.valueOf(3.5)};
30 static final String[]
sa = {
"Hello", null,
"World",
"!"};
31 static final Timestamp[]
ta = {
new Timestamp(System.currentTimeMillis()),
32 new Timestamp(System.currentTimeMillis() + 1000000),
38 public void setUp() throws Exception {
39 Properties pt =
new Properties();
40 pt.setProperty(
"user",
user);
41 pt.setProperty(
"password",
password);
42 m_conn = DriverManager.getConnection(
url, pt);
43 TimeZone.setDefault(TimeZone.getTimeZone(
"UTC"));
55 Array ints = m_conn.createArrayOf(
"INT",
ia);
56 Array floats = m_conn.createArrayOf(
"FLOAT",
fa);
57 Array decimals = m_conn.createArrayOf(
"DECIMAL",
da);
58 Array strings = m_conn.createArrayOf(
"STR",
sa);
59 Array timestamps = m_conn.createArrayOf(
"TIMESTAMP",
ta);
61 assertEquals(ints.getArray(),
ia);
62 assertEquals(floats.getArray(),
fa);
63 assertEquals(decimals.getArray(),
da);
64 assertEquals(strings.getArray(),
sa);
65 assertEquals(timestamps.getArray(),
ta);
67 ResultSet rs = ints.getResultSet();
70 if (rs.wasNull()) val = null;
71 assertEquals(val,
ia[rs.getInt(1) - 1]);
73 rs = floats.getResultSet();
75 Float val = rs.getFloat(2);
76 if (rs.wasNull()) val = null;
77 assertEquals(val,
fa[rs.getInt(1) - 1]);
79 rs = decimals.getResultSet();
81 assertEquals(rs.getBigDecimal(2),
da[rs.getInt(1) - 1]);
83 rs = strings.getResultSet();
85 assertEquals(rs.getString(2),
sa[rs.getInt(1) - 1]);
87 rs = timestamps.getResultSet();
89 assertEquals(rs.getTimestamp(2),
ta[rs.getInt(1) - 1]);
95 Statement stmt = m_conn.createStatement();
96 stmt.executeUpdate(
"DROP TABLE IF EXISTS arrays_tbl");
97 stmt.executeUpdate(
"CREATE TABLE arrays_tbl ("
101 +
"da DECIMAL(4,3)[],"
102 +
"sa TEXT[] ENCODING DICT,"
103 +
"ta TIMESTAMP(3)[])");
104 PreparedStatement ps =
105 m_conn.prepareStatement(
"INSERT INTO arrays_tbl VALUES(?, ?, ?, ?, ?, ?)");
107 ps.setArray(2, m_conn.createArrayOf(
"INT",
ia));
108 ps.setArray(3, m_conn.createArrayOf(
"FLOAT",
fa));
109 ps.setArray(4, m_conn.createArrayOf(
"DECIMAL",
da));
110 ps.setArray(5, m_conn.createArrayOf(
"STR",
sa));
111 ps.setArray(6, m_conn.createArrayOf(
"TIMESTAMP",
ta));
114 ResultSet rs = stmt.executeQuery(
"SELECT i, ia, fa, da, sa, ta FROM arrays_tbl");
115 assertTrue(rs.next());
116 assertEquals(rs.getInt(
"i"), 1);
117 assertArrayEquals((
Integer[]) rs.getArray(
"ia").getArray(),
ia);
118 assertArrayEquals((
Float[]) rs.getArray(
"fa").getArray(),
fa);
119 assertArrayEquals((BigDecimal[]) rs.getArray(
"da").getArray(),
da);
120 assertArrayEquals((String[]) rs.getArray(
"sa").getArray(),
sa);
121 assertArrayEquals((Timestamp[]) rs.getArray(
"ta").getArray(),
ta);
124 stmt.executeUpdate(
"DROP TABLE arrays_tbl");
131 m_conn.createArrayOf(
"BIGINT",
ia);
132 assertTrue(
"Should not be able to create BIGINT array from integers",
false);
133 }
catch (SQLException e) {
134 }
catch (Exception ex) {
135 assertTrue(
"Expected an SQLException",
false);
139 m_conn.createArrayOf(
"not_a_type",
ia);
140 assertTrue(
"Should not be able to create an array without a valid type",
false);
141 }
catch (SQLException e) {
142 }
catch (Exception ex) {
143 assertTrue(
"Expected an SQLException",
false);
147 m_conn.createArrayOf(
"BIGINT", null);
148 assertTrue(
"Should not be able to create an array from null",
false);
149 }
catch (SQLException e) {
150 }
catch (Exception ex) {
151 assertTrue(
"Expected an SQLException",
false);
154 Array ints = m_conn.createArrayOf(
"INT",
ia);
157 ints.getArray(sz / 2, sz / 2 + 2);
158 assertTrue(
"Should not be able to get array bigger than its initial size",
false);
159 }
catch (SQLException e) {
160 }
catch (Exception ex) {
161 assertTrue(
"Expected an SQLException",
false);
static Properties PROPERTIES
static final String password
void illegal_arguments_test()
static final Timestamp[] ta
static final BigDecimal[] da
void create_and_read_array_test()
void insert_select_test()
static final Integer[] ia