<?xml version="1.0" encoding="UTF-8"?>
<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/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Borba on Software &#187; ejb</title>
	<atom:link href="http://borba.blog.br/tag/ejb/feed/" rel="self" type="application/rss+xml" />
	<link>http://borba.blog.br</link>
	<description>Desenvolvendo Software com Qualidade.</description>
	<lastBuildDate>Thu, 19 Aug 2010 19:39:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>DbUnit x Hibernate</title>
		<link>http://borba.blog.br/2008/09/dbunit-x-hibernate/</link>
		<comments>http://borba.blog.br/2008/09/dbunit-x-hibernate/#comments</comments>
		<pubDate>Thu, 11 Sep 2008 14:41:00 +0000</pubDate>
		<dc:creator>Luiz Borba</dc:creator>
				<category><![CDATA[Programação]]></category>
		<category><![CDATA[dbunit]]></category>
		<category><![CDATA[ejb]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[princípio dry]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[yaml]]></category>

		<guid isPermaLink="false">http://metralhadoralord2.wordpress.com/2008/09/11/dbunit-x-hibernate/</guid>
		<description><![CDATA[Aqui na empresa, temos clientes que apenas mandam executar um projeto segundo a arquitetura definida por eles. Algumas vezes é frustante trabalhar neste tipo de projeto, afinal, nem sempre conseguimos seguir nossas convicções e utilizar plenamente nossos conhecimentos e experiência na definição da melhor solução para o problema. Em um desses projetos, fui convocado para [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignright size-full wp-image-49" style="margin-left: 10px;" title="testing" src="http://borba.blog.br/wordpress/wp-content/uploads/2008/09/testing1.jpg" alt="testing" width="256" height="176" />Aqui na empresa, temos clientes que apenas mandam executar um projeto segundo a arquitetura definida por eles. Algumas vezes é frustante trabalhar neste tipo de projeto, afinal, nem sempre conseguimos seguir nossas convicções e utilizar plenamente nossos conhecimentos e experiência na definição da melhor solução para o problema. Em um desses projetos, fui convocado para auxiliar a equipe que estava consumindo muito tempo na implementação dos testes.</p>
<p><strong>O Cenário:</strong></p>
<p>O sistema utiliza <a href="http://java.sun.com/products/ejb">EJB</a> 2 e <a href="http://www.hibernate.org/">Hibernate</a> 2. O cliente exige implementação de testes dos métodos da <a href="http://c2.com/cgi/wiki?FacadePattern">fachada</a> utilizando <a href="http://www.junit.org/">JUnit</a> e <a href="http://dbunit.sourceforge.net/">DbUnit</a>.</p>
<p><strong>Problemas:</strong></p>
<p>A montagem dos cenários é complexa. O DbUnit entende apenas o modelo relacional, isto significa que o engenheiro tem que fazer um "desmapeamento hibernate" na cabeça dele para montar as tabelas e colunas com os valores necessários. Veja um exemplo:</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">'1.0'</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">'UTF-8'</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dataset<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;employee</span> <span style="color: #000066;">start_date</span>=<span style="color: #ff0000;">&quot;2001-01-01&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">first_name</span>=<span style="color: #ff0000;">&quot;Drew&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">ssn</span>=<span style="color: #ff0000;">&quot;333-29-9999&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">last_name</span>=<span style="color: #ff0000;">&quot;Smith&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;employee</span> <span style="color: #000066;">start_date</span>=<span style="color: #ff0000;">&quot;2002-04-04&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">first_name</span>=<span style="color: #ff0000;">&quot;Nick&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">ssn</span>=<span style="color: #ff0000;">&quot;222-90-1111&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">last_name</span>=<span style="color: #ff0000;">&quot;Marquiss&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;employee</span> <span style="color: #000066;">start_date</span>=<span style="color: #ff0000;">&quot;2003-06-03&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">first_name</span>=<span style="color: #ff0000;">&quot;Jose&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">ssn</span>=<span style="color: #ff0000;">&quot;111-67-2222&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">last_name</span>=<span style="color: #ff0000;">&quot;Whitson&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dataset<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<p>Claro que esse é um modelo relacional simples, agora imagine modelos de classes complexos, com heranças, relacionamentos, etc. Neste caso, dá bastante trabalho.</p>
<p>Além da dificuldade em produzir a massa de dados, existe um problema muito grave. Dentro das classes de teste é necessário fazer validações contra os valores do arquivo xml (por exemplo em métodos de consulta). Neste caso podemos fazer as validações com valores hard coded, como neste exemplo:</p>
<p><code class="codecolorer java default"><span class="java">assertEquals<span style="color: #009900;">&#40;</span>employee.<span style="color: #006633;">getFirstName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #0000ff;">&quot;Jose&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></span></code></p>
<p>Que é uma péssima idéia, pois viola o <a href="http://c2.com/cgi/wiki?DontRepeatYourself">princípio DRY</a> (Don't Repeat Yourself). Outra saída é obter os valores do xml através da API do DbUnit, que vai dar uma trabalheira danada, ainda mais se você quiser montar o objeto para fazer comparações deste tipo:</p>
<p><code class="codecolorer java default"><span class="java">assertEquals<span style="color: #009900;">&#40;</span>employeeActual, employeeExpected<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></span></code></p>
<p>Já imaginou? Vai ter que construir outro hibernate só para pegar cada valor das tabelas e colunas do arquivo XML e remontar os objetos novamente.</p>
<p><strong>Como Resolver?</strong></p>
<p>O grande problema dessa abordagem é o fato do DbUnit entender apenas de tabelas e colunas. E se existisse um HibernateUnit? No hipotético HibernateUnit eu poderia especificar meus dados na forma de objetos. Além disso, poderia utilizar um formato mais legível e que pudesse ser transformado diretamente para objetos dentro do seu código. Esse formato pode ser o <a href="http://www.yaml.org/">YAML</a>. Veja como ficaria:</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">departaments:<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#006600; font-weight:bold;">&amp;</span>engineering<br />
&nbsp; &nbsp; &nbsp; id &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: <span style="color:#006666;">1</span><br />
&nbsp; &nbsp; &nbsp; name &nbsp; &nbsp; &nbsp; &nbsp;: Engineering<br />
&nbsp; &nbsp; &nbsp; employees:<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">-</span> id &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: <span style="color:#006666;">1</span><br />
&nbsp; &nbsp; &nbsp; startDate &nbsp; : <span style="color:#006666;">2001</span><span style="color:#006600; font-weight:bold;">-</span>01<span style="color:#006600; font-weight:bold;">-</span>01<br />
&nbsp; &nbsp; &nbsp; firstName &nbsp; : Drew<br />
&nbsp; &nbsp; &nbsp; lastName &nbsp; &nbsp;: Smith<br />
&nbsp; &nbsp; &nbsp; ssn &nbsp; &nbsp; &nbsp; &nbsp; : <span style="color:#006666;">333</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">29</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">9999</span><br />
&nbsp; &nbsp; &nbsp; departament : <span style="color:#006600; font-weight:bold;">*</span>engineering<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">-</span> id &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: <span style="color:#006666;">2</span><br />
&nbsp; &nbsp; &nbsp; startDate &nbsp; : <span style="color:#006666;">2002</span><span style="color:#006600; font-weight:bold;">-</span>04<span style="color:#006600; font-weight:bold;">-</span>04<br />
&nbsp; &nbsp; &nbsp; firstName &nbsp; : Nick<br />
&nbsp; &nbsp; &nbsp; lastName &nbsp; &nbsp;: Marquiss<br />
&nbsp; &nbsp; &nbsp; ssn &nbsp; &nbsp; &nbsp; &nbsp; : <span style="color:#006666;">222</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">90</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1111</span><br />
&nbsp; &nbsp; &nbsp; departament : <span style="color:#006600; font-weight:bold;">*</span>engineering<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">-</span> id &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: <span style="color:#006666;">3</span><br />
&nbsp; &nbsp; &nbsp; startDate &nbsp; : <span style="color:#006666;">2003</span><span style="color:#006600; font-weight:bold;">-</span>06<span style="color:#006600; font-weight:bold;">-</span>03<br />
&nbsp; &nbsp; &nbsp; firstName &nbsp; : Jose<br />
&nbsp; &nbsp; &nbsp; lastName &nbsp; &nbsp;: Whitson<br />
&nbsp; &nbsp; &nbsp; ssn &nbsp; &nbsp; &nbsp; &nbsp; : <span style="color:#006666;">111</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">67</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">2222</span><br />
&nbsp; &nbsp; &nbsp; departament : <span style="color:#006600; font-weight:bold;">*</span>engineering</div></div>
<p>Inclui um relacionamento para employee só para ficar um pouco mais rico. A vantagem dessa abordagem é que além de ficar mais legível, existem frameworks que serializam e deserializam esse formato para objetos. Com esse recurso, seria possível obter os objetos diretamente do arquivo e em seguida fazer comparações com objetos obtidos no banco. Legal, hein?</p>
<p>Acho que uma solução deste tipo poderia trazer muitos benefícios para o nosso problema. Estou fazendo uma pesquisa e se não existir nada semelhante, devo implementar essa solução como um projeto open source.</p>
<p>Pena que nosso cliente EXIGE que usemos o DbUnit.</p>
]]></content:encoded>
			<wfw:commentRss>http://borba.blog.br/2008/09/dbunit-x-hibernate/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
