1 | /* |
2 | * JTiger Unit Testing Framework for J2SE 1.5 |
3 | * Copyright (C) 2005 Tony Morris |
4 | * |
5 | * This software is licenced under the |
6 | * Common Public Licence version 1.0 |
7 | * http://www.opensource.org/licenses/cpl1.0.php |
8 | * |
9 | * You received a copy of this licence with this software. |
10 | */ |
11 | package org.jtiger.assertion; |
12 | |
13 | import java.lang.reflect.Method; |
14 | import java.lang.reflect.InvocationTargetException; |
15 | |
16 | final class ComparableTesterFactory |
17 | { |
18 | private static final String COMPARE_TO = "compareTo"; |
19 | |
20 | private ComparableTesterFactory() |
21 | { |
22 | |
23 | } |
24 | |
25 | public static ComparableTester newComparableTester(final ObjectFactory<? extends java.lang.Comparable> factory) throws NullPointerException |
26 | { |
27 | if(factory == null) |
28 | { |
29 | throw new NullPointerException(); |
30 | } |
31 | |
32 | return new ComparableTesterImpl(factory); |
33 | } |
34 | |
35 | private static final class ComparableTesterImpl implements ComparableTester |
36 | { |
37 | private static Method compareTo; |
38 | |
39 | static |
40 | { |
41 | try |
42 | { |
43 | compareTo = java.lang.Comparable.class.getMethod(ComparableTesterFactory.COMPARE_TO, Object.class); |
44 | |
45 | if(!compareTo.isAccessible()) |
46 | { |
47 | compareTo.setAccessible(true); |
48 | } |
49 | } |
50 | catch(NoSuchMethodException e) |
51 | { |
52 | throw new AssertionException(e.getMessage(), e); |
53 | } |
54 | } |
55 | |
56 | private final ObjectFactory<? extends java.lang.Comparable> factory; |
57 | |
58 | ComparableTesterImpl(final ObjectFactory<? extends java.lang.Comparable> factory) |
59 | { |
60 | this.factory = factory; |
61 | } |
62 | |
63 | public boolean equalComparesToZero() throws AssertionException |
64 | { |
65 | final java.lang.Comparable<?> x = factory.newInstanceX(); |
66 | final java.lang.Comparable<?> y = factory.newInstanceY(); |
67 | |
68 | try |
69 | { |
70 | return ((Integer)compareTo.invoke(x, x)) == 0 && ((Integer)compareTo.invoke(y, y)) == 0; |
71 | } |
72 | catch(IllegalAccessException e) |
73 | { |
74 | throw new AssertionException(e.getMessage(), e); |
75 | } |
76 | catch(InvocationTargetException e) |
77 | { |
78 | throw new AssertionException(e.getMessage(), e); |
79 | } |
80 | } |
81 | |
82 | public boolean notEqualNotComparesToZero() throws AssertionException |
83 | { |
84 | final java.lang.Comparable<?> x = factory.newInstanceX(); |
85 | final java.lang.Comparable<?> y = factory.newInstanceY(); |
86 | |
87 | try |
88 | { |
89 | return ((Integer)compareTo.invoke(x, y)) != 0 && ((Integer)compareTo.invoke(y, x)) != 0; |
90 | } |
91 | catch(IllegalAccessException e) |
92 | { |
93 | throw new AssertionException(e.getMessage(), e); |
94 | } |
95 | catch(InvocationTargetException e) |
96 | { |
97 | throw new AssertionException(e.getMessage(), e); |
98 | } |
99 | } |
100 | } |
101 | } |