KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > jdo > JdoInterceptorTests


1 /*
2  * Copyright 2002-2005 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.orm.jdo;
18
19 import java.lang.reflect.AccessibleObject JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21
22 import javax.jdo.PersistenceManager;
23 import javax.jdo.PersistenceManagerFactory;
24
25 import junit.framework.TestCase;
26
27 import org.aopalliance.intercept.Interceptor;
28 import org.aopalliance.intercept.Invocation;
29 import org.aopalliance.intercept.MethodInvocation;
30 import org.easymock.MockControl;
31 import org.springframework.transaction.support.TransactionSynchronizationManager;
32
33 /**
34  * @author Juergen Hoeller
35  */

36 public class JdoInterceptorTests extends TestCase {
37
38     public void testInterceptor() {
39         MockControl pmfControl = MockControl.createControl(PersistenceManagerFactory.class);
40         PersistenceManagerFactory pmf = (PersistenceManagerFactory) pmfControl.getMock();
41         MockControl pmControl = MockControl.createControl(PersistenceManager.class);
42         PersistenceManager pm = (PersistenceManager) pmControl.getMock();
43         pmf.getPersistenceManager();
44         pmfControl.setReturnValue(pm, 1);
45         pm.close();
46         pmControl.setVoidCallable(1);
47         pmfControl.replay();
48         pmControl.replay();
49
50         JdoInterceptor interceptor = new JdoInterceptor();
51         interceptor.setPersistenceManagerFactory(pmf);
52         try {
53             interceptor.invoke(new TestInvocation(pmf));
54         }
55         catch (Throwable JavaDoc t) {
56             fail("Should not have thrown Throwable: " + t.getMessage());
57         }
58
59         pmfControl.verify();
60         pmControl.verify();
61     }
62
63     public void testInterceptorWithPrebound() {
64         MockControl pmfControl = MockControl.createControl(PersistenceManagerFactory.class);
65         PersistenceManagerFactory pmf = (PersistenceManagerFactory) pmfControl.getMock();
66         MockControl pmControl = MockControl.createControl(PersistenceManager.class);
67         PersistenceManager pm = (PersistenceManager) pmControl.getMock();
68         pmfControl.replay();
69         pmControl.replay();
70
71         TransactionSynchronizationManager.bindResource(pmf, new PersistenceManagerHolder(pm));
72         JdoInterceptor interceptor = new JdoInterceptor();
73         interceptor.setPersistenceManagerFactory(pmf);
74         try {
75             interceptor.invoke(new TestInvocation(pmf));
76         }
77         catch (Throwable JavaDoc t) {
78             fail("Should not have thrown Throwable: " + t.getMessage());
79         }
80         finally {
81             TransactionSynchronizationManager.unbindResource(pmf);
82         }
83
84         pmfControl.verify();
85         pmControl.verify();
86     }
87
88
89     private static class TestInvocation implements MethodInvocation {
90
91         private PersistenceManagerFactory persistenceManagerFactory;
92
93         public TestInvocation(PersistenceManagerFactory persistenceManagerFactory) {
94             this.persistenceManagerFactory = persistenceManagerFactory;
95         }
96
97         public Object JavaDoc proceed() throws Throwable JavaDoc {
98             if (!TransactionSynchronizationManager.hasResource(this.persistenceManagerFactory)) {
99                 throw new IllegalStateException JavaDoc("PersistenceManager not bound");
100             }
101             return null;
102         }
103
104         public Object JavaDoc[] getArguments() {
105             return null;
106         }
107
108         public int getCurrentInterceptorIndex() {
109             return 0;
110         }
111
112         public int getNumberOfInterceptors() {
113             return 0;
114         }
115
116         public Interceptor getInterceptor(int i) {
117             return null;
118         }
119
120         public Method JavaDoc getMethod() {
121             return null;
122         }
123
124         public AccessibleObject JavaDoc getStaticPart() {
125             return getMethod();
126         }
127
128         public Object JavaDoc getArgument(int i) {
129             return null;
130         }
131
132         public void setArgument(int i, Object JavaDoc handler) {
133         }
134
135         public int getArgumentCount() {
136             return 0;
137         }
138
139         public Object JavaDoc getThis() {
140             return null;
141         }
142
143         public Object JavaDoc getProxy() {
144             return null;
145         }
146
147         public Invocation cloneInstance() {
148             return null;
149         }
150
151         public void release() {
152         }
153     }
154
155 }
156
Popular Tags