View Javadoc

1   package org.itracker;
2   
3   import java.util.Comparator;
4   
5   
6   
7   import org.apache.log4j.Logger;
8   import org.itracker.model.Entity;
9   
10  /**
11   * Itracker specific assertions for include static to test-classes.
12   * 
13   */
14  public class Assert extends junit.framework.Assert {
15  
16  	private static final Logger log = Logger.getLogger(Assert.class);
17  	
18  	/**
19  	 * asserts true when comparator compares for lhs being < rhs and rhs > lhs.
20  	 * 
21  	 * @param message
22  	 * @param comparator
23  	 * @param lhs
24  	 * @param rhs
25  	 */
26  	@SuppressWarnings("unchecked")
27  	public static void assertEntityComparator(String message, Comparator comparator, Entity lhs, Entity rhs)  {
28  		if (log.isDebugEnabled()) {
29  			log.debug("assertEntityComparator: " + message + " " + comparator);
30  		}
31  		if (null == lhs) {
32  			throw new IllegalArgumentException("lhs must not both be null.");
33  		}
34  		// test nullpointer
35  		
36  		if (null == rhs) {
37  
38  			try {
39  				assertNull(message + " lhs, null: " + comparator, comparator.compare(lhs, rhs));
40  				fail();
41  			} catch (NullPointerException npe) {} // ok
42  			
43  			try {
44  				assertNull(message  + " null, lhs: " + comparator, comparator.compare(rhs, lhs));
45  				fail();
46  			} catch (NullPointerException npe) {} // ok
47  			
48  			return;
49  		}
50  		
51  		assertTrue(message + " lhs, rhs: " + comparator, 0 > comparator.compare(lhs, rhs));
52  		assertTrue(message + " rhs, lhs: " + comparator, 0 < comparator.compare(rhs, lhs));
53  	}
54  	
55  	/**
56  	 * asserts true when comparator compares for lhs being equal to rhs and rhs equal lhs.
57  	 * 
58  	 * @param message
59  	 * @param comparator
60  	 * @param lhs
61  	 * @param rhs
62  	 */
63  
64  	@SuppressWarnings("unchecked")
65  	public static void assertEntityComparatorEquals(String message, Comparator comparator, Entity lhs, Entity rhs) {
66  		if (log.isDebugEnabled()) {
67  			log.debug("assertEntityComparatorEquals: " + message + " " + comparator);
68  		}
69  		if (null == lhs || null == rhs) {
70  			throw new IllegalArgumentException("rhs and lhs must not be null.");
71  		}
72  		
73  		assertTrue(message + " lhs, rhs: " + comparator, 0 == comparator.compare(lhs, rhs));
74  		assertTrue(message + " rhs, lhs: " + comparator, 0 == comparator.compare(rhs, lhs));
75  	}
76  }