Sunday, December 6, 2009

open source web-based centralized console for tripwire

When using tripwire to monitor changes on multiple servers, it is common to have to review and accept changes on a daily basis. Logging into multiple servers to accept the changes is troublesome. So, I've created a  web-based centralized console to review and accept changes. It is GPL licensed and can be downloaded from http://centralwire.sourceforge.net. Hope it is useful to others.

Friday, December 4, 2009

Handling BLOB/CLOB in postgreSQL with JDBC/Hibernate

Handling BLOB/CLOB in postgreSQL with JDBC/Hibernate

What is a BLOB/CLOB? It is a large object (binary for BLOB and char for CLOB). It is commonly used when you're storing large files into the database as column values.

It was really a challenge to work with BLOB/CLOB and in the process I almost pulled my hair out. Below are the hard lessons learned:

  • Avoid using LOB if possible! If say, it is just a few KB in size, just treat it as a string or byte array.
  • You must NOT try to read the stream outside the transaction. Note that using the open session in view is NOT enough. Once the transaction is ended, even if the Hibernate session is still open, you still can't read it (you'll get the "invalid large object descriptor" exception).
  • You must NOT try to read a LOB twice in the transaction. The "pointer" can't seem to be reset so you read past some portion, you can't read it again.
  • I still haven't figured this one out: If you repeatedly read a row containing a LOB in different transactions, it may cause the same "invalid large object descriptor" when you try to read it in a new Hibernate session.

Friday, November 13, 2009

Better approach to unit testing pages in web applications

It's common to see two approaches to unit testing pages in web applications: in-container (real container and browser) and out of container (mocked container and no browser). The former is good in that the tests can check the HTML DOM element and thus works with AJAX, but it is difficult to mock the services as the web application is running "on the other side". The latter is the opposite: Easy to mock the services but usually the tests can only check the internal state of the program, not the end HTML code.

In fact, a better approach is to combine them together: Run the application in the container and run the container in-process so that the test code can mock the services. This way you can control the right thing (user input on the web page and the data your pages get, from the services), and then observe the right thing (HTML DOM elements, possible manipulated by Javascript/AJAX). In order to run the container in-process, we can, eg, run an embedded Jetty. As it is run in the same process, the test code can access the servlet context and thus can get the opportunity to replace the services with mocks.

I've created a proof of concept library for unit testing Wicket pages. The mocking is done by using a chain of component injectors. The first one keeps a map of mock objects. The second one is the normal one to inject Spring beans. Usually the map in the first injector is empty so it has no practical effect in production. But your tests can put mock objects into there to perform mocking.

The library is released as open source software (LGPL). You're welcome to check it out at http://wicketpagetest.sourceforge.net. There is a step-by-step tutorial.

Finally, potentially this approach can be applied to other web frameworks like JSF, Tapestry and etc.

Sunday, October 4, 2009

Javablackbelt a rip off?

I always thought that javablackbelt was a non-profit community, but actually it is not. It is actually selling the questions people submit for profit! I think this is ethnically not right:

  1. It always advertises as a community, but actually it is a for-profit company.
  2. It calls the license copyleft, but actually it is not Gnu copyleft. It explicitly grants itself the right to sell the questions.
  3. It should highlight this term. Currently there is no obvious license displayed when people try to submit a question.
  4. This is unlike RedHat making money from open source software because that software is licensed by the authors to be freely available for anyone, so RedHat has to provide its own added value. If javablackbelt is going to sell the questions, they should share the profits with the authors.

Friday, January 16, 2009

PMP exam

Sat and passed the PMP exam in HK. It is very similar to those free example questions on  www.oliverlehmann.com. All I used for studying was the book by Joseph Philips and dthe free questions mentioned above. The book covers almost everything in the exam, even though it would easier to read if it included more concrete examples.