KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.lang.reflect.Modifier JavaDoc;
18
19 import org.apache.hivemind.service.ClassFab;
20 import org.apache.hivemind.service.ClassFabUtils;
21 import org.apache.hivemind.service.MethodFab;
22 import org.apache.hivemind.service.MethodSignature;
23 import org.apache.hivemind.test.HiveMindTestCase;
24 import org.easymock.MockControl;
25
26 /**
27  * Tests for {@link org.apache.hivemind.service.ClassFabUtils}
28  *
29  * @author Howard Lewis Ship
30  * @author James Carman
31  */

32 public class TestClassFabUtils extends HiveMindTestCase {
33     
34
35     public void testGetInstanceClass() {
36         final HiveMindClassPool pool = new HiveMindClassPool();
37         final CtClassSource classSource = new CtClassSource(pool);
38         final ClassFab classFab = new ClassFabImpl(classSource, pool
39                 .makeClass("Dummy"));
40         assertSame(ClassFabUtils.getInstanceClass(classFab,
41                 new BeanInterfaceImpl(), BeanInterface.class),
42                 BeanInterfaceImpl.class);
43         assertSame( ClassFabUtils.getInstanceClass(classFab,CglibBeanInterfaceFactory.createCglibBean(), BeanInterface.class ), BeanInterface.class );
44         assertSame( ClassFabUtils.getInstanceClass(classFab,JavassistBeanInterfaceFactory.createJavassistBean(), BeanInterface.class ), BeanInterface.class );
45         assertSame( ClassFabUtils.getInstanceClass(classFab,JdkBeanInterfaceFactory.createJdkBean(), BeanInterface.class ), BeanInterface.class );
46         
47     }
48
49     public static class BeanInterfaceImpl implements BeanInterface {
50
51         public String JavaDoc interfaceMethod() {
52             return "Hello, World!";
53         }
54
55     }
56
57     /** @since 1.1 */
58     public void testAddNoOpMethod() {
59         tryAddNoOpMethod(void.class, "{ }");
60         tryAddNoOpMethod(String JavaDoc.class, "{ return null; }");
61         tryAddNoOpMethod(boolean.class, "{ return false; }");
62         tryAddNoOpMethod(char.class, "{ return 0; }");
63         tryAddNoOpMethod(short.class, "{ return 0; }");
64         tryAddNoOpMethod(int.class, "{ return 0; }");
65         tryAddNoOpMethod(long.class, "{ return 0L; }");
66         tryAddNoOpMethod(double.class, "{ return 0.0d; }");
67         tryAddNoOpMethod(float.class, "{ return 0.0f; }");
68     }
69
70     /** @since 1.1 */
71     private void tryAddNoOpMethod(Class JavaDoc returnClass, String JavaDoc expectedBody) {
72         MethodSignature sig = new MethodSignature(returnClass, "run", null,
73                 null);
74
75         MockControl control = newControl(ClassFab.class);
76         ClassFab cf = (ClassFab) control.getMock();
77         MethodFab mf = (MethodFab) newMock(MethodFab.class);
78
79         cf.addMethod(Modifier.PUBLIC, sig, expectedBody);
80         control.setReturnValue(mf);
81
82         replayControls();
83
84         ClassFabUtils.addNoOpMethod(cf, sig);
85
86         verifyControls();
87     }
88
89     /** @since 1.1 */
90     public void testGenerateClassName() throws Exception JavaDoc {
91         String JavaDoc name = ClassFabUtils.generateClassName(Runnable JavaDoc.class);
92
93         assertRegexp("\\$Runnable_([0-9|a-f])+", name);
94     }
95 }
Popular Tags