Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
fix(CsvFileWriter): truncate existing files
Browse files Browse the repository at this point in the history
  • Loading branch information
menski committed Feb 25, 2019
1 parent 52e32eb commit 0beab39
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
18 changes: 16 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<name>Zeebe CSV Exporter</name>
Expand All @@ -13,7 +15,7 @@
<artifactId>camunda-release-parent</artifactId>
<version>2.5</version>
<!-- do not remove empty tag - http://jira.codehaus.org/browse/MNG-4687 -->
<relativePath />
<relativePath/>
</parent>

<licenses>
Expand Down Expand Up @@ -52,6 +54,7 @@
<plugin.version.fmt>2.6.0</plugin.version.fmt>
<plugin.version.license>3.0</plugin.version.license>
<plugin.version.shade>3.2.1</plugin.version.shade>
<plugin.version.surefire>3.0.0-M3</plugin.version.surefire>
</properties>

<dependencies>
Expand Down Expand Up @@ -154,6 +157,17 @@
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${plugin.version.surefire}</version>
<configuration>
<failIfNoTests>false</failIfNoTests>
<trimStackTrace>false</trimStackTrace>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/io/zeebe/exporter/writer/CsvFileWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
import io.zeebe.exporter.record.CsvRecord;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

public class CsvFileWriter implements CsvWriter {

Expand All @@ -47,8 +48,11 @@ public CsvFileWriter(Path output, String prefix, Class schemaClass) {
public void write(CsvRecord record) throws IOException {
if (sequenceWriter == null) {
Path path = filePath(output, prefix, record.getPartition());
File file = Files.createFile(path).toFile();
sequenceWriter = objectWriter.writeValues(new BufferedWriter(new FileWriter(file)));
OpenOption openOption =
Files.exists(path) ? StandardOpenOption.TRUNCATE_EXISTING : StandardOpenOption.CREATE_NEW;
BufferedWriter bufferedWriter =
Files.newBufferedWriter(path, StandardCharsets.UTF_8, openOption);
sequenceWriter = objectWriter.writeValues(bufferedWriter);
}

sequenceWriter.write(record);
Expand Down
43 changes: 43 additions & 0 deletions src/test/java/io/zeebe/exporter/CsvFileWriterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright © 2019 camunda services GmbH ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.zeebe.exporter;

import io.zeebe.exporter.record.JobCsvRecord;
import io.zeebe.exporter.writer.CsvFileWriter;
import java.io.IOException;
import java.nio.file.Path;
import java.util.function.Supplier;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

public class CsvFileWriterTest {

@Rule public TemporaryFolder temporaryFolder = new TemporaryFolder();

@Test
public void shouldOverwriteExistingFile() throws IOException {
// given
Path path = temporaryFolder.getRoot().toPath();
Supplier<CsvFileWriter> writerFactory =
() -> new CsvFileWriter(path, "test", JobCsvRecord.class);

writerFactory.get().write(new JobCsvRecord());

// when then is no exception thrown
writerFactory.get().write(new JobCsvRecord());
}
}

0 comments on commit 0beab39

Please sign in to comment.