InsertLine extends TransformationOperation<InsertLine> { @Override @edu.umd.cs.findbugs.annotations.SuppressFBWarnings (value="NP_ALWAYS_NULL_EXCEPTION") 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; 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)); writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileToBeChanged), StandardCharsets.UTF_8)); switch (insertionMode) { case LINE_NUMBER: result = insertAtSpecificLine(reader, writer, eol); break; case REGEX_FIRST: result = insertRegex(reader, writer, true, true, eol); break; case REGEX_ALL: result = insertRegex(reader, writer, false, true, eol); break; case REGEX_BEFORE_FIRST: result = insertRegex(reader, writer, true, false, eol); break; case REGEX_BEFORE_ALL: result = insertRegex(reader, writer, false, false, eol); break; default: case CONCAT: result = concat(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); } } } return result; } InsertLine();  InsertLine(String newLine);  InsertLine(String newLine, Integer lineNumber);  InsertLine(String newLine, String regex); InsertLine setInsertionMode(InsertionMode insertionMode); InsertLine setNewLine(String newLine); InsertLine setLineNumber(Integer lineNumber); InsertLine setRegex(String regex); @Override String getDescription();  }
@Test public void noOpLineNumberTest() throws IOException { InsertLine insertLine = new InsertLine(" color: yellow", 1427).relative("/src/main/resources/dogs.yaml"); TOExecutionResult executionResult = insertLine.execution(transformedAppFolder, transformationContext); Assert.assertEquals(executionResult.getType(), TOExecutionResult.Type.NO_OP); assertNotChangedFile("/src/main/resources/dogs.yaml"); }
@Test public void insertLineNumberTest() throws IOException { InsertLine insertLine = new InsertLine(" color: black and white", 6).relative("/src/main/resources/dogs.yaml"); TOExecutionResult executionResult = insertLine.execution(transformedAppFolder, transformationContext); Assert.assertEquals(executionResult.getType(), TOExecutionResult.Type.SUCCESS); assertChangedFile("/src/main/resources/dogs.yaml"); assertLineCount("/src/main/resources/dogs.yaml", 1); Map<String, Dog> dogs = (Map) getObjectFromYaml("/src/main/resources/dogs.yaml"); Dog mustache = dogs.get("Mustache"); Assert.assertEquals(mustache.getColor(), "black and white"); }
@Test public void insertFirstRegexTest() throws IOException, URISyntaxException { InsertLine insertLine = new InsertLine(" color: black and white", " name: Mustache").relative("/src/main/resources/dogs.yaml"); TOExecutionResult executionResult = insertLine.execution(transformedAppFolder, transformationContext); Assert.assertEquals(executionResult.getType(), TOExecutionResult.Type.SUCCESS); assertChangedFile("/src/main/resources/dogs.yaml"); assertLineCount("/src/main/resources/dogs.yaml", 1); Map<String, Dog> dogs = (Map) getObjectFromYaml("/src/main/resources/dogs.yaml"); Dog mustache = dogs.get("Mustache"); Assert.assertEquals(mustache.getColor(), "black and white"); }
@Test public void insertRegexAtEndTest() throws IOException { InsertLine insertLine = new InsertLine(" fixed: false", "(.*breed: pit bull.*)").relative("/src/main/resources/dogs.yaml"); TOExecutionResult executionResult = insertLine.execution(transformedAppFolder, transformationContext); Assert.assertEquals(executionResult.getType(), TOExecutionResult.Type.SUCCESS); assertChangedFile("/src/main/resources/dogs.yaml"); assertLineCount("/src/main/resources/dogs.yaml", 1); Map<String, Dog> dogs = (Map) getObjectFromYaml("/src/main/resources/dogs.yaml"); Assert.assertEquals(dogs.size(), 2); Dog dog = dogs.get("Mustache"); Assert.assertEquals(dog.getName(), "Mustache"); Assert.assertEquals(dog.getBreed(), "pit bull"); Assert.assertEquals(dog.isFixed(), false); }
@Test public void insertConcatTest() throws IOException { InsertLine insertLine = new InsertLine(" fixed: false").relative("/src/main/resources/dogs.yaml"); TOExecutionResult executionResult = insertLine.execution(transformedAppFolder, transformationContext); Assert.assertEquals(executionResult.getType(), TOExecutionResult.Type.SUCCESS); assertChangedFile("/src/main/resources/dogs.yaml"); assertLineCount("/src/main/resources/dogs.yaml", 1); Map<String, Dog> dogs = (Map) getObjectFromYaml("/src/main/resources/dogs.yaml"); Assert.assertEquals(dogs.size(), 2); Dog dog = dogs.get("Mustache"); Assert.assertEquals(dog.getName(), "Mustache"); Assert.assertEquals(dog.getBreed(), "pit bull"); Assert.assertEquals(dog.isFixed(), false); }
@Test public void oneLineNoEOLTest() throws IOException { RemoveLine removeLine = new RemoveLine("(.*foo.*)").relative("/src/main/resources/application.properties").setFirstOnly(false); TOExecutionResult executionResult = removeLine.execution(transformedAppFolder, transformationContext); Assert.assertEquals(executionResult.getType(), TOExecutionResult.Type.SUCCESS); assertChangedFile("/src/main/resources/application.properties"); assertLineCount("/src/main/resources/application.properties", -2); Properties properties = getProperties("/src/main/resources/application.properties"); Assert.assertEquals(properties.size(), 1); Assert.assertEquals(properties.getProperty("bar"), "barv"); InsertLine insertLine = new InsertLine("p1=p1v", "(.*barv.*)").relative("/src/main/resources/application.properties"); executionResult = insertLine.execution(transformedAppFolder, transformationContext); Assert.assertEquals(executionResult.getType(), TOExecutionResult.Type.SUCCESS); assertChangedFile("/src/main/resources/application.properties"); assertLineCount("/src/main/resources/application.properties", -1); properties = getProperties("/src/main/resources/application.properties"); Assert.assertEquals(properties.size(), 2); Assert.assertEquals(properties.getProperty("bar"), "barv"); Assert.assertEquals(properties.getProperty("p1"), "p1v"); }
@Test public void fileDoesNotExistTest() { InsertLine insertLine = new InsertLine(" color: yellow", " name: Billy").relative("/src/main/resources/caes.yaml"); TOExecutionResult executionResult = insertLine.execution(transformedAppFolder, transformationContext); Assert.assertEquals(executionResult.getType(), TOExecutionResult.Type.ERROR); Assert.assertEquals(executionResult.getException().getClass(), FileNotFoundException.class); }