Saturday, November 17, 2012

How to JUnit-test Envers

Recently, me and some fellow students of mine had to do a homework assignment with Spring and Hibernate. One of the requirements was to include versioning for some of the entities.
At first we thought about programming something on our own by using AOP and an interface. After suggesting this to our professor he told us, that there might be already a framework for versioning that works with Hibernate.
So I found Envers.
Using Envers isn't very difficult. If you want to know more about Envers, you'll have to take a look at its documentation.
My problem was how to write JUnit-Tests for the methods in my DAOs that use the AuditReader class.

Before I start explaining my problem and my solution I just want to tell you, that I'm no Spring developer and it was my first project with Spring (my teammates had no experience with Spring, too).

When persisting some objects in the tests Envers never wrote any data into its revision tables. I soon found out that the @Transactional annotation at my test-class put every method in one transaction (Spring people will know ;) ). At the end of one test-method the commit was performed and Envers didn't react.
So @Transactional had to vanish and the search began.

So when I tried to find a solution I found this article on Dzone but calling commit() on my session or using the TransactionManager lead to some exceptions.
This thread on stackoverflow finally was the solution. Unfortunately the second answer doesn't have any rating, so I ignored it and I had to visit that thread three times to recognize it. (please rate it up if you're registered on stackoverflow)

The superclass for all my Envers tests looks like this: ApplicationContextAwareTest
and in every test it use this method with the TransactionTemplate:
public <T> T makePersistentInTransaction(final T toPersist,
final GenericDAO<T, Long> dao) {
return transactionTemplate.execute(new TransactionCallback<T>() {
@Override
public T doInTransaction(TransactionStatus status) {
return dao.makePersistent(toPersist);
}
});
};

After every call there is a commit and Envers starts working. And after persisting some objects I can tests my methods, which take care of versioning.
Finally everything could be tested and I was happy :D 

Feel free to browse the GitHub project. It contains the whole homework. And please don't wonder that nearly everything is in German ;)

Sunday, July 15, 2012

Sorry for the delay

I know I promised you some information about Ivy and I am really, really late.
But studies and watching the European Soccer Championship consumed lots of time.
And right now, I am back at learning some Python and I gotta admit (again) that

a = []

looks much more elegant than

List<Object> a = new ArrayList<Object>();

Of course the compact list syntax is a feature and Groovy e.g. has the same ability.

And now to the promised Ivy part (it won't be long, because I don't want to duplicate the tutorial, which can be found here):
For me, Ivy is quite straightforward with its ivy.xml and the dependencies in there and the ivy.xml looks like the dependencies in a Maven POM.
Also adding <ivy:retrieve/> to your build.xml shouldn't be too complicated ;)
Having different repositories like Maven is also possible.
And, what I recently found out is, that Gradle and SBT work with Ivy, too.

So, give it a try.

Saturday, June 16, 2012

Getting started with Apache Ivy

Well, no big knowlegde sharing today, I just wanna let you guys know what I am up to.
2 days ago I discovered, that there is something called Apache Ivy. I was quite surprised that I never recognised it before.
Ivy is a dependency manager and it seems to me like a good way to keep your Ant build.xmls and to have the (very comfortable) dependency resolution from Maven.

So, my task for the weekend is to find more out about Ivy. I hope I'll be able to write a little bit more on Sunday.

Tuesday, June 5, 2012

How I can use my time...

I just found a good way how I could use my time a little bit better:
Every day I ride my bicycle 20 minutes to my workplace and 20 minutes back.
During that time I didn't do more than cycling (and taking a look at the landscape ;) ).

Thanks to the book 97 Things Every Programmer Should Know I realised, that I could use my time better:
I started to listen to podcasts on my way.
I am quite sure that I am not the first person who found out that that's possible, nevertheless it just came to my mind a short while ago.

Now I listen to some interesting software engineering podcasts (at the moment Software Engineering Radio and the German heise developer). If you can recommend some more podcasts feel free to leave a comment.

