Sunday, June 21, 2009

    Unit Test Helper

    Working on JUnit is fun and great but always remember a golden rule "Never change a good design just to make it accessable/runnable by JUnit". Let me elaborate on this statement to tell you what exactly I meant. In our code we write data access layer, which creates connections and execute sql.

    Specially in Web Application, connections are often managed inside container and are accessed by our code using JNDI. This is one of the industry best practices. Problem is how to test that code outside the Container. How to make that code JUnit accessible.

    Normally we might make some changes in our code in order to make JUnit run outside container, DON'T. With the above statement I meant these kind of changes specially. But the question is what is the solution for this situation. Should you leave that code untested? No, you can still test that code. And I am not going to talk about any new testing methodology either. You can test the same code with simple JUnit.

    This is made possible a small helper jar called JRUnitTestHelper from Java Ranch. This junit utility helps create JNDI based datasources and connections at the run time to assist junit test the code efficiently.

    Using this jar is very very simple, put this jar in your classpath while running your junit test and call these two lines

    if(JNDIUnitTestHelper.notInitialized()){
    JNDIUnitTestHelper.init("jndi_unit_test_helper.properties");
    }

    And of course please mention your connection details like IP, port, username etc in this "jndi_unit_test_helper.properties" properties file. Please feel free to shoot me emails if you have any questions.

    Wednesday, June 17, 2009

    Google Maps

    Google Maps is an interesting feature which most of us have used in some way or other. Either to find the way to a friend's new house or to locate a Pizza joint near our house. It helps and it sure do but what is interesting is to learn how to use this in our Applications to make them more user centric with all other Web 2.0 concepts.

    Let me start with giving you some examples, you go to a Bank's website and click on Locate an ATM link. It takes you to a page where you select a state from a drop down (Eeeaaakkhhhh!!!). The selection than loads another drop down below it with the city information (Double Eeeaaakkkkhh!!). You than select your city and the page is refreshed with 50 entries and their address. Now its your job to figure out which one is the nearest one and in most cases you doesn't even know your neighborhood else you would not had to use this service. Now as a Web developer how can you make this easy and interesting for the user. How about using Google Maps for this? Nope I am not kidding you can integrate Google Maps with your application (For FREE!!). Check out Google Maps API it have very interesting example and usage.

    To see a live usage you can goto my other blog Varun Smriti Udhyaan. It is a very flexible api completely usable using javascript technology and allows you to be very creative in finding the usage. Check out the examples and demo provided on Google Maps API page.

    Be Creative...

    Thursday, April 2, 2009

    Custom Error Pages

    Nope you are wrong, I am not talking about errorPage="true" attribute in this blog. I am talking about few error messages which are sent by the server to the browser like 404. How often you have seen this page.
    Now this is not a good way to treat your site visitors. The main problem with this kind of errors is that than the visitors can drift away from your site. So displaying a custom error message with features like navigation to similar pages or asking them to report a broken link is far better. You can do that pretty easily using the below mentioned solution.

    Implement custom error pages to hide raw exception messages. To do this, simply add something like the following to your web.xml:

    <error-page>
    <error-code>404</error-code>
    <location>/error/404.html</location>
    </error-page>




    Now you are free to write your own custom messages in the 404.html file

    Here is something which is out of scope for this article but you may find interesting. Here are reference to some of the best 404 pages on the web. Best 404 Pages
     

    Site Info

    Text

    Java 2 Enterprise Edition Copyright © 2009 Community is Designed by Bie