/* * HA-JDBC: High-Availability JDBC * Copyright (C) 2012 Paul Ferraro * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package net.sf.hajdbc.dialect; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Arrays; import java.util.Collection; import java.util.Iterator; import net.sf.hajdbc.ForeignKeyConstraint; import net.sf.hajdbc.QualifiedName; import net.sf.hajdbc.SequenceProperties; import net.sf.hajdbc.SequencePropertiesFactory; import net.sf.hajdbc.SequenceSupport; import net.sf.hajdbc.TableProperties; import net.sf.hajdbc.dialect.maxdb.MaxDBDialectFactory; /** * @author Paul Ferraro * */ @SuppressWarnings("nls") public class MaxDBDialectTest extends StandardDialectTest { public MaxDBDialectTest() { super(new MaxDBDialectFactory()); } /** * {@inheritDoc} * @see net.sf.hajdbc.dialect.StandardDialectTest#getSequenceSupport() */ @Override public void getSequenceSupport() { assertSame(this.dialect, this.dialect.getSequenceSupport()); } /** * {@inheritDoc} * @see net.sf.hajdbc.dialect.StandardDialectTest#getCreateForeignKeyConstraintSQL() */ @Override public void getCreateForeignKeyConstraintSQL() throws SQLException { QualifiedName table = mock(QualifiedName.class); QualifiedName foreignTable = mock(QualifiedName.class); ForeignKeyConstraint constraint = mock(ForeignKeyConstraint.class); when(table.getDDLName()).thenReturn("table"); when(foreignTable.getDDLName()).thenReturn("foreign_table"); when(constraint.getName()).thenReturn("name"); when(constraint.getTable()).thenReturn(table); when(constraint.getColumnList()).thenReturn(Arrays.asList("column1", "column2")); when(constraint.getForeignTable()).thenReturn(foreignTable); when(constraint.getForeignColumnList()).thenReturn(Arrays.asList("foreign_column1", "foreign_column2")); when(constraint.getDeferrability()).thenReturn(DatabaseMetaData.importedKeyInitiallyDeferred); when(constraint.getDeleteRule()).thenReturn(DatabaseMetaData.importedKeyCascade); when(constraint.getUpdateRule()).thenReturn(DatabaseMetaData.importedKeyRestrict); String result = this.dialect.getCreateForeignKeyConstraintSQL(constraint); assertEquals("ALTER TABLE table ADD CONSTRAINT name FOREIGN KEY (column1, column2) REFERENCES foreign_table (foreign_column1, foreign_column2) ON DELETE CASCADE", result); } /** * {@inheritDoc} * @see net.sf.hajdbc.dialect.StandardDialectTest#getSequences() */ @Override public void getSequences() throws SQLException { SequencePropertiesFactory factory = mock(SequencePropertiesFactory.class); SequenceProperties sequence1 = mock(SequenceProperties.class); SequenceProperties sequence2 = mock(SequenceProperties.class); DatabaseMetaData metaData = mock(DatabaseMetaData.class); Connection connection = mock(Connection.class); Statement statement = mock(Statement.class); ResultSet resultSet = mock(ResultSet.class); when(metaData.getConnection()).thenReturn(connection); when(connection.createStatement()).thenReturn(statement); when(statement.executeQuery("SELECT SEQUENCE_NAME, INCREMENT_BY FROM USER_SEQUENCES")).thenReturn(resultSet); when(resultSet.next()).thenReturn(true).thenReturn(true).thenReturn(false); when(resultSet.getString(1)).thenReturn("sequence1").thenReturn("sequence2"); when(resultSet.getInt(2)).thenReturn(1).thenReturn(2); when(factory.createSequenceProperties(null, "sequence1", 1)).thenReturn(sequence1); when(factory.createSequenceProperties(null, "sequence2", 2)).thenReturn(sequence2); Collection<SequenceProperties> results = this.dialect.getSequenceSupport().getSequences(metaData, factory); verify(statement).close(); assertEquals(2, results.size()); Iterator<SequenceProperties> sequences = results.iterator(); assertSame(sequence1, sequences.next()); assertSame(sequence2, sequences.next()); } /** * {@inheritDoc} * @see net.sf.hajdbc.dialect.StandardDialectTest#getTruncateTableSQL() */ @Override public void getTruncateTableSQL() throws SQLException { TableProperties table = mock(TableProperties.class); QualifiedName name = mock(QualifiedName.class); when(table.getName()).thenReturn(name); when(name.getDMLName()).thenReturn("table"); String result = this.dialect.getTruncateTableSQL(table); assertEquals("TRUNCATE TABLE table", result); } /** * {@inheritDoc} * @see net.sf.hajdbc.dialect.StandardDialectTest#parseSequence() */ @Override public void parseSequence() throws SQLException { SequenceSupport support = this.dialect.getSequenceSupport(); assertEquals("sequence", support.parseSequence("SELECT sequence.nextval")); assertEquals("sequence", support.parseSequence("SELECT sequence.currval")); assertEquals("sequence", support.parseSequence("SELECT sequence.nextval, * FROM table")); assertEquals("sequence", support.parseSequence("SELECT sequence.currval, * FROM table")); assertEquals("sequence", support.parseSequence("INSERT INTO table VALUES (sequence.nextval, 0)")); assertEquals("sequence", support.parseSequence("INSERT INTO table VALUES (sequence.currval, 0)")); assertEquals("sequence", support.parseSequence("UPDATE table SET id = sequence.nextval")); assertEquals("sequence", support.parseSequence("UPDATE table SET id = sequence.currval")); assertNull(support.parseSequence("SELECT NEXT VALUE FOR sequence")); } /** * {@inheritDoc} * @see net.sf.hajdbc.dialect.StandardDialectTest#getNextSequenceValueSQL() */ @Override public void getNextSequenceValueSQL() throws SQLException { SequenceProperties sequence = mock(SequenceProperties.class); QualifiedName name = mock(QualifiedName.class); when(sequence.getName()).thenReturn(name); when(name.getDMLName()).thenReturn("sequence"); String result = this.dialect.getSequenceSupport().getNextSequenceValueSQL(sequence); assertEquals("SELECT sequence.NEXTVAL FROM DUAL", result); } }