Saturday, April 17, 2010

Szczecin JUG Meeting. Filip "Filus" Pająk on TestNG.

On 22nd of April 2010 Filip "Filus" Pająk will present TestNG. If you are around Szczecin, feel invited. It's free :)

Filus is experienced Java programmer and tester. When he does something, you can be sure it's going to be good. I think he'll make great presentation.

I am very glad Szczecin JUG is organising another meeting. It's been some time since the last one.

Maybe I should also present something? JavaFX? Scala?

Monday, March 22, 2010

JavaScript training

This weekend I was able to take part in JavaScript training by Damian Wielgosik . As I was not very experienced JavaScript developer, I have learned a lot. I also got convinced that in JavaScript everything is possible :)

I am impressed by the trainer. I was quite surprised when I found out that it was the third training he conducted. I thought he is much more experienced. He had great "contact with the audience", atmosphere was very friendly, nobody was afraid to ask questions or discuss. Thank you Damian!

Two days full of training is quite a much time, so there was also place for exercises. This was very good, nothing consolidates knowledge like some exercises and because Damian was always there to help if we were stuck, we were not wasting time. Doing exercises in groups was also very good idea. We worked as a team and helped each other. I never doubted in efficiency of pair programming :)

Scope of topics was impressive. I learned a lot about prototypes and constructors, which was a bit strange for Java programmer (but not so much if he was also learning Ruby ;)) I think we covered pretty much everything in JS, from basics (which we run through quite fast) to optimization.

I would also like to thank other participants for great atmosphere, discussions, questions, help and info about "pierogarnia" :) I wish I could attend more such good trainings.

Friday, March 19, 2010

Review: Programming Scala, by Venkat Subramaniam

Programming Scala: Tackle Multi-Core Complexity on the Java Virtual Machine (Pragmatic Programmers)Courtesy of The Pragmatic Bookshelf I was able to read "Programming Scala" by Venkat Subramaniam. If you are a Java programmer, this book is a great introduction to Scala for you. It is packed with knowledge. There was not even one chapter after reading which I could say "well, this was quite obvious, I could expect this". Book is targeted for experienced Java programmers, so author does not cover things we already know. This was great for me, my time was not wasted learning what I don't need. Instead, I was led from easier topics in Scala (like classes, typing, closures) to more difficult ones (pattern matching, actors and concurrency) at a nice pace. Most topics are explained quite clearly and understandable. There are few which I had difficulties understanding and I needed to fiddle with some code to get it. But fiddling with code is fun for programmer, isn't it? One of such topics were actors, which is a great feature in Scala and something completely new for Java programmer (unless he knows Erlang too ;)). It took me some time with IDE to understand actors, but it was also fun.

The book not only teaches how to program in Scala, but also shows tools to run these programs. Knowing that Scala doesn't necessarly need to be compiled to run and that it can be run as a script can be very useful if you need to do some simple job on your system. Author shows how to use it as a scripting language. I also liked the last chapter in which author creates Scala application using various features covered in the book. It was a good way to sum up.

The only weak point of this book are examples. Especially at the end of the book they become more and more difficult and not always clearly explained. Another disadvantage of examples is their little connection with reality. Have you ever need to count letters in blog URL? In some cases they even show bad practice like throwing general Exception. I know it's only example, but educational book should not promote bad practice even there.

Said all this, I would recommend this book to every Java programmer who wants to learn Scala. If you are not afraid to fiddle a little with code (and you should not be, you are a programmer after all;) ) it is a very good book for you and you are it's perfect target.

Last but not least, not only author, but also editors deserve praise. I have PDF edition and I can login on pragprog.com page anytime and download it. I don't have to worry that if I loose it - it's lost for good. Moreover there are .mobi and .epub formats available. I could read the book on my phone on a bus or a tram which was very convenient. And there is even more - the books are DRM free, which shows publishers not only care for readers comfort, but also trust us. I appreciate that.

Tuesday, March 2, 2010

Simple AJAX with JSF 2.0

