2

I am working on a new Oracle ADF project, that is using Oragle 10g Database, and I am using Unitils and DBMaintainer in our project for:

  • updating the db structure
  • unittesting
  • read in seed data
  • read in test data
  • List item

In our project, we have 2 schemas, and 2 db users that have privilegies to connect to these schemas. I have them in a folder structure with incremental names and I am using the @convention for script naming.

001_@schemaA_name.sql 
002_@schemaB_name.sql 
003_@schemaA_name.sql 

This works fine with ant and DBMaintainer update task, and I supply the multiple user names by configuring extra elements for the ant task.

<target name="create" depends="users-drop, users-create" description="This tasks ... ">
    <updateDatabase scriptLocations="${dbscript.maintainer.dir}" autoCreateDbMaintainScriptsTable="true">
       <database name="${db.user.dans}" driverClassName="${driver}" userName="${db.user.dans}" password="${db.user.dans.pwd}" url="${db.url.full}" schemaNames="${db.user.dans}" />
        <database name="idp" driverClassName="${driver}" userName="${db.user.idp}"
            password="${db.user.idp.pwd}" url="${db.url.full}" schemaNames="${db.user.idp}" />
    </updateDatabase>
</target>

However, I cant figure out, how to make the DBMaintainer update task create the xsd schemas from my db schemas?

So, I decided to use Unitils, since its update creates xsd schemas. I haven't found any description or documentation for the Unitils ant tasks - can anyone give some hints? For the time being I have figured out to run Unitils by creating a Junit test, with @Dataset annotation. I can make it work with one schema, and one db user. But I am out of ideas how to make it work with multiple users?

Here is the unitils-local.properties setup I have:

database.url=jdbc\:oracle\:thin\:@localhost\:1521\:vipu
database.schemaNames=a,b
database.userName=a
database.password=a1

Can any of you guys give me a tip, how to make Unitils work with the second user/schema ?? I will be extremely gratefull for your help!

APC
  • 144,005
  • 19
  • 170
  • 281
themathmagician
  • 467
  • 5
  • 16

4 Answers4

3

eventually I found a way to inject any unitil.properties of your choice --- by instantiating Unitils yourself!

You need a method that is evoked @BeforeClass, in which you perform something like the following:

@BeforeClass
public void initializeUnitils {
    Properties properties;
    ...
    // load properties file/values depending on various conditions
    ...
    Unitils unitils = new Unitils();
    unitils.init(properties);
    Unitils.setInstance( unitils );     
}

I choose the properties file depending on which hibernate configuration is loaded (via @HibernateSessionFactory), but there should be other options as well

prusswan
  • 6,853
  • 4
  • 40
  • 61
  • Looks like this does not cover Injections such as `@TestDataSource private DataSource dataSource;` - the dataSource will be `null`. Too bad. – mgaert Oct 01 '13 at 12:08
1

I have figure out how to make dbmaintain and unitils work together on multi-database-user support, but the solution is a pure ant hack.

  1. I have set up the configuration for dbmaintain, using multi-database-user support.
  2. I have made a unitils-local.properties file with token keys for replacement.
  3. The init target of my ant script is generating a new unitils-local.properties file, by replacing tokens for username/password/schema with values that are correct for the target envirnonment, and then copies it to the users home directory.
  4. I have sorted the tests into folders, that are prefixed with the schema name
  5. When unitils is invoked, it picks up the unitils-local.properties file just created by the ant script, and does its magic.

Its far from pretty, but it works.

themathmagician
  • 467
  • 5
  • 16
  • I was looking for a programmatic solution to this (to the extent of generating unitils.properties on-the-fly), but eventually reached a similar conclusion – prusswan Jun 30 '11 at 16:01
0

I followed Ryan suggestion. I noticed couple changes when I debugged UnitilsDB.

Following is my running unitils-local.properties:

database.names=db1,db2
database.driverClassName.db1=oracle.jdbc.driver.OracleDriver
database.url.db1=jdbc:oracle:thin:@db1d.company.com:123:db1d
database.userName.db1=user
database.password.db1=password
database.dialect.db1=oracle
database.schemaNames.db1=user_admin

database.driverClassName.db2=oracle.jdbc.driver.OracleDriver
database.url.db2=jdbc:oracle:thin:@db2s.company.com:456:db2s
database.userName.db2=user
database.password.db2=password
database.dialect.db2=oracle

Make sure to use @ConfigurationProperties(prefix = "database.db1") to connecto to particular database in your test case:

@RunWith(UnitilsJUnit4TestClassRunner.class)
@ConfigurationProperties(prefix = "database.db1")
@Transactional
@DataSet
public class MyDAOTest {

..

}
0

Check out this link: http://www.dbmaintain.org/tutorial.html#From_Java_code

Specifically you may need to do something like:

databases.names=admin,user,read
database.driverClassName=oracle.jdbc.driver.OracleDriver
database.url=jdbc:oracle:thin://mydb:1521:MYDB
database.admin.username=admin
database.admin.password=adminpwd
database.admin.schemaNames=admin
database.user.userName=user
database.user.password=userpwd
database.user.schemaNames=user
database.read.userName=read
database.read.password=readpwd
database.read.schemaNames=read

Also this link may be helpful: http://www.dbmaintain.org/tutorial.html#Multi-database__user_support

Ryan Walls
  • 6,962
  • 1
  • 39
  • 42
  • Yes, I know of this multi-database DBMaintain configuration. The trouble is, that Unitils is using DBMaintain as a submodule, but seems not to honor the multi-database configuration. – themathmagician May 31 '11 at 13:29
  • Interesting. Yeah, I've been having similar problems with Unitils' dbMaintain module not behaving the same as what is documented on dbMaintain's site. See my question here: [link](http://stackoverflow.com/questions/6143505/using-unitils-dbmaintain-to-maintain-database-how-to-exclude-scripts-from-being). My assumption at this point is that Unitils is using an older version of dbMaintain. – Ryan Walls May 31 '11 at 14:34
  • Follow up: my assumption was correct. Unitils is using an old version of dbMaintain under the covers. See here: http://stackoverflow.com/a/6414416/411229 – Ryan Walls Feb 21 '12 at 20:01