2
* HA-JDBC: High-Availability JDBC
3
* Copyright (c) 2004-2007 Paul Ferraro
5
* This library is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU Lesser General Public License as published by the
7
* Free Software Foundation; either version 2.1 of the License, or (at your
8
* option) any later version.
10
* This library is distributed in the hope that it will be useful, but WITHOUT
11
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15
* You should have received a copy of the GNU Lesser General Public License
16
* along with this library; if not, write to the Free Software Foundation,
17
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
* Contact: ferraro@users.sourceforge.net
21
package net.sf.hajdbc.dialect;
23
import java.sql.Connection;
24
import java.sql.DatabaseMetaData;
25
import java.sql.ResultSet;
26
import java.sql.SQLException;
27
import java.sql.Statement;
28
import java.util.Collection;
29
import java.util.Iterator;
31
import net.sf.hajdbc.ForeignKeyConstraint;
32
import net.sf.hajdbc.QualifiedName;
33
import net.sf.hajdbc.SequenceProperties;
34
import net.sf.hajdbc.TableProperties;
35
import net.sf.hajdbc.cache.ForeignKeyConstraintImpl;
37
import org.easymock.EasyMock;
38
import org.testng.annotations.DataProvider;
39
import org.testng.annotations.Test;
42
* @author Paul Ferraro
44
@SuppressWarnings("nls")
46
public class TestOracleDialect extends TestStandardDialect
48
public TestOracleDialect()
50
super(new OracleDialect());
54
* @see net.sf.hajdbc.dialect.TestStandardDialect#testGetCreateForeignKeyConstraintSQL()
57
public void testGetCreateForeignKeyConstraintSQL() throws SQLException
59
ForeignKeyConstraint key = new ForeignKeyConstraintImpl("name", "table");
60
key.getColumnList().add("column1");
61
key.getColumnList().add("column2");
62
key.setForeignTable("foreign_table");
63
key.getForeignColumnList().add("foreign_column1");
64
key.getForeignColumnList().add("foreign_column2");
65
key.setDeferrability(DatabaseMetaData.importedKeyInitiallyDeferred);
66
key.setDeleteRule(DatabaseMetaData.importedKeyCascade);
67
key.setUpdateRule(DatabaseMetaData.importedKeyRestrict);
69
String result = this.getCreateForeignKeyConstraintSQL(key);
71
assert result.equals("ALTER TABLE table ADD CONSTRAINT name FOREIGN KEY (column1, column2) REFERENCES foreign_table (foreign_column1, foreign_column2) ON DELETE CASCADE") : result;
75
* @see net.sf.hajdbc.dialect.TestStandardDialect#testGetSequences()
78
public void testGetSequences() throws SQLException
80
DatabaseMetaData metaData = EasyMock.createStrictMock(DatabaseMetaData.class);
81
Connection connection = EasyMock.createStrictMock(Connection.class);
82
Statement statement = EasyMock.createStrictMock(Statement.class);
83
ResultSet resultSet = EasyMock.createStrictMock(ResultSet.class);
85
EasyMock.expect(metaData.getConnection()).andReturn(connection);
86
EasyMock.expect(connection.createStatement()).andReturn(statement);
87
EasyMock.expect(statement.executeQuery("SELECT SEQUENCE_NAME FROM USER_SEQUENCES")).andReturn(resultSet);
88
EasyMock.expect(resultSet.next()).andReturn(true);
89
EasyMock.expect(resultSet.getString(1)).andReturn("sequence1");
90
EasyMock.expect(resultSet.next()).andReturn(true);
91
EasyMock.expect(resultSet.getString(1)).andReturn("sequence2");
92
EasyMock.expect(resultSet.next()).andReturn(false);
96
EasyMock.replay(metaData, connection, statement, resultSet);
98
Collection<QualifiedName> result = this.getSequences(metaData);
100
EasyMock.verify(metaData, connection, statement, resultSet);
102
assert result.size() == 2 : result.size();
104
Iterator<QualifiedName> iterator = result.iterator();
105
QualifiedName sequence = iterator.next();
106
String schema = sequence.getSchema();
107
String name = sequence.getName();
109
assert schema == null : schema;
110
assert name.equals("sequence1") : name;
112
sequence = iterator.next();
113
schema = sequence.getSchema();
114
name = sequence.getName();
116
assert schema == null : schema;
117
assert name.equals("sequence2") : name;
121
* @see net.sf.hajdbc.dialect.TestStandardDialect#testGetSimpleSQL()
124
public void testGetSimpleSQL() throws SQLException
126
String result = this.getSimpleSQL();
128
assert result.equals("SELECT CURRENT_TIMESTAMP FROM DUAL") : result;
132
* @see net.sf.hajdbc.dialect.TestStandardDialect#testGetTruncateTableSQL()
135
public void testGetTruncateTableSQL() throws SQLException
137
TableProperties table = EasyMock.createStrictMock(TableProperties.class);
139
EasyMock.expect(table.getName()).andReturn("table");
141
EasyMock.replay(table);
143
String result = this.getTruncateTableSQL(table);
145
EasyMock.verify(table);
147
assert result.equals("TRUNCATE TABLE table") : result;
151
@DataProvider(name = "sequence-sql")
152
Object[][] sequenceSQLProvider()
154
return new Object[][] {
155
new Object[] { "SELECT success.nextval" },
156
new Object[] { "SELECT success.currval" },
157
new Object[] { "SELECT success.nextval, * FROM table" },
158
new Object[] { "SELECT success.currval, * FROM table" },
159
new Object[] { "INSERT INTO table VALUES (success.nextval, 0)" },
160
new Object[] { "INSERT INTO table VALUES (success.currval, 0)" },
161
new Object[] { "UPDATE table SET id = success.nextval" },
162
new Object[] { "UPDATE table SET id = success.currval" },
163
new Object[] { "SELECT * FROM table" },
168
* @see net.sf.hajdbc.dialect.TestStandardDialect#testParseInsertTable(java.lang.String)
171
@Test(dataProvider = "insert-table-sql")
172
public void testParseInsertTable(String sql) throws SQLException
174
String result = this.parseInsertTable(sql);
176
assert (result == null) : result;
180
* @see net.sf.hajdbc.dialect.TestStandardDialect#testGetNextSequenceValueSQL()
183
public void testGetNextSequenceValueSQL() throws SQLException
185
SequenceProperties sequence = EasyMock.createStrictMock(SequenceProperties.class);
187
EasyMock.expect(sequence.getName()).andReturn("sequence");
189
EasyMock.replay(sequence);
191
String result = this.getNextSequenceValueSQL(sequence);
193
EasyMock.verify(sequence);
195
assert result.equals("SELECT sequence.NEXTVAL FROM DUAL") : result;