Skip to content

Commit

Permalink
Fix resource loading in the ES test framework. (#98)
Browse files Browse the repository at this point in the history
For static indices, we would end up calling testInstance.getClass().getClass().getResource() (indirectly). Somehow java 8 is fine with this and can locate the resource (off java.lang.Class), but java 11 is not okay with it (which is what I would expect).

Fix the code by being explicit about which class to use.
  • Loading branch information
John Plaisted authored May 25, 2021
1 parent 682a19c commit 615bab7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,15 @@ public void beforeAll(ExtensionContext context) throws Exception {
}, ReflectionUtils.HierarchyTraversalMode.TOP_DOWN);

final SearchIndexFactory indexFactory = new SearchIndexFactory(_connection);
final List<SearchIndex<?>> indices = createIndices(indexFactory, context.getRequiredTestClass(), fields,
fieldName -> String.format("%s_%s_%s", fieldName, testClass.getSimpleName(), System.currentTimeMillis()));
final List<SearchIndex<?>> indices =
createIndices(indexFactory, context.getRequiredTestClass(), context.getRequiredTestClass(), fields,
fieldName -> String.format("%s_%s_%s", fieldName, testClass.getSimpleName(), System.currentTimeMillis()));
store.put(STATIC_INDICIES, indices);
}

private List<SearchIndex<?>> createIndices(@Nonnull SearchIndexFactory indexFactory, @Nonnull Object testInstance,
@Nonnull List<Field> fields, @Nonnull Function<String, String> nameFn) throws Exception {
private List<SearchIndex<?>> createIndices(@Nonnull SearchIndexFactory indexFactory, @Nonnull Class<?> testClass,
@Nonnull Object testInstance, @Nonnull List<Field> fields, @Nonnull Function<String, String> nameFn)
throws Exception {
final List<SearchIndex<?>> indices = new ArrayList<>();

for (Field field : fields) {
Expand All @@ -92,10 +94,10 @@ private List<SearchIndex<?>> createIndices(@Nonnull SearchIndexFactory indexFact
final String indexName = nameFn.apply(field.getName()).replaceAll("^_*", "").toLowerCase();

final SearchIndexSettings settings = field.getAnnotation(SearchIndexSettings.class);
final String settingsJson = settings == null ? null : loadResource(testInstance.getClass(), settings.value());
final String settingsJson = settings == null ? null : loadResource(testClass, settings.value());

final SearchIndexMappings mappings = field.getAnnotation(SearchIndexMappings.class);
final String mappingsJson = mappings == null ? null : loadResource(testInstance.getClass(), mappings.value());
final String mappingsJson = mappings == null ? null : loadResource(testClass, mappings.value());

final SearchIndex<?> index =
indexFactory.createIndex(searchIndexType.value(), indexName, settingsJson, mappingsJson);
Expand Down Expand Up @@ -164,9 +166,10 @@ public void beforeEach(ExtensionContext context) throws Exception {
}, ReflectionUtils.HierarchyTraversalMode.TOP_DOWN);

final SearchIndexFactory indexFactory = new SearchIndexFactory(_connection);
final List<SearchIndex<?>> indices = createIndices(indexFactory, context.getRequiredTestInstance(), fields,
fieldName -> String.format("%s_%s_%s_%s", fieldName, context.getRequiredTestMethod().getName(),
context.getRequiredTestClass().getSimpleName(), System.currentTimeMillis()));
final List<SearchIndex<?>> indices =
createIndices(indexFactory, context.getRequiredTestClass(), context.getRequiredTestInstance(), fields,
fieldName -> String.format("%s_%s_%s_%s", fieldName, context.getRequiredTestMethod().getName(),
context.getRequiredTestClass().getSimpleName(), System.currentTimeMillis()));
store.put(INDICIES, indices);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,15 @@ public void beforeAll(ExtensionContext context) throws Exception {
}, ReflectionUtils.HierarchyTraversalMode.TOP_DOWN);

final SearchIndexFactory indexFactory = new SearchIndexFactory(_connection);
final List<SearchIndex<?>> indices = createIndices(indexFactory, context.getRequiredTestClass(), fields,
fieldName -> String.format("%s_%s_%s", fieldName, testClass.getSimpleName(), System.currentTimeMillis()));
final List<SearchIndex<?>> indices =
createIndices(indexFactory, context.getRequiredTestClass(), context.getRequiredTestClass(), fields,
fieldName -> String.format("%s_%s_%s", fieldName, testClass.getSimpleName(), System.currentTimeMillis()));
store.put(STATIC_INDICIES, indices);
}

private List<SearchIndex<?>> createIndices(@Nonnull SearchIndexFactory indexFactory, @Nonnull Object testInstance,
@Nonnull List<Field> fields, @Nonnull Function<String, String> nameFn) throws Exception {
private List<SearchIndex<?>> createIndices(@Nonnull SearchIndexFactory indexFactory,
@Nonnull Class<?> testClass, @Nonnull Object testInstance, @Nonnull List<Field> fields,
@Nonnull Function<String, String> nameFn) throws Exception {
final List<SearchIndex<?>> indices = new ArrayList<>();

for (Field field : fields) {
Expand All @@ -91,10 +93,10 @@ private List<SearchIndex<?>> createIndices(@Nonnull SearchIndexFactory indexFact
final String indexName = nameFn.apply(field.getName()).replaceAll("^_*", "").toLowerCase();

final SearchIndexSettings settings = field.getAnnotation(SearchIndexSettings.class);
final String settingsJson = settings == null ? null : loadResource(testInstance.getClass(), settings.value());
final String settingsJson = settings == null ? null : loadResource(testClass, settings.value());

final SearchIndexMappings mappings = field.getAnnotation(SearchIndexMappings.class);
final String mappingsJson = mappings == null ? null : loadResource(testInstance.getClass(), mappings.value());
final String mappingsJson = mappings == null ? null : loadResource(testClass, mappings.value());

final DocType docType = field.getAnnotation(DocType.class);
final String docTypeStr = docType == null ? null : docType.value();
Expand Down Expand Up @@ -166,9 +168,10 @@ public void beforeEach(ExtensionContext context) throws Exception {
}, ReflectionUtils.HierarchyTraversalMode.TOP_DOWN);

final SearchIndexFactory indexFactory = new SearchIndexFactory(_connection);
final List<SearchIndex<?>> indices = createIndices(indexFactory, context.getRequiredTestInstance(), fields,
fieldName -> String.format("%s_%s_%s_%s", fieldName, context.getRequiredTestMethod().getName(),
context.getRequiredTestClass().getSimpleName(), System.currentTimeMillis()));
final List<SearchIndex<?>> indices =
createIndices(indexFactory, context.getRequiredTestClass(), context.getRequiredTestInstance(), fields,
fieldName -> String.format("%s_%s_%s_%s", fieldName, context.getRequiredTestMethod().getName(),
context.getRequiredTestClass().getSimpleName(), System.currentTimeMillis()));
store.put(INDICIES, indices);
}

Expand Down

0 comments on commit 615bab7

Please sign in to comment.