Lev,
I pretty much do the same thing you are asking about... which got
tiresome after a while when the tables changed significantly... so the
top of my tests run a method to drop the appropriate tables in a
@BeforeClass method.
Just put the following code into a method and call it from your JUnit test:
EntityManager em =
Stripersist.getEntityManager(persistenceUnitName);
EntityTransaction emTx = em.getTransaction();
try {
em.createNativeQuery(sqlPrefix + tableName).executeUpdate();
emTx.commit();
}
catch (Exception ex) {
// Do dispute resolution here
emTx.rollback();
}
... most parameters above are obvious except sqlPrefix which I make as
either: "drop table " OR "delete from " depending on what I want
to accomplish. In fact you could tweak the above to run any SQL query
you like. Yes - pretty cool - no Hibernate specific stuff required.
As far as table creation is concerned with the Hibernate update flag
that Thomas already mentioned your tables will automagically get created
once you try to access them if the table was previously dropped.
Bingo... no need to drop the database and no need to specify the exact
SQL to re-create your tables (of course this is wonderful in a test
environment but once you have a database in production a SQL script is a
must).
Lastly, one day I plan on porting my tests over to use DBUnit... which
is the preferred way to go... and should make them run faster and
eliminate the above code... . But until then this works great.
Enjoy.
--Nikolaos
Lev wrote:
> i would like to recreate the database between individual JUnit
> tests -- to ensure a clean starting state of the DB.
>
> instead of relying on the import.sql file, do you know of a way
> to do this programmatically? in other words, is there a method that
> i can write that will do this? that way, i can call it at the beginning
> of each JUnit test for which this is deemed necesarry.
>
> i have used the following in hibernate. is there a way to query the
> annotated classes registered with Stripersist?
>
> public static void recreateDatabase() {
> Configuration config = getInitializedConfiguration();
> new SchemaExport(config).create(true, true);
> }
>
> public static Configuration getInitializedConfiguration() {
> AnnotationConfiguration config = new AnnotationConfiguration();
>
> // add annotated classes
> if (annotatedClasses != null) {
> Iterator<Class> it = annotatedClasses.iterator();
> while (it.hasNext())
> config.addAnnotatedClass(it.next());
> }
> config.configure();
> return config;
> }
>
> (i believe the above code should be credited to cameron mckenzie.)
>
> ------------------------------------------------------------------------------
> This SF.net email is sponsored by Sprint
> What will you do first with EVO, the first 4G phone?
> Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
> _______________________________________________
> Stripes-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/stripes-users
>
>
--
Nikolaos Giannopoulos
Director, BrightMinds Software Inc.
e. [email protected]
w. www.brightminds.org
t. 1.613.822.1700
c. 1.613.797.0036
f. 1.613.822.1915
------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
Stripes-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-users