Friday, September 12, 2008

iText PDF Generator

Here I am going to explain how to creat a simple pdf file with java pdf library called iText. There are other well known competitors like Apache cocoon and PDFBox frameworks which are good and mature. But I choose to use iText, because of its popularity and support, infact, it is very good for pdf generation from scratch.

iText generates pdf dynamically, ideally this framework is very useful for developer who wants to enhance thier web application developement with dyanamic pdf generation based on user inputs on the fly.

Using iText, you can able to generate, maniulate and parse the pdf file. And, please remember, this is not an end user tool to view pdf files. To use this framework you should have installed Acrobat PDF software from Adobe Systems.

Enough said, we will get into the code and make our hands diryt. okay.
Download latest iText jar file Click here , and put it in your project's classpath
First, you need to create document object
com.lowagie.text.Document document = new com.lowagie.text.Document();
Second, create PDFWriter object by passing document, and give pdf file name you want to create
com.lowagie.text.pdf.PdfWriter.PdfWriter.getInstance(document, new
FileOutputStream("HelloWorld.pdf"));
Third, open the document to start writing
document.open();
Fourth, add a paragraph to your document
document.add(new Paragraph("Hello Ramraj...."));
Finally, you need to close the document
document.close();

Probably, some time later, I will attach the full source code :-)

Enough staying at office, I gotta go home, bye

Thursday, August 7, 2008

Junit friends DBUnit and Spring test framework :-)

Now-a-days TDD(Test driven development) is defacto standard in software developemnt. Dont know what is TDD? It is all about writing unit test cases for your business logic or for your data access layer a.k.a DAO layer before you actually implement your complete logic. I have been writing unit test cases for more than 2 and half years, and generally I use Junit framework. Of course, JUnit is a standard across java space for unit testing.

Junit is simple to implement for service/business logic. when it comes to database integration testing you may need to insert lots of data in database for to initialize your test data. For this to acheive you need to write lot of database insert scripts and run in setup method or you may prefer writing text/xml file with the required data and read the files using some other api and then insert the data in database. All these things you have to manage yourself, and may requires you write lots of bipolarate jdbc code, lots of complexity is involved when you use junit for integration tesitng.

Here comes spring test framework and DBUnit to simplify Junit and adds few usefull features which will ease the integration testing. These two frameworks are superset of Junit means they extend junit and adds additional features.

DBUnit features
  • Manages the insert/delete data from database (you just need to provide data set xml file)
  • DBUnit helps you preparing data-set file, so no manual work :-)
  • Exports tables in order based on foreign key constraint

Spring test framework features

  • Provides dependency injection in your unit test class( you just need to load your spring config files)
  • Automatic transactions applied to all the test* methods, so need to write transaction code.
  • Automatic rollback after unit test execution completed.

Your comments are appreciated. Have a good day :-) :-)

Monday, July 14, 2008

My First Blog

Hi Everybody, this is my first blog I ever created. I have planed long back to put up a blog about java technology, but couldnt find time to do so, finally wow I created a blog.

I am very much passionate about java technology and open source frameworks. Here in this blog, I am going to write about java opensoure frameworks like spring, struts, spring mvc, hibernate, ant, maven, log4j, JPA and many other frameworks. So, keep checking this blog regularly :-)