Skip to content

Spring Hibernate Maven Integration Testing setup

05-Mar-10

This one had me struggling for a day before I could actually get these guys to play nicely together. So here’s the setup and what I was trying to accomplish:

  • Standard maven 2 project layout
  • Spring MVC + IoC
  • Hibernate ORM

The project layout had the spring xml bean definitions under the WEB-INF directory. Which later proved to be a bad choice once I started writing the integration tests. Also log4j was misbehaving and not outputting any logs to the console and complaining. Well I’ll just cut to the chase and explain my setup. By the way I’m using annotated controllers and hibernate entities.

This is the directory structure from the maven project:

bss-dir-tree

Place the hibernate persistence.xml file under resources/META-INF. This is crucial if you want your hibernate annotation configured entities to be picked up automatically or else you will have to manually add each one to your persistence.xml file.

Extend your test case classes from AbstractTransactionalJUnit38SpringContextTests and annotate your test case with the following annotations

@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=true)
@Transactional
@ContextConfiguration("/spring/app-config.xml")

Now spring will automatically initialize the application context for you when you run your tests, and all your hibernate entities will be picked up by hibernate. This will prevent that “Unknown Entity” exception from making your life a living hell. Also make sure your entities import the javax.persistence.Entity and not the hibernate Entity annotation. This should take care of running the tests with the application context.

Sorting out the log4j is also quite simple.

Create a testing log4j configuration file called testing-log4j.xml and place it under src/test/resources. This could be a plain copy of your production log4j.xml file it doesn’t really matter. Then you have to configure maven to understand your new configuration file. Edit your pom.xml file and add this in the build section:

		
			
				src/test/resources
				
					testing-log4j.xml
				
			
		

This will tell maven to look at the src/test/resources directory for test resources and copy them to the output directory target/ when compiling.

Next add this:


					org.apache.maven.plugins
					maven-surefire-plugin
					
						

								log4j.configuration
								testing-log4j.xml
							
						
						true
						false
					
				

This will configure the sure-fire maven test runner plug-in and make it uses the test-log4j.xml when running tests. This will prevent your production log4j configuration from overriding your test version.

Zoundry desktop publishing

15-Jan-10

I’ll be trying out some desktop bloggers, and this is the first one from http://www.zoundryraven.com

New pet project

29-Dec-09

Apart from working at my day job at parkyeri/kartaca and pursuing a masters degree in software engineering, I also do little pet projects like CRET, Tobo etc. The next one in line is a remake of the famous 4X game by micro prose called Master Of Orion. Check out the project page for more info.

Disabling Thunderbird self-signed certificate alert

13-Jul-09

I use postbox, which is a derivative of mozilla thunderbird at work to manage E-mail. We also have an LDAP directory that we use to manage the users here. Naturily we use thunderbirds LDAP integration to address mails easily but postbox complains everytime when I try to compose an e-mail that our LDAP servers certificate is self signed and cannot be trusted. This popup is really annoying so here’s how I disabled it:

In Tools->Account Setting->security->View Certificates

Servers tab, click the add Exception button.

Enter the LDAP servers address and here’s the real trick: The port number after the address. Something like this:

master.directory.myserver:636

It doesn’t work if you don’t enter the port number.

Restart thunderbird and no more annoying popups.

Game theoretic approaches for wireless sensor networks

16-Jun-09

Here is my first paper as a part of my master’s degree in software engineering at BOUN

Groovy Unit Testing with Ant and Team City

11-Jun-09

Well the title has a lot of product names in it some which might not be familiar to all of you, so i’ll try to give short descriptions

  • Groovy: An agile programming language inspired by ruby that compiles to Java classes, works on the JVM and integrates with existing  Java libraries. A must use for agile development and  / or prototyping.
  • Ant: Apache’s build tool
  • Team City: A great continous integration server by Jetbrains.

Groovy has Junit integrated with in it distribution and has the ability to run Groovy classes that extend the GroovyTestCase class directly as unit tests without the need for creating a Test Suit. This is great but when you use this method on Team City (or anyother CI server i presume) you cannot see how many tests passed, and how many faile. So here’s the work around I use to get over this issue:

  • I create a target in my build.xml that compiles all the groovy source code and copies it to a test folder
  • I have another target that compiles all the unit test code and copies them to the same test folder
  • Then I have Ant’s Junit task batch test all off the code with the filter *Test.class. Using this filter is important because Junit will go around looking for Java files as default.

When you deploy this on Team City and use the ant build runner, TC will show you your test stats.

Here is an excerpt from the build.xml file

