-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ashley Frieze
committed
Nov 26, 2016
1 parent
09910db
commit f5d2af8
Showing
5 changed files
with
511 additions
and
9 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
54 changes: 54 additions & 0 deletions
54
src/main/java/com/greghaskins/spectrum/SpectrumOptions.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,54 @@ | ||
package com.greghaskins.spectrum; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Options that can be applied to a test class annotated with {@link org.junit.runner.RunWith} | ||
* with the {@link Spectrum} runner. E.g.<br> | ||
* <pre><code class="java"> | ||
* @RunWith(Spectrum.class) | ||
* @SpectrumOptions(includeTags={"wip","dev"}) | ||
* public class MyTest { ... } | ||
* </code></pre> | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.TYPE}) | ||
public @interface SpectrumOptions { | ||
String INCLUDE_TAGS_PROPERTY = "spectrum.include.tags"; | ||
String EXCLUDE_TAGS_PROPERTY = "spectrum.exclude.tags"; | ||
String TAGS_SEPARATOR = ","; | ||
|
||
/** | ||
* Allows tags to be selected for controlling the test at coding time. | ||
* This means you can supply tags which relate to, say, Work In Progress | ||
* specs temporarily while developing them. See {@link Spectrum#tag(String...)} | ||
* @return the tags hard coded for inclusion. | ||
*/ | ||
String[] includeTags() default {}; | ||
|
||
/** | ||
* Allows tags to be selected for controlling the test at coding time. | ||
* This means you can supply tags while developing. See {@link Spectrum#tag(String...)} | ||
* @return the tags hard coded for exclusion. | ||
*/ | ||
String[] excludeTags() default {}; | ||
|
||
/** | ||
* Which system property can be used to retrieve tags passed in by | ||
* property. These tags are to be comma separated in the property. | ||
* See {@link Spectrum#tag(String...)} | ||
* @return the system property to use if not default. | ||
*/ | ||
String includeTagsSystemProperty() default INCLUDE_TAGS_PROPERTY; | ||
|
||
/** | ||
* Which system property can be used to retrieve tags passed in by | ||
* property. These tags are to be comma separated in the property. | ||
* See {@link Spectrum#tag(String...)} | ||
* @return the system property to use if not default. | ||
*/ | ||
String excludeTagsSystemProperty() default EXCLUDE_TAGS_PROPERTY; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package com.greghaskins.spectrum; | ||
|
||
import static com.greghaskins.spectrum.Spectrum.excludeTags; | ||
import static com.greghaskins.spectrum.Spectrum.includeTags; | ||
import static com.greghaskins.spectrum.SpectrumOptions.EXCLUDE_TAGS_PROPERTY; | ||
import static com.greghaskins.spectrum.SpectrumOptions.INCLUDE_TAGS_PROPERTY; | ||
import static com.greghaskins.spectrum.SpectrumOptions.TAGS_SEPARATOR; | ||
|
||
import org.junit.runners.model.TestClass; | ||
|
||
import java.util.Arrays; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Decorator that applies tagging statements before calling decoratee. | ||
* Reads the tggging from the System properties and then the options. | ||
*/ | ||
final class TaggingBlock implements Block { | ||
private final SpectrumOptions options; | ||
private final Block decoratee; | ||
|
||
TaggingBlock(final Class<?> klazz, final Block decoratee) { | ||
this.options = new TestClass(klazz).getAnnotation(SpectrumOptions.class); | ||
this.decoratee = decoratee; | ||
} | ||
|
||
@Override | ||
public void run() throws Throwable { | ||
final String[] systemIncludes = readSystemPropertyIncludes(); | ||
final String[] systemExcludes = readSystemPropertyExcludes(); | ||
|
||
final String[] annotationIncludes = readAnnotationIncludes(); | ||
final String[] annotationExcludes = readAnnotationExcludes(); | ||
|
||
// the annotation can provide nothing and so we drop back to | ||
// the system properties - this is done separately | ||
// for includes and excludes | ||
includeTags(firstNonBlank(annotationIncludes, systemIncludes)); | ||
excludeTags(firstNonBlank(annotationExcludes, systemExcludes)); | ||
|
||
// pass control to the decoratee | ||
decoratee.run(); | ||
} | ||
|
||
private String[] firstNonBlank(final String[]... arrays) { | ||
return Arrays.stream(arrays) | ||
.filter(array -> array != null) | ||
.filter(array -> array.length > 0) | ||
.findFirst() | ||
.orElse(new String[] {}); | ||
} | ||
|
||
private String[] fromSystemProperty(final String property) { | ||
return Optional.ofNullable(System.getProperty(property)) | ||
.map(string -> string.split(TAGS_SEPARATOR)) | ||
.filter(TaggingBlock::notArrayWithEmptyValue) | ||
.orElse(null); | ||
} | ||
|
||
private static boolean notArrayWithEmptyValue(final String[] array) { | ||
return !(array.length == 1 && array[0].isEmpty()); | ||
} | ||
|
||
private String[] readSystemPropertyIncludes() { | ||
return fromSystemProperty(Optional.ofNullable(options) | ||
.map(SpectrumOptions::includeTagsSystemProperty) | ||
.orElse(INCLUDE_TAGS_PROPERTY)); | ||
} | ||
|
||
private String[] readSystemPropertyExcludes() { | ||
return fromSystemProperty(Optional.ofNullable(options) | ||
.map(SpectrumOptions::excludeTagsSystemProperty) | ||
.orElse(EXCLUDE_TAGS_PROPERTY)); | ||
} | ||
|
||
private String[] readAnnotationIncludes() { | ||
return Optional.ofNullable(options) | ||
.map(SpectrumOptions::includeTags) | ||
.orElse(null); | ||
} | ||
|
||
private String[] readAnnotationExcludes() { | ||
return Optional.ofNullable(options) | ||
.map(SpectrumOptions::excludeTags) | ||
.orElse(null); | ||
} | ||
|
||
} |
Oops, something went wrong.