I finally had a few spare cycles the past couple days and put together a prototype test component. At this point all of this is flexible, but I want to get a feeling from others whether a change like this would be welcomed. I'll note that I did some hacking/poaching from the eclipselink.jpa.test build to get things working. I'm not a build guy, but I wanted to get something that generally works... so don't get too hung up on the build pieces.
My end goal is to have a framework that makes it very easy for someone to create a new jse JPA test. I want to stress that this new component won't be an end-all be-all test component. The point of it is that anyone with JPA experience can easily and quickly create a JUnit test that can test a large majority of the runtime. That being said, there are certainly some aspects of the runtime that this component will not be able to test.
A few things to note about this component :
* It uses a single static weaving configuration. That could change in the future if cases come up, but at this point it isn't necessary. When the tests run via ant Entities will be enhanced at compile time. You can also easily use the -javaagent to enhance while running in Eclipse.
* It is NOT targeted to run in an app server
* For most test cases a persistence unit defined in a persistence.xml is not required.
* It depends on eclipselink.ddl-generation for table generation.
Attached below are a couple patches that have the entire change, but I'll give a quick summary.
To create a new testcase :
* Define your Entity model. Entities/embeddables/etc must be in a *.model package
* Define a Test class (in a *.test package) that extends AbstractJPATest. You must implement the getManagedTypes() method to return the Entities that your testcase is using. This method is analogous to the <class> section in a p.xml.
* Optionally override the getPersistentProperties() method to return an array of persistence properties your test might need to use.
* In each test method, there is an instance variable 'emf' that is based off the properties provided by your test class.
* Test cases need to ensure that EMs are cleaned up properly
* AbstractJPATest should clean up tables after test classes complete, but it doesn't yet.
To run a new testcase:
* Either run the default target in eclipselink.test.jpa.jse, providing db connection details in test.properties
* To run in Eclipse, provide db connection details as SystemProperties -Djavax.blah bah
Example test :
public class TestInheritancePrePersist extends AbstractJPATest {
@Override
public Class<?>[] getManagedTypes() {
return new Class<?>[] { ChildEntity.class, ParentEntity.class };
}
@Override
public String[] getPersistentProperties() {
return new String[] { "eclipselink.log", "FINE" };
}
@Test
public void testMultiplePrePersist() {
EntityManager em = emf.createEntityManager();
try {
em.getTransaction().begin();
ChildEntity ce = new ChildEntity();
em.persist(ce);
em.getTransaction().commit();
em.clear();
Assert.assertEquals(1, ce.getPrePersistChild());
Assert.assertEquals(1, ce.getPrePersistParent());
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
em.close();
}
}
...
Wow, that email got a lot longer than I intended. Anyway, I'm looking for a thumbs up / thumbs down on the direction I'm heading. If thumbs up, I'll start committing some code to master later this week.
Thanks,
Rick