[#2202] Add Mock JDBC objects for unit testing with jOOQ - Added
some unit tests
This commit is contained in:
parent
f782f27b00
commit
d56c0d71a2
@ -121,8 +121,8 @@ import org.jooq.conf.RenderNameStyle;
|
||||
import org.jooq.impl.CustomCondition;
|
||||
import org.jooq.impl.CustomField;
|
||||
import org.jooq.impl.Factory;
|
||||
import org.jooq.impl.SQLDataType;
|
||||
import org.jooq.test.data.Table1Record;
|
||||
import org.jooq.test.data.TestDataType;
|
||||
|
||||
import org.jmock.Expectations;
|
||||
import org.junit.Test;
|
||||
@ -1042,7 +1042,7 @@ public class BasicTest extends AbstractTest {
|
||||
|
||||
@Test
|
||||
public void testCustomField() throws Exception {
|
||||
Field<?> f = new CustomField<Integer>("test", TestDataType.INTEGER_TYPE) {
|
||||
Field<?> f = new CustomField<Integer>("test", SQLDataType.INTEGER) {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
|
||||
198
jOOQ/src/test/java/org/jooq/test/MockTest.java
Normal file
198
jOOQ/src/test/java/org/jooq/test/MockTest.java
Normal file
@ -0,0 +1,198 @@
|
||||
/**
|
||||
* Copyright (c) 2009-2013, Lukas Eder, lukas.eder@gmail.com
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed to you under the Apache License, Version 2.0
|
||||
* (the "License"); You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* . Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* . Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* . Neither the name "jOOQ" nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software without
|
||||
* specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package org.jooq.test;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static junit.framework.Assert.assertNull;
|
||||
import static org.jooq.test.data.Table1.FIELD_ID1;
|
||||
import static org.jooq.test.data.Table1.FIELD_NAME1;
|
||||
import static org.jooq.test.data.Table1.TABLE1;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import org.jooq.Record;
|
||||
import org.jooq.Result;
|
||||
import org.jooq.SQLDialect;
|
||||
import org.jooq.impl.Executor;
|
||||
import org.jooq.test.data.Table1;
|
||||
import org.jooq.test.data.Table1Record;
|
||||
import org.jooq.tools.jdbc.MockConnection;
|
||||
import org.jooq.tools.jdbc.MockDataProvider;
|
||||
import org.jooq.tools.jdbc.MockExecuteContext;
|
||||
import org.jooq.tools.jdbc.MockResult;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* This test suite contains tests for the JDBC mock implementations.
|
||||
*
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
public class MockTest extends AbstractTest {
|
||||
|
||||
@Test
|
||||
public void testEmptyResult() {
|
||||
Executor e = new Executor(new MockConnection(new EmptyResult()), SQLDialect.H2);
|
||||
Result<Record> result = e.fetch("select ?, ? from dual", 1, 2);
|
||||
|
||||
assertEquals(0, result.size());
|
||||
assertEquals(3, result.fields().length);
|
||||
for (int i = 0; i < 3; i++) {
|
||||
assertEquals(TABLE1.field(i).getName(), result.field(i).getName());
|
||||
assertEquals(TABLE1.field(i).getType(), result.field(i).getType());
|
||||
}
|
||||
}
|
||||
|
||||
class EmptyResult extends AbstractResult {
|
||||
@Override
|
||||
public MockResult[] execute(MockExecuteContext ctx) throws SQLException {
|
||||
execute0(ctx);
|
||||
|
||||
return new MockResult[] {
|
||||
new MockResult(0, create.newResult(Table1.TABLE1))
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSingleResult() {
|
||||
Executor e = new Executor(new MockConnection(new SingleResult()), SQLDialect.H2);
|
||||
Result<Record> result = e.fetch("select ?, ? from dual", 1, 2);
|
||||
|
||||
assertEquals(2, result.size());
|
||||
assertEquals(3, result.fields().length);
|
||||
|
||||
for (int i = 0; i < 3; i++) {
|
||||
assertEquals(TABLE1.field(i).getName(), result.field(i).getName());
|
||||
assertEquals(TABLE1.field(i).getType(), result.field(i).getType());
|
||||
}
|
||||
|
||||
assertEquals(1, (int) result.getValue(0, FIELD_ID1));
|
||||
assertEquals(2, (int) result.getValue(1, FIELD_ID1));
|
||||
assertEquals("1", result.getValue(0, FIELD_NAME1));
|
||||
assertEquals("2", result.getValue(1, FIELD_NAME1));
|
||||
assertNull(result.getValue(0, Table1.FIELD_DATE1));
|
||||
assertNull(result.getValue(1, Table1.FIELD_DATE1));
|
||||
}
|
||||
|
||||
class SingleResult extends AbstractResult {
|
||||
@Override
|
||||
public MockResult[] execute(MockExecuteContext ctx) throws SQLException {
|
||||
execute0(ctx);
|
||||
|
||||
Result<Table1Record> result = create.newResult(TABLE1);
|
||||
result.add(create.newRecord(TABLE1));
|
||||
result.add(create.newRecord(TABLE1));
|
||||
|
||||
result.get(0).setValue(FIELD_ID1, 1);
|
||||
result.get(1).setValue(FIELD_ID1, 2);
|
||||
result.get(0).setValue(FIELD_NAME1, "1");
|
||||
result.get(1).setValue(FIELD_NAME1, "2");
|
||||
|
||||
return new MockResult[] {
|
||||
new MockResult(0, result)
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoubleResult() {
|
||||
Executor e = new Executor(new MockConnection(new DoubleResult()), SQLDialect.H2);
|
||||
List<Result<Record>> result = e.fetchMany("select ?, ? from dual", 1, 2);
|
||||
|
||||
assertEquals(2, result.size());
|
||||
assertEquals(1, result.get(0).size());
|
||||
assertEquals(2, result.get(1).size());
|
||||
assertEquals(3, result.get(0).fields().length);
|
||||
assertEquals(3, result.get(1).fields().length);
|
||||
|
||||
for (int j = 0; j < 2; j++) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
assertEquals(TABLE1.field(i).getName(), result.get(j).field(i).getName());
|
||||
assertEquals(TABLE1.field(i).getType(), result.get(j).field(i).getType());
|
||||
}
|
||||
}
|
||||
|
||||
assertEquals(1, (int) result.get(0).getValue(0, FIELD_ID1));
|
||||
assertEquals(2, (int) result.get(1).getValue(0, FIELD_ID1));
|
||||
assertEquals(3, (int) result.get(1).getValue(1, FIELD_ID1));
|
||||
assertEquals("1", result.get(0).getValue(0, FIELD_NAME1));
|
||||
assertEquals("2", result.get(1).getValue(0, FIELD_NAME1));
|
||||
assertEquals("3", result.get(1).getValue(1, FIELD_NAME1));
|
||||
assertNull(result.get(0).getValue(0, Table1.FIELD_DATE1));
|
||||
assertNull(result.get(1).getValue(0, Table1.FIELD_DATE1));
|
||||
assertNull(result.get(1).getValue(1, Table1.FIELD_DATE1));
|
||||
}
|
||||
|
||||
class DoubleResult extends AbstractResult {
|
||||
@Override
|
||||
public MockResult[] execute(MockExecuteContext ctx) throws SQLException {
|
||||
execute0(ctx);
|
||||
|
||||
Result<Table1Record> result1 = create.newResult(TABLE1);
|
||||
Result<Table1Record> result2 = create.newResult(TABLE1);
|
||||
result1.add(create.newRecord(TABLE1));
|
||||
result2.add(create.newRecord(TABLE1));
|
||||
result2.add(create.newRecord(TABLE1));
|
||||
|
||||
result1.get(0).setValue(FIELD_ID1, 1);
|
||||
result2.get(0).setValue(FIELD_ID1, 2);
|
||||
result2.get(1).setValue(FIELD_ID1, 3);
|
||||
result1.get(0).setValue(FIELD_NAME1, "1");
|
||||
result2.get(0).setValue(FIELD_NAME1, "2");
|
||||
result2.get(1).setValue(FIELD_NAME1, "3");
|
||||
|
||||
return new MockResult[] {
|
||||
new MockResult(0, result1),
|
||||
new MockResult(0, result2),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
abstract class AbstractResult implements MockDataProvider {
|
||||
public void execute0(MockExecuteContext ctx) {
|
||||
assertEquals(1, ctx.getBatchSQL().length);
|
||||
assertEquals("select ?, ? from dual", ctx.getBatchSQL()[0]);
|
||||
assertEquals("select ?, ? from dual", ctx.getSQL());
|
||||
|
||||
assertEquals(1, ctx.getBatchBindings().length);
|
||||
assertEquals(asList(1, 2), asList(ctx.getBatchBindings()[0]));
|
||||
assertEquals(asList(1, 2), asList(ctx.getBindings()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -39,6 +39,7 @@ import java.sql.Date;
|
||||
|
||||
import org.jooq.Table;
|
||||
import org.jooq.TableField;
|
||||
import org.jooq.impl.SQLDataType;
|
||||
import org.jooq.impl.TableImpl;
|
||||
|
||||
/**
|
||||
@ -53,9 +54,9 @@ public class Table1 extends TableImpl<Table1Record> {
|
||||
|
||||
public static final Table<Table1Record> TABLE1 = new Table1();
|
||||
|
||||
public static final TableField<Table1Record, Integer> FIELD_ID1 = createField("ID1", TestDataType.INTEGER_TYPE, TABLE1);
|
||||
public static final TableField<Table1Record, String> FIELD_NAME1 = createField("NAME1", TestDataType.STRING_TYPE, TABLE1);
|
||||
public static final TableField<Table1Record, Date> FIELD_DATE1 = createField("DATE1", TestDataType.DATE_TYPE, TABLE1);
|
||||
public static final TableField<Table1Record, Integer> FIELD_ID1 = createField("ID1", SQLDataType.INTEGER, TABLE1);
|
||||
public static final TableField<Table1Record, String> FIELD_NAME1 = createField("NAME1", SQLDataType.VARCHAR, TABLE1);
|
||||
public static final TableField<Table1Record, Date> FIELD_DATE1 = createField("DATE1", SQLDataType.DATE, TABLE1);
|
||||
|
||||
public Table1() {
|
||||
super("TABLE1");
|
||||
|
||||
@ -39,6 +39,7 @@ import java.sql.Date;
|
||||
|
||||
import org.jooq.Table;
|
||||
import org.jooq.TableField;
|
||||
import org.jooq.impl.SQLDataType;
|
||||
import org.jooq.impl.TableImpl;
|
||||
|
||||
/**
|
||||
@ -53,9 +54,9 @@ public class Table2 extends TableImpl<Table2Record> {
|
||||
|
||||
public static final Table<Table2Record> TABLE2 = new Table2();
|
||||
|
||||
public static final TableField<Table2Record, Integer> FIELD_ID2 = createField("ID2", TestDataType.INTEGER_TYPE, TABLE2);
|
||||
public static final TableField<Table2Record, String> FIELD_NAME2 = createField("NAME2", TestDataType.STRING_TYPE, TABLE2);
|
||||
public static final TableField<Table2Record, Date> FIELD_DATE2 = createField("DATE2", TestDataType.DATE_TYPE, TABLE2);
|
||||
public static final TableField<Table2Record, Integer> FIELD_ID2 = createField("ID2", SQLDataType.INTEGER, TABLE2);
|
||||
public static final TableField<Table2Record, String> FIELD_NAME2 = createField("NAME2", SQLDataType.VARCHAR, TABLE2);
|
||||
public static final TableField<Table2Record, Date> FIELD_DATE2 = createField("DATE2", SQLDataType.DATE, TABLE2);
|
||||
|
||||
public Table2() {
|
||||
super("TABLE2");
|
||||
|
||||
@ -39,6 +39,7 @@ import java.sql.Date;
|
||||
|
||||
import org.jooq.Table;
|
||||
import org.jooq.TableField;
|
||||
import org.jooq.impl.SQLDataType;
|
||||
import org.jooq.impl.TableImpl;
|
||||
|
||||
/**
|
||||
@ -53,9 +54,9 @@ public class Table3 extends TableImpl<Table3Record> {
|
||||
|
||||
public static final Table<Table3Record> TABLE3 = new Table3();
|
||||
|
||||
public static final TableField<Table3Record, Integer> FIELD_ID3 = createField("ID3", TestDataType.INTEGER_TYPE, TABLE3);
|
||||
public static final TableField<Table3Record, String> FIELD_NAME3 = createField("NAME3", TestDataType.STRING_TYPE, TABLE3);
|
||||
public static final TableField<Table3Record, Date> FIELD_DATE3 = createField("DATE3", TestDataType.DATE_TYPE, TABLE3);
|
||||
public static final TableField<Table3Record, Integer> FIELD_ID3 = createField("ID3", SQLDataType.INTEGER, TABLE3);
|
||||
public static final TableField<Table3Record, String> FIELD_NAME3 = createField("NAME3", SQLDataType.VARCHAR, TABLE3);
|
||||
public static final TableField<Table3Record, Date> FIELD_DATE3 = createField("DATE3", SQLDataType.DATE, TABLE3);
|
||||
|
||||
public Table3() {
|
||||
super("TABLE3");
|
||||
|
||||
@ -37,6 +37,7 @@ package org.jooq.test.data;
|
||||
|
||||
import org.jooq.Table;
|
||||
import org.jooq.TableField;
|
||||
import org.jooq.impl.SQLDataType;
|
||||
import org.jooq.impl.TableImpl;
|
||||
|
||||
/**
|
||||
@ -51,9 +52,9 @@ public class Table4 extends TableImpl<Table4Record> {
|
||||
|
||||
public static final Table<Table4Record> TABLE4 = new Table4();
|
||||
|
||||
public static final TableField<Table4Record, Integer> FIELD_ID4 = createField("ID4", TestDataType.INTEGER_TYPE, TABLE4);
|
||||
public static final TableField<Table4Record, String> FIELD_NAME4 = createField("NAME4", TestDataType.STRING_TYPE, TABLE4);
|
||||
public static final TableField<Table4Record, Object[]> FIELD_ARRAY4 = createField("ARRAY4", TestDataType.ARRAY_TYPE, TABLE4);
|
||||
public static final TableField<Table4Record, Integer> FIELD_ID4 = createField("ID4", SQLDataType.INTEGER, TABLE4);
|
||||
public static final TableField<Table4Record, String> FIELD_NAME4 = createField("NAME4", SQLDataType.VARCHAR, TABLE4);
|
||||
public static final TableField<Table4Record, Object[]> FIELD_ARRAY4 = createField("ARRAY4", SQLDataType.OTHER.getArrayDataType(), TABLE4);
|
||||
|
||||
public Table4() {
|
||||
super("TABLE4");
|
||||
|
||||
@ -1,62 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2009-2013, Lukas Eder, lukas.eder@gmail.com
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed to you under the Apache License, Version 2.0
|
||||
* (the "License"); You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* . Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* . Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* . Neither the name "jOOQ" nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software without
|
||||
* specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package org.jooq.test.data;
|
||||
|
||||
import java.sql.Date;
|
||||
|
||||
import org.jooq.DataType;
|
||||
import org.jooq.SQLDialect;
|
||||
import org.jooq.impl.DefaultDataType;
|
||||
|
||||
/**
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
public final class TestDataType<T> extends DefaultDataType<T> {
|
||||
|
||||
/**
|
||||
* Generated UID
|
||||
*/
|
||||
private static final long serialVersionUID = 7621282509163949636L;
|
||||
|
||||
public static final DataType<Integer> INTEGER_TYPE = new TestDataType<Integer>(Integer.class);
|
||||
public static final DataType<String> STRING_TYPE = new TestDataType<String>(String.class);
|
||||
public static final DataType<Date> DATE_TYPE = new TestDataType<Date>(Date.class);
|
||||
public static final DataType<Object[]> ARRAY_TYPE = new TestDataType<Object[]>(Object[].class);
|
||||
|
||||
protected TestDataType(Class<T> type) {
|
||||
super(SQLDialect.ORACLE, type, type.getSimpleName());
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user