The equality of any two instances of user defined classes is determined by comparing the pairs of values for each field defined for that class. If the field value is an instance of another class, the comparison recursively continues the comparison of the two values. Circularity in data definitions is detected. If the objects that are being compared contain a field of the type double or float (or their wrapper classes), the checkInexact variant of the test method is used.
The class ExamplesUserTypes contains all test cases. The additional classes are user defined classes whose instances are compared.
Here is the complete source code for this test suite.
You can also download the entire souce code as a zip file.
Complete test results are shown here.
Here is a simple example of a class that refers to another class and the tests that check the equality of their instances:
public class Book {
public String title;
protected Author author;
protected float price;
public Book(String title, Author author) {
this.title = title;
this.author = author;
}
public Book(String title, Author author, float price) {
this.title = title;
this.author = author;
this.price = price;
}
}
public class Author {
public String name;
public int age;
public Author(String name, int age) {
this.name = name;
this.age = age;
}
}
public class ExamplesUserTypes {
Author js = new Author("John Steinbeck", 66);
Author jkr = new Author("J K Rowling", 45);
Book tp = new Book("The Pearl", js);
Book eoe = new Book("East of Eden", js);
Book hp = new Book("Harry Potter", jkr, 100.50f);
Book omam = new Book("Of Mice and Men", js, 75.25f);
public void testBooks(Tester t) {
t.checkExpect(js, new Author("John Steinbeck", 66),
"Success - t.checkExpect(js, new Author(John Steinbeck, 66))");
t.checkFail(jkr, new Author("J K Rowling", 44),
"Test to fail - t.checkFail(jkr, new Author(J K Rowling, 44))");
t.checkInexact(hp, new Book("Harry Potter", jkr, 100.0f), 0.01,
"Success - t.checkInexact(hp, new Book(Harry Potter, jkr, 100.0f), 0.01)");
t.checkInexactFail(hp, new Book ("Harry Potter", js), 0.01,
"Test to fail - t.checkInexactFail(hp, new Book (Harry Potter, js), 0.01)");
}
}