Skip to content
This repository was archived by the owner on Sep 8, 2022. It is now read-only.

Commit 349efff

Browse files
authored
Merge pull request #8 from RolandDahlemPV/feature/unix_lineendings
Feature/unix lineendings
2 parents f2ed410 + 8baa7a3 commit 349efff

File tree

1 file changed

+71
-19
lines changed

1 file changed

+71
-19
lines changed

modules/core/src/main/java/io/wcm/qa/galenium/persistence/util/TextSampleManager.java

Lines changed: 71 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,27 @@
1919
*/
2020
package io.wcm.qa.galenium.persistence.util;
2121

22+
import java.io.BufferedReader;
23+
import java.io.BufferedWriter;
24+
import java.io.DataInputStream;
25+
import java.io.DataOutputStream;
2226
import java.io.File;
27+
import java.io.FileInputStream;
28+
import java.io.FileOutputStream;
2329
import java.io.FileWriter;
2430
import java.io.IOException;
31+
import java.io.InputStreamReader;
32+
import java.io.OutputStreamWriter;
2533
import java.nio.charset.Charset;
34+
import java.nio.file.Files;
2635
import java.util.Properties;
2736

2837
import org.apache.commons.collections4.properties.SortedProperties;
38+
import org.apache.commons.io.FileUtils;
39+
import org.apache.commons.io.IOUtils;
2940
import org.apache.commons.io.output.WriterOutputStream;
3041
import org.slf4j.Logger;
42+
import org.slf4j.LoggerFactory;
3143

3244
import io.wcm.qa.galenium.configuration.GaleniumConfiguration;
3345
import io.wcm.qa.galenium.configuration.PropertiesUtil;
@@ -39,7 +51,7 @@
3951
* {@link GaleniumConfiguration#getTextComparisonInputDirectory()}.
4052
*/
4153
public final class TextSampleManager {
42-
54+
4355
private static final Charset CHARSET_UTF8 = Charset.forName("utf-8");
4456
private static final SortedProperties EXPECTED_TEXTS = new SortedProperties();
4557
private static final String FILE_NAME_EXPECTED_TEXTS = GaleniumConfiguration.getTextComparisonFile();
@@ -101,30 +113,70 @@ public static void persistNewTextSamples() {
101113
getLogger().debug("no text samples to persist.");
102114
}
103115
else {
104-
WriterOutputStream writerOutputStream = null;
105-
try {
106-
getLogger().debug("Persisting " + SAMPLED_TEXTS.size() + " text samples.");
107-
writerOutputStream = getOutputStream();
116+
writeNewTextSamples();
117+
reencodeToUnixLineEndings();
118+
}
119+
}
108120

109-
EXPECTED_TEXTS.store(writerOutputStream, "Expected texts");
110-
SAMPLED_TEXTS.store(writerOutputStream, "Sampled texts");
111-
}
112-
catch (IOException ex) {
113-
getLogger().error("Could not save sample texts to '" + OUTPUT_FILE + "'");
114-
}
115-
finally {
116-
if (writerOutputStream != null) {
117-
try {
118-
writerOutputStream.close();
119-
}
120-
catch (IOException ex) {
121-
getLogger().warn("error when closing file output stream: '" + OUTPUT_FILE + "'");
122-
}
121+
private static void writeNewTextSamples() {
122+
WriterOutputStream writerOutputStream = null;
123+
try {
124+
getLogger().debug("Persisting " + SAMPLED_TEXTS.size() + " text samples.");
125+
writerOutputStream = getOutputStream();
126+
EXPECTED_TEXTS.store(writerOutputStream, "Expected texts");
127+
SAMPLED_TEXTS.store(writerOutputStream, "Sampled texts");
128+
}
129+
catch (IOException ex) {
130+
getLogger().error("Could not save sample texts to '" + OUTPUT_FILE + "'");
131+
}
132+
finally {
133+
if (writerOutputStream != null) {
134+
try {
135+
writerOutputStream.close();
136+
}
137+
catch (IOException ex) {
138+
getLogger().warn("error when closing file output stream: '" + OUTPUT_FILE + "'");
123139
}
124140
}
125141
}
126142
}
127143

144+
/** to reduce changes in the file we force the lineendings to be in unix format */
145+
private static void reencodeToUnixLineEndings() {
146+
147+
File temp = null;
148+
BufferedReader reader = null;
149+
BufferedWriter writer = null;
150+
151+
try {
152+
temp = new File(OUTPUT_FILE.getAbsolutePath() + ".unixLines");
153+
temp.createNewFile();
154+
155+
reader = new BufferedReader(new InputStreamReader(new DataInputStream(new FileInputStream(OUTPUT_FILE))));
156+
writer = new BufferedWriter(new OutputStreamWriter(new DataOutputStream(new FileOutputStream(temp))));
157+
158+
String line;
159+
while ((line = reader.readLine()) != null) {
160+
writer.write(line);
161+
writer.write("\n");
162+
}
163+
reader.close();
164+
writer.close();
165+
166+
Files.delete(OUTPUT_FILE.toPath());
167+
Files.move(temp.toPath(), OUTPUT_FILE.toPath());
168+
getLogger().debug("successfully reencoded to unix lineendings: " + OUTPUT_FILE);
169+
170+
} catch (IOException e) {
171+
getLogger().warn("could not reencode: '" + OUTPUT_FILE + "'", e);
172+
} finally {
173+
FileUtils.deleteQuietly(temp);
174+
IOUtils.closeQuietly(reader);
175+
IOUtils.closeQuietly(writer);
176+
}
177+
178+
}
179+
128180
private static Logger getLogger() {
129181
return GaleniumReportUtil.getLogger();
130182
}

0 commit comments

Comments
 (0)