| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 6ec1a04 commit bdde374
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -577,8 +577,8 @@ public void testSetASingularAttributeUsingAListIsIgnoredWithoutStrictParsing() { | |||
| 577 | 577 | String[] args = new String[] { | |
| 578 | 578 | "--diskSizeGb=100", | |
| 579 | 579 | "--diskSizeGb=200"}; | |
| 580 | - expectedLogs.expectWarn("Strict parsing is disabled, ignoring option"); | ||
| 581 | 580 | PipelineOptionsFactory.fromArgs(args).withoutStrictParsing().create(); | |
| 581 | + expectedLogs.verifyWarn("Strict parsing is disabled, ignoring option"); | ||
| 582 | 582 | } | |
| 583 | 583 | ||
| 584 | 584 | @Test | |
@@ -610,8 +610,8 @@ public void testUsingArgumentWithUnknownPropertyIsNotAllowed() { | |||
| 610 | 610 | @Test | |
| 611 | 611 | public void testUsingArgumentWithUnknownPropertyIsIgnoredWithoutStrictParsing() { | |
| 612 | 612 | String[] args = new String[] {"--unknownProperty=value"}; | |
| 613 | - expectedLogs.expectWarn("missing a property named 'unknownProperty'"); | ||
| 614 | 613 | PipelineOptionsFactory.fromArgs(args).withoutStrictParsing().create(); | |
| 614 | + expectedLogs.verifyWarn("missing a property named 'unknownProperty'"); | ||
| 615 | 615 | } | |
| 616 | 616 | ||
| 617 | 617 | @Test | |
@@ -625,8 +625,8 @@ public void testUsingArgumentStartingWithIllegalCharacterIsNotAllowed() { | |||
| 625 | 625 | @Test | |
| 626 | 626 | public void testUsingArgumentStartingWithIllegalCharacterIsIgnoredWithoutStrictParsing() { | |
| 627 | 627 | String[] args = new String[] {" --diskSizeGb=100"}; | |
| 628 | - expectedLogs.expectWarn("Strict parsing is disabled, ignoring option"); | ||
| 629 | 628 | PipelineOptionsFactory.fromArgs(args).withoutStrictParsing().create(); | |
| 629 | + expectedLogs.verifyWarn("Strict parsing is disabled, ignoring option"); | ||
| 630 | 630 | } | |
| 631 | 631 | ||
| 632 | 632 | @Test | |
@@ -652,8 +652,8 @@ public void testUsingArgumentWithInvalidNameIsNotAllowed() { | |||
| 652 | 652 | @Test | |
| 653 | 653 | public void testUsingArgumentWithInvalidNameIsIgnoredWithoutStrictParsing() { | |
| 654 | 654 | String[] args = new String[] {"--=100"}; | |
| 655 | - expectedLogs.expectWarn("Strict parsing is disabled, ignoring option"); | ||
| 656 | 655 | PipelineOptionsFactory.fromArgs(args).withoutStrictParsing().create(); | |
| 656 | + expectedLogs.verifyWarn("Strict parsing is disabled, ignoring option"); | ||
| 657 | 657 | } | |
| 658 | 658 | ||
| 659 | 659 | @Test | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -100,7 +100,6 @@ public synchronized boolean waitTillSet(long timeout) throws InterruptedExceptio | |||
| 100 | 100 | ||
| 101 | 101 | @Test | |
| 102 | 102 | public void testJobWaitComplete() throws IOException, InterruptedException { | |
| 103 | - expectedLogs.expectInfo("Job finished with status DONE"); | ||
| 104 | 103 | ||
| 105 | 104 | DataflowPipelineRunner mockDataflowPipelineRunner = mock(DataflowPipelineRunner.class); | |
| 106 | 105 | DataflowPipelineJob mockJob = mock(DataflowPipelineJob.class); | |
@@ -141,6 +140,7 @@ public void run() { | |||
| 141 | 140 | mockWait.signalJobComplete(); | |
| 142 | 141 | assertTrue("run() should return after job completion is mocked.", | |
| 143 | 142 | jobCompleted.waitTillSet(2000)); | |
| 143 | + expectedLogs.verifyInfo("Job finished with status DONE"); | ||
| 144 | 144 | } | |
| 145 | 145 | ||
| 146 | 146 | @Test | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -18,8 +18,6 @@ | |||
| 18 | 18 | ||
| 19 | 19 | import static org.junit.Assert.fail; | |
| 20 | 20 | ||
| 21 | - import com.google.common.collect.Lists; | ||
| 22 | - | ||
| 23 | 21 | import org.hamcrest.Description; | |
| 24 | 22 | import org.hamcrest.Matcher; | |
| 25 | 23 | import org.hamcrest.TypeSafeMatcher; | |
@@ -28,10 +26,12 @@ | |||
| 28 | 26 | ||
| 29 | 27 | import java.util.Collection; | |
| 30 | 28 | import java.util.concurrent.ConcurrentLinkedDeque; | |
| 29 | + import java.util.logging.Formatter; | ||
| 31 | 30 | import java.util.logging.Handler; | |
| 32 | 31 | import java.util.logging.Level; | |
| 33 | 32 | import java.util.logging.LogRecord; | |
| 34 | 33 | import java.util.logging.Logger; | |
| 34 | + import java.util.logging.SimpleFormatter; | ||
| 35 | 35 | ||
| 36 | 36 | import javax.annotation.concurrent.ThreadSafe; | |
| 37 | 37 | ||
@@ -53,121 +53,168 @@ public static ExpectedLogs none(Class<?> klass) { | |||
| 53 | 53 | } | |
| 54 | 54 | ||
| 55 | 55 | /** | |
| 56 | - * Expect a logging event at the trace level with the given message. | ||
| 56 | + * Verify a logging event at the trace level with the given message. | ||
| 57 | 57 | * | |
| 58 | 58 | * @param substring The message to match against. | |
| 59 | 59 | */ | |
| 60 | - public void expectTrace(String substring) { | ||
| 61 | - expect(Level.FINEST, substring); | ||
| 60 | + public void verifyTrace(String substring) { | ||
| 61 | + verify(Level.FINEST, substring); | ||
| 62 | 62 | } | |
| 63 | 63 | ||
| 64 | 64 | /** | |
| 65 | - * Expect a logging event at the trace level with the given message and throwable. | ||
| 65 | + * Verify a logging event at the trace level with the given message and throwable. | ||
| 66 | 66 | * | |
| 67 | 67 | * @param substring The message to match against. | |
| 68 | 68 | * @param t The throwable to match against. | |
| 69 | 69 | */ | |
| 70 | - public void expectTrace(String substring, Throwable t) { | ||
| 71 | - expect(Level.FINEST, substring, t); | ||
| 70 | + public void verifyTrace(String substring, Throwable t) { | ||
| 71 | + verify(Level.FINEST, substring, t); | ||
| 72 | 72 | } | |
| 73 | 73 | ||
| 74 | 74 | /** | |
| 75 | - * Expect a logging event at the debug level with the given message. | ||
| 75 | + * Verify a logging event at the debug level with the given message. | ||
| 76 | 76 | * | |
| 77 | 77 | * @param substring The message to match against. | |
| 78 | 78 | */ | |
| 79 | - public void expectDebug(String substring) { | ||
| 80 | - expect(Level.FINE, substring); | ||
| 79 | + public void verifyDebug(String substring) { | ||
| 80 | + verify(Level.FINE, substring); | ||
| 81 | 81 | } | |
| 82 | 82 | ||
| 83 | 83 | /** | |
| 84 | - * Expect a logging event at the debug level with the given message and throwable. | ||
| 84 | + * Verify a logging event at the debug level with the given message and throwable. | ||
| 85 | 85 | * | |
| 86 | 86 | * @param message The message to match against. | |
| 87 | 87 | * @param t The throwable to match against. | |
| 88 | 88 | */ | |
| 89 | - public void expectDebug(String message, Throwable t) { | ||
| 90 | - expect(Level.FINE, message, t); | ||
| 89 | + public void verifyDebug(String message, Throwable t) { | ||
| 90 | + verify(Level.FINE, message, t); | ||
| 91 | 91 | } | |
| 92 | 92 | ||
| 93 | 93 | /** | |
| 94 | - * Expect a logging event at the info level with the given message. | ||
| 94 | + * Verify a logging event at the info level with the given message. | ||
| 95 | + * | ||
| 95 | 96 | * @param substring The message to match against. | |
| 96 | 97 | */ | |
| 97 | - public void expectInfo(String substring) { | ||
| 98 | - expect(Level.INFO, substring); | ||
| 98 | + public void verifyInfo(String substring) { | ||
| 99 | + verify(Level.INFO, substring); | ||
| 99 | 100 | } | |
| 100 | 101 | ||
| 101 | 102 | /** | |
| 102 | - * Expect a logging event at the info level with the given message and throwable. | ||
| 103 | + * Verify a logging event at the info level with the given message and throwable. | ||
| 103 | 104 | * | |
| 104 | 105 | * @param message The message to match against. | |
| 105 | 106 | * @param t The throwable to match against. | |
| 106 | 107 | */ | |
| 107 | - public void expectInfo(String message, Throwable t) { | ||
| 108 | - expect(Level.INFO, message, t); | ||
| 108 | + public void verifyInfo(String message, Throwable t) { | ||
| 109 | + verify(Level.INFO, message, t); | ||
| 110 | + } | ||
| 111 | + | ||
| 112 | + /** | ||
| 113 | + * Verify a logging event at the warn level with the given message. | ||
| 114 | + * | ||
| 115 | + * @param substring The message to match against. | ||
| 116 | + */ | ||
| 117 | + public void verifyWarn(String substring) { | ||
| 118 | + verify(Level.WARNING, substring); | ||
| 119 | + } | ||
| 120 | + | ||
| 121 | + /** | ||
| 122 | + * Verify a logging event at the warn level with the given message and throwable. | ||
| 123 | + * | ||
| 124 | + * @param substring The message to match against. | ||
| 125 | + * @param t The throwable to match against. | ||
| 126 | + */ | ||
| 127 | + public void verifyWarn(String substring, Throwable t) { | ||
| 128 | + verify(Level.WARNING, substring, t); | ||
| 109 | 129 | } | |
| 110 | 130 | ||
| 111 | 131 | /** | |
| 112 | - * Expect a logging event at the warn level with the given message. | ||
| 132 | + * Verify a logging event at the error level with the given message. | ||
| 113 | 133 | * | |
| 114 | 134 | * @param substring The message to match against. | |
| 115 | 135 | */ | |
| 116 | - public void expectWarn(String substring) { | ||
| 117 | - expect(Level.WARNING, substring); | ||
| 136 | + public void verifyError(String substring) { | ||
| 137 | + verify(Level.SEVERE, substring); | ||
| 118 | 138 | } | |
| 119 | 139 | ||
| 120 | 140 | /** | |
| 121 | - * Expect a logging event at the warn level with the given message and throwable. | ||
| 141 | + * Verify a logging event at the error level with the given message and throwable. | ||
| 122 | 142 | * | |
| 123 | 143 | * @param substring The message to match against. | |
| 124 | 144 | * @param t The throwable to match against. | |
| 125 | 145 | */ | |
| 126 | - public void expectWarn(String substring, Throwable t) { | ||
| 127 | - expect(Level.WARNING, substring, t); | ||
| 146 | + public void verifyError(String substring, Throwable t) { | ||
| 147 | + verify(Level.SEVERE, substring, t); | ||
| 128 | 148 | } | |
| 129 | 149 | ||
| 130 | 150 | /** | |
| 131 | - * Expect a logging event at the error level with the given message. | ||
| 151 | + * Verify there are no logging events with messages containing the given substring. | ||
| 132 | 152 | * | |
| 133 | 153 | * @param substring The message to match against. | |
| 134 | 154 | */ | |
| 135 | - public void expectError(String substring) { | ||
| 136 | - expect(Level.SEVERE, substring); | ||
| 155 | + public void verifyNotLogged(String substring) { | ||
| 156 | + verifyNotLogged(matcher(substring)); | ||
| 137 | 157 | } | |
| 138 | 158 | ||
| 139 | 159 | /** | |
| 140 | - * Expect a logging event at the error level with the given message and throwable. | ||
| 160 | + * Verify there is no logging event at the error level with the given message and throwable. | ||
| 141 | 161 | * | |
| 142 | 162 | * @param substring The message to match against. | |
| 143 | 163 | * @param t The throwable to match against. | |
| 144 | 164 | */ | |
| 145 | - public void expectError(String substring, Throwable t) { | ||
| 146 | - expect(Level.SEVERE, substring, t); | ||
| 165 | + public void verifyNoError(String substring, Throwable t) { | ||
| 166 | + verifyNo(Level.SEVERE, substring, t); | ||
| 167 | + } | ||
| 168 | + | ||
| 169 | + private void verify(final Level level, final String substring) { | ||
| 170 | + verifyLogged(matcher(level, substring)); | ||
| 147 | 171 | } | |
| 148 | 172 | ||
| 149 | - private void expect(final Level level, final String substring) { | ||
| 150 | - expectations.add(new TypeSafeMatcher<LogRecord>() { | ||
| 173 | + private TypeSafeMatcher<LogRecord> matcher(final String substring) { | ||
| 174 | + return new TypeSafeMatcher<LogRecord>() { | ||
| 175 | + @Override | ||
| 176 | + public void describeTo(Description description) { | ||
| 177 | + description.appendText(String.format("log message containing message [%s]", substring)); | ||
| 178 | + } | ||
| 179 | + | ||
| 180 | + @Override | ||
| 181 | + protected boolean matchesSafely(LogRecord item) { | ||
| 182 | + return item.getMessage().contains(substring); | ||
| 183 | + } | ||
| 184 | + }; | ||
| 185 | + } | ||
| 186 | + | ||
| 187 | + private TypeSafeMatcher<LogRecord> matcher(final Level level, final String substring) { | ||
| 188 | + return new TypeSafeMatcher<LogRecord>() { | ||
| 151 | 189 | @Override | |
| 152 | 190 | public void describeTo(Description description) { | |
| 153 | 191 | description.appendText(String.format( | |
| 154 | - "Expected log message of level [%s] containing message [%s]", level, substring)); | ||
| 192 | + "log message of level [%s] containing message [%s]", level, substring)); | ||
| 155 | 193 | } | |
| 156 | 194 | ||
| 157 | 195 | @Override | |
| 158 | 196 | protected boolean matchesSafely(LogRecord item) { | |
| 159 | 197 | return level.equals(item.getLevel()) | |
| 160 | 198 | && item.getMessage().contains(substring); | |
| 161 | 199 | } | |
| 162 | - }); | ||
| 200 | + }; | ||
| 201 | + } | ||
| 202 | + | ||
| 203 | + private void verify(final Level level, final String substring, final Throwable throwable) { | ||
| 204 | + verifyLogged(matcher(level, substring, throwable)); | ||
| 163 | 205 | } | |
| 164 | 206 | ||
| 165 | - private void expect(final Level level, final String substring, final Throwable throwable) { | ||
| 166 | - expectations.add(new TypeSafeMatcher<LogRecord>() { | ||
| 207 | + private void verifyNo(final Level level, final String substring, final Throwable throwable) { | ||
| 208 | + verifyNotLogged(matcher(level, substring, throwable)); | ||
| 209 | + } | ||
| 210 | + | ||
| 211 | + private TypeSafeMatcher<LogRecord> matcher( | ||
| 212 | + final Level level, final String substring, final Throwable throwable) { | ||
| 213 | + return new TypeSafeMatcher<LogRecord>() { | ||
| 167 | 214 | @Override | |
| 168 | 215 | public void describeTo(Description description) { | |
| 169 | 216 | description.appendText(String.format( | |
| 170 | - "Expected log message of level [%s] containg message [%s] with exception [%s] " | ||
| 217 | + "log message of level [%s] containg message [%s] with exception [%s] " | ||
| 171 | 218 | + "containing message [%s]", | |
| 172 | 219 | level, substring, throwable.getClass(), throwable.getMessage())); | |
| 173 | 220 | } | |
@@ -179,7 +226,26 @@ protected boolean matchesSafely(LogRecord item) { | |||
| 179 | 226 | && item.getThrown().getClass().equals(throwable.getClass()) | |
| 180 | 227 | && item.getThrown().getMessage().contains(throwable.getMessage()); | |
| 181 | 228 | } | |
| 182 | - }); | ||
| 229 | + }; | ||
| 230 | + } | ||
| 231 | + | ||
| 232 | + private void verifyLogged(Matcher<LogRecord> matcher) { | ||
| 233 | + for (LogRecord record : logSaver.getLogs()) { | ||
| 234 | + if (matcher.matches(record)) { | ||
| 235 | + return; | ||
| 236 | + } | ||
| 237 | + } | ||
| 238 | + | ||
| 239 | + fail(String.format("Missing match for [%s]", matcher)); | ||
| 240 | + } | ||
| 241 | + | ||
| 242 | + private void verifyNotLogged(Matcher<LogRecord> matcher) { | ||
| 243 | + // Don't use Matchers.everyItem(Matchers.not(matcher)) because it doesn't format the logRecord | ||
| 244 | + for (LogRecord record : logSaver.getLogs()) { | ||
| 245 | + if (matcher.matches(record)) { | ||
| 246 | + fail(String.format("Unexpected match of [%s]: [%s]", matcher, logFormatter.format(record))); | ||
| 247 | + } | ||
| 248 | + } | ||
| 183 | 249 | } | |
| 184 | 250 | ||
| 185 | 251 | @Override | |
@@ -193,30 +259,16 @@ protected void before() throws Throwable { | |||
| 193 | 259 | protected void after() { | |
| 194 | 260 | log.removeHandler(logSaver); | |
| 195 | 261 | log.setLevel(previousLevel); | |
| 196 | - Collection<Matcher<LogRecord>> missingExpecations = Lists.newArrayList(); | ||
| 197 | - FOUND: for (Matcher<LogRecord> expectation : expectations) { | ||
| 198 | - for (LogRecord log : logSaver.getLogs()) { | ||
| 199 | - if (expectation.matches(log)) { | ||
| 200 | - continue FOUND; | ||
| 201 | - } | ||
| 202 | - } | ||
| 203 | - missingExpecations.add(expectation); | ||
| 204 | - } | ||
| 205 | - | ||
| 206 | - if (!missingExpecations.isEmpty()) { | ||
| 207 | - fail(String.format("Missed logging expectations: %s", missingExpecations)); | ||
| 208 | - } | ||
| 209 | 262 | } | |
| 210 | 263 | ||
| 211 | 264 | private final Logger log; | |
| 212 | 265 | private final LogSaver logSaver; | |
| 213 | - private final Collection<Matcher<LogRecord>> expectations; | ||
| 266 | + private final Formatter logFormatter = new SimpleFormatter(); | ||
| 214 | 267 | private Level previousLevel; | |
| 215 | 268 | ||
| 216 | 269 | private ExpectedLogs(Class<?> klass) { | |
| 217 | 270 | log = Logger.getLogger(klass.getName()); | |
| 218 | 271 | logSaver = new LogSaver(); | |
| 219 | - expectations = Lists.newArrayList(); | ||
| 220 | 272 | } | |
| 221 | 273 | ||
| 222 | 274 | /** | |
| Back | FazBrowse Home | New Git URL |
0 commit comments