<?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>compactcode</title>
	<atom:link href="http://www.compactcode.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.compactcode.com</link>
	<description>keeping software simple</description>
	<lastBuildDate>Fri, 30 Apr 2010 06:42:41 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Parallel processing and Google collections</title>
		<link>http://www.compactcode.com/2010/04/parallel-processing-and-google-collections/</link>
		<comments>http://www.compactcode.com/2010/04/parallel-processing-and-google-collections/#comments</comments>
		<pubDate>Tue, 27 Apr 2010 12:17:38 +0000</pubDate>
		<dc:creator>Shanon</dc:creator>
				<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.compactcode.com/?p=561</guid>
		<description><![CDATA[If you have been using google collections lately, chances are you will have been using one of the transform static methods available to you.

  List&#60;String&#62; source = Lists.newArrayList&#40;&#34;a&#34;, &#34;b&#34;, &#34;c&#34;&#41;; 	
  ...
  Iterables.transform&#40;source, Functions.toInt&#40;&#41;&#41;;

By default the above transform will execute your list transformations on the current thread. While this approach works well [...]]]></description>
			<content:encoded><![CDATA[<p>If you have been using <a href="http://code.google.com/p/google-collections/">google collections</a> lately, chances are you will have been using one of the transform static methods available to you.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">  List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> source <span style="color: #339933;">=</span> Lists.<span style="color: #006633;">newArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;a&quot;</span>, <span style="color: #0000ff;">&quot;b&quot;</span>, <span style="color: #0000ff;">&quot;c&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 	
  ...
  <span style="color: #006633;">Iterables</span>.<span style="color: #006633;">transform</span><span style="color: #009900;">&#40;</span>source, Functions.<span style="color: #006633;">toInt</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>By default the above transform will execute your list transformations on the current thread. While this approach works well and is in fact preferable for the majority of cases you may find yourself in a situation where you need to leverage the power of concurrency.</p>
<p>Some examples that might benefit from concurrency:</p>
<ol>
<li>Transformations that interact with the network.</li>
<li>Transformations that read from a database.</li>
<li>Transformations that require significant computation.</li>
</ol>
<p>The first two examples provide excellent opportunities for exploiting concurrency. These are both likely to trigger a low level network operation that will cause the current thread to block until a remote process has completed. This is valuable time that could be used to spin up another thread and do something useful while the first one is blocked.</p>
<p>That sounds like , but how do we go about introducing some concurrency to our collection processing? Well, we will need to implement our own version of the transform method that processes elements using a number of threads.</p>
<p>First things first, what does transform actually do? It's pretty simple and can be boiled down to something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #339933;">&lt;</span>F, T<span style="color: #339933;">&gt;</span> List<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> transform<span style="color: #009900;">&#40;</span>List<span style="color: #339933;">&lt;</span>F<span style="color: #339933;">&gt;</span> src, Function<span style="color: #339933;">&lt;</span>F, T<span style="color: #339933;">&gt;</span> fn<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  List<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> result <span style="color: #339933;">=</span> Lists.<span style="color: #006633;">newArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
  <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>F element <span style="color: #339933;">:</span> src<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    result.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>fn.<span style="color: #006633;">apply</span><span style="color: #009900;">&#40;</span>element<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">return</span> result<span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Nothing too complicated, grab each element in the list and pair it with the given function. The google version is a lot more elegant in reality but the main point is to show how the transformation is applied.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">  ...
  <span style="color: #006633;">fn</span>.<span style="color: #006633;">apply</span><span style="color: #009900;">&#40;</span>element<span style="color: #009900;">&#41;</span>
  ...</pre></div></div>

<p>In order to make our element transformation concurrent we first need to wrap it up in something that can can be offloaded to another thread. For this the <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/Callable.html">Callable</a> interface introduced in Java 5 is ideal.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #339933;">&lt;</span>F, T<span style="color: #339933;">&gt;</span> List<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> transform<span style="color: #009900;">&#40;</span>List<span style="color: #339933;">&lt;</span>F<span style="color: #339933;">&gt;</span> src, Function<span style="color: #339933;">&lt;</span>F, T<span style="color: #339933;">&gt;</span> fn<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  ...
  <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>F element <span style="color: #339933;">:</span> src<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 	
    Callable<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> transformTask <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Callable<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">public</span> T call<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span> 
        <span style="color: #000000; font-weight: bold;">return</span> fn.<span style="color: #006633;">apply</span><span style="color: #009900;">&#40;</span>element<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> 
  ...
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Once transformations are represented as callable objects we need to ship them off to something that can coordinate a bunch of threads to get them all executed. The <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ExecutorService.html">ExecutorService</a> is a nice, easy way to get this done.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #339933;">&lt;</span>F, T<span style="color: #339933;">&gt;</span> List<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> transform<span style="color: #009900;">&#40;</span>List<span style="color: #339933;">&lt;</span>F<span style="color: #339933;">&gt;</span> src, Function<span style="color: #339933;">&lt;</span>F, T<span style="color: #339933;">&gt;</span> fn<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  ExecutorService service <span style="color: #339933;">=</span> Executors.<span style="color: #006633;">newFixedThreadPool</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  List<span style="color: #339933;">&lt;</span>Future<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;&gt;</span> resultFuture <span style="color: #339933;">=</span> Lists.<span style="color: #006633;">newArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>F element <span style="color: #339933;">:</span> src<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 	
    Callable<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> transformTask <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Callable<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">public</span> T call<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> fn.<span style="color: #006633;">apply</span><span style="color: #009900;">&#40;</span>element<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 				
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    resultFuture.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>service.<span style="color: #006633;">submit</span><span style="color: #009900;">&#40;</span>transformTask<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  ...
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The ExecutorService returns a <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/Future.html">Future</a> which allows us to track the progress of our callables. After we have scheduled our transformations we want the current thread to wait until everything has been completed. Future.get provides exactly this behaviour.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #339933;">&lt;</span>F, T<span style="color: #339933;">&gt;</span> List<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> transform<span style="color: #009900;">&#40;</span>List<span style="color: #339933;">&lt;</span>F<span style="color: #339933;">&gt;</span> src, Function<span style="color: #339933;">&lt;</span>F, T<span style="color: #339933;">&gt;</span> fn<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  ExecutorService service <span style="color: #339933;">=</span> Executors.<span style="color: #006633;">newFixedThreadPool</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  List<span style="color: #339933;">&lt;</span>Future<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;&gt;</span> resultFuture <span style="color: #339933;">=</span> Lists.<span style="color: #006633;">newArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>F element <span style="color: #339933;">:</span> src<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Callable<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> transformTask <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Callable<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">public</span> T call<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> fn.<span style="color: #006633;">apply</span><span style="color: #009900;">&#40;</span>element<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    resultFuture.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>service.<span style="color: #006633;">submit</span><span style="color: #009900;">&#40;</span>transformTask<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> 
  List<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> result <span style="color: #339933;">=</span> Lists.<span style="color: #006633;">newArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>Future<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> future <span style="color: #339933;">:</span> resultFuture<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
      result.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>future.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">RuntimeException</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">return</span> result<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And that's pretty much it, our transformations will be queued up into the executor service and executed in parallel. The current thread will block until the last transformation is complete and then the list of transformed results will be returned as expected. </p>
<p>Now all you need to do is replace the relevant imports of the google transform method with your own concurrent version and your done. If you are using <a href="http://github.com/compactcode/compacted-collections">compacted-collections</a> however, things are even easier. You can switch to parallel processing mode with a single fluent method call. It's as easy as selecting the number of threads you want:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Standard transformations</span>
FluentList.<span style="color: #006633;">listOf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;a&quot;</span>, <span style="color: #0000ff;">&quot;b&quot;</span>, <span style="color: #0000ff;">&quot;c&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">map</span><span style="color: #009900;">&#40;</span>Functions.<span style="color: #006633;">toInt</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Parallel transformations</span>
FluentList.<span style="color: #006633;">listOf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;a&quot;</span>, <span style="color: #0000ff;">&quot;b&quot;</span>, <span style="color: #0000ff;">&quot;c&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">parallel</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">map</span><span style="color: #009900;">&#40;</span>Functions.<span style="color: #006633;">toInt</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This will apply the <a href="http://github.com/compactcode/compacted-collections/blob/master/src/main/java/com/compactcode/strategy/ParallelListStrategy.java">parallel processing strategy</a> to all subsequent transform and filter operations. Everything works exactly as it would with the default <a href="http://github.com/compactcode/compacted-collections/blob/master/src/main/java/com/compactcode/strategy/ImmediateListStrategy.java">single threaded strategy</a>. This allows you to start out with lightweight single thread approach and easily move up to the big guns if and when performance starts to become a problem.</p>
<p>Check it out, I'm no concurrency expert so any feedback on areas for improvement is welcome.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.compactcode.com/2010/04/parallel-processing-and-google-collections/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Google collections and JavaBeans</title>
		<link>http://www.compactcode.com/2010/04/google-collections-and-javabeans/</link>
		<comments>http://www.compactcode.com/2010/04/google-collections-and-javabeans/#comments</comments>
		<pubDate>Wed, 21 Apr 2010 10:47:29 +0000</pubDate>
		<dc:creator>Shanon</dc:creator>
				<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.compactcode.com/?p=500</guid>
		<description><![CDATA[In this article I want to show you some useful techniques to simplify the process of using google collections with your JavaBeans. 
Examples will be based around the manipulation of the following customer data.

  public class Customer &#123;
    private String name;
    public Customer&#40;String name&#41; &#123;
    [...]]]></description>
			<content:encoded><![CDATA[<p>In this article I want to show you some useful techniques to simplify the process of using google collections with your <a href="http://en.wikipedia.org/wiki/JavaBean">JavaBeans</a>. </p>
<p>Examples will be based around the manipulation of the following customer data.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Customer <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> name<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> Customer<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">name</span> <span style="color: #339933;">=</span> name<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> name<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">private</span> List<span style="color: #339933;">&lt;</span>Customer<span style="color: #339933;">&gt;</span> customers <span style="color: #339933;">=</span> newArrayList<span style="color: #009900;">&#40;</span>
    <span style="color: #000000; font-weight: bold;">new</span> Customer<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;shanon&quot;</span><span style="color: #009900;">&#41;</span>, 
    <span style="color: #000000; font-weight: bold;">new</span> Customer<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;fred&quot;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Extracting a list of property values</strong></p>
<p>If you want to extract a list of names from your customers you might start with an implementation like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> com.<span style="color: #006633;">google</span>.<span style="color: #006633;">common</span>.<span style="color: #006633;">collect</span>.<span style="color: #006633;">Lists</span>.<span style="color: #006633;">newArrayList</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> com.<span style="color: #006633;">google</span>.<span style="color: #006633;">common</span>.<span style="color: #006633;">collect</span>.<span style="color: #006633;">Lists</span>.<span style="color: #006633;">transform</span><span style="color: #339933;">;</span>
  ...
  @Test
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> listCustomerNames<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Function<span style="color: #339933;">&lt;</span>Customer, String<span style="color: #339933;">&gt;</span> toName <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Function<span style="color: #339933;">&lt;</span>Customer, String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> apply<span style="color: #009900;">&#40;</span>Customer customer<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> customer.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> expected <span style="color: #339933;">=</span> newArrayList<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;shanon&quot;</span>, <span style="color: #0000ff;">&quot;fred&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    assertEquals<span style="color: #009900;">&#40;</span>expected, transform<span style="color: #009900;">&#40;</span>customers, toName<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>It's very likely that you will be writing this sort of code a lot. You might need to extract a list of customer emails for a marketing campaign. You might need to extract a list of customer ages so you can calculate the average age of your customers... You get the idea.</p>
<p>A lot of the time your simply going to be extracting a property value from a JavaBean. If your not scared of using a little reflection you can eliminate a lot of this tedious code and still maintain a reasonable level of type safety.</p>
<p><a href="http://github.com/compactcode/compacted-collections">compacted-collections</a> provides a simple reflection based solution that you can use, however it's not difficult to write your own.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> com.<span style="color: #006633;">google</span>.<span style="color: #006633;">common</span>.<span style="color: #006633;">collect</span>.<span style="color: #006633;">Lists</span>.<span style="color: #006633;">newArrayList</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> com.<span style="color: #006633;">google</span>.<span style="color: #006633;">common</span>.<span style="color: #006633;">collect</span>.<span style="color: #006633;">Lists</span>.<span style="color: #006633;">transform</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> com.<span style="color: #006633;">compactcode</span>.<span style="color: #006633;">Functions</span>.<span style="color: #006633;">toPropertyValue</span><span style="color: #339933;">;</span>
  ...
  @Test
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> listCustomerNamesUsingReflection<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Function<span style="color: #339933;">&lt;</span>Customer, String<span style="color: #339933;">&gt;</span> toName <span style="color: #339933;">=</span> toPropertyValue<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> expected <span style="color: #339933;">=</span> newArrayList<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;shanon&quot;</span>, <span style="color: #0000ff;">&quot;fred&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    assertEquals<span style="color: #009900;">&#40;</span>expected, transform<span style="color: #009900;">&#40;</span>customers, toName<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Filtering a list on a property value</strong></p>
<p>If you want to filter a list of customers by name you might start with an implementation like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> com.<span style="color: #006633;">google</span>.<span style="color: #006633;">common</span>.<span style="color: #006633;">collect</span>.<span style="color: #006633;">Iterables</span>.<span style="color: #006633;">find</span><span style="color: #339933;">;</span>
  ...
  @Test
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> findCustomerNameEqualToFred<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;fred&quot;</span>, find<span style="color: #009900;">&#40;</span>customers, nameEqualTo<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;fred&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> Predicate<span style="color: #339933;">&lt;</span>Customer<span style="color: #339933;">&gt;</span> nameEqualTo<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Predicate<span style="color: #339933;">&lt;</span>Customer<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> apply<span style="color: #009900;">&#40;</span>Customer customer<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> customer.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Inside this predicate we are converting our customer to a name and then performing an equals on that name. But again this is the sort of code your going to be writing a lot so we need something that's a bit more reusable.</p>
<p>Google collections provides a handy way to for us to combine existing functions and predicates in interesting ways that help promote code re-use.</p>
<p>Using composition we can combine our existing function for mapping customers to names with a predicate for matching strings like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> com.<span style="color: #006633;">google</span>.<span style="color: #006633;">common</span>.<span style="color: #006633;">base</span>.<span style="color: #006633;">Predicates</span>.<span style="color: #006633;">compose</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> com.<span style="color: #006633;">google</span>.<span style="color: #006633;">common</span>.<span style="color: #006633;">base</span>.<span style="color: #006633;">Predicates</span>.<span style="color: #006633;">equalTo</span><span style="color: #339933;">;</span>
  ...
  <span style="color: #000000; font-weight: bold;">private</span> Predicate<span style="color: #339933;">&lt;</span>Customer<span style="color: #339933;">&gt;</span> nameEqualTo<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> compose<span style="color: #009900;">&#40;</span>equalTo<span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span>, toName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>If you are using <a href="http://github.com/compactcode/compacted-collections">compacted-collections</a> you can achieve a similar result with the convenience methods that do the composition for you:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> com.<span style="color: #006633;">compactcode</span>.<span style="color: #006633;">FluentList</span>.<span style="color: #006633;">fluent</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> com.<span style="color: #006633;">google</span>.<span style="color: #006633;">common</span>.<span style="color: #006633;">base</span>.<span style="color: #006633;">Predicates</span>.<span style="color: #006633;">equalTo</span><span style="color: #339933;">;</span>
  ...
  @Test
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> findCustomerNameEqualToFred<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;fred&quot;</span>, customers.<span style="color: #006633;">find</span><span style="color: #009900;">&#40;</span>toName, equalTo<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;fred&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Or, if you wanted to avoid using composition altogether you could just use a Hamcrest matcher that achieves the same result:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> com.<span style="color: #006633;">compactcode</span>.<span style="color: #006633;">FluentList</span>.<span style="color: #006633;">fluent</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">hamcrest</span>.<span style="color: #006633;">beans</span>.<span style="color: #006633;">HasPropertyWithValue</span>.<span style="color: #006633;">hasProperty</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">hamcrest</span>.<span style="color: #006633;">core</span>.<span style="color: #006633;">IsEqual</span>.<span style="color: #006633;">equalTo</span><span style="color: #339933;">;</span>
  ...
  @Test
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> findCustomerNameEqualToFred<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Matcher<span style="color: #339933;">&lt;</span>Customer<span style="color: #339933;">&gt;</span> isFred <span style="color: #339933;">=</span> hasProperty<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;name&quot;</span>, equalTo<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;fred&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;fred&quot;</span>, customers.<span style="color: #006633;">find</span><span style="color: #009900;">&#40;</span>isFred<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Well, that's all I've got for now. I don't really feel like I've done these topics justice, especially composition but perhaps it is enough of a spark to start a fire <img src='http://www.compactcode.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.compactcode.com/2010/04/google-collections-and-javabeans/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Google collections made easier</title>
		<link>http://www.compactcode.com/2010/04/google-collections-made-easier/</link>
		<comments>http://www.compactcode.com/2010/04/google-collections-made-easier/#comments</comments>
		<pubDate>Mon, 19 Apr 2010 12:20:53 +0000</pubDate>
		<dc:creator>Shanon</dc:creator>
				<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.compactcode.com/?p=464</guid>
		<description><![CDATA[Due to legal/ethical objections around trademark violation the project presented here has been renamed to compacted-collections.
I spent a few hours over the last two weeks working on a little project that aims to make google collections even easier to use. I set out to make a thin wrapper that mimics the way the ruby programming [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Due to legal/ethical objections around trademark violation the project presented here has been renamed to compacted-collections.</strong></p>
<p>I spent a few hours over the last two weeks working on a little project that aims to make <a href="http://code.google.com/p/google-collections/">google collections</a> even easier to use. I set out to make a thin wrapper that mimics the way the ruby programming language <a href="http://ruby-doc.org/core/classes/Array.html">does collections</a>. </p>
<p>The result is <a href="http://github.com/compactcode/compacted-collections">compacted-collections</a> and this is what you can do with it...</p>
<p><strong>Sorting:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> com.<span style="color: #006633;">compactcode</span>.<span style="color: #006633;">FluentList</span>.<span style="color: #006633;">fluent</span><span style="color: #339933;">;</span>
&nbsp;
fluent<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">sort</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
fluent<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">reverse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Filtering:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> com.<span style="color: #006633;">compactcode</span>.<span style="color: #006633;">FluentList</span>.<span style="color: #006633;">fluent</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">hamcrest</span>.<span style="color: #006633;">number</span>.<span style="color: #006633;">OrderingComparisons</span>.<span style="color: #006633;">greaterThan</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">hamcrest</span>.<span style="color: #006633;">text</span>.<span style="color: #006633;">StringStartsWith</span>.<span style="color: #006633;">startsWith</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> com.<span style="color: #006633;">google</span>.<span style="color: #006633;">common</span>.<span style="color: #006633;">base</span>.<span style="color: #006633;">Predicates</span>.<span style="color: #006633;">equalTo</span><span style="color: #339933;">;</span>
&nbsp;
fluent<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;foo&quot;</span>, <span style="color: #0000ff;">&quot;bar&quot;</span>, <span style="color: #0000ff;">&quot;baz&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">filter</span><span style="color: #009900;">&#40;</span>startsWith<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ba&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
fluent<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">filter</span><span style="color: #009900;">&#40;</span>greaterThan<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
fluent<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">find</span><span style="color: #009900;">&#40;</span>equalTo<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
fluent<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">first</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
fluent<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">last</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In order to provide a bunch of out of the box predicates the project has been tightly integrated with the <a href="http://code.google.com/p/hamcrest/wiki/Tutorial">Hamcrest</a> project. You can use hamcrest matchers interchangeably with predicates.</p>
<p><strong>Transformation:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> com.<span style="color: #006633;">compactcode</span>.<span style="color: #006633;">FluentList</span>.<span style="color: #006633;">fluent</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> com.<span style="color: #006633;">compactcode</span>.<span style="color: #006633;">Functions</span>.<span style="color: #006633;">toInt</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> com.<span style="color: #006633;">compactcode</span>.<span style="color: #006633;">Functions</span>.<span style="color: #006633;">sum</span><span style="color: #339933;">;</span>
&nbsp;
fluent<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;1&quot;</span>, <span style="color: #0000ff;">&quot;2&quot;</span>, <span style="color: #0000ff;">&quot;3&quot;</span>, <span style="color: #0000ff;">&quot;4&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">map</span><span style="color: #009900;">&#40;</span>toInt<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
fluent<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">reduce</span><span style="color: #009900;">&#40;</span>sum<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Like google collections everything statically typed, so you will get instant feedback about your mistakes. The really nice thing though is that the interface is fluent so you can chain together operations in a nice readable way.</p>
<p><strong>Transformation + Filtering + Sorting:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> com.<span style="color: #006633;">compactcode</span>.<span style="color: #006633;">FluentList</span>.<span style="color: #006633;">fluent</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> com.<span style="color: #006633;">compactcode</span>.<span style="color: #006633;">Functions</span>.<span style="color: #006633;">toInt</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">hamcrest</span>.<span style="color: #006633;">number</span>.<span style="color: #006633;">OrderingComparisons</span>.<span style="color: #006633;">greaterThan</span><span style="color: #339933;">;</span>
&nbsp;
fluent<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;4&quot;</span>, <span style="color: #0000ff;">&quot;1&quot;</span>, <span style="color: #0000ff;">&quot;2&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">map</span><span style="color: #009900;">&#40;</span>toInt<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">filter</span><span style="color: #009900;">&#40;</span>greaterThan<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">sort</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Ignoring the imports it doesn't really read much different from a pure ruby implementation...</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;4&quot;</span>, <span style="color:#996600;">&quot;1&quot;</span>, <span style="color:#996600;">&quot;2&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">map</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>it<span style="color:#006600; font-weight:bold;">|</span> it.<span style="color:#9900CC;">to_i</span><span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#CC0066; font-weight:bold;">select</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>it<span style="color:#006600; font-weight:bold;">|</span> it <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">sort</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>If you're interested in having a look the code it's all available on <a href="http://github.com/compactcode/compacted-collections">github</a>. You can clone it, download it or fork it to your hearts content. The project is built using maven so it should be easy to get it working in your favourite IDE.</p>
<p>I'll be posting more about the features of the library over the next weeks so if you like what you see and you'd like something explained or improved, let me know.</p>
<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.compactcode.com/2010/04/google-collections-made-easier/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Introducing Compaction</title>
		<link>http://www.compactcode.com/2009/09/introducing-compaction/</link>
		<comments>http://www.compactcode.com/2009/09/introducing-compaction/#comments</comments>
		<pubDate>Thu, 24 Sep 2009 13:14:15 +0000</pubDate>
		<dc:creator>Shanon</dc:creator>
				<category><![CDATA[flex]]></category>

		<guid isPermaLink="false">http://www.compactcode.com/?p=429</guid>
		<description><![CDATA[I've been very quiet on the blogging front for the past few weeks. Although I would like to tell you that I have been relaxing on the beach somewhere, sipping cocktails and working a tan, it would be a lie. I have actually been spending my spare time creating a new open source project that [...]]]></description>
			<content:encoded><![CDATA[<p>I've been very quiet on the blogging front for the past few weeks. Although I would like to tell you that I have been relaxing on the beach somewhere, sipping cocktails and working a tan, it would be a lie. I have actually been spending my spare time creating a new open source project that I would like to introduce today.</p>
<p>The project I have been working on is called <a href="http://github.com/compactcode/compaction">compaction</a>, a small flex library designed to take some of the hard work out of creating CRUD based applications. </p>
<p>Some of the notable benefits of using the library include:</p>
<ul>
<li>Automatically detect user changes.</li>
<li>Automatically undo user changes.</li>
<li>Validate at the form level.</li>
<li>Convention over configuration data binding.</li>
<li>Disable buttons when validation fails.</li>
<li>Disable buttons until changes have been made.</li>
</ul>
<p>Although providing you with a lot of useful stuff, it has been designed to be as unobtrusive as possible. You can use <a href="http://github.com/compactcode/compaction">compaction</a> on a single form just like this:</p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;"><span style="color: #808080; font-style: italic;">&lt;!-- The edit model does the heavy lifting  --&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;model:EditModel</span> id=<span style="color: #ff0000;">&quot;model&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Form</span> id=<span style="color: #ff0000;">&quot;form&quot;</span> width=<span style="color: #ff0000;">&quot;100%&quot;</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:FormItem</span> label=<span style="color: #ff0000;">&quot;Name&quot;</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:TextInput</span> id=<span style="color: #ff0000;">&quot;nameInput&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:FormItem</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:FormItem</span> direction=<span style="color: #ff0000;">&quot;horizontal&quot;</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Button</span> id=<span style="color: #ff0000;">&quot;saveButton&quot;</span> label=<span style="color: #ff0000;">&quot;Save&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Button</span> id=<span style="color: #ff0000;">&quot;cancelButton&quot;</span> label=<span style="color: #ff0000;">&quot;Cancel&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:FormItem</span><span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:Form</span><span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #808080; font-style: italic;">&lt;!-- The binder wires the form components into the model --&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;binder:FormBinder</span> source=<span style="color: #ff0000;">&quot;{model}&quot;</span> target=<span style="color: #ff0000;">&quot;{form}&quot;</span> <span style="color: #7400FF;">/&gt;</span></span></pre></div></div>

<p>The edit model represents the state (editing, changed, valid, saving etc) of the editing process while the behaviour is encapsulated by a number of actions accessible from the model.</p>
<ul>
<li>model.edit (edit an object)</li>
<li>model.cancel (undo user changes)</li>
<li>model.save (persist user changes)</li>
</ul>
<p>These actions are automatically enabled/disabled based on the state of the model. They also display useful tool tips when they are disabled to reduce confusion.</p>
<p>The form binder uses a convention over configuration approach to wire the form components into the model.  I wont go into the conventions in this post but bindings on input fields are two way with user changes commited on "change" by default. </p>
<p>That concludes my very quick introduction to <a href="http://github.com/compactcode/compaction">compaction</a>, one of my main goals at this stage is to create a small sample application. I'd rather not reinvent the wheel here so if you know of an existing project that might benefit please let me know!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.compactcode.com/2009/09/introducing-compaction/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Flex Validation – Future Directions</title>
		<link>http://www.compactcode.com/2009/09/flex-validation-%e2%80%93-future-directions/</link>
		<comments>http://www.compactcode.com/2009/09/flex-validation-%e2%80%93-future-directions/#comments</comments>
		<pubDate>Thu, 03 Sep 2009 03:13:26 +0000</pubDate>
		<dc:creator>Shanon</dc:creator>
				<category><![CDATA[flex]]></category>

		<guid isPermaLink="false">http://www.compactcode.com/?p=372</guid>
		<description><![CDATA[I'm currently working on creating a small flex library with the goal of simplifying the task of implementing common user interface logic. One of the areas that is of particular interest to me is validation. Although the built in flex validators are useful they lack some important functionality needed to use them in any non [...]]]></description>
			<content:encoded><![CDATA[<p>I'm currently working on creating a small flex library with the goal of simplifying the task of implementing common user interface <strong>logic</strong>. One of the areas that is of particular interest to me is validation. Although the built in flex validators are useful they lack some important functionality needed to use them in any non trivial application. </p>
<p>In particular, the flex validators are quite narrowly focused on the validation of individual components like the text input or date chooser. While that's great it limits our ability to easily validate an entire form or re-use our validators outside mxml.</p>
<p>In the Java world when you want to validate an object you implement a single method that executes the desired validation routines and collects the results.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PersonValidator <span style="color: #000000; font-weight: bold;">implements</span> Validator <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> validate<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> obj, Errors e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    ValidationUtils.<span style="color: #006633;">rejectIfEmpty</span><span style="color: #009900;">&#40;</span>e, <span style="color: #0000ff;">&quot;name&quot;</span>, <span style="color: #0000ff;">&quot;name.empty&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Person p <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Person<span style="color: #009900;">&#41;</span> obj<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>p.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      e.<span style="color: #006633;">rejectValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;age&quot;</span>, <span style="color: #0000ff;">&quot;negativevalue&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>p.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">110</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      e.<span style="color: #006633;">rejectValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;age&quot;</span>, <span style="color: #0000ff;">&quot;too.darn.old&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I think this is a great approach for three reasons:</p>
<ol>
<li>You can use your validators everywhere, not just in the UI</li>
<li>Your validation routines are all grouped together in a single spot</li>
<li>You can easily unit test your validation</li>
</ol>
<p>So naturally I'm interested in bringing something like this to the Flex world. Using a <a href="http://en.wikipedia.org/wiki/Fluent_interface">fluent</a> <a href="http://en.wikipedia.org/wiki/Builder_pattern">builder pattern</a> I think I am getting to a position where the validation process is becoming fairly straightforward  e.g.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> CustomerValidator implements Validator <span style="color: #000000;">&#123;</span>
&nbsp;
  <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> blackList<span style="color: #000000; font-weight: bold;">:</span>ArrayCollection = <span style="color: #0033ff; font-weight: bold;">new</span> ArrayCollection<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span><span style="color: #990000;">&quot;a@a.com&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
  <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> validate<span style="color: #000000;">&#40;</span>builder<span style="color: #000000; font-weight: bold;">:</span>ValidationBuilder, item<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #6699cc; font-weight: bold;">var</span> customer<span style="color: #000000; font-weight: bold;">:</span>Customer = Customer<span style="color: #000000;">&#40;</span>item<span style="color: #000000;">&#41;</span>;
    builder.string<span style="color: #000000;">&#40;</span>customer.<span style="color: #004993;">name</span>, <span style="color: #990000;">&quot;name&quot;</span><span style="color: #000000;">&#41;</span>.notEmpty<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.minLength<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">3</span><span style="color: #000000;">&#41;</span>;
    builder.number<span style="color: #000000;">&#40;</span>customer.age, <span style="color: #990000;">&quot;age&quot;</span><span style="color: #000000;">&#41;</span>.between<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>, <span style="color: #000000; font-weight:bold;">110</span><span style="color: #000000;">&#41;</span>;
    builder.string<span style="color: #000000;">&#40;</span>customer.email, <span style="color: #990000;">&quot;email&quot;</span><span style="color: #000000;">&#41;</span>.validEmail<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.notIn<span style="color: #000000;">&#40;</span>blackList<span style="color: #000000;">&#41;</span>;
  <span style="color: #000000;">&#125;</span>		
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>When writing a validator most of the heavy lifting is done by interacting with the builder:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> interface ValidationBuilder <span style="color: #000000;">&#123;</span>
  <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #0033ff; font-weight: bold;">get</span> routines<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> ValidationRoutines;
  <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #0033ff; font-weight: bold;">get</span> messages<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> ValidationMessages;
&nbsp;
  <span style="color: #339966; font-weight: bold;">function</span> addError<span style="color: #000000;">&#40;</span><span style="color: #004993;">error</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>, key<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>=<span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span>;
  <span style="color: #339966; font-weight: bold;">function</span> hasErrors<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Boolean</span>;
&nbsp;
  <span style="color: #339966; font-weight: bold;">function</span> isNull<span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>, key<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>=<span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> ValidationBuilder;
  <span style="color: #339966; font-weight: bold;">function</span> isNotNull<span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>, key<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>=<span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> ValidationBuilder;	
&nbsp;
  <span style="color: #339966; font-weight: bold;">function</span> string<span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>, key<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>=<span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> StringValidationBuilder;
  <span style="color: #339966; font-weight: bold;">function</span> number<span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>, key<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>=<span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> NumberValidationBuilder;
  <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">date</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Date</span>, key<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>=<span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> DateValidationBuilder;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>You can easily use the the results of your validators in mxml using syntax similar to this...</p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;"><span style="color: #7400FF;">&lt;v:ValidationListener</span> source=<span style="color: #ff0000;">&quot;{validator}&quot;</span> target=<span style="color: #ff0000;">&quot;{nameText}&quot;</span> key=<span style="color: #ff0000;">&quot;name&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;v:ValidationListener</span> source=<span style="color: #ff0000;">&quot;{validator}&quot;</span> target=<span style="color: #ff0000;">&quot;{ageText}&quot;</span> key=<span style="color: #ff0000;">&quot;age&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;v:ValidationListener</span> source=<span style="color: #ff0000;">&quot;{validator}&quot;</span> target=<span style="color: #ff0000;">&quot;{emailText}&quot;</span> key=<span style="color: #ff0000;">&quot;email&quot;</span><span style="color: #7400FF;">/&gt;</span></span></pre></div></div>

<p>You'll notice that several things are missing from this small overview including how you trigger validation and what happens with the results. The short story is that validation is just one component in the overall functionality required when editing objects. </p>
<p>I'm working on simple solutions to the overall problem but today I wanted to discuss how the validation side of things is shaping up.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.compactcode.com/2009/09/flex-validation-%e2%80%93-future-directions/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Unit Testing Flex &#8211; Mocking</title>
		<link>http://www.compactcode.com/2009/08/unit-testing-flex-mocking/</link>
		<comments>http://www.compactcode.com/2009/08/unit-testing-flex-mocking/#comments</comments>
		<pubDate>Wed, 26 Aug 2009 12:09:39 +0000</pubDate>
		<dc:creator>Shanon</dc:creator>
				<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.compactcode.com/?p=288</guid>
		<description><![CDATA[If you have experimented with unit testing either recently or in the past you will be aware that sometimes it can get difficult. There are many challenges you might encounter including the following:

You need to ensure something happened, without going into detail.
You have to do too much to write a single test.
You need to test [...]]]></description>
			<content:encoded><![CDATA[<p>If you have experimented with unit testing either recently or in the past you will be aware that sometimes it can get difficult. There are many challenges you might encounter including the following:</p>
<ul>
<li>You need to ensure something happened, without going into detail.</li>
<li>You have to do too much to write a single test.</li>
<li>You need to test something that you don't have control over.</li>
</ul>
<p>In this post I want to show some ways to get around these common unit testing roadblocks using a concept called <a href="http://en.wikipedia.org/wiki/Mock_object">mock objects</a>. Basically a mock object is just an object that can simulate the behaviour of another object in order to make unit tests easier to write.</p>
<p><em>Imagine yourself working as a developer at an exciting new tech startup...</em></p>
<p>Your company is launching a new website tomorrow that lets users advertise and sell their cars online. Your boss turns up at your desk at 10 am in the morning, he has a rather worried look on his face.  It turns out that he forgot to schedule the feature that actually allows users to publish vehicles to the site. Oops! Your boss tells you to drop everything and get this done by the end of the day because if you don't, your fired!</p>
<p>You look through the code and find that a service method exists that does what you want:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> com.compact.mocking <span style="color: #000000;">&#123;</span>
  <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> VehicleService <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">publish</span><span style="color: #000000;">&#40;</span>item<span style="color: #000000; font-weight: bold;">:</span>Vehicle<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
      ...
    <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Excellent now all we need to do is ensure we call this function from our GUI model.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> com.compact.mocking <span style="color: #000000;">&#123;</span>
  <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> VehicleModel <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> service<span style="color: #000000; font-weight: bold;">:</span>VehicleService;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">publish</span><span style="color: #000000;">&#40;</span>item<span style="color: #000000; font-weight: bold;">:</span>Vehicle<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
      service.<span style="color: #004993;">publish</span><span style="color: #000000;">&#40;</span>item<span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Easy, next on the list is to write a unit test so we are 100% sure that we are calling the right function. This is were things get a little tricky, we know the VehicleService works and we don't need to test it again. All we really need to do is make sure that we call the vehicle service... but after that we don't really care what happens.</p>
<p><strong>You need to ensure something happened, without going into detail.</strong></p>
<p>Using <a href="http://bitbucket.org/loomis/mockito-flex/wiki/Home">mockito-flex</a> we are going to create a mock version of our service. We will then replace the real service with the mock version. Thanks to mockito-flex our mock will automatically record all invocations it receives. We can then inspect these records to verify if the publish function has been called.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> com.compact.mocking <span style="color: #000000;">&#123;</span>
  <span style="color: #0033ff; font-weight: bold;">import</span> org.mockito.MockitoTestCase;
&nbsp;
  <span style="color: #009900;">// MockitoTestCase extends the standard FlexUnit TestCase. </span>
  <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> CustomerModelTest extends MockitoTestCase <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _model<span style="color: #000000; font-weight: bold;">:</span>VehicleModel;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> CustomerModelTest<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
      <span style="color: #009900;">// Tell mockito which classes will need to be mocked.</span>
      <span style="color: #0033ff; font-weight: bold;">super</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span>VehicleService<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
    override <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> setUp<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
      _model = <span style="color: #0033ff; font-weight: bold;">new</span> VehicleModel<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
      <span style="color: #009900;">// Replace the real service with a mock version.</span>
      _model.service = mock<span style="color: #000000;">&#40;</span>VehicleService<span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> testPublishDelegatesToService<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
      <span style="color: #6699cc; font-weight: bold;">var</span> item<span style="color: #000000; font-weight: bold;">:</span>Vehicle = <span style="color: #0033ff; font-weight: bold;">new</span> Vehicle<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
      <span style="color: #009900;">// This should call the publish function of our mock service.</span>
      _model.<span style="color: #004993;">publish</span><span style="color: #000000;">&#40;</span>item<span style="color: #000000;">&#41;</span>;
&nbsp;
      <span style="color: #009900;">// Verify that the mock service received the right call.</span>
      verify<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.that<span style="color: #000000;">&#40;</span>_model.service.<span style="color: #004993;">publish</span><span style="color: #000000;">&#40;</span>item<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Your boss is impressed, you finished ALL that within 5 minutes! Unfortunately the victory is short lived because he just remembered something... that's right you need make sure the vehicle is valid before publishing.</p>
<p>You have a look through the code and find that this validation method exists on the vehicle:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> com.compact.mocking <span style="color: #000000;">&#123;</span>
  <span style="color: #000000;">&#91;</span>Bindable<span style="color: #000000;">&#93;</span>
  <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Vehicle <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> make<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> model<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> price<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> ownerName<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> locationName<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>;
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> isValid<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Boolean</span> <span style="color: #000000;">&#123;</span>
      <span style="color: #6699cc; font-weight: bold;">var</span> valid<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span> = <span style="color: #0033ff; font-weight: bold;">true</span>;
      valid = valid <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> notEmpty<span style="color: #000000;">&#40;</span>make<span style="color: #000000;">&#41;</span>;
      valid = valid <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> notEmpty<span style="color: #000000;">&#40;</span>model<span style="color: #000000;">&#41;</span>;
      valid = valid <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> notEmpty<span style="color: #000000;">&#40;</span>ownerName<span style="color: #000000;">&#41;</span>;
      valid = valid <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> notEmpty<span style="color: #000000;">&#40;</span>locationName<span style="color: #000000;">&#41;</span>;
      valid = valid <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> price <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight:bold;">0</span>;
      <span style="color: #0033ff; font-weight: bold;">return</span> valid;
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> notEmpty<span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Boolean</span> <span style="color: #000000;">&#123;</span>
      <span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #004993;">value</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #004993;">value</span>.<span style="color: #004993;">length</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight:bold;">0</span>
    <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>So you change your model to take advantage of it.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> com.compact.mocking <span style="color: #000000;">&#123;</span>
  <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> VehicleModel <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> service<span style="color: #000000; font-weight: bold;">:</span>VehicleService;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">publish</span><span style="color: #000000;">&#40;</span>item<span style="color: #000000; font-weight: bold;">:</span>Vehicle<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
      <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>item.isValid<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        service.<span style="color: #004993;">publish</span><span style="color: #000000;">&#40;</span>item<span style="color: #000000;">&#41;</span>;
      <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>And then expertly update your test.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> com.compact.mocking <span style="color: #000000;">&#123;</span>
  <span style="color: #0033ff; font-weight: bold;">import</span> org.mockito.MockitoTestCase;
&nbsp;
  <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> CustomerModelTest extends MockitoTestCase <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _model<span style="color: #000000; font-weight: bold;">:</span>VehicleModel;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> CustomerModelTest<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
      <span style="color: #0033ff; font-weight: bold;">super</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span>VehicleService<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
    override <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> setUp<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
      _model = <span style="color: #0033ff; font-weight: bold;">new</span> VehicleModel<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
      _model.service = mock<span style="color: #000000;">&#40;</span>VehicleService<span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> testPublishDelegatesToService<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
      <span style="color: #6699cc; font-weight: bold;">var</span> item<span style="color: #000000; font-weight: bold;">:</span>Vehicle = <span style="color: #0033ff; font-weight: bold;">new</span> Vehicle<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
      item.make = <span style="color: #990000;">&quot;a&quot;</span>;
      item.model = <span style="color: #990000;">&quot;b&quot;</span>
      item.price = <span style="color: #000000; font-weight:bold;">5</span>;
      item.ownerName = <span style="color: #990000;">&quot;fred&quot;</span>;
      item.locationName = <span style="color: #990000;">&quot;australia&quot;</span>
&nbsp;
      _model.<span style="color: #004993;">publish</span><span style="color: #000000;">&#40;</span>item<span style="color: #000000;">&#41;</span>;
&nbsp;
      verify<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.that<span style="color: #000000;">&#40;</span>_model.service.<span style="color: #004993;">publish</span><span style="color: #000000;">&#40;</span>item<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>You get the job done but you feel like you had to do a lot of work to get there. You also remember that your colleague Fred is currently working on adding more conditions to the validation function. That means you will need to add even more setup to this test.</p>
<p><strong>You have to do too much to write a single test.</strong></p>
<p>You wish there was a way to avoid having to do all that work to make your vehicle valid. Wouldn't it be great if you could just create a mock vehicle that is always valid? Well actually... you can!</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> com.compact.mocking <span style="color: #000000;">&#123;</span>
  <span style="color: #0033ff; font-weight: bold;">import</span> org.mockito.MockitoTestCase;
&nbsp;
  <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> CustomerModelTest extends MockitoTestCase <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _model<span style="color: #000000; font-weight: bold;">:</span>VehicleModel;
    <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _item<span style="color: #000000; font-weight: bold;">:</span>Vehicle;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> CustomerModelTest<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
      <span style="color: #009900;">// We are mocking the vehicle as well.</span>
      <span style="color: #0033ff; font-weight: bold;">super</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span>VehicleService, Vehicle<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
    override <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> setUp<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
      _model = <span style="color: #0033ff; font-weight: bold;">new</span> VehicleModel<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
      _model.service = mock<span style="color: #000000;">&#40;</span>VehicleService<span style="color: #000000;">&#41;</span>;
      <span style="color: #009900;">// Create the mock vehicle.</span>
      _item = mock<span style="color: #000000;">&#40;</span>Vehicle<span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> testPublishDelegatesToService<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
      <span style="color: #009900;">// Update the mock so the isValid() function always return true. </span>
      given<span style="color: #000000;">&#40;</span>_item.isValid<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.willReturn<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span>;
&nbsp;
      _model.<span style="color: #004993;">publish</span><span style="color: #000000;">&#40;</span>_item<span style="color: #000000;">&#41;</span>;
&nbsp;
      verify<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.that<span style="color: #000000;">&#40;</span>_model.service.<span style="color: #004993;">publish</span><span style="color: #000000;">&#40;</span>_item<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Your boss is definitely impressed now.. you just cut five lines out of that test and when Fred updates the validation function.. your test won't break!</p>
<p>Everything is going well as you get ready for the big release, it's 3pm and your just about to celebrate over a few drinks when your boss remembers something... You need to record the time that every vehicle was published, because you are planning on charging your users 50c for each day they have their vehicle on your website. </p>
<p>Amazingly it turns out this functionality is already there on the service!</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> com.compact.mocking <span style="color: #000000;">&#123;</span>
  <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> VehicleService <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">publish</span><span style="color: #000000;">&#40;</span>item<span style="color: #000000; font-weight: bold;">:</span>Vehicle<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
      ...
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> recordPublishTime<span style="color: #000000;">&#40;</span>item<span style="color: #000000; font-weight: bold;">:</span>Vehicle, <span style="color: #004993;">time</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Date</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
      ...
    <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>No problem, we just need to add a call to this function in our model.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> com.compact.mocking <span style="color: #000000;">&#123;</span>
  <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> VehicleModel <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> service<span style="color: #000000; font-weight: bold;">:</span>VehicleService;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">publish</span><span style="color: #000000;">&#40;</span>item<span style="color: #000000; font-weight: bold;">:</span>Vehicle<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
      <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>item.isValid<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        service.<span style="color: #004993;">publish</span><span style="color: #000000;">&#40;</span>item<span style="color: #000000;">&#41;</span>;
        service.recordPublishTime<span style="color: #000000;">&#40;</span>item, <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Date</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
      <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Ok so that's looking good, except how can we verify this call using mockito?<br />
A first attempt might look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> testPublishRecordsPublishTimeUsingService<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
      given<span style="color: #000000;">&#40;</span>_item.isValid<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.willReturn<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span>;
&nbsp;
      _model.<span style="color: #004993;">publish</span><span style="color: #000000;">&#40;</span>_item<span style="color: #000000;">&#41;</span>;
&nbsp;
      verify<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.that<span style="color: #000000;">&#40;</span>_model.service.recordPublishTime<span style="color: #000000;">&#40;</span>_item, <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Date</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span></pre></div></div>

<p>Unfortunately that won't work. The date we are using in our verify is not the same as the date we are creating in our model because the two dates have different millisecond values. How do we take control of the date creation process so that we can match these dates and complete the test?</p>
<p><strong>You need to test something that you don't have control over.</strong></p>
<p>Fortunately it's not too hard, we just need to introduce a date <a href="http://en.wikipedia.org/wiki/Factory_pattern">factory</a>. Once we have this factory, we can then use a mock to precisely control the date creation process. This is what the factory looks like.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> com.compact.mocking <span style="color: #000000;">&#123;</span>
  <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> TimeFactory <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> currentTime<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Date</span> <span style="color: #000000;">&#123;</span>
      <span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Date</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>We then change our model to use the factory instead of directly creating dates.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> com.compact.mocking <span style="color: #000000;">&#123;</span>
  <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> VehicleModel <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> service<span style="color: #000000; font-weight: bold;">:</span>VehicleService;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> timeFactory<span style="color: #000000; font-weight: bold;">:</span>TimeFactory = <span style="color: #0033ff; font-weight: bold;">new</span> TimeFactory<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">publish</span><span style="color: #000000;">&#40;</span>item<span style="color: #000000; font-weight: bold;">:</span>Vehicle<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
      <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>item.isValid<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        service.<span style="color: #004993;">publish</span><span style="color: #000000;">&#40;</span>item<span style="color: #000000;">&#41;</span>;
        service.recordPublishTime<span style="color: #000000;">&#40;</span>item, timeFactory.currentTime<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
      <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Now we can fairly easily mock the factory and complete the test.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> com.compact.mocking <span style="color: #000000;">&#123;</span>
  <span style="color: #0033ff; font-weight: bold;">import</span> org.mockito.MockitoTestCase;
&nbsp;
  <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> CustomerModelTest extends MockitoTestCase <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _model<span style="color: #000000; font-weight: bold;">:</span>VehicleModel;
    <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _item<span style="color: #000000; font-weight: bold;">:</span>Vehicle;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> CustomerModelTest<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
      <span style="color: #0033ff; font-weight: bold;">super</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span>VehicleService, Vehicle, TimeFactory<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
    override <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> setUp<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
      _model = <span style="color: #0033ff; font-weight: bold;">new</span> VehicleModel<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
      _model.service = mock<span style="color: #000000;">&#40;</span>VehicleService<span style="color: #000000;">&#41;</span>;
      _model.timeFactory = mock<span style="color: #000000;">&#40;</span>TimeFactory<span style="color: #000000;">&#41;</span>;
      _item = mock<span style="color: #000000;">&#40;</span>Vehicle<span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> testPublishRecordsPublishTimeUsingService<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
      <span style="color: #6699cc; font-weight: bold;">var</span> publishDate<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Date</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Date</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
      given<span style="color: #000000;">&#40;</span>_item.isValid<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.willReturn<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span>;
      given<span style="color: #000000;">&#40;</span>_model.timeFactory.currentTime<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.willReturn<span style="color: #000000;">&#40;</span>publishDate<span style="color: #000000;">&#41;</span>;
&nbsp;
      _model.<span style="color: #004993;">publish</span><span style="color: #000000;">&#40;</span>_item<span style="color: #000000;">&#41;</span>;
&nbsp;
      verify<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.that<span style="color: #000000;">&#40;</span>_model.service.recordPublishTime<span style="color: #000000;">&#40;</span>_item, publishDate<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p><strong>Phew! You did it.</strong></p>
<p>You just saved the company! You completed the feature that allows your customers to publish their vehicles onto your new website. You unit tested your code and did so in an elegant way using <a href="http://en.wikipedia.org/wiki/Mock_object">mock objects</a> and the <a href="http://bitbucket.org/loomis/mockito-flex/wiki/Home">mockito-flex</a> framework. You encountered three common problems on your journey and overcame them all. Now all you need to do is continue the good work you have been doing and try your hand writing more unit tests. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.compactcode.com/2009/08/unit-testing-flex-mocking/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Unit Testing Flex &#8211; Alerts</title>
		<link>http://www.compactcode.com/2009/08/flex-unit-testing-alerts/</link>
		<comments>http://www.compactcode.com/2009/08/flex-unit-testing-alerts/#comments</comments>
		<pubDate>Wed, 19 Aug 2009 11:04:09 +0000</pubDate>
		<dc:creator>Shanon</dc:creator>
				<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.compactcode.com/?p=253</guid>
		<description><![CDATA[If you ever have a serious attempt at unit testing Flex code sooner or later you are going to need to test something that involves user confirmation. Lets take a simple example of deleting critical business information, the last thing we want is to have a user accidently click the wrong button and delete something [...]]]></description>
			<content:encoded><![CDATA[<p>If you ever have a serious attempt at unit testing Flex code sooner or later you are going to need to test something that involves user confirmation. Lets take a simple example of deleting critical business information, the last thing we want is to have a user accidently click the wrong button and delete something they didn't mean to.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> com.compact <span style="color: #000000;">&#123;</span>
  <span style="color: #0033ff; font-weight: bold;">import</span> mx.controls.Alert;
  <span style="color: #0033ff; font-weight: bold;">import</span> mx.events.CloseEvent;
&nbsp;
  <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> CustomerListModel <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> wasDeleted<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span> = <span style="color: #0033ff; font-weight: bold;">false</span>;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> deleteCustomer<span style="color: #000000;">&#40;</span>customer<span style="color: #000000; font-weight: bold;">:</span>Customer<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
      Alert.<span style="color: #004993;">show</span><span style="color: #000000;">&#40;</span>
        <span style="color: #990000;">&quot;Are you sure you want to delete?&quot;</span>, 
        <span style="color: #990000;">&quot;Confirm&quot;</span>, 
        Alert.YES <span style="color: #000000; font-weight: bold;">+</span> Alert.NO, 
        <span style="color: #0033ff; font-weight: bold;">null</span>,
        <span style="color: #339966; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span>CloseEvent<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
          <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>e.detail == Alert.YES<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            wasDeleted = <span style="color: #0033ff; font-weight: bold;">true</span>;
          <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
      <span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Now, lets say we wanted to write a unit test so that we can be 100% sure that our important information is only going to be deleted if the user confirms that is what they want to do.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> com.compact <span style="color: #000000;">&#123;</span>
  <span style="color: #0033ff; font-weight: bold;">import</span> flexunit.framework.TestCase;
&nbsp;
  <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> CustomerListModelTest extends TestCase <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _model<span style="color: #000000; font-weight: bold;">:</span>CustomerListModel;
    override <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> setUp<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
      _model = <span style="color: #0033ff; font-weight: bold;">new</span> CustomerListModel<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> testDeleteSavesIfUserConfirms<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
      <span style="color: #009900;">// wait a minute, damn i'm screwed!</span>
      assertEquals<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">true</span>, _model.wasDeleted<span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> testDeleteDoesNotSaveIfUserDoesNotConfirm<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
      <span style="color: #009900;">// wait a minute, damn i'm screwed!</span>
      assertEquals<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">false</span>, _model.wasDeleted<span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Ok we didn't get very far! The main problem with trying to test this method is that <a href="http://livedocs.adobe.com/flex/3/langref/mx/controls/Alert.html#show()">Alert.show()</a> will only invoke our close handler if a user physically clicks the alert. We simply have no way to control or replace the actual behaviour of the alert with something that we can simulate for testing.</p>
<p>This is a common problem that you will encounter with unit testing, no matter which language you are using. Fortunately there is a very simple design pattern you can apply to solve it, the <a href="http://en.wikipedia.org/wiki/Adapter_pattern">adapter pattern</a>.</p>
<p>First we create an interface.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> com.compact <span style="color: #000000;">&#123;</span>
  <span style="color: #0033ff; font-weight: bold;">public</span> interface Confirmer <span style="color: #000000;">&#123;</span>
   <span style="color: #3f5fbf;">/**
    * Obtain user confirmation and call the appropriate functions.
    * 
    * @param onYes Invoked if the user responds yes.
    * @param onNo Invoked if the user responds no.
    * @param onComplete Invoked regardless of the response.
    */</span>
    <span style="color: #339966; font-weight: bold;">function</span> confirm<span style="color: #000000;">&#40;</span>
      onYes<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Function</span>, 
      onNo<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Function</span>=<span style="color: #0033ff; font-weight: bold;">null</span>, 
      onComplete<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Function</span>=<span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>;
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>And an implementation.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> com.compact <span style="color: #000000;">&#123;</span>
  <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
&nbsp;
  <span style="color: #0033ff; font-weight: bold;">import</span> mx.controls.Alert;
  <span style="color: #0033ff; font-weight: bold;">import</span> mx.events.CloseEvent;
&nbsp;
  <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> AlertConfirmer implements Confirmer <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> title<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;Confirm&quot;</span>;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">message</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;Are you sure you want to delete?&quot;</span>;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">parent</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span> = <span style="color: #0033ff; font-weight: bold;">null</span>;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> confirm<span style="color: #000000;">&#40;</span>
      onYes<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Function</span>, 
      onNo<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Function</span>=<span style="color: #0033ff; font-weight: bold;">null</span>, 
      onComplete<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Function</span>=<span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
&nbsp;
      Alert.<span style="color: #004993;">show</span><span style="color: #000000;">&#40;</span>
        <span style="color: #004993;">message</span>, 
        title, 
        Alert.YES <span style="color: #000000; font-weight: bold;">+</span> Alert.NO,
        <span style="color: #004993;">parent</span>, 
        <span style="color: #339966; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span>CloseEvent<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
          <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>e.detail == Alert.YES<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            onYes<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
          <span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>onNo <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            onNo<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
          <span style="color: #000000;">&#125;</span>
          <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>onComplete <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            onComplete<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
          <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
      <span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>And then we can change our model to use the new adapter.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> com.compact <span style="color: #000000;">&#123;</span>
  <span style="color: #0033ff; font-weight: bold;">import</span> mx.controls.Alert;
  <span style="color: #0033ff; font-weight: bold;">import</span> mx.events.CloseEvent;
&nbsp;
  <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> CustomerListModel <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> confirmer<span style="color: #000000; font-weight: bold;">:</span>Confirmer = <span style="color: #0033ff; font-weight: bold;">new</span> AlertConfirmer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> wasDeleted<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span> = <span style="color: #0033ff; font-weight: bold;">false</span>;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> deleteCustomer<span style="color: #000000;">&#40;</span>customer<span style="color: #000000; font-weight: bold;">:</span>Customer<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
      confirmer.confirm<span style="color: #000000;">&#40;</span>
        <span style="color: #339966; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
          wasDeleted = <span style="color: #0033ff; font-weight: bold;">true</span>;
        <span style="color: #000000;">&#125;</span>
      <span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>So now we are back were we started with one exception, our alert is hidden behind a well defined interface for obtaining confirmation. This gives us one very significant benefit, we can create a <a href="http://en.wikipedia.org/wiki/Mock_object">mock</a> (fake) confirmer that we can use for testing.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> com.compact <span style="color: #000000;">&#123;</span>
  <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> MockConfirmer implements Confirmer <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _confirm<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> MockConfirmer<span style="color: #000000;">&#40;</span>confirm<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
      _confirm = confirm;
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> confirm<span style="color: #000000;">&#40;</span>
      onYes<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Function</span>, 
      onNo<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Function</span>=<span style="color: #0033ff; font-weight: bold;">null</span>, 
      onComplete<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Function</span>=<span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
&nbsp;
      <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>_confirm<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        onYes<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
      <span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>onNo <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        onNo<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
      <span style="color: #000000;">&#125;</span>
      <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>onComplete <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        onComplete<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
      <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>And now, finally we can complete our unit test.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> com.compact <span style="color: #000000;">&#123;</span>
  <span style="color: #0033ff; font-weight: bold;">import</span> flexunit.framework.TestCase;
&nbsp;
  <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> CustomerListModelTest extends TestCase <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _model<span style="color: #000000; font-weight: bold;">:</span>CustomerListModel;
    override <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> setUp<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
      _model = <span style="color: #0033ff; font-weight: bold;">new</span> CustomerListModel<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> testDeleteSavesIfUserConfirms<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
      _model.confirmer = <span style="color: #0033ff; font-weight: bold;">new</span> MockConfirmer<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span>;
      _model.deleteCustomer<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> Customer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
      assertEquals<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">true</span>, _model.wasDeleted<span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> testDeleteDoesNotSaveIfUserDoesNotConfirm<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
      _model.confirmer = <span style="color: #0033ff; font-weight: bold;">new</span> MockConfirmer<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000000;">&#41;</span>;
      _model.deleteCustomer<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> Customer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
      assertEquals<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">false</span>, _model.wasDeleted<span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><img src="http://www.compactcode.com/wp-content/uploads/2009/08/alertunittest.png" alt="Successful unit test for alert confirmation." title="Successful unit test for alert confirmation." width="508" height="166" class="aligncenter size-full wp-image-272" /><br />
Excellent! Lets summarise what we have done:</p>
<p>We used the <a href="http://en.wikipedia.org/wiki/Adapter_pattern">adapter pattern</a> to create a wrapper around something that is difficult to test. We created a default implementation of the adapter that we will use to replace our original implementation.  We then created a very basic <a href="http://en.wikipedia.org/wiki/Mock_object">mock object</a> that allows us to simulate the behaviour of the adapter so that we can easily test it.</p>
<p>Congratulations you have just learned how to unit test alert confirmations in flex. But more importantly you have learned one of the most common techniques for testing code that is otherwise untestable. </p>
<p><em>Note: To be completely thorough you will need to manually test the default implementation of the adapter to make sure it works.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.compactcode.com/2009/08/flex-unit-testing-alerts/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Unit Testing Flex &#8211; States</title>
		<link>http://www.compactcode.com/2009/08/unit-testing-flex-states/</link>
		<comments>http://www.compactcode.com/2009/08/unit-testing-flex-states/#comments</comments>
		<pubDate>Fri, 14 Aug 2009 05:42:48 +0000</pubDate>
		<dc:creator>Shanon</dc:creator>
				<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.compactcode.com/?p=219</guid>
		<description><![CDATA[This is a quick post to share a solution to a problem I discovered while trying to unit some test some Flex code that involved states. 
FormWithStates.as

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
&#60;?xml version=&#34;1.0&#34; encoding=&#34;utf-8&#34;?&#62;
&#60;mx:HBox xmlns:mx=&#34;http://www.adobe.com/2006/mxml&#34;&#62;
  &#60;mx:states&#62;
    &#60;mx:State name=&#34;advanced&#34;&#62;
      &#60;mx:AddChild relativeTo=&#34;{nameItem}&#34; position=&#34;after&#34;&#62;
        &#60;mx:FormItem label=&#34;Age&#34;&#62;
  [...]]]></description>
			<content:encoded><![CDATA[<p>This is a quick post to share a solution to a problem I discovered while trying to unit some test some Flex code that involved states. </p>
<p><em>FormWithStates.as</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;">&lt;?xml version=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:HBox</span> xmlns:mx=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span><span style="color: #7400FF;">&gt;</span></span>
  <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:states</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:State</span> name=<span style="color: #ff0000;">&quot;advanced&quot;</span><span style="color: #7400FF;">&gt;</span></span>
      <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:AddChild</span> relativeTo=<span style="color: #ff0000;">&quot;{nameItem}&quot;</span> position=<span style="color: #ff0000;">&quot;after&quot;</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:FormItem</span> label=<span style="color: #ff0000;">&quot;Age&quot;</span><span style="color: #7400FF;">&gt;</span></span>
          <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:TextInput</span> id=<span style="color: #ff0000;">&quot;ageText&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:FormItem</span><span style="color: #7400FF;">&gt;</span></span>
      <span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:AddChild</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:State</span><span style="color: #7400FF;">&gt;</span></span>
  <span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:states</span><span style="color: #7400FF;">&gt;</span></span>
  <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Form</span> width=<span style="color: #ff0000;">&quot;100%&quot;</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:FormItem</span> id=<span style="color: #ff0000;">&quot;nameItem&quot;</span> label=<span style="color: #ff0000;">&quot;Name&quot;</span><span style="color: #7400FF;">&gt;</span></span>
      <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:TextInput</span> id=<span style="color: #ff0000;">&quot;nameText&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:FormItem</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:FormItem</span><span style="color: #7400FF;">&gt;</span></span>
      <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Button</span> label=<span style="color: #ff0000;">&quot;Search&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:FormItem</span><span style="color: #7400FF;">&gt;</span></span>
  <span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:Form</span><span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:HBox</span><span style="color: #7400FF;">&gt;</span></span></pre></td></tr></table></div>

<p>I was trying to reference the ageText component from my unit test like this:</p>
<p><em>FormWithStatesTest.as</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> com.compact <span style="color: #000000;">&#123;</span>
  <span style="color: #0033ff; font-weight: bold;">import</span> flexunit.framework.TestCase;
&nbsp;
  <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> FormWithStatesTest extends TestCase <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _view<span style="color: #000000; font-weight: bold;">:</span>FormWithStates;
&nbsp;
    override <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> setUp<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
      _view = <span style="color: #0033ff; font-weight: bold;">new</span> FormWithStates<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
      _view.initialize<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> testCanSetAgeText<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
      _view.ageText.<span style="color: #004993;">text</span> = <span style="color: #990000;">&quot;foo&quot;</span>;
      assertEquals<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;foo&quot;</span>, _view.ageText.<span style="color: #004993;">text</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Unfortunately every time I ran the test it would fail:</p>
<p><img src="http://www.compactcode.com/wp-content/uploads/2009/08/statesfailure.PNG" alt="Trying to use a component created in an mxml state can be tricky." title="Trying to use a component created in an mxml state can be tricky." width="423" height="281" class="aligncenter size-full wp-image-227" /></p>
<p>Null pointer exception. Ok thats because by default all the objects in an mxml state are created lazily. All we needed to do was to find a way to manually make sure my states are being initialized. I searched around the API for a while but failed to find anything obvious that would do the job. In the end I decided to do a bit of a hack and initialize all the components inside the state manually. This is what the end result looked like:</p>
<p><em>FormWithStatesTest.as</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> com.compact <span style="color: #000000;">&#123;</span>
  <span style="color: #0033ff; font-weight: bold;">import</span> flexunit.framework.TestCase;
&nbsp;
  <span style="color: #0033ff; font-weight: bold;">import</span> mx.core.UIComponent;
  <span style="color: #0033ff; font-weight: bold;">import</span> mx.states.IOverride;
  <span style="color: #0033ff; font-weight: bold;">import</span> mx.states.State;
&nbsp;
  <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> FormWithStatesTest extends TestCase <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _view<span style="color: #000000; font-weight: bold;">:</span>FormWithStates;
&nbsp;
    override <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> setUp<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
      _view = <span style="color: #0033ff; font-weight: bold;">new</span> FormWithStates<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
      _view.initialize<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
      initializeStates<span style="color: #000000;">&#40;</span>_view<span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> initializeStates<span style="color: #000000;">&#40;</span>component<span style="color: #000000; font-weight: bold;">:</span>UIComponent<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
      <span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #0033ff; font-weight: bold;">each</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> _state<span style="color: #000000; font-weight: bold;">:</span>State <span style="color: #0033ff; font-weight: bold;">in</span> component.states<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #0033ff; font-weight: bold;">each</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> _override<span style="color: #000000; font-weight: bold;">:</span>IOverride <span style="color: #0033ff; font-weight: bold;">in</span> _state.overrides<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
          _override.initialize<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
        <span style="color: #000000;">&#125;</span>
      <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> testCanSetAgeText<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
      _view.ageText.<span style="color: #004993;">text</span> = <span style="color: #990000;">&quot;foo&quot;</span>;
      assertEquals<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;foo&quot;</span>, _view.ageText.<span style="color: #004993;">text</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Boom. Tests pass. I feel that since its a Friday afternoon I should celebrate, beer anyone? <img src='http://www.compactcode.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.compactcode.com/2009/08/unit-testing-flex-states/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Flex Snippet &#8211; Automatic Dirty Checking</title>
		<link>http://www.compactcode.com/2009/08/flex-snippet-automatic-dirty-checking/</link>
		<comments>http://www.compactcode.com/2009/08/flex-snippet-automatic-dirty-checking/#comments</comments>
		<pubDate>Thu, 13 Aug 2009 13:59:09 +0000</pubDate>
		<dc:creator>Shanon</dc:creator>
				<category><![CDATA[flex]]></category>

		<guid isPermaLink="false">http://www.compactcode.com/?p=198</guid>
		<description><![CDATA[In order to prepare for an upcoming tutorial on flex unit testing I need to present a couple of simple concepts that I can build upon to create some useful example code.
The first of these is the concept of automatic dirty checking. You can use dirty checking to determine if any properties on given object [...]]]></description>
			<content:encoded><![CDATA[<p>In order to prepare for an upcoming tutorial on flex unit testing I need to present a couple of simple concepts that I can build upon to create some useful example code.</p>
<p>The first of these is the concept of automatic dirty checking. You can use dirty checking to determine if any properties on given object have changed since you last looked at it. This technique is quite common in applications where users are editing mission critical data. It can be used to prevent users from forgetting to save something they have changed. An example is probably the easiest way to explain it:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
  <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>importantDataWasChanged<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #6699cc; font-weight: bold;">var</span> saveFirst<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span> = askUserIfTheyWantToSaveBeforeClosing<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>saveFirst<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
      saveImportantInfo<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
  <span style="color: #009900;">// perform close</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>There are various ways you can do this but I want to present an approach that is both easy to use and more importantly, easy to test. The first rule of making something easy to test, create an interface:</p>
<p><em>ChangeDetector.as</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> com.compact <span style="color: #000000;">&#123;</span>
  <span style="color: #3f5fbf;">/**
    * Simple interface for observing changes in a given object.
    */</span>
  <span style="color: #0033ff; font-weight: bold;">public</span> interface ChangeDetector <span style="color: #000000;">&#123;</span>
    <span style="color: #339966; font-weight: bold;">function</span> watch<span style="color: #000000;">&#40;</span>object<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>;
    <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #0033ff; font-weight: bold;">get</span> changed<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>;
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Basically we want to use this interface to watch a single object at a time and use the changed property to query if that object has been modified since we began watching. We can achieve this by 'cloning' the object when we call watch and then comparing the clone to the current version of the object. </p>
<p><em>CloningChangeDetector.as</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> com.compact <span style="color: #000000;">&#123;</span>
  <span style="color: #0033ff; font-weight: bold;">import</span> mx.utils.ObjectUtil;
  <span style="color: #3f5fbf;">/**
    * Uses a clone and compare technique to determine if a given object
    * has changed/is dirty. 
    */</span>
  <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> CloningChangeDetector implements ChangeDetector <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _before<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>;
    <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _now<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>;
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> watch<span style="color: #000000;">&#40;</span>object<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
      _before = ObjectUtil.<span style="color: #004993;">copy</span><span style="color: #000000;">&#40;</span>object<span style="color: #000000;">&#41;</span>;
      _now = object;
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #0033ff; font-weight: bold;">get</span> changed<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span> <span style="color: #000000;">&#123;</span>
      <span style="color: #0033ff; font-weight: bold;">return</span> ObjectUtil.<span style="color: #004993;">compare</span><span style="color: #000000;">&#40;</span>_before, ObjectUtil.<span style="color: #004993;">copy</span><span style="color: #000000;">&#40;</span>_now<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #000000; font-weight:bold;">0</span>;
    <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>And to prove it all works:</p>
<p><em>CloningChangeDetectorTest.as</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> com.compact <span style="color: #000000;">&#123;</span>
  <span style="color: #0033ff; font-weight: bold;">import</span> flexunit.framework.TestCase;
&nbsp;
  <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> CloningChangeDetectorTest extends TestCase <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _detector<span style="color: #000000; font-weight: bold;">:</span>ChangeDetector;
&nbsp;
    override <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> setUp<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
      _detector = <span style="color: #0033ff; font-weight: bold;">new</span> CloningChangeDetector<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> testChangedReturnsFalseByDefault<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
      assertEquals<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">false</span>, _detector.changed<span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> testChangedReturnsFalseIfNothingChanged<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
      <span style="color: #6699cc; font-weight: bold;">var</span> _watched<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Object</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
      _watched.<span style="color: #004993;">name</span> = <span style="color: #990000;">&quot;fred&quot;</span>
      _detector.watch<span style="color: #000000;">&#40;</span>_watched<span style="color: #000000;">&#41;</span>;
      assertEquals<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">false</span>, _detector.changed<span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> testChangedReturnsTrueIfNameChanged<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
      <span style="color: #6699cc; font-weight: bold;">var</span> _watched<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Object</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
      _detector.watch<span style="color: #000000;">&#40;</span>_watched<span style="color: #000000;">&#41;</span>;
      _watched.<span style="color: #004993;">name</span> = <span style="color: #990000;">&quot;fred&quot;</span>
      assertEquals<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">true</span>, _detector.changed<span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> testChangedReturnsFalseIfNameChangeReversed<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
      <span style="color: #6699cc; font-weight: bold;">var</span> _watched<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Object</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
      _watched.<span style="color: #004993;">name</span> = <span style="color: #990000;">&quot;fred&quot;</span>
      _detector.watch<span style="color: #000000;">&#40;</span>_watched<span style="color: #000000;">&#41;</span>;
      _watched.<span style="color: #004993;">name</span> = <span style="color: #990000;">&quot;bob&quot;</span>
      _watched.<span style="color: #004993;">name</span> = <span style="color: #990000;">&quot;fred&quot;</span>
      assertEquals<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">false</span>, _detector.changed<span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> testDetectsNoChangeOnTypedObjects<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
      <span style="color: #6699cc; font-weight: bold;">var</span> _watched<span style="color: #000000; font-weight: bold;">:</span>Customer = <span style="color: #0033ff; font-weight: bold;">new</span> Customer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
      _detector.watch<span style="color: #000000;">&#40;</span>_watched<span style="color: #000000;">&#41;</span>;
      assertEquals<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">false</span>, _detector.changed<span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> testDetectsChangesOnTypedObjects<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
      <span style="color: #6699cc; font-weight: bold;">var</span> _watched<span style="color: #000000; font-weight: bold;">:</span>Customer = <span style="color: #0033ff; font-weight: bold;">new</span> Customer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
      _detector.watch<span style="color: #000000;">&#40;</span>_watched<span style="color: #000000;">&#41;</span>;
      _watched.<span style="color: #004993;">name</span> = <span style="color: #990000;">&quot;fred&quot;</span>
      assertEquals<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">true</span>, _detector.changed<span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #9900cc; font-weight: bold;">class</span> Customer <span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">name</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>;
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Voila, your good to go!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.compactcode.com/2009/08/flex-snippet-automatic-dirty-checking/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unit Testing Flex &#8211; More MXML</title>
		<link>http://www.compactcode.com/2009/08/unit-testing-flex-more-mxml/</link>
		<comments>http://www.compactcode.com/2009/08/unit-testing-flex-more-mxml/#comments</comments>
		<pubDate>Tue, 11 Aug 2009 14:36:38 +0000</pubDate>
		<dc:creator>Shanon</dc:creator>
				<category><![CDATA[flex]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.compactcode.com/?p=157</guid>
		<description><![CDATA[This is a continuation of my previous post. Today I am going to show you three more simple techniques you can use to unit test your mxml:

Button Clicks
List Selection
DataGrid Rendering 

Button Clicks
ClickForm.mxml

1
2
3
4
5
6
7
8
9
10
&#60;?xml version=&#34;1.0&#34; encoding=&#34;utf-8&#34;?&#62;
&#60;mx:HBox 
    xmlns:mx=&#34;http://www.adobe.com/2006/mxml&#34; 
    xmlns:compact=&#34;com.compact.*&#34;&#62;
    &#60;compact:ClickModel id=&#34;model&#34; /&#62;
    &#60;mx:Button 
 [...]]]></description>
			<content:encoded><![CDATA[<p>This is a continuation of my <a href="http://www.compactcode.com/index.php/2009/08/unit-testing-flex-–-mxml/">previous post</a>. Today I am going to show you three more simple techniques you can use to unit test your <strong>mxml</strong>:</p>
<ol>
<li>Button Clicks</li>
<li>List Selection</li>
<li>DataGrid Rendering </li>
</ol>
<h2>Button Clicks</h2>
<p><em>ClickForm.mxml</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;">&lt;?xml version=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:HBox</span> </span>
<span style="color: #000000;">    xmlns:mx=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span> </span>
<span style="color: #000000;">    xmlns:compact=<span style="color: #ff0000;">&quot;com.compact.*&quot;</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;compact:ClickModel</span> id=<span style="color: #ff0000;">&quot;model&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Button</span> </span>
<span style="color: #000000;">      id=<span style="color: #ff0000;">&quot;button&quot;</span> </span>
<span style="color: #000000;">      label=<span style="color: #ff0000;">&quot;Increment&quot;</span> </span>
<span style="color: #000000;">      click=<span style="color: #ff0000;">&quot;model.incrementClickCount()&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:HBox</span><span style="color: #7400FF;">&gt;</span></span></pre></td></tr></table></div>

<p><em>ClickModel.as</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> com.compact <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> ClickModel <span style="color: #000000;">&#123;</span>
      <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> clickCount<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">0</span>;
      <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> incrementClickCount<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
        clickCount<span style="color: #000000; font-weight: bold;">++</span>;
      <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><em>ClickTest.as</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> com.compact <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">MouseEvent</span>;	
    <span style="color: #0033ff; font-weight: bold;">import</span> flexunit.framework.TestCase;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> ClickTest extends TestCase <span style="color: #000000;">&#123;</span>
      <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _form<span style="color: #000000; font-weight: bold;">:</span>ClickForm;
      override <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> setUp<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
        _form = <span style="color: #0033ff; font-weight: bold;">new</span> ClickForm<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
        _form.initialize<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
      <span style="color: #000000;">&#125;</span>
      <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> testClickingButtonIncrementsClickCount<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
        _form.button.<span style="color: #004993;">dispatchEvent</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">CLICK</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
        assertEquals<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span>, _form.model.clickCount<span style="color: #000000;">&#41;</span>;
      <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<h2>List Selection</h2>
<p><em>SelectionForm.mxml</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;">&lt;?xml version=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:HBox</span> </span>
<span style="color: #000000;">    xmlns:mx=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span> </span>
<span style="color: #000000;">    xmlns:compact=<span style="color: #ff0000;">&quot;com.compact.*&quot;</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;compact:SelectionModel</span> id=<span style="color: #ff0000;">&quot;model&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:List</span> </span>
<span style="color: #000000;">      id=<span style="color: #ff0000;">&quot;listView&quot;</span> </span>
<span style="color: #000000;">      dataProvider=<span style="color: #ff0000;">&quot;{model.list}&quot;</span> </span>
<span style="color: #000000;">      change=<span style="color: #ff0000;">&quot;model.selected = listView.selectedItem&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:HBox</span><span style="color: #7400FF;">&gt;</span></span></pre></td></tr></table></div>

<p><em>SelectionModel.as</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> com.compact <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">import</span> mx.collections.ArrayCollection;
    <span style="color: #000000;">&#91;</span>Bindable<span style="color: #000000;">&#93;</span>
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> SelectionModel <span style="color: #000000;">&#123;</span>
        <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> list<span style="color: #000000; font-weight: bold;">:</span>ArrayCollection = <span style="color: #0033ff; font-weight: bold;">new</span> ArrayCollection<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
        <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> selected<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>;
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><em>SelectionTest.as</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> com.compact <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">import</span> flexunit.framework.TestCase;
    <span style="color: #0033ff; font-weight: bold;">import</span> mx.events.ListEvent;
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> SelectionTest extends TestCase <span style="color: #000000;">&#123;</span>
      <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _form<span style="color: #000000; font-weight: bold;">:</span>SelectionForm;
      override <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> setUp<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
        _form = <span style="color: #0033ff; font-weight: bold;">new</span> SelectionForm<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
        _form.initialize<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
      <span style="color: #000000;">&#125;</span>
      <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> testListViewSelectionUpdatesModel<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #6699cc; font-weight: bold;">var</span> itemOne<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Object</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
        <span style="color: #6699cc; font-weight: bold;">var</span> itemTwo<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Object</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
        _form.model.list.addItem<span style="color: #000000;">&#40;</span>itemOne<span style="color: #000000;">&#41;</span>;
        _form.model.list.addItem<span style="color: #000000;">&#40;</span>itemTwo<span style="color: #000000;">&#41;</span>;
        _form.listView.selectedItem = itemTwo;
        _form.listView.<span style="color: #004993;">dispatchEvent</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> ListEvent<span style="color: #000000;">&#40;</span>ListEvent.<span style="color: #004993;">CHANGE</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
        assertEquals<span style="color: #000000;">&#40;</span>itemTwo, _form.model.selected<span style="color: #000000;">&#41;</span>;
      <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<h2>DataGrid Rendering</h2>
<p><em>DataGridForm.mxml</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;">&lt;?xml version=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:HBox</span> </span>
<span style="color: #000000;">    xmlns:mx=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span> </span>
<span style="color: #000000;">    xmlns:compact=<span style="color: #ff0000;">&quot;com.compact.*&quot;</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;compact:DataGridModel</span> id=<span style="color: #ff0000;">&quot;model&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:DataGrid</span> id=<span style="color: #ff0000;">&quot;customerGrid&quot;</span> dataProvider=<span style="color: #ff0000;">&quot;{model.customers}&quot;</span><span style="color: #7400FF;">&gt;</span></span>
      <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:columns</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:DataGridColumn</span> id=<span style="color: #ff0000;">&quot;nameCol&quot;</span> dataField=<span style="color: #ff0000;">&quot;name&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:DataGridColumn</span> </span>
<span style="color: #000000;">          id=<span style="color: #ff0000;">&quot;birthCol&quot;</span> </span>
<span style="color: #000000;">          dataField=<span style="color: #ff0000;">&quot;birth&quot;</span> </span>
<span style="color: #000000;">          labelFunction=<span style="color: #ff0000;">&quot;model.formatBirth&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
       <span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:columns</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:DataGrid</span><span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:HBox</span><span style="color: #7400FF;">&gt;</span></span></pre></td></tr></table></div>

<p><em>DataGridModel.as</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> com.compact <span style="color: #000000;">&#123;</span>
  <span style="color: #0033ff; font-weight: bold;">import</span> mx.collections.ArrayCollection;
  <span style="color: #0033ff; font-weight: bold;">import</span> mx.controls.dataGridClasses.DataGridColumn;
  <span style="color: #0033ff; font-weight: bold;">import</span> mx.formatters.DateFormatter;
&nbsp;
  <span style="color: #000000;">&#91;</span>Bindable<span style="color: #000000;">&#93;</span>
  <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> DataGridModel <span style="color: #000000;">&#123;</span>
    <span style="color: #000000;">&#91;</span>ArrayElementType<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;com.compact.Customer&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> customers<span style="color: #000000; font-weight: bold;">:</span>ArrayCollection = <span style="color: #0033ff; font-weight: bold;">new</span> ArrayCollection<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _format<span style="color: #000000; font-weight: bold;">:</span>DateFormatter = <span style="color: #0033ff; font-weight: bold;">new</span> DateFormatter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> formatBirth<span style="color: #000000;">&#40;</span>item<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>, col<span style="color: #000000; font-weight: bold;">:</span>DataGridColumn<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">String</span> 
      _format.formatString = <span style="color: #990000;">&quot;MMMM/YYYY&quot;</span>;
      <span style="color: #0033ff; font-weight: bold;">return</span> _format.format<span style="color: #000000;">&#40;</span>item.birth<span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><em>Customer.as</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> com.compact <span style="color: #000000;">&#123;</span>
  <span style="color: #000000;">&#91;</span>Bindable<span style="color: #000000;">&#93;</span>
  <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Customer <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">name</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> birth<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Date</span>;
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><em>DataGridTest.as</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> com.compact <span style="color: #000000;">&#123;</span>
  <span style="color: #0033ff; font-weight: bold;">import</span> flexunit.framework.TestCase;
&nbsp;
  <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> DataGridTest extends TestCase <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _form<span style="color: #000000; font-weight: bold;">:</span>DataGridForm;
    override <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> setUp<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
      _form = <span style="color: #0033ff; font-weight: bold;">new</span> DataGridForm<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
      _form.initialize<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
      <span style="color: #009900;">// Initializes the datagrid columns.</span>
      _form.customerGrid.validateProperties<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> testNameIsRenderedAsIs<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
      <span style="color: #6699cc; font-weight: bold;">var</span> customer<span style="color: #000000; font-weight: bold;">:</span>Customer = <span style="color: #0033ff; font-weight: bold;">new</span> Customer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
      customer.<span style="color: #004993;">name</span> = <span style="color: #990000;">&quot;foo&quot;</span>;
      assertEquals<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;foo&quot;</span>, _form.nameCol.itemToLabel<span style="color: #000000;">&#40;</span>customer<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> testBirthIsRenderedUsingDateFormat<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
      <span style="color: #6699cc; font-weight: bold;">var</span> customer<span style="color: #000000; font-weight: bold;">:</span>Customer = <span style="color: #0033ff; font-weight: bold;">new</span> Customer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
      customer.birth = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Date</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">2000</span>, <span style="color: #000000; font-weight:bold;">0</span>, <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span>;
      assertEquals<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;January/2000&quot;</span>, _form.birthCol.itemToLabel<span style="color: #000000;">&#40;</span>customer<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Do you find this useful? Does it need more explanation? I'm not really sure what people are looking for so I have left this post as lightweight as possible.</p>
<p>As requested my next post will start looking at some slightly more complicated tests and how we can use mock objects to simplify them.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.compactcode.com/2009/08/unit-testing-flex-more-mxml/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
