~ubuntu-branches/ubuntu/vivid/gmetrics/vivid

« back to all changes in this revision

Viewing changes to src/test/groovy/org/gmetrics/result/MethodKeyTest.groovy

  • Committer: Package Import Robot
  • Author(s): Miguel Landaeta, Miguel Landaeta, tony mancill
  • Date: 2012-01-18 20:57:50 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120118205750-68fv86p7fs8xz470
Tags: 0.5-1
[Miguel Landaeta]
* New upstream release.
* Remove patch ftbfs_613266.diff since it was merged upstream.
* Update dates in copyright file.

[tony mancill]
* Set DMUA flag.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2011 the original author or authors.
 
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 org.gmetrics.result
 
17
 
 
18
import org.gmetrics.test.AbstractTestCase
 
19
import org.codehaus.groovy.ast.MethodNode
 
20
import org.gmetrics.source.SourceString
 
21
import org.codehaus.groovy.ast.ClassCodeVisitorSupport
 
22
import org.codehaus.groovy.control.SourceUnit
 
23
 
 
24
/**
 
25
 * Tests for MethodKey
 
26
 *
 
27
 * @author Chris Mair
 
28
 */
 
29
@SuppressWarnings('ExplicitCallToEqualsMethod')
 
30
class MethodKeyTest extends AbstractTestCase {
 
31
 
 
32
    void testMethodName() {
 
33
        def methodKey = new MethodKey('abc')
 
34
        assert methodKey.methodName == 'abc'
 
35
        assert methodKey.signature == null
 
36
    }
 
37
 
 
38
    void testMethodNode() {
 
39
        def methodNode = buildMethodNodes()[0]
 
40
        def methodKey = new MethodKey(methodNode)
 
41
        assert methodKey.methodName == 'myMethod'
 
42
        assert methodKey.signature == 'int myMethod(String, long)'
 
43
    }
 
44
 
 
45
    @SuppressWarnings('ComparisonWithSelf')
 
46
    void testEqualsAndHashCode_SameInstance() {
 
47
        def methodKey = new MethodKey('abc')
 
48
        assert methodKey.equals(methodKey)
 
49
        assert methodKey.hashCode() == methodKey.hashCode()
 
50
    }
 
51
 
 
52
    void testEqualsAndHashCode_SameMethodNameValue() {
 
53
        def methodKey1 = new MethodKey('abc')
 
54
        def methodKey2 = new MethodKey('abc')
 
55
        assert methodKey1.equals(methodKey2)
 
56
        assert methodKey1.hashCode() == methodKey2.hashCode()
 
57
    }
 
58
 
 
59
    void testEqualsAndHashCode_SameMethodNodeValue() {
 
60
        def methodNode1 = buildMethodNodes()[0]
 
61
        def methodNode2 = buildMethodNodes()[0]
 
62
        assertEqualMethodKeys(methodNode1, methodNode2)
 
63
    }
 
64
 
 
65
    void testEqualsAndHashCode_DifferentMethodNameValue() {
 
66
        def methodKey1 = new MethodKey('abc')
 
67
        def methodKey2 = new MethodKey('xxx')
 
68
        assert !methodKey1.equals(methodKey2)
 
69
        assert methodKey1.hashCode() != methodKey2.hashCode()
 
70
    }
 
71
 
 
72
    void testEqualsAndHashCode_DifferentMethodNodeValues() {
 
73
        def methodNode1 = buildMethodNodes()[0]
 
74
        def methodNode2 = buildMethodNodes()[1]
 
75
        assertUnequalMethodKeys(methodNode1, methodNode2)
 
76
    }
 
77
 
 
78
    void testEqualsAndHashCode_OverriddenMethod() {
 
79
        def methodNode1 = buildMethodNodes()[0]
 
80
        def methodNode2 = buildMethodNodes()[2]
 
81
        assertUnequalMethodKeys(methodNode1, methodNode2)
 
82
    }
 
83
 
 
84
    void testEquals_NotAMethodKey() {
 
85
        def methodKey = new MethodKey('abc')
 
86
        assert !methodKey.equals('XXX')
 
87
    }
 
88
 
 
89
    void testEquals_Null() {
 
90
        def methodKey = new MethodKey('abc')
 
91
        assert !methodKey.equals(null)
 
92
    }
 
93
 
 
94
    void testConstructor_String_NullOrEmptyString() {
 
95
        shouldFailWithMessageContaining('methodName') { new MethodKey((String)null) }
 
96
        shouldFailWithMessageContaining('methodName') { new MethodKey('') }
 
97
    }
 
98
 
 
99
    void testConstructor_MethodNode_NullMethodNode() {
 
100
        shouldFailWithMessageContaining('methodNode') { new MethodKey((MethodNode)null) }
 
101
    }
 
102
 
 
103
    //------------------------------------------------------------------------------------
 
104
    // Helper Methods
 
105
    //------------------------------------------------------------------------------------
 
106
 
 
107
    private void assertEqualMethodKeys(MethodNode methodNode1, MethodNode methodNode2) {
 
108
        def methodKey1 = new MethodKey(methodNode1)
 
109
        def methodKey2 = new MethodKey(methodNode2)
 
110
        assert methodKey1.equals(methodKey2)
 
111
        assert methodKey1.hashCode() == methodKey2.hashCode()
 
112
    }
 
113
 
 
114
    private void assertUnequalMethodKeys(MethodNode methodNode1, MethodNode methodNode2) {
 
115
        def methodKey1 = new MethodKey(methodNode1)
 
116
        def methodKey2 = new MethodKey(methodNode2)
 
117
        assert !methodKey1.equals(methodKey2)
 
118
        assert methodKey1.hashCode() != methodKey2.hashCode()
 
119
    }
 
120
 
 
121
    private List<MethodNode> buildMethodNodes() {
 
122
        final SOURCE = '''
 
123
            class MyClass {
 
124
                int myMethod(String name, long timestamp) { }
 
125
                def otherMethod() { }
 
126
                int myMethod(long timestamp) { }        // overridden
 
127
            }
 
128
        '''
 
129
        def sourceCode = new SourceString(SOURCE)
 
130
        def ast = sourceCode.ast
 
131
        def visitor = new MethodKeyTestVisitor()
 
132
        ast.classes.each {
 
133
            classNode -> visitor.visitClass(classNode)
 
134
        }
 
135
        return visitor.methodNodes
 
136
    }
 
137
 
 
138
    private static class MethodKeyTestVisitor extends ClassCodeVisitorSupport {
 
139
        def methodNodes = []
 
140
 
 
141
        @Override
 
142
        void visitMethod(MethodNode methodNode) {
 
143
            methodNodes << methodNode
 
144
        }
 
145
 
 
146
        @Override
 
147
        protected SourceUnit getSourceUnit() {
 
148
            return null
 
149
        }
 
150
    }
 
151
 
 
152
}