Programo Issues Cora Pérez Ariza ~ DECSAI ~ UGR Granada, January 28 th & 29 th, 2009
Index JUnit ◦Introduction to the topic ◦Tags ◦Example Design Decisions ◦equals and hashCode methods ◦toString method 2
JUnit Introduction 3 Why a JUnit? They reassure us that the expected behavior of our work is the actual behavior (and will stay like that in the future) How to use it? Creation is easy: short methods that test critical parts of the project’s classes. Integrated on NetBeans Few “tags” to learn
JUnit Tags
JUnit Example: Beginning 5 package programo.core.potential; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.junit.Ignore; import static org.junit.Assert.*; import programo.core.variable.*; import programo.core.assignation.*; You should import as many classes as tags you are going to use
JUnit Example: data members 6 public class PotentialTableTest { PotentialTable pot1, pot2, resNorm, resMarg, resComb, resProj; CategoricalVariable X1, X2, X3, X4; CategoricalAssignation configurationToProject; VariableSet setOfVars1, setOfVars2; You can declare variables as class members, to use as global variables for your tests
JUnit Example: before/after the tests public static void setUpClass() throws Exception { …. public static void tearDownClass() throws Exception { … } These methods will run before/after the bunch of tests (only once)
JUnit Example: before/after each test public void setUp() { … public void tearDown() { … } These methods will run before/after each test (so one time per test)
JUnit Example: auxiliar methods 9 public void initializeVariables(){ … } You can define auxiliar methods to perform specific operations within your JUnit file
JUnit Example: test cases public void testNormalizePotential() { initializeVariables(); pot1.normalizePotential(); assertTrue(resNorm.equalValues(pot1)); tag marks the method as a test Body of the test case Assertion to check if the test has worked
JUnit Example: test cases ready to public void testSumMarginalize() { initializeVariables(); Potential result = pot1.sumMarginalize(setOfVars2); assertTrue(resMarg.equalValues(result)); } You may not want to execute one or more test cases:
JUnit Result in NetBeans 12
JUnit Other useful utilities 13 Other utilities: Exception Handling Use “expected” parameter tag for test cases that expect = ArithmeticException.class) Timeout Define a timeout period in = 1000)
Design Decisions Overview: equals General Contract 14 public boolean equals(Object obj): Implements an equivalence relation: It is reflexive o For any reference value x, x.equals(x) should return true It is symmetric o For any x and y, x.equals(y) == y.equals(x) It is transitive o For any x, y, z, if x.equals(y) and y.equals(z), then x.equals(z) It is consistent o For any x and y, x.equals(y) should return the same in every invocation if both remains inmutable For any non-null reference value x, x.equals(null) should return false Equal objects must have equal hash codes
Design Decisions Overview: hashCode General Contract 15 public int hashCode(): Must be consistent during the same execution of the application Equal objects must produce equal hash codes, however unequal objects need not produce distinct hash codes o Different hash codes for different objects may improve the performance of hashtables
Design Decisions equals and hashCode: when to override 16 Do not override: Each instance of the class is inherently unique You don’t care whether the class provides a “logical equality” test A super class has already overridden equals appropriately for this class The class is private or package private, and you are certain that its equals method will never be invoked Override: When a class has a notion of logical equality that differs from mere object identity, and a super class has not already overridden equals to implement the desired behavior
Design Decisions equals and hashCode: possible problems 17 Problems with sets and hash maps: Overriding equals enables instances of the class to serve as map keys or set elements with predictable, desirable behavior
Design Decisions equals and hashCode: possible problems: Example 18 Problems with sets and hash maps: public class Point{ private final int x; private final int y; public Point(int x, int y){ this.x = x; this.y = y; } … HashMap tabla = new HashMap(); tabla.put(new Point(1,3), 1); if(tabla.containsKey(new Point(1,3))) System.out.println("both points are the same"); else System.out.println("two different objects");
Design Decisions equals and hashCode: possible problems: Example 19 Problems with sets and hash maps: public class Point{ private final int x; private final int y; public Point(int x, int y){ this.x = x; this.y = y; public boolean equals(Object obj){ if(this == obj) return true; if(obj == null) || (obj.getClass() != this.getClass()) return false; Point p = (Point) obj; return (p.x == this.x && p.y == this.y) }
Design Decisions equals and hashCode: possible problems: Example 20 Problems with sets and hash maps: public class Point{ private final int x; private final int y; public Point(int x, int y){ this.x = x; this.y = y; public boolean equals(Object obj){ if(this == obj) return true; if(obj == null) || (obj.getClass() != this.getClass()) return false; Point p = (Point) obj; return (p.x == this.x && p.y == this.y) public int hashCode(){ int hash = 7; hash = 31 * hash + this.x; hash = 31 * hash + this.y; return hash; }
Design Decisions equals and hashCode: general advice 21 equals: Do not change the type of the argument, it takes java.lang.Object Review your method to verify that it fulfills all the requirements stated by the general contract Do not forget to override hashCode method whenever you override equals method Primitives can be compared directly with equality operator (==) float to Float.floatToIntBits double to Double.doubleToLongBits instance of VS getClass Instance of checks if same class or subclass May break symmetry requirement Use only if class final getClass checks if same class exclusively
Design Decisions equals and hashCode: general advice 22 hashCode: Involve significant variables of your object in the calculation of the hash code Review your hashCode method and check if it is returning equal hash codes for equals objects Different hash codes for different objects is not mandatory, but advisable
Design Decisions equals method in ProGraMo 23 When should two objects be equals? When they refer to the same object? Shallow comparison When although they are different objects, their data members’ values are the same? Deep comparison
Design Decisions toString method 24 We should always override toString() Providing a good toString implementation makes a class much more pleasant to use toString method should return all of the interesting information contained in the object
Thanks! DECSAI ~ UGR “Effective Java”, by Joshua Bloch. Addison Wesley “Equals and HashCode”, by Manish Hatwalne “Junit 4 in 60 Seconds”, by cavdar.net