TDD – Test Driven D???

Few weeks after the great AgileByExample conference, I had the privilege to be an organizer of, I have few thoughts about TDD I would like to share.

If you are an experienced TDD hacker, then probably this would be no value for you, but If you are a beginner then you might enjoy it ;-)

The conference was really an eye opener for me. Basically I had an idea what TDD could be. I went to many presentations in the past that were trying to convince the audience, how great and awesome TDD is, and how it can cure cancer, but I think the presenters were very often missing the point there.

The point that I think I feel much better now, after taking part in a great Coding Dojo, that was lead by two great agile masters – Alexandru Bolboacă and Thomas Sundberg.

So the thing is – I always thought about TDD to be the Test Driven Development. But that is not all. That always implied for me, that I need to design first, and then do the TDD. And that really did not make much sense, cause If I knew that I need to create class Dog class Cat class DogDecorator class ViewPetHolder etc. – why would I start with test that checks that method which returns “Dog” really returns “Dog” ;-)

But if you think Test Driven Design, which I think is more accurate, you will find out, that you shouldn’t be thinking about how to achieve the goal – the actual solution will emerge from the tests.

I have seen it just yesterday when I remote paired with Alex, to practice a bit and see with my own eyes, that remote pair programming is possible.

Two things came out of it – Alex showed me how refactoring, like changing the names of variables to something more descriptive naturally creates the classes for you.

And that remote pair programming is as possible as one-room-pair-programming. We had absolutely no problems in doing that – one thing you need to do is that everyone writes on his own laptop, while the other is looking at it (try TeamViewer for example). Once one is done with his stuff the other needs to get his changes, for which any version control would be sufficient. We were using public github account.

I might not be 100% converted, but for some problems I can see a good use of this approach.

Oh and the shared code knowledge, and double the brain you get for free ;-)

New Toy – MacBookAir

Since I am taking a new, rather managing position quite soon, my company got me a brand new MacBook Air.

I’ve been using it for few days now, and wanted to share what do I think.

To start with, the specs (this is the simplest version):

  • 11.6″ LED 1366×768 display
  • 1.6Ghz dual-core Intel Core i5
  • 2GB 1337 DDR3 RAM
  • Intel HD 3000 (shared 256MB)
  • 64GB SSD drive
  • 1.08kg

More specs can be found on apple.com.

The tests

Speed

What I wanted to test is the compile time using maven of a big big project I am currently working on plus jboss (6) startup time.

I compared with my MacBook Pro I use everyday. It’s specs are:

  • 2.4Ghz C2D
  • 250GB 7200rpm HDD
  • 8GB 1067 DDR3 RAM

Results:

Maven

Repositories downloaded prior, second mvn clean install with all tests

MacBookPro : Total time: 3:59.889s

MacBookAir: Total time: 2:29.097s

JBoss Startup

note: on MBA had to decrease Xmx and Xms from 1.5GB to 1GB and permgen from 768MB to 512MB

MacBookPro: Started in 2m:843ms

MacBookAir: Started in 1m:27s:894ms

Huh ?

Yep, MBA is almost 50% faster. Why ? I think it’s the SSD (plus maybe new generation of the CPU) – the tests heavily use the filesystem for the DB tests.

Battery

I thought – I want to simulate the full load, so what I did is that I played a full HD (1080p) MKV movie, streamed from a NAS until the battery went totally flat. After turning it up I was able to tell exactly how many minutes of the movie were played (I have it in 2 parts, set to reply all in VLC).

So the wifi was up and running all the time, CPU was decoding full hd.

Result: 3 hours and 14 minutes. Pretty awesome. You can watch full blown movie using a single charge. If it was just writing docs, the apple’s up-to-5hrs might be actually true.

Overall

It is great. Not only it “feels” faster then my old MacBook Pro on boot, when opening apps etc. It *is* faster for doing what I do the most – java dev. I just need to upgrade the RAM :)

At the same time it is super-portable, super-sexy and runs my favorite Mac OS X (Lion). And the best is – you can get it for around 3000 PLN + VAT. Finally a fair price for a mac. I don’t think you can get much better alternative with windows for that kind of money and with that kind of portability.

Talk on cloudyfing your apps with Amazon

The talk I’ve given with Adam Warski during Confitura 2011 conference is available on vimeo.

It is in polish, so might be not for everyone ;-) You might want to watch it in HD in full screen – you’ll be able to see the slides right.

Confitura 2011 – “Rozchmurz swoje aplikacje” from Confitura Conference on Vimeo.

