~ubuntu-branches/ubuntu/quantal/cglib/quantal

« back to all changes in this revision

Viewing changes to src/test/net/sf/cglib/core/TestKeyFactory.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2009-10-08 21:23:45 UTC
  • Revision ID: james.westby@ubuntu.com-20091008212345-t2zy5hv2g8o4i40k
Tags: upstream-2.2+dfsg
ImportĀ upstreamĀ versionĀ 2.2+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 newInstance(int a, int[] b, boolean flag);
 
28
    }
 
29
 
 
30
    public interface MyKey2 {
 
31
        public Object newInstance(int[][] a);
 
32
    }
 
33
 
 
34
    public interface CharArrayKey {
 
35
        public Object newInstance(char[] a);
 
36
    }    
 
37
 
 
38
    public interface BooleanArrayKey {
 
39
        public Object newInstance(boolean[] a);
 
40
    }    
 
41
 
 
42
    public interface ClassArrayKey {
 
43
        public Object newInstance(Class[] a);
 
44
    }    
 
45
 
 
46
    public interface MethodKey {
 
47
        public Object newInstance(Class returnType, Class[] parameterTypes);
 
48
    }
 
49
 
 
50
    public interface PrimitivesKey {
 
51
        public Object newInstance(boolean b, double d, float f, int i, long l);
 
52
    }
 
53
 
 
54
    public interface IntegerKey {
 
55
        public Object newInstance(int i);
 
56
    }
 
57
 
 
58
    public interface LongKey {
 
59
        public Object newInstance(long l);
 
60
    }
 
61
 
 
62
    public interface FloatKey {
 
63
        public Object newInstance(float f);
 
64
    }
 
65
    
 
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());
 
70
    }
 
71
 
 
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);
 
77
        return gen.create();
 
78
    }
 
79
 
 
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);
 
84
    }
 
85
 
 
86
    public void testInteger() throws Exception {
 
87
        IntegerKey factory = (IntegerKey)helper(IntegerKey.class);
 
88
        Object instance = factory.newInstance(7);
 
89
        assertTrue(instance.hashCode() == 22);
 
90
    }
 
91
 
 
92
    public void testLong() throws Exception {
 
93
        LongKey factory = (LongKey)helper(LongKey.class);
 
94
        Object instance = factory.newInstance(7L);
 
95
        assertTrue(instance.hashCode() == 22);
 
96
    }
 
97
 
 
98
    public void testFloat() throws Exception {
 
99
        FloatKey factory = (FloatKey)helper(FloatKey.class);
 
100
        Object instance = factory.newInstance(7f);
 
101
        assertTrue(instance.hashCode() == 1088421903);
 
102
    }
 
103
    
 
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);
 
112
    }
 
113
 
 
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));
 
119
    }
 
120
 
 
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));
 
126
    }
 
127
 
 
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 })));
 
134
    }
 
135
 
 
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()));
 
139
    }
 
140
    
 
141
    
 
142
    
 
143
    public TestKeyFactory(String testName) {
 
144
        super(testName);
 
145
    }
 
146
    
 
147
    public static void main(String[] 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 loader) throws Throwable {
 
156
        
 
157
        KeyFactory.create(loader, MyKey.class, null );
 
158
    }
 
159
    
 
160
    public void testFailOnMemoryLeak() throws Throwable {
 
161
        if(leaks()){
 
162
          fail("Memory Leak in KeyFactory");
 
163
        }
 
164
    }
 
165
    
 
166
}