Thursday 2 April 2015

Great website for Java based tests

Sorry I've been away for quite some time.

I want to keep this blog alive and any appropriate coding problems that I run into and want to remember will still be posted here.

In the meantime, I'm using this excellent Indian Java testing website. It's good to keep you on your toes after a lot of time has passed.

Tuesday 20 May 2014

A great blogpost about JavaFx Event Handlers and Change Listeners

If your using JavaFx and want an informative and well written tutorial on how to use Event Handers and Change Listeners, this post by Marco Jakob is great.

Here is an example I wrote of how to use it when listening out for what a user is typing in a TextField

public class MysteriousCaveDoor  extends AnchorPane
{
   @FXML
   private TextField txtEnterSecretPhrase;
   
   public MysteriousCaveDoor
   {
          txtTitle.textProperty().addListener(new ChangeListener<String>() {
      @Override
      public void changed(ObservableValue<extends String> observableValue, String s, String s2) {
         //think of s2 as "newValue" in the Code.Makery Tutorial
         if(s2.contains("open sesame"))
            openTheCaveDoors(...);
         }
      });
   }
}


Wednesday 2 April 2014

Conflict between "instanceof" and using a right-hand-side passed in argument.

Came across a problem I never knew about which boils down to when to use instanceof vs myVar.getClass().equals(...) .

This gets complicated pretty quickly, depending on what you want.

My particular problem was as follows:

Class Animal
Class Dog extends Animal
Class Bird extends Animal
Class Cat extends Animal

class PetStore
{
 List<Animal> animalsInStore;
 
 public List<Animal> getListOfAnimalsByType(Class<?> classType)
 {
  ...
 }
}


And with the classes above, this was the goal:
... void main()
{
 List<Dog> availableDogs = (List<Dog>)(List<?>) getListOfAnimalsByType(Dog.class);
 printAvailableAnimals("Here are all the dogs you can buy.", availableDogs);
}




So basically it all came down to how the method getListOfAnimalsByType() was implemented. I initially tried this:

public List<Animal> getListOfAnimalsByType(Class<?> classType)
{
 List<Animal> results = new ArrayList<Animal>();
 for(Animal animal : animalsInStore)
 {
  if( animal instanceof classType.getClass() )
   results.add(animal);
 }
 return results;
}

But immediately I got a syntax error.
See while it would have been fine to do something like this:

animal instanceof Dog.class
 dogs.add(animal)

animal instanceof Cat.class
 cats.add(animal)

animal instanceof Bird.class
 birds.add(animal)


In this case, the method  getListOfAnimalsByType() had no idea which sub class it would have to compare it to. I could have written instanceof Dog and Cat and Bird but as you can see that would have been way too much code to write.
So the fix for this example is:

for(Animal animal : animalsInStore)
{
 if( animal.getClass().equals(classType.gettClass()) )
  results.add(animal);
}


So this was a clear cut example of when to use getClass().equals() over instanceof but its not always so simple.

Other use cases to consider:

Depending on some use cases, it can be hairy which solution is best. Look at the following examples:

class Animal
{
 @Overwrite
 public boolean equals(Object o)
 {
  if (this.getClass() != o.getClass())
   return false 
  
  if (!(o instanceof Animal))
   return false ;
  
  Animal animal = (Animal) o;
  
  //some test that this is an Animal
  if(isThisARealAnimal(animal))
   return true;
  return false;
 }
}


class Dog extends Animal
{
}

Dog q = new Dog("Rover");
Animal w = new Animal("Rover");
if(q.equals(w))
{
 /*
 This will always be false when comparing Class instances unless you comment out
 the code "if (this.getClass() != o.getClass())" in animal.equals() .
 */
}


So again, depending on what you want, you gotta be aware of the differences.

Notes:
The top-answer of this stackoverflow question helped me make some sense of this.
The solution for casting one type of List to another (List<?>) I found here .

Monday 31 March 2014

A better diff system than DiffUtils?

After looking around with some help, the help guided me to DiffMatchPatch ( google-diff-match-patch ), as the name says, a diff system from Google.

It was decided to use this instead of DiffUtils because you can compare two Strings instead of two List<String> and the deltas can also be stored as a single text String which is quite a bit smaller storage needs than storing the DiffUtils.Patch object.

So as a new example of how to use DiffMatchPatch, I've rewritten the code from the previous blog post to use it with the same JUnit test.

Note that "firstVersion" and "secondVersion" Strings are the same value as they were in the previous blog post.

Example:


@Test
public void javaDiffTests()
{
 String firstVersion = getStringVersionOne();
 String secondVersion = getStringVersionTwo();

 String result = getDiffMatchPatch(firstVersion, secondVersion);
 if(!result.equals(secondVersion))
 {
  fail("diff test has failed, they are not the same. Check the getDiffUtils() method");
 }
}

