~brian-thomason/+junk/ha-jdbc

« back to all changes in this revision

Viewing changes to test/net/sf/hajdbc/dialect/TestOracleDialect.java

  • Committer: Brian Thomason
  • Date: 2011-12-20 17:34:21 UTC
  • Revision ID: brian.thomason@canonical.com-20111220173421-p9jg95iq91jgdihh
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * HA-JDBC: High-Availability JDBC
 
3
 * Copyright (c) 2004-2007 Paul Ferraro
 
4
 * 
 
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.
 
9
 * 
 
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 
 
13
 * for more details.
 
14
 * 
 
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
 
18
 * 
 
19
 * Contact: ferraro@users.sourceforge.net
 
20
 */
 
21
package net.sf.hajdbc.dialect;
 
22
 
 
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;
 
30
 
 
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;
 
36
 
 
37
import org.easymock.EasyMock;
 
38
import org.testng.annotations.DataProvider;
 
39
import org.testng.annotations.Test;
 
40
 
 
41
/**
 
42
 * @author Paul Ferraro
 
43
 */
 
44
@SuppressWarnings("nls")
 
45
@Test
 
46
public class TestOracleDialect extends TestStandardDialect
 
47
{
 
48
        public TestOracleDialect()
 
49
        {
 
50
                super(new OracleDialect());
 
51
        }
 
52
 
 
53
        /**
 
54
         * @see net.sf.hajdbc.dialect.TestStandardDialect#testGetCreateForeignKeyConstraintSQL()
 
55
         */
 
56
        @Override
 
57
        public void testGetCreateForeignKeyConstraintSQL() throws SQLException
 
58
        {
 
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);
 
68
                
 
69
                String result = this.getCreateForeignKeyConstraintSQL(key);
 
70
                
 
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;
 
72
        }
 
73
 
 
74
        /**
 
75
         * @see net.sf.hajdbc.dialect.TestStandardDialect#testGetSequences()
 
76
         */
 
77
        @Override
 
78
        public void testGetSequences() throws SQLException
 
79
        {
 
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);
 
84
                
 
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);
 
93
                
 
94
                statement.close();
 
95
                
 
96
                EasyMock.replay(metaData, connection, statement, resultSet);
 
97
                
 
98
                Collection<QualifiedName> result = this.getSequences(metaData);
 
99
                
 
100
                EasyMock.verify(metaData, connection, statement, resultSet);
 
101
                
 
102
                assert result.size() == 2 : result.size();
 
103
                
 
104
                Iterator<QualifiedName> iterator = result.iterator();
 
105
                QualifiedName sequence = iterator.next();
 
106
                String schema = sequence.getSchema();
 
107
                String name = sequence.getName();
 
108
                
 
109
                assert schema == null : schema;
 
110
                assert name.equals("sequence1") : name;
 
111
                
 
112
                sequence = iterator.next();
 
113
                schema = sequence.getSchema();
 
114
                name = sequence.getName();
 
115
                
 
116
                assert schema == null : schema;
 
117
                assert name.equals("sequence2") : name;
 
118
        }
 
119
 
 
120
        /**
 
121
         * @see net.sf.hajdbc.dialect.TestStandardDialect#testGetSimpleSQL()
 
122
         */
 
123
        @Override
 
124
        public void testGetSimpleSQL() throws SQLException
 
125
        {
 
126
                String result = this.getSimpleSQL();
 
127
                
 
128
                assert result.equals("SELECT CURRENT_TIMESTAMP FROM DUAL") : result;
 
129
        }
 
130
 
 
131
        /**
 
132
         * @see net.sf.hajdbc.dialect.TestStandardDialect#testGetTruncateTableSQL()
 
133
         */
 
134
        @Override
 
135
        public void testGetTruncateTableSQL() throws SQLException
 
136
        {
 
137
                TableProperties table = EasyMock.createStrictMock(TableProperties.class);
 
138
                
 
139
                EasyMock.expect(table.getName()).andReturn("table");
 
140
                
 
141
                EasyMock.replay(table);
 
142
                
 
143
                String result = this.getTruncateTableSQL(table);
 
144
                
 
145
                EasyMock.verify(table);
 
146
                
 
147
                assert result.equals("TRUNCATE TABLE table") : result;
 
148
        }
 
149
 
 
150
        @Override
 
151
        @DataProvider(name = "sequence-sql")
 
152
        Object[][] sequenceSQLProvider()
 
153
        {
 
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" },
 
164
                };
 
165
        }
 
166
        
 
167
        /**
 
168
         * @see net.sf.hajdbc.dialect.TestStandardDialect#testParseInsertTable(java.lang.String)
 
169
         */
 
170
        @Override
 
171
        @Test(dataProvider = "insert-table-sql")
 
172
        public void testParseInsertTable(String sql) throws SQLException
 
173
        {
 
174
                String result = this.parseInsertTable(sql);
 
175
                
 
176
                assert (result == null) : result;
 
177
        }
 
178
        
 
179
        /**
 
180
         * @see net.sf.hajdbc.dialect.TestStandardDialect#testGetNextSequenceValueSQL()
 
181
         */
 
182
        @Override
 
183
        public void testGetNextSequenceValueSQL() throws SQLException
 
184
        {
 
185
                SequenceProperties sequence = EasyMock.createStrictMock(SequenceProperties.class);
 
186
                
 
187
                EasyMock.expect(sequence.getName()).andReturn("sequence");
 
188
                
 
189
                EasyMock.replay(sequence);
 
190
                
 
191
                String result = this.getNextSequenceValueSQL(sequence);
 
192
 
 
193
                EasyMock.verify(sequence);
 
194
                
 
195
                assert result.equals("SELECT sequence.NEXTVAL FROM DUAL") : result;
 
196
        }
 
197
}