1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.jdbc4olap.jdbc;
24
25 import junit.framework.TestCase;
26 import org.jdbc4olap.xmla.XmlaLogin;
27 import org.jdbc4olap.xmla.XmlaConn;
28 import org.jdbc4olap.xmla.XmlaLoginTest;
29
30 import java.sql.SQLException;
31 import java.util.ArrayList;
32
33
34
35
36
37
38 public class OlapPreparedStatementTest extends TestCase {
39
40 private static OlapPreparedStatement createMock(final XmlaLogin login, final String statement,
41 final ArrayList<String[]> mockCatalogCache,
42 final ArrayList<String[]> mockSchemaCache,
43 final String mockTable, final String mockColumn)
44 throws SQLException {
45
46 return new OlapPreparedStatement(OlapConnectionTest.createMock(login, mockCatalogCache, mockSchemaCache, mockTable, mockColumn), statement);
47 }
48
49 public void testGetStatementParameterCount() throws Exception {
50 assertEquals(0, OlapPreparedStatement.getStatementParameterCount(null));
51 assertEquals(0, OlapPreparedStatement.getStatementParameterCount(""));
52 assertEquals(0, OlapPreparedStatement.getStatementParameterCount("T"));
53 assertEquals(0, OlapPreparedStatement.getStatementParameterCount("select * from T"));
54
55 assertEquals(1, OlapPreparedStatement.getStatementParameterCount("select * from T where F=?"));
56
57 assertEquals(2, OlapPreparedStatement.getStatementParameterCount("F=?, F2>?"));
58 }
59
60 public void testCreate() throws Exception {
61 try {
62 new OlapPreparedStatement(null, null);
63 fail("Null connection and statement should fail");
64 } catch (NullPointerException e) {
65 assertNull(e.getMessage());
66 }
67
68
69 assertNotNull(new OlapPreparedStatement(OlapConnectionTest.createMock(XmlaConn.STANDARD_SERVER, null), null));
70 }
71
72 public void testCreateMock() throws Exception {
73
74 final ArrayList<String[]> mockCatalogCache = new ArrayList<String[]>();
75 final String mockCatalog = "cat";
76 mockCatalogCache.add(new String[] {mockCatalog});
77
78 final ArrayList<String[]> mockSchemaCache = new ArrayList<String[]>();
79 final String mockSchema = mockCatalog + "/schema";
80 mockSchemaCache.add(new String[] {mockSchema});
81
82 final String mockTable = "table";
83 final String mockColumn = "column";
84
85 final String mockStatement = "select * from " + mockCatalog + ".\"" + mockSchema + "\".\"" + mockTable + "\" where \"" + mockTable + "." + mockColumn + "\" = ?";
86 final OlapPreparedStatement olapPreparedStatement = createMock(null, mockStatement,
87 mockCatalogCache, mockSchemaCache, mockTable, mockColumn);
88 assertNotNull(olapPreparedStatement);
89 assertNotNull(olapPreparedStatement.getConnection());
90 assertNull(((OlapConnection) olapPreparedStatement.getConnection()).getXmlaConn().getLogin());
91
92 olapPreparedStatement.setString(1, "mockParam");
93
94
95
96 }
97
98 public void testExecuteEmptySql() throws Exception {
99 final OlapPreparedStatement ps = createMock(XmlaLoginTest.createMock("auser", null),
100 "", null, null, null, null);
101 try {
102 ps.executeQuery();
103 fail("invalid statement sql should have failed.");
104 } catch (SQLException e) {
105 assertTrue(e.getMessage().startsWith("Encountered \"<EOF>\" at line 0, column 0."));
106 }
107
108
109 OlapResultSetTest.assertEmptyResultSet(ps.executeQuery(""));
110 }
111
112 }