| 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.EqualsMethodContractTesterFactory.newEqualsMethodContractTester; |
| 14 | |
| 15 | /** |
| 16 | * Makes assertions on the general contract of the |
| 17 | * <a href="%j2se.api.spec%/java/lang/Object.htmll#equals(java.lang.Object)">java.lang.Object equals 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 HashCodeMethodContract |
| 24 | * @see Object#equals(Object) |
| 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 EqualsMethodContract |
| 31 | { |
| 32 | private EqualsMethodContract() |
| 33 | { |
| 34 | |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Asserts that the given <code>ObjectFactory</code> returns instances that meet one aspect of the equals method |
| 39 | * general contract. |
| 40 | * The <code>equals</code> method must return <code>false</code> for a <code>null</code> argument. |
| 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 equals method general contract. |
| 46 | */ |
| 47 | public static void assertEqualsMethodUnequalToNull(final ObjectFactory<?> factory, final Object... message) throws AssertionException |
| 48 | { |
| 49 | final EqualsMethodContractTester tester = newEqualsMethodContractTester(factory); |
| 50 | |
| 51 | if(!tester.isUnequalToNull()) |
| 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 equals method |
| 59 | * general contract. |
| 60 | * The <code>equals</code> method must consistently return <code>true</code> or <code>false</code> given the same |
| 61 | * argument when called on the same instance. |
| 62 | * |
| 63 | * @param factory The factory to use to return instances for asserting the aspect of the contract. |
| 64 | * @param message The assertion message. |
| 65 | * @throws AssertionException If the given <code>ObjectFactory</code> returns instances that do not meet the aspect |
| 66 | * of the equals method general contract. |
| 67 | */ |
| 68 | public static void assertEqualsMethodConsistentAcrossInvocations(final ObjectFactory<?> factory, final Object... message) throws AssertionException |
| 69 | { |
| 70 | final EqualsMethodContractTester tester = newEqualsMethodContractTester(factory); |
| 71 | |
| 72 | if(!tester.isConsistentAcrossInvocations()) |
| 73 | { |
| 74 | throw new AssertionException(message); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Asserts that the given <code>ObjectFactory</code> returns instances that meet one aspect of the equals method |
| 80 | * general contract. |
| 81 | * The <code>equals</code> method must return <code>true</code> when given an argument that refers to the same instance |
| 82 | * on which the method is called. |
| 83 | * |
| 84 | * @param factory The factory to use to return instances for asserting the aspect of the contract. |
| 85 | * @param message The assertion message. |
| 86 | * @throws AssertionException If the given <code>ObjectFactory</code> returns instances that do not meet the aspect |
| 87 | * of the equals method general contract. |
| 88 | */ |
| 89 | public static void assertEqualsMethodReflexive(final ObjectFactory<?> factory, final Object... message) throws AssertionException |
| 90 | { |
| 91 | final EqualsMethodContractTester tester = newEqualsMethodContractTester(factory); |
| 92 | |
| 93 | if(!tester.isReflexive()) |
| 94 | { |
| 95 | throw new AssertionException(message); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Asserts that the given <code>ObjectFactory</code> returns instances that meet one aspect of the equals method |
| 101 | * general contract. |
| 102 | * The <code>equals</code> method must be symmetric. |
| 103 | * |
| 104 | * @param factory The factory to use to return instances for asserting the aspect of the contract. |
| 105 | * @param message The assertion message. |
| 106 | * @throws AssertionException If the given <code>ObjectFactory</code> returns instances that do not meet the aspect |
| 107 | * of the equals method general contract. |
| 108 | */ |
| 109 | public static void assertEqualsMethodSymmetric(final ObjectFactory<?> factory, final Object... message) throws AssertionException |
| 110 | { |
| 111 | final EqualsMethodContractTester tester = newEqualsMethodContractTester(factory); |
| 112 | |
| 113 | if(!tester.isSymmetric()) |
| 114 | { |
| 115 | throw new AssertionException(message); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Asserts that the given <code>ObjectFactory</code> returns instances that meet one aspect of the equals method |
| 121 | * general contract. |
| 122 | * The <code>equals</code> method must be transitive. |
| 123 | * |
| 124 | * @param factory The factory to use to return instances for asserting the aspect of the contract. |
| 125 | * @param message The assertion message. |
| 126 | * @throws AssertionException If the given <code>ObjectFactory</code> returns instances that do not meet the aspect |
| 127 | * of the equals method general contract. |
| 128 | */ |
| 129 | public static void assertEqualsMethodTransitive(final ObjectFactory<?> factory, final Object... message) throws AssertionException |
| 130 | { |
| 131 | final EqualsMethodContractTester tester = newEqualsMethodContractTester(factory); |
| 132 | |
| 133 | if(!tester.isTransitive()) |
| 134 | { |
| 135 | throw new AssertionException(message); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Asserts that the given <code>ObjectFactory</code> returns instances that meet one aspect of the equals method |
| 141 | * general contract. |
| 142 | * For an instance that has defined unequal semantics, the <code>equals</code> method must be consistently return |
| 143 | * <code>false</code>. |
| 144 | * |
| 145 | * @param factory The factory to use to return instances for asserting the aspect of the contract. |
| 146 | * @param message The assertion message. |
| 147 | * @throws AssertionException If the given <code>ObjectFactory</code> returns instances that do not meet the aspect |
| 148 | * of the equals method general contract. |
| 149 | */ |
| 150 | public static void assertEqualsMethodUnequalToUnequalInstance(final ObjectFactory<?> factory, final Object... message) throws AssertionException |
| 151 | { |
| 152 | final EqualsMethodContractTester tester = newEqualsMethodContractTester(factory); |
| 153 | |
| 154 | if(!tester.isUnequalToUnequalInstance()) |
| 155 | { |
| 156 | throw new AssertionException(message); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Asserts that the given <code>ObjectFactory</code> returns instances that meet the entire equals method general |
| 162 | * contract. |
| 163 | * The contract is defined by |
| 164 | * <a href="%j2se.api.spec%/java/lang/Object.htmll#equals(java.lang.Object)">the java.lang.Object equals method</a>. |
| 165 | * |
| 166 | * @param factory The factory to use to return instances for asserting the entire general contract. |
| 167 | * @param message The assertion message. |
| 168 | * @throws AssertionException If the given <code>ObjectFactory</code> returns instances that do not meet the entire |
| 169 | * equals method general contract. |
| 170 | */ |
| 171 | public static void assertEqualsMethodFillsContract(final ObjectFactory<?> factory, final Object... message) throws AssertionException |
| 172 | { |
| 173 | final EqualsMethodContractTester tester = newEqualsMethodContractTester(factory); |
| 174 | |
| 175 | if(!tester.fillsContract()) |
| 176 | { |
| 177 | throw new AssertionException(message); |
| 178 | } |
| 179 | } |
| 180 | } |