1 /*
2  * Copyright 2002-2010 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package org.springframework.mock.staticmock;
18 
19 import java.rmi.RemoteException;
20 
21 import javax.persistence.PersistenceException;
22 
23 import junit.framework.Assert;
24 
25 import org.junit.Ignore;
26 import org.junit.Test;
27 import org.springframework.mock.staticmock.AnnotationDrivenStaticEntityMockingControl;
28 import org.springframework.mock.staticmock.MockStaticEntityMethods;
29 
30 //Used because verification failures occur after method returns,
31 //so we can't test for them in the test case itself
32 @MockStaticEntityMethods
33 @Ignore // This isn't meant for direct testing; rather it is driven from AnnotationDrivenStaticEntityMockingControl
34 public class Delegate {
35 
36 	@Test
testArgMethodNoMatchExpectReturn()37 	public void testArgMethodNoMatchExpectReturn() {
38 		long id = 13;
39 		Person found = new Person();
40 		Person.findPerson(id);
41 		AnnotationDrivenStaticEntityMockingControl.expectReturn(found);
42 		AnnotationDrivenStaticEntityMockingControl.playback();
43 		Assert.assertEquals(found, Person.findPerson(id + 1));
44 	}
45 
46 	@Test
testArgMethodNoMatchExpectThrow()47 	public void testArgMethodNoMatchExpectThrow() {
48 		long id = 13;
49 		Person found = new Person();
50 		Person.findPerson(id);
51 		AnnotationDrivenStaticEntityMockingControl.expectThrow(new PersistenceException());
52 		AnnotationDrivenStaticEntityMockingControl.playback();
53 		Assert.assertEquals(found, Person.findPerson(id + 1));
54 	}
55 
56 	@Test
failTooFewCalls()57 	public void failTooFewCalls() {
58 		long id = 13;
59 		Person found = new Person();
60 		Person.findPerson(id);
61 		AnnotationDrivenStaticEntityMockingControl.expectReturn(found);
62 		Person.countPeople();
63 		AnnotationDrivenStaticEntityMockingControl.expectReturn(25);
64 		AnnotationDrivenStaticEntityMockingControl.playback();
65 		Assert.assertEquals(found, Person.findPerson(id));
66 	}
67 
68 	@Test
doesntEverReplay()69 	public void doesntEverReplay() {
70 		Person.countPeople();
71 	}
72 
73 	@Test
doesntEverSetReturn()74 	public void doesntEverSetReturn() {
75 		Person.countPeople();
76 		AnnotationDrivenStaticEntityMockingControl.playback();
77 	}
78 
79 	@Test
rejectUnexpectedCall()80 	public void rejectUnexpectedCall() {
81 		AnnotationDrivenStaticEntityMockingControl.playback();
82 		Person.countPeople();
83 	}
84 
85 	@Test(expected=RemoteException.class)
testVerificationFailsEvenWhenTestFailsInExpectedManner()86 	public void testVerificationFailsEvenWhenTestFailsInExpectedManner() throws RemoteException {
87 		Person.countPeople();
88 		AnnotationDrivenStaticEntityMockingControl.playback();
89 		// No calls to allow verification failure
90 		throw new RemoteException();
91 	}
92 }
93