METAMODEL-1205: Fixed CassandraUnit, Guava, Hadoop for JDK9+
[metamodel.git] / jdbc / src / test / java / org / apache / metamodel / jdbc / dialects / OracleQueryRewriterTest.java
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
3 * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
4 * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
5 * License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11 * specific language governing permissions and limitations under the License.
12 */
13 package org.apache.metamodel.jdbc.dialects;
14
15 import static org.apache.metamodel.jdbc.JdbcDataContext.DATABASE_PRODUCT_ORACLE;
16 import static org.junit.Assert.assertEquals;
17
18 import java.sql.SQLException;
19
20 import org.apache.metamodel.jdbc.JdbcDataContext;
21 import org.apache.metamodel.query.FilterItem;
22 import org.apache.metamodel.query.OperatorType;
23 import org.apache.metamodel.query.Query;
24 import org.apache.metamodel.query.SelectItem;
25 import org.apache.metamodel.schema.MutableColumn;
26 import org.apache.metamodel.schema.MutableSchema;
27 import org.apache.metamodel.schema.MutableTable;
28 import org.easymock.EasyMock;
29 import org.junit.Assert;
30 import org.junit.Before;
31 import org.junit.Test;
32
33 public class OracleQueryRewriterTest {
34 private MutableTable table;
35 private MutableColumn column;
36 private JdbcDataContext mockContext;
37 private IQueryRewriter qr;
38
39 @Before
40 public void setUp() throws Exception {
41 table = new MutableTable("foo");
42 table.setSchema(new MutableSchema("MY_SCHEMA"));
43 table.setQuote("\"");
44
45 column = new MutableColumn("bar");
46 column.setQuote("\"");
47 column.setTable(table);
48
49 mockContext = EasyMock.createMock(JdbcDataContext.class);
50 setMetaData(DATABASE_PRODUCT_ORACLE, "R12.1.1.1");
51 qr = new OracleQueryRewriter(mockContext);
52 }
53
54 @Test
55 public void testReplaceEmptyStringWithNull() throws Exception {
56 final String alias = "alias";
57 SelectItem selectItem = new SelectItem("expression", alias);
58 final FilterItem filterItem = new FilterItem(selectItem, OperatorType.DIFFERENT_FROM, "");
59 final String rewrittenValue = qr.rewriteFilterItem(filterItem);
60 final String expectedValue = alias + " IS NOT NULL";
61
62 assertEquals(expectedValue, rewrittenValue);
63 }
64
65 @Test
66 public void testOffsetFetchConstruct() {
67 final int offset = 1000;
68 final int rows = 100;
69
70 final String offsetClause = " OFFSET " + (offset - 1) + " ROWS";
71 final String fetchClause = " FETCH NEXT " + rows + " ROWS ONLY";
72
73 final Query query = new Query().from(table).select(column);
74 Assert.assertEquals("There shouldn't be OFFSET-FETCH clause.", query.toSql(), qr.rewriteQuery(query));
75
76 query.setFirstRow(offset);
77 Assert.assertEquals("Wrong or missing OFFSET clause.", query.toSql() + offsetClause, qr.rewriteQuery(query));
78
79 query.setMaxRows(rows);
80 Assert.assertEquals("Wrong or missing OFFSET and FETCH clauses.", query.toSql() + offsetClause + fetchClause,
81 qr.rewriteQuery(query));
82
83 query.setFirstRow(null);
84 Assert.assertEquals("Wrong or missing FETCH clause.", query.toSql() + fetchClause, qr.rewriteQuery(query));
85 }
86
87 @Test
88 public void testOffsetFetchVersionCheck() throws SQLException {
89 setMetaData(DATABASE_PRODUCT_ORACLE, "10.1.1.1");
90
91 Query query = new Query().from(table).select(column).setFirstRow(1000).setMaxRows(100);
92 Assert.assertEquals("The query shouldn't be rewritten.", query.toSql(), qr.rewriteQuery(query));
93 }
94
95 @Test
96 public void testOffsetFetchVersionIsNull() throws SQLException {
97 setMetaData(DATABASE_PRODUCT_ORACLE, null);
98
99 Query query = new Query().from(table).select(column).setFirstRow(1000).setMaxRows(100);
100 Assert.assertEquals("The query shouldn't be rewritten.", query.toSql(), qr.rewriteQuery(query));
101 }
102
103 private void setMetaData(String productName, String version) throws SQLException {
104 EasyMock.reset(mockContext);
105
106 EasyMock.expect(mockContext.getDatabaseProductName()).andReturn(productName).anyTimes();
107 EasyMock.expect(mockContext.getDatabaseVersion()).andReturn(version).anyTimes();
108 EasyMock.expect(mockContext.getIdentifierQuoteString()).andReturn("quoteString").anyTimes();
109
110 EasyMock.replay(mockContext);
111 }
112 }