<target name="compile" depends="init">
<groovyc srcdir="." destdir="../sandbox" classpathref="classpath"/>
<groovyc srcdir="../tests" destdir="../sandbox" classpath="classpath"/>
</target>
<target name="test" depends="compile">
<junit fork="no" haltonfailure="yes" maxmemory="512">
<jvmarg value="-Xmx512m"/>
<classpath refid="classpath"/>
<formatter type="brief" usefile="false"/>
<batchtest>
<fileset dir="../sandbox" includes="**/*Test.class"/>
</batchtest>
</junit>
</target>

Acegi JdbcDaoImpl for logins

06-May-09

Acegi is a great security framework that I’m using at a project at work. If you checked out the tutorial on the site, they use a user.properties file to store the user names, passwords and the authorities of the users. This is o.k for an example but in real world applications you need a database, LDAP, etc. based authentication back-end. So this is how you transfer from the inMemoryDaoImpl to the jdbcDaoImpl to get the user details from the database.
In the applicationContext file:

[bean id="userDetailsService" class="org.acegisecurity.userdetails.jdbc.JdbcDaoImpl"]

the class that will provide this bean is the JdbcDaoImpl class which needs a datasource as a property.

[property name="dataSource"]
      [ref bean="bssDataSource"][/ref]
[/property]

next we have to override the default SQL’s that Acegi uses to get the data:

[property name="usersByUsernameQuery"]
    [value]
        select username, password, enabled from User where username = ?
    [/value]
[/property]
[property name="authoritiesByUsernameQuery"]
    [value]
        select username, authority from User where username = ?
    [/value]
[/property]

And that’s it. You would have to change the SQLs according to your database schema of course.
The final bean is this:

[bean id="userDetailsService" class="org.acegisecurity.userdetails.jdbc.JdbcDaoImpl"]

[property name="dataSource"]
      [ref bean="bssDataSource"][/ref]
[/property]

[property name="usersByUsernameQuery"]
    [value]
        select username, password, enabled from User where username = ?
    [/value]
[/property]
[property name="authoritiesByUsernameQuery"]
    [value]
        select username, authority from User where username = ?
    [/value]
[/property]
[/bean]

High performance large arrays in javascript

31-Mar-09

One of the primary goals of weblaster was to have a fast javascript based application to manage a media collection.
The amount of data we are talking about is an array with around 35.000 elements that contain something like this:

{
"id":"10098",
"track_name":"Sarah Yellin'",
"album_name":"Live Away From The Sun",
"artist_name":"3 Doors Down"
}

that’s around 110 bytes per element times 35K nearly 400K of Javascript. If you load this data with an ajax call, the browser will hog your CPU until Firefox goes gray and won’t recover. (Tested on a 4th generation Macbook Pro with 2 GB RAM, 2.4 Core duo. Believe me, even that can’t handle it). The slowness of eval() is to blame for this. You just can’t eval() such a large array, without blocking the browser. The solution that I used in weblaster for this problem was to embed the array into the page it self. The first few lines in the index.php file shows this.

<script type="text/javascript">
tracks = <?= echo json_encode(data); ?>
</script>

This way the array is loaded and parsed during the execution of the scripts on the page in the current runtime.

Another optimization technique that is well known is the reverse style array iteration. When iterating over large arrays, looking up the arrays length on the conditional check of the for loop is a large performance hit, especially if the array is a global variable.

So the iteration over the arrays are like this:

for (i=array.length-1;i>=0;i++) {
 do_something();
}

Gnome Terminal default size

21-Mar-09

I was sick of having to resize the gnome terminal after each launch, so here’s a way of changing the default size permanently

sudo vim /usr/share/vte/termcap/xterm

change the line

:co#80:it#8:li#24:\

to

:co#120:it#8:li#50:\

if you want a1 20×50 terminal.

Ubuntu Intrepid Macbook pro keyboard backlight

14-Mar-09

Not that I need it but just to squeeze every penny from my mac book pro 4,1 (Penryn):
First enable the MacTel repositories in your

/etc/apt/sources.list

file:

deb http://ppa.launchpad.net/mactel-support/ubuntu intrepid main

then a

apt-get update

and finally install the hal-applesmc for the mactel stuff.
Now add the applesmc to your

/etc/modules

file to load the module at boot time or just

modprobe applesmc

to load it now.
This should make the f1/f2 function keys adjust the LCD brightness. Pressing f5/f6 didn’t switch on the keyboard back light for me but

echo 100 | sudo tee  /sys/class/leds/smc\:\:kbd_backlight/brightness

seems to do the trick.