ReplaceExplicitVersionsWithProperties extends AbstractTransformer { @Override public void transform() { if (disabled) return; Map<String,String> allProps = pom.getProperties(); List<Element> dependencies = selectAllDependencies(); if (! disablePlugins) dependencies.addAll(selectAllPlugins()); if (replaceExisting) { Set<String> oldProps = replaceExistingDependencyProps(dependencies, allProps); removeReplacedProperties(oldProps); } Set<String> newProps = updateDependencies(dependencies, allProps); addNewProperties(allProps, newProps); } ReplaceExplicitVersionsWithProperties(PomWrapper pom, CommandLine args);  ReplaceExplicitVersionsWithProperties(PomWrapper pom); @Override void transform();  }
@Test public void testBasicOperation() throws Exception { new ReplaceExplicitVersionsWithProperties(loadPom("cleaner/VersionProps1.xml")).transform(); assertProperty("commons-io.version", "2.1"); assertProperty("junit.version", "4.10"); assertDependencyReference("junit", "junit", "${junit.version}"); assertDependencyReference("commons-io", "commons-io", "${commons-io.version}"); assertNoDependencyReference("junit", "junit", "4.10"); assertNoDependencyReference("commons-io", "commons-io", "2.1"); String existingProp = newXPath("/mvn:project/mvn:properties/mvn:project.build.sourceEncoding") .evaluateAsString(dom()); assertEquals("existing property still exists", "UTF-8", existingProp); String lastPropValue = ""; for (Element prop : newXPath("/mvn:project/mvn:properties/*").evaluate(dom(), Element.class)) { String value = DomUtil.getLocalName(prop); if (!value.endsWith(".version")) continue; if (value.compareTo(lastPropValue) < 0) fail("found property " + value + " after " + lastPropValue); lastPropValue = value; } }
@Test public void testAdditionOfPropertiesSection() throws Exception { new ReplaceExplicitVersionsWithProperties(loadPom("cleaner/VersionProps2.xml")).transform(); Element props = newXPath("/mvn:project/mvn:properties").evaluateAsElement(dom()); assertNotNull("should find properties section", props); assertEquals("<properties> should have two children", 2, DomUtil.getChildren(props).size()); assertProperty("junit.version", "4.10"); assertProperty("net.sf.kdgcommons.version", "1.0.6"); assertDependencyReference("junit", "junit", "${junit.version}"); assertDependencyReference("net.sf.kdgcommons", "kdgcommons", "${net.sf.kdgcommons.version}"); }
@Test public void testExistingVersionPropertiesLeftAlone() throws Exception { new ReplaceExplicitVersionsWithProperties(loadPom("cleaner/VersionProps3.xml")).transform(); assertProperty("junit.version", "4.10"); assertProperty("kdgcommons.version", "1.0.6"); assertDependencyReference("junit", "junit", "${junit.version}"); assertDependencyReference("net.sf.kdgcommons", "kdgcommons", "${kdgcommons.version}"); Element props = newXPath("/mvn:project/mvn:properties").evaluateAsElement(dom()); assertNotNull("should find properties section", props); assertEquals("<properties> should have two children", 2, DomUtil.getChildren(props).size()); }
@Test public void testSameGroupDifferentVersion() throws Exception { new ReplaceExplicitVersionsWithProperties(loadPom("cleaner/VersionProps4.xml")).transform(); assertProperty("com.example.version", "1.2.3"); assertProperty("com.example.bar.version", "4.5.6"); assertDependencyReference("com.example", "foo", "${com.example.version}"); assertDependencyReference("com.example", "bar", "${com.example.bar.version}"); }
@Test public void testAlwaysCombineGroupAndArtifact() throws Exception { CommandLine args = new CommandLine("--addArtifactIdToProp=com.example"); new ReplaceExplicitVersionsWithProperties(loadPom("cleaner/VersionProps4.xml"), args).transform(); assertProperty("com.example.foo.version", "1.2.3"); assertProperty("com.example.bar.version", "4.5.6"); assertDependencyReference("com.example", "foo", "${com.example.foo.version}"); assertDependencyReference("com.example", "bar", "${com.example.bar.version}"); }
@Test public void testSameGroupSameVersion() throws Exception { new ReplaceExplicitVersionsWithProperties(loadPom("cleaner/VersionProps5.xml")).transform(); assertEquals("should only be one property added", 1, newXPath("/mvn:project/mvn:properties/*").evaluate(dom()).size()); assertProperty("org.springframework.version", "3.1.2.RELEASE"); assertDependencyReference("org.springframework", "spring-tx", "${org.springframework.version}"); assertDependencyReference("org.springframework", "spring-core", "${org.springframework.version}"); assertDependencyReference("org.springframework", "spring-context", "${org.springframework.version}"); }
@Test public void testReplaceExistingVersionProperties() throws Exception { CommandLine args = new CommandLine("--replaceExistingProps"); new ReplaceExplicitVersionsWithProperties(loadPom("cleaner/VersionProps6.xml"), args).transform(); assertEquals("post-transform property count", 3, newXPath("/mvn:project/mvn:properties/*").evaluate(dom()).size()); assertProperty("com.example.version", "1.2.3"); assertProperty("com.other.version", "4.5.6"); assertDependencyReference("com.example", "foo", "${com.example.version}"); assertDependencyReference("com.other", "bar", "${com.other.version}"); assertProperty("some.innocuous.propery", "foo"); }
@Test public void testReplacePluginVersions() throws Exception { new ReplaceExplicitVersionsWithProperties(loadPom("cleaner/VersionProps7.xml")).transform(); assertProperty("commons-io.version", "2.1"); assertProperty("storm.version", "0.7.0"); assertProperty("plugin.maven-antrun-plugin.version", "1.3"); assertProperty("plugin.maven-compiler-plugin.version", "2.3.2"); assertProperty("plugin.cobertura-maven-plugin.version", "2.5.1"); assertEquals("build plugin", "${plugin.maven-antrun-plugin.version}", newXPath("/mvn:project/mvn:build/mvn:pluginManagement/mvn:plugins/mvn:plugin/mvn:version") .evaluateAsString(dom())); assertEquals("build plugin", "${plugin.maven-compiler-plugin.version}", newXPath("/mvn:project/mvn:build/mvn:plugins/mvn:plugin/mvn:version") .evaluateAsString(dom())); assertEquals("build plugin", "${plugin.cobertura-maven-plugin.version}", newXPath("/mvn:project/mvn:reporting/mvn:plugins/mvn:plugin/mvn:version") .evaluateAsString(dom())); String lastPluginProp = null; for (Element prop : newXPath("/mvn:project/mvn:properties/*").evaluate(dom(), Element.class)) { String value = DomUtil.getLocalName(prop); if (value.startsWith("plugin.")) lastPluginProp = value; else if (lastPluginProp != null) fail("found property " + value + " after " + lastPluginProp); } }
@Test public void testLeaveExistingPluginVersions() throws Exception { new ReplaceExplicitVersionsWithProperties(loadPom("cleaner/VersionProps8.xml")).transform(); assertProperty("commons-io.version", "2.1"); assertProperty("storm.version", "0.7.0"); assertProperty("plugins.maven-antrun-plugin", "1.3"); assertProperty("plugin.maven-compiler-plugin.version", "2.3.2"); assertEquals("build plugin", "${plugins.maven-antrun-plugin}", newXPath("/mvn:project/mvn:build/mvn:pluginManagement/mvn:plugins/mvn:plugin/mvn:version") .evaluateAsString(dom())); assertEquals("build plugin", "${plugin.maven-compiler-plugin.version}", newXPath("/mvn:project/mvn:build/mvn:plugins/mvn:plugin/mvn:version") .evaluateAsString(dom())); }
@Test public void testPluginsWithSameArtifactId() throws Exception { new ReplaceExplicitVersionsWithProperties(loadPom("cleaner/VersionProps9.xml")).transform(); assertProperty("plugin.example-plugin.version", "1.1"); assertProperty("plugin.example-plugin-1.2.version", "1.2"); assertEquals("build plugin", "${plugin.example-plugin.version}", newXPath(" .evaluateAsString(dom())); assertEquals("build plugin", "${plugin.example-plugin-1.2.version}", newXPath(" .evaluateAsString(dom())); }
@Test public void testReplaceExistingPluginVersionProperties() throws Exception { CommandLine args = new CommandLine("--replaceExistingProps"); new ReplaceExplicitVersionsWithProperties(loadPom("cleaner/VersionProps8.xml"), args).transform(); assertProperty("commons-io.version", "2.1"); assertProperty("storm.version", "0.7.0"); assertProperty("plugin.maven-antrun-plugin.version", "1.3"); assertProperty("plugin.maven-compiler-plugin.version", "2.3.2"); assertEquals("build plugin", "${plugin.maven-antrun-plugin.version}", newXPath("/mvn:project/mvn:build/mvn:pluginManagement/mvn:plugins/mvn:plugin/mvn:version") .evaluateAsString(dom())); assertEquals("build plugin", "${plugin.maven-compiler-plugin.version}", newXPath("/mvn:project/mvn:build/mvn:plugins/mvn:plugin/mvn:version") .evaluateAsString(dom())); }
@Test public void testIgnorePluginsWithoutVersions() throws Exception { new ReplaceExplicitVersionsWithProperties(loadPom("cleaner/VersionProps10.xml")).transform(); assertEquals("plugin group", "com.example", newXPath("/mvn:project/mvn:build/mvn:pluginManagement/mvn:plugins/mvn:plugin/mvn:groupId") .evaluateAsString(dom())); assertEquals("plugin artifact", "example-plugin", newXPath("/mvn:project/mvn:build/mvn:pluginManagement/mvn:plugins/mvn:plugin/mvn:artifactId") .evaluateAsString(dom())); assertEquals("plugin version", "", newXPath("/mvn:project/mvn:build/mvn:pluginManagement/mvn:plugins/mvn:plugin/mvn:version") .evaluateAsString(dom())); }
@Test public void testDisabled() throws Exception { CommandLine args = new CommandLine("--noVersionProps"); new ReplaceExplicitVersionsWithProperties(loadPom("cleaner/VersionProps1.xml"), args).transform(); assertNull("property should not be added", newXPath("/mvn:project/mvn:properties/mvn:junit.version").evaluateAsElement(dom())); assertDependencyReference("junit", "junit", "4.10"); }
@Test public void testDisablePluginConversion() throws Exception { CommandLine args = new CommandLine("--noConvertPluginVersions"); new ReplaceExplicitVersionsWithProperties(loadPom("cleaner/VersionProps7.xml"), args).transform(); assertProperty("commons-io.version", "2.1"); assertProperty("storm.version", "0.7.0"); assertProperty("plugin.maven-antrun-plugin.version", ""); assertProperty("plugin.maven-compiler-plugin.version", ""); assertProperty("plugin.cobertura-maven-plugin.version", ""); assertEquals("build plugin", "1.3", newXPath("/mvn:project/mvn:build/mvn:pluginManagement/mvn:plugins/mvn:plugin/mvn:version") .evaluateAsString(dom())); assertEquals("build plugin", "2.3.2", newXPath("/mvn:project/mvn:build/mvn:plugins/mvn:plugin/mvn:version") .evaluateAsString(dom())); assertEquals("build plugin", "2.5.1", newXPath("/mvn:project/mvn:reporting/mvn:plugins/mvn:plugin/mvn:version") .evaluateAsString(dom())); }