1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package io.wcm.qa.glnm.persistence;
21
22 import java.io.File;
23 import java.io.FileWriter;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27 import java.io.Reader;
28 import java.io.Writer;
29
30 import org.apache.commons.configuration2.PropertiesConfiguration;
31 import org.apache.commons.configuration2.builder.fluent.Configurations;
32 import org.apache.commons.configuration2.ex.ConfigurationException;
33 import org.apache.commons.io.FileUtils;
34 import org.apache.commons.lang3.ClassPathUtils;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 import io.wcm.qa.glnm.configuration.GaleniumConfiguration;
39
40 final class PersistenceUtil {
41
42 private static final Logger LOG = LoggerFactory.getLogger(PersistenceUtil.class);
43
44 private static final String POSTFIX_PROPERTIES = "properties";
45
46 private PersistenceUtil() {
47
48 }
49
50 private static File getOutputFile(Class clazz) {
51 String resourceName = getPropertiesResourceName(clazz);
52 File outputFile = getOutputFile(clazz, resourceName);
53 return outputFile;
54 }
55
56 private static File getOutputFile(Class clazz, String resourceName) {
57 String relativePath = ClassPathUtils.toFullyQualifiedPath(clazz, resourceName);
58 String textOutputDirectory = GaleniumConfiguration.getTextComparisonOutputDirectory();
59 File outputFile = FileUtils.getFile(textOutputDirectory, relativePath);
60 return outputFile;
61 }
62
63 private static String getPropertiesResourceName(Class clazz) {
64 String resourceName = clazz.getSimpleName() + "." + POSTFIX_PROPERTIES;
65 if (LOG.isDebugEnabled()) {
66 LOG.debug("properties resource name: " + resourceName);
67 }
68 return resourceName;
69 }
70
71 private static Reader getReaderForProperties(Class clazz) {
72 InputStream in = clazz.getResourceAsStream(getPropertiesResourceName(clazz));
73 if (in == null) {
74 return null;
75 }
76 return new InputStreamReader(in);
77 }
78
79 @SuppressWarnings("PMD.AvoidCatchingNPE")
80 static PropertiesConfiguration getPropertiesFor(Class clazz) {
81
82 try {
83 PropertiesConfiguration configuration = new Configurations().propertiesBuilder().getConfiguration();
84 Reader readerForProperties = getReaderForProperties(clazz);
85 if (readerForProperties != null) {
86 configuration.read(readerForProperties);
87 }
88 return configuration;
89 }
90 catch (ConfigurationException | IOException ex) {
91
92 if (LOG.isInfoEnabled()) {
93 LOG.info("Could not load properties for: " + clazz, ex);
94 }
95 return new PropertiesConfiguration();
96 }
97 }
98
99 static void writeSamplesForClass(PropertiesConfiguration samples, Class clazz) {
100 Writer writer = null;
101 try {
102 File outputFile = getOutputFile(clazz);
103 if (LOG.isInfoEnabled()) {
104 LOG.info("writing {} properties to '{}'", samples.size(), outputFile.getPath());
105 }
106 FileUtils.forceMkdirParent(outputFile);
107 writer = new FileWriter(outputFile, false);
108
109 samples.write(writer);
110 }
111 catch (IOException | ConfigurationException ex) {
112 LOG.error("could not persist samples.", ex);
113 }
114 finally {
115 if (writer != null) {
116 try {
117 writer.close();
118 }
119 catch (IOException ex) {
120 LOG.error("when closing writer while persisting samples.", ex);
121 }
122 }
123 }
124 }
125
126 }