| 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 static org.jtiger.assertion.HashCodeMethodContractTesterFactory.newHashCodeMethodContractTester; |
| 14 | |
| 15 | /** |
| 16 | * Makes assertions on the general contract of the |
| 17 | * <a href="%j2se.api.spec%/java/lang/Object.htmll#hashCode()">java.lang.Object hashCode method</a>. |
| 18 | * This requires the creation of an instance of {@link ObjectFactory} to return instances to use to test. |
| 19 | * The given {@link ObjectFactory} must meet the its own general contract. |
| 20 | * |
| 21 | * @see ObjectFactory |
| 22 | * @see ObjectFactoryContract |
| 23 | * @see EqualsMethodContract |
| 24 | * @see Object#hashCode() |
| 25 | * @author %javadoc.author.tag% |
| 26 | * @version %version%<br/> |
| 27 | * <i>Build Number %build.number%</i><br/> |
| 28 | * <i>Build Time %build.time% CET (GMT + 1)</i> |
| 29 | */ |
| 30 | public final class HashCodeMethodContract |
| 31 | { |
| 32 | private HashCodeMethodContract() |
| 33 | { |
| 34 | |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Asserts that the given <code>ObjectFactory</code> returns instances that meet one aspect of the hashCode method |
| 39 | * general contract. |
| 40 | * The <code>hashCode</code> method must consistently return the same value over multiple invocations. |
| 41 | * |
| 42 | * @param factory The factory to use to return instances for asserting the aspect of the contract. |
| 43 | * @param message The assertion message. |
| 44 | * @throws AssertionException If the given <code>ObjectFactory</code> returns instances that do not meet the aspect |
| 45 | * of the hashCode method general contract. |
| 46 | */ |
| 47 | public static void assertHashCodeMethodConsistentResult(final ObjectFactory<?> factory, final Object... message) throws AssertionException |
| 48 | { |
| 49 | final HashCodeMethodContractTester tester = newHashCodeMethodContractTester(factory); |
| 50 | |
| 51 | if(!tester.isConsistentResult()) |
| 52 | { |
| 53 | throw new AssertionException(message); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Asserts that the given <code>ObjectFactory</code> returns instances that meet one aspect of the hashCode method |
| 59 | * general contract. |
| 60 | * The <code>hashCode</code> method must return the same value for instances that are equal |
| 61 | * according to their equals method implementation. |
| 62 | * |
| 63 | * @see Object#equals(Object) |
| 64 | * @param factory The factory to use to return instances for asserting the aspect of the contract. |
| 65 | * @param message The assertion message. |
| 66 | * @throws AssertionException If the given <code>ObjectFactory</code> returns instances that do not meet the aspect |
| 67 | * of the hashCode method general contract. |
| 68 | */ |
| 69 | public static void assertHashCodeMethodEqualOnEqualInstance(final ObjectFactory<?> factory, final Object... message) throws AssertionException |
| 70 | { |
| 71 | final HashCodeMethodContractTester tester = newHashCodeMethodContractTester(factory); |
| 72 | |
| 73 | if(!tester.isEqualOnEqualInstance()) |
| 74 | { |
| 75 | throw new AssertionException(message); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Asserts that the given <code>ObjectFactory</code> returns instances that meet the entire hashCode method general |
| 81 | * contract. |
| 82 | * The contract is defined by |
| 83 | * <a href="%j2se.api.spec%/java/lang/Object.htmll#hashCode()">the java.lang.Object hashCode method</a>. |
| 84 | * |
| 85 | * @param factory The factory to use to return instances for asserting the entire general contract. |
| 86 | * @param message The assertion message. |
| 87 | * @throws AssertionException If the given <code>ObjectFactory</code> returns instances that do not meet the entire |
| 88 | * hashCode method general contract. |
| 89 | */ |
| 90 | public static void assertHashCodeMethodFillsContract(final ObjectFactory<?> factory, final Object... message) throws AssertionException |
| 91 | { |
| 92 | final HashCodeMethodContractTester tester = newHashCodeMethodContractTester(factory); |
| 93 | |
| 94 | if(!tester.fillsContract()) |
| 95 | { |
| 96 | throw new AssertionException(message); |
| 97 | } |
| 98 | } |
| 99 | } |