Below also the slides.

We got very nice feedback from the viewers plus quite high marks (5 is best 1 is worst):

5: 24
4: 37
3: 13
2: 2
1: 2
Average: 4.01

Thank you !

Enojoy !

Conditionally skip JSF validation but perform model updates

The Problem

If you are developing forms in JSF2 that are using AJAX to show or hide some parts of the form for the best user experience, you surely got into a situation where your ajax call, that was supposed to rerender something, wouldn’t work, because at the same time you wanted to use some JSF validators, or you’ve marked your field as required.

And probably you got there more then once.

If you are not sure what am I talking about let me give you an example.

The Example

The client I am working for at the moment had a need to have:

A Person who might have some Partners

class Person {
   List<Partner> partners;
}
class Partner {
   String name;
}

And a form, that:

  • has an input for number of Partners a Person has
  • after choosing it the form should generate N number of input fields to capture each Partner’s name

My first take was to have:

  • rich:inputNumberSpinner, so the user can set the number of partners upfront
  • attach f:ajax “onchange” that would update the number of Partners in the list on Person
  • and rerender the panel that would ui:repeat through it showing an input box per each Partner

The view:

<h:panelGroup id="partners">

<rich:inputNumberSpinner value="#{bean.numberOfPartners}">
   <f:ajax execute="partners" render="partners"/>
</rich:inputNumberSpinner>
<ui:repeat value="#{bean.person.partners}" var="partner">
   Partner's name:
   <h:inputText value="#{partner.name}" required="true"/>
</ui:repeat>

</h:panelGroup>

<h:commandButton action="#{bean.save"}/>

And the backing bean:

@Named("bean")
@ViewScoped
public class PersonBean {
   private Integer numberOfPartners;
   private Person person;

   //ADD numberOfPartners getter

   public void setNumberOfPartners(Integer numberOfPartners) {
      this.numberOfPartners = numberOfPartners;

      // if number less then current size - remove
      while (person.partners.size() > numberOfPartners) {
         person.getPartners().remove(person.getPartners().size() - 1);
      }

      // otherwise add
      while (person.getPartners().size() < numberOfRelatedEntities) {
         person.getPartners().add(new Partner());
      }
   }

   public String save() {
      // persisting logic
   }
}

But this doesn’t work. Why ? The name on the partners is required. So now imagine such a scenario – you’ve chosen 7 partners upfront, started filling them in, but then realized you had only six.

So you try to change the number of partners to 6 but what happens is that the system throws validation error, that the name of the 7th partner is required, and you cannot submit.

What is wrong ? AJAX request, as any other request not only updates the model, but performs the validation. The validation does not make any sense in this case, because we are not saving anything, we just want to update form basing on some conditions.

So I thought it would be cool if I could skip the validation on that particular request. Or actually on any kind of request, but the one when the user finally clicks on SUBMIT to persist the updated Person.

Google had nothing for me.

OnSubmitValidator

So I decided to write something of my own.

I figured that I can write a wrapper validator that could also perform required=”true” like checks but conditionally – only when some parameter is passed together with the submit.

And that’s how OnSubmitValidator from softwaremill-common was created. It’s opensource on apache license – do whatever you want with it.

So now the xhtml looks as follows

<h:panelGroup id="partners">

<rich:inputNumberSpinner value="#{bean.numberOfPartners}">
   <f:ajax execute="partners" render="partners"/>
</rich:inputNumberSpinner>
<ui:repeat value="#{bean.person.partners}" var="partner">
   Partner's name:
   <h:inputText value="#{partner.name}">
      <f:validator validatorId="onSubmitValidator"/>
      <f:attribute name="onSubmitRequired" value="true"/>
   </h:inputText>
</ui:repeat>

</h:panelGroup>

<h:commandButton action="#{bean.save"}>
   <f:param name="performValidation" value="true"/>
</h:commandButton>

Notice two changes:

  • The inputText doesn’t have required=”true” anymore, but uses the new validator with attribute onSubmitRequired set to true (lines 9-10)
  • The saving commmand button has a parameter performValidation which tells the validator that this is the moment to perform the validation (line 17)

If you wanted to use also special validator inside, you could’ve specified it with

<f:attribute name="validatorId" value="VALIDATOR_ID"/>

To sum it up…

So out of the box you get a conditional required=”true” and custom validator’s validation.

A step further would be to somehow wrap also the standard validators like f:validateRange or such.

Or a step even further to hijack javax.validation annotation validation – with @NotNull etc.

