001: package net.sf.mockcreator;
002:
003: import java.lang.reflect.Constructor;
004: import java.lang.reflect.Method;
005:
006: import net.sf.mockcreator.codegeneration.MockDescriptor;
007:
008: public class HashCodeTest extends TestCase {
009: public HashCodeTest(String name) {
010: super (name);
011: }
012:
013: public void testMethodParametersOrder() throws Exception {
014: long hash1 = getMethodHash("I1", "foo", new Class[] {
015: String.class, int.class });
016: long hash2 = getMethodHash("I2", "foo", new Class[] {
017: int.class, String.class });
018:
019: assertFalse(hash1 == hash2);
020: }
021:
022: public void testConstructorParametersOrder() throws Exception {
023: String className1 = (HashCodeTest.class.getName() + "$C1")
024: .replace('/', '.');
025: Class a1 = Class.forName(className1);
026: Constructor foo1 = a1.getConstructor(new Class[] {
027: String.class, int.class });
028:
029: long hashI1 = MockDescriptor.hashCode("SameC", foo1
030: .getModifiers(), null, foo1.getParameterTypes(), foo1
031: .getExceptionTypes());
032:
033: String className2 = (HashCodeTest.class.getName() + "$C2")
034: .replace('/', '.');
035: Class a2 = Class.forName(className2);
036: Constructor foo2 = a2.getConstructor(new Class[] { int.class,
037: String.class });
038:
039: long hashI2 = MockDescriptor.hashCode("SameC", foo2
040: .getModifiers(), null, foo2.getParameterTypes(), foo2
041: .getExceptionTypes());
042:
043: assertFalse(hashI1 == hashI2);
044: }
045:
046: public void testMethodModifiers() throws Exception {
047: long hash1 = getMethodHash("M1", "foo", null);
048: long hash2 = getMethodHash("M2", "foo", null);
049: long hash3 = getMethodHash("M3", "foo", null);
050:
051: assertFalse(hash1 == hash2);
052: assertFalse(hash2 == hash3);
053: assertFalse(hash3 == hash1);
054: }
055:
056: public void testMethodExceptions() throws Exception {
057: long hash1 = getMethodHash("E1", "foo", null);
058: long hash2 = getMethodHash("E2", "foo", null);
059: long hash3 = getMethodHash("E3", "foo", null);
060:
061: assertFalse(hash1 == hash2);
062: assertFalse(hash2 == hash3);
063: assertFalse(hash3 == hash1);
064: }
065:
066: public void testMethodReturn() throws Exception {
067: long hash1 = getMethodHash("R1", "foo", null);
068: long hash2 = getMethodHash("R2", "foo", null);
069:
070: assertFalse(hash1 == hash2);
071: }
072:
073: private long getMethodHash(String innerClass, String methodName,
074: Class[] params) throws Exception {
075: String className = (HashCodeTest.class.getName() + "$" + innerClass)
076: .replace('/', '.');
077: Class cls = Class.forName(className);
078: Method mth = cls.getDeclaredMethod(methodName,
079: (params == null) ? new Class[] {} : params);
080:
081: return MockDescriptor.hashCode(mth.getName(), mth
082: .getModifiers(), mth.getReturnType(), mth
083: .getParameterTypes(), mth.getExceptionTypes());
084: }
085:
086: public static interface I1 {
087: void foo(String a, int b);
088: }
089:
090: public static interface I2 {
091: void foo(int b, String a);
092: }
093:
094: public static class C1 {
095: public C1(String a, int b) {
096: }
097: }
098:
099: public static class C2 {
100: public C2(int b, String a) {
101: }
102: }
103:
104: public static class M1 {
105: public void foo() {
106: }
107: }
108:
109: public static class M2 {
110: protected void foo() {
111: }
112: }
113:
114: public static class M3 {
115: protected static void foo() {
116: }
117: }
118:
119: public static class E1 {
120: public void foo() throws IllegalStateException {
121: }
122: }
123:
124: public static class E2 {
125: public void foo() throws IllegalArgumentException {
126: }
127: }
128:
129: public static class E3 {
130: public void foo() {
131: }
132: }
133:
134: public static class R1 {
135: public void foo() {
136: }
137: }
138:
139: public static class R2 {
140: public String foo() {
141: return null;
142: }
143: }
144: }
|