miércoles, 18 de julio de 2012

Run Single Test in Maven

If you guys read the reason I started this blog, you'll recall  that it was just to summarize stupid stuff one always forget when coding.
And so here is one of them, how to run just a single test from the command line with maven.
Well let me just C&P the data from the Apache Maven site


To run this through Maven, set the test property to a specific test case.
mvn -Dtest=TestCircle test
The value for the test parameter is the name of the test class (without the extension; we'll strip off the extension if you accidentally provide one).
You may also use patterns to run a number of tests:
mvn -Dtest=TestCi*le test
And you may use multiple names/patterns, separated by commas:
mvn -Dtest=TestSquare,TestCi*le test
I know I know this has not actual merit but hey think of this blog as place were you can find this kind of things because, except from one or two original things this is what you'll find.

martes, 17 de julio de 2012

Stream of bytes

Today's post is a short one.

So I was playing with streams, and by playing I mean I had to deal with them at work :P

Any way, I do know that unless you are absolutely sure about the content of the stream you should treat them as bytes always, but I made the mistake to handle them as string.
Any way that lead me to find Apache Commons IO, in particular IOUtils.

As it turns out I needed to read all the content from an HTTPInputStream and then returned as byte array.
For those of you who don't see the problem here, the thing is that it's a pain to read something of an input stream at place it in an array. Just to much code for something so simple. Even more if it's a byte array.

Any way a good friend of mine introduce me to this portion of the Apache common library.
(Do take a look at his blog it's just awesome)

And as a result here are the two lines of code that you'll need to do just that i.e. read from an input stream and place the content in a byte array:


import org.apache.commons.io.IOUtils;

return IOUtils.toByteArray((InputStream) yourObjecStream.getStream());


So that's it, please note the casting of the yourObjectStream is just a copy paste hehe.
Hope you enjoy it.


jueves, 12 de julio de 2012

XML & JDom

Hi there!!!!
So todays subject is XML parsing.
Right, you'll say this is a common thing, who didn't work with XMLs.

And you are right, but, as it turns out my line of work didn't usually take me to work with xml files.

Thing is I needed to take an XML string and just add it a new node.
This example it's quite simple except in my case I needed to use name spaces, for the xml file used them.

So with out further due here it is:


SAXBuilder builder = new SAXBuilder();

Document document;

document = builder.build(new ByteArrayInputStream(xmlMessage.getBytes()));


Namespace atomNS = Namespace.getNamespace("atom", "http://www.w3.org/2005/Atom");
Namespace cmisraNS = Namespace.getNamespace("cmisra", "http://docs.oasis-open.org/ns/cmis/restatom/200908/");
Namespace cmisNS = Namespace.getNamespace("cmis", "http://docs.oasis-open.org/ns/cmis/core/200908/");

Element newElement = new Element("propertyString").setAttribute("propertyDefinitionId", "aNameSpace:uuid");
newElement.setNamespace(cmisNS);


Element newValue = new Element("value").setNamespace(cmisNS).addContent(uuid);
newElement.addContent(newValue);

Element rootElement = document.getRootElement();
rootElement.getChild("object", cmisraNS).getChild("properties", cmisNS).addContent(newElement);



As you can see there is no much new here, even more if you compare it to this site that I used as a starting point.
The only difference is that as my XML has namespaces you must use them in order to JDom parse the document correctly. This is, to get the actual node you are looking for.

In the same way when you create your new elements/nodes you must specify the name space that should be used.

martes, 3 de julio de 2012

EasyMock & Matchers

Hi there!

If you are down this rode chances are you were looking for something related to EasyMock right??
In short (for those who don't know) EasyMock is a mocking framework, makes sense ah?

I suggest you check mocking in Wikipedia, you are lost at this point.

For the rest of you here is the actual point of this post.
Haven you ever find your self mocking a method call where you know ALMOST ALL the values of the parameters that the method would be receiving. Please note that I set almost.

Well thing is, for those values we don't know Easy Mock provide us with matchers. It is a way to say "hey you're going to be receiving, let's say, a String".
Don't really know which one, don't know the content of it and I JUST DON'T CARE.

Here is how you can do that:

The method firm: public void myMetod(String firstStr, String secondStr)


Easy mock call: EasyMock.expects(obj.myMethod("", EasyMock.anyObject(String.class)); 

Now this makes sense right!!!!
BUT IT DOESN'T

You see, although I'm not laying to you, and Easy Mock does allow you to do such things the above it's not the correct way.
EasyMock allow you to use matchers in your method call BUT
Either all the parameters are passed through Matchers or not. You can't have mixed things as before.

So here are two valid calls:

EasyMock.expects(obj.myMethod(EasyMock.anyObject(String.class), EasyMock.anyObject(String.class)); EasyMock.expects(obj.myMethod("", ""); 
Now all this brings us to the main problem:

What if I know some of the value an I don't know some of them. More important I don't know some of the values but at least one must be a particular value.

Well Easy Mock does also provide you with a way to do that, by the use of the following matcher:

EasyMock.eq("");

So the invalid call above should turn into this:

EasyMock.expects(obj.myMethod(EasyMock.eq(""), EasyMock.anyObject(String.class)); 

In this way you can keep on mocking.

For those of you that may be googling this article the exception thrown when doing this is:


java.lang.IllegalStateException: 2 matchers expected, 1 recorded.

Any way, I hope you have enjoy this!
Please also check this article that help me to find out this