2
* Copyright 2003,2004 The Apache Software Foundation
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
16
package net.sf.cglib.core;
18
import junit.framework.*;
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 $
25
public class TestKeyFactory extends net.sf.cglib.CodeGenTestCase {
26
public interface MyKey {
27
public Object newInstance(int a, int[] b, boolean flag);
30
public interface MyKey2 {
31
public Object newInstance(int[][] a);
34
public interface CharArrayKey {
35
public Object newInstance(char[] a);
38
public interface BooleanArrayKey {
39
public Object newInstance(boolean[] a);
42
public interface ClassArrayKey {
43
public Object newInstance(Class[] a);
46
public interface MethodKey {
47
public Object newInstance(Class returnType, Class[] parameterTypes);
50
public interface PrimitivesKey {
51
public Object newInstance(boolean b, double d, float f, int i, long l);
54
public interface IntegerKey {
55
public Object newInstance(int i);
58
public interface LongKey {
59
public Object newInstance(long l);
62
public interface FloatKey {
63
public Object newInstance(float f);
66
public void testSimple() throws Exception {
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());
72
private Object helper(Class type) {
73
KeyFactory.Generator gen = new KeyFactory.Generator();
74
gen.setInterface(type);
75
gen.setHashConstant(5);
76
gen.setHashMultiplier(3);
80
public void testPrimitives() throws Exception {
81
PrimitivesKey factory = (PrimitivesKey)helper(PrimitivesKey.class);
82
Object instance = factory.newInstance(true, 1.234d, 5.678f, 100, 200L);
83
assertTrue(instance.hashCode() == 1525582882);
86
public void testInteger() throws Exception {
87
IntegerKey factory = (IntegerKey)helper(IntegerKey.class);
88
Object instance = factory.newInstance(7);
89
assertTrue(instance.hashCode() == 22);
92
public void testLong() throws Exception {
93
LongKey factory = (LongKey)helper(LongKey.class);
94
Object instance = factory.newInstance(7L);
95
assertTrue(instance.hashCode() == 22);
98
public void testFloat() throws Exception {
99
FloatKey factory = (FloatKey)helper(FloatKey.class);
100
Object instance = factory.newInstance(7f);
101
assertTrue(instance.hashCode() == 1088421903);
104
public void testNested() throws Exception {
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 instance = mykey2.newInstance(new int[][]{ { 1, 2 }, { 3, 4 } });
111
assertTrue(instance.hashCode() == 31914243);
114
public void testCharArray() throws Exception {
115
CharArrayKey f = (CharArrayKey)KeyFactory.create(CharArrayKey.class);
116
Object key1 = f.newInstance(new char[]{ 'a', 'b' });
117
Object key2 = f.newInstance(new char[]{ 'a', '_' });
118
assertTrue(!key1.equals(key2));
121
public void testBooleanArray() throws Exception {
122
BooleanArrayKey f = (BooleanArrayKey)KeyFactory.create(BooleanArrayKey.class);
123
Object key1 = f.newInstance(new boolean[]{ true, false, true });
124
Object key2 = f.newInstance(new boolean[]{ true, false, true });
125
assertTrue(key1.equals(key2));
128
public void testMethodKey() throws Exception {
129
MethodKey factory = (MethodKey)KeyFactory.create(MethodKey.class);
130
Set methodSet = new HashSet();
131
methodSet.add(factory.newInstance(Number.class, new Class[]{ int.class }));
132
assertTrue(methodSet.contains(factory.newInstance(Number.class, new Class[]{ int.class })));
133
assertTrue(!methodSet.contains(factory.newInstance(Number.class, new Class[]{ Integer.class })));
136
public void testEqualOtherClass() throws Exception {
137
MyKey mykey = (MyKey)KeyFactory.create(MyKey.class);
138
assertTrue(!mykey.newInstance(5, new int[]{ 6, 7 }, false).equals(new Object()));
143
public TestKeyFactory(String testName) {
147
public static void main(String[] args) {
148
junit.textui.TestRunner.run(suite());
151
public static Test suite() {
152
return new TestSuite(TestKeyFactory.class);
155
public void perform(ClassLoader loader) throws Throwable {
157
KeyFactory.create(loader, MyKey.class, null );
160
public void testFailOnMemoryLeak() throws Throwable {
162
fail("Memory Leak in KeyFactory");