Skip to content

Commit

Permalink
Cleaned up matcher use to use short-cut versions whenever possible. U…
Browse files Browse the repository at this point in the history
…nified over application.
  • Loading branch information
Rafael Winterhalter committed Dec 17, 2015
1 parent 98b7a43 commit 4cd8bf4
Show file tree
Hide file tree
Showing 19 changed files with 196 additions and 199 deletions.
@@ -1,25 +1,24 @@
package net.bytebuddy.agent;

import net.bytebuddy.test.utility.ObjectPropertyAssertion;
import org.hamcrest.CoreMatchers;
import org.junit.Test;

import java.util.Arrays;
import java.util.Iterator;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.isA;
import static org.hamcrest.MatcherAssert.assertThat;

public class ByteBuddyAgentAttachmentProviderTest {

private static final String FOO = "foo";

@Test
@SuppressWarnings("unchecked")
public void testSimpleAccessor() throws Exception {
ByteBuddyAgent.AttachmentProvider.Accessor accessor = new ByteBuddyAgent.AttachmentProvider.Accessor.Simple(Void.class, FOO);
assertThat(accessor.isAvailable(), is(true));
assertThat(accessor.getVirtualMachineType(), isA((Class) Void.class));
assertThat(accessor.getVirtualMachineType(), CoreMatchers.<Class<?>>is(Void.class));
assertThat(accessor.getProcessId(), is(FOO));
}

Expand Down
Expand Up @@ -153,12 +153,12 @@ public void setUp() throws Exception {

@Test
public void testPrecondition() throws Exception {
assertThat(describe(first), equalTo(describe(first)));
assertThat(describe(second), equalTo(describe(second)));
assertThat(describe(first), not(equalTo(describe(second))));
assertThat(describe(first).getAnnotationType(), equalTo(describe(second).getAnnotationType()));
assertThat(describe(first).getAnnotationType(), not(equalTo((TypeDescription) new TypeDescription.ForLoadedType(Other.class))));
assertThat(describe(second).getAnnotationType(), not(equalTo((TypeDescription) new TypeDescription.ForLoadedType(Other.class))));
assertThat(describe(first), is(describe(first)));
assertThat(describe(second), is(describe(second)));
assertThat(describe(first), not(describe(second)));
assertThat(describe(first).getAnnotationType(), is(describe(second).getAnnotationType()));
assertThat(describe(first).getAnnotationType(), not((TypeDescription) new TypeDescription.ForLoadedType(Other.class)));
assertThat(describe(second).getAnnotationType(), not((TypeDescription) new TypeDescription.ForLoadedType(Other.class)));
assertThat(describe(first).getAnnotationType().represents(first.annotationType()), is(true));
assertThat(describe(second).getAnnotationType().represents(second.annotationType()), is(true));
}
Expand All @@ -181,13 +181,13 @@ private void assertToString(String actual, Annotation loaded) throws Exception {
public void testHashCode() throws Exception {
assertThat(describe(first).hashCode(), is(describe(first).hashCode()));
assertThat(describe(second).hashCode(), is(describe(second).hashCode()));
assertThat(describe(first).hashCode(), not(is(describe(second).hashCode())));
assertThat(describe(first).hashCode(), not(describe(second).hashCode()));
}

@Test
public void testEquals() throws Exception {
AnnotationDescription identical = describe(first);
assertThat(identical, equalTo(identical));
assertThat(identical, is(identical));
AnnotationDescription equalFirst = mock(AnnotationDescription.class);
when(equalFirst.getAnnotationType()).thenReturn(new TypeDescription.ForLoadedType(first.annotationType()));
when(equalFirst.getValue(Mockito.any(MethodDescription.InDefinedShape.class))).then(new Answer<Object>() {
Expand All @@ -197,7 +197,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
return AnnotationDescription.ForLoadedAnnotation.of(first).getValue(method);
}
});
assertThat(describe(first), equalTo(equalFirst));
assertThat(describe(first), is(equalFirst));
AnnotationDescription equalSecond = mock(AnnotationDescription.class);
when(equalSecond.getAnnotationType()).thenReturn(new TypeDescription.ForLoadedType(first.annotationType()));
when(equalSecond.getValue(Mockito.any(MethodDescription.InDefinedShape.class))).then(new Answer<Object>() {
Expand All @@ -207,7 +207,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
return AnnotationDescription.ForLoadedAnnotation.of(second).getValue(method);
}
});
assertThat(describe(second), equalTo(equalSecond));
assertThat(describe(second), is(equalSecond));
AnnotationDescription equalFirstTypeOnly = mock(AnnotationDescription.class);
when(equalFirstTypeOnly.getAnnotationType()).thenReturn(new TypeDescription.ForLoadedType(Other.class));
when(equalFirstTypeOnly.getValue(Mockito.any(MethodDescription.InDefinedShape.class))).then(new Answer<Object>() {
Expand All @@ -217,13 +217,13 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
return AnnotationDescription.ForLoadedAnnotation.of(first).getValue(method);
}
});
assertThat(describe(first), not(equalTo(equalFirstTypeOnly)));
assertThat(describe(first), not(equalFirstTypeOnly));
AnnotationDescription equalFirstNameOnly = mock(AnnotationDescription.class);
when(equalFirstNameOnly.getAnnotationType()).thenReturn(new TypeDescription.ForLoadedType(first.annotationType()));
when(equalFirstNameOnly.getValue(Mockito.any(MethodDescription.InDefinedShape.class))).thenReturn(null);
assertThat(describe(first), not(equalTo(equalFirstNameOnly)));
assertThat(describe(first), not(equalTo(equalSecond)));
assertThat(describe(first), not(equalTo(new Object())));
assertThat(describe(first), not(equalFirstNameOnly));
assertThat(describe(first), not(equalSecond));
assertThat(describe(first), not(new Object()));
assertThat(describe(first), not(equalTo(null)));
}

