KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > junitx > extensions > ComparabilityTestCase


1 /*
2  * The JUnit-addons Software License, Version 1.0
3  * (based on the Apache Software License, Version 1.1)
4  *
5  * Copyright (c) 2002-2003 Vladimir R. Bossicard. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if
20  * any, must include the following acknowlegement:
21  * "This product includes software developed by Vladimir R.
22  * Bossicard as well as other contributors
23  * (http://junit-addons.sourceforge.net/)."
24  * Alternately, this acknowlegement may appear in the software itself,
25  * if and wherever such third-party acknowlegements normally appear.
26  *
27  * 4. The name "JUnit-addons" must not be used to endorse or promote
28  * products derived from this software without prior written
29  * permission. For written permission, please contact
30  * vbossica@users.sourceforge.net.
31  *
32  * 5. Products derived from this software may not be called "JUnit-addons"
33  * nor may "JUnit-addons" appear in their names without prior written
34  * permission of the project managers.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ======================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals. For more information on the JUnit-addons Project, please
52  * see <http://junit-addons.sourceforge.net/>.
53  */

54
55 package junitx.extensions;
56
57 import junit.framework.AssertionFailedError;
58 import junit.framework.TestCase;
59 import junitx.framework.Assert;
60 import junitx.framework.ComparableAssert;
61
62 /**
63  * Extend me in order to test a class's functional compliance with the
64  * {@link java.lang.Comparable Comparable} interface.
65  * <p>
66  * Override my {@link #createLessInstance() createLessInstance},
67  * {@link #createEqualInstance() createEqualInstance}, and
68  * {@link #createGreaterInstance() createGreaterInstance} methods to
69  * provide me with objects to test against. These methods should return
70  * objects that are of the same class.
71  *
72  * @see java.lang.Comparable
73  * @version $Revision: 1.3 $ $Date: 2003/03/21 06:13:47 $
74  * @author <a HREF="mailto:pholser@yahoo.com">Paul Holser</a>
75  */

