KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > transaction > interceptor > TransactionInterceptorTests


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.transaction.interceptor;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.Properties JavaDoc;
21
22 import org.springframework.aop.framework.ProxyFactory;
23 import org.springframework.transaction.PlatformTransactionManager;
24 import org.springframework.transaction.TransactionDefinition;
25 import org.springframework.transaction.TransactionException;
26 import org.springframework.transaction.TransactionStatus;
27 import org.springframework.util.SerializationTestUtils;
28
29 /**
30  * Mock object based tests for TransactionInterceptor.
31  *
32  * @author Rod Johnson
33  * @since 16.03.2003
34  */

35 public class TransactionInterceptorTests extends AbstractTransactionAspectTests {
36     
37     /**
38      * Template method to create an advised object given the
39      * target object and transaction setup.
40      * Creates a TransactionInterceptor and applies it.
41      */

42     protected Object JavaDoc advised(Object JavaDoc target, PlatformTransactionManager ptm, TransactionAttributeSource tas) {
43         TransactionInterceptor ti = new TransactionInterceptor();
44         ti.setTransactionManager(ptm);
45         assertEquals(ptm, ti.getTransactionManager());
46         ti.setTransactionAttributeSource(tas);
47         assertEquals(tas, ti.getTransactionAttributeSource());
48
49         ProxyFactory pf = new ProxyFactory(target);
50         pf.addAdvice(0, ti);
51         return pf.getProxy();
52     }
53     
54     /**
55      * A TransactionInterceptor should be serializable if its
56      * PlatformTransactionManager is.
57      */

58     public void testSerializableWithAttributeProperties() throws Exception JavaDoc {
59         TransactionInterceptor ti = new TransactionInterceptor();
60         Properties JavaDoc p = new Properties JavaDoc();
61         p.setProperty("methodName", "PROPAGATION_REQUIRED");
62         ti.setTransactionAttributes(p);
63         PlatformTransactionManager ptm = new SerializableTransactionManager();
64         ti.setTransactionManager(ptm);
65         ti = (TransactionInterceptor) SerializationTestUtils.serializeAndDeserialize(ti);
66         // Check that logger survived deserialization
67
assertNotNull(ti.logger);
68         assertTrue(ti.getTransactionManager() instanceof SerializableTransactionManager);
69     }
70
71
72     /**
73      * We won't use this: we just want to know it's serializable.
74      */

75     private static class SerializableTransactionManager implements PlatformTransactionManager, Serializable JavaDoc {
76
77         public TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException {
78             throw new UnsupportedOperationException JavaDoc();
79         }
80
81         public void commit(TransactionStatus status) throws TransactionException {
82             throw new UnsupportedOperationException JavaDoc();
83         }
84
85         public void rollback(TransactionStatus status) throws TransactionException {
86             throw new UnsupportedOperationException JavaDoc();
87         }
88         
89     }
90 }
91
Popular Tags