Embracing the Messiness in Search of Epic Solutions

Month: January 2014

  • Guava: Elegant Caching Mechanisms

    There are many different ways to implement a caching mechanism. Google’s Guava provides very simple and elegant solutions to do so. OPTION 1: Caching using Supplier If you want to cache just one thing, Supplier should satisfy that requirement. In the above example, we cache all employees for a day. OPTION 2: Caching using LoadingCache… Read More…

  • MockMvc : Circular view path [view]: would dispatch back to the current handler URL [/view] again

    PROBLEM Let’s assume we want to test this controller:- Here’s the test file:- When executing this test, we get the following error:- SOLUTION The reason this is happening is because the uri “/help” matches the returned view name “help” and we didn’t set a ViewResolver when constructing the standalone MockMvc. Since MockMvcBuilders.standaloneSetup(…) doesn’t load Spring… Read More…

  • MockMvc + Mockito = Epic Tests

    Spring Framework 3.2 introduces a very elegant way to test Spring MVC controller using MockMvc. Based on the documentation, there are two ways to configure MockMvc:- The first approach will automatically load the Spring configuration and inject WebApplicationContext into the test. The second approach does not load the Spring configuration. While both options work, my… Read More…

  • Guava: Elegant Collection Ordering

    PROBLEM Java provides a built-in API to sort a list, but it is flat out ugly and prone to error especially when dealing with Comparator. Here’s an example:- Number of puppies killed = 3 … from the if-else statement. SOLUTION A better way to do this is to use Ordering from Guava:- </request,> In this… Read More…

  • Developing against H2 Database: Lesson Learned

    While my production database is MS SQL Server, I have been using H2 database for rapid local development. I wrote my DDL scripts as “Transact-SQL” as possible so that I don’t need to maintain multiple DDL scripts for each database. So far, it has been working out great. My web development runs against H2’s file-based… Read More…

  • HTML: Creating a Default Placeholder for Image that Fails to Load

    PROBLEM Let’s assume our web application relies on an external system to provide the image link, for example: employee photo link of a company. If the image link is invalid, we will get the ugly looking “x” on certain browsers. SOLUTION To fix this, we can load an image placeholder from our own web application… Read More…