<?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>JSF Blog</title>
	<atom:link href="http://www.jsfblog.info/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jsfblog.info</link>
	<description>Discussion on all things Java and JSF</description>
	<lastBuildDate>Sun, 05 Sep 2010 12:48:54 +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>JSF, Iterating with tomahawk radio buttons, t:datalist, a4j:repeat</title>
		<link>http://www.jsfblog.info/2010/05/jsf-iterating-with-tomahawk-radio-buttons-tdatalist-a4jrepeat/</link>
		<comments>http://www.jsfblog.info/2010/05/jsf-iterating-with-tomahawk-radio-buttons-tdatalist-a4jrepeat/#comments</comments>
		<pubDate>Fri, 14 May 2010 18:22:48 +0000</pubDate>
		<dc:creator>paul</dc:creator>
				<category><![CDATA[JSF]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[tomahawk]]></category>

		<guid isPermaLink="false">http://www.jsfblog.info/?p=224</guid>
		<description><![CDATA[The standard JSF radio buttons h:selectOneRadio render a load of old school html (i.e. they use table tags to do layout, (all a bit 1996)). e.g. JSF h:selectOneRadio tag &#60;h:selectOneRadio id=&#34;aRadio&#34;&#62; &#60;f:selectItems value=&#34;#{radioOptions.items}&#34; /&#62; &#60;/h:selectOneRadio&#62; Code for #{radioOptions} import java.util.ArrayList; import java.util.List; import javax.faces.model.SelectItem; import org.jboss.seam.ScopeType; import org.jboss.seam.annotations.Name; import org.jboss.seam.annotations.Scope; @Name(&#34;radioOptions&#34;) @Scope(ScopeType.APPLICATION) public class RadioOptions [...]]]></description>
			<content:encoded><![CDATA[<p>The standard JSF radio buttons <a href="http://java.sun.com/javaee/javaserverfaces/1.2/docs/tlddocs/h/selectOneRadio.html">h:selectOneRadio</a> render a load of old school html (i.e. they use table tags to do layout, (all a bit 1996)).</p>
<p>e.g. JSF h:selectOneRadio tag</p>
<pre class="brush: xml;">
&lt;h:selectOneRadio id=&quot;aRadio&quot;&gt;
	&lt;f:selectItems value=&quot;#{radioOptions.items}&quot; /&gt;
&lt;/h:selectOneRadio&gt;
</pre>
<p>Code for #{radioOptions}</p>
<pre class="brush: java;">
import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

@Name(&quot;radioOptions&quot;)
@Scope(ScopeType.APPLICATION)
public class RadioOptions
{
	public List&lt;SelectItem&gt; items = new ArrayList&lt;SelectItem&gt;();

	public RadioOptions()
	{
		items.add(new SelectItem(&quot;1&quot;, &quot;label 1&quot;));
		items.add(new SelectItem(&quot;2&quot;, &quot;label 2&quot;));
		items.add(new SelectItem(&quot;3&quot;, &quot;label 3&quot;));
	}

	public List&lt;SelectItem&gt; getItems()
	{
		return items;
	}

	public void setItems(List&lt;SelectItem&gt; items)
	{
		this.items = items;
	}
}
</pre>
<p>Note: I&#8217;m using jboss seam in the example above, but you could easily remove the seam annotations and define the bean in using jsf managed beans or faces config etc. Also the examples here are a bit mickey mouse and could be better.</p>
<p>Under JSF 1.2 will be rendered as</p>
<pre class="brush: xml;">
&lt;table id=&quot;myForm:aRadio&quot;&gt;
	&lt;tr&gt;
		&lt;td&gt;
			&lt;input type=&quot;radio&quot; name=&quot;myForm:aRadio&quot; id=&quot;myForm:aRadio:0&quot; value=&quot;1&quot; /&gt;
			&lt;label for=&quot;myForm:aRadio:0&quot;&gt;Label 1&lt;/label&gt;
		&lt;/td&gt;
		&lt;td&gt;
			&lt;input type=&quot;radio&quot; name=&quot;myForm:aRadio&quot; id=&quot;myForm:aRadio:1&quot; value=&quot;2&quot; /&gt;
			&lt;label for=&quot;myForm:aRadio:1&quot;&gt;Label 2&lt;/label&gt;
		&lt;/td&gt;
		&lt;td&gt;
			&lt;input type=&quot;radio&quot; name=&quot;myForm:aRadio&quot; id=&quot;myForm:aRadio:2&quot; value=&quot;3&quot; /&gt;
			&lt;label for=&quot;myForm:aRadio:2&quot;&gt;Label 3&lt;/label&gt;
		&lt;/td&gt;
	&lt;/tr&gt;
&lt;/table&gt;
</pre>
<p>Anyway this limitation is well known and as a result we&#8217;ve been using the apache tomahawk <a href="http://myfaces.apache.org/tomahawk-project/tomahawk12/tagdoc/t_selectOneRadio.html">t:selectOneRadio</a> (using the layout=&#8221;spread&#8221; attribute) and <a href="http://myfaces.apache.org/tomahawk-project/tomahawk12/tagdoc/t_radio.html">t:radio</a> tags to provide more flexible layouts which can be styled by css.</p>
<p><strong>Iterating through a list of select items</strong></p>
<p>There are times when I want to render the radio buttons in a flexible fashion (offered by t:radio) using a loop tag such as <a href="http://myfaces.apache.org/tomahawk-project/tomahawk12/tagdoc/t_dataList.html">t:dataList</a> or <a href="http://docs.jboss.org/richfaces/latest_3_3_X/en/tlddoc/a4j/repeat.html">a4j:repeat</a> e.g.</p>
<pre class="brush: xml;">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xmlns:ui=&quot;http://java.sun.com/jsf/facelets&quot; xmlns:h=&quot;http://java.sun.com/jsf/html&quot; xmlns:f=&quot;http://java.sun.com/jsf/core&quot; xmlns:fn=&quot;http://java.sun.com/jsp/jstl/functions&quot; xmlns:c=&quot;http://java.sun.com/jstl/core&quot; xmlns:t=&quot;http://myfaces.apache.org/tomahawk&quot; xmlns:a4j=&quot;https://ajax4jsf.dev.java.net/ajax&quot;&gt;
&lt;f:view&gt;
&lt;h:form id=&quot;myForm&quot;&gt;
	&lt;!-- radio button, layout spread  --&gt;
	&lt;t:selectOneRadio id=&quot;myRadio&quot; forceId=&quot;true&quot; layout=&quot;spread&quot;&gt;
		&lt;f:selectItems value=&quot;#{radioOptions.items}&quot; /&gt;
	&lt;/t:selectOneRadio&gt;

	&lt;!-- data list, loops through all the radio button options --&gt;
	&lt;t:dataList var=&quot;helper&quot; value=&quot;#{radioOptions.items}&quot; rowIndexVar=&quot;idx&quot;&gt;
		&lt;!-- example html, not constrained to table layout --&gt;
		&lt;h1&gt;Heading&lt;/h1&gt;
		&lt;t:radio for=&quot;myRadio&quot; index=&quot;#{idx}&quot;/&gt;
		&lt;h1&gt;Another Heading&lt;/h1&gt;
	&lt;/t:dataList&gt;
&lt;/h:form&gt;
&lt;/f:view&gt;
&lt;/html&gt;
</pre>
<p>Unfortunately this doesn&#8217;t work and throws:</p>
<pre class="brush: plain;">
java.lang.IllegalStateException: Could not find component 'myRadio' (calling findComponent on component 'myForm:j_id3:0:j_id5')
</pre>
<p><strong>The Fix:</strong></p>
<p>I <em>almost always</em> forget this which is why im blogging about it,</p>
<pre class="brush: xml;">
&lt;t:radio for=&quot;myRadio&quot; index=&quot;#{idx}&quot;/&gt;
</pre>
<p>needs to be replaced with</p>
<pre class="brush: xml;">
&lt;t:radio for=&quot;:myForm:myRadio&quot; index=&quot;#{idx}&quot;/&gt;
</pre>
<p>The tomahawk t:radio component must have the <a href="#fullyQualified"><em>fully qualified</em></a> component name of the t:selectOneRadio name otherwise it cannot find it.</p>
<p>Since the form is defined as:</p>
<pre class="brush: xml;">
&lt;h:form id=&quot;myForm&quot;&gt;
</pre>
<p>:myForm needs to be prefixed to the for attribute of the t:radio giving :myForm:myRadio</p>
<p>I take absolutely no credit for this because its explained <a href="https://issues.apache.org/jira/browse/TOMAHAWK-26">here</a> and on the <a href="http://wiki.apache.org/myfaces/Display_Radio_Buttons_In_Columns">Myfaces wiki </a>this article is merely a note to self.</p>
<p>Btw, the above works with a4j:repeat as well as t:datalist</p>
<p>Currently tomahawk isn&#8217;t working in JSF 2.0 which is a shame as we are looking to upgrade very soon. Hopefully a decent radio button will become part of JSF 2 at some point.</p>
<h5 id="fullyQualified">Update: Fully Qualified JSF Name</h5>
<p>A bit of searching around on the web took me to Lincoln Baxter&#8217;s blog  where he mentions <a href="http://ocpsoft.com/java/jsf2-java/how-to-jsf-2-0-render-components-outside-of-the-form/">rendering  components outside of the form</a> and explains that the first &#8220;:&#8221;  tells JSF to start looking from the very top of the JSF View Root. Thanks for that!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jsfblog.info/2010/05/jsf-iterating-with-tomahawk-radio-buttons-tdatalist-a4jrepeat/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Maven site generation error: DTDDVFactoryImpl does not extend from DTDDVFactory</title>
		<link>http://www.jsfblog.info/2010/04/maven-site-generation-error-dtddvfactoryimpl-does-not-extend-from-dtddvfactory/</link>
		<comments>http://www.jsfblog.info/2010/04/maven-site-generation-error-dtddvfactoryimpl-does-not-extend-from-dtddvfactory/#comments</comments>
		<pubDate>Mon, 26 Apr 2010 11:35:01 +0000</pubDate>
		<dc:creator>Colin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Maven]]></category>
		<category><![CDATA[exceptions]]></category>
		<category><![CDATA[maven-site-plugin]]></category>
		<category><![CDATA[plugins]]></category>
		<category><![CDATA[xerces]]></category>

		<guid isPermaLink="false">http://www.jsfblog.info/?p=197</guid>
		<description><![CDATA[When generating a site with Maven, we encountered the following exception when a particular reporting plugin was executed: org.apache.xerces.impl.dv.DVFactoryException: DTD factory class org.apache.xerces.impl.dv.dtd.DTDDVFactoryImpl does not extend from DTDDVFactory. This basically meant that an incompatible version of xerces was trying to be run. We have everything set to use Java 1.6, which ships with its own [...]]]></description>
			<content:encoded><![CDATA[<p>When generating a site with Maven, we encountered the following exception when a particular reporting plugin was executed:</p>
<pre class="brush: plain;">
org.apache.xerces.impl.dv.DVFactoryException:
DTD factory class org.apache.xerces.impl.dv.dtd.DTDDVFactoryImpl does not extend from DTDDVFactory.
</pre>
<p>This basically meant that an incompatible version of xerces was trying to be run. We have everything set to use Java 1.6, which ships with its own version of xerces. After some debugging, it was tracked down to the <a title="Maven Site Plugin" href="http://maven.apache.org/plugins/maven-site-plugin/">maven-site-plugin</a> which we had recently upgraded to use version 2.1 of the plugin by specifying it in our pom file like so:</p>
<pre class="brush: xml;">
&lt;pluginManagement&gt;
	&lt;plugins&gt;
		&lt;plugin&gt;
			&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
			&lt;artifactId&gt;maven-site-plugin&lt;/artifactId&gt;
			&lt;version&gt;2.1&lt;/version&gt;
		&lt;/plugin&gt;
	&lt;/plugins&gt;
&lt;/pluginManagement&gt;
</pre>
<p>The maven-site-site plugin has the following dependency structure:</p>
<ul>
<li>org.apache.maven.plugins:maven-site-plugin:maven-plugin:2.1
<ul>
<li>org.apache.maven.doxia:doxia-module-xhtml:jar:1.1.2 (compile)
<ul>
<li>org.apache.maven.doxia:doxia-core:jar:1.1.2 (compile)
<ul>
<li>xerces:xercesImpl:jar:2.8.1 (compile)</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>It is the dependency of the xercesImpl jar 2.8.1 that is the problem. To get around this issue, you can tell the maven-site-plugin to exclude a particular dependency. This can be done like so:</p>
<pre class="brush: xml;">
&lt;pluginManagement&gt;
	&lt;plugins&gt;
		&lt;plugin&gt;
			&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
			&lt;artifactId&gt;maven-site-plugin&lt;/artifactId&gt;
			&lt;version&gt;2.1&lt;/version&gt;
			&lt;dependencies&gt;
				&lt;dependency&gt;
					&lt;groupId&gt;org.apache.maven.doxia&lt;/groupId&gt;
					&lt;artifactId&gt;doxia-core&lt;/artifactId&gt;
					&lt;version&gt;1.1.2&lt;/version&gt;
					&lt;exclusions&gt;
						&lt;exclusion&gt;
							&lt;groupId&gt;xerces&lt;/groupId&gt;
							&lt;artifactId&gt;xercesImpl&lt;/artifactId&gt;
						&lt;/exclusion&gt;
					&lt;/exclusions&gt;
				&lt;/dependency&gt;
			&lt;/dependencies&gt;
		&lt;/plugin&gt;
	&lt;/plugins&gt;
&lt;/pluginManagement&gt;
</pre>
<p>There are a couple of maven reporting plugins that we found produced this exception when run with maven-site-plugin 2.1. These plugins are:</p>
<ul>
<li><a title="DocFlax XML Maven Plugin" href="http://www.filigris.com/downloads/view/docflex_xml/maven/index.html">DocFlex/XML Maven Plugin</a></li>
<li><a title="Canoo Webtest Maven Plugin" href="http://people.apache.org/~sgoeschl/download/maven-plugins/webtest-maven-plugin/site/index.html">Canoo Webtest Maven Plugin</a></li>
</ul>
<p>If you use an older version of the maven-site-plugin then the above mentioned reporting plugins should work ok. It is only when specifying a version of 2.1 or above that the exception occurs.</p>
<p>Some of the solutions suggested on the net didn&#8217;t work for us, like adding the xercesImpl jar as a dependency to the actual reporting plugin in question, so hopefully the solution mentioned in this post is helpful.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jsfblog.info/2010/04/maven-site-generation-error-dtddvfactoryimpl-does-not-extend-from-dtddvfactory/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>3D Secure and the PaReq field in Google Chrome &amp; Safari Browsers</title>
		<link>http://www.jsfblog.info/2010/03/3d-secure-and-the-pareq-field-in-google-chrome-safari-browsers/</link>
		<comments>http://www.jsfblog.info/2010/03/3d-secure-and-the-pareq-field-in-google-chrome-safari-browsers/#comments</comments>
		<pubDate>Mon, 01 Mar 2010 17:45:15 +0000</pubDate>
		<dc:creator>paul</dc:creator>
				<category><![CDATA[HTML]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[3D Secure]]></category>
		<category><![CDATA[browsers]]></category>
		<category><![CDATA[chrome]]></category>
		<category><![CDATA[markup]]></category>
		<category><![CDATA[PaReq]]></category>
		<category><![CDATA[safari]]></category>

		<guid isPermaLink="false">http://www.jsfblog.info/?p=128</guid>
		<description><![CDATA[I recently had to implement the 3D Secure payment system. In order to do this 3 fields must be sent to the 3D Secure ACS (Access Control Server) MD Term Url PaReq (Payer authentication request) Initally everything went well until we tested our pages in Google Chrome and Safari. Under both browsers the 3D Secure [...]]]></description>
			<content:encoded><![CDATA[<p>I recently had to implement the <a href="http://en.wikipedia.org/wiki/3-D_Secure">3D Secure</a> payment system.</p>
<p>In order to do this 3 fields must be sent to the 3D Secure ACS (Access Control Server)</p>
<ul>
<li>MD</li>
<li>Term Url</li>
<li>PaReq (Payer authentication request)</li>
</ul>
<p>Initally everything went well until we tested our pages in Google Chrome and Safari. Under both browsers the 3D Secure inline frame displayed the following message</p>
<p>&#8220;Error decoding PAREQ message&#8221;</p>
<p>A <a href="https://datacash.custhelp.com/cgi-bin/datacash.cfg/php/enduser/std_adp.php?p_faqid=1025">support page</a> mentions that a PaReq contains newlines and that if <em>any</em> of these are missing the PaReq decoding will fail. The page also mentions that there are issues with Chrome and Safari but provides no solution.</p>
<h2>So what is the problem?</h2>
<p>The problem is indeed the newline.</p>
<p>In our markup we use and EL (Expression Language) to output the PaReq e.g.</p>
<pre class="brush: xml;">
&lt;input type=&quot;hidden&quot; name=&quot;PaReq&quot; value=&quot;#{paReq}&quot; /&gt;
</pre>
<p>If the PaReq returned a string such as</p>
<pre class="brush: xml;">
&quot;I am
The PaReq
&quot;
</pre>
<p>Note the quotes above mark the begining and end of the string. The string itself is terminated by a newline, in code it would be:</p>
<pre class="brush: java;">
String paReq = &quot;I am\nThe PaReq\n&quot;;
</pre>
<p>The browser <em>should</em> generate the following markup</p>
<pre class="brush: xml;">
&lt;input type=&quot;hidden&quot; name=&quot;PaReq&quot; value=&quot;I am
The PaReq
&quot; /&gt;
</pre>
<p>However Chrome and Safari generate the following markup (as both browsers do this I assume its a <a href="http://webkit.org/">Webkit</a> thing)</p>
<pre class="brush: xml;">
&lt;input type=&quot;hidden&quot; name=&quot;PaReq&quot; value=&quot;I am
The PaReq&quot; /&gt;
</pre>
<p><strong>Note, the traililng newline has disappeared</strong> and herein lies the problem, the server fails to recoginse the PaReq as the <em>trailing newline</em> has gone (currently Chrome 4.0.429.89 contains the above bug).</p>
<p>The fix? Dont use Chrome or Safari, Only kidding</p>
<p>The way we overcame this was to use a &#8220;hidden&#8221; textarea</p>
<pre class="brush: xml;">
&lt;textarea name=&quot;PaReq&quot; style=&quot;display:none&quot;&gt;#{paReq}&lt;/textarea&gt;
</pre>
<p>Which renders</p>
<pre class="brush: xml;">
&lt;textarea name=&quot;PaReq&quot; style=&quot;display:none&quot;&gt;I am
The PaReq
&lt;/textarea&gt;
</pre>
<p>In all browsers and preserves the trailing newline. The style=&#8221;display:none&#8221; hides the textarea. Although this is crude it does provide a fix for Chrome and Safari.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jsfblog.info/2010/03/3d-secure-and-the-pareq-field-in-google-chrome-safari-browsers/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Suppress logging in java unit tests</title>
		<link>http://www.jsfblog.info/2010/03/suppress-logging-in-java-unit-tests/</link>
		<comments>http://www.jsfblog.info/2010/03/suppress-logging-in-java-unit-tests/#comments</comments>
		<pubDate>Mon, 01 Mar 2010 15:22:05 +0000</pubDate>
		<dc:creator>Colin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Maven]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[JUnit]]></category>
		<category><![CDATA[logging]]></category>
		<category><![CDATA[surefire]]></category>

		<guid isPermaLink="false">http://www.jsfblog.info/?p=110</guid>
		<description><![CDATA[Here are a couple of different ways of suppressing logging when running unit tests. If you are using apache commons logging then you can suppress logging with the following code: LogFactory.getFactory().setAttribute( &#34;org.apache.commons.logging.Log&#34;, &#34;org.apache.commons.logging.impl.NoOpLog&#34;); If running your unit tests with JUnit, it is probably best to put this code in the setUp() method. However, if you [...]]]></description>
			<content:encoded><![CDATA[<p>Here are a couple of different ways of suppressing logging when running unit tests. If you are using <a href="http://commons.apache.org/logging/">apache commons logging</a> then you can suppress logging with the following code:</p>
<pre class="brush: java;">
LogFactory.getFactory().setAttribute(
                &quot;org.apache.commons.logging.Log&quot;,
                &quot;org.apache.commons.logging.impl.NoOpLog&quot;);
</pre>
<p>If running your unit tests with JUnit, it is probably best to put this code in the setUp() method. However, if you only wanted it suppressed on particular tests, it could go at the top of the test method instead. Bear in mind that if the logger is a static in the class you are testing, you  may need to then reset the attribute at the end of the test method, otherwise, any tests being run after that one will also have their logging suppressed.</p>
<p>If you run your unit tests within an IDE, you may want to see the logging, but not see the logging when you actually run the build of your project with something like Maven. Thankfully, there is a very simple answer to this if you are using Maven. The maven-surefire-plugin is responsible for running the tests and there is a configuration option which needs to be specified to redirect all the test output to file instead of to the console. This keeps your build reports much cleaner as you aren&#8217;t interested in seeing the logging messages. As a bonus, since all output is saved to file, exceptions will also be output to file, so whenever exceptions are thrown in the methods being tested, the stack trace wont show up in the build report.</p>
<p>The configuration option that needs specifying is <strong>&lt;redirectTestOutputToFile&gt;</strong> and this needs to be set to <strong>true</strong>, as shown below.</p>
<pre class="brush: xml; highlight: [4];">
&lt;plugin&gt;
	&lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
	&lt;configuration&gt;
		&lt;redirectTestOutputToFile&gt;true&lt;/redirectTestOutputToFile&gt;
	&lt;/configuration&gt;
&lt;/plugin&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.jsfblog.info/2010/03/suppress-logging-in-java-unit-tests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mocking Statics and java.lang.IllegalStateException: no last call on a mock available</title>
		<link>http://www.jsfblog.info/2010/02/mocking-statics-and-java-lang-illegalstateexception-no-last-call-on-a-mock-available/</link>
		<comments>http://www.jsfblog.info/2010/02/mocking-statics-and-java-lang-illegalstateexception-no-last-call-on-a-mock-available/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 15:28:02 +0000</pubDate>
		<dc:creator>paul</dc:creator>
				<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[EasyMock]]></category>
		<category><![CDATA[mocking]]></category>
		<category><![CDATA[PowerMock]]></category>

		<guid isPermaLink="false">http://www.jsfblog.info/?p=112</guid>
		<description><![CDATA[If you use PowerMock to mock a static object e.g. mockStatic(FacesContext.class) and encounter the following error: java.lang.IllegalStateException: no last call on a mock available at org.easymock.EasyMock.getControlForLastCall(EasyMock.java:174) at org.easymock.EasyMock.expect(EasyMock.java:156) Check that you have prepared the Class for test using the @PrepareForTest annotation. e.g. @RunWith(PowerMockRunner.class) @PrepareForTest( { FacesContext.class }) public class .... An excellent article discussing mocking [...]]]></description>
			<content:encoded><![CDATA[<p>If you use PowerMock to mock a static object e.g.</p>
<pre class="brush: java;">
mockStatic(FacesContext.class)
</pre>
<p>and encounter the following error:</p>
<pre class="brush: plain;">
java.lang.IllegalStateException: no last call on a mock available
at org.easymock.EasyMock.getControlForLastCall(EasyMock.java:174)
at org.easymock.EasyMock.expect(EasyMock.java:156)
</pre>
<p>Check that you have prepared the Class for test using the @PrepareForTest annotation.</p>
<p>e.g.</p>
<pre class="brush: java;">
@RunWith(PowerMockRunner.class)
@PrepareForTest(
{
	FacesContext.class
})
public class ....
</pre>
<p>An excellent article discussing mocking statics by one of the PowerMock developers can be found here: <a href="http://blog.jayway.com/2009/05/17/mocking-static-methods-in-java-system-classes/">http://blog.jayway.com/2009/05/17/mocking-static-methods-in-java-system-classes/</a></p>
<p>Note that you also need to <a href="http://powermock.googlecode.com/svn/docs/powermock-1.3.5/apidocs/org/powermock/core/classloader/annotations/PrepareForTest.html">@PrepareForTest</a> any classes where you are expecting a private method call with expectPrivate.  If you don&#8217;t, your test execution will try and go into the private method and actually run all the code. The @PrepareForTest annotation can either go at the method level as shown in the example below, or at the class level as shown in the example posted above.</p>
<pre class="brush: java;">
@PrepareForTest(ClassUnderTest.class)
@Test
public void testNext() throws Exception
{
	String privateMethod = &quot;isNextEnabled&quot;;
	ClassUnderTest partMock = createPartialMock(ClassUnderTest.class, privateMethod);
	expectPrivate(partMock, privateMethod).andReturn(true);
	replayAll();
	partMock.next();
	verifyAll();
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.jsfblog.info/2010/02/mocking-statics-and-java-lang-illegalstateexception-no-last-call-on-a-mock-available/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cobertura code coverage with Maven and PowerMock</title>
		<link>http://www.jsfblog.info/2010/02/cobertura-code-coverage-with-maven-and-powermock/</link>
		<comments>http://www.jsfblog.info/2010/02/cobertura-code-coverage-with-maven-and-powermock/#comments</comments>
		<pubDate>Thu, 11 Feb 2010 22:09:40 +0000</pubDate>
		<dc:creator>Colin</dc:creator>
				<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[Maven]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Cobertura]]></category>
		<category><![CDATA[PowerMock]]></category>

		<guid isPermaLink="false">http://www.jsfblog.info/?p=56</guid>
		<description><![CDATA[If you use Powermock for unit testing with Maven and Cobertura for code coverage, you are likely to be affected by the following issue noted on the PowerMock issue tracker. If you did not have the maven-surefire-plugin forkmode configured to &#8216;pertest&#8217;, you would find that any tests annotated with @RunWith(PowerMockRunner.class) had zero percentage code coverage [...]]]></description>
			<content:encoded><![CDATA[<p>If you use <a href="http://code.google.com/p/powermock/">Powermock</a> for unit testing with <a href="http://maven.apache.org/">Maven</a> and <a href="http://cobertura.sourceforge.net/">Cobertura</a> for code coverage, you are likely to be affected by the <a href="http://code.google.com/p/powermock/issues/detail?id=122">following issue</a> noted on the PowerMock issue tracker. If you did not have the <a href="http://maven.apache.org/plugins/maven-surefire-plugin/">maven-surefire-plugin</a> forkmode configured to &#8216;pertest&#8217;, you would find that any tests annotated with @RunWith(PowerMockRunner.class) had zero percentage code coverage when you viewed the resulting code coverage report produced by the <a href="http://mojo.codehaus.org/cobertura-maven-plugin/">cobertura-maven-plugin</a>. Whilst running with &#8216;pertest&#8217; fixed the issue, it was very slow to run the Maven build compared against a forkmode of &#8216;once&#8217;.</p>
<p>In September 2009, Cobertura released version 1.9.3 and one of the changes mentioned was the following:</p>
<ul>
<li>Support the case where multiple classloaders each load the Cobertura classes.</li>
</ul>
<p>Seeing this, I decided to test if that would fix the issue. At present, the latest version of the cobertura-maven-plugin is 2.3 which runs with Cobertura 1.9.2. There are some snapshots in the <a href="http://snapshots.repository.codehaus.org/org/codehaus/mojo/cobertura-maven-plugin/">codehaus snapshot repository</a> which run with Cobertura 1.9.3.  However, I found that the snapshots were constantly changing, and each run of the Maven build downloaded the latest version and sometimes the latest version had issues. There is probably a way to stop this from happening but if you don&#8217;t want to use snapshots, there is a change you can make to the 2.3 version which will also fix the issue. The cobertura-maven-plugin-2.3.pom file needs to be edited. It can be found in your repository under the following directory: org/codehaus/mojo/cobertura-maven-plugin/2.3/</p>
<p>Find the dependencies section of the pom file and change the versions of Cobertura from 1.9.2 to 1.9.3. There are two instances that need changing:</p>
<pre class="brush: xml; highlight: [4,9];">
&lt;dependency&gt;
	&lt;groupId&gt;net.sourceforge.cobertura&lt;/groupId&gt;
	&lt;artifactId&gt;cobertura&lt;/artifactId&gt;
	&lt;version&gt;1.9.3&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
	&lt;groupId&gt;net.sourceforge.cobertura&lt;/groupId&gt;
	&lt;artifactId&gt;cobertura-runtime&lt;/artifactId&gt;
	&lt;version&gt;1.9.3&lt;/version&gt;
	&lt;type&gt;pom&lt;/type&gt;
&lt;/dependency&gt;
</pre>
<p>With that change, you should find that you can safely remove the &#8216;pertest&#8217; forkmode configuration since by default the surefire plugin will run with forkmode set to &#8216;once&#8217;, and your cobertura code coverage results should be reporting correctly.</p>
<h5>UPDATE (26th April 2010):</h5>
<p>There is actually a better way of doing this which doesn&#8217;t involve having to edit the pom file for the actual plugin.</p>
<p>In your pom file you will have defined the cobertura-maven-plugin in the reporting section. The reporting section does not allow to add dependencies to a plugin. However the build section does, and the plugins defined in the reporting section will also pick up those dependencies. Therefore, you can also define the cobertura-maven-plugin in the build section as a plugin. This will allow you to specify some dependencies, as shown below.</p>
<pre class="brush: xml;">
&lt;build&gt;
	&lt;plugins&gt;
		&lt;plugin&gt;
			&lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
			&lt;artifactId&gt;cobertura-maven-plugin&lt;/artifactId&gt;
			&lt;version&gt;2.3&lt;/version&gt;
			&lt;dependencies&gt;
				&lt;dependency&gt;
					&lt;groupId&gt;net.sourceforge.cobertura&lt;/groupId&gt;
					&lt;artifactId&gt;cobertura&lt;/artifactId&gt;
					&lt;version&gt;1.9.4.1&lt;/version&gt;
				&lt;/dependency&gt;
				&lt;dependency&gt;
					&lt;groupId&gt;net.sourceforge.cobertura&lt;/groupId&gt;
					&lt;artifactId&gt;cobertura-runtime&lt;/artifactId&gt;
					&lt;version&gt;1.9.4.1&lt;/version&gt;
					&lt;type&gt;pom&lt;/type&gt;
				&lt;/dependency&gt;
			&lt;/dependencies&gt;
		&lt;/plugin&gt;
    &lt;/plugins&gt;
&lt;/build&gt;
</pre>
<p>This will then use cobertura 1.9.4.1.  Any version above 1.9.2 should fix the issue, but 1.9.4.1 is currently the latest and supposedly a lot faster than previous versions.</p>
<h5>UPDATE (11th May 2010):</h5>
<p>Good news! There is now version 2.4 of the cobertura-maven-plugin. This uses 1.9.4.1 version of Cobertura, so none of the above is now necessary.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jsfblog.info/2010/02/cobertura-code-coverage-with-maven-and-powermock/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Chaining EasyMock&#8217;s expectation return values</title>
		<link>http://www.jsfblog.info/2010/02/chaining-easymocks-expectation-return-values/</link>
		<comments>http://www.jsfblog.info/2010/02/chaining-easymocks-expectation-return-values/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 15:53:08 +0000</pubDate>
		<dc:creator>Colin</dc:creator>
				<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[EasyMock]]></category>
		<category><![CDATA[mocking]]></category>

		<guid isPermaLink="false">http://www.jsfblog.info/?p=33</guid>
		<description><![CDATA[Recently I discovered quite a handy feature of EasyMock&#8217;s IExpectationSetters andReturn method. You are able to chain the return values so that you can return different values each time that expectation is called. It becomes useful if you consider the following method, where Voucher is an object that cannot be instantiated and needs to be [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I discovered quite a handy feature of <a title="View the EasyMock homepage" href="http://easymock.org/">EasyMock&#8217;s</a> IExpectationSetters <a title="View andReturn method API documentation" href="http://easymock.org/api/easymock/2.5.2/org/easymock/IExpectationSetters.html#andReturn(T)">andReturn</a> method. You are able to chain the return values so that you can return different values each time that expectation is called.</p>
<p>It becomes useful if you consider the following method, where Voucher is an object that cannot be instantiated and needs to be mocked:</p>
<pre class="brush: java;">
public class VoucherHelper
{
	public static boolean isVoucherExpired(Voucher voucher)
	{
		boolean expiredVoucher = true;
		Date expiryDate = voucher.getExpiryDate();
		if (expiryDate != null)
		{
			Date now = new Date();
			expiredVoucher = now.after(expiryDate);
		}
		return expiredVoucher;
	}
}
</pre>
<p>To test this method, there are 3 tests that need to be done.</p>
<ul>
<li>expiryDate returning null</li>
<li>expiryDate returning an earlier date than the current date</li>
<li>expiryDate returning a later date than the current date</li>
</ul>
<p>This can all be done in one expectation line by chaining the &#8216;andReturn&#8217; of the expectation.</p>
<pre class="brush: java;">
@Test
public final void testIsVoucherExpired()
{
	Voucher voucher = createMock(Voucher.class);
	Calendar expiredDate = Calendar.getInstance();
	// Make it expired by taking a year off
	expiredDate.set(Calendar.YEAR, expiredDate.get(Calendar.YEAR) - 1);

	Calendar notExpired = Calendar.getInstance();
	// Make it not expired by adding a year
	notExpired.set(Calendar.YEAR, notExpired.get(Calendar.YEAR) + 1);

	// Test with date returning null, then returning expired, then returning not expired
	expect(voucher.getExpiryDate())
		.andReturn(null)
		.andReturn(expiredDate.getTime())
		.andReturn(notExpired.getTime());
	replayAll();
	assertTrue(VoucherHelper.isVoucherExpired(voucher));
	assertTrue(VoucherHelper.isVoucherExpired(voucher));
	assertFalse(VoucherHelper.isVoucherExpired(voucher));
	verifyAll();
}
</pre>
<p>EasyMock also allows chaining on the <a title="View times method API documentation" href="http://easymock.org/api/easymock/2.5.2/org/easymock/IExpectationSetters.html#times%28int%29">times</a> method and the <a title="View andThrow method API documentation" href="http://easymock.org/api/easymock/2.5.2/org/easymock/IExpectationSetters.html#andThrow%28java.lang.Throwable%29">andThrow</a> method, and all can be used at the same time if you wanted.</p>
<pre class="brush: java;">
expect(voucher.getExpiryDate())
	.andReturn(null).times(2)
	.andThrow(new RuntimeException())
	.andReturn(new Date()).times(3);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.jsfblog.info/2010/02/chaining-easymocks-expectation-return-values/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
