Entries Tagged 'testing' ↓
September 4th, 2007 — frame2, java6, testing
This seems to happen to me no matter what project I’m on. I try to implement a simple (or not so simple) feature and end up exposing at least one bug that’s been hiding around for quite some time.
As an illustration, I was adding a feature to Frame2 today to allow some parameters to be passed between events within a mapping – without making developers use the HttpSession. After a couple of false starts, I had a pretty good idea of how to make the changes and starting ripping out code. I came up with several unit tests, and modifying code to make the tests pass. Pretty standard Test Driven Development…until I wrote the test that made sure event chain validation was working.
Before I explain the bug, I’ll give a high level description of how Frame2 works when submitting form data:
- Form is filled out by user and submitted.
- Frame2 looks up the appropriate event from the form action.
- An instance of the appropriate event is created and populated by the introspector.
- Based on the mapping for the event, Frame2 will then validate the event. This may involve the Commons Validator if it is enabled and/or overriding the
Event.validate() method.
- If validation fails, the mapping must specify a location for the framework to forward back to. This is usually the same page where the data was entered.
- If validation passes, the framework then iterates through the list of event handlers, calling the
handle() method. If handle() returns null, the framework continues to the next handler. A non-null result usually indicates an error, and the framework will forward to the location named by the result.
- Once all of the handlers have been invoked, the framework will default to the view specified in the mapping. Usually this is just a JSP, but Frame2 supports forwarding to events – called “chaining”.
- For the chained event, Frame2 is supposed to start back at step 2 and continue processing until it gets to the end of the chain.
Notice how I say “supposed to” in step 9? The framework was actually only going back to step 6, bypassing validation for all events except the first in the chain. Subtle, yes, and unlikely to be noticed by most users – but it’s still a bug. The bug has now been fixed so that the framework goes back to step 2 like it’s supposed to.
In case you’re wondering what use event chaining is, here’s an example:
- User enters a blog entry and submits the form.
- Frame2 validates the form data and begins invoking handlers.
- The blog entry handler persists the data, perhaps to a database.
- The default view for the event mapping tells the framework to forward to the “View All Blog Entries” event.
- The framework creates the appropriate object and begins invoking the handlers in the “View All” mapping.
- The “View All” handler populates all of the blog entries into the “View All” event.
- The default view for the “View All” mapping is a JSP page, which renders the blog entries using JSTL.
Chaining allows the “View All” event to remain its own entity that can be invoked separately, while allowing the same functionality to be used in other places where appropriate.
By way of comparison, the feature I added allows the above sequence to be reworked like this:
- User enters a blog entry and submits the form.
- Frame2 validates the form data and begins invoking handlers.
- The blog entry handler persists the data, perhaps to a database.
- The blog entry handler sets an attribute with the new entry id into the context.
- The default view for the event mapping tells the framework to forward to the “View Specific Blog Entry” event.
- The framework creates the appropriate object and uses the context value(s) with the introspector to set some values in the event.
- The framework then begins invoking the handlers in the “View Specific” mapping.
- The “View Specific” handler looks up the entry with the specified id and sets its data into the event.
- The default view for the “View Specific” mapping is a JSP page, which renders the blog entry using JSTL.
August 31st, 2007 — frame2, general, sourceforge, testing
Frame2 is almost ready to be released as a new version. I may still make some changes, but it’s definitely in Release Candidate stage now. There are only a few things that I may add before testing the heck out of it:
- Upgrade unit tests to JUnit 4. There are a lot of tests, especially the ones that mock a servlet container, that do a lot of repetitive work in
setUp() . JUnit 4 has finally brought the ability to annotate methods to be run before and after all tests in the class with @BeforeClass and @AfterClass, respectively. Individual test setup activities can still be performed by annotating a method with @Before.
- Fix outstanding bugs. There’s only one right now, and it’s SOAP specific, so I may or may not fix it. It may take me longer to figure out how to reproduce the problem with unit tests than it’s worth.
- Add outstanding feature requests. Again, there’s only one, but it’s (in my opinion) a biggie – the ability to add parameters to the response when handling an event.
- Doc updates. The web services doc needs to be written, and I’m sure all of the other doc needs a good examination.
I’ll be testing the latest build with a web application that I’ve been working on for years that I’ve come to think of as an unofficial reference implementation of Frame2. I haven’t decided yet if the web app should become part of the project on Sourceforge or not. We’ll see.
Once the testing is done and I’ve released a new version of Frame2, I plan on working on the Eclipse plugin. The poor plugin has pretty much been ignored for several years now, and it needs some attention.
August 30th, 2007 — general, testing
I’ve been mucking around in the codebase, cleaning up pieces of the API that annoy me. Some bright person thought that it was a good idea to have a method that returns an Iterator to access a collection. I’ve been cleaning up all of these methods, changing them so that they return an actual collection – occasionally made unmodifiable through the Collections class.
Naturally, this has led to unit test failures. Almost all of the failures have been easy to fix, but once in a while one pops up that makes me scratch my head. Here’s an example:
public void testGetIfEmpty() {
assertTrue(this.errors.isEmpty());
assertNull(this.errors.get(FOO));
assertEquals(0, this.errors.get().length);
assertEquals(0, this.errors.get(FOO).length);
}
In this instance, errors is a class that aggregates individual Error objects. The get(String) method returns an array of Error objects that have the specified key, while get() retrieves all Errors. Pretty simple, right? After my changes, the test fails on the assertNull() statement, which isn’t surprising.
The failing statement read assertNull(this.errors.iterator(FOO)) before I removed the iterator() method, which tells us a bit about how the API was originally coded. The iterator(String) returned null if there were no matching entries. However, as the unit test clearly shows, get(String) returns an empty array when it doesn’t find any matching Errors. Quite the difference, and potentially confusing – the developer has to remember which method may return null.
The API has now been changed to return an empty collection instead of null when no matching errors are found. Now, a user of the API simply has to call the method and loop over the result (zero times if it’s empty) without having to make null checks.
Whoever wrote this unit test obviously knew that one method returned null while the other returned an empty array for the same input, since the test passed. I only wish that person would have looked at those lines and noticed the inconsistency, instead of leaving it for me to find.
August 29th, 2007 — findbugs, frame2, java6, testing
I’ve mentioned before that I’m using FindBugs to help make sure that the Frame2 code is up to snuff. The Eclipse compiler can catch a lot of things, bug FindBugs has some neat tricks up its sleeve to find things that Eclipse can’t. FindBugs uses static analysis of the code to search for patterns that indicate possible issues. Being the Type A person that I am, I always turn on all of the possible matches and whittle the list down. Quite often, however, FindBugs will find a false positive or two. Up until now, I’ve either tried to rework the code to avoid the warning or disabled the category altogether. I’ve never liked the approach of turning off a whole category just to avoid seeing a warning or two, so I spent the morning muttering to myself how useful it would be if FindBugs allowed me to ignore certain warnings like I can do with @SuppressWarnings.
Then I went and RTFM.
FindBugs has a filtering system that makes @SuppressWarnings look amateur. Here’s an example: in the SoapRequestProcessor, FindBugs marked a warning that an Exception was being caught when no Exception was being thrown. After looking at the code and verifying that the try block in question throws several different exceptions, I created a filter entry. The entry looks like this:
<Match>
<Class name="org.megatome.frame2.front.SoapRequestProcessor"/>
<Method name="getEvents"/>
<Bug pattern="REC_CATCH_EXCEPTION"/>
</Match>
This tells FindBugs to match a specific bug type in a specified method in a desired class. This entry can be used in both an inclusion or exclusion filter – I use it to exclude that warning from the results.
Here’s a more complex example:
<Match>
<Class name="~.*introspector\.Bean\d+" />
<Bug pattern="EI_EXPOSE_REP,EI_EXPOSE_REP2"/>
</Match>
There are some test classes that don’t exactly follow good rules of programming when it comes to dealing with mutability. Since they are test classes, I don’t really care to fix them. Instead I set up the filter to match all classes in an introspector package named Bean1, Bean2, etc. Simple as can be!
If you aren’t using FindBugs, you should be. Go check it out.
August 28th, 2007 — frame2, testing
I’ve finally reached the point where I hurdled my JAXB related validation issues, so another round of full unit testing is called for. Frame2 has two levels of testing:
- Unit tests that can be run outside of a servlet container. These include simple tests on discreet sections of the framework, and some tests written using (ironically) StrutsTestCase to simulate a servlet container.
- Tests that require a live servlet container. These include the TagUnit tests for checking the custom tag libraries and the Cactus tests that verify tag behavior.
The JAXB functionality is tested mainly through the first set of tests. However, as anyone who’s built a web app can attest, things work differently in a container. In this case, I’m talking specifically about loading resources. Using a getClass().getResourceAsStream() works just fine from the Ant console, but returns null when run within Tomcat. The solution, of course, is to use the ServletContext to load the resource, but obviously it’s not available when running outside of the container.
The simple solution was change the loading code to try to get at the ServletContext first, then fail to the getResourceAsStream() call. The best solution would be to refactor the appropriate unit tests so that they use the mock servlet structure – but I’m lazy today and mock objects can be quite tedious. Don’t get me wrong – I have nothing against mock objects. I would just rather get a new release of Frame2 out the door before devoting a lot of time to rewriting the test infrastructure, which is something most end users won’t care about.