In JSF 2.0, there are tags added to make AJAX calls easy. Without further ado:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
 xmlns:ui="http://java.sun.com/jsf/facelets"
 xmlns:h="http://java.sun.com/jsf/html"
 xmlns:f="http://java.sun.com/jsf/core">
<h:head>
   <h:outputScript name="jsf.js" library="javax.faces" target="body"></h:outputScript>
   <title>Ajax</title>
</h:head>
<h:body>
   <f:view contentType="text/html; charset=UTF-8">
       <h1>Hello Ajax World!</h1>
       <h:form id="form">
           <h:inputText id="name" value="#{nameMBean.name}"/>

           <h:commandButton value="Reverse name via Ajax!">
               <f:ajax execute="name" render="reverseName"/>
           </h:commandButton>
           <h:outputText id="reverseName" value="#{nameMBean.reverseName}"/>
       </h:form>
   </f:view>
</h:body>
</html>

First things first. To use AJAX in JSF 2.0, we have to attach jsf.js file (line 8). Then we are ready.

Most interesting is line 18. <f:ajax> added to command button makes AJAX action fire when button is pressed. We could as well put it into <h:inputText> element, then it would fire on change of text inside (onchange event).

Parameter execute tells JSF which parts it should send to server for model update. This could be also form, for all the parameters in the form, or list of element ids separated by space. Parameter render denotes which parts of view should be rerendered after receiving AJAX response. Here we can also put more than one.

We can put more parameters to <f:ajax>, like event, where we can specify on which event AJAX action should occur (not necessarly click event), or listener, where we can call some method on managed bean.

Thursday, February 11, 2010

Make Ubuntu faster

There are few very simple methods to make Ubuntu faster. Some of them work not only on Ubuntu, but on other linux systems as well.

Change dynamic linking to static one

When compiler generates binary file, elements which should connect with external libraries are only stubs. When running application for the first time (in a session), it is dynamically linked, which means stubs are replaced with real calls to libraries. This process takes some time. Open Office on my machine runs about 5 seconds for the first time, and 1-2 seconds every next time.

Fortunately there is a program called prelink, accessible from Ubuntu repositories.

sudo apt-get install prelink
sudo prelink -amR

After the second command prelink scans binaries specified in /etc/prelink.conf and modifies them replacing dynamic calls with static ones, but in binary files, so this change is permanent. Disadvantage of this solution is that after every update of processed program we need to run prelink again.

We can always go back to dynamic linking (undo prelink):

sudo prelink -au

You HAVE TO go back to dynamic linking if you want to remove prelink.

Get now what I'll need in a few miliseconds

Next useful program fastening Ubuntu is preloader. It uses magic (complicated algorithms) to predict what data is going to be necessary next and loads it before application asks.

You only need to install it:

sudo apt-get install preload

It should run automatically, but if for some reason it is not running:

sudo /etc/init.d/preload start

Koziołek says it consumes a lot of RAM, but I didn't notice :)

Run services in paralell

If you have more than one core, you can set your system to start services in paralell during startup. This is potentially dangerous, as there might be services which depend on each other.

Edit /etc/init.d/rc and find line CONCURRENCY=none. Change it to CONCURRENCY=startpar.

This post is here courtesy of Bartłomiej "Koziołek" Kuczyński, and is translation of his post in Polish.

Tuesday, January 12, 2010

Partial mocks in Mockito - Mock only what you need, left the rest to the original class

In Mockito you can not only create "regular" mocks, but also partial mocks. Let's assume we need to use instance of class A, and we want to mock it. We can do mock:
A aMock = Mockito.mock(A.class);
Sometimes, we want to use instance of real class A, but mock only part of it. Only one or few methods. With Mockito it is possible:
A a = Mockito.spy(new A());
So we spy real object, we can verify it's method calls, but we can also do that:
Mockito.when(a.methodCall()).thenReturn(1);
We can mock some of A's methods, leaving the rest to the real A instance.
For me it was useful while I was creating test involving user. User object's have ID, but there is only getter for it. ID is always set by database, and should never been changed by programmers, so there is only getter. Thanks to partial mocks, I mocked only getId() method, without need to add setId() just for testing purposes.

