Shaun Abram
Java and Technology weblog
Hamcrest Matcher
As a follow up to the Hamcrest post I made yesterday, I wanted to post an example of my own Hamcrest matcher implementation. This matcher tests if a string contains a specified number of substrings.
An example usage could be:
String sql = "select a,b,c from tableA";
assertThat(sql, hasNumberOfSubstrings(",", 2));
See the source code below. I have been reading up on OSS licenses recently and decided to release this using the same license as Hamcrest – the new BSD license.
I have also attached a jar which includes the associated unit tests, although you will need the hamcrest-unit-test project to compile, which can be downloaded as part of the hamcrest all-in-one jar.
Read more
Tags: Hamcrest, JUnit, Testing, unittesting
Hamcrest
Hamcrest is a framework for writing matcher objects. Matchers have a variety of uses, but are particularly useful when writing unit tests. Instead of using JUnit’s assertEquals methods, we use Hamcrest’s assertThat construct with one (or more) of the many Matchers available. For example
assertTrue(a.equalTo(b));
becomes
assertThat(a, equalTo(b));
A small change in this example, but Hamcrest’s benefits are many, enabling you to write much more flexible tests that are easier to read and have more meaningful failure messages.
Read more
Tags: Hamcrest, JUnit, Testing
Final day at JavaOne
I posted some notes about my final day at JavaOne to theServerSide. At the moment, currently on the front page. Cool ![]()
http://www.theserverside.com/news/thread.tss?thread_id=60987
Tags: javaone
Singleton implementations
A singleton is a class that is instantiated exactly once (or at least, once per classloader). It is considered to be an anti-pattern by many (including me!) because it makes unit testing difficult. You can’t substitute a mock implementation for a singleton (unless it happens to implement an interface, which I haven’t seen too often). And singletons also make your APIs into liars.
However, if you do want/have to use singletons, this article discusses the best approach.
Tags: singleton
HelloWorld: JSP -> Servlet
A while ago, I posted a simple HelloWorld Html->Servlet->JSP setup. Even though the html part is unnecessary (a JSP can obviously handle that alone), I deliberately included it as a template for Html/Servlet/JSP interaction. I have used it a bunch of times to get simple Proof of Concept projects up and running, or as a basis to create a simple browser interface to some server side process.
Most of the more recent projects I have used it on however have used straight JSP->Servlet, with no .html file involved. So I am now posting that setup, including some simple JSTL EL and conditional logic.
Selenium talk at SF JUG
I attended another great San Francisco JUG meeting tonight, this time on How to use Selenium with Maven/Ant to automate testing of web apps.
The talk was given by Chris Bedford, from Build Lackey Labs – “Automating the Monkey Work Out and the Quality In!”. Overall, I thought this was a great talk by Chris. He clearly has a huge amount of experience creating automated tests and integrating them with build tools and he gave a well structured, interesting, well delivered presentation. I have posted a copy of Chris’s slides and I think the video will be posted on the SF JUG site at some point, but I have also posted my notes from the presentation below…
Read more
Tags: JUG, q, selenium, sfjug, Testing, unittesting
Java User Groups and cultivating your career
There is an interesting article here about the importance of User Groups, which links to this one about cultivating your career.
Both definitely worth a read.
Data binding in Spring MVC
Two of the most important tasks carried out by Spring MVC when you submit a form are Data binding and validation.
The following article discusses data binding, including the use of custom PropertyEditors, and some of the options available for registering such editors. Most of the information discussed applies to Spring in general, but its application in Spring MVC is my primary interest.
In a future article I would like to discuss validation including the use of custom error messages.
Note that these notes relate to version 2.5.6 of Spring, the latest production code at time of writing, and depend heavily on the corresponding Spring reference docs.
HelloWorld: Html -> Servlet -> JSP
A couple of times recently I have had to setup up some very simple code to display on the client side (browser) some values created on the server side based on basic user input.
This has been part of some prototype/proof-of-concept work. I am posting my basic setup as an example in the form of the ubiquitous HelloWorld app. It uses a Html page to receive user input, which is submitted to a Servlet, processed, and the response displayed in a JSP using EL (Expression Language).
Obviously this could be done in various ways, including just JSPs, but this example acts as a template of Html/Servlet/JSP interaction.
1) Create a Html page called HelloWorld.html
<html>
<head>
<title>Greeting Setup - Hello Who?</title>
</head>
<body>
<FORM ACTION="/HelloWorld/HelloWorldServlet">
Enter greeting recipient:
<INPUT TYPE="TEXT" NAME="msg">
<p></p>
<INPUT TYPE="SUBMIT" VALUE="Submit">
</FORM>
</body>
</html>
2) Create a servlet to receive the user input
HelloWorldServlet.java
package com.helloworld;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWorldServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
//get inputs
String msg = req.getParameter("msg");
//any additional processing can be done here
//set outputs
req.setAttribute("msg", msg);
//forward to jsp
req.getRequestDispatcher("/HelloWorld.jsp").forward(req, res);
}
}
3) Create a JSP to display the response
HelloWorld.jsp:
Hello ${msg}
4) Create the web application’s deployment descriptor file:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<welcome-file-list>
<welcome-file>HelloWorld.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>HelloWorldServlet</servlet-name>
<servlet-class>com.helloworld.HelloWorldServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorldServlet</servlet-name>
<url-pattern>/HelloWorldServlet</url-pattern>
</servlet-mapping>
</web-app>
That’s it.
The easiest way I have found for this kind of development is to create in Eclipse as a Dynamic Web Project (available via the Web Tools Platform that comes with the in the “Eclipse IDE for Java EE Developers” package). When you have carried out the above steps, just right click the project -> Run As -> Run on Server (assuming you have a server like Tomcat or GlassFish setup in Eclipse…).
I have attached the source in a war file.
Tags: el, jsp, servlet, webapp
JUG Meetup: Joshua Bloch (Effective Java)
I had the chance tonight to see Joshua Bloch speak at the Silicon Valley Web JUG meetup down at the Googleplex in Mountain View. I have read and blogged about his great book “Effective Java” – probably the single best book I have read on Java – so it was great to hear him in person. The talk covered a couple of examples from his Java Puzzlers book as well as a discussion on some of the items from the Effective Java book. As expected, he was a great presenter, both insightful and funny.
Subscribe to RSS Feed