<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-829770856568193696</id><updated>2011-07-08T08:55:52.216-07:00</updated><title type='text'>Reflective Java</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://reflectivejava.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/829770856568193696/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://reflectivejava.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Ben Turner</name><uri>http://www.blogger.com/profile/01889838040524599770</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://3.bp.blogspot.com/_GEBw_tPBC6M/SRF9gmuZvAI/AAAAAAAAAHc/OMr1fvIEYNk/S220/51832528-M.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>3</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-829770856568193696.post-5155531477917557124</id><published>2007-10-16T04:29:00.001-07:00</published><updated>2007-10-18T06:31:37.677-07:00</updated><title type='text'>It's CLOBbering time</title><content type='html'>&lt;div xmlns="http://www.w3.org/1999/xhtml"&gt;Sometimes in your database life, VARCHAR2(4000) just wont cut the mustard. And as I discovered recently, that's as big as Oracle VARCHARs will go. It's time to break out the CLOBs...&lt;br /&gt;&lt;br /&gt;CLOBs stand for... well, Character LOBs. I'm afraid LOB doesn't mean anything (so says Jim Starkey, and he invented the term). But some people call them Large objects, others say Locator OBjects as they work like pointers. This also makes dropping a CLOB in for a VARCHAR less than straight forward, as they work from this pointer based access mechanism. Like SAX and DOM with XML parsing, it's two different philosophy's on large data access.&lt;br /&gt;&lt;br /&gt;But I am a modern day Spring + Hibernate Java guy, so straight away I look to Hibernate. This offers support for mapping &lt;em&gt;java.sql.Clob&lt;/em&gt; or &lt;em&gt;java.sql.Blob&lt;/em&gt; objects into the database. But I'm not too keen to add them into my domain model and tie it to JDBC. It might also wreak havoc with my unit tests, as you can't access LOB properties without a database connection.&lt;br /&gt;&lt;br /&gt;So onward up the stack to Spring and it's &lt;a href="http://www.springframework.org/docs/api/org/springframework/jdbc/support/lob/OracleLobHandler.html"&gt;OracleLobHandler&lt;/a&gt; which is an Oracle specific version of the more generic &lt;a href="http://www.springframework.org/docs/api/org/springframework/jdbc/support/lob/DefaultLobHandler.html"&gt;DefaultLobHandler&lt;/a&gt; class, which is an: &lt;blockquote&gt;Abstraction for handling large binary fields and large text fields in&lt;br /&gt;specific databases, no matter if represented as simple types or Large OBjects (from the &lt;a href="http://www.springframework.org/docs/api/org/springframework/jdbc/support/lob/LobHandler.html"&gt;LobHandler javadoc&lt;/a&gt;).&lt;br /&gt;&lt;/blockquote&gt;Figuring out the best way to use this object came from &lt;a href="http://jroller.com/revetkn/entry/handling_oracle_blobs_with_hibernate"&gt;Mark Allen's Weblog&lt;/a&gt;, which suggests simply adding the LobHandler to the Spring SessionFactory, then use a Spring support type for CLOBs, such as ClobStringType, in the Hibernate mapping file. Simple !&lt;br /&gt;&lt;br /&gt;So firstly I add the following in my datasources Spring config file:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family: trebuchet ms;"&gt;&lt;pre&gt;&lt;br /&gt;&amp;lt;bean id="sessionFactory" name="sessionFactory"&lt;br /&gt; class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&amp;gt;&lt;br /&gt; ...&lt;br /&gt; &amp;lt;property name="lobHandler"&amp;gt;&lt;br /&gt;     &amp;lt;bean class="org.springframework.jdbc.support.lob.OracleLobHandler"&amp;gt;&lt;br /&gt;         &amp;lt;property name="nativeJdbcExtractor"&amp;gt;&lt;br /&gt;             &amp;lt;bean class="org.spring...CommonsDbcpNativeJdbcExtractor" /&amp;gt;&lt;br /&gt;         &amp;lt;/property&amp;gt;&lt;br /&gt;     &amp;lt;/bean&amp;gt;&lt;br /&gt; &amp;lt;/property&amp;gt;&lt;br /&gt;&amp;lt;/bean&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Then I tweak the property in my Hibernate mapping to use the Spring support class:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family: trebuchet ms;"&gt;&lt;pre&gt;&lt;br /&gt;&amp;lt;property name="message" column="MESSAGE" lazy="true"&lt;br /&gt; type="org.springframework.orm.hibernate3.support.ClobStringType" /&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;And that pretty much does the magic. I change the column from VARCHAR2(4000) to CLOB in the database, and then the above code seemlessly turns the new CLOBs back into Java Strings as I pull them back, requiring no code changes at all. Hibernate can be a pain at times, but as always, adding Spring to the mix makes a change like this very smooth. Once you know how, of course ;)&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/829770856568193696-5155531477917557124?l=reflectivejava.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reflectivejava.blogspot.com/feeds/5155531477917557124/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=829770856568193696&amp;postID=5155531477917557124' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/829770856568193696/posts/default/5155531477917557124'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/829770856568193696/posts/default/5155531477917557124'/><link rel='alternate' type='text/html' href='http://reflectivejava.blogspot.com/2007/10/it-clobbering-time.html' title='It&amp;#39;s CLOBbering time'/><author><name>Ben Turner</name><uri>http://www.blogger.com/profile/01889838040524599770</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://3.bp.blogspot.com/_GEBw_tPBC6M/SRF9gmuZvAI/AAAAAAAAAHc/OMr1fvIEYNk/S220/51832528-M.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-829770856568193696.post-6437714853493332294</id><published>2007-08-03T02:17:00.000-07:00</published><updated>2007-08-03T03:29:27.856-07:00</updated><title type='text'>Spring Framework Integration Tests</title><content type='html'>Having been a huge fan of the Spring framework, and esp. the JDBC support within it, I have had a shocking two days working on the most simple of tests. I wrote a simple Repository / DAO to pull out a four parameter javabean from a four column table, providing two finder methods to query for an object by two of the four parameters.&lt;br /&gt;&lt;br /&gt;This is the ABC of database access, and code that most of our team can write in our sleep. But the problem came not with the code, but attempting to TEST the code.&lt;br /&gt;&lt;br /&gt;The obvious choice for testing Spring JDBC Repository code is the famously named &lt;span style="font-family: courier new;"&gt;AbstractTransactionalDataSourceSpringContextTests&lt;/span&gt; class. This provides a transaction to work in, so all changes are wiped between tests, as well as a convenient &lt;span style="font-family: courier new;"&gt;JdbcTemplate&lt;/span&gt; object configured in your Spring context to point to the database. All in all this allowed me to write a quick test to wipe the table I was interested in clean, insert one row, then use the Repository I was testing to pull back that one row and confirm the object wasn't null.&lt;br /&gt;&lt;br /&gt;And it failed... so I tweaked and fixed and refactored and guessed and shouted. But it still failed. I got down to just running two lines of code in my test:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt;   jdbcTemplate.update("Insert into CLIENTS (ID, DESCRIPTION) VALUES (1, 'TEST'");&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt;   assertTrue(jdbcTemplate.queryForInt("SELECT count(0) from CLIENTS ") &gt; 0);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;And... it failed !?! This led to others (convinced of my stupidity) chipping in with ideas like checking the output of the deletes and inserts - they returned the numbers of rows affected as expected. But it seemed that EVERY SQL statement ran seemed to have no effect on the others - as if the transaction might be rolling back between every single statement ! I could insert the same row with the same primary key twice, and each time it told me it had added a row, and each time a select statement bought back 0 rows.&lt;br /&gt;&lt;br /&gt;This was frustrating - the "my first SQL for dummies" step in Spring JDBC and it had me grinding my teeth and swearing under my breath, not to mention impacting on a tight deadline (what other kind are there ?!) and raising the stress levels - I needed to step back. Something wasn't right with my transaction manager, but what could it be...&lt;br /&gt;&lt;br /&gt;First step - got in with the debugger and look what I've setup. My test case has the &lt;span style="font-family: courier new;"&gt;jdbcTemplate&lt;/span&gt; setup with the same &lt;span style="font-family: courier new;"&gt;DataSource&lt;/span&gt; as the &lt;span style="font-family: courier new;"&gt;TransactionManager&lt;/span&gt;. Next step - go through the code. It seemed that all the Jdbc statements were running with &lt;span style="font-family: courier new;"&gt;numberOfTransactions&lt;/span&gt; set to 0 and &lt;span style="font-family: courier new;"&gt;transactionStatus&lt;/span&gt; set to &lt;span style="font-family: courier new;"&gt;null&lt;/span&gt; - clearly no transaction was working around the code here ? But equally a &lt;span style="font-family: courier new;"&gt;transactionManager&lt;/span&gt; MUST be set up, otherwise the test case would not actually start (the spring code throws an exception).&lt;br /&gt;&lt;br /&gt;Some more investigation and delving into the spring source code, and suddenly I see my error...&lt;br /&gt;&lt;br /&gt;I have overridden &lt;span style="font-family: courier new;"&gt;onSetUp()&lt;/span&gt;. With the spring transactional test cases, the setUp()  method is final, so &lt;span style="font-family: courier new;"&gt;onSetUp()&lt;/span&gt; is provided. But this method is non-empty in &lt;span style="font-family: courier new;"&gt;AbstractTransactionalSpringContextTests&lt;/span&gt;, and it is here that all the transactional magic happens. And of course, in overriding it, I had not thought to call &lt;span style="font-family: courier new;"&gt;super.onSetUp()&lt;/span&gt;, so stopped all of the transactional set up happening. I can only assume the side effect of every statement not affecting the other was caused by &lt;span style="font-family: courier new;"&gt;autoCommit&lt;/span&gt; being set to false on the connection as well.&lt;br /&gt;&lt;br /&gt;The lesson here - make sure to call the &lt;span style="font-family: courier new;"&gt;super.onSetUp()&lt;/span&gt; method (a good rule for all Junit set up methods, not just this example. Or more specific to this problem, use the provided &lt;span style="font-family: courier new;"&gt;onSetUpBeforeTransaction()&lt;/span&gt; and &lt;span style="font-family: courier new;"&gt;onSetUpInTransaction()&lt;/span&gt; methods which are empty place holder methods for subclasses to add behaviour. This quick change applied and everything works normally and makes sense again. Finally my brain works and I can get onto some new code :)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/829770856568193696-6437714853493332294?l=reflectivejava.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reflectivejava.blogspot.com/feeds/6437714853493332294/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=829770856568193696&amp;postID=6437714853493332294' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/829770856568193696/posts/default/6437714853493332294'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/829770856568193696/posts/default/6437714853493332294'/><link rel='alternate' type='text/html' href='http://reflectivejava.blogspot.com/2007/08/spring-framework-integration-tests.html' title='Spring Framework Integration Tests'/><author><name>Ben Turner</name><uri>http://www.blogger.com/profile/01889838040524599770</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://3.bp.blogspot.com/_GEBw_tPBC6M/SRF9gmuZvAI/AAAAAAAAAHc/OMr1fvIEYNk/S220/51832528-M.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-829770856568193696.post-1248142286403544169</id><published>2007-06-26T05:40:00.000-07:00</published><updated>2007-06-26T06:52:24.938-07:00</updated><title type='text'>Automated parametrized testing or testing without the repetition</title><content type='html'>The world of software testing is huge. Unit testing, integration testing, system testing, user acceptance testing, QA testing, load testing, DR testing and so forth. And good luck on finding two people who agree on the definitions of each testing category...&lt;br /&gt;&lt;br /&gt;From a developers view, the water starts to get muddy on the fringes of unit tests. Basic unit tests are easy, and if well designed, more advanced tests with mock objects or stubs can fill out the unit test suite to the powerful and respected tool that many software development maven's proclaim it to be.&lt;br /&gt;&lt;br /&gt;But then you find yourself in integration testing territory - where the options and pathways of what &lt;span style="font-style: italic;"&gt;could &lt;/span&gt;happen tend to grow exponentially with each layer. And we developers love layers and interfaces. So sometimes integration testing to pull out error conditions and alternate pathways can be tricky, and perhaps redundant if our unit test suite has covered it.&lt;br /&gt;&lt;br /&gt;But one thing I never really use integration tests for enough is automated "poke" testing -  grabbing a whole bunch of data and firing it at the server to see if I can break it. Normally this requires live data and a little business understanding to do well, not often things developers might possess ! Tools like &lt;a href="http://fitnesse.org/"&gt;FitNesse &lt;/a&gt;are built around this principle. But as a developer I still find myself prodding software manually, rather than automated this "just bung a load of data in".&lt;br /&gt;&lt;br /&gt;One of the historical reasons for this has been a lack of availability of parametrized testing frameworks or tools in the open source space to make this easier for me. But with the general acceptance point that JUnit 4.0 is "standard", I can make use of the &lt;a href="http://junit.sourceforge.net/javadoc_40/org/junit/runners/Parameterized.html"&gt;parametrized &lt;/a&gt;test runner that provides this functionality. It's not too clean (the TestNG approach looks a little cleaner) but it seems to do the trick. Now when I get new test data, I can just throw it onto the stack and whenever I need to sanity check my code just inject all the test data through the relevant part of the system in one click.&lt;br /&gt;&lt;br /&gt;In many ways, this approach is obvious. Child's play almost. But sometimes I can get so buried in the wide sea of information on Java and programming in general that I fail to stop, take a couple of minutes and reflect. It's only then that such things become highlighted as so obvious, and with that reflective knowledge I should be able to build up a powerful set of test suites. Just don't ask we which of them are integration tests and which are systems tests...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/829770856568193696-1248142286403544169?l=reflectivejava.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reflectivejava.blogspot.com/feeds/1248142286403544169/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=829770856568193696&amp;postID=1248142286403544169' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/829770856568193696/posts/default/1248142286403544169'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/829770856568193696/posts/default/1248142286403544169'/><link rel='alternate' type='text/html' href='http://reflectivejava.blogspot.com/2007/06/automated-parametrized-testing-or.html' title='Automated parametrized testing or testing without the repetition'/><author><name>Ben Turner</name><uri>http://www.blogger.com/profile/01889838040524599770</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://3.bp.blogspot.com/_GEBw_tPBC6M/SRF9gmuZvAI/AAAAAAAAAHc/OMr1fvIEYNk/S220/51832528-M.jpg'/></author><thr:total>0</thr:total></entry></feed>
