<?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>Smartkey - Java Software Consultancy</title>
	<atom:link href="http://blog.smartkey.co.uk/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.smartkey.co.uk</link>
	<description></description>
	<lastBuildDate>Thu, 02 Sep 2010 15:25:49 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Specifying a different config file for Log4J</title>
		<link>http://blog.smartkey.co.uk/2010/09/specifying-config-file-for-log4j/</link>
		<comments>http://blog.smartkey.co.uk/2010/09/specifying-config-file-for-log4j/#comments</comments>
		<pubDate>Thu, 02 Sep 2010 15:24:55 +0000</pubDate>
		<dc:creator>Steve Neal</dc:creator>
				<category><![CDATA[Java Programming]]></category>
		<category><![CDATA[Configuration]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[log4j]]></category>

		<guid isPermaLink="false">http://blog.smartkey.co.uk/?p=772</guid>
		<description><![CDATA[Generally, adding a log4j.properties file to the classpath is all that needed to bootstrap your Log4J runtime. In certain cases though you might want to override this behaviour.
I needed to do this recently and it took me a while to find out how to do it in the Log4J docs, so I though I&#8217;d make [...]]]></description>
			<content:encoded><![CDATA[<p>Generally, adding a log4j.properties file to the classpath is all that needed to bootstrap your Log4J runtime. In certain cases though you might want to override this behaviour.</p>
<p>I needed to do this recently and it took me a while to find out how to do it in the Log4J docs, so I though I&#8217;d make a note of how to do it here.</p>
<p>To specify a file that Log4J will use instead of the default, pass its details as a system property when starting the JVM as follows:</p>
<pre class="brush: php">

java -Dlog4j.configuration=resources/log4j_dev.properties
</pre>
<p>The above example will load the log4j_dev.properties file from within the resources folder in the classpath. A file URL may also be specified:</p>
<pre class="brush: php">

java -Dlog4j.configuration=file:/resources/log4j_dev.properties
</pre>
<p>This will cause the log4j_dev.properties file to be loaded from the resources folder at the root of the file system.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.smartkey.co.uk/2010/09/specifying-config-file-for-log4j/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting a &#8220;Netscape security model is no longer supported&#8221; error when using IntelliJ</title>
		<link>http://blog.smartkey.co.uk/2010/08/netscape-security-model-error-in-intellij/</link>
		<comments>http://blog.smartkey.co.uk/2010/08/netscape-security-model-error-in-intellij/#comments</comments>
		<pubDate>Fri, 06 Aug 2010 10:48:01 +0000</pubDate>
		<dc:creator>Steve Neal</dc:creator>
				<category><![CDATA[Java Programming]]></category>
		<category><![CDATA[errors]]></category>
		<category><![CDATA[IntelliJ]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[netscape]]></category>

		<guid isPermaLink="false">http://blog.smartkey.co.uk/?p=769</guid>
		<description><![CDATA[I&#8217;ve been working on a JMS application that uses encryption with JNDI authentication and have encountered the following error message when running it:

Netscape security model is no longer supported. Please migrate to the Java 2 security model instead.

It turns out that this is a problem with the JDK&#8217;s plugin.jar file and can be fixed quite [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working on a JMS application that uses encryption with JNDI authentication and have encountered the following error message when running it:<code></code><br />
<code><br />
Netscape security model is no longer supported. Please migrate to the Java 2 security model instead.<br />
</code></p>
<p>It turns out that this is a problem with the JDK&#8217;s plugin.jar file and can be fixed quite easily by removing it from IntelliJ&#8217;s JDK configuration page as follows:</p>
<ol>
<li>In your project open the Project Structure dialog and click to edit the JDK you are working with</li>
<li>Remove the plugin.jar file from the JDK classpath</li>
</ol>
<p>That should fix it.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.smartkey.co.uk/2010/08/netscape-security-model-error-in-intellij/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Formatting dates in Sybase</title>
		<link>http://blog.smartkey.co.uk/2010/07/formatting-dates-in-sybase/</link>
		<comments>http://blog.smartkey.co.uk/2010/07/formatting-dates-in-sybase/#comments</comments>
		<pubDate>Thu, 01 Jul 2010 15:22:38 +0000</pubDate>
		<dc:creator>Steve Neal</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[Date]]></category>
		<category><![CDATA[formatting]]></category>
		<category><![CDATA[sybase]]></category>

		<guid isPermaLink="false">http://blog.smartkey.co.uk/?p=759</guid>
		<description><![CDATA[It's easy to format dates using the convert function in Sybase, but it's less easy to remember the cryptic formatting style codes that go with it. This post lists the formatting styles.]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s easy to format dates using the convert function in Sybase, but it&#8217;s less easy to remember the cryptic formatting style codes that go with it.</p>
<p>For example using style code 112:</p>
<pre class="brush: php">
select convert(char(8), birthday, 112) from People
</pre>
<p>will format the birthday column (with value 19th Feb 1984) as:</p>
<pre class="brush: php">

19840231
</pre>
<p>For quick reference, these formatting codes are listed below:</p>
<table border="0">
<tbody>
<tr style="text-align: left;">
<th>Style</th>
<th>Output</th>
</tr>
<tr>
<td>0</td>
<td>mon dd yyyy hh:miAM  (or PM)</td>
</tr>
<tr>
<td>1</td>
<td>mm/dd/yy</td>
</tr>
<tr>
<td>2</td>
<td>yy.mm.dd</td>
</tr>
<tr>
<td>3</td>
<td>dd/mm/yy</td>
</tr>
<tr>
<td>4</td>
<td>yy.mm.dd</td>
</tr>
<tr>
<td>5</td>
<td>dd-mm-yy</td>
</tr>
<tr>
<td>6</td>
<td>dd mon yy</td>
</tr>
<tr>
<td>7</td>
<td>mon dd, yy</td>
</tr>
<tr>
<td>8</td>
<td>hh:mm:ss</td>
</tr>
<tr>
<td>9</td>
<td>mon dd yyyy  hh:mi:ss:mmmAM (or PM)</td>
</tr>
<tr>
<td>10</td>
<td>mm-dd-yy</td>
</tr>
<tr>
<td>11</td>
<td>yy/mm/dd</td>
</tr>
<tr>
<td>12</td>
<td>yymmdd</td>
</tr>
<tr>
<td>100</td>
<td>mon dd yyyy hh:miAM (or PM)</td>
</tr>
<tr>
<td>101</td>
<td>mm/dd/yyyy</td>
</tr>
<tr>
<td>102</td>
<td>yyyy.mm.dd</td>
</tr>
<tr>
<td>103</td>
<td>dd/mm/yyyy</td>
</tr>
<tr>
<td>104</td>
<td>yyyy.mm.dd</td>
</tr>
<tr>
<td>105</td>
<td>dd-mm-yyyy</td>
</tr>
<tr>
<td>106</td>
<td>dd mon yyyy</td>
</tr>
<tr>
<td>107</td>
<td>mon dd, yyyy</td>
</tr>
<tr>
<td>108</td>
<td>hh:mm:ss</td>
</tr>
<tr>
<td>109</td>
<td>mon dd yyyy  hh:mi:ss:mmmAM (or PM)</td>
</tr>
<tr>
<td>110</td>
<td>mm-dd-yyyy</td>
</tr>
<tr>
<td>111</td>
<td>yyyy/mm/dd</td>
</tr>
<tr>
<td>112</td>
<td>yyyymmdd</td>
</tr>
</tbody>
</table>
]]></content:encoded>
			<wfw:commentRss>http://blog.smartkey.co.uk/2010/07/formatting-dates-in-sybase/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting Maven to work from behind a firewall</title>
		<link>http://blog.smartkey.co.uk/2010/05/maven-behind-firewall/</link>
		<comments>http://blog.smartkey.co.uk/2010/05/maven-behind-firewall/#comments</comments>
		<pubDate>Fri, 28 May 2010 14:51:51 +0000</pubDate>
		<dc:creator>Steve Neal</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Configuration]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Maven]]></category>

		<guid isPermaLink="false">http://blog.smartkey.co.uk/?p=748</guid>
		<description><![CDATA[Maven needs to access the Internet to download a number of things including POM files and dependent JAR files that are not held in your local repository. If you are trying to do this from behind a corportate firewall then you&#8217;ll need to configure Maven to work around it or you&#8217;ll get a load of [...]]]></description>
			<content:encoded><![CDATA[<p>Maven needs to access the Internet to download a number of things including POM files and dependent JAR files that are not held in your local repository. If you are trying to do this from behind a corportate firewall then you&#8217;ll need to configure Maven to work around it or you&#8217;ll get a load of error messages when it runs.</p>
<p>First you&#8217;ll need to find out your network&#8217;s HTTP proxy settings. You could ask a network administrator, or just look in your Internet browser settings and figure it out for yourself.</p>
<p>Once you&#8217;ve got the details of the proxy, edit Maven&#8217;s conf/settings.xml file and enter the proxy details in the proxy element:</p>
<pre class="brush: xml">
&lt;proxy&gt;
    &lt;id&gt;optional&lt;/id&gt;
    &lt;active&gt;true&lt;/active&gt;
    &lt;protocol&gt;http&lt;/protocol&gt;
    &lt;username&gt;proxyuser&lt;/username&gt;
    &lt;password&gt;proxypassword&lt;/password&gt;
    &lt;host&gt;webproxy.smartkey.co.uk&lt;/host&gt;
    &lt;port&gt;8080&lt;/port&gt;
    &lt;nonProxyHosts&gt;&lt;/nonProxyHosts&gt;
&lt;/proxy&gt;
</pre>
<p>Save the changes in this file and try launching Maven again. If it still doesn&#8217;t play ball, check the settings with your network admin, make sure that the above XML is uncommented (it&#8217;ll be commented in the file that ships with Maven) and try again.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.smartkey.co.uk/2010/05/maven-behind-firewall/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java 1.4 Debugger Not Working?</title>
		<link>http://blog.smartkey.co.uk/2010/05/java-1-4-debugger-not-working/</link>
		<comments>http://blog.smartkey.co.uk/2010/05/java-1-4-debugger-not-working/#comments</comments>
		<pubDate>Thu, 13 May 2010 14:55:44 +0000</pubDate>
		<dc:creator>Steve Neal</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Debug]]></category>
		<category><![CDATA[Error]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Socket]]></category>

		<guid isPermaLink="false">http://blog.smartkey.co.uk/?p=732</guid>
		<description><![CDATA[There&#8217;s a bug that I&#8217;d encountered running the debugger in JDK 1.4.2  ages ago that reared it&#8217;s head again today. I&#8217;ve been working on a system that uses this old JDK and thought I&#8217;d note the solution here in case I ever came across it again.
The problem is encountered when the JVM is launched [...]]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s a bug that I&#8217;d encountered running the debugger in JDK 1.4.2  ages ago that reared it&#8217;s head again today. I&#8217;ve been working on a system that uses this old JDK and thought I&#8217;d note the solution here in case I ever came across it again.</p>
<p>The problem is encountered when the JVM is launched in debug mode and is reported as:</p>
<p><code>FATAL ERROR in native method: No transports initialized<br />
Transport dt_socket failed to initialize, rc = 509.<br />
</code><br />
Apparently, from looking at reports of this on the Web, this can happen on any operating system. I encountered it on a Windows XP machine and found that the simplest fix was to copy the following two files from the JAVA_HOME/jre/bin folder, to my c:/windows/system32 folder:</p>
<p><code>dt_socket.dll<br />
dt_shmem.dll<br />
</code><br />
You can also fix this by putting the JRE bin folder at the start of your PATH environment variable.  For a more detailed discussion of the problem take a look at <a href="http://forums.sun.com/thread.jspa?threadID=160025&#038;start=15">this thread</a> on the Sun forums web site.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.smartkey.co.uk/2010/05/java-1-4-debugger-not-working/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Resolving Subservsion Tree Conflicts when Merging</title>
		<link>http://blog.smartkey.co.uk/2010/05/resolving-subversion-conflicts/</link>
		<comments>http://blog.smartkey.co.uk/2010/05/resolving-subversion-conflicts/#comments</comments>
		<pubDate>Thu, 13 May 2010 10:10:18 +0000</pubDate>
		<dc:creator>Steve Neal</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Conflict]]></category>
		<category><![CDATA[Merge]]></category>
		<category><![CDATA[Subversion]]></category>

		<guid isPermaLink="false">http://blog.smartkey.co.uk/?p=721</guid>
		<description><![CDATA[When merging changes in a Subversion project, you&#8217;ll need to resolve any conflicts between your checked out version and the one that you are about to merge from the server. Commonly, conflicts arise when two or more developers have modified the same file; these can be resolved by accepting either parties changes or by completing [...]]]></description>
			<content:encoded><![CDATA[<p>When merging changes in a Subversion project, you&#8217;ll need to resolve any conflicts between your checked out version and the one that you are about to merge from the server. Commonly, conflicts arise when two or more developers have modified the same file; these can be resolved by accepting either parties changes or by completing a more detailed merge. </p>
<p>Another common conflicts is a Tree Conflict. Tree conflicts arise when the directory layout of the project changes, and can arise when folders and files are moved, deleted or created. Such conflics will cause a capital &#8216;C&#8217; to appear in the svn status commands output &#8211; these must be resolved before your merge can take place so that svn knows what it should do with the files that will be merged. Note that you should treat these conflicts carefully and ensure that anyone else who may be affected by the changes is happy with the resolution you are about to apply.</p>
<p>Once you&#8217;ve agreed on how the conflicts will be fixed, you can use the <strong>svn resolve</strong> command to determine what action should be taken suring the merge.</p>
<p>For example:</p>
<pre class="brush: php">
svn resolve --accept mine-full /path/to/folder
</pre>
<p>indicates that the local changes should be used. </p>
<p>In contrast, if the changes from the server are to be used, then use this:</p>
<pre class="brush: php">
svn resolve --accept theirs-full /path/to/folder
</pre>
<p>Be aware that the above instruction will cause the local changes to be lost.</p>
<p>There are a number of different options to select (in addition to mine-full and theirs-full) that give you more control over resolving, or postponing, the conflict. See the excellent SVN manual for deails of these.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.smartkey.co.uk/2010/05/resolving-subversion-conflicts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Open Session in View Pattern for Spring and JPA</title>
		<link>http://blog.smartkey.co.uk/2010/03/open-session-in-view-pattern-spring-jpa/</link>
		<comments>http://blog.smartkey.co.uk/2010/03/open-session-in-view-pattern-spring-jpa/#comments</comments>
		<pubDate>Mon, 08 Mar 2010 18:13:24 +0000</pubDate>
		<dc:creator>Steve Neal</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java Programming]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[JPA]]></category>
		<category><![CDATA[JSP]]></category>
		<category><![CDATA[Open]]></category>
		<category><![CDATA[Session]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Transaction]]></category>
		<category><![CDATA[View]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://blog.smartkey.co.uk/?p=676</guid>
		<description><![CDATA[The Open Session in View Pattern is well publicised in Hibernate circles as the best practice approach to presenting data in the Web tier of an application. In this article I'll demonstrate how simple it is to configure a Spring application to do the same thing with the Java Persistence API (JPA).]]></description>
			<content:encoded><![CDATA[<p>The Open Session in View Pattern is a well publicised <a href="https://www.hibernate.org/43.html" target="_blank">design pattern</a> for Hibernate applications and is considered the best practice approach to presenting data within the Web tier of an application. In this article I&#8217;ll demonstrate how simple it is to configure a Spring application to work the same way when JPA is used for the persistence technology. The exact same principles apply for a plain Spring/Hibernate application that does not use JPA.</p>
<h3>What is Open Session in View?</h3>
<p>The Java Persistence API (JPA) allows an object oriented model to be mapped to a relational database. JPA is a standard specification for Java based Object Relational Mapping frameworks &#8211; in order to use JPA an underlying implementation must be available; the most common choice being Hibernate.</p>
<p>Both JPA an Hibernate support lazy loading of data to restrict the number of queries fired off to the database. In general this means that data will be loaded on demand when methods are called, on a loaded object, that require more data to be loaded. In order for this to work, the object that the method is called on must have been loaded by JPA and be part of the current running transaction.</p>
<p>In a Spring application, calls to demarcate transactions are generally handled by the Spring interceptors.Transactions are normally started when a method call is made on a Spring managed object and committed once that method call ends. This means that if a JSP page requests data by calling a transactional method on a Spring managed bean, then it can only access the data in that bean that has already been loaded within that call. Any calls for data that might be loaded lazily will fail because the object is no longer attached to a JPA transaction after that method call has returned.</p>
<p>Hibernate developers solved this problem using the Open Session in View design, which associates the active session (and hence it&#8217;s transaction) with the thread that makes the call. In this design, the transaction will be committed when the thread completed processing the request, rather than when a method call completes. This allows lazily loaded data to be loaded within the JSP page not just within Spring managed objects.</p>
<h3>Providing Request Scoped Transactions</h3>
<p>A standard way to intercept requests in any Java Web application is to use a web filter. Spring provide an out-of-the-box Web filters that implement the Open Session in View design for both Hibernate and JPA. We will see the JPA version here, but the Hibernate one works in just the same way; just change the class name in the configuration file.</p>
<p>In web.xml:</p>
<pre class="brush: xml">
&lt;filter&gt;
    &lt;filter-name&gt;oemInViewFilter&lt;/filter-name&gt;
    &lt;filter-class&gt;
        org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
    &lt;/filter-class&gt;
    &lt;init-param&gt;
        &lt;param-name&gt;entityManagerFactoryBeanName&lt;/param-name&gt;
        &lt;param-value&gt;reportsEntityManagerFactory&lt;/param-value&gt;
    &lt;/init-param&gt;
&lt;/filter&gt;

&lt;filter-mapping&gt;
    &lt;filter-name&gt;oemInViewFilter&lt;/filter-name&gt;
    &lt;url-pattern&gt;*.jsp&lt;/url-pattern&gt;
&lt;/filter-mapping&gt;
</pre>
<p>The filter class is from the Spring framework libraries. The filter mapping, in this example, applies this filter to all URLs ending with &#8216;.jsp&#8217;; this means that all JSPs that retrieve a lazily loaded object form the service tier of the application are now free to read any data members from it &#8211; any unloaded data will be loaded from the database when calls are made to access it. </p>
<p>Be aware that the filter turns off the auto-flush behaviour of the standard entity manager. This means that any changes that are made to the data in the JSP will not be persisted. The filter can be configured to flush these if requested.</p>
<p>Finally, the init-param that&#8217;s provided to the filter is optional and can be used to explicitly name the entity manager factory that will be used. The example shows how to use an entity manager named &#8216;reportsEntityManagerFactory&#8217; in the Spring configuration files; the default value for this is &#8216;entityManagerFactory&#8217;.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.smartkey.co.uk/2010/03/open-session-in-view-pattern-spring-jpa/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Mockito to Unit Test Java Applications</title>
		<link>http://blog.smartkey.co.uk/2010/02/mockito-unit-test-java/</link>
		<comments>http://blog.smartkey.co.uk/2010/02/mockito-unit-test-java/#comments</comments>
		<pubDate>Thu, 25 Feb 2010 18:39:26 +0000</pubDate>
		<dc:creator>Steve Neal</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java Programming]]></category>
		<category><![CDATA[Tool support]]></category>
		<category><![CDATA[Google Code]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Mockito]]></category>
		<category><![CDATA[Test]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://blog.smartkey.co.uk/?p=604</guid>
		<description><![CDATA[If you&#8217;ve spent any time writing unit tests then you&#8217;ll know that it&#8217;s not always straight-forward. Certain things are inherently hard to test. In this post I&#8217;ll show you the basic principles of creating mock objects with a little help from the Mockito mocking tool.
One common problem faced when unit testing is how to test [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve spent any time writing unit tests then you&#8217;ll know that it&#8217;s not always straight-forward. Certain things are inherently hard to test. In this post I&#8217;ll show you the basic principles of creating mock objects with a little help from the <a href="http://www.mockito.org">Mockito</a> mocking tool.</p>
<p>One common problem faced when unit testing is how to test one object when it is dependant on another object. You could create instances of both the object under test and the dependent object and test them both together, however testing aggregated objects is not what unit testing is about &#8211; unit tests should test individual objects for their correct behaviour, not aggregations of objects! Moreover, this approach just won&#8217;t work if the dependent object hasn&#8217;t even been implemented yet.</p>
<p>A common technique for handling dependencies in unit tests is to provide a surrogate, or &#8220;mock&#8221; object, for the dependent object instead of a real one. The mock object will implement a simplified version of the real objects methods that return predictable results and can be used for testing purposes. </p>
<p>The drawback of this approach is that in a complex application, you could find yourself creating a lot of mock objects. This is where frameworks like Mockito can save you a lot of time and effort.</p>
<h3>A Test Scenario</h3>
<p>To demonstrate what you can do with Mockito, we&#8217;ll examine how we might test an Account object that is dependant on a data access object (AccountDAO).  The account object can calculate the charges applicable in the current month by using the DAO to count the number of days overdrawn and then performing a simple calculation on the value obtained from that call. The method names on the classes we&#8217;ll use are self-explanatory:</p>
<p><img src="http://blog.smartkey.co.uk/wp-content/uploads/2010/02/AccountAndDAO1.png" alt="AccountAndDAO" title="AccountAndDAO" width="376" height="55" class="aligncenter size-full wp-image-648" /><br />
To test this thoroughly, we should test two things:</p>
<ol>
<li>that the account obtains the number of overdrawn days by calling the correct method on the DAO,</li>
<li>that the account calculates the correct fees based upon the value it gets back from the call.</li>
</ol>
<h3>Testing that the account calls the correct method on the DAO</h3>
<p>Using JUnit to run the test case, we could write something like this:</p>
<pre class="brush: java">
import static org.mockito.Mockito.*;

public class TestAccount {
    @Test
    public void checkAccountCallsDaoMethods() {
        //create the object under test
        Account account = new Account();

        //create a mock DAO
        AccountDAO mockedDao = mock(AccountDAO.class);	

        //associate the mocked DAO with the object under test
        account.setDAO(dao);

        //call the method under test
        long charge = account.calculateCharges();

        //verify that the &#039;countOverdrawnDaysThisMonth&#039; was called
        verify(mockedDao).countOverdrawnDaysThisMonth();
    }
}
</pre>
<p>Taking centre stage in all this is the Mockito class which has a bunch of static methods that are used within the unit test (note the static import on line 1). </p>
<p>The call to the &#8216;mock&#8217; method (line 10) creates a mock object that can be used in place of a real AccountDAO. The call to &#8216;verify&#8217; (line 19), will throw an exception if the &#8216;countOverdrawnDaysThisMonth&#8217; method was not called on the mock object. </p>
<p>It is worth noting here that this is a simple example which just demonstrates the basic principle of verification, it is also possible to use the verify method to check for parameter values and ranges in the method call too.</p>
<h3>Testing that the account performs its calculations correctly</h3>
<p>In the above example, we mocked the DAO but didn&#8217;t specify what any of the mocked methods should do. In this case, all methods will return sensible default values of: null, zero or false. More commonly we need to specify something other than these defaults when writing our tests:</p>
<pre class="brush: java">
import static org.mockito.Mockito.*;
import static junit.framework.Assert.*;
import org.junit.*;

public class TestAccount {
    private Account account;

    @Before
    public void setup() {
        AccountDAO mockedDao = mock(AccountDAO.class);

    	//specify that this method on the mock object should return 8
        when(mockedDao.countOverdrawnDaysThisMonth()).thenReturn(8);

        account = new Account();
        account.setDAO(dao);
    }

    @Test
    public void checkChargesWhenOverdrawn() {
        //call the method under test
        long charge = account.calculateCharges();

        //assert that the charge was £2 per day overdrawn
        assertEquals(charge, 16);
    }
}
</pre>
<p>The statement on line 18 specifies that whenever the &#8216;countOverdrawnDaysThisMonth&#8217; is called it should return a value of 8; overriding the default of zero.</p>
<p>Given that the correct charge is two pounds (or dollars) per day overdrawn, then the assertion on line 25 will pass if the account performed the correct calculation (8 x 2 = 16 right).</p>
<h3>Comments</h3>
<p>There&#8217;s loads more to Mockito than we&#8217;ve looked at here. It&#8217;s a neat testing tool that works great with JUnit or TestNG. I&#8217;ve found that it&#8217;s small enough to learn quickly and capable enough to be really useful. Give it a go and write a comment below to let me know what you think of it.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.smartkey.co.uk/2010/02/mockito-unit-test-java/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Configuring MySQL on Mac OS X</title>
		<link>http://blog.smartkey.co.uk/2010/02/configuring-mysql-on-mac-os-x/</link>
		<comments>http://blog.smartkey.co.uk/2010/02/configuring-mysql-on-mac-os-x/#comments</comments>
		<pubDate>Tue, 23 Feb 2010 14:41:03 +0000</pubDate>
		<dc:creator>Steve Neal</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Tool support]]></category>
		<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://blog.smartkey.co.uk/?p=624</guid>
		<description><![CDATA[I found the documentation for MySQL a little rough around the edges recently when I needed to change a configuration setting for it &#8211; everything seemed spread around their comprehensive PDF documentation and it took me a little while to fathom my way through it. 
It&#8217;s not often that changes to MySQL configuration need to [...]]]></description>
			<content:encoded><![CDATA[<p>I found the documentation for MySQL a little rough around the edges recently when I needed to change a configuration setting for it &#8211; everything seemed spread around their comprehensive PDF documentation and it took me a little while to fathom my way through it. </p>
<p>It&#8217;s not often that changes to MySQL configuration need to be made. This post is, therefore, more a reminder to myself than anything else.</p>
<p>First locate the configuration file. On my Mac I found this empty configuration file had been created as part of the installation process:</p>
<p><code>/etc/my.cnf</code> </p>
<p>You&#8217;ll need super-user privileges to update it. I used:</p>
<p><code>sudo vi /etc/my.cnf</code></p>
<p>Enter the configuration settings you require. I wanted to update the maximum packet size so I entered:</p>
<p><code>[mysqld]<br />
max_allowed_packet=16000000<br />
</code></p>
<p>Then you should be able to stop MySql with:</p>
<p><code>mysqladmin -u root -p shutdown<br />
</code></p>
<p>Restart it using whatever start script you prefer. To make sure that your changes have taken effect you can print the server variables to the console with:</p>
<p><code>mysqladmin -u root -p variables<br />
</code></p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.smartkey.co.uk/2010/02/configuring-mysql-on-mac-os-x/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Assertions in Java</title>
		<link>http://blog.smartkey.co.uk/2010/02/java-assertions/</link>
		<comments>http://blog.smartkey.co.uk/2010/02/java-assertions/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 14:47:25 +0000</pubDate>
		<dc:creator>Steve Neal</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java Programming]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.smartkey.co.uk/?p=607</guid>
		<description><![CDATA[I&#8217;ve been working on a project this week and have been delighted to see that the original team of programmers who developed it have made the effort to use assertions in their code. 
The assert keyword was introduced in Java 1.4 and has since been a sadly under-used language feature. Assertions offer developers a neat [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working on a project this week and have been delighted to see that the original team of programmers who developed it have made the effort to use assertions in their code. </p>
<p>The assert keyword was introduced in Java 1.4 and has since been a sadly under-used language feature. Assertions offer developers a neat way to implement error detecting statements that are only active during development and can be turned off in production environments. </p>
<p>For example you might want to check that appropriate values are passed to a method during development to ensure that it is being called correctly:</p>
<pre class="brush: java">
private void onlyPositiveNumbersPlease(int i) {
    assert i &gt; 0;

    //rest of method implementation...

}
</pre>
<p>If the assertion fails, then an AssertionError is thrown. If you have a lot of assertions in your code, then its useful to be able to specify a value that will be used in the error message:</p>
<pre class="brush: java">
private void onlyPositiveNumbersPlease(int i) {
    assert i &gt; 0 : &quot;only positive numbers are allowed&quot;;

    //rest of method implementation...

}
</pre>
<p>The value that is specified can also be any primitive type, for example:</p>
<pre class="brush: java">
File f = loadFile();
assert f != null : 404;
</pre>
<p>In all cases, the value is converted to a String and set as the detail message of the AssertionException.</p>
<p>When the Java source is compiled, assertions instructions are included in the bytecode that is generated. However, by default, the assertions are not executed at runtime.</p>
<p>In order to activate all of the assertions, a command line flag must be passed to the JVM:<code></p>
<p>$ java <b>-ea</b> co.uk.smartkey.ui.MainClass<br />
</code></p>
<p>it is also possible to specify that assertions should only be activated for classes within a certain package. For example to run the same application as before, but only activate assertions for classes in the util package of our application, you could use something like:<code></p>
<p>$ java <b>-ea:co.uk.smartkey.util</b> co.uk.smartkey.ui.MainClass<br />
</code></p>
<p>if you just wanted to turn on assertions for a single class, you can specify the class name instead of the package name. Finally, if you really feel the need to, you can turn on assertions in the Java system classes with this:<code></p>
<p>$ java <b>-esa</b> co.uk.smartkey.ui.MainClass<br />
</code></p>
<p>Personally, I like using assertions as they can help to iron out problems during development that are unlikely to need checking for in production. Language level support for them means that the statements are only used if the JVM is launched with them activated; this ensures that there is no performance overhead associated with them when they are not in use.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.smartkey.co.uk/2010/02/java-assertions/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