Monday, December 14, 2009

CouchDB

CouchDB is a new kind of database. It's not a relational database, it's not objective database. CouchDB just stores documents. A document is something like java map, it has keys and values. Values can be strings, numbers, dates, lists, maps, also binary attachments. Documents are accessible by pure http, REST service, in JSON format. CouchDB is written in Erlang, so it is well scalable for many processors/cores. (At least this is what it's authors claim ;)) Let's give it a try.

Installation

On ubuntu issue:

apt-get install couchdb

During installation, couchdb user was created. Now you can run database as a background process:

sudo -i -u couchdb couchdb -b

Ok, fire up your favorite browser and open page http://localhost:5984/ You should see something like: {"couchdb":"Welcome","version":"0.10.0"}

Not very useful, is it? Try going to http://localhost:5984/_utils/ This is Futon, CouchDB admin tool. From here you can create new databases and documents and manage them.

Simple operations by Futon

Create database tryme and some document. Field _id is generated automatically, add two more fields:

firstname: "Pawel"
surname: "Stawicki"

Click "Save document". You can see that field _rev was added automatically. Every change of every document is remembered, and you can always get the old version.

Protection

So there is one document in the database, let's protect it. Go to /etc/couchdb/local.ini and edit [admins] section. Add:

admin = <admin_password>

Password will be automatically hashed after run.

If you want to enable access from any machine, change bind_address from 127.0.0.1 to 0.0.0.0

Operations by RESTful HTTP interface

CouchDB can be accessed by RESTful http interface. Curl is one nice tool for it, so install it if you don't have it already on your system:

sudo apt-get curl

Now issue:

curl http://localhost:5984

You should see known welcome message. Try

curl http://localhost:5984/tryme

and some data about tryme database should appear. We can also list all existing databases:

curl http://localhost:5984/_all_dbs

Ok, all this stuff above you can do by the browser. But you can also create documents and databases in CouchDB by REST interface. Try this:

curl -X PUT http://localhost:5984/tryme/1 -d '{ "firstname": "Leszek", "surname": "Gruchała" }'

You just created a new document. In curly braces is document in JSON format. It's id however is not long stream of characters, but just "1". Much more predictable and repeatable. UUIDs are generally better. CouchDB can generate one for you:

curl http://leonidas:5984/_uuids
It generates one id. If you need more, you can pass "count" parameter:
curl http://leonidas:5984/_uuids?count=3

So we've seen that CouchDB uses JSON as documents format, and we can do operations by HTTP. However, from now on, I'll be using Futon because it is just easier. Futon is CouchDB administration tool (http://localhost:5984/_utils).

Creating view by map/reduce

JSON is JavaScript format, and it allows to put also functions into the document. Map functions (from map/reduce) are useful to create something similar to SQL view.

Let's add another document to our database, with following fields:

firstname: "Leszek"
surname: "Kowalski"

Now choose "Temporary view" from selection box on the right. Here you can write map and reduce functions. For view, map is enough, let's leave reduce empty for now. Create function:

function(doc) {
  if (doc.firstname == 'Leszek') {
    emit(doc.firstname, doc.surname);
  }
}

It filters documents to those with firstname "Leszek" only.

So we can filter documents to only those which interest us. What about joins? Can we get some document, and relevant documents data in one query? It's also possible. Let's add addresses to those guys in database. First, we need to distinguish documents related to persons from the ones related to addresses. For that, add type field to each person, and as value enter "person". Now, we'll need their id's, so copy/paste it somewhere.

Create address as a new document, and add those fields:

type: "address"
person_id: <id_of_person>
city: "New York"

The same for two remaining persons. Put some another city there. Ok, now our map function is a little more complicated:

function(doc) {
  if (doc.type == "person") {
    emit([doc._id, 0], doc);
  } else if (doc.type == "address") {
    emit([doc.person_id, 1], doc);
  }
}

This way we have person always next to her address.

We learn another CouchDB feature here - result of map is always sorted by key. In this case key is 2-element array, in which first element is the person id, and second one is 0 in case of person, 1 in case of her address. It's not like SQL join, we don't get all the data in one document, but still it answers our needs - we have person and it's address.

If we are interested in specific person, we can add parameter to GET request. But first we need to save our view in "desing document". Click "Save As..." button, fill in design document name (e.g. "reader") and view name (e.g. "person-address") and voila.

Design documents are special documents in database for storing views. You can have different design documents e.g. for readers and for writers or administrators. In desing document you can set a lot of stuff telling CouchDB how to render it's content (means how to change it into nice html). If you want to learn more about design documents, look into CouchDB book.

GET parameters for narrowing search

Now when our view is saved, we can use GET request to get it's content, and set parameters to limit what we get to person which we are interested in. Such URL: http://localhost:5984/tryme/_design/reader/_view/person-address?startkey=["1",0]&endkey=["1",1] will return person with _id=1 and it's address documents.

Another useful parameters are:

  • key - for specific key (not range, like in case of startkey, endkey)
  • descending=true - by default view is ordered by ascending key, set this parameter to order it descending
  • group_level - sets reduce level. Default is 0.
  • group=true - behaves like group_level set to maximum
  • revs_info=true - lists revisions of specified document. Applicable only to document selected by id, not to view

To understand group_level, we'll need reduce function. First, let's add some more parameters to our persons. Let's add "position" and "salary":

To one person add:

position: "manager"
salary: 5000
To second one:
position: "developer"
salary: 3000
and to third one:
position: "developer"
salary: 3500
Now create new view, with map and reduce functions:
//map:
function(doc) {
  emit([doc.position, doc._id], doc);
}

//reduce:
function(keys, values, rereduce) {
  var salaries = 0;
  for(i = 0; i < values.length; i++) {
    salaries += values[i].salary;
  }
  return salaries;
}

Save this view as "salaries". Now open URL http://leonidas:5984/tryme/_design/bla/_view/salaries and output is:

    {"key":null,"value":11500}

All values are grouped, and key is ignored.

Add parameter: http://leonidas:5984/tryme/_design/bla/_view/salaries?group_level=1 and it returns

{"key":["developer"],"value":6500},
{"key":["manager"],"value":5000}

Values are grouped for first level of key (first element of key array).

group_level=2 returns
{"key":["developer","61fe9c6c226b978f74b76329191806b3"],"value":3000},
{"key":["developer","eb3873f48bb581df13762324b8ec0313"],"value":3500},
{"key":["manager","1"],"value":5000}

Two elements of array are taken into account. In this case, it is the same like group=true, which takes all elements of key array.

What is rereduce for?

As you noticed, reduce function has third parameter rereduce

Result of map function is kept as sorted B-tree. Let's assume we want to summarize some value from each document (tree node). Just like salaries.

First, there are keys and whole nodes (because value of our map function is whole document) passed to reduce function, and rereduce is set to false

Look at the picture, we can reduce branch of the tree into 17, another branch into 4, and third one into 2. Assume each branch has common key at specified group_level (e.g. all people from first branch are developers, second are managers and third are administrators, if group_level is set to 1).

Then reduce function is called again, with following parameters:

function(["developer", "manager", "administrator"], [17, 4, 2], true);

There is common key for whole branch, reduced scalar value for that branch, and rereduce parameter set to true.

Important thing here is to remember that not always values are whole nodes, sometimes it can be already reduced values, and in such case rereduce parameter is helpful to distinguish this situation. In fact, our reduce function wouldn't work for big amount of nodes, because it handles only nodes (only rereduce=false case), not reduced values. To work with a lot of data, we should handle also scalar values:

function(keys, values, rereduce) {
  if (rereduce) {
    return sum(values);
  } else {
    var salaries = 0;
    for(i = 0; i < values.length; i++) {
  }
}

Now it should count and sum all the salaries.