Index: src/main/java/javax/sql/rowset/serial/SQLInputImpl.java =================================================================== --- src/main/java/javax/sql/rowset/serial/SQLInputImpl.java (revision 534361) +++ src/main/java/javax/sql/rowset/serial/SQLInputImpl.java (working copy) @@ -26,110 +26,403 @@ import java.sql.Clob; import java.sql.Date; import java.sql.Ref; +import java.sql.SQLData; import java.sql.SQLException; import java.sql.SQLInput; +import java.sql.Struct; import java.sql.Time; import java.sql.Timestamp; +import java.util.Map; import org.apache.harmony.luni.util.NotImplementedException; +import org.apache.harmony.sql.internal.nls.Messages; +/** + * A concrete implementation of SQLInput. The readXXX methods will be called + * by SQLData.readSQL, which read different objects such as Array, BigDecimal + * from this SQLInputImpl object. + * + * Different JDBC drivers may have their own implementation of SQLInput and + * won't use this class. + */ public class SQLInputImpl implements SQLInput { + + private Object[] attributes; + private Map> map; + private int readPosition = 0; + + /** + * Constructs a new SQLInputImpl object using an array of attributes and a + * custom name-type map. + * + * @param attributes - + * the array of given attribute objects. + * @param map - + * the UDT(user defined type) name-type map + * @throws SQLException - + * if the attributes or the map is null + */ + public SQLInputImpl(Object[] attributes, Map> map)throws SQLException { + if (null == attributes || null == map) { + throw new SQLException(Messages.getString("sql.34")); //$NON-NLS-1$ + } + this.attributes = attributes; + this.map = map; + } - public Array readArray() throws SQLException, NotImplementedException { - throw new NotImplementedException(); + /** + * {@inheritDoc} + * + * @see java.sql.SQLInput#readArray() + */ + public Array readArray() throws SQLException { + if(readPosition >= attributes.length) { + throw new SQLException(Messages.getString("sql.35")); + } + Object o = attributes[readPosition++]; + if(o == null) { + return null; + } + return (Array) o; } - public InputStream readAsciiStream() throws SQLException, - NotImplementedException { - throw new NotImplementedException(); + /** + * {@inheritDoc} + * + * @see java.sql.SQLInput#readAsciiStream() + */ + public InputStream readAsciiStream() throws SQLException { + if(readPosition >= attributes.length) { + throw new SQLException(Messages.getString("sql.35")); + } + Object o = attributes[readPosition++]; + if(o == null) { + return null; + } + return (InputStream) o; } - public BigDecimal readBigDecimal() throws SQLException, - NotImplementedException { - throw new NotImplementedException(); + /** + * {@inheritDoc} + * + * @see java.sql.SQLInput#readBigDecimal() + */ + public BigDecimal readBigDecimal() throws SQLException { + if(readPosition >= attributes.length) { + throw new SQLException(Messages.getString("sql.35")); + } + Object o = attributes[readPosition++]; + if(o == null) { + return null; + } + return (BigDecimal) o; } - public InputStream readBinaryStream() throws SQLException, - NotImplementedException { - throw new NotImplementedException(); + /** + * {@inheritDoc} + * + * @see java.sql.SQLInput#readBinaryStream() + */ + public InputStream readBinaryStream() throws SQLException { + if(readPosition >= attributes.length) { + throw new SQLException(Messages.getString("sql.35")); + } + Object o = attributes[readPosition++]; + if(o == null) { + return null; + } + return (InputStream) o; } - public Blob readBlob() throws SQLException, NotImplementedException { - throw new NotImplementedException(); + /** + * {@inheritDoc} + * + * @see java.sql.SQLInput#readBlob() + */ + public Blob readBlob() throws SQLException { + if(readPosition >= attributes.length) { + throw new SQLException(Messages.getString("sql.35")); + } + Object o = attributes[readPosition++]; + if(o == null) { + return null; + } + return (Blob) o; } + /** + * {@inheritDoc} + * + * @see java.sql.SQLInput#readBoolean() + */ public boolean readBoolean() throws SQLException, NotImplementedException { - throw new NotImplementedException(); + if(readPosition >= attributes.length) { + throw new SQLException(Messages.getString("sql.35")); + } + Object o = attributes[readPosition++]; + return (Boolean) o; } - public byte readByte() throws SQLException, NotImplementedException { - throw new NotImplementedException(); + /** + * {@inheritDoc} + * + * @see java.sql.SQLInput#readByte() + */ + public byte readByte() throws SQLException { + if(readPosition >= attributes.length) { + throw new SQLException(Messages.getString("sql.35")); + } + Object o = attributes[readPosition++]; + return (Byte) o; } - public byte[] readBytes() throws SQLException, NotImplementedException { - throw new NotImplementedException(); + /** + * {@inheritDoc} + * + * @see java.sql.SQLInput#readBytes() + */ + public byte[] readBytes() throws SQLException { + if(readPosition >= attributes.length) { + throw new SQLException(Messages.getString("sql.35")); + } + Object o = attributes[readPosition++]; + if(o == null) { + return null; + } + return (byte[]) o; } - public Reader readCharacterStream() throws SQLException, - NotImplementedException { - throw new NotImplementedException(); + /** + * {@inheritDoc} + * + * @see java.sql.SQLInput#readCharacterStream() + */ + public Reader readCharacterStream() throws SQLException { + if(readPosition >= attributes.length) { + throw new SQLException(Messages.getString("sql.35")); + } + Object o = attributes[readPosition++]; + if(o == null) { + return null; + } + return (Reader) o; } - public Clob readClob() throws SQLException, NotImplementedException { - throw new NotImplementedException(); + /** + * {@inheritDoc} + * + * @see java.sql.SQLInput#readClob() + */ + public Clob readClob() throws SQLException { + if(readPosition >= attributes.length) { + throw new SQLException(Messages.getString("sql.35")); + } + Object o = attributes[readPosition++]; + if(o == null) { + return null; + } + return (Clob) o; } - public Date readDate() throws SQLException, NotImplementedException { - throw new NotImplementedException(); + /** + * {@inheritDoc} + * + * @see java.sql.SQLInput#readDate() + */ + public Date readDate() throws SQLException { + if(readPosition >= attributes.length) { + throw new SQLException(Messages.getString("sql.35")); + } + Object o = attributes[readPosition++]; + if(o == null) { + return null; + } + return (Date) o; } - public double readDouble() throws SQLException, NotImplementedException { - throw new NotImplementedException(); + /** + * {@inheritDoc} + * + * @see java.sql.SQLInput#readDouble() + */ + public double readDouble() throws SQLException { + if(readPosition >= attributes.length) { + throw new SQLException(Messages.getString("sql.35")); + } + Object o = attributes[readPosition++]; + return (Double) o; } - public float readFloat() throws SQLException, NotImplementedException { - throw new NotImplementedException(); + /** + * {@inheritDoc} + * + * @see java.sql.SQLInput#readFloat() + */ + public float readFloat() throws SQLException { + if(readPosition >= attributes.length) { + throw new SQLException(Messages.getString("sql.35")); + } + Object o = attributes[readPosition++]; + return (Float) o; } - public int readInt() throws SQLException, NotImplementedException { - throw new NotImplementedException(); + /** + * {@inheritDoc} + * + * @see java.sql.SQLInput#readInt() + */ + public int readInt() throws SQLException { + if(readPosition >= attributes.length) { + throw new SQLException(Messages.getString("sql.35")); + } + Object o = attributes[readPosition++]; + return (Integer) o; } - public long readLong() throws SQLException, NotImplementedException { - throw new NotImplementedException(); + /** + * {@inheritDoc} + * + * @see java.sql.SQLInput#readLong() + */ + public long readLong() throws SQLException { + if(readPosition >= attributes.length) { + throw new SQLException(Messages.getString("sql.35")); + } + Object o = attributes[readPosition++]; + return (Long) o; } - public Object readObject() throws SQLException, NotImplementedException { - throw new NotImplementedException(); + /** + * {@inheritDoc} + * + * @see java.sql.SQLInput#readObject() + */ + public Object readObject() throws SQLException { + if(readPosition >= attributes.length) { + throw new SQLException(Messages.getString("sql.35")); + } + Object o = attributes[readPosition++]; + if (o instanceof Struct) { + Struct structuredType = (Struct)o; + String typeName = structuredType.getSQLTypeName(); + Class c = map.get(typeName); + if(c != null) { + try { + SQLData data = (SQLData)c.newInstance(); + SQLInputImpl input = new SQLInputImpl(structuredType.getAttributes(), map); + data.readSQL(input, typeName); + return data; + } catch (IllegalAccessException e) { + throw new SQLException(e.getMessage()); + } catch (InstantiationException e) { + throw new SQLException(e.getMessage()); + } + + } + } + return o; } - public Ref readRef() throws SQLException, NotImplementedException { - throw new NotImplementedException(); + /** + * {@inheritDoc} + * + * @see java.sql.SQLInput#readRef() + */ + public Ref readRef() throws SQLException { + if(readPosition >= attributes.length) { + throw new SQLException(Messages.getString("sql.35")); + } + Object o = attributes[readPosition++]; + if(o == null) { + return null; + } + return (Ref) o; } - public short readShort() throws SQLException, NotImplementedException { - throw new NotImplementedException(); + /** + * {@inheritDoc} + * + * @see java.sql.SQLInput#readShort() + */ + public short readShort() throws SQLException { + if(readPosition >= attributes.length) { + throw new SQLException(Messages.getString("sql.35")); + } + Object o = attributes[readPosition++]; + return (Short) o; } - public String readString() throws SQLException, NotImplementedException { - throw new NotImplementedException(); + /** + * {@inheritDoc} + * + * @see java.sql.SQLInput#readString() + */ + public String readString() throws SQLException { + if(readPosition >= attributes.length) { + throw new SQLException(Messages.getString("sql.35")); + } + Object o = attributes[readPosition++]; + if(o == null) { + return null; + } + return (String) o; } - public Time readTime() throws SQLException, NotImplementedException { - throw new NotImplementedException(); + /** + * {@inheritDoc} + * + * @see java.sql.SQLInput#readTime() + */ + public Time readTime() throws SQLException { + if(readPosition >= attributes.length) { + throw new SQLException(Messages.getString("sql.35")); + } + Object o = attributes[readPosition++]; + if(o == null) { + return null; + } + return (Time) o; } - public Timestamp readTimestamp() throws SQLException, - NotImplementedException { - throw new NotImplementedException(); + /** + * {@inheritDoc} + * + * @see java.sql.SQLInput#readTimestamp() + */ + public Timestamp readTimestamp() throws SQLException { + if(readPosition >= attributes.length) { + throw new SQLException(Messages.getString("sql.35")); + } + Object o = attributes[readPosition++]; + if(o == null) { + return null; + } + return (Timestamp) o; } - public URL readURL() throws SQLException, NotImplementedException { - throw new NotImplementedException(); + /** + * {@inheritDoc} + * + * @see java.sql.SQLInput#readURL() + */ + public URL readURL() throws SQLException { + if(readPosition >= attributes.length) { + throw new SQLException(Messages.getString("sql.35")); + } else { + throw new SQLException(Messages.getString("sql.37")); + } } + /** + * {@inheritDoc} + * + * @see java.sql.SQLInput#wasNull() + */ public boolean wasNull() throws SQLException, NotImplementedException { - throw new NotImplementedException(); + if(readPosition > attributes.length) { + throw new SQLException(Messages.getString("sql.35")); + } + return attributes[readPosition - 1] == null; } } Index: src/test/java/org/apache/harmony/sql/tests/javax/sql/rowset/BaseRowSetTest.java =================================================================== --- src/test/java/org/apache/harmony/sql/tests/javax/sql/rowset/BaseRowSetTest.java (revision 534470) +++ src/test/java/org/apache/harmony/sql/tests/javax/sql/rowset/BaseRowSetTest.java (working copy) @@ -17,12 +17,6 @@ package org.apache.harmony.sql.tests.javax.sql.rowset; -import java.io.ByteArrayInputStream; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.Reader; -import java.io.StringReader; -import java.io.Writer; import java.sql.Array; import java.sql.Blob; import java.sql.Clob; @@ -27,10 +21,8 @@ import java.sql.Blob; import java.sql.Clob; import java.sql.Ref; -import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Types; -import java.util.Map; import javax.sql.rowset.BaseRowSet; import javax.sql.rowset.serial.SerialArray; @@ -225,159 +217,5 @@ super.initParams(); } } - - static class MockArray implements Array { - - public Object getArray() throws SQLException { - return new Object[0]; - } - - public Object getArray(long index, int count) throws SQLException { - return null; - } - - public Object getArray(long index, int count, Map> map) - throws SQLException { - return null; - } - - public Object getArray(Map> map) throws SQLException { - return null; - } - - public int getBaseType() throws SQLException { - return 0; - } - - public String getBaseTypeName() throws SQLException { - return null; - } - - public ResultSet getResultSet() throws SQLException { - return null; - } - - public ResultSet getResultSet(long index, int count) - throws SQLException { - return null; - } - public ResultSet getResultSet(long index, int count, - Map> map) throws SQLException { - return null; - } - - public ResultSet getResultSet(Map> map) - throws SQLException { - return null; - } - - } - - static class MockBlob implements Blob { - - public InputStream getBinaryStream() throws SQLException { - return null; - } - - public byte[] getBytes(long pos, int length) throws SQLException { - return new byte[0]; - } - - public long length() throws SQLException { - return 0; - } - - public long position(Blob pattern, long start) throws SQLException { - return 0; - } - - public long position(byte[] pattern, long start) throws SQLException { - return 0; - } - - public OutputStream setBinaryStream(long pos) throws SQLException { - return null; - } - - public int setBytes(long pos, byte[] theBytes) throws SQLException { - return 0; - } - - public int setBytes(long pos, byte[] theBytes, int offset, int len) - throws SQLException { - return 0; - } - - public void truncate(long len) throws SQLException { - } - } - - static class MockClob implements Clob { - - public Reader characterStreamReader = new StringReader("xys"); - public InputStream asciiInputStream = new ByteArrayInputStream("hello".getBytes()); - - public InputStream getAsciiStream() throws SQLException { - return asciiInputStream; - } - - public Reader getCharacterStream() throws SQLException { - return characterStreamReader; - } - - public String getSubString(long pos, int length) throws SQLException { - return null; - } - - public long length() throws SQLException { - return 3; - } - - public long position(Clob searchstr, long start) throws SQLException { - return 0; - } - - public long position(String searchstr, long start) throws SQLException { - return 0; - } - - public OutputStream setAsciiStream(long pos) throws SQLException { - return null; - } - - public Writer setCharacterStream(long pos) throws SQLException { - return null; - } - - public int setString(long pos, String str) throws SQLException { - return 0; - } - - public int setString(long pos, String str, int offset, int len) - throws SQLException { - return 0; - } - - public void truncate(long len) throws SQLException { - } - } - - public class MockRef implements Ref { - - public String getBaseTypeName() throws SQLException { - return "ref"; - } - - public Object getObject() throws SQLException { - return null; - } - - public Object getObject(Map> map) throws SQLException { - return null; - } - - public void setObject(Object value) throws SQLException { - } - } } Index: src/test/java/org/apache/harmony/sql/tests/javax/sql/rowset/serial/SQLInputImplTest.java =================================================================== --- src/test/java/org/apache/harmony/sql/tests/javax/sql/rowset/serial/SQLInputImplTest.java (revision 534361) +++ src/test/java/org/apache/harmony/sql/tests/javax/sql/rowset/serial/SQLInputImplTest.java (working copy) @@ -17,100 +17,513 @@ package org.apache.harmony.sql.tests.javax.sql.rowset.serial; -import junit.framework.TestCase; +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.io.Reader; +import java.io.StringReader; +import java.math.BigDecimal; +import java.net.MalformedURLException; +import java.net.URL; +import java.sql.Array; +import java.sql.Blob; +import java.sql.Clob; +import java.sql.Date; +import java.sql.Ref; +import java.sql.SQLData; +import java.sql.SQLException; +import java.sql.SQLInput; +import java.sql.SQLOutput; +import java.sql.Struct; +import java.sql.Time; +import java.sql.Timestamp; +import java.util.HashMap; +import java.util.Map; -public class SQLInputImplTest extends TestCase { +import javax.sql.rowset.serial.SQLInputImpl; - public void testReadArray() { +import junit.framework.TestCase; - } +import org.apache.harmony.sql.tests.javax.sql.rowset.MockArray; +import org.apache.harmony.sql.tests.javax.sql.rowset.MockBlob; +import org.apache.harmony.sql.tests.javax.sql.rowset.MockClob; +import org.apache.harmony.sql.tests.javax.sql.rowset.MockRef; - public void testReadAsciiStream() { +public class SQLInputImplTest extends TestCase { + + /** + * @tests {@link javax.sql.rowset.serial.SQLInputImpl#SQLInputImpl(Object[], Map)} + */ + @SuppressWarnings("unchecked") + public void test_Constructor() { + + try { + new SQLInputImpl(null, new HashMap()); + fail("should throw SQLException"); + } catch (SQLException e) { + // expected + } + + try { + new SQLInputImpl(null, null); + fail("should throw SQLException"); + } catch (SQLException e) { + // expected + } + + try { + new SQLInputImpl(new Object[0], null); + fail("should throw SQLException"); + } catch (SQLException e) { + // expected + } } - - public void testReadBigDecimal() { - + + /** + * @tests {@link javax.sql.rowset.serial.SQLInputImpl#readArray()} + */ + public void testReadArray() throws SQLException { + Array array = new MockArray(); + Object[] attributes = new Object[] {array}; + SQLInputImpl impl = new SQLInputImpl(attributes, new HashMap>()); + assertEquals(array, impl.readArray()); + + try { + impl.readArray(); + fail("should throw SQLException"); + } catch (SQLException e) { + // expected + } } - public void testReadBinaryStream() { - + /** + * @tests {@link javax.sql.rowset.serial.SQLInputImpl#readAsciiStream()} + */ + public void testReadAsciiStream() throws SQLException { + InputStream stream = new ByteArrayInputStream("abc".getBytes()); + Object[] attributes = new Object[] {stream}; + SQLInputImpl impl = new SQLInputImpl(attributes, new HashMap>()); + assertEquals(stream, impl.readAsciiStream()); + + try { + impl.readAsciiStream(); + fail("should throw SQLException"); + } catch (SQLException e) { + // expected + } } - public void testReadBlob() { - + /** + * @tests {@link javax.sql.rowset.serial.SQLInputImpl#readBigDecimal()} + */ + public void testReadBigDecimal() throws SQLException { + BigDecimal bd = new BigDecimal("12.5"); + Object[] attributes = new Object[] {bd}; + SQLInputImpl impl = new SQLInputImpl(attributes, new HashMap>()); + assertEquals(bd, impl.readBigDecimal()); + + try { + impl.readBigDecimal(); + fail("should throw SQLException"); + } catch (SQLException e) { + // expected + } } - public void testReadBoolean() { - + /** + * @tests {@link javax.sql.rowset.serial.SQLInputImpl#readBinaryStream()} + */ + public void testReadBinaryStream() throws SQLException { + InputStream stream = new ByteArrayInputStream("abc".getBytes()); + Object[] attributes = new Object[] {stream}; + SQLInputImpl impl = new SQLInputImpl(attributes, new HashMap>()); + assertEquals(stream, impl.readBinaryStream()); } - public void testReadByte() { - + /** + * @tests {@link javax.sql.rowset.serial.SQLInputImpl#readBlob()} + */ + public void testReadBlob() throws SQLException { + Blob blob = new MockBlob(); + Object[] attributes = new Object[] {blob}; + SQLInputImpl impl = new SQLInputImpl(attributes, new HashMap>()); + assertEquals(blob, impl.readBlob()); + + try { + impl.readBlob(); + fail("should throw SQLException"); + } catch (SQLException e) { + // expected + } } - public void testReadBytes() { - + /** + * @tests {@link javax.sql.rowset.serial.SQLInputImpl#readBoolean()} + */ + public void testReadBoolean() throws SQLException { + Object[] attributes = new Object[] {Boolean.TRUE}; + SQLInputImpl impl = new SQLInputImpl(attributes, new HashMap>()); + assertEquals(true, impl.readBoolean()); + + try { + impl.readBoolean(); + fail("should throw SQLException"); + } catch (SQLException e) { + // expected + } } - public void testReadCharacterStream() { - + /** + * @tests {@link javax.sql.rowset.serial.SQLInputImpl#readByte()} + */ + public void testReadByte() throws SQLException { + Object[] attributes = new Object[] {Byte.valueOf("3")}; + SQLInputImpl impl = new SQLInputImpl(attributes, new HashMap>()); + assertEquals((byte)3, impl.readByte()); + + try { + impl.readByte(); + fail("should throw SQLException"); + } catch (SQLException e) { + // expected + } } - public void testReadClob() { - + /** + * @tests {@link javax.sql.rowset.serial.SQLInputImpl#readBytes()} + */ + public void testReadBytes() throws SQLException { + byte[] bytes = new byte[] {1, 2, 3}; + Object[] attributes = new Object[] {bytes}; + SQLInputImpl impl = new SQLInputImpl(attributes, new HashMap>()); + assertEquals(bytes, impl.readBytes()); + + try { + impl.readBytes(); + fail("should throw SQLException"); + } catch (SQLException e) { + // expected + } } - public void testReadDate() { + /** + * @tests {@link javax.sql.rowset.serial.SQLInputImpl#readCharacterStream()} + */ + public void testReadCharacterStream() throws SQLException { + Reader stream = new StringReader("abc"); + Object[] attributes = new Object[] {stream}; + SQLInputImpl impl = new SQLInputImpl(attributes, new HashMap>()); + assertEquals(stream, impl.readCharacterStream()); + + try { + impl.readCharacterStream(); + fail("should throw SQLException"); + } catch (SQLException e) { + // expected + } + } + /** + * @tests {@link javax.sql.rowset.serial.SQLInputImpl#readClob()} + */ + public void testReadClob() throws SQLException { + Clob clob = new MockClob(); + Object[] attributes = new Object[] {clob}; + SQLInputImpl impl = new SQLInputImpl(attributes, new HashMap>()); + assertEquals(clob, impl.readClob()); + + try { + impl.readClob(); + fail("should throw SQLException"); + } catch (SQLException e) { + // expected + } } - public void testReadDouble() { + /** + * @tests {@link javax.sql.rowset.serial.SQLInputImpl#readDate()} + */ + public void testReadDate() throws SQLException { + Date date = new Date(12); + Object[] attributes = new Object[] {date}; + SQLInputImpl impl = new SQLInputImpl(attributes, new HashMap>()); + assertEquals(date, impl.readDate()); + + try { + impl.readDate(); + fail("should throw SQLException"); + } catch (SQLException e) { + // expected + } + } + /** + * @tests {@link javax.sql.rowset.serial.SQLInputImpl#readDouble()} + */ + public void testReadDouble() throws SQLException { + Object[] attributes = new Object[] {Double.valueOf("3")}; + SQLInputImpl impl = new SQLInputImpl(attributes, new HashMap>()); + assertEquals((double)3, impl.readDouble()); + + try { + impl.readDouble(); + fail("should throw SQLException"); + } catch (SQLException e) { + // expected + } } - public void testReadFloat() { + /** + * @tests {@link javax.sql.rowset.serial.SQLInputImpl#readFloat()} + */ + public void testReadFloat() throws SQLException { + Object[] attributes = new Object[] {Float.valueOf("3.5")}; + SQLInputImpl impl = new SQLInputImpl(attributes, new HashMap>()); + assertEquals((float)3.5, impl.readFloat()); + + try { + impl.readFloat(); + fail("should throw SQLException"); + } catch (SQLException e) { + // expected + } + } + /** + * @tests {@link javax.sql.rowset.serial.SQLInputImpl#readInt()} + */ + public void testReadInt() throws SQLException { + Object[] attributes = new Object[] {Integer.valueOf("3")}; + SQLInputImpl impl = new SQLInputImpl(attributes, new HashMap>()); + assertEquals(3, impl.readInt()); + + try { + impl.readInt(); + fail("should throw SQLException"); + } catch (SQLException e) { + // expected + } } - public void testReadInt() { + /** + * @tests {@link javax.sql.rowset.serial.SQLInputImpl#readLong()} + */ + public void testReadLong() throws SQLException { + Object[] attributes = new Object[] {Long.valueOf("3")}; + SQLInputImpl impl = new SQLInputImpl(attributes, new HashMap>()); + assertEquals((long)3, impl.readLong()); + + try { + impl.readLong(); + fail("should throw SQLException"); + } catch (SQLException e) { + // expected + } + } + /** + * @tests {@link javax.sql.rowset.serial.SQLInputImpl#readObject()} + */ + public void testReadObject() throws SQLException { + Object[] structAttributes = {"hello", Boolean.TRUE, "abc", Integer.valueOf(99)}; + Struct struct = new MockStruct(structAttributes, "harmonytests.MockSQLData"); + Struct struct2 = new MockStruct(structAttributes, "not stored name"); + HashMap> types = new HashMap>(); + types.put("harmonytests.MockSQLData", MockSQLData.class); + Object[] attributes = new Object[] {struct, struct2, null, "xyz"}; + SQLInputImpl impl = new SQLInputImpl(attributes, types); + Object obj = impl.readObject(); + assertTrue(obj instanceof MockSQLData); + MockSQLData sqlData = (MockSQLData)obj; + assertEquals(structAttributes[0], sqlData.firstAttribute); + assertEquals(structAttributes[1], sqlData.secondAttribute); + assertEquals(structAttributes[2], sqlData.thirdAttribute); + assertEquals(structAttributes[3], sqlData.fourthAttribute); + Object obj2 = impl.readObject(); + assertEquals(struct2, obj2); + Object obj3 = impl.readObject(); + assertNull(obj3); + Object obj4 = impl.readObject(); + assertEquals(attributes[3], obj4); } - public void testReadLong() { + /** + * @tests {@link javax.sql.rowset.serial.SQLInputImpl#readRef()} + */ + public void testReadRef() throws SQLException { + Ref ref = new MockRef(); + Object[] attributes = new Object[] {ref}; + SQLInputImpl impl = new SQLInputImpl(attributes, new HashMap>()); + assertEquals(ref, impl.readRef()); + + try { + impl.readRef(); + fail("should throw SQLException"); + } catch (SQLException e) { + // expected + } + } + /** + * @tests {@link javax.sql.rowset.serial.SQLInputImpl#readShort()} + */ + public void testReadShort() throws SQLException { + Object[] attributes = new Object[] {Short.valueOf("3")}; + SQLInputImpl impl = new SQLInputImpl(attributes, new HashMap>()); + assertEquals((short)3, impl.readShort()); + + try { + impl.readShort(); + fail("should throw SQLException"); + } catch (SQLException e) { + // expected + } } - public void testReadObject() { + /** + * @tests {@link javax.sql.rowset.serial.SQLInputImpl#readString()} + */ + public void testReadString() throws SQLException { + Object[] attributes = new Object[] {"hello"}; + SQLInputImpl impl = new SQLInputImpl(attributes, new HashMap>()); + assertEquals("hello", impl.readString()); + + try { + impl.readString(); + fail("should throw SQLException"); + } catch (SQLException e) { + // expected + } + } + /** + * @tests {@link javax.sql.rowset.serial.SQLInputImpl#readTime()} + */ + public void testReadTime() throws SQLException { + Time time = new Time(345); + Object[] attributes = new Object[] {time}; + SQLInputImpl impl = new SQLInputImpl(attributes, new HashMap>()); + assertEquals(time, impl.readTime()); + + try { + impl.readTime(); + fail("should throw SQLException"); + } catch (SQLException e) { + // expected + } } - public void testReadRef() { + /** + * @tests {@link javax.sql.rowset.serial.SQLInputImpl#readTimestamp()} + */ + public void testReadTimestamp() throws SQLException { + Timestamp time = new Timestamp(345); + Object[] attributes = new Object[] {time}; + SQLInputImpl impl = new SQLInputImpl(attributes, new HashMap>()); + assertEquals(time, impl.readTimestamp()); + + try { + impl.readTimestamp(); + fail("should throw SQLException"); + } catch (SQLException e) { + // expected + } + } + /** + * @tests {@link javax.sql.rowset.serial.SQLInputImpl#readURL()} + */ + public void testReadURL() throws SQLException, MalformedURLException { + URL url = new URL("http://www.apache.org"); + Object[] attributes = new Object[] {url}; + SQLInputImpl impl = new SQLInputImpl(attributes, new HashMap>()); + try { + impl.readURL(); + fail("should throw SQLException"); + } catch (SQLException e) { + // expected + } + + try { + impl.readURL(); + fail("should throw SQLException"); + } catch (SQLException e) { + // expected + } } - public void testReadShort() { - + /** + * @tests {@link javax.sql.rowset.serial.SQLInputImpl#wasNull()} + */ + public void testWasNull() throws SQLException { + Object[] attributes = new Object[] {null, "hello"}; + SQLInputImpl impl = new SQLInputImpl(attributes, new HashMap>()); + assertEquals(null, impl.readString()); + assertTrue(impl.wasNull()); + assertEquals("hello", impl.readString()); + assertFalse(impl.wasNull()); + try { + impl.readString(); + fail("should throw SQLException"); + } catch (SQLException e) { + // expected + } + assertFalse(impl.wasNull()); } - public void testReadString() { - } + /* + * Mock classes used by both this test and SQLOutputImplTest to test + * readObject and writeObject methods respectively. + */ + + public static class MockStruct implements Struct { + + private String sqlTypeName; - public void testReadTime() { + public MockStruct(Object[] attributes, String sqlTypeName) { + this.attributes = attributes; + this.sqlTypeName = sqlTypeName; + } + + Object[] attributes; - } + public Object[] getAttributes() throws SQLException { + return attributes; + } - public void testReadTimestamp() { + public Object[] getAttributes(Map> theMap) throws SQLException { + return attributes; + } + public String getSQLTypeName() throws SQLException { + return sqlTypeName; + } } - - public void testReadURL() { + + public static class MockSQLData implements SQLData { + + public String firstAttribute; + public Boolean secondAttribute; + public String thirdAttribute; + public Integer fourthAttribute; - } + public String getSQLTypeName() throws SQLException { + return "harmonytests.MockSQLData"; + } - public void testWasNull() { + public void readSQL(SQLInput stream, String typeName) throws SQLException { + firstAttribute = stream.readString(); + secondAttribute = stream.readBoolean(); + thirdAttribute = stream.readString(); + fourthAttribute = stream.readInt(); + } + public void writeSQL(SQLOutput stream) throws SQLException { + stream.writeString(firstAttribute); + stream.writeBoolean(secondAttribute); + stream.writeString(thirdAttribute); + stream.writeInt(fourthAttribute); + } } - } Index: src/test/java/org/apache/harmony/sql/tests/javax/sql/rowset/MockArray.java =================================================================== --- src/test/java/org/apache/harmony/sql/tests/javax/sql/rowset/MockArray.java +++ src/test/java/org/apache/harmony/sql/tests/javax/sql/rowset/MockArray.java @@ -0,0 +1,73 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.harmony.sql.tests.javax.sql.rowset; + +import java.sql.Array; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Map; + +public class MockArray implements Array { + + public Object getArray() throws SQLException { + return new Object[0]; + } + + public Object getArray(long index, int count) throws SQLException { + return null; + } + + public Object getArray(long index, int count, Map> map) + throws SQLException { + return null; + } + + public Object getArray(Map> map) throws SQLException { + return null; + } + + public int getBaseType() throws SQLException { + return 0; + } + + public String getBaseTypeName() throws SQLException { + return "base"; + } + + public ResultSet getResultSet() throws SQLException { + return null; + } + + public ResultSet getResultSet(long index, int count) + throws SQLException { + return null; + } + + public ResultSet getResultSet(long index, int count, + Map> map) throws SQLException { + return null; + } + + public ResultSet getResultSet(Map> map) + throws SQLException { + return null; + } + + public void free() throws SQLException { + } + +} \ No newline at end of file Index: src/test/java/org/apache/harmony/sql/tests/javax/sql/rowset/MockBlob.java =================================================================== --- src/test/java/org/apache/harmony/sql/tests/javax/sql/rowset/MockBlob.java +++ src/test/java/org/apache/harmony/sql/tests/javax/sql/rowset/MockBlob.java @@ -0,0 +1,68 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.harmony.sql.tests.javax.sql.rowset; + +import java.io.InputStream; +import java.io.OutputStream; +import java.sql.Blob; +import java.sql.SQLException; + +public class MockBlob implements Blob { + + public InputStream getBinaryStream() throws SQLException { + return null; + } + + public byte[] getBytes(long pos, int length) throws SQLException { + return new byte[0]; + } + + public long length() throws SQLException { + return 0; + } + + public long position(Blob pattern, long start) throws SQLException { + return 0; + } + + public long position(byte[] pattern, long start) throws SQLException { + return 0; + } + + public OutputStream setBinaryStream(long pos) throws SQLException { + return null; + } + + public int setBytes(long pos, byte[] theBytes) throws SQLException { + return 0; + } + + public int setBytes(long pos, byte[] theBytes, int offset, int len) + throws SQLException { + return 0; + } + + public void truncate(long len) throws SQLException { + } + + public void free() throws SQLException { + } + + public InputStream getBinaryStream(long pos, long length) throws SQLException { + return null; + } +} \ No newline at end of file Index: src/test/java/org/apache/harmony/sql/tests/javax/sql/rowset/MockClob.java =================================================================== --- src/test/java/org/apache/harmony/sql/tests/javax/sql/rowset/MockClob.java +++ src/test/java/org/apache/harmony/sql/tests/javax/sql/rowset/MockClob.java @@ -0,0 +1,83 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.harmony.sql.tests.javax.sql.rowset; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.Reader; +import java.io.StringReader; +import java.io.Writer; +import java.sql.Clob; +import java.sql.SQLException; + +public class MockClob implements Clob { + + public Reader characterStreamReader = new StringReader("xys"); + public InputStream asciiInputStream = new ByteArrayInputStream("hello".getBytes()); + + public InputStream getAsciiStream() throws SQLException { + return asciiInputStream; + } + + public Reader getCharacterStream() throws SQLException { + return characterStreamReader; + } + + public String getSubString(long pos, int length) throws SQLException { + return null; + } + + public long length() throws SQLException { + return 3; + } + + public long position(Clob searchstr, long start) throws SQLException { + return 0; + } + + public long position(String searchstr, long start) throws SQLException { + return 0; + } + + public OutputStream setAsciiStream(long pos) throws SQLException { + return null; + } + + public Writer setCharacterStream(long pos) throws SQLException { + return null; + } + + public int setString(long pos, String str) throws SQLException { + return 0; + } + + public int setString(long pos, String str, int offset, int len) + throws SQLException { + return 0; + } + + public void truncate(long len) throws SQLException { + } + + public void free() throws SQLException { + } + + public Reader getCharacterStream(long pos, long length) throws SQLException { + return null; + } +} \ No newline at end of file Index: src/test/java/org/apache/harmony/sql/tests/javax/sql/rowset/MockRef.java =================================================================== --- src/test/java/org/apache/harmony/sql/tests/javax/sql/rowset/MockRef.java +++ src/test/java/org/apache/harmony/sql/tests/javax/sql/rowset/MockRef.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.harmony.sql.tests.javax.sql.rowset; + +import java.sql.Ref; +import java.sql.SQLException; +import java.util.Map; + +public class MockRef implements Ref { + + public String getBaseTypeName() throws SQLException { + return "ref"; + } + + public Object getObject() throws SQLException { + return null; + } + + public Object getObject(Map> map) throws SQLException { + return null; + } + + public void setObject(Object value) throws SQLException { + } +} \ No newline at end of file