Mock Testing

Image taken from http://zeroturnaround.com/rebellabs/how-to-mock-up-your-unit-test-environment-to-create-alternate-realities/

Mock Testing explained:

Concise Example of the EasyMock testing framework:

Reflection in Java

Six years of programming with Java and I’ve never had to use reflection. I don’t know if that a good or a bad thing but below is the best, most concise explanation of Java Reflection which I have found. The commentator confuses himself at times but the core message and logic comes across well. Only 11 or so minutes long.

Bean Validation Made Simple With JSR 303

http://java.dzone.com/articles/bean-validation-made-simple   The URL above explains clearly and concisely how to avoid checking that a value meets a constraint without the need for an if(isStringValueNull?) code block.

Security Issue for Java and MySQL or SQL

The most basic vulnerability is to leave the database operating on the default port of 3306 with the default user name and password. Ensure that this port is not open to the web.

SQL Injection Vulnerability

To protect against SQL Injection, it is necessary to avoid methods which take an argumenrt which is directly used in an SQL query.

The solution is a two setp process.

  1. First, validate the input argument against a regex or caught parse exception. Therefore you will know, if the method is supposed to accept a String containing a int, or an entire set of digits, you can be sure that this is the case. This ensures you are not using user input to directly query the database without validating it first.
  2. The second prevention step is not to use a direct SQL query. Instead use a prepared statement. This means that you are not using string concatenation or string replacement to query the database. If this were the case, an attacker can pass your method an SQL statement which will then be run against your database.

JAXB Exceptions Errors

org.xml.sax.SAXParseException: The processing instruction target matching “[xX][mM][lL]” is not allowed.

You cant have spaces before the start of your XML schema.

Builder Pattern in Java to avoid multiple argument constructors for the same object

This post is heavily inspired by page 14, chapter 2 of Joshua Bloch’s Effective Java Second Edition. I typed it out here as an exercise to memorise it.

The idea of this pattern is to implement a better way of creating an object which has optional parameters. For example, a Dog object in a database needs to have a name attribute. However, breed, age and colour attributes are optional. Consider the old constructor setup, and by old I mean the way I coded it before I discovered the Builder pattern:

public class Dog{

       public Dog(String name){…}

       public Dog(String name, String breed){…}

       public Dog(String name, String breed, int age, String colour){…}

etc, etc,

Not very efficient, the following is an example of the Builder Pattern

public class Dog{

 private final String name;       

 private final String breed;

 private final String colour;

public static class Builder{

//required parameter
private final String name;

//optional parameters
private final String breed = “”
private final String colour = “”

public Builder(String name){
this.name = name;
}

public Builder breed(String val)
{ breed = val; return this;}

public Builder breed(String val)

{ colour= val; return this;}

public Dog build(){
return new Dog(this);
}

private Dog(Builder builder){
breed = builder.breed;
colour = builder.colour;
}
}

The object of type Dog is then created as such:

Dog arsenesDog = new Dog.Builder(“Thierry”).breed(“lab”).colour(“golden”).build();

thierry-henry

Thierry Henry, an evangelist for the Builder Pattern

Schedule A Java Program Windows 7 – Windows 7 Cron Job

Recently I wrote a Java program to clean up a directory on my Windows 7 machine. I wanted this java program to run each day at 5pm. Here is how I scheduled it to do so.

1 – Compiled my java program into a jar file named “FileArchiver.jar”

2 – Created a batch file with the command which runs my program. Creating a batch file is as simple as opening notepad and saving the file with a “.bat” extension. The contents of my batch file, which is named “archive.bat”, consisted entirely of the following line:

java -jar “C:\GlenIris\NetBeansProjects\FileArchiver\dist\FileArchiver.jar” C:\\GlenIris\\NetBeansProjects\\FileArchiver\\properties\\securecam.properties

I then tested that the batch file ran my program by double clicking on it. It did.

3 – Schedule the task to run every day at 5. I did this by opening Windows Task Scheduler like so:

Start>>All Programs>>Accessories>>System Tools>>Task Scheduler

On the right hand side, click ‘create task’.

Select ‘Start a program’ from the action dropdown. In the text field, type the path to the bat file. eg:

C:\GlenIris\archive.bat

Each option in each of the other tabs is now self explanatory.

A divil for scheduling tasks on Windows Machines was Stefan Schwarz

A divil for scheduling tasks on Windows Machines was Stefan Schwarz