76 public abstract class ComparabilityTestCase
77         extends TestCase {
78
79     private Comparable JavaDoc less;
80     private Comparable JavaDoc equal1;
81     private Comparable JavaDoc equal2;
82     private Comparable JavaDoc greater;
83
84     /**
85      * Creates a new test.
86      *
87      * @param name name of the test
88      */

89     public ComparabilityTestCase(String JavaDoc name) {
90         super(name);
91     }
92
93     /**
94      * Creates and returns an instance of the class under test.
95      *
96      * @return a new instance of the class under test; each object returned
97      * from this method should compare less than the objects returned from
98      * {@link #createEqualInstance() createEqualInstance()}
99      * @throws Exception
100      */

101     protected abstract Comparable JavaDoc createLessInstance()
102             throws Exception JavaDoc;
103
104     /**
105      * Creates and returns an instance of the class under test.
106      *
107      * @return a new instance of the class under test; each object returned
108      * from this method should compare equal to each other
109      * @throws Exception
110      */

111     protected abstract Comparable JavaDoc createEqualInstance()
112             throws Exception JavaDoc;
113
114     /**
115      * Creates and returns an instance of the class under test.
116      *
117      * @return a new instance of the class under test; each object returned
118      * from this method should compare greater than the objects returned from
119      * {@link #createEqualInstance() createEqualInstance()}
120      * @throws Exception
121      */

122     protected abstract Comparable JavaDoc createGreaterInstance()
123             throws Exception JavaDoc;
124
125     /**
126      * Sets up the test fixture.
127      *
128      * @throws Exception
129      */

130     protected void setUp()
131             throws Exception JavaDoc {
132         super.setUp();
133
134         less = createLessInstance();
135         equal1 = createEqualInstance();
136         equal2 = createEqualInstance();
137         greater = createGreaterInstance();
138
139         // We want these assertions to yield errors, not failures.
140
try {
141             assertNotNull("createLessInstance() returned null", less);
142             assertNotNull("createEqualInstance() returned null", equal1);
143             assertNotNull("2nd createEqualInstance() returned null", equal2);
144             assertNotNull("createGreaterInstance() returned null", greater);
145
146             assertEquals("less and equal1 of different classes",
147                     less.getClass(),
148                     equal1.getClass());
149             assertEquals("less and equal2 of different classes",
150                     less.getClass(),
151                     equal2.getClass());
152             assertEquals("less and greater of different classes",
153                     less.getClass(),
154                     greater.getClass());
155
156             checkForEquality(equal1, equal2);
157         } catch (AssertionFailedError ex) {
158             throw new IllegalArgumentException JavaDoc(ex.getMessage());
159         }
160     }
161
162     /**
163      * Override as a no-op if you do not require that
164      * {@link #createEqualInstance() createEqualInstance()} return distinct but
165      * equivalent objects.
166      */

167     protected void checkForEquality(Comparable JavaDoc c1,
168                                       Comparable JavaDoc c2) {
169         Assert.assertNotSame("1st equal instance same as 2nd", equal1, equal2);
170         assertEquals("1st equal not equal to 2nd", equal1, equal2);
171     }
172
173     /**
174      * Tests whether <code>sgn(x.compareTo(y)) == -sgn(y.compareTo(x))</code>
175      * for all <code>x</code> and <code>y</code> given to this test.
176      */

177     public final void testForReverseSigns() {
178         assertEquals(
179                 "less vs. equal1",
180                 sgn(less.compareTo(equal1)),
181                 -sgn(equal1.compareTo(less)));
182         assertEquals(
183                 "less vs. equal2",
184                 sgn(less.compareTo(equal2)),
185                 -sgn(equal2.compareTo(less)));
186         assertEquals(
187                 "less vs. greater",
188                 sgn(less.compareTo(greater)),
189                 -sgn(greater.compareTo(less)));
190         assertEquals(
191                 "equal1 vs. equal2",
192                 sgn(equal1.compareTo(equal2)),
193                 -sgn(equal2.compareTo(equal1)));
194         assertEquals(
195                 "equal1 vs. greater",
196                 sgn(equal1.compareTo(greater)),
197                 -sgn(greater.compareTo(equal1)));
198         assertEquals(
199                 "equal2 vs. greater",
200                 sgn(equal2.compareTo(greater)),
201                 -sgn(greater.compareTo(equal2)));
202     }
203
204     /**
205      * Tests whether <code>sgn(x.compareTo(z)) == sgn(y.compareTo(z))</code>
206      * for all <code>z</code> when <code>x.compareTo(y) == 0</code>.
207      */

208     public final void testForSameSigns() {
209         assertEquals(
210                 "equal vs. less",
211                 sgn(equal1.compareTo(less)),
212                 sgn(equal2.compareTo(less)));
213         assertEquals(
214                 "equal vs. greater",
215                 sgn(equal1.compareTo(greater)),
216                 sgn(equal2.compareTo(greater)));
217     }
218
219     /**
220      * Tests for sensible return values from the class under test's
221      * <code>compareTo</code> method. Doing so effectively tests the
222      * transitivity of <code>compareTo</code> also--
223      * <code>(x.compareTo(y)>0 && y.compareTo(z)>0)</code> implies
224      * <code>x.compareTo(z)>0</code>.
225      */

226     public final void testReturnValues() {
227         ComparableAssert.assertLesser(equal1, less);
228         ComparableAssert.assertLesser(equal2, less);
229         ComparableAssert.assertGreater(less, greater);
230         ComparableAssert.assertEquals(equal1, equal2);
231         ComparableAssert.assertGreater(equal1, greater);
232         ComparableAssert.assertGreater(equal2, greater);
233     }
234
235     /**
236      * Tests whether <code>compareTo</code> throws a ClassCastException when
237      * appropriate.
238      */

239     public final void testForClassCastException() throws Exception JavaDoc {
240         try {
241             less.compareTo(new Object JavaDoc());
242         } catch (ClassCastException JavaDoc success) {
243             return;
244         }
245         fail("should have thrown ClassCastException");
246     }
247
248     private int sgn(int x) {
249         return (x == 0) ? 0 : (x / Math.abs(x));
250     }
251
252 }
253
Popular Tags