1 package com.eireneh.eliza.util; 2 3 import java.lang.reflect.Method ; 4 5 import junit.framework.TestCase; 6 7 13 public class OverloadUtilTest extends TestCase 14 { 15 private static final String METHOD = "method"; 16 17 private static final String ANOTHER_METHOD = "anotherMethod"; 18 19 private static final Class [] NO_PARAMETERS = null; 20 21 private Method returnsStringNoParameters = getMethod(ConcreteClass.class, METHOD, NO_PARAMETERS); 23 24 private Method returnsStringOneParameter = getMethod(ConcreteClass.class, METHOD, String .class); 25 26 private Method returnsObjectNoParametersDifferentName = getMethod(ConcreteClass.class, ANOTHER_METHOD, NO_PARAMETERS); 27 28 private Method returnsStringNoParametersOnSubClass = getMethod(SubClass.class, METHOD, NO_PARAMETERS); 29 30 private Method returnsObjectThreeParametersOnSubClass = getMethod(SubClass.class, METHOD, String .class, String .class, String .class); 31 32 private Method returnsObjectNoParametersOnInterfaceType = getMethod(Interface.class, METHOD, NO_PARAMETERS); 33 34 39 public void testTwoSimpleOverloadedMethodsOnSameClass() throws Exception 40 { 41 assertTrue(OverloadUtil.isOverloaded(returnsStringNoParameters, returnsStringOneParameter)); 42 assertTrue(OverloadUtil.isOverloaded(returnsStringOneParameter, returnsStringNoParameters)); 43 } 44 45 public void testMoreSpecificMethodOnConcreteClassOverloadsInterfaceMethods() throws Exception 46 { 47 assertTrue(OverloadUtil.isOverloaded(returnsObjectNoParametersOnInterfaceType, returnsStringOneParameter)); 48 } 49 50 public void testInterfaceImplementationIsNotOverloading() throws Exception 51 { 52 assertFalse(OverloadUtil.isOverloaded(returnsObjectNoParametersOnInterfaceType, returnsStringNoParameters)); 53 } 54 55 60 public void testIsNotOverloadingForDifferentNamesOnSameClass() throws Exception 61 { 62 assertFalse(OverloadUtil.isOverloaded(returnsStringNoParameters, returnsObjectNoParametersDifferentName)); 63 assertFalse(OverloadUtil.isOverloaded(returnsStringOneParameter, returnsObjectNoParametersDifferentName)); 64 } 65 66 public void testIsNotOverloadingForDifferentNamesInterfaceAgainstConcrete() throws Exception 67 { 68 assertFalse(OverloadUtil.isOverloaded(returnsObjectNoParametersOnInterfaceType, returnsObjectNoParametersDifferentName)); 69 } 70 71 public void testOverridingIsNotOverloading() throws Exception 72 { 73 assertFalse(OverloadUtil.isOverloaded(returnsStringNoParameters, returnsStringNoParametersOnSubClass)); 74 } 75 76 public void testMethodOnSubclassOverloadsInterfaceMethod() throws Exception 77 { 78 assertTrue(OverloadUtil.isOverloaded(returnsObjectNoParametersOnInterfaceType, returnsObjectThreeParametersOnSubClass)); 79 } 80 81 86 public void testMethodOnSubclassOverloadsMethodOnSameClass() throws Exception 87 { 88 assertTrue(OverloadUtil.isOverloaded(returnsStringNoParametersOnSubClass, returnsObjectThreeParametersOnSubClass)); 89 assertTrue(OverloadUtil.isOverloaded(returnsObjectThreeParametersOnSubClass, returnsStringNoParametersOnSubClass)); 90 } 91 92 public void testMethodOnSubclassOverloadsMethodOnSuperClass() throws Exception 93 { 94 assertTrue(OverloadUtil.isOverloaded(returnsStringOneParameter, returnsObjectThreeParametersOnSubClass)); 95 } 96 97 public void testMethodOnSubclassDoesNotOverloadMethodOfDifferentNameOnSuperClass() throws Exception 98 { 99 assertFalse(OverloadUtil.isOverloaded(returnsObjectNoParametersDifferentName, returnsObjectThreeParametersOnSubClass)); 100 } 101 102 110 private static Method getMethod(Class clazz, String name, Class ... parameterTypes) 111 { 112 Method method = null; 113 try 114 { 115 method = clazz.getMethod(name, parameterTypes); 116 } 117 catch (Exception e) 118 { 119 e.printStackTrace(); 120 } 121 return method; 122 } 123 124 } 125 126 interface Interface 127 { 128 public Object method(); 129 } 130 131 class ConcreteClass implements Interface 132 { 133 134 138 public String method() 139 { 140 return null; 141 } 142 143 148 public String method(String one) 149 { 150 return null; 151 } 152 153 public Object anotherMethod() 154 { 155 return null; 156 } 157 } 158 159 class SubClass extends ConcreteClass 160 { 161 164 public String method() 165 { 166 return null; 167 } 168 169 public Object method(String one, String two, String three) 170 { 171 return null; 172 } 173 } 174 | Popular Tags |