AddProperty extends TransformationOperation<AddProperty> { @Override @edu.umd.cs.findbugs.annotations.SuppressFBWarnings (value="NP_ALWAYS_NULL_EXCEPTION") protected TOExecutionResult execution(File transformedAppFolder, TransformationContext transformationContext) { FileInputStream fileInputStream = null; TOExecutionResult result = null; File fileToBeChanged = getAbsoluteFile(transformedAppFolder, transformationContext); try { Properties properties = new Properties(); fileInputStream = new FileInputStream(fileToBeChanged); properties.load(fileInputStream); fileInputStream.close(); if(!properties.containsKey(propertyName)) { result = addProperty(transformedAppFolder, transformationContext); } else { result = setProperty(transformedAppFolder, transformationContext); } } catch (IOException e) { result = TOExecutionResult.error(this, new TransformationOperationException("Property file could not be modified", e)); } finally { if (fileInputStream != null) try { fileInputStream.close(); } catch (IOException e) { result.addWarning(e); } } return result; } AddProperty();  AddProperty(String propertyName, String propertyValue); AddProperty setPropertyName(String propertyName); AddProperty setPropertyValue(String propertyValue); String getPropertyName(); String getPropertyValue(); @Override String getDescription();  }
@Test public void successSetTest() throws IOException { AddProperty addProperty = new AddProperty("foo", "boo").relative("/src/main/resources/application.properties"); TOExecutionResult executionResult = addProperty.execution(transformedAppFolder, transformationContext); assertEquals(executionResult.getType(), TOExecutionResult.Type.SUCCESS); assertChangedFile("/src/main/resources/application.properties"); assertSameLineCount("/src/main/resources/application.properties"); Properties properties = getProperties("/src/main/resources/application.properties"); assertEquals(properties.size(), 3); assertEquals(properties.getProperty("bar"), "barv"); assertEquals(properties.getProperty("foo"), "boo"); assertEquals(properties.getProperty("foofoo"), "foofoov"); }
@Test public void fileDoesNotExistTest() { AddProperty addProperty = new AddProperty("foo", "boo").relative("/src/main/resources/application_zeta.properties"); TOExecutionResult executionResult = addProperty.execution(transformedAppFolder, transformationContext); assertEquals(executionResult.getType(), TOExecutionResult.Type.ERROR); assertEquals(executionResult.getException().getClass(), TransformationOperationException.class); assertEquals(executionResult.getException().getMessage(), "Property file could not be modified"); assertEquals(executionResult.getException().getCause().getClass(), FileNotFoundException.class); assertEquals(executionResult.getException().getCause().getMessage(), new File(transformedAppFolder, "/src/main/resources/application_zeta.properties").getAbsolutePath() + " (No such file or directory)"); }