1   /*
2    * PRELYTIS.
3    * Copyright 2007, PRELYTIS S.A., and individual contributors
4    * as indicated by the @author tags. See the copyright.txt file in the
5    * distribution for a full listing of individual contributors.
6    *
7    * This is free software; you can redistribute it and/or modify it
8    * under the terms of the GNU Lesser General Public License as
9    * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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   * @author Dan Rollo
35   * Date: Jul 17, 2008
36   * Time: 12:53:41 AM
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          // @todo Do we need to override OlapStatement#execute(String) methods in OlapPreparedStatement?
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          // @todo Implement getParameterMetaData()?
94          //assertNotNull(olapPreparedStatement.getParameterMetaData());
95          // @todo Why is OlapPreparedStatemt.prepareSql() never called?
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         // @todo Do we need to override OlapStatement#execute(String) methods in OlapPreparedStatement?
109         OlapResultSetTest.assertEmptyResultSet(ps.executeQuery(""));
110     }
111 
112 }