-
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.
First cut - demonstrates the hooks mechanism.
- Loading branch information
Ashley Frieze
committed
Dec 4, 2016
1 parent
903d5a6
commit d947193
Showing
31 changed files
with
581 additions
and
139 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
src/main/java/com/greghaskins/spectrum/AbstractSupplyingHook.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,47 @@ | ||
package com.greghaskins.spectrum; | ||
|
||
import static com.greghaskins.spectrum.Spectrum.assertSpectrumInTestMode; | ||
|
||
import com.greghaskins.spectrum.model.Singleton; | ||
|
||
/** | ||
* A base class for supplying hooks to use. Override before or after. Return the singleton | ||
* value from the before method. | ||
* You can use this to write any plugin which needs to make a value visible to the specs. | ||
* This is not the only way to achieve that - you can also build from {@link SupplyingHook} | ||
* but this captures the template for a complex hook. | ||
*/ | ||
public class AbstractSupplyingHook<T> extends Singleton<T> implements SupplyingHook<T> { | ||
/** | ||
* Override this to supply behaviour for before the block is run. | ||
* @return the value that the singleton will store to supply | ||
*/ | ||
protected T before() { | ||
return null; | ||
} | ||
|
||
/** | ||
* Override this to supply behaviour for after the block is run. | ||
*/ | ||
protected void after() {} | ||
|
||
/** | ||
* Template method for a hook which supplies. | ||
* @param block the inner block that will be run | ||
* @throws Throwable on error | ||
*/ | ||
@Override | ||
public void acceptOrThrow(Block block) throws Throwable { | ||
set(before()); | ||
block.run(); | ||
after(); | ||
clear(); | ||
} | ||
|
||
@Override | ||
public T get() { | ||
assertSpectrumInTestMode(); | ||
|
||
return super.get(); | ||
} | ||
} |
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,41 @@ | ||
package com.greghaskins.spectrum; | ||
|
||
import com.greghaskins.spectrum.internal.Atomic; | ||
import com.greghaskins.spectrum.internal.Child; | ||
import com.greghaskins.spectrum.internal.FailureDetectingRunListener; | ||
import com.greghaskins.spectrum.internal.Parent; | ||
import com.greghaskins.spectrum.model.TaggingState; | ||
|
||
import org.junit.runner.Description; | ||
import org.junit.runner.notification.RunNotifier; | ||
|
||
/** | ||
* Subclass of {@link Suite} that represent the fact that some tests are composed | ||
* of interrelated steps which add up to a single test. | ||
*/ | ||
final class CompositeTest extends Suite implements Atomic { | ||
/** | ||
* Constructs a Composite Test, which is a suite run as an atomic test. | ||
* @param description of the test | ||
* @param parent parent suite | ||
* @param tagging tagging state to inherit from parent | ||
*/ | ||
CompositeTest(final Description description, final Parent parent, final TaggingState tagging) { | ||
super(description, parent, CompositeTest::abortOnFailureChildRunner, tagging); | ||
} | ||
|
||
private static void abortOnFailureChildRunner(final Suite suite, final RunNotifier runNotifier) { | ||
FailureDetectingRunListener listener = new FailureDetectingRunListener(); | ||
runNotifier.addListener(listener); | ||
try { | ||
for (Child child : suite.children) { | ||
if (listener.hasFailedYet()) { | ||
child.ignore(); | ||
} | ||
suite.runChild(child, runNotifier); | ||
} | ||
} finally { | ||
runNotifier.removeListener(listener); | ||
} | ||
} | ||
} |
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,11 @@ | ||
package com.greghaskins.spectrum; | ||
|
||
/** | ||
* A hook allows you to inject functionality before and/or after a {@link Block}. | ||
* Just implement the {@link ThrowingConsumer#acceptOrThrow(Object)} method and | ||
* call {@link Block#run()} within your implementation. | ||
* If your hook is going to provide an object to the running test, then implement | ||
* {@link SupplyingHook} or subclass {@link AbstractSupplyingHook}. | ||
*/ | ||
public interface Hook extends ThrowingConsumer<Block> { | ||
} |
26 changes: 0 additions & 26 deletions
26
src/main/java/com/greghaskins/spectrum/NotifyingBlock.java
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.