<?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>Matt's Blog &#187; Frameworks</title>
	<atom:link href="http://blog.rueedlinger.ch/category/frameworks/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.rueedlinger.ch</link>
	<description>Software Engineering and Java</description>
	<lastBuildDate>Tue, 07 Sep 2010 17:38:55 +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>More readable and typeable&#8230;</title>
		<link>http://blog.rueedlinger.ch/2009/02/more-readable-and-typeable/</link>
		<comments>http://blog.rueedlinger.ch/2009/02/more-readable-and-typeable/#comments</comments>
		<pubDate>Thu, 26 Feb 2009 22:27:00 +0000</pubDate>
		<dc:creator>Matthias Rüedlinger</dc:creator>
				<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://blog.rueedlinger.ch/?p=42</guid>
		<description><![CDATA[Hamcrest allows you to write readable constraints, which for example can be used with an assert statement. Hamcrest is included since JUnit 4.4 and it is the first time that third-party classes have been included in JUnit. But when you play around with Hamcrest and the assertThat statement you don&#8217;t want go back to old [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://code.google.com/p/hamcrest/">Hamcrest</a> allows you to write readable constraints, which for example can be used with an assert statement. <a href="http://code.google.com/p/hamcrest/">Hamcrest</a> is included since <a href="http://junit.sourceforge.net/doc/ReleaseNotes4.4.html">JUnit 4.4</a> and it is the first time that third-party classes have been included in JUnit. But when you play around with Hamcrest and the <strong>assertThat</strong> statement you don&#8217;t want go back to old complex and unreadable assert statements.</p>
<h3>JUnit and assertThat</h3>
<p>With Hamcrest it&#8217;s very easy to write readable assert statements in a JUnit test case . In the following example we want to check if a person is older than 21 years.</p>
<pre class="code">import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.*;

...

Person person1 = new Person();
person1.setName(&quot;Bob&quot;);
person1.setAge(22);

<strong>assertThat</strong>(person1.getAge(), greaterThan(21));</pre>
<p>As you can see the assert statement is pretty straightforward. The second parameter of an assertThat statement is a Matcher which evaluates if the first parameter is greater than the integer value from the Matcher. This syntax allows you to think in terms of subject, verb, object and not in assertEquals.</p>
<p>Hamcrest uses Matcher which are responsible to evaluate a given statement. A good overview about Hamcrest is the <a href="http://code.google.com/p/hamcrest/wiki/Tutorial">official tutorial</a>.</p>
<p>JUnit currently ships with a few matchers, defined in <strong>org.hamcrest.CoreMatchers</strong> and <strong>org.junit.matchers.JUnitMatchers</strong>. To use the other matchers download the <strong>hamcrest-all.*.jar</strong> library.</p>
<h3>hamcrest-collections</h3>
<p><a href="http://code.google.com/p/hamcrest-collections/">hamcrest-collections</a> implements features such as select, reject, map, reduce and zip which can be used on collections. This is a useful library which uses Hamcrest to operate on collections.</p>
<p>The following example show how to select from a List of integers all numbers between 5 and 9.</p>
<pre class="code">import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrestcollections.Selector.*;

...

List&lt;Integer&gt; numbers = new ArrayList&lt;Integer&gt;();
for (int i = 0; i &lt;= 10; i++) {
	numbers.add(i);
}

Iterable&lt;Integer&gt; items = select(numbers, allOf(greaterThan(5), lessThan(9)));

System.out.println(items);</pre>
<p>Which genearte the following output:</p>
<pre class="code">[6, 7, 8]</pre>
</pre>
<h3>Further reading...</h3>
<p>A good starting point as already mention is the <a href="http://code.google.com/p/hamcrest/">official Hamcrest Homepage</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.rueedlinger.ch/2009/02/more-readable-and-typeable/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>JYaml &#8211; YAML for Java</title>
		<link>http://blog.rueedlinger.ch/2009/02/jyaml-yaml-for-java/</link>
		<comments>http://blog.rueedlinger.ch/2009/02/jyaml-yaml-for-java/#comments</comments>
		<pubDate>Wed, 11 Feb 2009 21:45:43 +0000</pubDate>
		<dc:creator>Matthias Rüedlinger</dc:creator>
				<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://blog.rueedlinger.ch/?p=41</guid>
		<description><![CDATA[“YAML Ain’t Markup Language” (abbreviated YAML) is a data serialization language designed to be human-friendly according to the official Yaml homepage. One implementation for the Java language is the JYaml framework.
There are a lot of other YAML frameworks for different languages. You find a list of some frameworks on the official Yaml homepage. 
Short Introduction [...]]]></description>
			<content:encoded><![CDATA[<p>“YAML Ain’t Markup Language” (abbreviated YAML) is a data serialization language designed to be human-friendly according to the official <a href="http://www.yaml.org">Yaml</a> homepage. One implementation for the Java language is the <a href="http://jyaml.sourceforge.net/">JYaml</a> framework.</p>
<p>There are a lot of other YAML frameworks for different languages. You find a list of some frameworks on the official <a href="http://www.yaml.org">Yaml</a> homepage. </p>
<h3>Short Introduction to YAML</h3>
<p>In YAML there are different kinds of nodes:</p>
<ul>
<li>Scalar is a value that can be presented as a series of zero or more Unicode characters.</li>
<li>Sequence is an ordered series of zero or more nodes.</li>
<li>Mapping is an unordered set of key: value node pairs, with the restriction that each of the keys is unique.</li>
</ul>
<p>YAML uses three dashes (“<strong>- &#8211; -</strong>”) to show that a document begins.  A comment is denoted by a <strong>#</strong>. In the following sections we now have a look at the YAML constructs sequences, mappings, tags, anchors and aliases. </p>
<p><b>Sequence</b><br />
A sequence is  a series of nodes, each denoted by a leading “<strong>-</strong>” indicator.</p>
<pre class="code">
- Memoirs Found in a Bathtub
- Snow Crash
- Ghost World
</pre>
<p><b>Mapping</b><br />
A mapping represents a key: value pair. </p>
<pre class="code">
Stanislaw Lem: Memoirs Found in a Bathtub
Neal Stephenson: Snowcrash
Daniel Clowes: Ghost World
</pre>
<p><b>Tag</b><br />
YAML represents type information of native data structures with a simple identifier, called a tag (<strong>!</strong>). The tag serves to restrict the set of possible values the content can have. In this example from JYaml the tag specifies from which type of class the node is.</p>
<pre class="code">
<strong>!</strong>my.app.yaml.model.Book
</pre>
<p><b>Anchors and Aliases</b><br />
In the representation graph, a node may appear in more than one collection. An anchor is denoted by the <strong>“&#038;</strong>” indicator and an alias node by the “<strong>*</strong>” indicator. The alias refers to the most recent preceding node  having the same anchor. </p>
<pre class="code">
- !my.app.yaml.model.Book
  authors:
    - <strong>&#038;3 </strong>!my.app.yaml.model.Author
      firstName: Gary
      lastName: Michaels
- !my.app.yaml.model.Book
  authors:
    - !my.app.yaml.model.Author
      firstName: Bob
      lastName: Jackson
    - <strong>*3</strong>
</pre>
<p>You find more detail about the YAML standard in the <a href="http://yaml.org/spec/1.2/">YAML specification</a>. </p>
<p><b>Note</b>: Some of the examples are from the homepage <a href="http://yaml.kwiki.org/?YamlInFiveMinutes">Yaml In Five Minutes</a>.</p>
<h3>JYaml</h3>
<p>When you want to serialize and deserialize Java classes with JYaml then all these classes have to obey the following Java beans naming convention.</p>
<ul>
<li>The class must have a public default constructor.</li>
<li>Set/get methods for properties</li>
</ul>
<p>JYaml currently supports the serialization and deserialization of the following types of Java objects:</p>
<ul>
<li>Primitives and respective wrapper classes</li>
<li>Collection (List and Set)</li>
<li>Maps</li>
<li>Arrays</li>
<li>BigInteger and BigDecimal</li>
<li>Date</li>
<li>Custom Java Objects by implementing ObjectsWrappers yourself</li>
</ul>
<p>JYaml is also in the maven repository. You have just to add the following lines in your <strong>pom.xml</strong> file.</p>
<pre class="code">&lt;dependency&gt;
 &lt;groupId&gt;org.jyaml&lt;/groupId&gt;
 &lt;artifactId&gt;jyaml&lt;/artifactId&gt;
 &lt;version&gt;1.3&lt;/version&gt;
&lt;/dependency&gt;</pre>
<h3>Data structure</h3>
<p>So we want to serialize and deserialize our data structure which are books and authors. The following line of codes shows you the simple data structure as Java code.</p>
<pre class="code">public class Book {

	private String isbn;
	private String title;
	private int pages;	

	private List&lt;Author&gt; authors = new ArrayList&lt;Author&gt;();

	public String getIsbn() {
		return isbn;
	}

	public void setIsbn(String isbn) {
		this.isbn = isbn;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public int getPages() {
		return pages;

	}

	public void setPages(int pages) {
		this.pages = pages;
	}

	public List&lt;Author&gt; getAuthors() {
		return authors;
	}

	public void setAuthors(List&lt;Author&gt; authors) {
		this.authors = authors;
	}
}

public class Author {

	private String firstName;
	private String lastName;

	private Date dateOfBirth;

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public Date getDateOfBirth() {
		return dateOfBirth;
	}

	public void setDateOfBirth(Date dateOfBirth) {
		this.dateOfBirth = dateOfBirth;
	}
}
</pre>
<h3>Serialize</h3>
<p>The serialize step is quite simple all work is done with the static methods from the <b>org.ho.yaml.Yaml</b> class.</p>
<pre class="code">List&lt;Book&gt; books = new ArrayList&lt;Book&gt;();

Book book1 = new Book();
book1.setIsbn("123-x");
book1.setTitle("YAML in Action");
book1.setPages(330);

Author author1 = new Author();
author1.setDateOfBirth(new Date());
author1.setFirstName("Gary");
author1.setLastName("Michaels");

book1.getAuthors().add(author1);

books.add(book1);

Book book2 = new Book();
book2.setIsbn("897-x");
book2.setTitle("JYAML for dummies");
book2.setPages(230);

Author author2 = new Author();

author2.setDateOfBirth(new Date());
author2.setFirstName("Bob");
author2.setLastName("Jackson");
book2.getAuthors().add(author2);
book2.getAuthors().add(author1);

books.add(book2);

// write to file
<strong>Yaml.dump(books, new File("books.yml"));</strong>
</pre>
<p>The output after the serialization process looks as follow:</p>
<pre class="code">---
- !my.app.yaml.model.Book
  authors:
    - &amp;3 !my.app.yaml.model.Author
      dateOfBirth: !java.util.Date "1234212351636"
      firstName: Gary
      lastName: Michaels
  isbn: 123-x
  pages: 330
  title: YAML in Action
- !my.app.yaml.model.Book
  authors:
    - !my.app.yaml.model.Author
      dateOfBirth: !java.util.Date "1234212351636"
      firstName: Bob
      lastName: Jackson
    - *3
  isbn: 897-x
  pages: 230
  title: JYAML for dummies
</pre>
<h3>Deserialize</h3>
<p>To obtain the Java objects from a YAML file you have just to add the following line of code:</p>
<pre class="code">List&lt;Book&gt; out = Yaml.loadType(new File("books.yml"), ArrayList.class);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.rueedlinger.ch/2009/02/jyaml-yaml-for-java/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