public String getDiffMatchPatch(String firstVersion, String secondVersion)
{
 DiffMatchPatch dmp = new DiffMatchPatch();
 LinkedList<DiffMatchPatch.Diff> result = dmp.diff_main(firstVersion, secondVersion);

 LinkedList<DiffMatchPatch.Patch> patches = dmp.patch_make(result);

 String patchesText = dmp.patch_toText(patches);//This is to prove that you can store your diffs as a single String
 LinkedList<DiffMatchPatch.Patch> revivedFromTextPatches = (LinkedList<DiffMatchPatch.Patch>) dmp.patch_fromText(patchesText);

 Object[] patchedResultObjects = dmp.patch_apply(revivedFromTextPatches, firstVersion);

 boolean[] passRate = (boolean[]) patchedResultObjects[1];//"...The second element is an array of true/false values indicating which of the patches were successfully applied."

 for(boolean b : passRate)
 {
  if(!b)//we only want to proceed if all patches were successfully applied
   return null;
 }

 String merge = (String) patchedResultObjects[0];//"...The first element of the return value is the newly patched text."

 return merge;
}


Links:

The site for DiffMatchPatch is here .
You can find the Maven dependency for DiffMatchPatch from its forked project DiffPatch here .

An example of how to use DiffUtils

Might want to look at the newer blog post about using DiffMatchPatch instead of DiffUtils?

Summary:

The code that follows is a JUnit test example of how to use the DiffUtils API.

This example has two variables and two goals. 

The first variable is the original String (firstVersion).
The second variable is how the original String has been edited (secondVersion).

The first goal is to print out the Delta's (differences) of the edited String compared to the original String.

The second goal is to initiate a "rebuilding process". Taking the original edited String, applying the new found Delta's (changes if you like) to the original String to return a result String. If the "rebuilding process"(ie: the Patch object methods) is successful, the result String should be identical to the secondVersion (edited String).

Code:


@Test
public void diffUtilsExample()
{
 String firstVersion = "This is a sentence with no change.\n" +
   "\n" +
   "No change here.\n" +
   "No change here. No change here. No change here. No change here.\n" +
   "No change here. No change here.\n" +
   "No change here.\n" +
   "No change here.\n" +
   "No change here. No change here. No change here.\n" +
   "No change here.\n" +
   "\n" +
   "No change here.\n" +
   "No change here. No change here. No change here.";

 String secondVersion = "This is a sentence with some change.\n" +
   "\n" +
   "No change here.\n" +
   "No change here. No change here. No change here. No change here.\n" +
   "Changed over here. No change here.\n" +
   "No change here.\n" +
   "No change here.\n" +
   "No change here. Also changed here. No change here.\n" +
   "No change here.\n" +
   "\n" +
   "No change here.\n" +
   "No change here. Some changes over here. No change here.";

 String result = getDiff(firstVersion, secondVersion, "\n");
 if(!result.equals(secondVersion))
 {
  fail("diff test has failed, they are not the same. Check the getDiff() method");
 }
}

public String getDiff(String firstVersion, String secondVersion, String splitValue)
{
 List<String> original = new ArrayList(Arrays.asList(firstVersion.split(splitValue)));
 List<String> revised = new ArrayList(Arrays.asList(secondVersion.split(splitValue)));

 Patch patch = DiffUtils.diff(original, revised);

 for(Delta delta : patch.getDeltas())
 {
  System.out.println(delta);
 }

 try {
  List<String> result = (List<String>) patch.applyTo(original);

  if(!result.equals(revised))
  {
   fail("the patch.applyTo 'rebuild from diffs' method has not produced a result that matches the revised string-list");
   return null;
  }

  StringBuilder stringList = new StringBuilder();

  for(int i = 0; i < result.size(); i++)
  {
   String s = result.get(i);
   if(i != result.size() - 1)
    stringList.append(s + splitValue);
   else
    stringList.append(s);
  }

  String merge = String.valueOf(stringList);

  return merge;
 } catch (PatchFailedException e) {
  e.printStackTrace();
 }
 return null;
}

Notes:

The java-diff-utils libraries can be found here .

I haven't investigated a way to use DiffUtils without using two List<String> as the comparison variables. I wish I could simply use two Strings as the arguments for the DiffUtils.diff() method. Hope I'll come across this answer sometime in the future. Found a solution for this problem in my newer post .

Friday 28 March 2014

Getting mutable (non-finite) values from a String array.

Problem:

Had a String that I needed to split into Strings by new-line and put into List<String> , afterwards adding more values to the List.

So code looked something like this:
String sentence = "The quick brown fox \n" +
 "jumped over the lazy dog.";
 
List<String> lines = Arrays.asList(sentence.split("\n"));

lines.add("\n" + " ...and the fox escaped.");

But that didn't work. Got an java.lang.UnsupportedOperationException .

The fix:

I found out the problem was that Arrays.asList was returning a finite List, but I needed to add to it. So the work around to that was to make sure the asList was given to an ArrayList which can take your array of Strings[] and turn it into a mutable list.

List<String> lines = new ArrayList(Arrays.asList(sentence.split("\n")));


Context Notes:
This post was from me working with DiffUtils (com.googlecode.java-diff-utils) . Taking lines of text from an original String and detecting the Delta 's and then reapplying the deltas to a "rebuild" String that would match the original as a JUnit test.
This stackoverflow question gave me the answer under "For a Mutable List".