InsertCommonProperties extends AbstractTransformer { @Override public void transform() { if (disabled) return; for (Map.Entry<String,String> prop : COMMON_PROPS.entrySet()) { if (StringUtil.isBlank(pom.getProperty(prop.getKey()))) { logger.info("adding property: " + prop.getKey()); pom.setProperty(prop.getKey(), prop.getValue()); } } } InsertCommonProperties(PomWrapper pom, CommandLine options);  InsertCommonProperties(PomWrapper pom); @Override void transform();  }
@Test public void testAddPropsIfTheyDontExist() throws Exception { new InsertCommonProperties(loadPom("cleaner/CommonProps1.xml")).transform(); assertEquals("number of properties", 2, newXPath("/mvn:project/mvn:properties/*").evaluate(dom(), Element.class).size()); assertEquals("project.build.sourceEncoding", "UTF-8", newXPath("/mvn:project/mvn:properties/mvn:project.build.sourceEncoding").evaluateAsString(dom())); assertEquals("project.reporting.outputEncoding", "UTF-8", newXPath("/mvn:project/mvn:properties/mvn:project.reporting.outputEncoding").evaluateAsString(dom())); }
@Test public void testLeaveExistingPropsAlone() throws Exception { new InsertCommonProperties(loadPom("cleaner/CommonProps2.xml")).transform(); assertEquals("number of properties", 2, newXPath("/mvn:project/mvn:properties/*").evaluate(dom(), Element.class).size()); assertEquals("project.build.sourceEncoding", "US-ASCII", newXPath("/mvn:project/mvn:properties/mvn:project.build.sourceEncoding").evaluateAsString(dom())); assertEquals("project.reporting.outputEncoding", "UTF-8", newXPath("/mvn:project/mvn:properties/mvn:project.reporting.outputEncoding").evaluateAsString(dom())); }
@Test public void testDisabled() throws Exception { CommandLine args = new CommandLine("--noCommonProps"); new InsertCommonProperties(loadPom("cleaner/CommonProps1.xml"), args).transform(); assertEquals("number of properties", 0, newXPath("/mvn:project/mvn:properties/*").evaluate(dom(), Element.class).size()); }