Files
spring-boot-demo/log4j2-levels/src/test/java/com/ankurm/log4j2levels/Transcript.java
T

43 lines
1.2 KiB
Java

package com.ankurm.log4j2levels;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
/**
* Collects the lines a test prints and writes them to {@code output/NN-name.txt}. The same test
* asserts the same numbers, so a line in a transcript is something the build checks.
*/
public final class Transcript implements AutoCloseable {
private final Path file;
private final List<String> lines = new ArrayList<>();
private Transcript(Path file) {
this.file = file;
}
public static Transcript open(String name) {
return new Transcript(Path.of("output", name));
}
public synchronized Transcript line(String format, Object... args) {
String text = args.length == 0 ? format : String.format(format, args);
lines.add(text);
System.out.println(text);
return this;
}
@Override
public synchronized void close() {
try {
Files.createDirectories(file.getParent());
Files.write(file, lines);
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
}