InsertText extends TransformationOperation<InsertText> { @edu.umd.cs.findbugs.annotations.SuppressFBWarnings (value="NP_ALWAYS_NULL_EXCEPTION") @Override protected TOExecutionResult execution(File transformedAppFolder, TransformationContext transformationContext) { File fileToBeChanged = getAbsoluteFile(transformedAppFolder, transformationContext); if (!fileToBeChanged.exists()) { FileNotFoundException ex = new FileNotFoundException("File to be modified has not been found"); return TOExecutionResult.error(this, ex); } BufferedReader reader = null; BufferedReader readerText = null; BufferedWriter writer = null; TOExecutionResult result = null; try { final String eol = EolHelper.findEolDefaultToOs(fileToBeChanged); File readFile = getOrCreateReadFile(transformedAppFolder, transformationContext); reader = new BufferedReader(new InputStreamReader(new FileInputStream(readFile), StandardCharsets.UTF_8)); readerText = new BufferedReader(new InputStreamReader(textFileUrl.openStream(), StandardCharsets.UTF_8)); writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileToBeChanged), StandardCharsets.UTF_8)); switch (insertionMode) { case LINE_NUMBER: result = insertAtSpecificLine(readerText, reader, writer, eol); break; case REGEX_FIRST: result = insertAfterRegex(readerText, reader, writer, true, eol); break; case REGEX_ALL: result = insertAfterRegex(readerText, reader, writer, false, eol); break; default: case CONCAT: result = concat(readerText, reader, writer, eol); break; } } catch (IOException e) { result = TOExecutionResult.error(this, e); } finally { try { if (writer != null) try { writer.close(); } catch (IOException e) { result.addWarning(e); } } finally { if(reader != null) try { reader.close(); } catch (IOException e) { result.addWarning(e); } finally { if(readerText != null) try { readerText.close(); } catch (IOException e) { result.addWarning(e); } } } } return result; } InsertText();  InsertText(URL textFileUrl);  InsertText(URL textFileUrl, Integer lineNumber);  InsertText(URL textFileUrl, String regex); InsertText setInsertionMode(InsertionMode insertionMode); InsertText setTextFileUrl(URL textFileUrl); InsertText setLineNumber(Integer lineNumber); InsertText setRegex(String regex); @Override String getDescription();  }
@Test public void insertConcatTest() throws IOException { InsertText insertText = new InsertText(billyURL).relative("src/main/resources/dogs.yaml"); TOExecutionResult executionResult = insertText.execution(transformedAppFolder, transformationContext); Assert.assertEquals(executionResult.getType(), TOExecutionResult.Type.SUCCESS); assertChangedFile("src/main/resources/dogs.yaml"); assertLineCount("src/main/resources/dogs.yaml", 3); Map<String, Dog> dogs = (Map) getObjectFromYaml("src/main/resources/dogs.yaml"); Assert.assertEquals(dogs.size(), 3); Dog dog = dogs.get("Billy"); Assert.assertEquals(dog.getName(), "Billy"); Assert.assertEquals(dog.getBreed(), "lab"); }
@Test public void fileDoesNotExistTest() { InsertText insertText = new InsertText(billyURL).relative("caes.yaml"); TOExecutionResult executionResult = insertText.execution(transformedAppFolder, transformationContext); Assert.assertEquals(executionResult.getType(), TOExecutionResult.Type.ERROR); Assert.assertEquals(executionResult.getException().getClass(), FileNotFoundException.class); }