If there’s a need for that, I’m gonna think about it. Maybe you have some ideas ?

A different take on Object Services

Back from vacations

… wait – we have vacations now ;-)

It’s been a while since my last post. Finally I have decided to give the blog a new breath.

From now on, I will try rather not to enlighten the world with my Great Thoughts, but rather have a little developer journal (or kitchen journal, whatever am I up to ;-) ).

But let’s get to the post’s topic – Object Services

Before anything if you are not familiar with CDI please check out CDI’s reference implementation Weld.

A while ago Adam proposed a new take on dependency injection with injection that bases on the serviced objects.

While his implementation is very nice I always thought that it must be implementable in maybe an easier way. The thing I wanted to overcome the most was the need to inject a provider object. I thought – with proxies and generics it must be possible to call the service directly.

The idea

Hard to believe you’re not familiar with Adam’s blog, but let me explain what do we want to achieve ;-)

We have a PaintService that we want to paint different animals with.

We can, of course, do many instanceof checks and then look for an appropriate service (using Factory for example), but with CDI we want the container to do it for us automatically.

So if we call paint(elephant, canvas) we would like to get the appropriate service that is capable of painting elephants and invoke the paint method on it.

My way of doing things

So here is my idea using the example in Adam’s blog:

Prerequisites – objects:

abstract class Animal
class Elephant extends Animal
class Ant extends Animal

Let’s start with defining our service’s interface.

@OS
public interface PaintService<T extends Animal> {

    void paint(T animal, Canvas c);
}

We have an interface PaintService, that is parametrized and provides a single method with the parameter object.

Three things are required

  1. The @OS annotation that marks the Object Service interface
  2. The interface must be parametrized – in the current implementation it has to be one and only one parameter
  3. All methods need to use the parametrized type
With the interface ready let’s create the implementations
@OSImpl
public class ElephantPaintService implements PaintService<Elephant>{
    @Override
    public void paint(Elephant animal, Canvas c) {
        // do some magic with the elephant and canvas
    }
}
@OSImpl
public class AntPaintService implements PaintService<Ant>{
    public void paint(Ant animal, Canvas c) {
        // do some magic with the ant and canvas
    }
}
Now let’s use it !

public class MyCoolBean {
    @Inject
    private PaintService paint;

    public void ourLogic(Animal a, Canvas c) {
        // do something and finally paint the animal
        paint.paint(a, c);
    }
}

If you ever need to inject your service directly, the @OSImpl annotation is a qualifier

@Inject @OSImpl
private ElephantPaintService elephantPaintService;

The code should also work nicely with Scopes and of course supports inheritance. In the above example we can pass BlackAnt extends And or RedAnt extends Ant, and the AntPaintService will be executed.

The pros and cons

  • Execution of the service is more elegant
  • … but the serviced object has to be included in every method

What can be done ?

  • Currently only one parametrized type can be used, but the implementation can be changed to support more then one.

Play with it yourself !

Everything is available in softwaremill-common/CDI module and also deployed into our Maven repository (starting from version 51-SNAPSHOT). Check out the softwaremill-common README on github for the repository URLs.

And kudos to Adam Warski for the idea.

Update your wordpress…

I got a break in my blog yesterday (changing my Scorpions on Poland ! entry into a list of penis enlargment, viagra etc. links). I think it was a SQL Injection attack done by a robot.

Tip od the day: update your wordpress when the new version comes out.

Thanks to Paweł for spoting it !

Scorpions in Poland !


UPDATE: Thanks to General Electric I know that this weird fella is a Pseudoscorpion or polish Zaleszczotek. Heh! They happen to be useful because they eat pests. Good I didn’t kill it.

Some time ago we had windows (part of the building, not OS) changed in our flat. It was damn cold, so we spent whole day closed in bathroom.

After some time this strange little fellow walked around our wall…

scorpion !

scorpion !

And a movie with this thing walking and waving his tongs !

ANYONE has any idea what the heck is that ?!

JavaPolis 2007 Report…

NOTE: This post has a movie inside and from now on feed with entries should be recognized as a podcast. Do not hesitate to add it to your iTunes.

Finally our team managed to meet. And I’m not talking about meeting on AIM, only-for-oldies IRC or anything virtual. We realized that we’re made of flesh, bones, blood and all those things that a human is made of.

