Sunday, September 08, 2013

Apache Commons IO File Monitoring Example

Here is another example from the archives of the mythical archana on File monitoring using Apache Commons I/O for file monitoring. I remember working on this to see if we could come up with a solution since we could not upgrade the application to Java SE 7. So this proof of concept was to try out a couple of different file monitoring options.

This example uses a FileAlterationListener to monitor file changes. It is simple to implement.

The code for the project can be found here: file-monitor

Friday, September 06, 2013

Apache Commons File Upload Servlet Example

I was doing some code cleanup, and found this servlet that does file upload using Apache Commons Fileupload.

I have a couple of other articles and code that are better, but I thought I would publish this just as another example. I did a quick code search and it appears I was using some code example snippets from the Apache Commons fileupload site, some forum posts, and an article OnJava on the O'Reilly & Associates site: Using the Jakarta Commons, Part 1.

Simply put, the provenance of this code is questionable. If you contributed to it, please send me a link to your code and I will add your name to the authors list. I don't want to claim the code as my own since I am sure I got it from multiple F/OSS sources.

Here are some of the other articles which I am sure I know about:
Here is the servlet code: apache-commons-file-upload

ApacheCommonsFileUploadServlet.java

Java EE Tip of the Day: @WebFilter Testing Using Arquillian, Warp, and Drone

Introduction

I was looking for a simple solution to test @WebFilter servlet filters. The tests that I had used a lot of mocks, but really just left me unsatisfied. It was not a real world production solution. How would they "really" behave in the actual server environment. I had been using Arquillian to do some testing of our CDI layer, and decided to try it on filters.

I looked at the API, and examples and found really nothing. I found one really "bogus" test example from core api tests that left me really wondering if I should bother. As many of you know though, I love a challenge. I tried a number of ideas including making the filter a JSR-316 @ManagedBean. I am not sure what glue I sniffed for that one (it didn't work...), and using @Inject (that didn't work either... more glue).

What I settled on was using Arquillian Warp and Drone to check the HttpServletRequest and HttpServletResponse after the filter processed it. There are a couple of annotations @BeforeServlet and @AfterServlet that helped me with checking my filters. There are no before and after filter annotations though. That would have made life easier.

Note: The thing to keep in mind with these annotations is that the filtering will have already occurred. You are looking at the post-filter results.

I also wanted to be able to demonstrate another really cool feature called ShrinkWrap, and its Java EE descriptor extensions. This allows you to programmatically create your web.xml file to provide additional flexibility like overriding annotations.

Problem Description

We want to be able to test a @WebFilter that modifies HttpServletRequest attributes, and HttpServletResponse headers. The filter should be tested in isolation, and in a real container to examine the real-world response. The tests should be able to be run on a variety of containers with no modification to the test code.

A Solution

Using MavenArquillianWarpDroneShrinkWrap, and GlassFish 3.1.2.2 embedded we can accomplish all aspects of the testing requirements. I used GlassFish since it is the Reference Implementation for Java EE 5, EE 6 and EE7, and it is a great application server. The container can be swapped out easily in maven with another profile. In the example code below, I used NetBeans 7.4 Beta to do the development.

The code for the project can be found on BitBucket here: maven-arqullian-filter-test

There are two types of examples included. One set of examples demonstrate simple integration tests that use annotations, and annotation override using web.xml. The second set of examples demonstrate dynamic web.xml generation, and overriding. Here are some code snippets from the project.

Popular Posts