So, try to think about your daily routine and maybe you'll discover (like I did) some time interval you could use better.

Thursday, May 24, 2012

A lot of work with CDI

I didn't write a lot of posts these days, that's because CDI is taking my full attention.
CDI is quite a nice invention for Java EE but despite my first thought, it's not just about injection.
All those
  • Interceptors
  • Decorators
  • Portable Extensions
  • ...
are nice things. It's just that you have to learn about them. But I have to thank Gavin King and the JSR-299 Expert Group for writing a very good and understandable specification. If you want to learn more about CDI and you're tired of all those tutorials on the internet, try to take a look at the specification. You can find it here. They put some really good examples into the first chapters, too.
So, have with injection ;)

Tuesday, May 15, 2012

@Nonbinding

I experiment recently a lot with CDI and one point I stumbled upon were the return values of my annotation functions.
Consider an annotation like this:

@Qualifier
@Retention(...)
@Target(...)
public @interface MyInterface{
 String value();
}

I tried to combine this annotation with a producer method and to use the value in the method (by asking the InjectionPoint for its value). Every time my Eclipse tried to deploy the .war it showed me an error.
After a while I found out that the only way to evade this, is to use the annotation @Nonbindung.
The altered annotation looks like this:

@Qualifier
@Retention(...)
@Target(...)
public @interface MyInterface{
 @Nonbinding
 String value();
}


Maybe I didn't read the spec good enough, but it took me a while to find that out.
So I hope you guys won't have the same problem like me ;)

Thursday, May 3, 2012

new vs. valueOf() in Java primitive wrappers

Did you ever take a close look at the Javadoc which is written at, for example, Integer.valueOf()?

No? Ok, I'll tell you ;)

It says you should prefer using the valueOf() method instead of using Integer's constructor. You'll find the same annotation at all the other wrapper classes for the primitive types in Java. (in Double, the Javadoc says the same, but double doesn't have a cache, weird...)

The reason is that Java will cache some values, so the VM won't create a lot of new objects.
I cannot imagine a certain case, when I explicitly need a new Integer or something similar (maybe in a JUnit-Test). A friend of mine mentioned when it's the key of a WeakHashMap.

So the next time you want to type new Integer(...), consider using Integer.valueOf(...) ;)

Thursday, April 19, 2012

JavaScript IE and Options

First thing I gotta say is, I am not a web developer. Maybe you guys out there all know the problem I had to handle with these days, but for me it was new.
I helped a friend of mine and wrote some JavaScript for him. I dynamically created Options for a select-Tag by using the constructor of the Object:
Option(text, value, defaultSelected, selected)

With FF and Chrome everything was ok. But when my friend tried with his IE it didn't work out.
I knew that there are some differences between FF and IE when it comes to JavaScript but this one seems really ridiculous to me. When you only set the "text" attribute at the Option object it seems like IE doesn't know how to handle it. So no text is presented in the select box. You have to explicitly set the innerText attribute for IE as mentioned in this post.

So, know I'll have to change my script only for IE and I have another reason to be angry at IE.

Friday, April 13, 2012

Code Kata

Some weeks ago I read about Code Kata in "The passionate programmer". I haven't had heard of it before.

From Karate I know Kata. It's a fixed sequence of movements and punches/kicks, which you repeat again and again and again...
That ways you gather automatism. And, what I got to add, a Kata is not directly meant to be used in a combat.

So, a Code Kata is the same for programming, at least it's supposed to be. According to Wikipedia Dave Thomas coined the term Code Kata and he invented some Code Kata, too, which you can use to train your programming skills. You can find them here or you can just use the links in the Wikipedia article.

I just started with Dave Thomas' Code Kata. Nevertheless, I like the concept and I suggest you have a try, too.

Saturday, April 7, 2012

Do you know the beginning of Linux? (book review)

