-
Notifications
You must be signed in to change notification settings - Fork 6
Fix supplier lifecycle in executor #127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
163 changes: 163 additions & 0 deletions
163
src/main/java/com/pdsl/executors/ColorizedLoggerObserver.java
This file contains hidden or 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,163 @@ | ||
| package com.pdsl.executors; | ||
|
|
||
| import com.pdsl.logging.AnsiTerminalColorHelper; | ||
| import com.pdsl.logging.PdslThreadSafeOutputStream; | ||
| import com.pdsl.reports.MetadataTestRunResults; | ||
| import com.pdsl.specifications.PolymorphicDslTransformationException; | ||
| import com.pdsl.testcases.TestCase; | ||
| import com.pdsl.testcases.TestSection; | ||
| import org.antlr.v4.runtime.tree.ParseTreeListener; | ||
| import org.antlr.v4.runtime.tree.ParseTreeVisitor; | ||
| import org.antlr.v4.runtime.tree.ParseTreeWalker; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.nio.charset.Charset; | ||
| import java.util.Collection; | ||
|
|
||
| /** | ||
| * A logger for the progress of test execution where the output has color. | ||
| * | ||
| * The underlying implementation uses ANSII escape characters which may not be supported by all terminals. | ||
| */ | ||
| public class ColorizedLoggerObserver implements ExecutorObserver { | ||
|
|
||
| private static final PdslThreadSafeOutputStream stream = new PdslThreadSafeOutputStream(); | ||
| private static final String exceptionMessage = "Could not log!"; | ||
| private final Charset charset; | ||
| private final byte[] RESET; | ||
|
|
||
| public ColorizedLoggerObserver() { | ||
| this.charset = Charset.defaultCharset(); | ||
| this.RESET = AnsiTerminalColorHelper.RESET.getBytes(charset); | ||
| } | ||
|
|
||
| public ColorizedLoggerObserver(Charset charset) { | ||
| this.charset = charset; | ||
| this.RESET = AnsiTerminalColorHelper.RESET.getBytes(charset); | ||
| } | ||
|
|
||
| private void notifyStreams(InputStream inputStream) { | ||
| try { | ||
| stream.write(inputStream.readAllBytes()); | ||
| } catch (IOException e) { | ||
| throw new PolymorphicDslTransformationException(exceptionMessage, e); | ||
| } | ||
| } | ||
|
|
||
| private void notifyStreams(byte[] bytes) { | ||
| try { | ||
| stream.write(bytes); | ||
| } catch (IOException e) { | ||
| throw new PolymorphicDslTransformationException(exceptionMessage, e); | ||
| } | ||
| } | ||
|
|
||
| private void notifyStreams(String str) { | ||
| try { | ||
| stream.write(str.getBytes(Charset.defaultCharset())); | ||
| } catch (IOException e) { | ||
| throw new PolymorphicDslTransformationException(exceptionMessage, e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void onBeforeTestCase(TestCase testCase) { | ||
| notifyStreams(AnsiTerminalColorHelper.YELLOW | ||
| + String.format("%s%n%s%n", testCase.getOriginalSource(), testCase.getTestTitle() | ||
| + AnsiTerminalColorHelper.RESET)); | ||
| } | ||
|
|
||
| @Override | ||
| public void onTestCaseSuccess(TestCase testCase) { | ||
| notifyStreams( | ||
| (AnsiTerminalColorHelper.GREEN + "All Sentences are parsed." + "\n" | ||
| + AnsiTerminalColorHelper.RESET).getBytes(charset)); | ||
| } | ||
|
|
||
| private void beforePhrase(TestSection testSection) { | ||
| if (testSection.getMetaData().isPresent()) { | ||
| notifyStreams(AnsiTerminalColorHelper.CYAN.getBytes(charset)); | ||
| notifyStreams(testSection.getMetaData().get()); | ||
| notifyStreams(RESET); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void onBeforePhrase(ParseTreeVisitor<?> visitor, TestSection testSection) { | ||
| beforePhrase(testSection); | ||
| } | ||
|
|
||
| @Override | ||
| public void onAfterPhrase(ParseTreeListener listener, ParseTreeWalker walker, TestSection testSection) { | ||
| afterPhrase(testSection); | ||
| } | ||
|
|
||
| @Override | ||
| public void onAfterPhrase(ParseTreeVisitor<?> visitor, TestSection testSection) { | ||
| afterPhrase(testSection); | ||
| } | ||
|
|
||
| private void afterPhrase(TestSection testSection) { | ||
| notifyStreams( | ||
| (AnsiTerminalColorHelper.GREEN + testSection.getPhrase().getParseTree().getText() + "\n" | ||
| + AnsiTerminalColorHelper.RESET).getBytes(charset)); | ||
| } | ||
|
|
||
| @Override | ||
| public void onPhraseFailure(ParseTreeListener listener, TestSection testSection, TestCase testCase, Throwable exception) { | ||
| onFailure(testSection); | ||
| } | ||
|
|
||
| private void onFailure(TestSection testSection) { | ||
| notifyStreams( | ||
| (AnsiTerminalColorHelper.BRIGHT_RED + testSection.getPhrase().getParseTree().getText() + "\n" | ||
| + AnsiTerminalColorHelper.RESET).getBytes(charset)); | ||
| } | ||
|
|
||
| @Override | ||
| public void onPhraseFailure(ParseTreeVisitor<?> visitor, TestSection testSection, TestCase testCase, Throwable exception) { | ||
| onFailure(testSection); | ||
| } | ||
|
|
||
| @Override | ||
| public void onBeforeTestSuite(Collection<? extends TestCase> testCases, ParseTreeVisitor<?> visitor, String context) { | ||
| logBeforeSuite(); | ||
| } | ||
|
|
||
| @Override | ||
| public void onBeforeTestSuite(Collection<? extends TestCase> testCases, ParseTreeListener listener, String context) { | ||
| logBeforeSuite(); | ||
| } | ||
|
|
||
| @Override | ||
| public void onAfterTestSuite(Collection<? extends TestCase> testCases, ParseTreeVisitor<?> visitor, MetadataTestRunResults results, String context) { | ||
| logAfterSuite(results); | ||
| } | ||
|
|
||
| @Override | ||
| public void onAfterTestSuite(Collection<? extends TestCase> testCases, ParseTreeListener listener, MetadataTestRunResults results, String context) { | ||
| logAfterSuite(results); | ||
| } | ||
|
|
||
| @Override | ||
| public void onDuplicateSkipped(TestCase testCase) { | ||
| notifyStreams(String.format( | ||
| "A test was skipped because after filtering it duplicated an earlier run test!%n\t%s", | ||
| testCase.getTestTitle())); | ||
| } | ||
|
|
||
| private void logAfterSuite(MetadataTestRunResults results) { | ||
| if (results.failingTestTotal() == 0) { | ||
| notifyStreams(AnsiTerminalColorHelper.BRIGHT_GREEN + "All phrases successfully executed!" | ||
| + AnsiTerminalColorHelper.RESET); | ||
| } else { | ||
| notifyStreams(AnsiTerminalColorHelper.BRIGHT_RED + "There were test failures!" | ||
| + AnsiTerminalColorHelper.RESET); | ||
| } | ||
| } | ||
|
|
||
| private void logBeforeSuite() { | ||
| notifyStreams(AnsiTerminalColorHelper.BRIGHT_YELLOW + "Running tests..." + AnsiTerminalColorHelper.RESET); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.