|
19 | 19 | */
|
20 | 20 | package io.wcm.qa.galenium.persistence.util;
|
21 | 21 |
|
| 22 | +import java.io.BufferedReader; |
| 23 | +import java.io.BufferedWriter; |
| 24 | +import java.io.DataInputStream; |
| 25 | +import java.io.DataOutputStream; |
22 | 26 | import java.io.File;
|
| 27 | +import java.io.FileInputStream; |
| 28 | +import java.io.FileOutputStream; |
23 | 29 | import java.io.FileWriter;
|
24 | 30 | import java.io.IOException;
|
| 31 | +import java.io.InputStreamReader; |
| 32 | +import java.io.OutputStreamWriter; |
25 | 33 | import java.nio.charset.Charset;
|
| 34 | +import java.nio.file.Files; |
26 | 35 | import java.util.Properties;
|
27 | 36 |
|
28 | 37 | import org.apache.commons.collections4.properties.SortedProperties;
|
| 38 | +import org.apache.commons.io.FileUtils; |
| 39 | +import org.apache.commons.io.IOUtils; |
29 | 40 | import org.apache.commons.io.output.WriterOutputStream;
|
30 | 41 | import org.slf4j.Logger;
|
| 42 | +import org.slf4j.LoggerFactory; |
31 | 43 |
|
32 | 44 | import io.wcm.qa.galenium.configuration.GaleniumConfiguration;
|
33 | 45 | import io.wcm.qa.galenium.configuration.PropertiesUtil;
|
|
39 | 51 | * {@link GaleniumConfiguration#getTextComparisonInputDirectory()}.
|
40 | 52 | */
|
41 | 53 | public final class TextSampleManager {
|
42 |
| - |
| 54 | + |
43 | 55 | private static final Charset CHARSET_UTF8 = Charset.forName("utf-8");
|
44 | 56 | private static final SortedProperties EXPECTED_TEXTS = new SortedProperties();
|
45 | 57 | private static final String FILE_NAME_EXPECTED_TEXTS = GaleniumConfiguration.getTextComparisonFile();
|
@@ -101,30 +113,70 @@ public static void persistNewTextSamples() {
|
101 | 113 | getLogger().debug("no text samples to persist.");
|
102 | 114 | }
|
103 | 115 | 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 | + } |
108 | 120 |
|
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 + "'"); |
123 | 139 | }
|
124 | 140 | }
|
125 | 141 | }
|
126 | 142 | }
|
127 | 143 |
|
| 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 | + |
128 | 180 | private static Logger getLogger() {
|
129 | 181 | return GaleniumReportUtil.getLogger();
|
130 | 182 | }
|
|
0 commit comments