KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > framework > adapter > AdvisorAdapterRegistrationTests


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.aop.framework.adapter;
18
19 import junit.framework.TestCase;
20
21 import org.springframework.aop.Advisor;
22 import org.springframework.aop.SimpleBeforeAdviceImpl;
23 import org.springframework.aop.framework.Advised;
24 import org.springframework.beans.ITestBean;
25 import org.springframework.context.ApplicationContext;
26 import org.springframework.context.support.ClassPathXmlApplicationContext;
27
28 /**
29  * TestCase for AdvisorAdapterRegistrationManager mechanism.
30  *
31  * @author Dmitriy Kopylenko
32  */

33 public class AdvisorAdapterRegistrationTests extends TestCase {
34
35     public void testAdvisorAdapterRegistrationManagerNotPresentInContext() {
36         ApplicationContext ctx = new ClassPathXmlApplicationContext("/org/springframework/aop/framework/adapter/withoutBPPContext.xml");
37         ITestBean tb = (ITestBean) ctx.getBean("testBean");
38         // just invoke any method to see if advice fired
39
try {
40             tb.getName();
41             fail("Should throw UnknownAdviceTypeException");
42         }
43         catch (UnknownAdviceTypeException ex) {
44             // expected
45
assertEquals(0, getAdviceImpl(tb).getInvocationCounter());
46         }
47     }
48
49     public void testAdvisorAdapterRegistrationManagerPresentInContext() {
50         ApplicationContext ctx = new ClassPathXmlApplicationContext("/org/springframework/aop/framework/adapter/withBPPContext.xml");
51         ITestBean tb = (ITestBean) ctx.getBean("testBean");
52         // just invoke any method to see if advice fired
53
try {
54             tb.getName();
55             assertEquals(1, getAdviceImpl(tb).getInvocationCounter());
56         }
57         catch (UnknownAdviceTypeException ex) {
58             fail("Should not throw UnknownAdviceTypeException");
59         }
60     }
61
62     private SimpleBeforeAdviceImpl getAdviceImpl(ITestBean tb) {
63         Advised advised = (Advised) tb;
64         Advisor advisor = advised.getAdvisors()[0];
65         return (SimpleBeforeAdviceImpl) advisor.getAdvice();
66     }
67
68 }
69
Popular Tags