Well, I won't ask if you know Linux or Linus Torvalds, I guess my spam filter couldn't handle all those mails I would get ;) but, I'll rather ask you: Do you know the beginning of Linux?

If you're like myself, you'll know, that one Finnish geek (in a positive manner), named Linus Torvalds started to develop Linux during his college time and it became quite popular after it was published under Richard Stallman's GNU license and because of some other circumstances.

I recently started to read Just for fun (link) written by Linus Torvalds and David Diamond and it is quite a good book.

I don't wanna do advertisement here (well, somehow I do, but I definitely won't get any money for more copies sold ;) ) but I am half way through that book right now and I can recommend it. If you're quite nerdy and you would like to know in detail how Linux started, you'll have to read this book. It is NOT a manual how to become famous by developing cool stuff (at least from my point of view, because those times were unique). It is rather a book to become nostalgic and ask yourself, what you did during that time (if you're older than I am ;) ).

All in all, I like this book and it should be in every geek's book shelf.

Wednesday, April 4, 2012

Did you every consider using the zsh?

I guess a lot of you guys use the bash to execute their scripts and to do some funky stuff on the terminal, but did you ever consider using something different?
As far as I know the bash is the standard in Ubuntu, Mac OS X and Cygwin. That ways it is quite normal to use it and to stay with it. I did it the same way.
But then (what feels already like an eternity away) I heard a nice speech about the zsh and its advantages. And now I only use the zsh, at least with my Mac.
I am far away from knowing every aspect of the zsh and even more far away from using all of its "power", but I want to make you think about the tool you use, when starting your terminal.

You can find all the features here: http://en.wikipedia.org/wiki/Z_shell#Features
The feature I like most is the completion. When using the bash and you start to write the command and hit tab your command will be completed or you'll be presented all the choices that fit (I guess you all know that ;) ). In zsh the nice thing is, that I also completes the options.
If you type "ls -" and hit tab, you'll be presented all the possibilities. Ok, for ls the options aren't that complicated, but when you use "ipconfig" it gets way more complicated.

All in all, give the zsh a chance and try it out, I did that and I don't regret it ;)

Friday, March 30, 2012

Decompiling an Android App "at" Marakana

This time my topic is not directly presented here, you can find it here.
I will just tell you the backgroud story.

So, how comes, that I wrote an article for marakana?
Roughly two weeks ago I attended the Droidcon in Berlin and listened to a presentation by Marko. Marko is one of the founders of marakana (what I didn't know by that time). In the evening, in fact during the socialising part of the Droidcon, I talked to him because his presentation was very interesting for me and I wanted to know more. That ways we started talking and he told me, that I could write an article for marakana so a bigger audience could read it (because way more people know marakana than my blog).
Well, as you can see, I wrote the article and the counter there is at 139 right now. Even with some people opening it twice or three times it's quite an impressive number for me.

What do we learn from this?

From time to time you gotta leave your "nerd cave" and talk to random people ;)

Thursday, March 29, 2012

Overview Git commands

As I mentioned in my first Git-post I wanted to prepare a nice overview for the Git commands, so you can print it and put it next to your PC or keep it in your pocket ;)
Unfortunately the people at GitHub didn't answer my question if I may publish it. Because I didn't want to wait any longer and because I wanted to give it to you guys I made sure it fits the German citation laws.

So, here you go: http://goo.gl/eUyIu

If you find any mistakes or have an idea for improvements please let me know.

Saturday, March 24, 2012

Problems with ADT17/Reverting to ADT16

Yesterday, I upgraded my Android SDK and ADT to version 17, especially because I wanted to see, how I could use the new, generated BuildConfig class.
So, after starting my emulator, a NoClassDefFoundError was thrown, when starting the second activity with an intent.
After some searching I found this solution http://android.foxykeep.com/dev/how-to-fix-the-classdefnotfounderror-with-adt-17
Well, I use Maven for my dependencies and that's why that solution doesn't work for me(I just assume, that FoxyKeep is right).

