Wednesday, November 28, 2012

DbUnit teardown exception

Recently new problem appears in JUnit test logs:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Cannot truncate a table referenced in a foreign key constraint (`cub_test`.`api_month_stat`, CONSTRAINT `api_month_stat_access` FOREIGN KEY (`id_api_access`) REFERENCES `cub_test`.`api_access` (`id_api_access`))
 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
 at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
 at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
 at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
 at com.mysql.jdbc.Util.getInstance(Util.java:381)
 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030)
 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
 at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)
 at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
 at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2536)
 at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2465)
 at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:734)
 at com.mchange.v2.c3p0.impl.NewProxyStatement.execute(NewProxyStatement.java:1006)
 at org.dbunit.database.statement.SimpleStatement.executeBatch(SimpleStatement.java:66)
 at org.dbunit.operation.DeleteAllOperation.execute(DeleteAllOperation.java:122)
 at org.dbunit.operation.TruncateTableOperation.execute(TruncateTableOperation.java:84)
 at org.dbunit.AbstractDatabaseTester.executeOperation(AbstractDatabaseTester.java:179)
 at org.dbunit.AbstractDatabaseTester.onTearDown(AbstractDatabaseTester.java:82)
 at org.dbunit.DatabaseTestCase.tearDown(DatabaseTestCase.java:146)

When I tried to perform manually tear down operation truncate on table following problem appears:
mysql> truncate table api_access;
ERROR 1701 (42000): Cannot truncate a table referenced in a foreign key constraint (`cub_test`.`api_month_stat`, CONSTRAINT `api_month_stat_access` FOREIGN KEY (`id_api_access`) REFERENCES `cub_test`.`api_access` (`id_api_access`))
mysql> 
Problem is closely described at http://dev.mysql.com/doc/refman/5.0/en/truncate-table.html. In our case was solution to use "DELETE_ALL" as tear down operation. Tear down operation define what happen when test is executed. It could loo like:
    @Override
    protected DatabaseOperation getTearDownOperation() throws Exception {
 return DatabaseOperation.DELETE_ALL;
    }

Hibernate problem

We have met following problem in JUnit logs:
2012.11.28.22.42.45 ERROR hibernate.tool.hbm2ddl.SchemaUpdate - Unsuccessful: create table api_access (id_api_access integer not null, id_user integer not null, month_limit integer, api_key varchar(250), primary key (id_api_access)) type=InnoDB
2012.11.28.22.42.45 ERROR hibernate.tool.hbm2ddl.SchemaUpdate - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'type=InnoDB' at line 1
Problem is that hibernate use invalid dialect. In hibernate configuration property hibernate.dialect should have value org.hibernate.dialect.MySQL5InnoDBDialect.

Tuesday, November 20, 2012

How to display code listing at blogger.com

I found few articles about adding syntax highlighting to blogger.com. All of them was based on Java Script library SyntaxHighlighter. All post describe old version of SyntaxHighlighter. Because I had to play little bit with here is short guide how to use it.
First go to blog administration interface. Than choose "Template" and "Edit HTML". Than adjust HTML template just before closing head tag as it's on next snippet:
<head>

    ...

    <script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'/>
    <link href='http://alexgorbatchev.com/pub/sh/current/styles/shCore.css' rel='stylesheet' type='text/css'/>

    <script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js' type='text/javascript'/>
    <script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPlain.js' type='text/javascript'/>
    <script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js' type='text/javascript'/>

    <link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/>

</head>

Adjust HTML template before closing body tag as it's on next snippet:
</body>

   ...

   <script type='text/javascript'>
       SyntaxHighlighter.all()
   </script>

</body>

Now close and save HTML template. Open new post and switch to "HTML" editing. Write post with following content:
<pre class="brush:java">
public void hello(final String name){  
    System.out.println("Hello " + name);  
}
</pre>

If is everything fine than you should see:
public void hello(final String name){  
    System.out.println("Hello " + name);  
}


When XML or HTML have to be displayed and highlighted it have to written in post with HTML entities. To find proper tool try google search for "encode html to entities".

How to use public maven repository

Nexus started provide maven repository for free.  It's also for non-opensource projects.

All information how to participate are at https://docs.sonatype.org/display/Repository/Sonatype+OSS+Maven+Repository+Usage+Guide#SonatypeOSSMavenRepositoryUsageGuide-7a.1.POMandsettingsconfig

Carefully go through all steps. For releasing in staging repository you artifacts have to be signed. There is also nice article how to do that at https://docs.sonatype.org/display/Repository/How+To+Generate+PGP+Signatures+With+Maven