Expand All @@ -234,18 +234,18 @@ public void testIllegalMethod() throws Exception {

@Test
public void testLoadedEquals() throws Exception {
assertThat(describe(first).prepare(Sample.class).load(), equalTo(first));
assertThat(describe(first).prepare(Sample.class).load(), equalTo(describe(first).prepare(Sample.class).load()));
assertThat(describe(first).prepare(Sample.class).load(), not(equalTo(describe(second).prepare(Sample.class).load())));
assertThat(describe(second).prepare(Sample.class).load(), equalTo(second));
assertThat(describe(first).prepare(Sample.class).load(), not(equalTo(second)));
assertThat(describe(first).prepare(Sample.class).load(), is(first));
assertThat(describe(first).prepare(Sample.class).load(), is(describe(first).prepare(Sample.class).load()));
assertThat(describe(first).prepare(Sample.class).load(), not(describe(second).prepare(Sample.class).load()));
assertThat(describe(second).prepare(Sample.class).load(), is(second));
assertThat(describe(first).prepare(Sample.class).load(), not(second));
}

@Test
public void testLoadedHashCode() throws Exception {
assertThat(describe(first).prepare(Sample.class).load().hashCode(), equalTo(first.hashCode()));
assertThat(describe(second).prepare(Sample.class).load().hashCode(), equalTo(second.hashCode()));
assertThat(describe(first).prepare(Sample.class).load().hashCode(), not(equalTo(second.hashCode())));
assertThat(describe(first).prepare(Sample.class).load().hashCode(), is(first.hashCode()));
assertThat(describe(second).prepare(Sample.class).load().hashCode(), is(second.hashCode()));
assertThat(describe(first).prepare(Sample.class).load().hashCode(), not(second.hashCode()));
}

@Test
Expand Down
Expand Up @@ -54,39 +54,39 @@ public void assertToString() throws Exception {

@Test
public void assertType() throws Exception {
assertThat(describe(Sample.FIRST).getEnumerationType(), equalTo((TypeDescription) new TypeDescription.ForLoadedType(Sample.class)));
assertThat(describe(Sample.SECOND).getEnumerationType(), equalTo((TypeDescription) new TypeDescription.ForLoadedType(Sample.class)));
assertThat(describe(Sample.FIRST).getEnumerationType(), is((TypeDescription) new TypeDescription.ForLoadedType(Sample.class)));
assertThat(describe(Sample.SECOND).getEnumerationType(), is((TypeDescription) new TypeDescription.ForLoadedType(Sample.class)));
}

@Test
public void assertHashCode() throws Exception {
assertThat(describe(Sample.FIRST).hashCode(), is(Sample.FIRST.name().hashCode() + 31 * new TypeDescription.ForLoadedType(Sample.class).hashCode()));
assertThat(describe(Sample.SECOND).hashCode(), is(Sample.SECOND.name().hashCode() + 31 * new TypeDescription.ForLoadedType(Sample.class).hashCode()));
assertThat(describe(Sample.FIRST).hashCode(), not(is(describe(Sample.SECOND).hashCode())));
assertThat(describe(Sample.FIRST).hashCode(), not(describe(Sample.SECOND).hashCode()));
}

@Test
public void assertEquals() throws Exception {
EnumerationDescription identical = describe(Sample.FIRST);
assertThat(identical, equalTo(identical));
assertThat(identical, is(identical));
EnumerationDescription equalFirst = mock(EnumerationDescription.class);
when(equalFirst.getValue()).thenReturn(Sample.FIRST.name());
when(equalFirst.getEnumerationType()).thenReturn(new TypeDescription.ForLoadedType(Sample.class));
assertThat(describe(Sample.FIRST), equalTo(equalFirst));
assertThat(describe(Sample.FIRST), is(equalFirst));
EnumerationDescription equalSecond = mock(EnumerationDescription.class);
when(equalSecond.getValue()).thenReturn(Sample.SECOND.name());
when(equalSecond.getEnumerationType()).thenReturn(new TypeDescription.ForLoadedType(Sample.class));
assertThat(describe(Sample.SECOND), equalTo(equalSecond));
assertThat(describe(Sample.SECOND), is(equalSecond));
EnumerationDescription equalFirstTypeOnly = mock(EnumerationDescription.class);
when(equalFirstTypeOnly.getValue()).thenReturn(Sample.SECOND.name());
when(equalFirstTypeOnly.getEnumerationType()).thenReturn(new TypeDescription.ForLoadedType(Sample.class));
assertThat(describe(Sample.FIRST), not(equalTo(equalFirstTypeOnly)));
assertThat(describe(Sample.FIRST), not(equalFirstTypeOnly));
EnumerationDescription equalFirstNameOnly = mock(EnumerationDescription.class);
when(equalFirstNameOnly.getValue()).thenReturn(Sample.FIRST.name());
when(equalFirstNameOnly.getEnumerationType()).thenReturn(new TypeDescription.ForLoadedType(Other.class));
assertThat(describe(Sample.FIRST), not(equalTo(equalFirstNameOnly)));
assertThat(describe(Sample.FIRST), not(equalTo(equalSecond)));
assertThat(describe(Sample.FIRST), not(equalTo(new Object())));
assertThat(describe(Sample.FIRST), not(equalFirstNameOnly));
assertThat(describe(Sample.FIRST), not(equalSecond));
assertThat(describe(Sample.FIRST), not(new Object()));
assertThat(describe(Sample.FIRST), not(equalTo(null)));
}

Expand Down
Expand Up @@ -33,9 +33,9 @@ public void setUp() throws Exception {

@Test
public void testPrecondition() throws Exception {
assertThat(describe(first), not(equalTo(describe(second))));
assertThat(describe(first), equalTo(describe(first)));
assertThat(describe(second), equalTo(describe(second)));
assertThat(describe(first), not(describe(second)));
assertThat(describe(first), is(describe(first)));
assertThat(describe(second), is(describe(second)));
assertThat(describe(first), is((FieldDescription) new FieldDescription.ForLoadedField(first)));
assertThat(describe(second), is((FieldDescription) new FieldDescription.ForLoadedField(second)));
}
Expand Down Expand Up @@ -78,31 +78,31 @@ public void testHashCode() throws Exception {
assertThat(describe(second).hashCode(), is(new TypeDescription.ForLoadedType(SecondSample.class).hashCode() + 31 * second.getName().hashCode()));
assertThat(describe(first).hashCode(), is(describe(first).hashCode()));
assertThat(describe(second).hashCode(), is(describe(second).hashCode()));
assertThat(describe(first).hashCode(), not(is(describe(second).hashCode())));
assertThat(describe(first).hashCode(), not(describe(second).hashCode()));
}

@Test
public void testEquals() throws Exception {
FieldDescription identical = describe(first);
assertThat(identical, equalTo(identical));
assertThat(identical, is(identical));
FieldDescription equalFirst = mock(FieldDescription.class);
when(equalFirst.getName()).thenReturn(first.getName());
when(equalFirst.getDeclaringType()).thenReturn(new TypeDescription.ForLoadedType(FirstSample.class));
assertThat(describe(first), equalTo(equalFirst));
assertThat(describe(first), is(equalFirst));
FieldDescription equalSecond = mock(FieldDescription.class);
when(equalSecond.getName()).thenReturn(second.getName());
when(equalSecond.getDeclaringType()).thenReturn(new TypeDescription.ForLoadedType(SecondSample.class));
assertThat(describe(second), equalTo(equalSecond));
assertThat(describe(second), is(equalSecond));
FieldDescription equalFirstTypeOnly = mock(FieldDescription.class);
when(equalFirstTypeOnly.getName()).thenReturn(second.getName());
when(equalFirstTypeOnly.getDeclaringType()).thenReturn(new TypeDescription.ForLoadedType(FirstSample.class));
assertThat(describe(first), not(equalTo(equalFirstTypeOnly)));
assertThat(describe(first), not(equalFirstTypeOnly));
FieldDescription equalFirstNameOnly = mock(FieldDescription.class);
when(equalFirstNameOnly.getName()).thenReturn(first.getName());
when(equalFirstNameOnly.getDeclaringType()).thenReturn(new TypeDescription.ForLoadedType(SecondSample.class));
assertThat(describe(first), not(equalTo(equalFirstNameOnly)));
assertThat(describe(first), not(equalTo(equalSecond)));
assertThat(describe(first), not(equalTo(new Object())));
assertThat(describe(first), not(equalFirstNameOnly));
assertThat(describe(first), not(equalSecond));
assertThat(describe(first), not(new Object()));
assertThat(describe(first), not(equalTo(null)));
}

Expand Down

0 comments on commit 4cd8bf4

Please sign in to comment.