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 { public void setUp() throws Exception {
super.setUp(DROP_TABLES, Lobs.class, Blobs.class); 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 // test with null
EntityManager em = emf.createEntityManager(); EntityManager em = emf.createEntityManager();
Blobs lobs = new Blobs(); Blobs lobs = new Blobs();
@ -53,20 +72,7 @@ public class TestLobs extends SingleEMFTestCase {
em.persist(lobs); em.persist(lobs);
em.getTransaction().commit(); em.getTransaction().commit();
em.close(); 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 = emf.createEntityManager();
em.getTransaction().begin(); em.getTransaction().begin();
Query query = em.createQuery("select e from Blobs e"); Query query = em.createQuery("select e from Blobs e");
@ -75,10 +81,14 @@ public class TestLobs extends SingleEMFTestCase {
em.remove(lobs); em.remove(lobs);
em.getTransaction().commit(); em.getTransaction().commit();
em.close(); em.close();
}
public void testBlobZeroLengthByteArray() {
// test with 0 length bytes // test with 0 length bytes
bytes = new byte[0]; byte[] bytes = new byte[0];
em = emf.createEntityManager(); EntityManager em = emf.createEntityManager();
Blobs lobs = new Blobs();
em.getTransaction().begin(); em.getTransaction().begin();
lobs.setLobNotNullable(bytes); lobs.setLobNotNullable(bytes);
lobs.setLobNullable(bytes); lobs.setLobNullable(bytes);
@ -88,7 +98,7 @@ public class TestLobs extends SingleEMFTestCase {
em = emf.createEntityManager(); em = emf.createEntityManager();
em.getTransaction().begin(); em.getTransaction().begin();
query = em.createQuery("select e from Blobs e"); Query query = em.createQuery("select e from Blobs e");
lobs = (Blobs)query.getSingleResult(); lobs = (Blobs)query.getSingleResult();
assertTrue(lobs.getLobNullable() == null || lobs.getLobNullable().length == 0); assertTrue(lobs.getLobNullable() == null || lobs.getLobNullable().length == 0);
assertTrue(lobs.getLobNotNullable() == null || lobs.getLobNotNullable().length == 0); assertTrue(lobs.getLobNotNullable() == null || lobs.getLobNotNullable().length == 0);
@ -98,14 +108,16 @@ public class TestLobs extends SingleEMFTestCase {
em.remove(lobs); em.remove(lobs);
em.getTransaction().commit(); em.getTransaction().commit();
em.close(); em.close();
}
public void testBlobLargeData() {
// test with large data // test with large data
bytes = new byte[5000]; byte[] bytes = new byte[5000];
for (int i = 0; i < bytes.length; i++) for (int i = 0; i < bytes.length; i++)
bytes[i] = randomByte().byteValue(); bytes[i] = randomByte().byteValue();
em = emf.createEntityManager(); EntityManager em = emf.createEntityManager();
lobs = new Blobs(); Blobs lobs = new Blobs();
em.getTransaction().begin(); em.getTransaction().begin();
lobs.setLobNotNullable(bytes); lobs.setLobNotNullable(bytes);
lobs.setLobNullable(bytes); lobs.setLobNullable(bytes);
@ -115,7 +127,7 @@ public class TestLobs extends SingleEMFTestCase {
em = emf.createEntityManager(); em = emf.createEntityManager();
em.getTransaction().begin(); em.getTransaction().begin();
query = em.createQuery("select e from Blobs e"); Query query = em.createQuery("select e from Blobs e");
lobs = (Blobs)query.getSingleResult(); lobs = (Blobs)query.getSingleResult();
for (int i = 0; i < 5000; i++) { for (int i = 0; i < 5000; i++) {
assertEquals(lobs.getLobNullable()[i], lobs.getLobNotNullable()[i]); assertEquals(lobs.getLobNullable()[i], lobs.getLobNotNullable()[i]);
@ -125,8 +137,10 @@ public class TestLobs extends SingleEMFTestCase {
em.getTransaction().commit(); em.getTransaction().commit();
em.close(); em.close();
} }
// lob tests
public void testNullableAndNonNullableLobs() { public void testLobPersistQuery() {
// test with null // test with null
EntityManager em = emf.createEntityManager(); EntityManager em = emf.createEntityManager();
Lobs lobs = new Lobs(); Lobs lobs = new Lobs();
@ -137,19 +151,6 @@ public class TestLobs extends SingleEMFTestCase {
em.getTransaction().commit(); em.getTransaction().commit();
em.close(); 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 = emf.createEntityManager();
em.getTransaction().begin(); em.getTransaction().begin();
Query query = em.createQuery("select e from Lobs e"); Query query = em.createQuery("select e from Lobs e");
@ -159,11 +160,29 @@ public class TestLobs extends SingleEMFTestCase {
em.remove(lobs); em.remove(lobs);
em.getTransaction().commit(); em.getTransaction().commit();
em.close(); 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(); em.getTransaction().begin();
lobs.setLobNotNullable("");
lobs.setLobNullable(""); lobs.setLobNullable("");
em.persist(lobs); em.persist(lobs);
em.getTransaction().commit(); em.getTransaction().commit();
@ -171,7 +190,7 @@ public class TestLobs extends SingleEMFTestCase {
em = emf.createEntityManager(); em = emf.createEntityManager();
em.getTransaction().begin(); em.getTransaction().begin();
query = em.createQuery("select e from Lobs e"); Query query = em.createQuery("select e from Lobs e");
lobs = (Lobs)query.getSingleResult(); lobs = (Lobs)query.getSingleResult();
assertTrue(lobs.getLobNullable() == null || lobs.getLobNullable().length() == 0); assertTrue(lobs.getLobNullable() == null || lobs.getLobNullable().length() == 0);
assertTrue(lobs.getLobNotNullable() == null || lobs.getLobNotNullable().length() == 0); assertTrue(lobs.getLobNotNullable() == null || lobs.getLobNotNullable().length() == 0);
@ -179,14 +198,16 @@ public class TestLobs extends SingleEMFTestCase {
em.remove(lobs); em.remove(lobs);
em.getTransaction().commit(); em.getTransaction().commit();
em.close(); em.close();
}
public void testLobLargeData() {
// test with large data // test with large data
String temp = ""; String temp = "";
for (int i = 0; i < 500; i++) // at 400 it changes from strings to Objects for (int i = 0; i < 500; i++) // at 400 it changes from strings to Objects
temp = temp + "1234567890"; temp = temp + "1234567890";
em = emf.createEntityManager(); EntityManager em = emf.createEntityManager();
lobs = new Lobs(); Lobs lobs = new Lobs();
em.getTransaction().begin(); em.getTransaction().begin();
lobs.setLobNotNullable(temp); lobs.setLobNotNullable(temp);
lobs.setLobNullable(temp); lobs.setLobNullable(temp);
@ -196,7 +217,7 @@ public class TestLobs extends SingleEMFTestCase {
em = emf.createEntityManager(); em = emf.createEntityManager();
em.getTransaction().begin(); em.getTransaction().begin();
query = em.createQuery("select e from Lobs e"); Query query = em.createQuery("select e from Lobs e");
lobs = (Lobs)query.getSingleResult(); lobs = (Lobs)query.getSingleResult();
assertEquals(lobs.getLobNullable(), lobs.getLobNotNullable()); assertEquals(lobs.getLobNullable(), lobs.getLobNotNullable());
assertEquals(temp, lobs.getLobNullable()); assertEquals(temp, lobs.getLobNullable());