1 /* 2 * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package test.com.sun.javafx.test; 27 28 import static org.junit.Assert.assertEquals; 29 import static org.junit.Assert.assertFalse; 30 import static org.junit.Assert.assertNotSame; 31 import static org.junit.Assert.fail; 32 33 import org.junit.Test; 34 35 public abstract class ObjectMethodsTestBase { 36 private final Configuration configuration; 37 38 public ObjectMethodsTestBase(final Configuration configuration) { 39 this.configuration = configuration; 40 } 41 42 @Test 43 public void testEquals() { 44 configuration.equalsTest(); 45 } 46 47 @Test 48 public void testHashCode() { 49 configuration.hashCodeTest(); 50 } 51 52 @Test 53 public void testToString() { 54 configuration.toStringTest(); 55 } 56 57 public static Object[] equalObjects(final Object... objects) { 58 return config(new Configuration(EQUAL_OBJECTS_EQUALS_TEST, 59 EQUAL_OBJECTS_HASHCODE_TEST, 60 EQUAL_OBJECTS_TOSTRING_TEST, 61 objects)); 62 } 63 64 public static Object[] differentObjects( 65 final Object... objects) { 66 return config(new Configuration(DIFFERENT_OBJECTS_EQUALS_TEST, 67 DIFFERENT_OBJECTS_HARD_HASHCODE_TEST, 68 DIFFERENT_OBJECTS_TOSTRING_TEST, 69 objects)); 70 } 71 72 public static Object[] differentObjectsEasyHashcode( 73 final Object... objects) { 74 return config(new Configuration(DIFFERENT_OBJECTS_EQUALS_TEST, 75 DIFFERENT_OBJECTS_EASY_HASHCODE_TEST, 76 DIFFERENT_OBJECTS_TOSTRING_TEST, 77 objects)); 78 } 79 80 public static Object[] differentObjectsMediumHashcode( 81 final Object... objects) { 82 return config(new Configuration(DIFFERENT_OBJECTS_EQUALS_TEST, 83 DIFFERENT_OBJECTS_MEDIUM_HASHCODE_TEST, 84 DIFFERENT_OBJECTS_TOSTRING_TEST, 85 objects)); 86 } 87 88 public static Object[] config(final Configuration configuration) { 89 return new Object[] { configuration }; 90 } 91 92 public static final class Configuration { 93 private final Object[] objects; 94 95 private final TestInstance equalsTest; 96 97 private final TestInstance hashCodeTest; 98 99 private final TestInstance toStringTest; 100 101 public Configuration(final TestInstance equalsTest, 102 final TestInstance hashCodeTest, 103 final TestInstance toStringTest, 104 final Object... objects) { 105 this.equalsTest = equalsTest; 106 this.hashCodeTest = hashCodeTest; 107 this.toStringTest = toStringTest; 108 this.objects = objects; 109 } 110 111 public void equalsTest() { 112 if (equalsTest != null) { 113 equalsTest.test(objects); 114 } 115 } 116 117 public void hashCodeTest() { 118 if (hashCodeTest != null) { 119 hashCodeTest.test(objects); 120 } 121 } 122 123 public void toStringTest() { 124 if (toStringTest != null) { 125 toStringTest.test(objects); 126 } 127 } 128 } 129 130 public interface TestInstance { 131 void test(Object[] objects); 132 } 133 134 public static final TestInstance EQUAL_OBJECTS_EQUALS_TEST = 135 objects -> { 136 for (int i = 0; i < objects.length; ++i) { 137 for (int j = 0; j < objects.length; ++j) { 138 assertEquals(objects[i], objects[j]); 139 } 140 assertFalse(objects[i].equals(null)); 141 } 142 }; 143 144 public static final TestInstance EQUAL_OBJECTS_HASHCODE_TEST = 145 objects -> { 146 for (int i = 0; i < objects.length; ++i) { 147 for (int j = 0; j < objects.length; ++j) { 148 assertEquals(objects[i].hashCode(), 149 objects[j].hashCode()); 150 } 151 } 152 }; 153 154 public static final TestInstance EQUAL_OBJECTS_TOSTRING_TEST = 155 objects -> { 156 for (int i = 0; i < objects.length; ++i) { 157 for (int j = 0; j < objects.length; ++j) { 158 assertEquals(objects[i].toString(), 159 objects[j].toString()); 160 } 161 } 162 }; 163 164 public static final TestInstance DIFFERENT_OBJECTS_EQUALS_TEST = 165 objects -> { 166 for (int i = 0; i < objects.length; ++i) { 167 for (int j = 0; j < objects.length; ++j) { 168 if (i != j) { 169 assertFalse(objects[i].equals(objects[j])); 170 } 171 } 172 assertFalse(objects[i].equals(null)); 173 } 174 }; 175 176 public static final TestInstance DIFFERENT_OBJECTS_EASY_HASHCODE_TEST = 177 objects -> { 178 // if objects are different, their hashcodes can return the 179 // same or different values 180 }; 181 182 public static final TestInstance DIFFERENT_OBJECTS_MEDIUM_HASHCODE_TEST = 183 objects -> { 184 // we require that at least one tested object returns a 185 // different hash code value 186 final int firstHashCodeValue = objects[0].hashCode(); 187 for (int i = 1; i < objects.length; ++i) { 188 if (objects[i].hashCode() != firstHashCodeValue) { 189 return; 190 } 191 } 192 193 // all the hash codes are same, this violates our criteria 194 fail(); 195 }; 196 197 public static final TestInstance DIFFERENT_OBJECTS_HARD_HASHCODE_TEST = 198 objects -> { 199 // we require that at all tested objects returns a different 200 // hash code value 201 for (int i = 0; i < objects.length; ++i) { 202 for (int j = 0; j < objects.length; ++j) { 203 if (i != j) { 204 assertNotSame(objects[i].hashCode(), 205 objects[j].hashCode()); 206 } 207 } 208 } 209 }; 210 211 public static final TestInstance DIFFERENT_OBJECTS_TOSTRING_TEST = 212 objects -> { 213 for (int i = 0; i < objects.length; ++i) { 214 for (int j = 0; j < objects.length; ++j) { 215 if (i != j) { 216 assertFalse(objects[i].toString().equals( 217 objects[j].toString())); 218 } 219 } 220 } 221 }; 222 }