001: /*
002: * Copyright (C) 2007 Rob Manning
003: * manningr@users.sourceforge.net
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package net.sourceforge.squirrel_sql.plugins.dbdiff;
020:
021: import static java.sql.Types.INTEGER;
022: import static java.sql.Types.VARCHAR;
023: import static java.util.Arrays.asList;
024: import static org.junit.Assert.assertEquals;
025: import net.sourceforge.squirrel_sql.BaseSQuirreLJUnit4TestCase;
026: import net.sourceforge.squirrel_sql.fw.sql.ISQLDatabaseMetaData;
027: import net.sourceforge.squirrel_sql.fw.sql.TableColumnInfo;
028: import net.sourceforge.squirrel_sql.test.TestUtil;
029:
030: import org.junit.After;
031: import static org.junit.Assert.*;
032: import org.junit.Before;
033: import org.junit.Test;
034:
035: public class ColumnDifferenceTest extends BaseSQuirreLJUnit4TestCase {
036:
037: // Instance under test
038: ColumnDifference columnDifference = null;
039:
040: // Mock Objects
041: ISQLDatabaseMetaData mockMetaData = null;
042: TableColumnInfo mockVarcharCol1Table1 = null;
043: TableColumnInfo mockVarcharCol1Table1Length1000 = null;
044: TableColumnInfo mockIntegerCol1Table1 = null;
045:
046: TableColumnInfo mockVarcharCol2Table1 = null;
047: TableColumnInfo mockVarcharCol2Table1Nullable = null;
048: TableColumnInfo mockVarcharCol2Table1NotNullable = null;
049:
050: TableColumnInfo mockVarcharCol1Table2 = null;
051:
052: @Before
053: public void setUp() throws Exception {
054: // init class under test
055: columnDifference = new ColumnDifference();
056:
057: // init mocks
058: mockMetaData = TestUtil.getEasyMockSQLMetaData("Oracle",
059: "oracle:jdbc:thin@localhost:1521:ORCL", false);
060:
061: String[] columnNames = new String[] { "col1", "col1", "col2" };
062: Integer[] columnTypes = new Integer[] { VARCHAR, INTEGER,
063: VARCHAR };
064:
065: TableColumnInfo[] columns = TestUtil.getEasyMockTableColumns(
066: "doo", "foo", "table1", asList(columnNames),
067: asList(columnTypes));
068:
069: mockVarcharCol1Table1 = columns[0];
070: mockIntegerCol1Table1 = columns[1];
071: mockVarcharCol2Table1 = columns[2];
072:
073: mockVarcharCol1Table2 = TestUtil.getEasyMockTableColumn("doo",
074: "foo", "table2", "col1", VARCHAR);
075:
076: mockVarcharCol1Table1Length1000 = TestUtil
077: .setEasyMockTableColumnInfoSize(mockVarcharCol1Table1,
078: 1000);
079:
080: mockVarcharCol2Table1Nullable = TestUtil
081: .setEasyMockTableColumnInfoNullable(
082: mockVarcharCol2Table1, true);
083: mockVarcharCol2Table1NotNullable = TestUtil
084: .setEasyMockTableColumnInfoNullable(
085: mockVarcharCol2Table1, false);
086:
087: }
088:
089: @After
090: public void tearDown() throws Exception {
091: columnDifference = null;
092: }
093:
094: // setColumns Error conditions
095:
096: @Test(expected=IllegalArgumentException.class)
097: public final void testSetColumnsNullCol1() {
098: columnDifference.setColumns(null, mockIntegerCol1Table1);
099: }
100:
101: @Test(expected=IllegalArgumentException.class)
102: public final void testSetColumnsNullCol2() {
103: columnDifference.setColumns(mockVarcharCol1Table1, null);
104: }
105:
106: @Test(expected=IllegalArgumentException.class)
107: public final void testSetColumnsDifferentColumnName() {
108: columnDifference.setColumns(mockVarcharCol1Table1,
109: mockVarcharCol2Table1);
110: }
111:
112: @Test(expected=IllegalArgumentException.class)
113: public final void testSetColumnsDifferentTableName() {
114: columnDifference.setColumns(mockVarcharCol1Table1,
115: mockVarcharCol1Table2);
116: }
117:
118: // Sunny day
119:
120: @Test
121: public final void testSetColumns() {
122:
123: columnDifference.setColumns(mockVarcharCol1Table1,
124: mockIntegerCol1Table1);
125:
126: assertEquals(10, columnDifference.getCol1Length());
127: assertEquals(10, columnDifference.getCol2Length());
128:
129: assertEquals(VARCHAR, columnDifference.getCol1Type());
130: assertEquals(INTEGER, columnDifference.getCol2Type());
131:
132: assertEquals(true, columnDifference.col1AllowsNull());
133: assertEquals(true, columnDifference.col2AllowsNull());
134:
135: }
136:
137: @Test
138: public final void testSetColumn1() {
139:
140: }
141:
142: @Test
143: public final void testSetColumn2() {
144:
145: }
146:
147: @Test
148: public final void testGetCol1Type() {
149: columnDifference.setColumns(mockVarcharCol1Table1,
150: mockIntegerCol1Table1);
151: assertEquals(VARCHAR, columnDifference.getCol1Type());
152: }
153:
154: @Test
155: public final void testGetCol1Length() {
156:
157: }
158:
159: @Test
160: public final void testCol1AllowsNull() {
161: columnDifference.setColumns(mockVarcharCol2Table1Nullable,
162: mockVarcharCol2Table1NotNullable);
163: assertTrue(columnDifference.col1AllowsNull());
164:
165: columnDifference.setColumns(mockVarcharCol2Table1NotNullable,
166: mockVarcharCol2Table1Nullable);
167: assertFalse(columnDifference.col1AllowsNull());
168: }
169:
170: @Test
171: public final void testGetCol2Type() {
172:
173: }
174:
175: @Test
176: public final void testGetCol2Length() {
177:
178: }
179:
180: @Test
181: public final void testCol2AllowsNull() {
182: columnDifference.setColumns(mockVarcharCol2Table1NotNullable,
183: mockVarcharCol2Table1Nullable);
184: assertTrue(columnDifference.col2AllowsNull());
185:
186: columnDifference.setColumns(mockVarcharCol2Table1Nullable,
187: mockVarcharCol2Table1NotNullable);
188: assertFalse(columnDifference.col2AllowsNull());
189: }
190:
191: @Test
192: public final void testExecuteDifferentTypes() {
193: columnDifference.setColumns(mockVarcharCol1Table1,
194: mockIntegerCol1Table1);
195: assertTrue(columnDifference.execute());
196: }
197:
198: @Test
199: public final void testExecuteDifferentLengths() {
200: columnDifference.setColumns(mockVarcharCol1Table1,
201: mockVarcharCol1Table1Length1000);
202: assertTrue(columnDifference.execute());
203: }
204:
205: @Test
206: public final void testExecuteDifferentNullable() {
207: columnDifference.setColumns(mockVarcharCol2Table1Nullable,
208: mockVarcharCol2Table1NotNullable);
209: assertTrue(columnDifference.execute());
210: }
211:
212: @Test
213: public final void testExecuteColumnMissing() {
214: columnDifference.setCol1Exists(true);
215: columnDifference.setCol2Exists(false);
216: assertTrue(columnDifference.execute());
217: assertTrue(columnDifference.isCol1Exists());
218: assertFalse(columnDifference.isCol2Exists());
219:
220: columnDifference.setCol1Exists(false);
221: columnDifference.setCol2Exists(true);
222: assertTrue(columnDifference.execute());
223: assertFalse(columnDifference.isCol1Exists());
224: assertTrue(columnDifference.isCol2Exists());
225: }
226:
227: @Test
228: public final void testExecuteNoDifferences() {
229: TableColumnInfo mockVarcharCol1Table1Copy = TestUtil
230: .setEasyMockTableColumnInfoType(mockVarcharCol1Table1,
231: VARCHAR);
232: columnDifference.setColumns(mockVarcharCol1Table1Copy,
233: mockVarcharCol1Table1);
234: assertFalse(columnDifference.execute());
235: }
236:
237: @Test
238: public final void testSetGetTableName() {
239: columnDifference.setTableName("table1");
240: assertEquals("table1", columnDifference.getTableName());
241: columnDifference.setTableName("table2");
242: assertEquals("table2", columnDifference.getTableName());
243: }
244:
245: @Test
246: public final void testSetGetColumnName() {
247: columnDifference.setColumnName("column1");
248: assertEquals("column1", columnDifference.getColumnName());
249: columnDifference.setColumnName("column2");
250: assertEquals("column2", columnDifference.getColumnName());
251: }
252:
253: }
|