Refactor testcase and ensure expected exception occurs.

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1021490 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Dick 2010-10-11 20:05:00 +00:00
parent b229fbd13e
commit a63ba3ed38
1 changed files with 66 additions and 45 deletions

View File

@ -39,7 +39,26 @@ public class TestLobs extends SingleEMFTestCase {
public void setUp() throws Exception {
super.setUp(DROP_TABLES, Lobs.class, Blobs.class);
}
public void testNullableAndNonNullableBLobs() {
// blob tests
public void testBlobSetToNull() {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Blobs lobs = new Blobs();
lobs.setLobNotNullable(null);
lobs.setLobNullable(null);
em.persist(lobs);
try {
em.getTransaction().commit();
fail("Expected a RollbackException");
} catch (Exception e) {
assertError(e, RollbackException.class);
}
em.close();
}
public void testBlobPersistQuery() {
// test with null
EntityManager em = emf.createEntityManager();
Blobs lobs = new Blobs();
@ -54,19 +73,6 @@ public class TestLobs extends SingleEMFTestCase {
em.getTransaction().commit();
em.close();
em = emf.createEntityManager();
em.getTransaction().begin();
lobs = new Blobs();
lobs.setLobNotNullable(null);
lobs.setLobNullable(null);
em.persist(lobs);
try {
em.getTransaction().commit();
} catch (Exception e) {
assertError(e, RollbackException.class);
}
em.close();
em = emf.createEntityManager();
em.getTransaction().begin();
Query query = em.createQuery("select e from Blobs e");
@ -75,10 +81,14 @@ public class TestLobs extends SingleEMFTestCase {
em.remove(lobs);
em.getTransaction().commit();
em.close();
}
public void testBlobZeroLengthByteArray() {
// test with 0 length bytes
bytes = new byte[0];
em = emf.createEntityManager();
byte[] bytes = new byte[0];
EntityManager em = emf.createEntityManager();
Blobs lobs = new Blobs();
em.getTransaction().begin();
lobs.setLobNotNullable(bytes);
lobs.setLobNullable(bytes);
@ -88,7 +98,7 @@ public class TestLobs extends SingleEMFTestCase {
em = emf.createEntityManager();
em.getTransaction().begin();
query = em.createQuery("select e from Blobs e");
Query query = em.createQuery("select e from Blobs e");
lobs = (Blobs)query.getSingleResult();
assertTrue(lobs.getLobNullable() == null || lobs.getLobNullable().length == 0);
assertTrue(lobs.getLobNotNullable() == null || lobs.getLobNotNullable().length == 0);
@ -98,14 +108,16 @@ public class TestLobs extends SingleEMFTestCase {
em.remove(lobs);
em.getTransaction().commit();
em.close();
}
public void testBlobLargeData() {
// test with large data
bytes = new byte[5000];
byte[] bytes = new byte[5000];
for (int i = 0; i < bytes.length; i++)
bytes[i] = randomByte().byteValue();
em = emf.createEntityManager();
lobs = new Blobs();
EntityManager em = emf.createEntityManager();
Blobs lobs = new Blobs();
em.getTransaction().begin();
lobs.setLobNotNullable(bytes);
lobs.setLobNullable(bytes);
@ -115,7 +127,7 @@ public class TestLobs extends SingleEMFTestCase {
em = emf.createEntityManager();
em.getTransaction().begin();
query = em.createQuery("select e from Blobs e");
Query query = em.createQuery("select e from Blobs e");
lobs = (Blobs)query.getSingleResult();
for (int i = 0; i < 5000; i++) {
assertEquals(lobs.getLobNullable()[i], lobs.getLobNotNullable()[i]);
@ -126,7 +138,9 @@ public class TestLobs extends SingleEMFTestCase {
em.close();
}
public void testNullableAndNonNullableLobs() {
// lob tests
public void testLobPersistQuery() {
// test with null
EntityManager em = emf.createEntityManager();
Lobs lobs = new Lobs();
@ -137,19 +151,6 @@ public class TestLobs extends SingleEMFTestCase {
em.getTransaction().commit();
em.close();
em = emf.createEntityManager();
em.getTransaction().begin();
lobs = new Lobs();
lobs.setLobNotNullable(null);
lobs.setLobNullable(null);
em.persist(lobs);
try {
em.getTransaction().commit();
} catch (Exception e) {
assertError(e, RollbackException.class);
}
em.close();
em = emf.createEntityManager();
em.getTransaction().begin();
Query query = em.createQuery("select e from Lobs e");
@ -159,11 +160,29 @@ public class TestLobs extends SingleEMFTestCase {
em.remove(lobs);
em.getTransaction().commit();
em.close();
}
// test with ""
em = emf.createEntityManager();
public void testLobSetToNull() {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Lobs lobs = new Lobs();
lobs.setLobNotNullable(null);
lobs.setLobNullable(null);
em.persist(lobs);
try {
em.getTransaction().commit();
fail("Expected a RollbackException");
} catch (Exception e) {
assertError(e, RollbackException.class);
}
em.close();
}
public void testLobEmptyString() {
// test with ""
EntityManager em = emf.createEntityManager();
Lobs lobs = new Lobs();
em.getTransaction().begin();
lobs.setLobNotNullable("");
lobs.setLobNullable("");
em.persist(lobs);
em.getTransaction().commit();
@ -171,7 +190,7 @@ public class TestLobs extends SingleEMFTestCase {
em = emf.createEntityManager();
em.getTransaction().begin();
query = em.createQuery("select e from Lobs e");
Query query = em.createQuery("select e from Lobs e");
lobs = (Lobs)query.getSingleResult();
assertTrue(lobs.getLobNullable() == null || lobs.getLobNullable().length() == 0);
assertTrue(lobs.getLobNotNullable() == null || lobs.getLobNotNullable().length() == 0);
@ -179,14 +198,16 @@ public class TestLobs extends SingleEMFTestCase {
em.remove(lobs);
em.getTransaction().commit();
em.close();
}
public void testLobLargeData() {
// test with large data
String temp = "";
for (int i = 0; i < 500; i++) // at 400 it changes from strings to Objects
temp = temp + "1234567890";
em = emf.createEntityManager();
lobs = new Lobs();
EntityManager em = emf.createEntityManager();
Lobs lobs = new Lobs();
em.getTransaction().begin();
lobs.setLobNotNullable(temp);
lobs.setLobNullable(temp);
@ -196,7 +217,7 @@ public class TestLobs extends SingleEMFTestCase {
em = emf.createEntityManager();
em.getTransaction().begin();
query = em.createQuery("select e from Lobs e");
Query query = em.createQuery("select e from Lobs e");
lobs = (Lobs)query.getSingleResult();
assertEquals(lobs.getLobNullable(), lobs.getLobNotNullable());
assertEquals(temp, lobs.getLobNullable());