KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > framework > MockTargetSource


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;
18
19 import org.springframework.aop.TargetSource;
20
21 /**
22  *
23  * @author Rod Johnson
24  */

25 public class MockTargetSource implements TargetSource {
26     
27     private Object JavaDoc target;
28     
29     public int gets;
30     
31     public int releases;
32     
33     public void reset() {
34         this.target = null;
35         gets = releases = 0;
36     }
37     
38     public void setTarget(Object JavaDoc target) {
39         this.target = target;
40     }
41
42     /**
43      * @see org.springframework.aop.TargetSource#getTargetClass()
44      */

45     public Class JavaDoc getTargetClass() {
46         return target.getClass();
47     }
48
49     /**
50      * @see org.springframework.aop.TargetSource#getTarget()
51      */

52     public Object JavaDoc getTarget() throws Exception JavaDoc {
53         ++gets;
54         return target;
55     }
56
57     /**
58      * @see org.springframework.aop.TargetSource#releaseTarget(java.lang.Object)
59      */

60     public void releaseTarget(Object JavaDoc pTarget) throws Exception JavaDoc {
61         if (pTarget != this.target)
62             throw new RuntimeException JavaDoc("Released wrong target");
63         ++releases;
64     }
65     
66     /**
67      * Check that gets and releases match
68      *
69      */

70     public void verify() {
71         if (gets != releases)
72             throw new RuntimeException JavaDoc("Expectation failed: " + gets + " gets and " + releases + " releases");
73     }
74
75     /**
76      * @see org.springframework.aop.TargetSource#isStatic()
77      */

78     public boolean isStatic() {
79         return false;
80     }
81
82 }
83
Popular Tags