|
| 1 | +/* |
| 2 | + * Copyright 2015-2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package extensions.annotation; |
| 17 | + |
| 18 | +import static java.lang.annotation.ElementType.METHOD; |
| 19 | +import static java.lang.annotation.ElementType.TYPE; |
| 20 | +import static java.lang.annotation.RetentionPolicy.RUNTIME; |
| 21 | + |
| 22 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 23 | +import static org.hamcrest.Matchers.allOf; |
| 24 | + |
| 25 | +import java.io.ByteArrayOutputStream; |
| 26 | +import java.io.IOException; |
| 27 | +import java.io.OutputStream; |
| 28 | +import java.io.PrintStream; |
| 29 | +import java.lang.annotation.Retention; |
| 30 | +import java.lang.annotation.Target; |
| 31 | +import java.util.ArrayList; |
| 32 | +import java.util.List; |
| 33 | + |
| 34 | +import org.hamcrest.Matcher; |
| 35 | +import org.junit.jupiter.api.extension.AfterAllCallback; |
| 36 | +import org.junit.jupiter.api.extension.AfterEachCallback; |
| 37 | +import org.junit.jupiter.api.extension.BeforeAllCallback; |
| 38 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 39 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 40 | +import org.junit.jupiter.api.extension.ExtensionContext.Namespace; |
| 41 | +import org.junit.jupiter.api.extension.ExtensionContext.Store; |
| 42 | +import org.junit.jupiter.api.extension.ParameterContext; |
| 43 | +import org.junit.jupiter.api.extension.ParameterResolver; |
| 44 | +import org.junit.platform.commons.support.ReflectionSupport; |
| 45 | + |
| 46 | +/** |
| 47 | + * {@code @CaptureSystemOutput} is a JUnit JUpiter extension for capturing output to {@code System.out} and |
| 48 | + * {@code System.err} with expectations supported via Hamcrest matchers. |
| 49 | + * |
| 50 | + * <h4>Example Usage</h4> |
| 51 | + * |
| 52 | + * <pre style="code"> |
| 53 | + * {@literal @}Test |
| 54 | + * {@literal @}CaptureSystemOutput |
| 55 | + * void systemOut(OutputCapture outputCapture) { |
| 56 | + * outputCapture.expect(containsString("System.out!")); |
| 57 | + * |
| 58 | + * System.out.println("Printed to System.out!"); |
| 59 | + * } |
| 60 | + * |
| 61 | + * {@literal @}Test |
| 62 | + * {@literal @}CaptureSystemOutput |
| 63 | + * void systemErr(OutputCapture outputCapture) { |
| 64 | + * outputCapture.expect(containsString("System.err!")); |
| 65 | + * |
| 66 | + * System.err.println("Printed to System.err!"); |
| 67 | + * } |
| 68 | + * </pre> |
| 69 | + * |
| 70 | + * <p> |
| 71 | + * Based on code from Spring Boot's <a href= |
| 72 | + * "https://github.com/spring-projects/spring-boot/blob/d3c34ee3d1bfd3db4a98678c524e145ef9bca51c/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/rule/OutputCapture.java">OutputCapture</a> |
| 73 | + * rule for JUnit 4 by Phillip Webb and Andy Wilkinson. |
| 74 | + * |
| 75 | + * <p> |
| 76 | + * Borrowing source from Sam Brannen as listed online at spring and stackoverflow from here <a href= |
| 77 | + * "https://github.com/sbrannen/junit5-demo/blob/master/src/test/java/extensions/CaptureSystemOutput.java">CaptureSystemOutput</a> |
| 78 | + * |
| 79 | + * <p> |
| 80 | + * Additional changes to Sam Brannen logic supplied by kazuki43zoo from here <a href= |
| 81 | + * "https://github.com/kazuki43zoo/mybatis-spring-boot/commit/317c9809326baba1f6ee0a0f8c2c471cd75993b3">enhancement |
| 82 | + * capture</a> |
| 83 | + * |
| 84 | + * @author Sam Brannen |
| 85 | + * @author Phillip Webb |
| 86 | + * @author Andy Wilkinson |
| 87 | + */ |
| 88 | +@Target({ TYPE, METHOD }) |
| 89 | +@Retention(RUNTIME) |
| 90 | +@ExtendWith(CaptureSystemOutput.Extension.class) |
| 91 | +public @interface CaptureSystemOutput { |
| 92 | + |
| 93 | + class Extension implements BeforeAllCallback, AfterAllCallback, AfterEachCallback, ParameterResolver { |
| 94 | + |
| 95 | + @Override |
| 96 | + public void beforeAll(ExtensionContext context) { |
| 97 | + getOutputCapture(context).captureOutput(); |
| 98 | + } |
| 99 | + |
| 100 | + public void afterAll(ExtensionContext context) { |
| 101 | + getOutputCapture(context).releaseOutput(); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void afterEach(ExtensionContext context) { |
| 106 | + OutputCapture outputCapture = getOutputCapture(context); |
| 107 | + try { |
| 108 | + if (!outputCapture.matchers.isEmpty()) { |
| 109 | + String output = outputCapture.toString(); |
| 110 | + assertThat(output, allOf(outputCapture.matchers)); |
| 111 | + } |
| 112 | + } finally { |
| 113 | + outputCapture.reset(); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { |
| 119 | + boolean isTestMethodLevel = extensionContext.getTestMethod().isPresent(); |
| 120 | + boolean isOutputCapture = parameterContext.getParameter().getType() == OutputCapture.class; |
| 121 | + return isTestMethodLevel && isOutputCapture; |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { |
| 126 | + return getOutputCapture(extensionContext); |
| 127 | + } |
| 128 | + |
| 129 | + private OutputCapture getOutputCapture(ExtensionContext context) { |
| 130 | + return getOrComputeIfAbsent(getStore(context), OutputCapture.class); |
| 131 | + } |
| 132 | + |
| 133 | + private <V> V getOrComputeIfAbsent(Store store, Class<V> type) { |
| 134 | + return store.getOrComputeIfAbsent(type, ReflectionSupport::newInstance, type); |
| 135 | + } |
| 136 | + |
| 137 | + private Store getStore(ExtensionContext context) { |
| 138 | + return context.getStore(Namespace.create(getClass())); |
| 139 | + } |
| 140 | + |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * {@code OutputCapture} captures output to {@code System.out} and {@code System.err}. |
| 145 | + * |
| 146 | + * <p> |
| 147 | + * To obtain an instance of {@code OutputCapture}, declare a parameter of type {@code OutputCapture} in a JUnit |
| 148 | + * Jupiter {@code @Test}, {@code @BeforeEach}, or {@code @AfterEach} method. |
| 149 | + * |
| 150 | + * <p> |
| 151 | + * {@linkplain #expect Expectations} are supported via Hamcrest matchers. |
| 152 | + * |
| 153 | + * <p> |
| 154 | + * To obtain all output to {@code System.out} and {@code System.err}, simply invoke {@link #toString()}. |
| 155 | + * |
| 156 | + * @author Phillip Webb |
| 157 | + * @author Andy Wilkinson |
| 158 | + * @author Sam Brannen |
| 159 | + */ |
| 160 | + static class OutputCapture { |
| 161 | + |
| 162 | + private final List<Matcher<? super String>> matchers = new ArrayList<>(); |
| 163 | + |
| 164 | + private CaptureOutputStream captureOut; |
| 165 | + |
| 166 | + private CaptureOutputStream captureErr; |
| 167 | + |
| 168 | + private ByteArrayOutputStream copy; |
| 169 | + |
| 170 | + void captureOutput() { |
| 171 | + this.copy = new ByteArrayOutputStream(); |
| 172 | + this.captureOut = new CaptureOutputStream(System.out, this.copy); |
| 173 | + this.captureErr = new CaptureOutputStream(System.err, this.copy); |
| 174 | + System.setOut(new PrintStream(this.captureOut)); |
| 175 | + System.setErr(new PrintStream(this.captureErr)); |
| 176 | + } |
| 177 | + |
| 178 | + void releaseOutput() { |
| 179 | + System.setOut(this.captureOut.getOriginal()); |
| 180 | + System.setErr(this.captureErr.getOriginal()); |
| 181 | + this.copy = null; |
| 182 | + } |
| 183 | + |
| 184 | + private void flush() { |
| 185 | + try { |
| 186 | + this.captureOut.flush(); |
| 187 | + this.captureErr.flush(); |
| 188 | + } catch (IOException ex) { |
| 189 | + // ignore |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * Verify that the captured output is matched by the supplied {@code matcher}. |
| 195 | + * |
| 196 | + * <p> |
| 197 | + * Verification is performed after the test method has executed. |
| 198 | + * |
| 199 | + * @param matcher |
| 200 | + * the matcher |
| 201 | + */ |
| 202 | + public void expect(Matcher<? super String> matcher) { |
| 203 | + this.matchers.add(matcher); |
| 204 | + } |
| 205 | + |
| 206 | + /** |
| 207 | + * Return all captured output to {@code System.out} and {@code System.err} as a single string. |
| 208 | + */ |
| 209 | + @Override |
| 210 | + public String toString() { |
| 211 | + flush(); |
| 212 | + return this.copy.toString(); |
| 213 | + } |
| 214 | + |
| 215 | + void reset() { |
| 216 | + this.matchers.clear(); |
| 217 | + this.copy.reset(); |
| 218 | + } |
| 219 | + |
| 220 | + private static class CaptureOutputStream extends OutputStream { |
| 221 | + |
| 222 | + private final PrintStream original; |
| 223 | + |
| 224 | + private final OutputStream copy; |
| 225 | + |
| 226 | + CaptureOutputStream(PrintStream original, OutputStream copy) { |
| 227 | + this.original = original; |
| 228 | + this.copy = copy; |
| 229 | + } |
| 230 | + |
| 231 | + PrintStream getOriginal() { |
| 232 | + return this.original; |
| 233 | + } |
| 234 | + |
| 235 | + @Override |
| 236 | + public void write(int b) throws IOException { |
| 237 | + this.copy.write(b); |
| 238 | + this.original.write(b); |
| 239 | + this.original.flush(); |
| 240 | + } |
| 241 | + |
| 242 | + @Override |
| 243 | + public void write(byte[] b) throws IOException { |
| 244 | + write(b, 0, b.length); |
| 245 | + } |
| 246 | + |
| 247 | + @Override |
| 248 | + public void write(byte[] b, int off, int len) throws IOException { |
| 249 | + this.copy.write(b, off, len); |
| 250 | + this.original.write(b, off, len); |
| 251 | + } |
| 252 | + |
| 253 | + @Override |
| 254 | + public void flush() throws IOException { |
| 255 | + this.copy.flush(); |
| 256 | + this.original.flush(); |
| 257 | + } |
| 258 | + |
| 259 | + } |
| 260 | + |
| 261 | + } |
| 262 | + |
| 263 | +} |
0 commit comments