Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #562 from sonatype/nexus-5296-fix-IT

[NEXUS-5296] Provide additional info about invocations in case of failing assertions
  • Loading branch information...
commit fd291a4d3076dbb22012471842dc8fc472de5786 2 parents 10a2e32 + d7595b8
Alin Dreghiciu adreghiciu authored
106 ...rent/nexus-indexer-lucene-plugin/src/test/java/org/sonatype/nexus/index/Nexus5249IndexerManagerTest.java
@@ -12,11 +12,17 @@
12 12 */
13 13 package org.sonatype.nexus.index;
14 14
  15 +import static org.hamcrest.MatcherAssert.assertThat;
  16 +import static org.hamcrest.Matchers.is;
  17 +import static org.hamcrest.Matchers.iterableWithSize;
  18 +
15 19 import java.io.FileNotFoundException;
16 20 import java.io.IOException;
17 21 import java.lang.reflect.InvocationHandler;
18 22 import java.lang.reflect.Method;
19 23 import java.lang.reflect.Proxy;
  24 +import java.util.Arrays;
  25 +import java.util.List;
20 26
21 27 import javax.annotation.Nullable;
22 28
@@ -25,6 +31,10 @@
25 31 import org.apache.maven.index.context.IndexingContext;
26 32 import org.apache.maven.index.updater.IndexUpdateRequest;
27 33 import org.apache.maven.index.updater.IndexUpdater;
  34 +import org.hamcrest.Description;
  35 +import org.hamcrest.MatcherAssert;
  36 +import org.hamcrest.Matchers;
  37 +import org.hamcrest.TypeSafeMatcher;
28 38 import org.junit.Assert;
29 39 import org.junit.Test;
30 40 import org.sonatype.nexus.proxy.maven.MavenProxyRepository;
@@ -35,6 +45,7 @@
35 45 import org.sonatype.nexus.util.CompositeException;
36 46
37 47 import com.google.common.base.Predicate;
  48 +import com.google.common.collect.Lists;
38 49
39 50 /**
40 51 * Test for NEXUS-5249 and related ones (see linked issues). In general, we ensure that 404 happened during remote
@@ -203,7 +214,7 @@ public void remoteNon404ResponseFailsProcessingAtTheEnd()
203 214 catch ( IOException e )
204 215 {
205 216 // ensure we fetched from one we wanted (failingRepository)
206   - Assert.assertEquals( indexedProxyRepositories, fetchCountingInvocationHandler.getInvocationCount() );
  217 + assertThat( fetchCountingInvocationHandler, new InvocationMatcher( indexedProxyRepositories ) );
207 218 // ensure we scanned all the repositories (minus the one failed, as it failed _BEFORE_ scan invocation)
208 219 Assert.assertEquals( indexedRepositories - 1, scanCountingInvocationHandler.getInvocationCount() );
209 220 // ensure we have composite exception
@@ -267,11 +278,18 @@ public Object invoke( final Object proxy, final Method method, final Object[] ar
267 278
268 279 private int count;
269 280
  281 + private List<Object[]> invocationsArguments;
  282 +
  283 + private List<StackTraceElement[]> invocationsStackTraces;
  284 +
  285 +
270 286 public CountingInvocationHandler( final InvocationHandler delegate, final Method countedMethod )
271 287 {
272 288 super( delegate );
273 289 this.method = countedMethod;
274 290 this.count = 0;
  291 + this.invocationsArguments = Lists.newArrayList();
  292 + this.invocationsStackTraces = Lists.newArrayList();
275 293 }
276 294
277 295 @Override
@@ -281,6 +299,13 @@ public Object invoke( final Object proxy, final Method method, final Object[] ar
281 299 if ( method.equals( this.method ) )
282 300 {
283 301 count++;
  302 + invocationsArguments.add( args );
  303 +
  304 + final StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
  305 + final StackTraceElement[] actualStackTrace = new StackTraceElement[stackTrace.length - 3];
  306 + System.arraycopy( stackTrace, 3, actualStackTrace, 0, stackTrace.length - 3 );
  307 +
  308 + invocationsStackTraces.add( actualStackTrace );
284 309 }
285 310 return super.invoke( proxy, method, args );
286 311 }
@@ -327,4 +352,83 @@ public Object invoke( final Object proxy, final Method method, final Object[] ar
327 352 }
328 353 }
329 354 }
  355 +
  356 + private class InvocationMatcher
  357 + extends TypeSafeMatcher<CountingInvocationHandler>
  358 + {
  359 +
  360 + private final int expectedCount;
  361 +
  362 + InvocationMatcher( final int expectedCount )
  363 + {
  364 + this.expectedCount = expectedCount;
  365 + }
  366 +
  367 + @Override
  368 + protected boolean matchesSafely( final CountingInvocationHandler item )
  369 + {
  370 + return item.getInvocationCount() == expectedCount;
  371 + }
  372 +
  373 + @Override
  374 + public void describeTo( final Description description )
  375 + {
  376 + description.appendText( "to have " ).appendValue( expectedCount ).appendText( " invocations" );
  377 + }
  378 +
  379 + @Override
  380 + protected void describeMismatchSafely( final CountingInvocationHandler item,
  381 + final Description mismatchDescription )
  382 + {
  383 + mismatchDescription
  384 + .appendText( "had " )
  385 + .appendValue( item.getInvocationCount() )
  386 + .appendText(" invocations of method " )
  387 + .appendText( item.method.getDeclaringClass().getName() )
  388 + .appendText( "#" )
  389 + .appendText( item.method.getName() )
  390 + .appendText( "\n" );
  391 +
  392 + if ( item.invocationsArguments.size() > 0 )
  393 + {
  394 + for ( int i = 0; i < item.invocationsArguments.size(); i++ )
  395 + {
  396 + mismatchDescription.appendText( "\n" ).appendText( "Invocation " ).appendValue( i + 1 ).appendText(
  397 + ":\n" );
  398 + mismatchDescription.appendText( "\t Arguments:\n" );
  399 + for ( Object argument : item.invocationsArguments.get( i ) )
  400 + {
  401 + if ( argument instanceof IndexUpdateRequest )
  402 + {
  403 + IndexUpdateRequest arg = (IndexUpdateRequest) argument;
  404 + mismatchDescription
  405 + .appendText( "\t\t " )
  406 + .appendText( IndexUpdateRequest.class.getSimpleName() )
  407 + .appendText( "->" )
  408 + .appendText( IndexingContext.class.getSimpleName() )
  409 + .appendText( " repository=" ).appendValue( arg.getIndexingContext().getRepositoryId() )
  410 + .appendText( "\n" );
  411 + }
  412 + else
  413 + {
  414 + mismatchDescription
  415 + .appendText( "\t\t " )
  416 + .appendText( argument.toString() )
  417 + .appendText( "\n" );
  418 + }
  419 + }
  420 + mismatchDescription.appendText( "\t Stack trace:\n" );
  421 + for ( StackTraceElement element : item.invocationsStackTraces.get( i ) )
  422 + {
  423 + mismatchDescription
  424 + .appendText( "\t\t " )
  425 + .appendText( element.toString() )
  426 + .appendText( "\n" );
  427 + }
  428 + }
  429 + mismatchDescription.appendText( "\n" );
  430 + }
  431 + }
  432 + }
  433 +
330 434 }

0 comments on commit fd291a4

Please sign in to comment.
Something went wrong with that request. Please try again.