Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove mmdb files from test resources (#71)
* Do not run tests if maxmind database resources are missing Add a custom JUnit test runner and a custom condition annotation to check if the required resources exist. * Remove mmdb resources and add mmdb files to gitignore
- Loading branch information
Showing
7 changed files
with
132 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ node_modules | |
node | ||
build | ||
cache/ | ||
*.mmdb |
84 changes: 84 additions & 0 deletions
84
src/test/java/org/graylog/plugins/map/ConditionalRunner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package org.graylog.plugins.map; | ||
|
||
import org.junit.runners.BlockJUnit4ClassRunner; | ||
import org.junit.runners.model.FrameworkMethod; | ||
import org.junit.runners.model.InitializationError; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* JUnit test runner based on {@link BlockJUnit4ClassRunner} that disables tests based on condition annotations. | ||
* <p> | ||
* Supported conditions: | ||
* <ul> | ||
* <li>{@link ResourceExistsCondition} - Disable tests if any given resource doesn't exist | ||
* </ul> | ||
*/ | ||
public class ConditionalRunner extends BlockJUnit4ClassRunner { | ||
private static final Logger LOG = LoggerFactory.getLogger(ConditionalRunner.class); | ||
|
||
public ConditionalRunner(Class<?> clazz) throws InitializationError { | ||
super(clazz); | ||
} | ||
|
||
private List<Class<?>> getClassAndSuperclasses(final Class<?> testClass) { | ||
final List<Class<?>> classes = new ArrayList<>(); | ||
|
||
Class<?> clazz = testClass; | ||
while (clazz != null) { | ||
classes.add(clazz); | ||
clazz = clazz.getSuperclass(); | ||
} | ||
|
||
return classes; | ||
} | ||
|
||
private Stream<String> missingResourcesStream(final ResourceExistsCondition condition, final Class<?> clazz) { | ||
if (condition == null) { | ||
return Stream.empty(); | ||
} | ||
|
||
final Stream<String> resources = Arrays.stream(condition.value()); | ||
|
||
// Return all missing resources | ||
return resources.filter(resource -> clazz.getResource(resource) == null); | ||
} | ||
|
||
private Set<String> missingResources(final FrameworkMethod method) { | ||
// Check method class and all its superclasses for annotations | ||
final Stream<String> missingClassResourcesStream = getClassAndSuperclasses(method.getDeclaringClass()).stream() | ||
.flatMap(clazz -> { | ||
final ResourceExistsCondition condition = clazz.getAnnotation(ResourceExistsCondition.class); | ||
|
||
return missingResourcesStream(condition, clazz); | ||
}); | ||
|
||
// Check this method for annotations | ||
final ResourceExistsCondition methodCondition = method.getAnnotation(ResourceExistsCondition.class); | ||
|
||
return Stream.concat( | ||
missingClassResourcesStream, | ||
missingResourcesStream(methodCondition, method.getDeclaringClass()) | ||
).collect(Collectors.toSet()); | ||
} | ||
|
||
@Override | ||
protected boolean isIgnored(FrameworkMethod child) { | ||
final Set<String> missingResources = missingResources(child); | ||
|
||
if (!missingResources.isEmpty()) { | ||
LOG.warn("Not running test {}#{}() because of missing resources: {}", | ||
child.getDeclaringClass().getCanonicalName(), child.getName(), missingResources); | ||
return true; | ||
} | ||
|
||
return super.isIgnored(child); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/test/java/org/graylog/plugins/map/ResourceExistsCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.graylog.plugins.map; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Can be used in conjunction with {@link ConditionalRunner} to disable tests if one or more resources doesn't exist. | ||
* <p> | ||
* Example: | ||
* <pre>{@code | ||
* @literal @RunWith(ConditionalRunner.class) | ||
* @literal @ResourceExistsCondition({"/file1.txt", "/file2.txt"}) | ||
* public class GeoIpResolverEngineTest { | ||
* @literal @Test | ||
* @literal @ResourceExistsCondition("/file3.txt") | ||
* public void test() { | ||
* } | ||
* } | ||
* }</pre> | ||
*/ | ||
@Target({ElementType.METHOD, ElementType.TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface ResourceExistsCondition { | ||
/** List of resources that must exist to run the tests. */ | ||
String[] value(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.