KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > service > impl > TestLoggingInterceptorFactory


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.service.impl;
16
17 import hivemind.test.FrameworkTestCase;
18
19 import java.lang.reflect.Constructor JavaDoc;
20 import java.util.Collections JavaDoc;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.hivemind.Registry;
24 import org.apache.hivemind.definition.ImplementationConstructor;
25 import org.apache.hivemind.definition.ImplementationDefinition;
26 import org.apache.hivemind.definition.RegistryDefinition;
27 import org.apache.hivemind.definition.impl.ModuleDefinitionImpl;
28 import org.apache.hivemind.definition.impl.RegistryDefinitionImpl;
29 import org.apache.hivemind.definition.impl.ImplementationDefinitionImpl;
30 import org.apache.hivemind.definition.impl.ServicePointDefinitionImpl;
31 import org.apache.hivemind.impl.InterceptorStackImpl;
32 import org.apache.hivemind.internal.ServiceModel;
33 import org.apache.hivemind.internal.ServicePoint;
34 import org.apache.hivemind.service.ClassFactory;
35 import org.easymock.MockControl;
36
37 /**
38  * Tests for {@link org.apache.hivemind.service.impl.LoggingInterceptorClassFactory}.
39  *
40  * @author Howard Lewis Ship
41  * @author James Carman
42  */

43 public class TestLoggingInterceptorFactory extends FrameworkTestCase
44 {
45     /**
46      * A test for HIVEMIND-55 ... ensure that the LoggingInterceptor can work on
47      * top of a JDK proxy.
48      */

49     public void testLoggingOverProxy() throws Exception JavaDoc
50     {
51         ClassFactory cf = new ClassFactoryImpl();
52
53         Runnable JavaDoc r = (Runnable JavaDoc) newMock(Runnable JavaDoc.class);
54         MockControl logControl = newControl(Log.class);
55         Log log = (Log) logControl.getMock();
56
57         LoggingInterceptorClassFactory f = new LoggingInterceptorClassFactory(cf);
58
59         MockControl spControl = newControl(ServicePoint.class);
60         ServicePoint sp = (ServicePoint) spControl.getMock();
61         
62         // Training
63

64         sp.getServiceInterface();
65         spControl.setReturnValue(Runnable JavaDoc.class);
66                 
67         sp.getExtensionPointId();
68         spControl.setReturnValue("foo.bar");
69         
70         replayControls();
71
72         // Create interceptor
73
InterceptorStackImpl is = new InterceptorStackImpl(log, sp, r);
74
75         Class JavaDoc interceptorClass = f.constructInterceptorClass(is, Collections.EMPTY_LIST);
76         Constructor JavaDoc c = interceptorClass.getConstructors()[0];
77
78         Object JavaDoc interceptor = c.newInstance(new Object JavaDoc[] { is.getServiceLog(), is.peek() });
79         is.push(interceptor);
80
81         Runnable JavaDoc ri = (Runnable JavaDoc) is.peek();
82
83         verifyControls();
84
85         // Training
86

87         log.isDebugEnabled();
88         logControl.setReturnValue(true);
89
90         log.debug("BEGIN run()");
91         log.debug("END run()");
92         
93         r.run();
94
95         replayControls();
96
97         ri.run();
98
99         verifyControls();
100     }
101     
102     public void testJavassistProxies() throws Exception JavaDoc {
103         
104         Registry reg = createRegistry(new JavassistBeanInterfaceFactory(newLocation(), "module"));
105         final BeanInterface bean = ( BeanInterface )reg.getService( "hivemind.tests.serviceByInterface.BeanInterface", BeanInterface.class );
106         bean.interfaceMethod();
107     }
108     
109     public void testCglibProxies() throws Exception JavaDoc {
110         Registry reg = createRegistry(new CglibBeanInterfaceFactory(newLocation(), "module"));
111         final BeanInterface bean = ( BeanInterface )reg.getService( "hivemind.tests.serviceByInterface.BeanInterface", BeanInterface.class );
112         bean.interfaceMethod();
113     }
114     
115     public void testJdkProxies() throws Exception JavaDoc {
116         Registry reg = createRegistry(new JdkBeanInterfaceFactory(newLocation(), "module"));
117         final BeanInterface bean = ( BeanInterface )reg.getService( "hivemind.tests.serviceByInterface.BeanInterface", BeanInterface.class );
118         bean.interfaceMethod();
119     }
120     
121
122     /**
123      * Builds a registry containing a service "BeanInterface" that constructs its instance
124      * by using the passed constructor.
125      */

126     private Registry createRegistry(ImplementationConstructor constructor)
127     {
128         RegistryDefinition definition = new RegistryDefinitionImpl();
129
130         ModuleDefinitionImpl module = createModuleDefinition("hivemind.tests.serviceByInterface");
131         definition.addModule(module);
132         
133         ServicePointDefinitionImpl sp1 = createServicePointDefinition(module, "BeanInterface", BeanInterface.class);
134         ImplementationDefinition impl = new ImplementationDefinitionImpl(module, newLocation(),
135                 constructor, ServiceModel.SINGLETON, true);
136         sp1.addImplementation(impl);
137         module.addServicePoint(sp1);
138         Registry reg = buildFrameworkRegistry(module);
139         return reg;
140     }
141     
142 }
Popular Tags