Author: tfischer
Date: Sun Jan 27 15:23:53 2013
New Revision: 1439098
URL: http://svn.apache.org/viewvc?rev=1439098&view=rev
Log:
Add tests for the byte[] types
Added:
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/datatypes/BinaryTest.java
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/datatypes/ByteTypeTest.java
- copied, changed from r1438200,
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/datatypes/BlobTest.java
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/datatypes/LongvarbinaryTest.java
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/datatypes/VarbinaryTest.java
Modified:
db/torque/torque4/trunk/torque-test/src/main/schema/types-schema.xml
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/datatypes/BlobTest.java
Modified: db/torque/torque4/trunk/torque-test/src/main/schema/types-schema.xml
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-test/src/main/schema/types-schema.xml?rev=1439098&r1=1439097&r2=1439098&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-test/src/main/schema/types-schema.xml
(original)
+++ db/torque/torque4/trunk/torque-test/src/main/schema/types-schema.xml Sun
Jan 27 15:23:53 2013
@@ -115,23 +115,23 @@ Column.getJavaObject() not implimented f
<column name="CLOB_OBJECT_VALUE" type="CLOB" javaType="object"/>
</table>
- <!-- table name="BINARY_TYPE" description="Table to test BINARY type">
+ <table name="BINARY_TYPE" description="Table to test BINARY type">
<column name="ID" primaryKey="true" type="INTEGER"/>
- <column name="BINARY_VALUE" type="BINARY"/>
- <column name="BINARY_OBJECT_VALUE" type="BINARY" javaType="object"/>
+ <column name="BINARY_VALUE" type="BINARY" size="100"/>
+ <column name="BINARY_OBJECT_VALUE" type="BINARY" size="100"
javaType="object"/>
</table>
<table name="VARBINARY_TYPE" description="Table to test VARBINARY type">
<column name="ID" primaryKey="true" type="INTEGER"/>
- <column name="VARBINARY_VALUE" type="VARBINARY"/>
- <column name="VARBINARY_OBJECT_VALUE" type="VARBINARY" javaType="object"/>
+ <column name="VARBINARY_VALUE" type="VARBINARY" size="100"/>
+ <column name="VARBINARY_OBJECT_VALUE" type="VARBINARY" size="100"
javaType="object"/>
</table>
<table name="LONGVARBINARY_TYPE" description="Table to test LONGVARBINARY
type">
<column name="ID" primaryKey="true" type="INTEGER"/>
<column name="LONGVARBINARY_VALUE" type="LONGVARBINARY"/>
<column name="LONGVARBINARY_OBJECT_VALUE" type="LONGVARBINARY"
javaType="object"/>
- </table-->
+ </table>
<table name="BINT_BCHAR_TYPE" idMethod="none">
<column name="ID" required="true" primaryKey="true" type="VARCHAR"
size="20"/>
Added:
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/datatypes/BinaryTest.java
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/datatypes/BinaryTest.java?rev=1439098&view=auto
==============================================================================
---
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/datatypes/BinaryTest.java
(added)
+++
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/datatypes/BinaryTest.java
Sun Jan 27 15:23:53 2013
@@ -0,0 +1,32 @@
+package org.apache.torque.datatypes;
+
+import org.apache.torque.test.dbobject.BinaryType;
+import org.apache.torque.test.peer.BinaryTypePeer;
+
+public class BinaryTest extends ByteTypeTest<BinaryType>
+{
+
+ public BinaryTest() {
+ super(BinaryTypePeer.getBinaryTypePeerImpl(),
+ new BinaryType());
+ }
+
+ @Override
+ protected void setBytes(byte[] bytes, BinaryType toFill)
+ {
+ toFill.setBinaryValue(bytes);
+ toFill.setBinaryObjectValue(bytes);
+ }
+
+ @Override
+ protected byte[] getBytes(BinaryType toRead)
+ {
+ return toRead.getBinaryValue();
+ }
+
+ @Override
+ public int getByteArraySize() {
+ return 100;
+ }
+
+}
Modified:
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/datatypes/BlobTest.java
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/datatypes/BlobTest.java?rev=1439098&r1=1439097&r2=1439098&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/datatypes/BlobTest.java
(original)
+++
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/datatypes/BlobTest.java
Sun Jan 27 15:23:53 2013
@@ -1,108 +1,32 @@
package org.apache.torque.datatypes;
-import java.util.Arrays;
-import java.util.List;
-
-import org.apache.torque.BaseDatabaseTestCase;
-import org.apache.torque.TorqueException;
-import org.apache.torque.criteria.Criteria;
import org.apache.torque.test.dbobject.BlobType;
import org.apache.torque.test.peer.BlobTypePeer;
-/*
- * 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.
- */
-
-/**
- * Tests the data type BLOB.
- * @version $Id$
- */
-public class BlobTest extends BaseDatabaseTestCase
+public class BlobTest extends ByteTypeTest<BlobType>
{
- /**
- * check that blob cloumns can be read and written correctly
- * @throws Exception if the test fails
- */
- public void testBlobs() throws Exception
- {
- BlobTypePeer.doDelete(new Criteria());
- // create a new BlobType Object with large blob values
- // and save it
- BlobType blobType = fillAndSaveBlobType(new BlobType(), 100000);
-
- // read the BlobTypes from the database
- // and check the values against the original values
- List<BlobType> blobTypeList = BlobTypePeer.doSelect(new Criteria());
- assertEquals(1, blobTypeList.size());
-
- BlobType readBlobType = blobTypeList.get(0);
- assertTrue("read and written blobs should be equal. "
- + "Size of read blob is"
- + readBlobType.getBlobValue().length
- + " size of written blob is "
- + blobType.getBlobValue().length,
- Arrays.equals(
- blobType.getBlobValue(),
- readBlobType.getBlobValue()));
-
- // test updating
- blobType = readBlobType;
- fillAndSaveBlobType(blobType, 200000);
-
- // read the BlobTypes from the database
- // and check the values against the updated values
- blobTypeList = BlobTypePeer.doSelect(new Criteria());
- assertEquals(1, blobTypeList.size());
-
- readBlobType = blobTypeList.get(0);
- assertTrue("read and written blobs should be equal. "
- + "Size of read blob is"
- + readBlobType.getBlobValue().length
- + " size of written blob is "
- + blobType.getBlobValue().length,
- Arrays.equals(
- blobType.getBlobValue(),
- readBlobType.getBlobValue()));
+ public BlobTest() {
+ super(BlobTypePeer.getBlobTypePeerImpl(), new BlobType());
}
- /**
- * Fills the Blob in a BlobType, writes it to to the database and returns
it.
- *
- * @param blobType the blobType to fill and save, not null.
- * @param size the size of the blob.
- *
- * @return the updated BlobType.
- *
- * @throws TorqueException if saving fails
- */
- private BlobType fillAndSaveBlobType(BlobType blobType, int size)
- throws TorqueException
+ @Override
+ protected void setBytes(byte[] bytes, BlobType toFill)
{
- {
- byte[] bytes = new byte[size];
- for (int i = 0; i < size; ++i)
- {
- bytes[i] = new Integer(i % 256).byteValue();
- }
- blobType.setBlobValue(bytes);
- }
- blobType.save();
- return blobType;
+ toFill.setBlobValue(bytes);
+ toFill.setBlobObjectValue(bytes);
+ }
+
+ @Override
+ protected byte[] getBytes(BlobType toRead)
+ {
+ return toRead.getBlobValue();
+ }
+
+ @Override
+ public boolean canWriteNullBytes() {
+ // the problem with postgresql is that a setNull(Types.BLOB)
+ // is executed by torque which is not accepted for BYTEA columns
+ return false;
}
}
Copied:
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/datatypes/ByteTypeTest.java
(from r1438200,
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/datatypes/BlobTest.java)
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/datatypes/ByteTypeTest.java?p2=db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/datatypes/ByteTypeTest.java&p1=db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/datatypes/BlobTest.java&r1=1438200&r2=1439098&rev=1439098&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/datatypes/BlobTest.java
(original)
+++
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/datatypes/ByteTypeTest.java
Sun Jan 27 15:23:53 2013
@@ -3,11 +3,13 @@ package org.apache.torque.datatypes;
import java.util.Arrays;
import java.util.List;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
import org.apache.torque.BaseDatabaseTestCase;
import org.apache.torque.TorqueException;
import org.apache.torque.criteria.Criteria;
-import org.apache.torque.test.dbobject.BlobType;
-import org.apache.torque.test.peer.BlobTypePeer;
+import org.apache.torque.om.Persistent;
+import org.apache.torque.util.BasePeerImpl;
/*
* Licensed to the Apache Software Foundation (ASF) under one
@@ -29,80 +31,152 @@ import org.apache.torque.test.peer.BlobT
*/
/**
- * Tests the data type BLOB.
+ * Tests byte data types.
* @version $Id$
*/
-public class BlobTest extends BaseDatabaseTestCase
+public abstract class ByteTypeTest<T extends Persistent> extends
BaseDatabaseTestCase
{
+ private static Log log = LogFactory.getLog(ByteTypeTest.class);
+
+ private BasePeerImpl<T> peer;
+
+ private T dbObject;
+
+ public ByteTypeTest(BasePeerImpl<T> peer, T dbObject) {
+ this.peer = peer;
+ this.dbObject = dbObject;
+ }
+
/**
- * check that blob cloumns can be read and written correctly
+ * Check that byte[] columns can be inserted and read correctly.
+ *
* @throws Exception if the test fails
*/
- public void testBlobs() throws Exception
+ public void testInsertReadByteArray() throws Exception
{
- BlobTypePeer.doDelete(new Criteria());
+ // prepare
+ peer.doDelete(new Criteria());
- // create a new BlobType Object with large blob values
- // and save it
- BlobType blobType = fillAndSaveBlobType(new BlobType(), 100000);
-
- // read the BlobTypes from the database
- // and check the values against the original values
- List<BlobType> blobTypeList = BlobTypePeer.doSelect(new Criteria());
- assertEquals(1, blobTypeList.size());
-
- BlobType readBlobType = blobTypeList.get(0);
- assertTrue("read and written blobs should be equal. "
- + "Size of read blob is"
- + readBlobType.getBlobValue().length
- + " size of written blob is "
- + blobType.getBlobValue().length,
+ // execute write
+ T written = fillAndSave(dbObject, getByteArraySize());
+ // execute read
+ List<T> readList = peer.doSelect(new Criteria());
+
+ // verify
+ assertEquals(1, readList.size());
+ T read = readList.get(0);
+ assertTrue("read and written byte arrays should be equal. "
+ + "Size of read byte array is"
+ + getBytes(read).length
+ + " size of byte array blob is "
+ + getBytes(written).length,
Arrays.equals(
- blobType.getBlobValue(),
- readBlobType.getBlobValue()));
+ getBytes(written),
+ getBytes(read)));
+ }
- // test updating
- blobType = readBlobType;
- fillAndSaveBlobType(blobType, 200000);
-
- // read the BlobTypes from the database
- // and check the values against the updated values
- blobTypeList = BlobTypePeer.doSelect(new Criteria());
- assertEquals(1, blobTypeList.size());
-
- readBlobType = blobTypeList.get(0);
- assertTrue("read and written blobs should be equal. "
- + "Size of read blob is"
- + readBlobType.getBlobValue().length
- + " size of written blob is "
- + blobType.getBlobValue().length,
+ /**
+ * Check that byte[] columns can be updated and read correctly.
+ *
+ * @throws Exception if the test fails
+ */
+ public void testUpdateReadByteArray() throws Exception
+ {
+ // prepare
+ peer.doDelete(new Criteria());
+ fillAndSave(dbObject, getByteArraySize() / 2);
+
+ // execute write
+ T written = fillAndSave(dbObject, getByteArraySize());
+ // execute read
+ List<T> readList = peer.doSelect(new Criteria());
+
+ // verify
+ assertEquals(1, readList.size());
+ T read = readList.get(0);
+ assertTrue("read and written byte arrays should be equal. "
+ + "Size of read byte array is"
+ + getBytes(read).length
+ + " size of written byte array is "
+ + getBytes(written).length,
Arrays.equals(
- blobType.getBlobValue(),
- readBlobType.getBlobValue()));
+ getBytes(written),
+ getBytes(read)));
+ }
+
+ /**
+ * Check that byte[] columns can be updated and read correctly.
+ *
+ * @throws Exception if the test fails
+ */
+ public void testInsertReadNullByteArray() throws Exception
+ {
+ if (!canWriteNullBytes()) {
+ log.error("database with adapter "
+ + defaultAdapter.getClass().getName()
+ + " does not support writing null bate arrays to type "
+ + dbObject.getClass().getName());
+ return;
+ }
+ // prepare
+ peer.doDelete(new Criteria());
+ setBytes(null, dbObject);
+
+ // execute write
+ dbObject.save();
+ // execute read
+ List<T> readList = peer.doSelect(new Criteria());
+
+ // verify
+ assertEquals(1, readList.size());
+ T read = readList.get(0);
+ assertNull(getBytes(read));
}
+
+ protected abstract void setBytes(byte[] bytes, T toFill);
+
+ protected abstract byte[] getBytes(T toRead);
+
/**
- * Fills the Blob in a BlobType, writes it to to the database and returns
it.
+ * Fills the byte value in a dbObject, writes it to to the database and
returns it.
*
- * @param blobType the blobType to fill and save, not null.
- * @param size the size of the blob.
+ * @param toFill the object to fill and save, not null.
+ * @param size the size of the byte array to set.
*
- * @return the updated BlobType.
+ * @return the updated dbObject.
*
* @throws TorqueException if saving fails
*/
- private BlobType fillAndSaveBlobType(BlobType blobType, int size)
- throws TorqueException
+ private T fillAndSave(T toFill, int size)
+ throws Exception
{
+ byte[] bytes = createBytes(size);
+ setBytes(bytes, toFill);
+ toFill.save();
+ return toFill;
+ }
+
+ private byte[] createBytes(int size) {
+ byte[] bytes = new byte[size];
+ for (int i = 0; i < size; ++i)
{
- byte[] bytes = new byte[size];
- for (int i = 0; i < size; ++i)
- {
- bytes[i] = new Integer(i % 256).byteValue();
- }
- blobType.setBlobValue(bytes);
+ bytes[i] = new Integer(i % 256).byteValue();
}
- blobType.save();
- return blobType;
+ return bytes;
+ }
+
+ /**
+ * Can be overwritten to indicate that the database cannot write null
+ * values for the selected type.
+ *
+ * @return true if null byte arrays can be written, false if not;
+ */
+ public boolean canWriteNullBytes() {
+ return true;
+ }
+
+ public int getByteArraySize() {
+ return 200000;
}
}
Added:
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/datatypes/LongvarbinaryTest.java
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/datatypes/LongvarbinaryTest.java?rev=1439098&view=auto
==============================================================================
---
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/datatypes/LongvarbinaryTest.java
(added)
+++
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/datatypes/LongvarbinaryTest.java
Sun Jan 27 15:23:53 2013
@@ -0,0 +1,36 @@
+package org.apache.torque.datatypes;
+
+import org.apache.torque.adapter.DerbyAdapter;
+import org.apache.torque.test.dbobject.LongvarbinaryType;
+import org.apache.torque.test.peer.LongvarbinaryTypePeer;
+
+public class LongvarbinaryTest extends ByteTypeTest<LongvarbinaryType>
+{
+
+ public LongvarbinaryTest() {
+ super(LongvarbinaryTypePeer.getLongvarbinaryTypePeerImpl(),
+ new LongvarbinaryType());
+ }
+
+ @Override
+ protected void setBytes(byte[] bytes, LongvarbinaryType toFill)
+ {
+ toFill.setLongvarbinaryValue(bytes);
+ toFill.setLongvarbinaryObjectValue(bytes);
+ }
+
+ @Override
+ protected byte[] getBytes(LongvarbinaryType toRead)
+ {
+ return toRead.getLongvarbinaryValue();
+ }
+
+ @Override
+ public int getByteArraySize() {
+ if (defaultAdapter instanceof DerbyAdapter) {
+ return 32000; // Maximum 32700 for derby
+ }
+ return super.getByteArraySize();
+ }
+
+}
Added:
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/datatypes/VarbinaryTest.java
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/datatypes/VarbinaryTest.java?rev=1439098&view=auto
==============================================================================
---
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/datatypes/VarbinaryTest.java
(added)
+++
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/datatypes/VarbinaryTest.java
Sun Jan 27 15:23:53 2013
@@ -0,0 +1,31 @@
+package org.apache.torque.datatypes;
+
+import org.apache.torque.test.dbobject.VarbinaryType;
+import org.apache.torque.test.peer.VarbinaryTypePeer;
+
+public class VarbinaryTest extends ByteTypeTest<VarbinaryType>
+{
+
+ public VarbinaryTest() {
+ super(VarbinaryTypePeer.getVarbinaryTypePeerImpl(),
+ new VarbinaryType());
+ }
+
+ @Override
+ protected void setBytes(byte[] bytes, VarbinaryType toFill)
+ {
+ toFill.setVarbinaryValue(bytes);
+ toFill.setVarbinaryObjectValue(bytes);
+ }
+
+ @Override
+ protected byte[] getBytes(VarbinaryType toRead)
+ {
+ return toRead.getVarbinaryValue();
+ }
+
+ @Override
+ public int getByteArraySize() {
+ return 100;
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]