KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > cglib > core > TestKeyFactory


1 /*
2  * Copyright 2003,2004 The Apache Software Foundation
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 package net.sf.cglib.core;
17
18 import junit.framework.*;
19 import java.util.*;
20
21 /**
22  * @author Chris Nokleberg <a HREF="mailto:chris@nokleberg.com">chris@nokleberg.com</a>
23  * @version $Id: TestKeyFactory.java,v 1.6 2004/06/24 21:15:17 herbyderby Exp $
24  */

25 public class TestKeyFactory extends net.sf.cglib.CodeGenTestCase {
26     public interface MyKey {
27         public Object JavaDoc newInstance(int a, int[] b, boolean flag);
28     }
29
30     public interface MyKey2 {
31         public Object JavaDoc newInstance(int[][] a);
32     }
33
34     public interface CharArrayKey {
35         public Object JavaDoc newInstance(char[] a);
36     }
37
38     public interface BooleanArrayKey {
39         public Object JavaDoc newInstance(boolean[] a);
40     }
41
42     public interface ClassArrayKey {
43         public Object JavaDoc newInstance(Class JavaDoc[] a);
44     }
45
46     public interface MethodKey {
47         public Object JavaDoc newInstance(Class JavaDoc returnType, Class JavaDoc[] parameterTypes);
48     }
49
50     public interface PrimitivesKey {
51         public Object JavaDoc newInstance(boolean b, double d, float f, int i, long l);
52     }
53
54     public interface IntegerKey {
55         public Object JavaDoc newInstance(int i);
56     }
57
58     public interface LongKey {
59         public Object JavaDoc newInstance(long l);
60     }
61
62     public interface FloatKey {
63         public Object JavaDoc newInstance(float f);
64     }
65     
66     public void testSimple() throws Exception JavaDoc {
67         MyKey mykey = (MyKey)KeyFactory.create(MyKey.class);
68         assertTrue(mykey.newInstance(5, new int[]{ 6, 7 }, false).hashCode() ==
69                    mykey.newInstance(5, new int[]{ 6, 7 }, false).hashCode());
70     }
71
72     private Object JavaDoc helper(Class JavaDoc type) {
73         KeyFactory.Generator gen = new KeyFactory.Generator();
74         gen.setInterface(type);
75         gen.setHashConstant(5);
76         gen.setHashMultiplier(3);
77         return gen.create();
78     }
79
80     public void testPrimitives() throws Exception JavaDoc {
81         PrimitivesKey factory = (PrimitivesKey)helper(PrimitivesKey.class);
82         Object JavaDoc instance = factory.newInstance(true, 1.234d, 5.678f, 100, 200L);
83         assertTrue(instance.hashCode() == 1525582882);
84     }
85
86     public void testInteger() throws Exception JavaDoc {
87         IntegerKey factory = (IntegerKey)helper(IntegerKey.class);
88         Object JavaDoc instance = factory.newInstance(7);
89         assertTrue(instance.hashCode() == 22);
90     }
91
92     public void testLong() throws Exception JavaDoc {
93         LongKey factory = (LongKey)helper(LongKey.class);
94         Object JavaDoc instance = factory.newInstance(7L);
95         assertTrue(instance.hashCode() == 22);
96     }
97
98     public void testFloat() throws Exception JavaDoc {
99         FloatKey factory = (FloatKey)helper(FloatKey.class);
100         Object JavaDoc instance = factory.newInstance(7f);
101         assertTrue(instance.hashCode() == 1088421903);
102     }
103     
104     public void testNested() throws Exception JavaDoc {
105         KeyFactory.Generator gen = new KeyFactory.Generator();
106         gen.setInterface(MyKey2.class);
107         gen.setHashConstant(17);
108         gen.setHashMultiplier(37);
109         MyKey2 mykey2 = (MyKey2)gen.create();
110         Object JavaDoc instance = mykey2.newInstance(new int[][]{ { 1, 2 }, { 3, 4 } });
111         assertTrue(instance.hashCode() == 31914243);
112     }
113
114     public void testCharArray() throws Exception JavaDoc {
115         CharArrayKey f = (CharArrayKey)KeyFactory.create(CharArrayKey.class);
116         Object JavaDoc key1 = f.newInstance(new char[]{ 'a', 'b' });
117         Object JavaDoc key2 = f.newInstance(new char[]{ 'a', '_' });
118         assertTrue(!key1.equals(key2));
119     }
120
121     public void testBooleanArray() throws Exception JavaDoc {
122         BooleanArrayKey f = (BooleanArrayKey)KeyFactory.create(BooleanArrayKey.class);
123         Object JavaDoc key1 = f.newInstance(new boolean[]{ true, false, true });
124         Object JavaDoc key2 = f.newInstance(new boolean[]{ true, false, true });
125         assertTrue(key1.equals(key2));
126     }
127
128     public void testMethodKey() throws Exception JavaDoc {
129         MethodKey factory = (MethodKey)KeyFactory.create(MethodKey.class);
130         Set methodSet = new HashSet();
131         methodSet.add(factory.newInstance(Number JavaDoc.class, new Class JavaDoc[]{ int.class }));
132         assertTrue(methodSet.contains(factory.newInstance(Number JavaDoc.class, new Class JavaDoc[]{ int.class })));
133         assertTrue(!methodSet.contains(factory.newInstance(Number JavaDoc.class, new Class JavaDoc[]{ Integer JavaDoc.class })));
134     }
135
136     public void testEqualOtherClass() throws Exception JavaDoc {
137         MyKey mykey = (MyKey)KeyFactory.create(MyKey.class);
138         assertTrue(!mykey.newInstance(5, new int[]{ 6, 7 }, false).equals(new Object JavaDoc()));
139     }
140     
141     
142     
143     public TestKeyFactory(String JavaDoc testName) {
144         super(testName);
145     }
146     
147     public static void main(String JavaDoc[] args) {
148         junit.textui.TestRunner.run(suite());
149     }
150     
151     public static Test suite() {
152         return new TestSuite(TestKeyFactory.class);
153     }
154     
155     public void perform(ClassLoader JavaDoc loader) throws Throwable JavaDoc {
156         
157         KeyFactory.create(loader, MyKey.class, null );
158     }
159     
160     public void testFailOnMemoryLeak() throws Throwable JavaDoc {
161         if(leaks()){
162           fail("Memory Leak in KeyFactory");
163         }
164     }
165     
166 }
167
Popular Tags