OmniSciDB  2e3a973ef4
OmniSciArrayTest.java
Go to the documentation of this file.
1 package com.omnisci.jdbc;
2 
3 import static org.junit.Assert.*;
4 import static org.junit.Assert.assertEquals;
5 
6 import org.junit.After;
7 import org.junit.Before;
8 import org.junit.Test;
9 
10 import java.math.BigDecimal;
11 import java.sql.*;
12 import java.util.Properties;
13 import java.util.TimeZone;
14 
15 // Create Array and validate
16 // Insert arrays and validate it is the same
17 
18 public class OmniSciArrayTest {
19  static Properties PROPERTIES = new Property_loader("prepare_test.properties");
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");
23 
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),
27  null,
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),
33  null};
34 
35  private Connection m_conn = null;
36 
37  @Before
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"));
44  }
45 
46  @After
47  public void tearDown() throws Exception {
48  m_conn.close();
49  }
50 
51  // the test below makes sure that what we put into OmniSciArray
52  // is exactly what we get both from getArray() and getResultSet() methods
53  @Test
54  public void create_and_read_array_test() throws Exception {
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);
60 
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);
66 
67  ResultSet rs = ints.getResultSet();
68  while (rs.next()) {
69  Integer val = rs.getInt(2);
70  if (rs.wasNull()) val = null;
71  assertEquals(val, ia[rs.getInt(1) - 1]);
72  }
73  rs = floats.getResultSet();
74  while (rs.next()) {
75  Float val = rs.getFloat(2);
76  if (rs.wasNull()) val = null;
77  assertEquals(val, fa[rs.getInt(1) - 1]);
78  }
79  rs = decimals.getResultSet();
80  while (rs.next()) {
81  assertEquals(rs.getBigDecimal(2), da[rs.getInt(1) - 1]);
82  }
83  rs = strings.getResultSet();
84  while (rs.next()) {
85  assertEquals(rs.getString(2), sa[rs.getInt(1) - 1]);
86  }
87  rs = timestamps.getResultSet();
88  while (rs.next()) {
89  assertEquals(rs.getTimestamp(2), ta[rs.getInt(1) - 1]);
90  }
91  }
92 
93  @Test
94  public void insert_select_test() throws Exception {
95  Statement stmt = m_conn.createStatement();
96  stmt.executeUpdate("DROP TABLE IF EXISTS arrays_tbl");
97  stmt.executeUpdate("CREATE TABLE arrays_tbl ("
98  + "i integer,"
99  + "ia INTEGER[],"
100  + "fa FLOAT[],"
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(?, ?, ?, ?, ?, ?)");
106  ps.setInt(1, 1);
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));
112  ps.executeUpdate();
113 
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);
122 
123  ps.close();
124  stmt.executeUpdate("DROP TABLE arrays_tbl");
125  stmt.close();
126  }
127 
128  @Test
129  public void illegal_arguments_test() throws Exception {
130  try {
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);
136  }
137 
138  try {
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);
144  }
145 
146  try {
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);
152  }
153 
154  Array ints = m_conn.createArrayOf("INT", ia);
155  int sz = ia.length;
156  try {
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);
162  }
163  }
164 }
static final Timestamp [] ta
int64_t const int32_t sz
static final BigDecimal [] da