1 /*
2  * Copyright (c) 2007 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 package org.mockito.internal.invocation;
6 
7 import org.junit.Before;
8 import org.junit.Test;
9 import org.mockitoutil.TestBase;
10 
11 import java.io.ByteArrayOutputStream;
12 import java.io.ObjectOutputStream;
13 import java.lang.reflect.Method;
14 
15 import static org.junit.Assert.*;
16 
17 
18 public class SerializableMethodTest extends TestBase {
19 
20     private MockitoMethod method;
21     private Method toStringMethod;
22     private Class<?>[] args;
23 
24     @Before
createMethodToTestWith()25     public void createMethodToTestWith() throws SecurityException, NoSuchMethodException {
26         args = new Class<?>[0];
27         toStringMethod = this.getClass().getMethod("toString", args);
28         method = new SerializableMethod(toStringMethod);
29     }
30 
31     @Test
shouldBeSerializable()32     public void shouldBeSerializable() throws Exception {
33         ByteArrayOutputStream serialized = new ByteArrayOutputStream();
34         new ObjectOutputStream(serialized).writeObject(method);
35     }
36 
37     @Test
shouldBeAbleToRetrieveMethodExceptionTypes()38     public void shouldBeAbleToRetrieveMethodExceptionTypes() throws Exception {
39         assertArrayEquals(toStringMethod.getExceptionTypes(), method.getExceptionTypes());
40     }
41 
42     @Test
shouldBeAbleToRetrieveMethodName()43     public void shouldBeAbleToRetrieveMethodName() throws Exception {
44         assertEquals(toStringMethod.getName(), method.getName());
45     }
46 
47     @Test
shouldBeAbleToCheckIsArgVargs()48     public void shouldBeAbleToCheckIsArgVargs() throws Exception {
49         assertEquals(toStringMethod.isVarArgs(), method.isVarArgs());
50     }
51 
52     @Test
shouldBeAbleToGetParameterTypes()53     public void shouldBeAbleToGetParameterTypes() throws Exception {
54         assertArrayEquals(toStringMethod.getParameterTypes(), method.getParameterTypes());
55     }
56 
57     @Test
shouldBeAbleToGetReturnType()58     public void shouldBeAbleToGetReturnType() throws Exception {
59         assertEquals(toStringMethod.getReturnType(), method.getReturnType());
60     }
61 
62     @Test
shouldBeEqualForTwoInstances()63     public void shouldBeEqualForTwoInstances() throws Exception {
64         assertTrue(new SerializableMethod(toStringMethod).equals(method));
65     }
66 
67     @Test
shouldNotBeEqualForSameMethodFromTwoDifferentClasses()68     public void shouldNotBeEqualForSameMethodFromTwoDifferentClasses() throws Exception {
69         Method testBaseToStringMethod = String.class.getMethod("toString", args);
70         assertFalse(new SerializableMethod(testBaseToStringMethod).equals(method));
71     }
72 
73     //TODO: add tests for generated equals() method
74 
75 }
76