Skip to content

Commit

Permalink
HSEARCH-2434 In ES schema validation ITs, do not rely on indexes star…
Browse files Browse the repository at this point in the history
…ting in a particular order

The index managers are stored in a hashmap, whose order is undefined.
Strangely, our tests seem to have been working until now.
  • Loading branch information
yrodiere committed Mar 21, 2017
1 parent 837d79e commit 321472d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -596,12 +596,16 @@ public void multipleErrors_multipleIndexManagers() throws Exception {

thrown.expect(
isException( ElasticsearchSchemaValidationException.class )
.withMainOrSuppressed(
isException( ElasticsearchSchemaValidationException.class )
.withMessage( VALIDATION_FAILED_MESSAGE_ID )
.withMessage(
"\nindex 'org.hibernate.search.elasticsearch.test.elasticsearchschemavalidationit$simplebooleanentity', mapping 'org.hibernate.search.elasticsearch.test.ElasticsearchSchemaValidationIT$SimpleBooleanEntity':"
+ "\n\tInvalid value for attribute 'dynamic'. Expected 'STRICT', actual is 'FALSE'"
)
.withSuppressed(
.build()
)
.withMainOrSuppressed(
isException( ElasticsearchSchemaValidationException.class )
.withMessage( VALIDATION_FAILED_MESSAGE_ID )
.withMessage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
import org.hamcrest.CoreMatchers;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeDiagnosingMatcher;
import org.hamcrest.TypeSafeMatcher;
import org.hibernate.search.util.impl.CollectionHelper;

/**
* @author Yoann Rodiere
Expand All @@ -34,6 +36,10 @@ private ExceptionMatcherBuilder(Class<? extends Throwable> clazz) {
matchers.add( CoreMatchers.<Throwable>instanceOf( clazz ) );
}

public ExceptionMatcherBuilder withMainOrSuppressed(Matcher<?> matcher) {
return matching( mainOrSuppressed( matcher ) );
}

public ExceptionMatcherBuilder withSuppressed(Matcher<?> matcher) {
suppressedMatchers.add( matcher );
return this;
Expand All @@ -44,16 +50,16 @@ public ExceptionMatcherBuilder causedBy(Class<? extends Throwable> clazz) {
}

public Matcher<? super Throwable> build() {
if ( matchers.size() == 1 && suppressedMatchers.isEmpty() ) {
if ( !suppressedMatchers.isEmpty() ) {
@SuppressWarnings("unchecked")
Matcher<? super Throwable>[] suppressedMatchersAsArray = castedSuppressedMatchers().toArray( new Matcher[suppressedMatchers.size()] );
ExceptionMatcherBuilder.this.matching( hasSuppressed( CoreMatchers.hasItems( suppressedMatchersAsArray ) ) );
suppressedMatchers.clear();
}
if ( matchers.size() == 1 ) {
return castedMatchers().get( 0 );
}
else {
if ( !suppressedMatchers.isEmpty() ) {
@SuppressWarnings("unchecked")
Matcher<? super Throwable>[] suppressedMatchersAsArray = castedSuppressedMatchers().toArray( new Matcher[suppressedMatchers.size()] );
ExceptionMatcherBuilder.this.matching( hasSuppressed( CoreMatchers.hasItems( suppressedMatchersAsArray ) ) );
suppressedMatchers.clear();
}
return CoreMatchers.allOf( castedMatchers() );
}
}
Expand All @@ -68,8 +74,8 @@ private List<Matcher<? super Throwable>> castedSuppressedMatchers() {
return new ArrayList<Matcher<? super Throwable>>( (List) suppressedMatchers );
}

public ExceptionMatcherBuilder matching(final Matcher<?> messageMatcher) {
matchers.add( messageMatcher );
public ExceptionMatcherBuilder matching(final Matcher<?> throwableMatcher) {
matchers.add( throwableMatcher );
return this;
}

Expand Down Expand Up @@ -194,4 +200,46 @@ protected void describeMismatchSafely(Throwable item, Description description) {
private static Matcher<Throwable> hasSuppressed(Matcher<?> suppressedMatcher) {
return new ThrowableSuppressedMatcher( suppressedMatcher );
}

public static class ThrowableMainOrSuppressedMatcher extends TypeSafeDiagnosingMatcher<Throwable> {

private final Matcher<?> mainOrSuppressedMatcher;

public ThrowableMainOrSuppressedMatcher(Matcher<?> suppressedMatcher) {
this.mainOrSuppressedMatcher = suppressedMatcher;
}

@Override
public void describeTo(Description description) {
description.appendText( "exception (or one of its suppressed exceptions) " );
description.appendDescriptionOf( mainOrSuppressedMatcher );
}

@Override
protected boolean matchesSafely(Throwable item, Description mismatchDescription) {
Throwable[] suppressedArray = item.getSuppressed();
List<Throwable> mainAndSuppressed = CollectionHelper.newArrayList( suppressedArray.length + 1 );
mainAndSuppressed.add( item );
for ( Throwable suppressed : suppressedArray ) {
mainAndSuppressed.add( suppressed );
}

boolean first = true;
for ( Throwable element : mainAndSuppressed ) {
if ( mainOrSuppressedMatcher.matches( element ) ) {
return true;
}
if ( !first ) {
mismatchDescription.appendText( ", " );
}
mainOrSuppressedMatcher.describeMismatch( element, mismatchDescription );
first = false;
}
return false;
}
}

private static Matcher<Throwable> mainOrSuppressed(Matcher<?> mainOrSuppressedMatcher) {
return new ThrowableMainOrSuppressedMatcher( mainOrSuppressedMatcher );
}
}

0 comments on commit 321472d

Please sign in to comment.