Skip to content

Commit

Permalink
Added further test cases. Refactored few other tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafael Winterhalter committed Jul 30, 2015
1 parent 59f59fd commit 2b326db
Show file tree
Hide file tree
Showing 12 changed files with 960 additions and 41 deletions.
Expand Up @@ -837,7 +837,9 @@ public boolean equals(Object other) {

@Override
public int hashCode() {
return name != null ? name.hashCode() : 0;
return name != null
? name.hashCode()
: super.hashCode();
}

@Override
Expand Down
Expand Up @@ -45,7 +45,7 @@ public MethodGraph getSuperGraph() {
public MethodGraph getInterfaceGraph(TypeDescription typeDescription) {
MethodGraph interfaceGraph = interfaceGraphs.get(typeDescription);
return interfaceGraph == null
? Illegal.INSTANCE
? Empty.INSTANCE
: interfaceGraph;
}

Expand Down Expand Up @@ -192,7 +192,7 @@ public MethodGraph.Linked make(TypeDescription typeDescription) {
}
return new Linked.Delegation(rootStore.asGraph(),
superType == null
? Illegal.INSTANCE
? Empty.INSTANCE
: snapshots.get(superType).asGraph(),
interfaceGraphs);
}
Expand Down Expand Up @@ -291,24 +291,23 @@ public String toString() {

class ForJavaMethod implements Identifier {

private final MethodDescription.Token token;
private final MethodDescription.Token methodToken;

protected ForJavaMethod(MethodDescription.Token token) {
this.token = token;
protected ForJavaMethod(MethodDescription.Token methodToken) {
this.methodToken = methodToken;
}

@Override
public MethodDescription.Token getToken() {
return token;
return methodToken;
}

@Override
public boolean equals(Object other) {
if (this == other) return true;
if (!(other instanceof ForJavaMethod)) return false;
ForJavaMethod forJavaMethod = (ForJavaMethod) other;
if (!token.getInternalName().equals(forJavaMethod.token.getInternalName())) return false;
List<ParameterDescription.Token> tokens = token.getParameterTokens(), otherTokens = forJavaMethod.token.getParameterTokens();
List<ParameterDescription.Token> tokens = methodToken.getParameterTokens(), otherTokens = forJavaMethod.methodToken.getParameterTokens();
if (tokens.size() != otherTokens.size()) return false;
for (int index = 0; index < tokens.size(); index++) {
if (!tokens.get(index).getType().asRawType().equals(otherTokens.get(index).getType().asRawType())) return false;
Expand All @@ -318,8 +317,8 @@ public boolean equals(Object other) {

@Override
public int hashCode() {
int result = token.getInternalName().hashCode();
for (ParameterDescription.Token parameterToken : token.getParameterTokens()) {
int result = 17;
for (ParameterDescription.Token parameterToken : methodToken.getParameterTokens()) {
result = 31 * result + parameterToken.getType().asRawType().hashCode();
}
return result;
Expand All @@ -328,7 +327,7 @@ public int hashCode() {
@Override
public String toString() {
return "MethodGraph.Compiler.Default.Identifier.ForJavaMethod{" +
"token=" + token +
"methodToken=" + methodToken +
'}';
}
}
Expand All @@ -348,13 +347,25 @@ public MethodDescription.Token getToken() {

@Override
public boolean equals(Object other) {
return this == other || !(other == null || getClass() != other.getClass())
&& methodToken.equals(((ForJVMMethod) other).methodToken);
if (this == other) return true;
if (!(other instanceof ForJVMMethod)) return false;
ForJVMMethod forJavaMethod = (ForJVMMethod) other;
if (!methodToken.getReturnType().asRawType().equals(forJavaMethod.methodToken.getReturnType().asRawType())) return false;
List<ParameterDescription.Token> tokens = methodToken.getParameterTokens(), otherTokens = forJavaMethod.methodToken.getParameterTokens();
if (tokens.size() != otherTokens.size()) return false;
for (int index = 0; index < tokens.size(); index++) {
if (!tokens.get(index).getType().asRawType().equals(otherTokens.get(index).getType().asRawType())) return false;
}
return true;
}

@Override
public int hashCode() {
return methodToken.hashCode();
int result = methodToken.getReturnType().asRawType().hashCode();
for (ParameterDescription.Token parameterToken : methodToken.getParameterTokens()) {
result = 31 * result + parameterToken.getType().asRawType().hashCode();
}
return result;
}

@Override
Expand All @@ -376,7 +387,7 @@ protected Key(String internalName, S token) {
this(internalName, Collections.singleton(token));
}

private Key(String internalName, Set<S> identifiers) {
protected Key(String internalName, Set<S> identifiers) {
this.internalName = internalName;
this.identifiers = identifiers;
}
Expand All @@ -390,6 +401,7 @@ protected Set<S> findBridges(S identifier) {
@Override
public boolean equals(Object other) {
return other == this || (other instanceof Key
&& internalName.equals(((Key) other).internalName)
&& !Collections.disjoint(identifiers, ((Key) other).identifiers));
}

Expand All @@ -416,7 +428,7 @@ protected Identifying(String internalName, V identifier) {
super(internalName, identifier);
}

private Identifying(String internalName, Set<V> identifiers) {
protected Identifying(String internalName, Set<V> identifiers) {
super(internalName, identifiers);
}

Expand Down Expand Up @@ -835,7 +847,7 @@ public String toString() {
}
}

enum Illegal implements MethodGraph.Linked {
enum Empty implements MethodGraph.Linked {

INSTANCE;

Expand Down
Expand Up @@ -20,6 +20,30 @@ public class FieldDescriptionTokenTest {

private static final int MODIFIERS = 42;

@Test
public void testFieldNameEqualityHashCode() throws Exception {
assertThat(new FieldDescription.Token(FOO,
MODIFIERS,
mock(GenericTypeDescription.class),
Collections.singletonList(mock(AnnotationDescription.class))).hashCode(),
is(new FieldDescription.Token(FOO,
MODIFIERS * 2,
mock(GenericTypeDescription.class),
Collections.singletonList(mock(AnnotationDescription.class))).hashCode()));
}

@Test
public void testFieldNameInequalityHashCode() throws Exception {
assertThat(new FieldDescription.Token(FOO,
MODIFIERS,
mock(GenericTypeDescription.class),
Collections.singletonList(mock(AnnotationDescription.class))).hashCode(),
not(new FieldDescription.Token(BAR,
MODIFIERS * 2,
mock(GenericTypeDescription.class),
Collections.singletonList(mock(AnnotationDescription.class))).hashCode()));
}

@Test
public void testFieldNameEquality() throws Exception {
assertThat(new FieldDescription.Token(FOO,
Expand Down
Expand Up @@ -5,6 +5,9 @@
import net.bytebuddy.description.type.generic.GenericTypeDescription;
import net.bytebuddy.test.utility.MockitoRule;
import net.bytebuddy.test.utility.ObjectPropertyAssertion;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.MatcherAssert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -21,9 +24,7 @@

public class MethodDescriptionTokenTest {

private static final String FOO = "foo";

private static final String BAR = "bar";
private static final String FOO = "foo", BAR = "bar";

private static final int MODIFIERS = 42;

Expand All @@ -47,6 +48,106 @@ public void setUp() throws Exception {
when(secondParameter.getType()).thenReturn(second);
}

@Test
public void testMethodEqualityHashCode() throws Exception {
assertThat(new MethodDescription.Token(FOO,
MODIFIERS,
Collections.singletonList(mock(GenericTypeDescription.class)),
first,
Collections.singletonList(firstParameter),
Collections.singletonList(mock(GenericTypeDescription.class)),
Collections.singletonList(mock(AnnotationDescription.class)),
MethodDescription.NO_DEFAULT_VALUE).hashCode(),
is(new MethodDescription.Token(FOO,
MODIFIERS,
Collections.singletonList(mock(GenericTypeDescription.class)),
first,
Collections.singletonList(firstParameter),
Collections.singletonList(mock(GenericTypeDescription.class)),
Collections.singletonList(mock(AnnotationDescription.class)),
MethodDescription.NO_DEFAULT_VALUE).hashCode()));
}

@Test
public void testMethodNameInequalityHashCode() throws Exception {
assertThat(new MethodDescription.Token(FOO,
MODIFIERS,
Collections.singletonList(mock(GenericTypeDescription.class)),
first,
Collections.singletonList(firstParameter),
Collections.singletonList(mock(GenericTypeDescription.class)),
Collections.singletonList(mock(AnnotationDescription.class)),
MethodDescription.NO_DEFAULT_VALUE).hashCode(),
not(new MethodDescription.Token(BAR,
MODIFIERS,
Collections.singletonList(mock(GenericTypeDescription.class)),
first,
Collections.singletonList(firstParameter),
Collections.singletonList(mock(GenericTypeDescription.class)),
Collections.singletonList(mock(AnnotationDescription.class)),
MethodDescription.NO_DEFAULT_VALUE).hashCode()));
}

@Test
public void testReturnTypeInequalityHashCode() throws Exception {
assertThat(new MethodDescription.Token(FOO,
MODIFIERS,
Collections.singletonList(mock(GenericTypeDescription.class)),
first,
Collections.singletonList(firstParameter),
Collections.singletonList(mock(GenericTypeDescription.class)),
Collections.singletonList(mock(AnnotationDescription.class)),
MethodDescription.NO_DEFAULT_VALUE).hashCode(),
not(new MethodDescription.Token(FOO,
MODIFIERS,
Collections.singletonList(mock(GenericTypeDescription.class)),
second,
Collections.singletonList(firstParameter),
Collections.singletonList(mock(GenericTypeDescription.class)),
Collections.singletonList(mock(AnnotationDescription.class)),
MethodDescription.NO_DEFAULT_VALUE).hashCode()));
}

@Test
public void testParameterTypeInequalityHashCode() throws Exception {
assertThat(new MethodDescription.Token(FOO,
MODIFIERS,
Collections.singletonList(mock(GenericTypeDescription.class)),
first,
Collections.singletonList(firstParameter),
Collections.singletonList(mock(GenericTypeDescription.class)),
Collections.singletonList(mock(AnnotationDescription.class)),
MethodDescription.NO_DEFAULT_VALUE).hashCode(),
not(new MethodDescription.Token(FOO,
MODIFIERS,
Collections.singletonList(mock(GenericTypeDescription.class)),
first,
Collections.singletonList(secondParameter),
Collections.singletonList(mock(GenericTypeDescription.class)),
Collections.singletonList(mock(AnnotationDescription.class)),
MethodDescription.NO_DEFAULT_VALUE).hashCode()));
}

@Test
public void testParameterTypeLengthInequalityHashCode() throws Exception {
assertThat(new MethodDescription.Token(FOO,
MODIFIERS,
Collections.singletonList(mock(GenericTypeDescription.class)),
first,
Collections.singletonList(firstParameter),
Collections.singletonList(mock(GenericTypeDescription.class)),
Collections.singletonList(mock(AnnotationDescription.class)),
MethodDescription.NO_DEFAULT_VALUE).hashCode(),
not(new MethodDescription.Token(FOO,
MODIFIERS,
Collections.singletonList(mock(GenericTypeDescription.class)),
first,
Collections.<ParameterDescription.Token>emptyList(),
Collections.singletonList(mock(GenericTypeDescription.class)),
Collections.singletonList(mock(AnnotationDescription.class)),
MethodDescription.NO_DEFAULT_VALUE).hashCode()));
}

@Test
public void testMethodEquality() throws Exception {
assertThat(new MethodDescription.Token(FOO,
Expand All @@ -56,15 +157,15 @@ public void testMethodEquality() throws Exception {
Collections.singletonList(firstParameter),
Collections.singletonList(mock(GenericTypeDescription.class)),
Collections.singletonList(mock(AnnotationDescription.class)),
null),
MethodDescription.NO_DEFAULT_VALUE),
is(new MethodDescription.Token(FOO,
MODIFIERS,
Collections.singletonList(mock(GenericTypeDescription.class)),
first,
Collections.singletonList(firstParameter),
Collections.singletonList(mock(GenericTypeDescription.class)),
Collections.singletonList(mock(AnnotationDescription.class)),
null)));
MethodDescription.NO_DEFAULT_VALUE)));
}

@Test
Expand All @@ -76,15 +177,15 @@ public void testMethodNameInequality() throws Exception {
Collections.singletonList(firstParameter),
Collections.singletonList(mock(GenericTypeDescription.class)),
Collections.singletonList(mock(AnnotationDescription.class)),
null),
MethodDescription.NO_DEFAULT_VALUE),
not(new MethodDescription.Token(BAR,
MODIFIERS,
Collections.singletonList(mock(GenericTypeDescription.class)),
first,
Collections.singletonList(firstParameter),
Collections.singletonList(mock(GenericTypeDescription.class)),
Collections.singletonList(mock(AnnotationDescription.class)),
null)));
MethodDescription.NO_DEFAULT_VALUE)));
}

@Test
Expand All @@ -96,15 +197,15 @@ public void testReturnTypeInequality() throws Exception {
Collections.singletonList(firstParameter),
Collections.singletonList(mock(GenericTypeDescription.class)),
Collections.singletonList(mock(AnnotationDescription.class)),
null),
MethodDescription.NO_DEFAULT_VALUE),
not(new MethodDescription.Token(FOO,
MODIFIERS,
Collections.singletonList(mock(GenericTypeDescription.class)),
second,
Collections.singletonList(firstParameter),
Collections.singletonList(mock(GenericTypeDescription.class)),
Collections.singletonList(mock(AnnotationDescription.class)),
null)));
MethodDescription.NO_DEFAULT_VALUE)));
}

@Test
Expand All @@ -116,15 +217,15 @@ public void testParameterTypeInequality() throws Exception {
Collections.singletonList(firstParameter),
Collections.singletonList(mock(GenericTypeDescription.class)),
Collections.singletonList(mock(AnnotationDescription.class)),
null),
MethodDescription.NO_DEFAULT_VALUE),
not(new MethodDescription.Token(FOO,
MODIFIERS,
Collections.singletonList(mock(GenericTypeDescription.class)),
first,
Collections.singletonList(secondParameter),
Collections.singletonList(mock(GenericTypeDescription.class)),
Collections.singletonList(mock(AnnotationDescription.class)),
null)));
MethodDescription.NO_DEFAULT_VALUE)));
}

@Test
Expand All @@ -136,15 +237,15 @@ public void testParameterTypeLengthInequality() throws Exception {
Collections.singletonList(firstParameter),
Collections.singletonList(mock(GenericTypeDescription.class)),
Collections.singletonList(mock(AnnotationDescription.class)),
null),
MethodDescription.NO_DEFAULT_VALUE),
not(new MethodDescription.Token(FOO,
MODIFIERS,
Collections.singletonList(mock(GenericTypeDescription.class)),
first,
Collections.<ParameterDescription.Token>emptyList(),
Collections.singletonList(mock(GenericTypeDescription.class)),
Collections.singletonList(mock(AnnotationDescription.class)),
null)));
MethodDescription.NO_DEFAULT_VALUE)));
}

@Test
Expand Down

0 comments on commit 2b326db

Please sign in to comment.