Archive for the ‘Java’ Category

Testing with TestNG

Friday, January 4th, 2008

When asked about automated unit testing in Java, most developers first think of JUnit. Without a doubt, JUnit is still the de facto standard when it comes to test-driven development in Java, and is arguably the most popular/successful in the xUnit family of frameworks created by Kent Beck, and (in case of jUnit) Erich Gamma, the co-author of the very influential Design Patterns book (one of GoF).

Yet, despite all the popularity and the success behind jUnit, the open source community chose to embrace yet another automated testing framework for Java called TestNG. Created by Cedric Beust and Alexandru Popescu in 2004, TestNG tries to address many of the shortcomings of jUnit outlined here and here.

Of course, the jUnit camp has not stood still since 2004, and it caught up to TestNG with many of the features introduced in version 4.0, most notably the annotation-based API. Yet, TestNG is still considered superior to jUnit is the areas of flexibility, configuration-driven-testing, dependency-driven-testing, reusability of test cases through parameters, and efficient/quick rerunning of failed and skipped tests. Also, TestNG is not just limited to unit-testing, since it can also serve well as an all-around testing framework for integration and system testing.

For those with a significant investment in jUnit, TestNG folks even provide a simple Java utility to help with the migration from jUnit.

Filippo Diotalevi from IBM wrote a great TestNG example that outlines some of the main principles behind the framework as well as the @Test, @Before, @After annotation syntax that is at the heart of TestNG. It's a great read.

Update: if you live in the Bay Area, you can hear me talk about TestNG at the next event of The San Francisco Java Meetup on February 11th, 2008 at our classroom in the city. I hope to see you there!

Securing Java web applications using FORM-based container-managed security

Tuesday, November 20th, 2007

This is a simple example on how to secure a Java web application (regardless of whether it is based on Servlets, JSPs, Struts, Spring, JSF, or any other combination) using just simple FORM-based container-managed security. True, other, much more sophisticated security frameworks exist, but this is by far the simplest way to wrap a security layer around an application regardless of its underlying implementation.

We also demonstrate how to authenticate users against a relational database (in this case we used MySQL).

The example application shown below was tested on Tomcat, but the principles should work on any modern Java EE servlet container.

(more…)

Implementing Simple Java GZIP Utility

Friday, January 26th, 2007

This examaple shows how to implement a GZIP program in Java as a command-line utility. It is demonstrating the use of Java I/O streams and shows the simplicity of plugging one stream type into another.

(more…)

Java JDBC Example - Basic Database Operation

Friday, January 26th, 2007

This is a simple code that shows how to use JDBC. It shows how to select data from the northwind database running on localhost to which we connect using USERNAME/PASSWORD login. This is the basic database operation. INSERT/UPDATE/DELETE are even simpler since they don't require looping through the result set.

(more…)

Java JSP - Login System Example

Wednesday, January 17th, 2007

Here is an example of a simple login system.

<html>
<body>
<form action="login.jsp" method="post">
    <h1>Please Login</h1>
    Login:    <input type="text" name="login"><br>
    Password: <input type="password" name="password"><br>
    <input type=submit value="Login">
</form>
</body>
</html>
Login JSP Page
<%
String login = request.getParameter("login");
String password = request.getParameter("password");
if ( login.equals("admin") && password.equals("guess") ) {
        // Valid login
        session.setAttribute("authorized", "yes");
} else {
        // Invalid login
        session.setAttribute("authorized", "no");
}
%>
<html>
<body>
<h1>Login Confirmation</h1>
You logged in with <%= login+"/"+password %>
<br><br>
<a href="secret.jsp">See the secret</a>
</body>
</html>
The Secret Page
<jsp:include page="secret_include.jsp"/>
<html>
<body>
<!– Assume user is logged in and good to go –>
<h1>Welcome</h1>
Here is the secret
</body>
</html>
The Code Protecting The Secret
<%
String authorized = (String)session.getAttribute("authorized");
if ( authorized.equals("no") ) {
%>
<jsp:forward page="login.html"/>
<%
}
%>

Java JSP - Simple JSP Example Example

Wednesday, January 17th, 2007

This example illustrates how to write a simple JavaServer Page. Here we implement a game of Craps:

1. Roll two dice
2. If the sum of the two is 7 or 11, you win

(more…)

Java JDBC - Fetching a Record Example

Wednesday, January 17th, 2007

This program gets a customer from a NorthWind-like database.

The example demonstrates the use of prepared statements, SQL SELECT * calls, and use of result meta info.

To run this example against a MySQL database you would do:

/path/to/java
-Djdbc.drivers=com.mysql.jdbc.Driver
-classpath .:mysql-connector-java-3.1.8-bin.jar
ShowCustomer
"jdbc:mysql://my.server.com:3306/Northwind?user=myuser&password=mypassword"

(more…)

Java JDBC - Updating a Record Example

Monday, January 15th, 2007

This program modifies a customer in a NorthWind-like database.

The example demonstrates the use of prepared statements and SQL UPDATE calls.

To run this example against a MySQL database you would do:

/path/to/java
-Djdbc.drivers=com.mysql.jdbc.Driver
-classpath .:mysql-connector-java-3.1.8-bin.jar
ModifyCustomer
"jdbc:mysql://my.server.com:3306/Northwind?user=myuser&password=mypassword"

(more…)

Java JDBC - Listing Records Example

Sunday, January 14th, 2007

This program lists customers from a NorthWind-like database.

The example demonstrates the use of JDBC statements and SQL SELECT calls.

To run this example against a MySQL database you would do:

/path/to/java
-Djdbc.drivers=com.mysql.jdbc.Driver
-classpath .:mysql-connector-java-3.1.8-bin.jar
ListCustomers
"jdbc:mysql://my.server.com:3306/Northwind?user=myuser&password=mypassword"

(more…)

Java JDBC - Inserting a Record Example

Sunday, January 14th, 2007

This program adds a customer into a NorthWind-like database.

The example demonstrates the use of prepared statements and SQL INSERT calls.

To run this example against a MySQL database you would do:

/path/to/java
-Djdbc.drivers=com.mysql.jdbc.Driver
-classpath .:mysql-connector-java-3.1.8-bin.jar
AddCustomer
"jdbc:mysql://my.server.com:3306/Northwind?user=myuser&password=mypassword"

(more…)

Java Networking - Web Server Example

Saturday, January 13th, 2007

This program demonstrates a simple multi-threaded web server.

This example demonstrates:

- Server Socket
- Reading/writing from/to sockets
- Threads
- Reading files from disk
- Regular Expression matching
- Date formatting
- Nested (inner) classes

(more…)

Java Networking - Web Client Example

Saturday, January 13th, 2007

This program fetches the content referenced by the specified URL and writes it to STDOUT.

This example demonstrates the use of URLs, URLConnections, and InputStreams.

(more…)