From 7c279747beb43c7e88633a6228a155a30e6834f7 Mon Sep 17 00:00:00 2001 From: Benjamin Culkin Date: Mon, 27 May 2024 11:38:33 -0400 Subject: Initial import --- .../mock/sql/MockResultSetMetaDataTest.java | 113 +++++++++ .../foundation/mock/sql/MockResultSetTest.java | 263 +++++++++++++++++++++ .../net/israfil/foundation/mock/sql/RowTest.java | 86 +++++++ 3 files changed, 462 insertions(+) create mode 100644 israfil-foundation-testing/src/test/java/net/israfil/foundation/mock/sql/MockResultSetMetaDataTest.java create mode 100644 israfil-foundation-testing/src/test/java/net/israfil/foundation/mock/sql/MockResultSetTest.java create mode 100644 israfil-foundation-testing/src/test/java/net/israfil/foundation/mock/sql/RowTest.java (limited to 'israfil-foundation-testing/src/test/java') diff --git a/israfil-foundation-testing/src/test/java/net/israfil/foundation/mock/sql/MockResultSetMetaDataTest.java b/israfil-foundation-testing/src/test/java/net/israfil/foundation/mock/sql/MockResultSetMetaDataTest.java new file mode 100644 index 0000000..8a2d7c8 --- /dev/null +++ b/israfil-foundation-testing/src/test/java/net/israfil/foundation/mock/sql/MockResultSetMetaDataTest.java @@ -0,0 +1,113 @@ +/* + * Copyright © 2003-2009 Israfil Consulting Services Corporation + * Copyright © 2003-2009 Christian Edward Gruber + * All Rights Reserved + * + * This software is licensed under the Berkeley Standard Distribution license, + * (BSD license), as defined below: + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. 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. + * 3. Neither the name of Israfil Consulting Services 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. + * + * $Id: MockResultSetMetaDataTest.java 130 2006-12-31 23:22:17Z cgruber $ + */ +package net.israfil.foundation.mock.sql; + +import java.sql.SQLException; + +import net.israfil.foundation.mock.sql.MockResultSetMetaData.ColSpec; +import net.israfil.foundation.mock.sql.MockResultSetMetaData.DBType; + +import org.testng.Assert; +import org.testng.annotations.Test; + +/** + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 130 $ + */ +public class MockResultSetMetaDataTest { + + + public MockResultSetMetaDataTest() { + super(); + } + + @Test + public void testResultSetMetaDataCreation() throws SQLException { + String colA = "colA", colB = "colB", colC = "colC"; + String[] cols = { colA, colB, colC }; + MockResultSetMetaData rsmd = new MockResultSetMetaData(cols); + Assert.assertEquals(3,rsmd.getColumnCount()); + Assert.assertEquals(colA,rsmd.getColumnName(1)); + Assert.assertEquals(colB,rsmd.getColumnName(2)); + Assert.assertEquals(colC,rsmd.getColumnName(3)); + } + + @Test + public void testResultSetMetaDataColumnReferencing() throws SQLException { + String colA = "colA", colB = "colB", colC = "colC"; + String[] cols = { colA, colB, colC }; + MockResultSetMetaData rsmd = new MockResultSetMetaData(cols); + + Assert.assertEquals(3,rsmd.getColumnCount()); + + Assert.assertEquals(colA,rsmd.getColumnName(1)); + Assert.assertEquals(colB,rsmd.getColumnName(2)); + Assert.assertEquals(colC,rsmd.getColumnName(3)); + + Assert.assertEquals(1,rsmd.getColumnNumber(colA)); + Assert.assertEquals(2,rsmd.getColumnNumber(colB)); + Assert.assertEquals(3,rsmd.getColumnNumber(colC)); + } + + @Test + public void testResultSetMetaDataCreationWithColDefs() throws SQLException { + ColSpec colA = new ColSpec("colA", DBType.STRING, 12 ); + ColSpec colB = new ColSpec("colB", DBType.INTEGER, 5, 0 ); + ColSpec colC = new ColSpec("colC", DBType.DECIMAL, 3, 5, false); + ColSpec colD = new ColSpec("colD", DBType.STRING, 4, false); + ColSpec[] cols = { colA, colB, colC, colD }; + MockResultSetMetaData rsmd = new MockResultSetMetaData(cols); + Assert.assertEquals(4,rsmd.getColumnCount()); + + Assert.assertEquals(colA.name,rsmd.getColumnName(1)); + Assert.assertEquals(colB.name,rsmd.getColumnName(2)); + Assert.assertEquals(colC.name,rsmd.getColumnName(3)); + Assert.assertEquals(colD.name,rsmd.getColumnName(4)); + + Assert.assertEquals(colA.type.sqltype,rsmd.getColumnType(1)); + Assert.assertEquals(colB.type.sqltype,rsmd.getColumnType(2)); + Assert.assertEquals(colC.type.sqltype,rsmd.getColumnType(3)); + Assert.assertEquals(colD.type.sqltype,rsmd.getColumnType(4)); + + Assert.assertEquals(colA.width,rsmd.getColumnDisplaySize(1)); + Assert.assertEquals(colB.precision,rsmd.getPrecision(2)); + Assert.assertEquals(colB.scale,rsmd.getScale(2)); + Assert.assertEquals(colC.precision,rsmd.getPrecision(3)); + Assert.assertEquals(colC.scale,rsmd.getScale(3)); + Assert.assertEquals(colD.width,rsmd.getColumnDisplaySize(4)); + } + +} diff --git a/israfil-foundation-testing/src/test/java/net/israfil/foundation/mock/sql/MockResultSetTest.java b/israfil-foundation-testing/src/test/java/net/israfil/foundation/mock/sql/MockResultSetTest.java new file mode 100644 index 0000000..7dfcbcb --- /dev/null +++ b/israfil-foundation-testing/src/test/java/net/israfil/foundation/mock/sql/MockResultSetTest.java @@ -0,0 +1,263 @@ +/* + * Copyright © 2003-2009 Israfil Consulting Services Corporation + * Copyright © 2003-2009 Christian Edward Gruber + * All Rights Reserved + * + * This software is licensed under the Berkeley Standard Distribution license, + * (BSD license), as defined below: + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. 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. + * 3. Neither the name of Israfil Consulting Services 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. + * + * $Id: MockResultSetTest.java 130 2006-12-31 23:22:17Z cgruber $ + */ +package net.israfil.foundation.mock.sql; + +import java.io.InputStream; +import java.io.Reader; +import java.lang.reflect.Method; +import java.math.BigDecimal; +import java.sql.Array; +import java.sql.Blob; +import java.sql.Clob; +import java.sql.Date; +import java.sql.Ref; +import java.sql.SQLException; +import java.sql.Time; +import java.sql.Timestamp; +import java.util.Calendar; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import net.israfil.foundation.mock.sql.MockResultSetMetaData.ColSpec; +import net.israfil.foundation.mock.sql.MockResultSetMetaData.DBType; + +import org.testng.Assert; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +/** + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 130 $ + */ +public class MockResultSetTest { + + ColSpec colA, colB, colC, colD, colE, + colF, colG, colH, colI, colJ, + colK; + ColSpec[] cols; + Object[][] data; + MockResultSet mrs; + + public MockResultSetTest() { + super(); + } + + @SuppressWarnings("deprecation") + @BeforeMethod(alwaysRun=true) + public void setUp() throws Exception { + colA = new ColSpec("colA", DBType.STRING, 12 ); + colB = new ColSpec("colB", DBType.INTEGER); + colC = new ColSpec("colC", DBType.SHORT); + colD = new ColSpec("colD", DBType.LONG); + colE = new ColSpec("colE", DBType.BIT); + colF = new ColSpec("colF", DBType.BYTE); + colG = new ColSpec("colG", DBType.FLOAT); + colH = new ColSpec("colH", DBType.DOUBLE); + colI = new ColSpec("colI", DBType.DECIMAL, 3, 5, false); + colJ = new ColSpec("colJ", DBType.TIMESTAMP,true); + colK = new ColSpec("colK", DBType.OBJECT); + cols = new ColSpec[]{ colA, colB, colC, colD, colE}; + + data = new Object[][] { + { "str1", 254, (short)355, 921124432352L, + true, 0x44, 3.1415f, 9.994d, + new BigDecimal(4.45), Calendar.getInstance(), null }, + { "str2", -922, (short)9283, 1910382394511L, + true, 0x94, null, -0.994d, + new BigDecimal(-229444.452322), new Date(2005,04,02), colE }, + { null, 011, (short)4242, 4992830402903L, + false, 0x4a, 9.95d, 0.0d, + new BigDecimal(0.0), null, null }, + }; + + mrs = new MockResultSet(cols,data); + } + + @AfterMethod(alwaysRun=true) + public void tearDown() throws Exception { + colA = colB = colC = colD = colE = null; + data = null; + mrs = null; + } + + /** Should simply not throw an exception */ + @Test + public void testResultSetCreationWithStrings() throws SQLException { + String[] cols2 = { "colA", "colB", "colC" }; + mrs = new MockResultSet(cols2,data); + List dataMap = mrs.getData(); + for (int i = 0; i < data.length ; i++) { + Row row = dataMap.get(i); + Assert.assertEquals(data[i][0], row.get("colA")); + Assert.assertEquals(data[i][1], row.get("colB")); + Assert.assertEquals(data[i][2], row.get("colC")); + } + } + + @Test + public void testResultSetCreationWithNull() throws SQLException { + mrs = new MockResultSet(cols,null); + Assert.assertEquals(0,mrs.getData().size()); + } + + @Test + public void testResultSetAbsoluteNavigation() throws SQLException { + Assert.assertFalse(mrs.absolute(0)); + Assert.assertTrue(mrs.absolute(1)); + Assert.assertTrue(mrs.absolute(2)); + Assert.assertTrue(mrs.absolute(3)); + Assert.assertFalse(mrs.absolute(4)); + Assert.assertFalse(mrs.absolute(5)); + + Assert.assertTrue(mrs.absolute(-1)); + Assert.assertEquals(3,mrs.getRow()); + Assert.assertTrue(mrs.absolute(-2)); + Assert.assertEquals(2,mrs.getRow()); + Assert.assertTrue(mrs.absolute(-3)); + Assert.assertEquals(1,mrs.getRow()); + Assert.assertFalse(mrs.absolute(-4)); + Assert.assertEquals(0,mrs.getRow()); + Assert.assertFalse(mrs.absolute(-5)); + Assert.assertEquals(0,mrs.getRow()); + Assert.assertTrue(mrs.isBeforeFirst()); + + Assert.assertEquals(0,mrs.getRow()); + Assert.assertFalse(mrs.isAfterLast()); + mrs.first(); + Assert.assertEquals(1,mrs.getRow()); + Assert.assertFalse(mrs.isBeforeFirst()); + Assert.assertFalse(mrs.isAfterLast()); + mrs.last(); + Assert.assertEquals(3,mrs.getRow()); + Assert.assertFalse(mrs.isBeforeFirst()); + Assert.assertFalse(mrs.isAfterLast()); + mrs.afterLast(); + Assert.assertEquals(0,mrs.getRow()); + Assert.assertFalse(mrs.isBeforeFirst()); + Assert.assertTrue(mrs.isAfterLast()); + } + + @Test + public void testResultSetRelativeNavigation() throws SQLException { + mrs.beforeFirst(); + Assert.assertTrue(mrs.isBeforeFirst()); + Assert.assertTrue(mrs.next()); + Assert.assertTrue(mrs.next()); + Assert.assertTrue(mrs.next()); + Assert.assertFalse(mrs.next()); + Assert.assertTrue(mrs.isAfterLast()); + Assert.assertTrue(mrs.previous()); + Assert.assertTrue(mrs.previous()); + Assert.assertTrue(mrs.previous()); + Assert.assertFalse(mrs.previous()); + Assert.assertTrue(mrs.isBeforeFirst()); + } + + + + /** + * Test each group of accessors depending on the type. + */ + @Test + public void testResultSetGetOperations() throws Throwable { + String[] ptypes = { "java.lang.String", "int" }; + Class[][] classes = { + { Timestamp.class, Time.class, Date.class }, + { short.class, long.class, int.class, float.class, + double.class, BigDecimal.class, boolean.class, byte.class }, + { String.class }, + { Object.class }, + { byte[].class, Reader.class, Clob.class, Ref.class, + InputStream.class, InputStream.class, Array.class, Blob.class } + }; + String[][] props = { { "Timestamp", "Time", "Date" }, + { "Short", "Long", "Int", "Float", + "Double", "BigDecimal", "Boolean", "Byte" }, + { "String" }, + { "Object" }, + { "Bytes", "CharacterStream", "Clob", "Ref", + "BinaryStream", "AsciiStream", "Array", "Blob" } }; + mrs.beforeFirst(); + System.out.println("Count:"+props.length); + while(mrs.next()) { + for (int colNo = 0 ; colNo < cols.length ; colNo++) { + ColSpec col = cols[colNo]; + for (int i = 0; i < props[col.type.group.value].length ; i++) { + for (String ptype : ptypes) { // try both int and string column access + //System.out.println(col.name + ":col.type.group=" + col.type.group + "(" + col.type.group.value + ")" + ":method=get" + props[col.type.group.value][i] + "(" + ptype + "):data=" + data[mrs.getRow()-1][colNo] ); + try { + // Do this the unsafe way so we don't use DynamicUtil and can avoid the dependency. + Method m = mrs.getClass().getMethod("get"+props[col.type.group.value][i],new Class[] { String.class }); + Object obj = m.invoke(mrs, col.name ); + + Class expectedClass = _boxedTypeEquivalents + .containsKey(classes[col.type.group.value][i]) ? + _boxedTypeEquivalents.get(classes[col.type.group.value][i]) : + classes[col.type.group.value][i]; + Assert.assertEquals(expectedClass,obj.getClass()); + Assert.assertTrue(expectedClass.isAssignableFrom(obj.getClass())); + } catch (ClassCastException e) { + //e.printStackTrace(); + } catch (RuntimeException e) { + if (e.getCause() != null && !e.getCause().getClass().equals(ClassCastException.class)) throw e; + } + + } + } + } + } + } + static Map, Class> _boxedTypeEquivalents = new HashMap, Class>(); + static { + _boxedTypeEquivalents.put(boolean.class,Boolean.class); + _boxedTypeEquivalents.put(int.class,Integer.class); + _boxedTypeEquivalents.put(long.class,Long.class); + _boxedTypeEquivalents.put(short.class,Short.class); + _boxedTypeEquivalents.put(byte.class,Byte.class); + _boxedTypeEquivalents.put(char.class,Character.class); + _boxedTypeEquivalents.put(double.class,Double.class); + _boxedTypeEquivalents.put(float.class,Float.class); + } + + +} + + + + + diff --git a/israfil-foundation-testing/src/test/java/net/israfil/foundation/mock/sql/RowTest.java b/israfil-foundation-testing/src/test/java/net/israfil/foundation/mock/sql/RowTest.java new file mode 100644 index 0000000..215a282 --- /dev/null +++ b/israfil-foundation-testing/src/test/java/net/israfil/foundation/mock/sql/RowTest.java @@ -0,0 +1,86 @@ +/* + * Copyright © 2003-2009 Israfil Consulting Services Corporation + * Copyright © 2003-2009 Christian Edward Gruber + * All Rights Reserved + * + * This software is licensed under the Berkeley Standard Distribution license, + * (BSD license), as defined below: + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. 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. + * 3. Neither the name of Israfil Consulting Services 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. + * + * $Id: RowTest.java 130 2006-12-31 23:22:17Z cgruber $ + */ +package net.israfil.foundation.mock.sql; + +import java.sql.SQLException; +import java.util.Arrays; +import java.util.List; + +import org.testng.Assert; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; + +/** + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 130 $ + */ +public class RowTest { + + Row row; + String[] columns = { "ColumnA", "ColumnB", "ColumnC" }; + MockResultSetMetaData rsmd = new MockResultSetMetaData(columns); + String valc = "Value C"; + List data = Arrays.asList(new Object[] {"Value A", "Value B", null}); + + @BeforeMethod(alwaysRun=true) + public void setUp() throws Exception { + row = new Row(rsmd, data); + } + + @AfterMethod(alwaysRun=true) + public void tearDown() throws Exception { + row = null; + } + + /* Should simply not throw an exception */ + public void testSetWithInt() throws SQLException { + Assert.assertEquals(null,row.get(3)); + row.set(3,valc); + Assert.assertEquals(valc,row.get(3)); + } + + /* Should simply not throw an exception */ + public void testSetWithString() throws SQLException { + Assert.assertEquals(null,row.get("ColumnC")); + row.set("ColumnC",valc); + Assert.assertEquals(valc,row.get("ColumnC")); + } +} + + + + + -- cgit v1.2.3