1 15 package hivemind.test.services; 16 17 import java.io.IOException ; 18 import java.io.ObjectInput ; 19 import java.io.ObjectInputStream ; 20 import java.lang.reflect.Method ; 21 import java.sql.SQLException ; 22 23 import org.apache.hivemind.service.MethodSignature; 24 import org.apache.hivemind.test.HiveMindTestCase; 25 26 31 public class TestMethodSignature extends HiveMindTestCase 32 { 33 private MethodSignature find(Class sourceClass, String methodName) 34 { 35 Method [] methods = sourceClass.getMethods(); 36 37 for (int i = 0; i < methods.length; i++) 38 { 39 Method m = methods[i]; 40 41 if (m.getName().equals(methodName)) 42 return new MethodSignature(m); 43 } 44 45 unreachable(); 46 return null; 47 } 48 49 public void testEqualsAndHashCode() 50 { 51 MethodSignature m1 = find(Object .class, "hashCode"); 52 MethodSignature m2 = find(Boolean .class, "hashCode"); 53 54 assertEquals(m1.hashCode(), m2.hashCode()); 55 assertTrue(m1.equals(m2)); 56 57 m1 = find(String .class, "charAt"); 58 m2 = find(StringBuffer .class, "charAt"); 59 60 assertEquals(m1.hashCode(), m2.hashCode()); 61 assertTrue(m1.equals(m2)); 62 63 m1 = find(ObjectInput .class, "close"); 64 m2 = find(ObjectInputStream .class, "close"); 65 66 assertEquals(m1.hashCode(), m2.hashCode()); 67 assertTrue(m1.equals(m2)); 68 } 69 70 public void testEqualsAndHashCodeWithNulls() 71 { 72 MethodSignature m1 = new MethodSignature(void.class, "foo", null, null); 73 MethodSignature m2 = new MethodSignature(void.class, "foo", new Class [0], new Class [0]); 74 75 assertEquals(m1, m2); 76 assertEquals(m2, m1); 77 78 assertEquals(m1.hashCode(), m2.hashCode()); 79 } 80 81 82 83 public void testEqualsNameMismatch() 84 { 85 MethodSignature m1 = new MethodSignature(void.class, "foo", null, null); 86 MethodSignature m2 = new MethodSignature(void.class, "bar", null, null); 87 88 assertEquals(false, m1.equals(m2)); 89 } 90 91 92 public void testParametersMismatch() 93 { 94 MethodSignature m1 = new MethodSignature(void.class, "foo", new Class [] 95 { String .class }, null); 96 MethodSignature m2 = new MethodSignature(void.class, "foo", new Class [] 97 { Boolean .class }, null); 98 99 assertEquals(false, m1.equals(m2)); 100 } 101 102 103 104 public void testEqualsNull() 105 { 106 MethodSignature m1 = new MethodSignature(void.class, "foo", null, null); 107 108 assertEquals(false, m1.equals(null)); 109 } 110 111 112 113 public void testEqualsNonMethodSignature() 114 { 115 MethodSignature m1 = new MethodSignature(void.class, "foo", null, null); 116 117 assertEquals(false, m1.equals("Method Signature")); 118 } 119 120 public void testToString() 121 { 122 MethodSignature m = find(String .class, "getChars"); 123 124 assertEquals("void getChars(int, int, char[], int)", m.toString()); 125 126 m = find(Class .class, "newInstance"); 127 128 assertEquals( 129 "java.lang.Object newInstance() throws java.lang.InstantiationException, java.lang.IllegalAccessException", 130 m.toString()); 131 } 132 133 134 public void testGetUniqueId() 135 { 136 MethodSignature m = find(String .class, "getChars"); 137 138 assertEquals("getChars(int,int,char[],int)", m.getUniqueId()); 139 140 m = find(Class .class, "newInstance"); 141 142 assertEquals("newInstance()", m.getUniqueId()); 143 } 144 145 146 147 public void testOverridingSigTypeMismatch() 148 { 149 MethodSignature m1 = new MethodSignature(void.class, "foo", null, null); 150 MethodSignature m2 = new MethodSignature(int.class, "foo", null, null); 151 152 assertEquals(false, m1.isOverridingSignatureOf(m2)); 153 } 154 155 156 157 public void testOverridingSigNameMismatch() 158 { 159 MethodSignature m1 = new MethodSignature(void.class, "foo", null, null); 160 MethodSignature m2 = new MethodSignature(void.class, "bar", null, null); 161 162 assertEquals(false, m1.isOverridingSignatureOf(m2)); 163 } 164 165 166 167 public void testOverridingSigParametersMismatch() 168 { 169 MethodSignature m1 = new MethodSignature(void.class, "foo", null, null); 170 MethodSignature m2 = new MethodSignature(void.class, "foo", new Class [] 171 { String .class }, null); 172 173 assertEquals(false, m1.isOverridingSignatureOf(m2)); 174 } 175 176 177 178 public void testOverridingSig() 179 { 180 MethodSignature m1 = new MethodSignature(void.class, "close", null, new Class [] 181 { Exception .class }); 182 MethodSignature m2 = new MethodSignature(void.class, "close", null, new Class [] 183 { RuntimeException .class }); 184 185 assertEquals(true, m1.isOverridingSignatureOf(m2)); 186 assertEquals(false, m2.isOverridingSignatureOf(m1)); 187 } 188 189 194 public void testOverridingSigShortcut() 195 { 196 MethodSignature m1 = new MethodSignature(void.class, "close", null, null); 197 MethodSignature m2 = new MethodSignature(void.class, "close", null, new Class [] 198 { RuntimeException .class }); 199 200 assertEquals(false, m1.isOverridingSignatureOf(m2)); 201 assertEquals(true, m2.isOverridingSignatureOf(m1)); 202 } 203 204 209 public void testMultipleExceptionsToMatch() 210 { 211 MethodSignature m1 = new MethodSignature(void.class, "close", null, new Class [] 212 { SQLException .class, NumberFormatException .class }); 213 MethodSignature m2 = new MethodSignature(void.class, "close", null, new Class [] 214 { SQLException .class, IOException .class }); 215 216 assertEquals(false, m1.isOverridingSignatureOf(m2)); 217 assertEquals(false, m2.isOverridingSignatureOf(m1)); 218 } 219 } | Popular Tags |