Skip to content
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

[#130] Removed additional empty line for RollingFileOutputWriter #131

Merged
merged 1 commit into from
Apr 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public synchronized void postCollect() throws IOException {
getTemporaryFileWriter().close();
if (logger.isLoggable(getDebugLevel()))
logger.log(getDebugLevel(), "Overwrite " + file.getAbsolutePath() + " by " + temporaryFile.getAbsolutePath());
IoUtils.appendToFile(temporaryFile, file, maxFileSize, maxBackupIndex);
IoUtils.appendToFile(temporaryFile, file, maxFileSize, maxBackupIndex, !singleLine);
} finally {
temporaryFileWriter = null;
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/org/jmxtrans/agent/util/io/IoUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ public static void replaceFile(File source, File destination) throws IOException
* @param destination
* @throws java.io.IOException
*/
private static void doCopySmallFile(File source, File destination, boolean append) throws IOException {
private static void doCopySmallFile(File source, File destination, boolean append, boolean prependNewLine) throws IOException {
if (destination.exists() && destination.isDirectory()) {
throw new IOException("Can not copy file, destination is a directory: " + destination.getAbsolutePath());
} else if (!destination.exists()) {
Expand All @@ -293,7 +293,7 @@ private static void doCopySmallFile(File source, File destination, boolean appen
long initialSize = destination.length();
try {
fos = new FileOutputStream(destination, append);
if (append) {
if (prependNewLine) {
fos.write(("\n").getBytes(StandardCharsets.UTF_8));
}
fos.write(Files.readAllBytes(Paths.get(source.getAbsolutePath())));
Expand All @@ -314,14 +314,14 @@ else if (append && destination.length() <= initialSize ) {

}

public static void appendToFile(File source, File destination, long maxFileSize, int maxBackupIndex) throws IOException {
public static void appendToFile(File source, File destination, long maxFileSize, int maxBackupIndex, boolean prependNewLine) throws IOException {
boolean destinationExists = validateDestinationFile(source, destination, maxFileSize, maxBackupIndex);
if (destinationExists) {
doCopySmallFile(source, destination, true);
doCopySmallFile(source, destination, true, prependNewLine);
} else {
boolean renamed = source.renameTo(destination);
if (!renamed) {
doCopySmallFile(source, destination, false);
doCopySmallFile(source, destination, false, false);
}
}
}
Expand Down Expand Up @@ -349,7 +349,7 @@ public static void rollFiles(File destination, int maxBackupIndex) throws IOExce
if (!f.exists()) continue;

File fNext = new File(destination + "." + (i + 1));
doCopySmallFile(f, fNext, false);
doCopySmallFile(f, fNext, false, false);
}

boolean deleted = destination.delete();
Expand Down
36 changes: 24 additions & 12 deletions src/test/java/org/jmxtrans/agent/RollingFileOutputWriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
import org.junit.Test;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.*;
import java.util.List;

import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.collection.IsArrayContainingInOrder.arrayContaining;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.isEmptyString;
import static org.junit.Assert.assertThat;

public class RollingFileOutputWriterTest {
Expand All @@ -19,11 +23,15 @@ public void testWriteQueryResultMulti() throws IOException {
Files.deleteIfExists(defaultLog);
Resource resource = new ClasspathResource("classpath:jmxtrans-config-rolling-multi-test.xml");
JmxTransConfigurationLoader loader = new JmxTransConfigurationXmlLoader(resource);
JmxTransExporter exporter = new JmxTransExporter(loader);
exporter.collectAndExport();
String text = new String(Files.readAllBytes(defaultLog));
String[] splitted = text.split("\n");
assertThat(splitted, arrayContaining(containsString("jvm.thread"), containsString("os.systemLoadAverage")));
new JmxTransExporter(loader).collectAndExport();
new JmxTransExporter(loader).collectAndExport();
List<String> lines = Files.readAllLines(defaultLog, Charset.defaultCharset());
assertThat(lines, hasSize(5));
assertThat(lines, contains(
containsString("jvm.thread"), containsString("os.systemLoadAverage"),
isEmptyString(),
containsString("jvm.thread"), containsString("os.systemLoadAverage")
));
Files.deleteIfExists(defaultLog);
}

Expand All @@ -33,10 +41,14 @@ public void testWriteQueryResultSingle() throws IOException {
Files.deleteIfExists(defaultLog);
Resource resource = new ClasspathResource("classpath:jmxtrans-config-rolling-single-test.xml");
JmxTransConfigurationLoader loader = new JmxTransConfigurationXmlLoader(resource);
JmxTransExporter exporter = new JmxTransExporter(loader);
exporter.collectAndExport();
String text = new String(Files.readAllBytes(defaultLog));
assertThat(text, allOf(containsString("jvm.thread="), containsString("os.systemLoadAverage=")));
new JmxTransExporter(loader).collectAndExport();
new JmxTransExporter(loader).collectAndExport();
List<String> lines = Files.readAllLines(defaultLog, Charset.defaultCharset());
assertThat(lines, hasSize(2));
assertThat(lines, contains(
allOf(containsString("jvm.thread="), containsString("os.systemLoadAverage=")),
allOf(containsString("jvm.thread="), containsString("os.systemLoadAverage="))
));
Files.deleteIfExists(defaultLog);
}
}