diff --git a/jOOQ/src/test/java/org/jooq/test/BasicTest.java b/jOOQ/src/test/java/org/jooq/test/BasicTest.java index 4b4512b197..a046aa4668 100644 --- a/jOOQ/src/test/java/org/jooq/test/BasicTest.java +++ b/jOOQ/src/test/java/org/jooq/test/BasicTest.java @@ -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("test", TestDataType.INTEGER_TYPE) { + Field f = new CustomField("test", SQLDataType.INTEGER) { private static final long serialVersionUID = 1L; @Override diff --git a/jOOQ/src/test/java/org/jooq/test/MockTest.java b/jOOQ/src/test/java/org/jooq/test/MockTest.java new file mode 100644 index 0000000000..d429990815 --- /dev/null +++ b/jOOQ/src/test/java/org/jooq/test/MockTest.java @@ -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 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 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 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 = 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 result1 = create.newResult(TABLE1); + Result 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())); + } + } +} diff --git a/jOOQ/src/test/java/org/jooq/test/data/Table1.java b/jOOQ/src/test/java/org/jooq/test/data/Table1.java index e96f2ac1eb..c66c4a5d69 100644 --- a/jOOQ/src/test/java/org/jooq/test/data/Table1.java +++ b/jOOQ/src/test/java/org/jooq/test/data/Table1.java @@ -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 { public static final Table TABLE1 = new Table1(); - public static final TableField FIELD_ID1 = createField("ID1", TestDataType.INTEGER_TYPE, TABLE1); - public static final TableField FIELD_NAME1 = createField("NAME1", TestDataType.STRING_TYPE, TABLE1); - public static final TableField FIELD_DATE1 = createField("DATE1", TestDataType.DATE_TYPE, TABLE1); + public static final TableField FIELD_ID1 = createField("ID1", SQLDataType.INTEGER, TABLE1); + public static final TableField FIELD_NAME1 = createField("NAME1", SQLDataType.VARCHAR, TABLE1); + public static final TableField FIELD_DATE1 = createField("DATE1", SQLDataType.DATE, TABLE1); public Table1() { super("TABLE1"); diff --git a/jOOQ/src/test/java/org/jooq/test/data/Table2.java b/jOOQ/src/test/java/org/jooq/test/data/Table2.java index 2e389c9039..92a0bfa3bd 100644 --- a/jOOQ/src/test/java/org/jooq/test/data/Table2.java +++ b/jOOQ/src/test/java/org/jooq/test/data/Table2.java @@ -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 { public static final Table TABLE2 = new Table2(); - public static final TableField FIELD_ID2 = createField("ID2", TestDataType.INTEGER_TYPE, TABLE2); - public static final TableField FIELD_NAME2 = createField("NAME2", TestDataType.STRING_TYPE, TABLE2); - public static final TableField FIELD_DATE2 = createField("DATE2", TestDataType.DATE_TYPE, TABLE2); + public static final TableField FIELD_ID2 = createField("ID2", SQLDataType.INTEGER, TABLE2); + public static final TableField FIELD_NAME2 = createField("NAME2", SQLDataType.VARCHAR, TABLE2); + public static final TableField FIELD_DATE2 = createField("DATE2", SQLDataType.DATE, TABLE2); public Table2() { super("TABLE2"); diff --git a/jOOQ/src/test/java/org/jooq/test/data/Table3.java b/jOOQ/src/test/java/org/jooq/test/data/Table3.java index ec45e1f75e..209f2d36df 100644 --- a/jOOQ/src/test/java/org/jooq/test/data/Table3.java +++ b/jOOQ/src/test/java/org/jooq/test/data/Table3.java @@ -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 { public static final Table TABLE3 = new Table3(); - public static final TableField FIELD_ID3 = createField("ID3", TestDataType.INTEGER_TYPE, TABLE3); - public static final TableField FIELD_NAME3 = createField("NAME3", TestDataType.STRING_TYPE, TABLE3); - public static final TableField FIELD_DATE3 = createField("DATE3", TestDataType.DATE_TYPE, TABLE3); + public static final TableField FIELD_ID3 = createField("ID3", SQLDataType.INTEGER, TABLE3); + public static final TableField FIELD_NAME3 = createField("NAME3", SQLDataType.VARCHAR, TABLE3); + public static final TableField FIELD_DATE3 = createField("DATE3", SQLDataType.DATE, TABLE3); public Table3() { super("TABLE3"); diff --git a/jOOQ/src/test/java/org/jooq/test/data/Table4.java b/jOOQ/src/test/java/org/jooq/test/data/Table4.java index 3fc5984018..958cd7037e 100644 --- a/jOOQ/src/test/java/org/jooq/test/data/Table4.java +++ b/jOOQ/src/test/java/org/jooq/test/data/Table4.java @@ -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 { public static final Table TABLE4 = new Table4(); - public static final TableField FIELD_ID4 = createField("ID4", TestDataType.INTEGER_TYPE, TABLE4); - public static final TableField FIELD_NAME4 = createField("NAME4", TestDataType.STRING_TYPE, TABLE4); - public static final TableField FIELD_ARRAY4 = createField("ARRAY4", TestDataType.ARRAY_TYPE, TABLE4); + public static final TableField FIELD_ID4 = createField("ID4", SQLDataType.INTEGER, TABLE4); + public static final TableField FIELD_NAME4 = createField("NAME4", SQLDataType.VARCHAR, TABLE4); + public static final TableField FIELD_ARRAY4 = createField("ARRAY4", SQLDataType.OTHER.getArrayDataType(), TABLE4); public Table4() { super("TABLE4"); diff --git a/jOOQ/src/test/java/org/jooq/test/data/TestDataType.java b/jOOQ/src/test/java/org/jooq/test/data/TestDataType.java deleted file mode 100644 index 58b651aaba..0000000000 --- a/jOOQ/src/test/java/org/jooq/test/data/TestDataType.java +++ /dev/null @@ -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 extends DefaultDataType { - - /** - * Generated UID - */ - private static final long serialVersionUID = 7621282509163949636L; - - public static final DataType INTEGER_TYPE = new TestDataType(Integer.class); - public static final DataType STRING_TYPE = new TestDataType(String.class); - public static final DataType DATE_TYPE = new TestDataType(Date.class); - public static final DataType ARRAY_TYPE = new TestDataType(Object[].class); - - protected TestDataType(Class type) { - super(SQLDialect.ORACLE, type, type.getSimpleName()); - } -}