After messing up the whole classpath of my app, I decided to just switch back to version 16.
If you want to do that too, follow these steps:
  1. Download the old SDK (actually I am not totally sure, if it's necessary to revert the SDK, but I wanted to minimise possible errors). Just copy one of the download links and change the version number.
  2.  Replace your tools folder with the tools folder from the old version.
  3. Deinstall the ADT in Eclipse (reverting in Eclipse didn't work for me)
  4. Download the old ADT by taking the link from here and again, changing the number to the desired version.
  5. Install the ADT in Eclipse by following the steps listed under "Troubleshooting ADT Installation"
And now you're good to go. Maybe you have to fix the conversion-to-dalvik-format-failed-error again, but that's all.
If anyone finds a solution for ADT 17, please let me know ;)

Friday, March 23, 2012

Experiences with local Git and Eclipse

Some weeks ago I wanted to learn a little bit about Git because it is quite a buzzword. I already knew how to work with SVN and CVS, so I thought that it couldn't be very different.
I won't list any differences between those here. You can find out by yourself (and you are encouraged to do so ;) ).
What was really useful for me, was, that it is pretty easy to put a local folder under Git control. Just installing Git, adding it to the PATH-variable and that's all the preparation you need.
After that, just switch into a directory you like and type:
git init
Now your folder is under version control. (if you type into your shell ls -la you'll see the new .git folder)
If you put some files into that directory and add them with  
git add
and commit them with
git commit
Now you can keep track of your changes.
Of course, if your hard disk gets broken Git won't have any use for you. But one possibility would be to add a folder to Git (as described before) on an external hard disk or an usb-drive and then, on your hard disk, call
git clone "path to the external drive"
Now you are able to store your changes there and, depending on how you treat your hardware ;) , keep them quite safely.

Another nice advantage is, that you can put your existing Eclipse projects under version control and then use EGit. You only have to remove those projects from Eclipse, call git init on them and add the repository to Eclipse (under Git repository view). Now you can import them from the repository view.
It'll look like this:
EGit showing some repositories, including the local one

Last week I accidentally deleted a src-folder from one of my projects because I didn't read the Eclipse dialog, if I was sure about doing that. What a luck that folder was under my local version control. I was able to restore everything (of course except for my last changes).


It gotta thank the people of http://gitref.org/ because that website is a really good overview for the different commands. I'll try to shorten those commands a little bit, so that they fit one piece of paper and put that file up, so everybody can print it, as kind of short reference. But before I do that, I will ask the gitref people because I used their website.


Wednesday, March 21, 2012

The symbol @ in LaTex or just thinking way too complicated

When I recently wrote a report in LaTex I struggled to get the letter @ into that report.
My thought was, that @ has to be some kind of special symbol in LaTex, because everything that's not a letter or number is a special symbol for starting e.g. some formatting (what is actually wrong, but you'll find out later).
So, my first try was to escape the @ with a backslash. And, after saving, my TeXlipse showed me an error (Missing $ inserted. \@).
Ok, no problem, I have to escape the backslash for escaping the @ with another backslash. Actually, that worked so far.
So, I went on writing. After a while I took a look into the PDF and saw, that the titles with the @ (the title started with it) had a line feed and was not, where it was supposed to be. Now Google had to solve my problem. Unfortunately Google couldn't solve my problem. The only halfway helpful result was a blog in which the author suggested to write the @ in math-mode, like $@$.
That worked for the title and the @ in the text. But, because of the math-mode, the @ wasn't as pretty as it should be.
That was the point when I got a little bit upset about LaTex. In general I like it, but that was really bugging me. Also, because it seemed to be such a small problem.

After even more wild combinations in Google and some time, I found the solution.
.
.
.
Simply write the @ and everything is fine. No escaping, no special command (like \texttrademark) and no import of some symbol packages.
Sometimes the solution is sooooo simple ;)

 

Copyright @ 2013 Wrong tracks of a developer.

Designed by Templateiy