For me it was also first time to meet our not-so-new boss, Bob. He turned out to be a huge, huge guy. Like two meters or so… hard to tell from AIM usually :-) . We had a really good time, lots of laughing, Adam playing with *things* in restaurants, James talking with his nice texan accent, Przemek not talking at all, Rysiek trying to fix some weird JSF stuff, Paweł being a bit to serious and Rebeca (Bob’s wife) trying to tidy this crazy bunch of geeks up.

.ORG in it's glory

We looked like tourists sometimes…

tourists....

There was also Mark Newton, but had to leave ( :-( ) soon and I haven’t even got a chance to shoot him with my camera.

From JBoss side I met few new guys (Emmanuel, Thomas Heute, Stefan Trojanowski who turned out not to be Polish, but Macedonian – yes – they end with -ski as well and Sasha Labourney) and got a chance to talk with well-known Julien about food, especially foix gras, and joke about .ORG <-> Portal stuff as always :-) .

Julien

Antwerp itself was something between Kraków and not-so-big-and-very-ugly medium town in Poland – old town with outstanding Cathedral is very very nice. Outside full of blocks of flats in it’s ugliest way. Just look at those:

Blocks of flats in Antwerp

It’s not what i expect from Western Europe :) . Anyway. The funniest part was when we decided to visit some crazy metal café. There was a huge skeleton hanging, music I don’t usually listen too played, anorectic black, pierced waitress etc. Bob started turning into a vampire…

Bob vampire

So James invented clever Plan B – In Case Of Vampires . We had a really good laugh there :-) . Some of us survived.


Work-wise we made a huge improvement so in all fields the trip was a success.

Oh – and this happened when we tried to buy tickets in Brussels :-)

window$ $ux

What makes you computer scientist (and a good one…)

After almost 2 years of working at JBoss (a division of Red Hat ;-) ) i have few thoughts I’d like to share.

First of all having nice job, especially such nice as JBoss, is very important. And very nice. It’s both money and doing what I like. I wish everyone such a position in life :-)

There is tho a little but. Those are 2 years of a hard work. Combined with studies, now with working on my MSc, it’s something that really makes you exhausted. And not having 3 months of vacations like I used to doesn’t help :) .

The thing is… suddenly (yesterday) something hit me. I’m not so excited about Computer Science like I used to. I mean… I have SJCP, i went for JBoss Advanced training. I use Java every day. I get exceptions but almost always I know why. I solve same problems every day (mostly making Labs work with newer version of JBoss Portal ;-) ). It’s not the same feeling I had when i was in secondary school. Those times I would write completely unneeded stuff just for fun. And it was fun fun.

So what did i do ? I decided to write plugin for Beagle to index messages of Kadu. Beagle is indexing system (something like Google Desktop, but deaply integrated with Linux environment), Kadu is the best Linux Gadu-Gadu client (polish IM system). Believe me how was i surprised when i found out that Beagle is written in Mono (.NET for Linux). OH YEAH ! Finally i found mountain to climb. C# i hardly know, founding out history structure for Kadu (guessing to add three zeros for “Received” and “Sent” time). I felt great. Again The Feeling I forgot. Plugin still doesn’t work, but who cares !

So the conclusion is… in my opinion there is no chance to be a good computer scientist if you don’t have this passion. You have to forget about the labor you do every day. Make yourself a little vacation. Just one day. Write something for fun. Discover some silly computer language out there. Do it for yourself :) . This wont make you good engineer as is, but without it there’s no chance.

And finally to stop the boredom. What is marriage ? I have to pictures:

Just after married:

money money !

Month later:

mud !

What Poland and USA have in common ?

Stupid legislation. And i mean it. Totally ridiculous.

Here’s the link

http://news.yahoo.com/s/nm/20070301/tv_nm/newyork_word_dc_3

And short explanation: New York city council banned use of the N-word. They say it ain’t good to use it in songs, stand up comedies, write it on walls, in your scrapbooks, ask whats up nigga etc etc etc.

Two things:

1) USA claims itself to be the most freedom-of-speech country in the world.

2) What is even better they just banned it. Yes. That’s it. No more acts. No penalties. Nothing.

And that’s where we, polish, have something in common with them. We also like creating laws, that have no administrative acts (one of the many examples is Act of Profession of Psychologists – it just exists, but without anything that would drive it). I mean – guys – we are Tiger of Europe and stuff, but give me a break ;-) .

So what is racism ? Is banning any using of anything going to help ? I don’t think so. People will just laugh. At least I will.

n-word diagram

What does it mean ? That every country is just the same. Every bunch of idiots, that rule it, just create some irrational legislation. It doesn’t matter if you’re a continent-wide country as USA or small one like Poland :-) . And that’s nice.