disable test for databases which cannot handle large PKs

On MariaDB and MySQL the allowed size of compound primary keys is very limited.
This very test will not work with them. It's nothing JPA can heal, users
are restricted and have to work around it.
This commit is contained in:
Mark Struberg 2021-04-03 16:08:56 +02:00
parent 8d3acdc11e
commit 495fe202a3
1 changed files with 162 additions and 155 deletions

View File

@ -28,185 +28,192 @@ import junit.framework.Assert;
public class TestRelationFieldAsPrimaryKeyAndForeignKey
extends SingleEMFTestCase {
extends SingleEMFTestCase {
@Override
@Override
public void setUp() {
setUp(C.class, CM.class, D.class, E.class, VC.class,
VCS.class, CLEAR_TABLES);
setUp(C.class, CM.class, D.class, E.class, VC.class,
VCS.class, CLEAR_TABLES);
EntityManager em = emf.createEntityManager();
try {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
List<E> es = (List<E>) em.createQuery(
"Select e from E e").getResultList();
"Select e from E e").getResultList();
for (E e : es)
em.remove(e);
em.getTransaction().commit();
em.close();
} catch (Exception e) {
em = emf.createEntityManager();
em.getTransaction().begin();
E e = new E();
e.setEId("E1");
e.setName("E1");
VC vc = new VC();
vc.setVcId("VC1");
VCS vcset = new VCS();
vcset.setVcsId("VCS1");
vcset.setName("VCSET1");
vcset.addVC(vc);
vcset.setE(e);
C c = new C();
c.setCId("C1");
CM cm = new CM();
cm.setCmId("CM1");
cm.setE(e);
cm.addC(c);
D d = new D();
d.setA("addr");
d.setVc(vc);
d.setId("IM1");
em.persist(e);
em.persist(vc);
em.persist(vcset);
em.persist(c);
em.persist(cm);
em.persist(d);
em.getTransaction().commit();
em.close();
} catch (Exception e) {
if (e.getMessage().contains("max key length")) {
// on MariaDB the keys are too long
setTestsDisabled(true);
}
}
}
public void testUnboundEntities() {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
VCS vcSet = new VCS();
vcSet.setVcsId("VCSET2");
vcSet.setName("VCSET2");
try {
em.persist(vcSet);
em.getTransaction().commit();
Assert.fail("didn't throw expected PersistenceException");
} catch (Exception e) {
// test pass
} finally {
if (em.getTransaction().isActive())
em.getTransaction().rollback();
}
em = emf.createEntityManager();
em.getTransaction().begin();
em.getTransaction().begin();
VC vc = new VC();
vc.setVcId("VC2");
try {
em.persist(vc);
em.getTransaction().commit();
Assert.fail("didn't throw expected PersistenceException");
} catch (Exception e) {
// test pass
} finally {
if (em.getTransaction().isActive())
em.getTransaction().rollback();
}
E e = new E();
e.setEId("E1");
e.setName("E1");
em.getTransaction().begin();
CM cm = new CM();
cm.setCmId("CMID2");
try {
em.persist(cm);
em.getTransaction().commit();
Assert.fail("didn't throw expected PersistenceException");
} catch (Exception e) {
// test pass
} finally {
if (em.getTransaction().isActive())
em.getTransaction().rollback();
}
VC vc = new VC();
vc.setVcId("VC1");
em.getTransaction().begin();
C c = new C();
c.setCId("CID2");
try {
em.persist(c);
em.getTransaction().commit();
Assert.fail("didn't throw expected PersistenceException");
} catch (Exception e) {
// test pass
} finally {
if (em.getTransaction().isActive())
em.getTransaction().rollback();
}
VCS vcset = new VCS();
vcset.setVcsId("VCS1");
vcset.setName("VCSET1");
vcset.addVC(vc);
vcset.setE(e);
em.close();
}
C c = new C();
c.setCId("C1");
CM cm = new CM();
cm.setCmId("CM1");
cm.setE(e);
cm.addC(c);
D d = new D();
d.setA("addr");
d.setVc(vc);
d.setId("IM1");
em.persist(e);
em.persist(vc);
em.persist(vcset);
em.persist(c);
em.persist(cm);
em.persist(d);
em.getTransaction().commit();
em.close();
}
public void testUnboundEntities() {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
VCS vcSet = new VCS();
vcSet.setVcsId("VCSET2");
vcSet.setName("VCSET2");
try {
em.persist(vcSet);
em.getTransaction().commit();
Assert.fail("didn't throw expected PersistenceException");
} catch (Exception e) {
// test pass
} finally {
if (em.getTransaction().isActive())
em.getTransaction().rollback();
}
em.getTransaction().begin();
VC vc = new VC();
vc.setVcId("VC2");
try {
em.persist(vc);
em.getTransaction().commit();
Assert.fail("didn't throw expected PersistenceException");
} catch (Exception e) {
// test pass
} finally {
if (em.getTransaction().isActive())
em.getTransaction().rollback();
}
em.getTransaction().begin();
CM cm = new CM();
cm.setCmId("CMID2");
try {
em.persist(cm);
em.getTransaction().commit();
Assert.fail("didn't throw expected PersistenceException");
} catch (Exception e) {
// test pass
} finally {
if (em.getTransaction().isActive())
em.getTransaction().rollback();
}
em.getTransaction().begin();
C c = new C();
c.setCId("CID2");
try {
em.persist(c);
em.getTransaction().commit();
Assert.fail("didn't throw expected PersistenceException");
} catch (Exception e) {
// test pass
} finally {
if (em.getTransaction().isActive())
em.getTransaction().rollback();
}
em.close();
}
public void testQuery() {
EntityManager em = emf.createEntityManager();
List<E> es = (List<E>) em.createQuery(
"Select e from E e where e.name='E1'").getResultList();
Assert.assertEquals(1, es.size());
E e = (E) es.get(0);
Assert.assertEquals("E1", e.getName());
Assert.assertEquals(1, e.getVcss().size());
Assert.assertEquals(1, e.getCms().size());
Assert.assertEquals(1, e.getVcss().size());
public void testQuery() {
EntityManager em = emf.createEntityManager();
List<E> es = (List<E>) em.createQuery(
"Select e from E e where e.name='E1'").getResultList();
Assert.assertEquals(1, es.size());
E e = (E) es.get(0);
Assert.assertEquals("E1", e.getName());
Assert.assertEquals(1, e.getVcss().size());
Assert.assertEquals(1, e.getCms().size());
Assert.assertEquals(1, e.getVcss().size());
// Get virtual container set and check that it has a reference to the
// ensemble
List<VCS> vcss = (List<VCS>) em.createQuery(
"Select vcset from VCS vcset where vcset.vcsId='VCS1'")
.getResultList();
Assert.assertEquals(1, vcss.size());
Assert.assertEquals(e, ((VCS) vcss.get(0)).getE());
em.close();
}
// ensemble
List<VCS> vcss = (List<VCS>) em.createQuery(
"Select vcset from VCS vcset where vcset.vcsId='VCS1'")
.getResultList();
Assert.assertEquals(1, vcss.size());
Assert.assertEquals(e, ((VCS) vcss.get(0)).getE());
em.close();
}
public void testDeletes() {
// Remove VC set and check that all VCs belonging to that set are
// deleted but not the ensemble itself
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
VCS vcset = (VCS) em.createQuery(
"Select vcset from VCS vcset where vcset.vcsId='VCS1'")
.getSingleResult();
em.remove(vcset);
em.getTransaction().commit();
public void testDeletes() {
// Remove VC set and check that all VCs belonging to that set are
// deleted but not the ensemble itself
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
VCS vcset = (VCS) em.createQuery(
"Select vcset from VCS vcset where vcset.vcsId='VCS1'")
.getSingleResult();
em.remove(vcset);
em.getTransaction().commit();
// Get virtualContainer
List<VC> vcs = (List<VC>) em.createQuery(
"Select vc from VC vc where vc.vcId='VC1'")
.getResultList();
Assert.assertEquals(0, vcs.size());
// Get virtualContainer
List<VC> vcs = (List<VC>) em.createQuery(
"Select vc from VC vc where vc.vcId='VC1'")
.getResultList();
Assert.assertEquals(0, vcs.size());
// Make sure E and I are still there
List<E> es = (List<E>) em.createQuery(
"Select e from E e").getResultList();
Assert.assertEquals(1, es.size());
}
// Make sure E and I are still there
List<E> es = (List<E>) em.createQuery(
"Select e from E e").getResultList();
Assert.assertEquals(1, es.size());
}
@Override
@Override
public void tearDown() throws Exception {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
List<E> es = (List<E>) em.createQuery(
"Select e from E e").getResultList();
for (E e : es) {
em.remove(e);
}
if (!isTestsDisabled()) {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
List<E> es = (List<E>) em.createQuery(
"Select e from E e").getResultList();
for (E e : es) {
em.remove(e);
}
em.getTransaction().commit();
em.close();
super.tearDown();
}
em.getTransaction().commit();
em.close();
}
super.tearDown();
}
}