SharedFileWriter extends AbstractFormatPatternWriter { @Override public void write(final LogEntry logEntry) throws IOException { byte[] data = render(logEntry).getBytes(charset); writer.write(data, data.length); } SharedFileWriter();  SharedFileWriter(final Map<String, String> properties); @Override void write(final LogEntry logEntry); @Override void flush(); @Override void close();  }
@Test public void appendingDisabledForMultipleProcesses() throws IOException, InterruptedException { File file = new File(FileSystem.createTemporaryFile()); String path = file.getAbsolutePath(); if (!file.delete()) { throw new IOException("Failed to delete temporary file: " + path); } List<Process> processes = new JvmProcessBuilder(SharedFileWriterTest.class, path, "false").start(NUMBER_OF_PROCESSES); Files.write(file.toPath(), Arrays.asList("PREAMBLE")); for (Process process : processes) { process.waitFor(); } assertThat(FileSystem.readFile(path)) .hasLineCount(NUMBER_OF_PROCESSES * NUMBER_OF_LINES) .matches("(" + Pattern.quote(LOG_ENTRY_MESSAGE + NEW_LINE) + "){" + (NUMBER_OF_PROCESSES * NUMBER_OF_LINES) + "}"); }
@Test public void appendingEnabledForMultipleProcesses() throws IOException, InterruptedException { File file = new File(FileSystem.createTemporaryFile()); String path = file.getAbsolutePath(); if (!file.delete()) { throw new IOException("Failed to delete temporary file: " + path); } List<Process> processes = new JvmProcessBuilder(SharedFileWriterTest.class, path, "true").start(NUMBER_OF_PROCESSES); Files.write(file.toPath(), Arrays.asList("PREAMBLE")); for (Process process : processes) { process.waitFor(); } int entries = NUMBER_OF_PROCESSES * NUMBER_OF_LINES; assertThat(FileSystem.readFile(path)) .hasLineCount(entries + 1) .matches(Pattern.quote("PREAMBLE" + NEW_LINE) + "(" + Pattern.quote(LOG_ENTRY_MESSAGE + NEW_LINE) + "){" + entries + "}"); }