Skip to content

Commit

Permalink
Extended test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafael Winterhalter committed Apr 14, 2015
1 parent e1055c3 commit bfab770
Showing 1 changed file with 81 additions and 3 deletions.
Expand Up @@ -23,6 +23,7 @@
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;

public class MethodRegistryDefaultTest {
Expand All @@ -39,6 +40,9 @@ public class MethodRegistryDefaultTest {
@Mock
private MethodRegistry.Handler.Compiled firstCompiledHandler, secondCompiledHandler;

@Mock
private TypeWriter.MethodPool.Entry firstEntry, secondEntry;

@Mock
private MethodAttributeAppender.Factory firstFactory, secondFactory;

Expand Down Expand Up @@ -97,6 +101,8 @@ public void setUp() throws Exception {
when(secondFactory.make(typeDescription)).thenReturn(secondAppender);
when(instrumentationTargetFactory.make(finding, new MethodList.Explicit(Collections.singletonList(instrumentedMethod))))
.thenReturn(instrumentationTarget);
when(firstCompiledHandler.assemble(firstAppender)).thenReturn(firstEntry);
when(secondCompiledHandler.assemble(secondAppender)).thenReturn(secondEntry);
}

@Test
Expand Down Expand Up @@ -170,9 +176,12 @@ public void testMultipleRegistryDoesNotPrepareMultipleTimes() throws Exception {
MethodRegistry.Prepared methodRegistry = new MethodRegistry.Default()
.append(firstMatcher, firstHandler, firstFactory)
.append(firstMatcher, firstHandler, firstFactory)
.append(secondMatcher, secondHandler, secondFactory)
.append(secondMatcher, firstHandler, secondFactory)
.append(secondMatcher, firstHandler, firstFactory)
.append(secondMatcher, firstHandler, secondFactory)
.append(secondMatcher, secondHandler, secondFactory)
.append(firstMatcher, secondHandler, secondFactory)
.append(firstMatcher, firstHandler, secondFactory)
.append(firstMatcher, secondHandler, firstFactory)
.prepare(firstType, methodLookupEngine, methodFilter);
assertThat(methodRegistry.getInstrumentedType(), is(typeDescription));
assertThat(methodRegistry.getInstrumentedMethods(), is((MethodList) new MethodList.Explicit(Collections.singletonList(instrumentedMethod))));
Expand All @@ -183,11 +192,55 @@ public void testMultipleRegistryDoesNotPrepareMultipleTimes() throws Exception {
}

@Test
public void testCompiled() throws Exception {
public void testCompiledAppendingMatchesFirstAppended() throws Exception {
when(resolvedMethodFilter.matches(instrumentedMethod)).thenReturn(true);
when(firstFilter.matches(instrumentedMethod)).thenReturn(true);
when(secondFilter.matches(instrumentedMethod)).thenReturn(true);
when(resolvedMethodFilter.matches(instrumentedMethod)).thenReturn(true);
MethodRegistry.Compiled methodRegistry = new MethodRegistry.Default()
.append(firstMatcher, firstHandler, firstFactory)
.append(secondMatcher, secondHandler, secondFactory)
.prepare(firstType, methodLookupEngine, methodFilter)
.compile(instrumentationTargetFactory);
assertThat(methodRegistry.getInstrumentedType(), is(typeDescription));
assertThat(methodRegistry.getInstrumentedMethods(), is((MethodList) new MethodList.Explicit(Collections.singletonList(instrumentedMethod))));
assertThat(methodRegistry.getTypeInitializer(), is(typeInitializer));
assertThat(methodRegistry.getLoadedTypeInitializer(), is(loadedTypeInitializer));
verify(firstHandler).prepare(firstType);
verify(secondHandler).prepare(secondType);
verify(firstFactory).make(typeDescription);
verifyZeroInteractions(secondFactory);
assertThat(methodRegistry.target(instrumentedMethod), is(firstEntry));
}

@Test
public void testCompiledPrependingMatchesLastPrepended() throws Exception {
when(resolvedMethodFilter.matches(instrumentedMethod)).thenReturn(true);
when(firstFilter.matches(instrumentedMethod)).thenReturn(true);
when(secondFilter.matches(instrumentedMethod)).thenReturn(true);
when(resolvedMethodFilter.matches(instrumentedMethod)).thenReturn(true);
MethodRegistry.Compiled methodRegistry = new MethodRegistry.Default()
.append(secondMatcher, secondHandler, secondFactory)
.prepend(firstMatcher, firstHandler, firstFactory)
.prepare(firstType, methodLookupEngine, methodFilter)
.compile(instrumentationTargetFactory);
assertThat(methodRegistry.getInstrumentedType(), is(typeDescription));
assertThat(methodRegistry.getInstrumentedMethods(), is((MethodList) new MethodList.Explicit(Collections.singletonList(instrumentedMethod))));
assertThat(methodRegistry.getTypeInitializer(), is(typeInitializer));
assertThat(methodRegistry.getLoadedTypeInitializer(), is(loadedTypeInitializer));
verify(firstHandler).prepare(firstType);
verify(secondHandler).prepare(secondType);
verify(firstFactory).make(typeDescription);
verifyZeroInteractions(secondFactory);
assertThat(methodRegistry.target(instrumentedMethod), is(firstEntry));
}

@Test
public void testCompiledAppendingMatchesSecondAppendedIfFirstDoesNotMatch() throws Exception {
when(resolvedMethodFilter.matches(instrumentedMethod)).thenReturn(true);
when(firstFilter.matches(instrumentedMethod)).thenReturn(false);
when(secondFilter.matches(instrumentedMethod)).thenReturn(true);
when(resolvedMethodFilter.matches(instrumentedMethod)).thenReturn(true);
MethodRegistry.Compiled methodRegistry = new MethodRegistry.Default()
.append(firstMatcher, firstHandler, firstFactory)
.append(secondMatcher, secondHandler, secondFactory)
Expand All @@ -199,6 +252,31 @@ public void testCompiled() throws Exception {
assertThat(methodRegistry.getLoadedTypeInitializer(), is(loadedTypeInitializer));
verify(firstHandler).prepare(firstType);
verify(secondHandler).prepare(secondType);
verifyZeroInteractions(firstFactory);
verify(secondFactory).make(typeDescription);
assertThat(methodRegistry.target(instrumentedMethod), is(secondEntry));
}

@Test
public void testSkipEntryIfNotMatched() throws Exception {
when(resolvedMethodFilter.matches(instrumentedMethod)).thenReturn(true);
when(firstFilter.matches(instrumentedMethod)).thenReturn(false);
when(secondFilter.matches(instrumentedMethod)).thenReturn(false);
when(resolvedMethodFilter.matches(instrumentedMethod)).thenReturn(true);
MethodRegistry.Compiled methodRegistry = new MethodRegistry.Default()
.append(firstMatcher, firstHandler, firstFactory)
.append(secondMatcher, secondHandler, secondFactory)
.prepare(firstType, methodLookupEngine, methodFilter)
.compile(instrumentationTargetFactory);
assertThat(methodRegistry.getInstrumentedType(), is(typeDescription));
assertThat(methodRegistry.getInstrumentedMethods().size(), is(0));
assertThat(methodRegistry.getTypeInitializer(), is(typeInitializer));
assertThat(methodRegistry.getLoadedTypeInitializer(), is(loadedTypeInitializer));
verify(firstHandler).prepare(firstType);
verify(secondHandler).prepare(secondType);
verifyZeroInteractions(firstFactory);
verifyZeroInteractions(secondFactory);
assertThat(methodRegistry.target(instrumentedMethod), is((TypeWriter.MethodPool.Entry) TypeWriter.MethodPool.Entry.ForSkippedMethod.INSTANCE));
}

@Test
Expand Down

0 comments on commit bfab770

Please sign in to comment.