<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.3.3" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>

<channel>
	<title>Java Blog - Java, J2EE, SOA, Spring and Hibernate &#187; Spring</title>
	<link>http://javablog.info</link>
	<description></description>
	<pubDate>Thu, 17 Apr 2008 00:24:36 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.3</generator>
	<language>en</language>
			<item>
		<title>Spring Based JUnit Integration Test approach</title>
		<link>http://javablog.info/2007/11/29/spring-based-junit-integration-tests/</link>
		<comments>http://javablog.info/2007/11/29/spring-based-junit-integration-tests/#comments</comments>
		<pubDate>Thu, 29 Nov 2007 04:13:58 +0000</pubDate>
		<dc:creator>Ravi Nallakukkala</dc:creator>
		
		<category><![CDATA[Design]]></category>

		<category><![CDATA[Spring]]></category>

		<guid isPermaLink="false">http://javablog.info/2007/04/09/spring-based-junit-integration-tests/</guid>
		<description><![CDATA[The Spring Framework provides a good support for the integration tests for enterprise software applications and these support classes are packaged in spring-mock.jar and all these classes are JUnit test classes. org.springframework.test package provides valuable JUnit TestCase superclasses for integration testing using a Spring container and this approach is completely independent of any web/ application [...]]]></description>
			<content:encoded><![CDATA[<p>The Spring Framework provides a good support for the integration tests for enterprise software applications and these support classes are packaged in spring-mock.jar and all these classes are JUnit test classes. org.springframework.test package provides valuable JUnit TestCase superclasses for integration testing using a Spring container and this approach is completely independent of any web/ application server.</p>
<p>Spring out-of-box provide the following functionality:</p>
<ul>
<li>Transaction management though spring container.</li>
<li>Dependency Injection feature.</li>
<li>Spring Ioc Container caching between execution.</li>
</ul>
<p>On Surface, Spring Test cases can be classified into</p>
<ul>
<li>JUnit with Dependency Injection.</li>
<li>Junit with Dependency Injection and Transactional Control.</li>
</ul>
<p><strong>Spring based JUnit with Dependency Injection</strong></p>
<p>For JUnit Classes which need to use Dependency injection feature of spring will be required to inherit the class â€˜AbstractDependencyInjectionSpringContextTestsâ€˜ and the subclass need to implement the superclassâ€™s abstract method â€˜protected abstract String[] getConfigLocations();â€™ the implementation of this method need to return the spring configuration XML files which are required to participate in the execution of the test case.<br />
E.g,<br />
public final class ExampleDaoTests extends <strong>AbstractDependencyInjectionSpringContextTests</strong>  {</p>
<p>// this reference will be automatically injected through spring<br />
private ExampleDao exampleDao;</p>
<p>// a setter method to enable DI for the Dao instance variable<br />
public void setExampleDao(ExampleDao exampleDao) {<br />
this.exampleDao = exampleDao;<br />
}</p>
<p>public void testExample() throws Exception {<br />
BusinessObject bo = this.exampleDao.loadBo(new Long(10));<br />
assertNotNull(title);<br />
}</p>
<p>// specifies the Spring configuration to load for this test.<br />
protected String[] getConfigLocations() {<br />
return new String[] { â€œclasspath:com/foo/daos.xmlâ€ };<br />
}<br />
}</p>
<p><strong>&lt;&lt; Spring Configuration file &gt;&gt;</strong></p>
<p>&lt;?xml version=â€1.0? encoding=â€UTF-8??&gt;<br />
&lt;!DOCTYPE beans PUBLIC  â€œ-//SPRING//DTD BEAN 2.0//ENâ€<br />
â€œhttp://www.springframework.org/dtd/http://www.springframework.org/dtd/spring-beans-2.0.dtdâ€&gt;<br />
&lt;beans&gt;</p>
<p>&lt;!â€“ this bean will be injected into the ExampleDaoTests class â€“&gt;<br />
&lt;bean id=â€exampleDaoâ€ class=â€com.foo.dao.hibernate.ExampleDaoâ€&gt;<br />
&lt;property name=â€sessionFactoryâ€ ref=â€sessionFactoryâ€/&gt;<br />
&lt;/bean&gt;</p>
<p>&lt;bean id=â€sessionFactoryâ€ class=â€org.springframework.orm.hibernate3.LocalSessionFactoryBeanâ€&gt;<br />
&lt;!â€“ dependencies elided for clarity â€“&gt;<br />
&lt;/bean&gt;<br />
&lt;/beans&gt;</p>
<p><strong>Spring based JUnit with Transaction Control/ Integration Test</strong></p>
<p>One common problem in integration test is to access a database and still maintain the state of the persistence store. When using a database, changes to the database state may effect the future tests and CURD operations canâ€™t be verified outside a transaction.</p>
<p>The org.springframework.test.AbstractTransactionalSpringContextTests superclass (and subclasses) exist to meet this requirement. By default, this class create and rollback a transaction for each test. From a developer/ development point of view, You simply write code that assumes the existence of a transaction.</p>
<p>It has to be noted that, this class extends AbstractDependencyInjectionSpringContextTests so spring dependency injection is made available for all the subclasses extending AbstractTransactionalSpringContextTests</p>
<p>Spring based JUnit also provides an option to commit a transaction (unusual, but occasionally useful when you want a particular test to populate the database) - you can invoke the setComplete() method inherited from AbstractTransactionalSpringContextTests. This will force the transaction to commit instead of roll back.</p>
<p>There is also convenient ability to end a transaction before the test case ends, through calling the endTransaction() method. This will roll back the transaction by default, and commit it only if setComplete() had previously been called. This functionality is useful if you want to test the behavior of â€˜disconnectedâ€™ data objects, such as Hibernate-mapped objects that will be used in a web or remoting tier outside a transaction. Often, lazy loading errors are discovered only through UI testing; if you call endTransaction() you can ensure correct operation of the UI through your JUnit test suite.<br />
E.g,</p>
<p>//as this test class extends â€˜AbstractTransactionalSpringContextTestsâ€™ so all the<br />
//test methods will be bound with a transaction.<br />
public final class ExampleDaoTests extends <strong>AbstractTransactionalSpringContextTests </strong> {</p>
<p>// this reference will be automatically injected through spring<br />
private ExampleDao exampleDao;</p>
<p>// a setter method to enable DI for the Dao instance variable<br />
public void setExampleDao(ExampleDao exampleDao) {<br />
this.exampleDao = exampleDao;<br />
}</p>
<p>public void testExample() throws Exception {<br />
BusinessObject bo = createBo(); // create the Business Objects<br />
exampleDao.create(bo); // Business Object will be created</p>
<p>//now retrieve the object which was created in the database.<br />
BusinessObject returnedObject = exampleDao.findById(bo.getId());<br />
assertEquals(bo.getId(), returnedObject.getId());<br />
//after the execution of the test method, data which was inse<br />
}</p>
<p>// specifies the Spring configuration to load for this test.<br />
protected String[] getConfigLocations() {<br />
return new String[] { â€œclasspath:com/foo/daos.xmlâ€ };<br />
}<br />
}</p>
<p><strong>&lt;&lt; Spring Configuration file &gt;&gt;</strong></p>
<p>&lt;?xml version=â€1.0? encoding=â€UTF-8??&gt;<br />
&lt;!DOCTYPE beans PUBLIC  â€œ-//SPRING//DTD BEAN 2.0//ENâ€<br />
â€œhttp://www.springframework.org/dtd/http://www.springframework.org/dtd/spring-beans-2.0.dtdâ€&gt;<br />
&lt;beans&gt;</p>
<p>&lt;!â€“ this bean will be injected into the ExampleDaoTests class â€“&gt;<br />
&lt;bean id=â€exampleDaoâ€ class=â€com.foo.dao.hibernate.ExampleDaoâ€&gt;<br />
&lt;property name=â€sessionFactoryâ€ ref=â€sessionFactoryâ€/&gt;<br />
&lt;/bean&gt;</p>
<p>&lt;bean id=â€sessionFactoryâ€ class=â€org.springframework.orm.hibernate3.LocalSessionFactoryBeanâ€&gt;<br />
&lt;!â€“ dependencies elided for clarity â€“&gt;<br />
&lt;/bean&gt;</p>
<p>&lt;/beans&gt;</p>
]]></content:encoded>
			<wfw:commentRss>http://javablog.info/2007/11/29/spring-based-junit-integration-tests/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Spring Framework Declarative Transaction with Annotations</title>
		<link>http://javablog.info/2007/04/10/spring-framework-declarative-transaction-with-annotations/</link>
		<comments>http://javablog.info/2007/04/10/spring-framework-declarative-transaction-with-annotations/#comments</comments>
		<pubDate>Tue, 10 Apr 2007 06:20:40 +0000</pubDate>
		<dc:creator>Ravi Nallakukkala</dc:creator>
		
		<category><![CDATA[Spring]]></category>

		<guid isPermaLink="false">http://javablog.info/2007/04/10/spring-framework-declarative-transaction-with-annotations/</guid>
		<description><![CDATA[
Using Spring 2.0 and Java 5, it is possible to declare the transactions using annotation-based approach.
Spring framework supports @Transactional annotation for declarative transaction handling, this annotation can be used on a concrete class or method (with public visibility) of a concrete class. This annotation can be used on an interface, but this will only work [...]]]></description>
			<content:encoded><![CDATA[<ul>
<li>Using Spring 2.0 and Java 5, it is possible to declare the transactions using annotation-based approach.</li>
<li>Spring framework supports @Transactional annotation for declarative transaction handling, this annotation can be used on a concrete class or method (with public visibility) of a concrete class. This annotation can be used on an interface, but this will only work as you would expect it to if you are using interface-based proxies.</li>
<li>The default @Transactional settings are:
<ul>
<li>The propagation setting is PROPAGATION_REQUIRED</li>
<li>The isolation level is ISOLATION_DEFAULT</li>
<li>The transaction is read/write</li>
<li>The transaction timeout defaults to the default timeout of the underlying transaction system, or none if timeouts are not supported</li>
<li>Any RuntimeException will trigger rollback, and any checked Exception will not.</li>
</ul>
</li>
<li>Enabling the configuration of transactional behavior based on annotations can be done as follows</li>
</ul>
<p>&lt;tx:annotation-driven transaction-manager=&#8221;txManager&#8221;/&gt; where txManager points the underlying transaction manager and the tx namespace refers to http://www.springframework.org/schema/tx</p>
<ul>
<li>Transaction rollback can be controlled though Transactional annotation for the property â€˜rollbackForâ€™ and â€˜rollbackForClassNameâ€™.</li>
<li>Timeout for the transaction can be controlled through the Transactional annotation for the property &#8216;timeout&#8217;.</li>
<li>Example usage for Declarative transaction with Annotations</li>
</ul>
<p> <a href="http://javablog.info/2007/04/10/spring-framework-declarative-transaction-with-annotations/#more-28" class="more-link">(more&#8230;)</a></p>
]]></content:encoded>
			<wfw:commentRss>http://javablog.info/2007/04/10/spring-framework-declarative-transaction-with-annotations/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Dozer - DTO to Business object (Bo) data transfer</title>
		<link>http://javablog.info/2007/03/25/dozer/</link>
		<comments>http://javablog.info/2007/03/25/dozer/#comments</comments>
		<pubDate>Sun, 25 Mar 2007 09:03:42 +0000</pubDate>
		<dc:creator>Ravi Nallakukkala</dc:creator>
		
		<category><![CDATA[Spring]]></category>

		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://javablog.info/2007/03/25/dozer/</guid>
		<description><![CDATA[Dozer is a powerful, but simple Java Bean to Java Bean mapper that recursively copies data from one object to another.
Dozer supports simple property mapping, complex type mapping, bi-directional mapping, implicit-explicit mapping, as well as recursive mapping. This includes mapping collection attributes that also need mapping at the element level.
This is different from Bean utils, [...]]]></description>
			<content:encoded><![CDATA[<p>Dozer is a powerful, but simple Java Bean to Java Bean mapper that recursively copies data from one object to another.<br />
Dozer supports simple property mapping, complex type mapping, bi-directional mapping, implicit-explicit mapping, as well as recursive mapping. This includes mapping collection attributes that also need mapping at the element level.</p>
<p>This is different from Bean utils, which copies the data recursively for the object which has the same property names. Bean-Utils has a restriction for working with complex data types.</p>
<p>Dozer tool can be found <a href="http://dozer.sourceforge.net/" target="_blank">here</a></p>
<p>here&#8217;s some of the key advantages found on Dozer</p>
<ul>
<li>Can define the mapping / exlude mapping in a configuration mapping file.</li>
<li>Support for Spring (tested Dozer 3.0/ 3.1 with Spring 2.0).</li>
</ul>
<p>Recommanded Usage Scenario:</p>
<ul>
<li>Highly recommanded for the scenarios, where the usage of design patterns for Data Transfer Object (Dto) and Business Objects (Bo).</li>
<li>For scenario of Data transfer from UI form elements to Data transfer objects (Dto).</li>
</ul>
<p>Disadvantages for Dozer</p>
<ul>
<li>I used this tool under Java 5/ Spring 2.0. Dozer was instantiated using Spring. our architecture was like, all the business Objects (Bo) were placed in one project. Had a facade (facade has Dto) for a set of functionalitie. Now the problem was the definition of spring configuration for the dozer, as Dto and Bo were present in various projects, so this created a need for the definition spring configuration definition on a top level project than at leaf level.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://javablog.info/2007/03/25/dozer/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
