Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dealing with reserved characters in DBUnit

I'm using DBUnit during integration tests for mocking data for a legacy Spring/Hibernate project I've inherited. Unfortunately a long while ago someone decided that it would be nice to have a user table called user, which is a reserved keyword in our DB. This was then solved by renaming the entity table mapping to `user` which now has been living on for ages.

As I've now a couple years later started to incorporate integration testing this presents a problem as DBUnit does not allow ` characters in it's xml dataset files (it is XML you know...) and the CSV reader would require the file to be called `user`.csv which seems to fail under my OS.

Is there any way to add this table without using either XML or CSV readers of DBUnit, or should I tacke this problem from another angle? One alternative would be to i.e. use another mapping during integration tests so that I could name the table to it's intended name (user). But I dont know whether that is doable or not.

I've been fighting this for 2 days now so I'd love some external feedback.

So, just to clarify. I have an entity such as:

@Entity (name = "`user`")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Auditable
public class User implements UserDetails, Serializable {
  properties etc....

During my integration tests hibernate takes care of creating this an other tables by configuration of the persistence.xml file. When dbunit then tries to populate the data I get errors due to missing table irregardless of how I try to escape the user table name.

like image 523
Billybong Avatar asked Nov 02 '22 09:11

Billybong


1 Answers

I suspect you need the escape pattern configuration.

Something resembling

dbunitConn.getConfig().setProperty(DatabaseConfig.PROPERTY_ESCAPE_PATTERN, "\"?\"");

where dbUnitConn is your dbUnit connection, should do the trick.

like image 109
Don Roby Avatar answered Nov 09 '22 05:11

Don Roby