<?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>Giordano Scalzo&#039;s Personal Blog &#187; jbehave</title>
	<atom:link href="http://giordano.scalzo.biz/tag/jbehave/feed/" rel="self" type="application/rss+xml" />
	<link>http://giordano.scalzo.biz</link>
	<description>Just another useless weblog</description>
	<lastBuildDate>Fri, 09 Jul 2010 09:41:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Bdd with JBehave</title>
		<link>http://giordano.scalzo.biz/2009/09/04/bdd-with-jbehave/</link>
		<comments>http://giordano.scalzo.biz/2009/09/04/bdd-with-jbehave/#comments</comments>
		<pubDate>Fri, 04 Sep 2009 11:50:20 +0000</pubDate>
		<dc:creator>giordano scalzo</dc:creator>
				<category><![CDATA[bdd]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[jbehave]]></category>
		<category><![CDATA[kata]]></category>

		<guid isPermaLink="false">http://www.scalzo.biz/?p=32</guid>
		<description><![CDATA[Somebody defined Behaviour-Driven Development &#8220;TDD done right&#8221;; maybe is a bit too provocative as definition, but I think it&#8217;s the time to get my hands wet and give Bdd a chance.
JBehave is the first BDD&#8217;s framework, developed by the BDD&#8217;s inventor himself, Dan North, and still one of the more supported in Java community.
I choose [...]]]></description>
			<content:encoded><![CDATA[<p>Somebody defined <a href="http://behaviour-driven.org/" target="_blank">Behaviour-Driven Development</a> &#8220;TDD done right&#8221;; maybe is a bit too provocative as definition, but I think it&#8217;s the time to get my hands wet and give Bdd a chance.</p>
<p><a href="http://jbehave.org/" target="_blank">JBehave</a> is the first BDD&#8217;s framework, developed by the BDD&#8217;s inventor himself, <a href="http://dannorth.net/" target="_blank">Dan North</a>, and still one of the more supported in Java community.</p>
<p>I choose a simple kata to develop in Bdd-way, the StringTemplater kata, as saw in a <a href="http://www.coreyhaines.com/" target="_blank">Corey Haines</a>&#8216; <a href="http://blog.envylabs.com/2009/08/corey-haines-performance-kata/" target="_blank">video</a> and following this excellent <a href="http://www.shaafshah.com/2009/08/12/getting-started-with-jbehave-in-8-steps/" target="_blank">post</a>.</p>
<h3>JBehave on Eclipse: half an hour tutorial</h3>
<p>First of all, we should create a project under Eclipse and add JBehave <a href="http://jbehave.org/software/download/" target="_blank">jars</a>.</p>
<p>Then we should write the basic infrastructure: a textual file with the scenarios and two classes connecting our stories to JBehave.</p>
<p>By default the scenarios file should be without extension and in the same directory of classes, but, aiming to separate specs code and production code, I created a new source directory called <code>scenario</code>.</p>
<p>The scenarios file should have a meaningful name and the words should separated by underscores; in this example I called it <code>replace_tokens_in_string</code>.</p>
<p>At the beginning of the file, we can write the story we should implement: it isn&#8217;t mandatory, but it&#8217;s useful if we use specs to comunicate with the customers.<br />
Following we can write a first basic scenario:</p>
<pre class="brush: bash">Story: replace tokens in String
As a user
I would like replace token in a string with values
So that I can create templates for my configuration files

Scenario: replace empty string
Given I have a StringTemplater
When I ask to replace an emptystring
Then I should get an emptystring</pre>
<p>The story is just descriptive, but the scenario must be of the form of<br />
<code><strong>Given</strong> something<br />
<strong>When</strong> action<br />
<strong>Then</strong> check</code></p>
<p>Now we need create two java classes.<br />
The first one is JBehave wrapper to JUnit TestCase and its name should match the one of textual file converted in CamelCase: in our example should be <code>ReplaceTokensInString</code>.</p>
<pre class="brush: java">package biz.scalzo.kata.stringtemplater.jbehave;

import org.jbehave.scenario.Scenario;

public class ReplaceTokensInString extends Scenario{
	  public ReplaceTokensInString() {
	        super(new ReplaceTokensInStringSteps());
	    }
}</pre>
<p>The second class, <code>ReplaceTokensInStringSteps</code>, implementing the steps of the scenario:</p>
<pre class="brush: java">package biz.scalzo.kata.stringtemplater.jbehave;

import org.jbehave.scenario.steps.Steps;

public class ReplaceTokensInStringSteps extends Steps {
}</pre>
<p>That&#8217;s it!<br />
As said before, JBehave is built over JUnit so we can run <code>ReplaceTokensInString</code> as Junit test obtaining a message from the engine:</p>
<pre class="brush: bash">Story: replace tokens in String
As a user
I would like replace token in a string with values
So that I can create templates for my configuration files

Scenario: replace empty string

Given I have a StringTemplater (PENDING)
When I ask to replace an emptystring (PENDING)
Then I should get an emptystring (PENDING)</pre>
<p><code>PENDING</code> means we have to build that step; so we do it with an empty implementation:</p>
<pre class="brush: java">package biz.scalzo.kata.stringtemplater.jbehave;

import org.jbehave.scenario.annotations.Given;
import org.jbehave.scenario.annotations.Then;
import org.jbehave.scenario.annotations.When;
import org.jbehave.scenario.steps.Steps;

public class ReplaceTokensInStringSteps extends Steps {
	@Given("I have a StringTemplater")
	public void createStringTemplater() {
	}
	@When("I ask to replace an emptystring")
	public void replaceAnEmptystring() {
	}
	@Then("I should get an emptystring")
	public void shouldGetEmptyString() {
	}
}</pre>
<p>and when we run again the test, the <code>PENDING</code> messages should disappear.</p>
<p>This is the basic infrastructure, but, in my tiny experience was the most difficult part: once the empty scenario worked, implement the complete kata was quite straightforward.</p>
<p>This is my final scenarios file:</p>
<pre class="brush: java">Story: replace tokens in String
As a user
I would like replace token in a string with values
So that I can create templates for my configuration files

Scenario: replace empty string
Given I have a StringTemplater
When I ask to replace an emptystring
Then I should get an emptystring

Scenario: replace string without tokens
Given I have a StringTemplater
When I ask to replace 'this string'
Then I should get 'this string'

Scenario: replace string with a token
Given I have a StringTemplater
When I ask to replace 'Hello, $name' with [name : pippo]
Then I should get 'Hello, pippo'

Scenario: replace string with two tokens
Given I have a StringTemplater
When I ask to replace 'Hello, $name, how a $attitude day' with [name : pippo, attitude: wonderful]
Then I should get 'Hello, pippo, how a wonderful day'

Scenario: replace string with two adjacent tokens
Given I have a StringTemplater
When I ask to replace 'Hello, $adj$name' with [name : friend, adj: good]
Then I should get 'Hello, goodfriend'

Scenario: replace string with two tokens and only one value
Given I have a StringTemplater
When I ask to replace 'Hello, $name, how a $attitude day' with [name : pippo]
Then I should get 'Hello, pippo, how a  day'</pre>
<p>This my steps:</p>
<pre class="brush: java">package biz.scalzo.kata.stringtemplater.jbehave;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.jbehave.Ensure.ensureThat;

import org.jbehave.scenario.annotations.Given;
import org.jbehave.scenario.annotations.Then;
import org.jbehave.scenario.annotations.When;
import org.jbehave.scenario.steps.Steps;

public class ReplaceTokensInStringSteps extends Steps {
	private StringTemplater templater;
	private String result;

	@Given("I have a StringTemplater")
	public void createStringTemplater() {
		templater = new StringTemplater();
	}

	@When("I ask to replace an emptystring")
	public void replaceAnEmptystring() {
		result = templater.replace("");
	}

	@Then("I should get an emptystring")
	public void shouldGetEmptyString() {
		ensureThat(result, is(equalTo("")));
	}

	@When("I ask to replace '$stringToReplace'")
	public void replaceStringWithoutTokens(String stringToReplace) {
		result = templater.replace(stringToReplace);
	}

	@Then("I should get '$expected'")
	public void checkStringResult(String expected) {
		ensureThat(result, is(equalTo(expected)));
	}

	@When("I ask to replace '$stringToReplace' with [$tokens]")
	public void replaceStringWithTokens(String stringToReplace, String tokens) {
		result = templater. replace(stringToReplace,tokens);
	}

}</pre>
<p>and, last but not least, <code>StringTemplater</code> class:</p>
<pre class="brush: java">package biz.scalzo.kata.stringtemplater.jbehave;

import java.util.HashMap;
import java.util.Map.Entry;

public class StringTemplater {

	public String replace(String originaleString) {
		return replace(originaleString, "");
	}

	public String replace(String stringToReplace, String tokensAsString) {
		HashMap tokensMap = splitTokens(tokensAsString);
		return removeKeywordWithoutValue(replaceKeywords(stringToReplace,
				tokensMap));
	}

	private String removeKeywordWithoutValue(String replaceKeywords) {
		return replaceKeywords.replaceAll("\\$\\w+", "");
	}

	private String replaceKeywords(String initialValue,
			HashMap tokensMap) {
		String result = initialValue;
		for (Entry entry : tokensMap.entrySet()) {
			result = result
					.replaceAll("\\$" + entry.getKey(), entry.getValue());
		}
		return result;
	}

	private HashMap splitTokens(String tokensAsString) {
		String[] pairs = splitPairs(tokensAsString);

		HashMap result = new HashMap();
		for (String pair : pairs) {
			String[] tokens = pair.split(":");
			if (tokens.length &gt; 1) {
				result.put(tokens[0].trim(), tokens[1].trim());
			}
		}
		return result;
	}

	private String[] splitPairs(String tokensAsString) {
		return tokensAsString.split(",");
	}
}</pre>
<h3>Conclusions</h3>
<p>This is my first impact with Bdd in Java, I liked and I think it&#8217;s very promising.<br />
Neverthless, I still don&#8217;t know if it is something I can do in day by day work or just a proof of concept: Jbehave is quite verbose and the stories are high level specifications, so we need to write a lot of boilerplate code to specify a class.</p>
<p>In my trip in Bdd-land, next steps will be give other Bdd engines a try, starting with ones written in high level languages as <a href="http://www.easyb.org/" target="_blank">easyb</a> or <a href="http://www.artima.com/scalatest/" target="_blank">scalatest</a>.</p>

<!-- start wp-tags-to-technorati 1.01 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/bdd' rel='tag' target='_self'>bdd</a>, <a class='technorati-link' href='http://technorati.com/tag/eclipse' rel='tag' target='_self'>eclipse</a>, <a class='technorati-link' href='http://technorati.com/tag/java' rel='tag' target='_self'>java</a>, <a class='technorati-link' href='http://technorati.com/tag/jbehave' rel='tag' target='_self'>jbehave</a>, <a class='technorati-link' href='http://technorati.com/tag/kata' rel='tag' target='_self'>kata</a>, <a class='technorati-link' href='http://technorati.com/tag/tutorial' rel='tag' target='_self'>tutorial</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://giordano.scalzo.biz/2009/09/04/bdd-with-jbehave/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
