/* * 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); } }