2
* Copyright 2011 the original author or authors.
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 org.gmetrics.result
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
29
@SuppressWarnings('ExplicitCallToEqualsMethod')
30
class MethodKeyTest extends AbstractTestCase {
32
void testMethodName() {
33
def methodKey = new MethodKey('abc')
34
assert methodKey.methodName == 'abc'
35
assert methodKey.signature == null
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)'
45
@SuppressWarnings('ComparisonWithSelf')
46
void testEqualsAndHashCode_SameInstance() {
47
def methodKey = new MethodKey('abc')
48
assert methodKey.equals(methodKey)
49
assert methodKey.hashCode() == methodKey.hashCode()
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()
59
void testEqualsAndHashCode_SameMethodNodeValue() {
60
def methodNode1 = buildMethodNodes()[0]
61
def methodNode2 = buildMethodNodes()[0]
62
assertEqualMethodKeys(methodNode1, methodNode2)
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()
72
void testEqualsAndHashCode_DifferentMethodNodeValues() {
73
def methodNode1 = buildMethodNodes()[0]
74
def methodNode2 = buildMethodNodes()[1]
75
assertUnequalMethodKeys(methodNode1, methodNode2)
78
void testEqualsAndHashCode_OverriddenMethod() {
79
def methodNode1 = buildMethodNodes()[0]
80
def methodNode2 = buildMethodNodes()[2]
81
assertUnequalMethodKeys(methodNode1, methodNode2)
84
void testEquals_NotAMethodKey() {
85
def methodKey = new MethodKey('abc')
86
assert !methodKey.equals('XXX')
89
void testEquals_Null() {
90
def methodKey = new MethodKey('abc')
91
assert !methodKey.equals(null)
94
void testConstructor_String_NullOrEmptyString() {
95
shouldFailWithMessageContaining('methodName') { new MethodKey((String)null) }
96
shouldFailWithMessageContaining('methodName') { new MethodKey('') }
99
void testConstructor_MethodNode_NullMethodNode() {
100
shouldFailWithMessageContaining('methodNode') { new MethodKey((MethodNode)null) }
103
//------------------------------------------------------------------------------------
105
//------------------------------------------------------------------------------------
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()
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()
121
private List<MethodNode> buildMethodNodes() {
124
int myMethod(String name, long timestamp) { }
125
def otherMethod() { }
126
int myMethod(long timestamp) { } // overridden
129
def sourceCode = new SourceString(SOURCE)
130
def ast = sourceCode.ast
131
def visitor = new MethodKeyTestVisitor()
133
classNode -> visitor.visitClass(classNode)
135
return visitor.methodNodes
138
private static class MethodKeyTestVisitor extends ClassCodeVisitorSupport {
142
void visitMethod(MethodNode methodNode) {
143
methodNodes << methodNode
147
protected SourceUnit getSourceUnit() {