JdoInterceptorTests.java
Upload User: jiancairen
Upload Date: 2007-08-27
Package Size: 26458k
Code Size: 4k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * Copyright 2002-2004 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. package org.springframework.orm.jdo;
  17. import java.lang.reflect.AccessibleObject;
  18. import java.lang.reflect.Method;
  19. import javax.jdo.PersistenceManager;
  20. import javax.jdo.PersistenceManagerFactory;
  21. import junit.framework.TestCase;
  22. import org.aopalliance.intercept.Interceptor;
  23. import org.aopalliance.intercept.Invocation;
  24. import org.aopalliance.intercept.MethodInvocation;
  25. import org.easymock.MockControl;
  26. import org.springframework.transaction.support.TransactionSynchronizationManager;
  27. /**
  28.  * @author Juergen Hoeller
  29.  */
  30. public class JdoInterceptorTests extends TestCase {
  31. public void testInterceptor() {
  32. MockControl pmfControl = MockControl.createControl(PersistenceManagerFactory.class);
  33. PersistenceManagerFactory pmf = (PersistenceManagerFactory) pmfControl.getMock();
  34. MockControl pmControl = MockControl.createControl(PersistenceManager.class);
  35. PersistenceManager pm = (PersistenceManager) pmControl.getMock();
  36. pmf.getPersistenceManager();
  37. pmfControl.setReturnValue(pm, 1);
  38. pm.close();
  39. pmControl.setVoidCallable(1);
  40. pmfControl.replay();
  41. pmControl.replay();
  42. JdoInterceptor interceptor = new JdoInterceptor();
  43. interceptor.setPersistenceManagerFactory(pmf);
  44. try {
  45. interceptor.invoke(new TestInvocation(pmf));
  46. }
  47. catch (Throwable t) {
  48. fail("Should not have thrown Throwable: " + t.getMessage());
  49. }
  50. pmfControl.verify();
  51. pmControl.verify();
  52. }
  53. public void testInterceptorWithPrebound() {
  54. MockControl pmfControl = MockControl.createControl(PersistenceManagerFactory.class);
  55. PersistenceManagerFactory pmf = (PersistenceManagerFactory) pmfControl.getMock();
  56. MockControl pmControl = MockControl.createControl(PersistenceManager.class);
  57. PersistenceManager pm = (PersistenceManager) pmControl.getMock();
  58. pmfControl.replay();
  59. pmControl.replay();
  60. TransactionSynchronizationManager.bindResource(pmf, new PersistenceManagerHolder(pm));
  61. JdoInterceptor interceptor = new JdoInterceptor();
  62. interceptor.setPersistenceManagerFactory(pmf);
  63. try {
  64. interceptor.invoke(new TestInvocation(pmf));
  65. }
  66. catch (Throwable t) {
  67. fail("Should not have thrown Throwable: " + t.getMessage());
  68. }
  69. finally {
  70. TransactionSynchronizationManager.unbindResource(pmf);
  71. }
  72. pmfControl.verify();
  73. pmControl.verify();
  74. }
  75. private static class TestInvocation implements MethodInvocation {
  76. private PersistenceManagerFactory persistenceManagerFactory;
  77. public TestInvocation(PersistenceManagerFactory persistenceManagerFactory) {
  78. this.persistenceManagerFactory = persistenceManagerFactory;
  79. }
  80. public Object proceed() throws Throwable {
  81. if (!TransactionSynchronizationManager.hasResource(this.persistenceManagerFactory)) {
  82. throw new IllegalStateException("PersistenceManager not bound");
  83. }
  84. return null;
  85. }
  86. public Object[] getArguments() {
  87. return null;
  88. }
  89. public int getCurrentInterceptorIndex() {
  90. return 0;
  91. }
  92. public int getNumberOfInterceptors() {
  93. return 0;
  94. }
  95. public Interceptor getInterceptor(int i) {
  96. return null;
  97. }
  98. public Method getMethod() {
  99. return null;
  100. }
  101. public AccessibleObject getStaticPart() {
  102. return getMethod();
  103. }
  104. public Object getArgument(int i) {
  105. return null;
  106. }
  107. public void setArgument(int i, Object handler) {
  108. }
  109. public int getArgumentCount() {
  110. return 0;
  111. }
  112. public Object getThis() {
  113. return null;
  114. }
  115. public Object getProxy() {
  116. return null;
  117. }
  118. public Invocation cloneInstance() {
  119. return null;
  120. }
  121. public void release() {
  122. }
  123. }
  124. }