<?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; tutorial</title>
	<atom:link href="http://giordano.scalzo.biz/tag/tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://giordano.scalzo.biz</link>
	<description>Just another useless weblog</description>
	<lastBuildDate>Mon, 09 May 2011 09:50:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Objective-C for busy Java Developers 1: Calling methods</title>
		<link>http://giordano.scalzo.biz/2010/01/19/objective-c-for-busy-java-developers-1-calling-methods/</link>
		<comments>http://giordano.scalzo.biz/2010/01/19/objective-c-for-busy-java-developers-1-calling-methods/#comments</comments>
		<pubDate>Tue, 19 Jan 2010 13:34:43 +0000</pubDate>
		<dc:creator>giordano scalzo</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://giordano.scalzo.biz/?p=261</guid>
		<description><![CDATA[At last I got a wonderful MacBookPro, so I started to study Objective-C to develop some cool Iphone applications. Objective-C is a language derived from C, to which it adds some modern features as ObjectOriented or Smalltalk-style messaging. As far I&#8217;m a complete newbie, I&#8217;m trying to learn it recalling some well know patterns and [...]]]></description>
			<content:encoded><![CDATA[<p>At last I got a wonderful MacBookPro, so I started to study <a href="http://en.wikipedia.org/wiki/Objective-C">Objective-C</a> to develop some cool Iphone applications.</p>
<p><a href="http://en.wikipedia.org/wiki/Objective-C">Objective-C</a> is a language derived from C, to which it adds some modern features as ObjectOriented or Smalltalk-style messaging.</p>
<p>As far I&#8217;m a complete newbie, I&#8217;m trying to learn it recalling some well know patterns and scenarios as made in Java , following <a href="http://cocoadevcentral.com/d/learn_objectivec/">this good tutorial</a>.</p>
<p><a href="http://en.wikipedia.org/wiki/Objective-C">Objective-C</a> has a little strange way to call method, that could be disorienting at first glance:</p>
<p><b>Java</b>:</p>
<pre class='brush:java'>
object.method;
object.methodWithInput(input);

output = object.methodWithOutput();
output = object.methodWithInputAndOutput(Object input);
</pre>
<p><b>Objective-C</b>:</p>
<pre class='brush:c'>
[object method];
[object methodWithInput:input];

output = [object methodWithOutput];
output = [object methodWithInputAndOutput:input];
</pre>
<p>Obviously, it&#8217;s possible to call methods of class, instead of instance:</p>
<p><b>Java</b>:</p>
<pre class='brush:java'>
Object oString = new String();
</pre>
<p><b>Objective-C</b>:</p>
<pre class='brush:c'>
id oString = [NSString string];
</pre>
<p>The
<pre>id</pre>
<p> refers any kind of object, so it&#8217;s little different from Java counterpart.<br />
Better code is:</p>
<p><b>Java</b>:</p>
<pre class='brush:java'>
String sString = new String();
</pre>
<p><b>Objective-C</b>:</p>
<pre class='brush:c'>
NSString* sString = [NSString string];
</pre>
<p>With this style, it&#8217;s a little cumbersome write nested calls:</p>
<p><b>Java</b>:</p>
<pre class='brush:java'>
calculator.add(numbers.split());
</pre>
<p><b>Objective-C</b>:</p>
<pre class='brush:c'>
[calculator add:[numbers split]];
</pre>
<p>This syntax disencourage the nesting of more than one method.</p>
<p>Some methods take multiple input arguments, Objective-C deals with that allowing split method names:</p>
<p><b>Java</b>:</p>
<pre class='brush:java'>
boolean writeToFile(String path, boolean useAuxiliaryFile)

boolean result = myData.writeToFile("/tmp/log.txt", false);
</pre>
<p><b>Objective-C</b>:</p>
<pre class='brush:c'>
-(BOOL)writeToFile:(NSString *)path withAuxFile:(BOOL)useAuxiliaryFile;

BOOL result = [myData writeToFile:@"/tmp/log.txt" withAuxFile:NO];
</pre>
<p>Objective-C has properties built in, in Java you need to implement getters and setters:</p>
<p><b>Java</b>:</p>
<pre class='brush:java'>
photo.setCaption("Day at the Beach");
output = photo.getCaption();
</pre>
<p><b>Objective-C</b>:</p>
<pre class='brush:c'>
photo.caption = @"Day at the Beach";
output = photo.caption;
</pre>
<p>A property should be marked
<pre>@property</pre>
<p> in declaration and
<pre>@synthesize</pre>
<p> in implementation.</p>
<p>To create an object, the function
<pre>alloc</pre>
<p> should be called and then an init method should be called:</p>
<p><b>Java</b>:</p>
<pre class='brush:java'>
object = new ComplexObject(1.0f);
</pre>
<p><b>Objective-C</b>:</p>
<pre class='brush:c'>
object = [[ComplexObject alloc] initWithFloat:1.0f];
</pre>
<p>When working in an environment without garbage collector, any object created with alloc should be released:</p>
<p><b>Objective-C</b>:</p>
<pre class='brush:c'>
[object release];
</pre>
<p>To complete this introductory post, take a look at this <a href='http://giordano.scalzo.biz/wp-content/uploads/2010/01/objectiveccheatsheet.pdf'>ObjectiveC CheatSheet</a>: it contains all the most used constructs needed to start to code for Mac.</p>

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

<p class='technorati-tags'>Technorati Tags: <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/objective-c' rel='tag' target='_self'>objective-c</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/2010/01/19/objective-c-for-busy-java-developers-1-calling-methods/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<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. [...]]]></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.02 -->

<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>
		<item>
		<title>Private git repositories on Site5</title>
		<link>http://giordano.scalzo.biz/2009/08/28/private-git-repositories-on-site5/</link>
		<comments>http://giordano.scalzo.biz/2009/08/28/private-git-repositories-on-site5/#comments</comments>
		<pubDate>Fri, 28 Aug 2009 21:51:18 +0000</pubDate>
		<dc:creator>giordano scalzo</dc:creator>
				<category><![CDATA[tutorial]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[site5]]></category>
		<category><![CDATA[ssh]]></category>

		<guid isPermaLink="false">http://www.scalzo.biz/?p=12</guid>
		<description><![CDATA[It happens I have a shared host on Site5 and it happens most of kool koders are moving to git. I&#8217;m not a so kool koder, but I think git deserve a try. I know Github offers a wonderful service for open source projects, and I tried it for a bunch of pet works, but [...]]]></description>
			<content:encoded><![CDATA[<p>It happens I have a shared host on <a href="http://www.site5.com">Site5</a> and it happens most of kool koders are moving to <a href="http://www.git-scm.com">git</a>.<br />
I&#8217;m not a so kool koder, but I think <a href="http://www.git-scm.com">git</a> deserve a try.</p>
<p>I know <a href="http://www.github.com">Github</a> offers a wonderful service for open source projects, and I tried it for a bunch of pet works, but something is better to keep private, so, some month ago, I tried to set up a <a href="http://www.git-scm.com">git</a> repository on my <a href="http://www.site5.com">Site5</a> account without success.</p>
<p>Some day ago, I bumped into a <a href="http://mark.ryall.name/blog/2008/08/17/using-git-on-site5-the-details/">couple</a> of <a href="http://rwmj.wordpress.com/2009/03/06/git-fatal-no-matching-remote-head/">posts</a> and, finally, I reached my goal.</p>
<h4>Set up a password-less connection</h4>
<p><a href="http://www.git-scm.com">git</a> communication is based on ssh, but a password request each &#8216;git push&#8217; or &#8216;git pull&#8217; can be annoying in day-by-day working, so the first thing is to make our connection trusted by the server.<br />
In  <a href="http://mark.ryall.name/blog/2008/08/17/using-git-on-site5-the-details/">this post</a>, we can find detailed instructions for a Linux system:</p>
<pre class="brush: bash; gutter: false">user@localhost:~$ ssh-keygen -t dsa
user@localhost:~$ scp ~/.ssh/id_dsa.pub remoteuser@remotehost:
user@localhost:~$ ssh remoteuser@remotehost:
[remoteuser@remotehost ~]$ cat id_dsa.pub &gt;&gt; .ssh/authorized_keys</pre>
<p>For other OS, Google is your <a href="http://www.google.it/search?q=ssh+without+password+windows/">friend</a>.</p>
<h4>Create a repository on Site5</h4>
<p>Some month ago, all <a href="http://www.site5.com">Site5</a>&#8216;s servers <a href="http://www.site5.com/blog/s5/updated-git/20090422/">migrated</a> to the last version of <a href="http://www.git-scm.com">git</a> , but if you can&#8217;t find it installed, you can open a ticket: <a href="http://www.site5.com">Site5</a>&#8216;s support is always friendly and quick.<br />
Now we can create our repository:</p>
<pre class="brush: bash; gutter: false">[remoteuser@remotehost ~]$ mkdir -p git/repo.git
[remoteuser@remotehost ~]$ cd git/repo.git
[remoteuser@remotehost ~]$ git --bare init
[remoteuser@remotehost ~]$ exit</pre>
<p>and create our local repository:</p>
<pre class="brush: bash; gutter: false">user@host:~$ mkdir git
user@host:~$ cd git
user@host:~$ git clone ssh://remoteuser@remotehost/~/git/repo.git

user@host:~$ echo 'Here we go' &gt; README
user@host:~$ git add .
user@host:~$ git commit
user@host:~$ git push</pre>
<h4>Fixing “fatal: no matching remote head”-error</h4>
<p>After that, when I tried to push my modifies I got an “fatal: no matching remote head” error&#8230; fortunately I wasn&#8217;t <a href="http://rwmj.wordpress.com/2009/03/06/git-fatal-no-matching-remote-head/">alone</a> with that problem.<br />
First of all we need create an empty local repository then tell <a href="http://www.git-scm.com">git</a> that the &#8220;origin&#8221; of the local repository is the remote repository</p>
<pre class="brush: bash; gutter: false">
user@host:~$ cd repo
user@host:~$ git init
user@host:~$ echo 'Here we go' &gt; README
user@host:~$ git add .
user@host:~$ git commit -ma ''
user@host:~$ git push
user@host:~$ git remote add origin ssh://remotehost/~/git/repo.git
</pre>
<p>Then we have to connect the upstream &#8220;origin&#8221; with the current local branch:</p>
<pre class="brush: bash; gutter: false">vi .git/config</pre>
<p>and adding the following at the bottom:</p>
<pre class="brush: bash; gutter: false">[branch "master"]
  remote = origin
  merge = refs/heads/master</pre>
<p>Finally we have to synchronize remote with local repository:</p>
<pre class="brush: bash; gutter: false">git push origin master</pre>
<p>After that</p>
<pre class="brush: bash; gutter: false">git push</pre>
<p>and</p>
<pre class="brush: bash; gutter: false">git pull</pre>
<p>should work without problem</p>
<h4>Conclusion</h4>
<p>When discovered all necessary steps, any new repository installation goes smooth; in this post I tried to be complete and gather all the necessary informations.<br />
If you are new to <a href="http://www.git-scm.com">git</a> in <a href="http://blog.xebia.com/2009/01/26/git-101/">this</a> page you will find some useful links.</p>

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

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/git' rel='tag' target='_self'>git</a>, <a class='technorati-link' href='http://technorati.com/tag/site5' rel='tag' target='_self'>site5</a>, <a class='technorati-link' href='http://technorati.com/tag/ssh' rel='tag' target='_self'>ssh</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/08/28/private-git-repositories-on-site5/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

