1 16 17 package org.springframework.orm.jdo; 18 19 import java.lang.reflect.AccessibleObject ; 20 import java.lang.reflect.Method ; 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 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 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 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 proceed() throws Throwable { 98 if (!TransactionSynchronizationManager.hasResource(this.persistenceManagerFactory)) { 99 throw new IllegalStateException ("PersistenceManager not bound"); 100 } 101 return null; 102 } 103 104 public Object [] 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 getMethod() { 121 return null; 122 } 123 124 public AccessibleObject getStaticPart() { 125 return getMethod(); 126 } 127 128 public Object getArgument(int i) { 129 return null; 130 } 131 132 public void setArgument(int i, Object handler) { 133 } 134 135 public int getArgumentCount() { 136 return 0; 137 } 138 139 public Object getThis() { 140 return null; 141 } 142 143 public Object 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 |