@Test public void testTransformCrEols() throws Exception { File properties = File.createTempFile("temp", "properties"); Files.write("momo=1.0\rpopo=2.0\r", properties, Charset.forName("utf-8")); Map<String, String> versions = Maps.newHashMap(); versions.put("momo", "2.0"); versions.put("popo", "3.0"); boolean transformed = new PropertiesTransformer(properties, versions).transform(); assertTrue(transformed); String transformedProps = Files.toString(properties, Charset.forName("utf-8")); assertFalse(transformedProps.contains("\n"), "Unexpected EOL."); assertTrue(transformedProps.contains("\r"), "Expected a CR EOL."); assertTrue(transformedProps.endsWith("\r"), "Expected the properties to end with an EOL"); }
public Boolean transform() throws IOException, InterruptedException { if (!propertiesFile.exists()) { throw new IllegalArgumentException("Couldn't find properties file: " + propertiesFile.getAbsolutePath()); } Properties properties = new Properties(); EolDetectingInputStream eolDetectingInputStream = null; try { eolDetectingInputStream = new EolDetectingInputStream(new FileInputStream(propertiesFile)); properties.load(eolDetectingInputStream); } finally { IOUtils.closeQuietly(eolDetectingInputStream); } String eol = eolDetectingInputStream.getEol(); boolean hasEol = !"".equals(eol); StringBuilder resultBuilder = new StringBuilder(); boolean modified = false; Enumeration<?> propertyNames = properties.propertyNames(); while (propertyNames.hasMoreElements()) { String propertyName = (String) propertyNames.nextElement(); String propertyValue = properties.getProperty(propertyName); StringBuilder lineBuilder = new StringBuilder(propertyName).append("="); String newPropertyValue = versionsByName.get(propertyName); if ((newPropertyValue != null) && !newPropertyValue.equals(propertyValue)) { if (!modified) { modified = true; } lineBuilder.append(newPropertyValue); } else { lineBuilder.append(propertyValue); } resultBuilder.append(lineBuilder.toString()); if (hasEol) { resultBuilder.append(eol); } } if (modified) { propertiesFile.delete(); String toWrite = resultBuilder.toString(); Files.write(toWrite, propertiesFile, Charsets.UTF_8); } return modified; }
PropertiesTransformer { public Boolean transform() throws IOException, InterruptedException { if (!propertiesFile.exists()) { throw new IllegalArgumentException("Couldn't find properties file: " + propertiesFile.getAbsolutePath()); } Properties properties = new Properties(); EolDetectingInputStream eolDetectingInputStream = null; try { eolDetectingInputStream = new EolDetectingInputStream(new FileInputStream(propertiesFile)); properties.load(eolDetectingInputStream); } finally { IOUtils.closeQuietly(eolDetectingInputStream); } String eol = eolDetectingInputStream.getEol(); boolean hasEol = !"".equals(eol); StringBuilder resultBuilder = new StringBuilder(); boolean modified = false; Enumeration<?> propertyNames = properties.propertyNames(); while (propertyNames.hasMoreElements()) { String propertyName = (String) propertyNames.nextElement(); String propertyValue = properties.getProperty(propertyName); StringBuilder lineBuilder = new StringBuilder(propertyName).append("="); String newPropertyValue = versionsByName.get(propertyName); if ((newPropertyValue != null) && !newPropertyValue.equals(propertyValue)) { if (!modified) { modified = true; } lineBuilder.append(newPropertyValue); } else { lineBuilder.append(propertyValue); } resultBuilder.append(lineBuilder.toString()); if (hasEol) { resultBuilder.append(eol); } } if (modified) { propertiesFile.delete(); String toWrite = resultBuilder.toString(); Files.write(toWrite, propertiesFile, Charsets.UTF_8); } return modified; } }
PropertiesTransformer { public Boolean transform() throws IOException, InterruptedException { if (!propertiesFile.exists()) { throw new IllegalArgumentException("Couldn't find properties file: " + propertiesFile.getAbsolutePath()); } Properties properties = new Properties(); EolDetectingInputStream eolDetectingInputStream = null; try { eolDetectingInputStream = new EolDetectingInputStream(new FileInputStream(propertiesFile)); properties.load(eolDetectingInputStream); } finally { IOUtils.closeQuietly(eolDetectingInputStream); } String eol = eolDetectingInputStream.getEol(); boolean hasEol = !"".equals(eol); StringBuilder resultBuilder = new StringBuilder(); boolean modified = false; Enumeration<?> propertyNames = properties.propertyNames(); while (propertyNames.hasMoreElements()) { String propertyName = (String) propertyNames.nextElement(); String propertyValue = properties.getProperty(propertyName); StringBuilder lineBuilder = new StringBuilder(propertyName).append("="); String newPropertyValue = versionsByName.get(propertyName); if ((newPropertyValue != null) && !newPropertyValue.equals(propertyValue)) { if (!modified) { modified = true; } lineBuilder.append(newPropertyValue); } else { lineBuilder.append(propertyValue); } resultBuilder.append(lineBuilder.toString()); if (hasEol) { resultBuilder.append(eol); } } if (modified) { propertiesFile.delete(); String toWrite = resultBuilder.toString(); Files.write(toWrite, propertiesFile, Charsets.UTF_8); } return modified; } PropertiesTransformer(File propertiesFile, Map<String, String> versionsByName); }
PropertiesTransformer { public Boolean transform() throws IOException, InterruptedException { if (!propertiesFile.exists()) { throw new IllegalArgumentException("Couldn't find properties file: " + propertiesFile.getAbsolutePath()); } Properties properties = new Properties(); EolDetectingInputStream eolDetectingInputStream = null; try { eolDetectingInputStream = new EolDetectingInputStream(new FileInputStream(propertiesFile)); properties.load(eolDetectingInputStream); } finally { IOUtils.closeQuietly(eolDetectingInputStream); } String eol = eolDetectingInputStream.getEol(); boolean hasEol = !"".equals(eol); StringBuilder resultBuilder = new StringBuilder(); boolean modified = false; Enumeration<?> propertyNames = properties.propertyNames(); while (propertyNames.hasMoreElements()) { String propertyName = (String) propertyNames.nextElement(); String propertyValue = properties.getProperty(propertyName); StringBuilder lineBuilder = new StringBuilder(propertyName).append("="); String newPropertyValue = versionsByName.get(propertyName); if ((newPropertyValue != null) && !newPropertyValue.equals(propertyValue)) { if (!modified) { modified = true; } lineBuilder.append(newPropertyValue); } else { lineBuilder.append(propertyValue); } resultBuilder.append(lineBuilder.toString()); if (hasEol) { resultBuilder.append(eol); } } if (modified) { propertiesFile.delete(); String toWrite = resultBuilder.toString(); Files.write(toWrite, propertiesFile, Charsets.UTF_8); } return modified; } PropertiesTransformer(File propertiesFile, Map<String, String> versionsByName); Boolean transform(); }
PropertiesTransformer { public Boolean transform() throws IOException, InterruptedException { if (!propertiesFile.exists()) { throw new IllegalArgumentException("Couldn't find properties file: " + propertiesFile.getAbsolutePath()); } Properties properties = new Properties(); EolDetectingInputStream eolDetectingInputStream = null; try { eolDetectingInputStream = new EolDetectingInputStream(new FileInputStream(propertiesFile)); properties.load(eolDetectingInputStream); } finally { IOUtils.closeQuietly(eolDetectingInputStream); } String eol = eolDetectingInputStream.getEol(); boolean hasEol = !"".equals(eol); StringBuilder resultBuilder = new StringBuilder(); boolean modified = false; Enumeration<?> propertyNames = properties.propertyNames(); while (propertyNames.hasMoreElements()) { String propertyName = (String) propertyNames.nextElement(); String propertyValue = properties.getProperty(propertyName); StringBuilder lineBuilder = new StringBuilder(propertyName).append("="); String newPropertyValue = versionsByName.get(propertyName); if ((newPropertyValue != null) && !newPropertyValue.equals(propertyValue)) { if (!modified) { modified = true; } lineBuilder.append(newPropertyValue); } else { lineBuilder.append(propertyValue); } resultBuilder.append(lineBuilder.toString()); if (hasEol) { resultBuilder.append(eol); } } if (modified) { propertiesFile.delete(); String toWrite = resultBuilder.toString(); Files.write(toWrite, propertiesFile, Charsets.UTF_8); } return modified; } PropertiesTransformer(File propertiesFile, Map<String, String> versionsByName); Boolean transform();  }