From a2eb2fe3ee7b1362d3d43c07a0b47ff532dbdf37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Boutemy?= Date: Wed, 2 Sep 2015 04:36:00 +0200 Subject: [PATCH] added extensive urls inheritance unit tests, even for most tricky parts --- .../DefaultInheritanceAssemblerTest.java | 100 ++++++++++++++++-- .../poms/inheritance/flat-urls-child.xml | 34 ++++++ .../poms/inheritance/flat-urls-expected.xml | 50 +++++++++ .../poms/inheritance/flat-urls-parent.xml | 49 +++++++++ .../tricky-flat-artifactId-urls-child.xml | 34 ++++++ .../tricky-flat-artifactId-urls-expected.xml | 50 +++++++++ .../tricky-flat-artifactId-urls-parent.xml | 49 +++++++++ .../tricky-flat-directory-urls-child.xml | 34 ++++++ .../tricky-flat-directory-urls-expected.xml | 50 +++++++++ .../tricky-flat-directory-urls-parent.xml | 49 +++++++++ .../resources/poms/inheritance/urls-child.xml | 34 ++++++ .../poms/inheritance/urls-expected.xml | 50 +++++++++ .../poms/inheritance/urls-parent.xml | 49 +++++++++ 13 files changed, 623 insertions(+), 9 deletions(-) create mode 100644 maven-model-builder/src/test/resources/poms/inheritance/flat-urls-child.xml create mode 100644 maven-model-builder/src/test/resources/poms/inheritance/flat-urls-expected.xml create mode 100644 maven-model-builder/src/test/resources/poms/inheritance/flat-urls-parent.xml create mode 100644 maven-model-builder/src/test/resources/poms/inheritance/tricky-flat-artifactId-urls-child.xml create mode 100644 maven-model-builder/src/test/resources/poms/inheritance/tricky-flat-artifactId-urls-expected.xml create mode 100644 maven-model-builder/src/test/resources/poms/inheritance/tricky-flat-artifactId-urls-parent.xml create mode 100644 maven-model-builder/src/test/resources/poms/inheritance/tricky-flat-directory-urls-child.xml create mode 100644 maven-model-builder/src/test/resources/poms/inheritance/tricky-flat-directory-urls-expected.xml create mode 100644 maven-model-builder/src/test/resources/poms/inheritance/tricky-flat-directory-urls-parent.xml create mode 100644 maven-model-builder/src/test/resources/poms/inheritance/urls-child.xml create mode 100644 maven-model-builder/src/test/resources/poms/inheritance/urls-expected.xml create mode 100644 maven-model-builder/src/test/resources/poms/inheritance/urls-parent.xml diff --git a/maven-model-builder/src/test/java/org/apache/maven/model/inheritance/DefaultInheritanceAssemblerTest.java b/maven-model-builder/src/test/java/org/apache/maven/model/inheritance/DefaultInheritanceAssemblerTest.java index f9e95ecd67..ae4528367a 100644 --- a/maven-model-builder/src/test/java/org/apache/maven/model/inheritance/DefaultInheritanceAssemblerTest.java +++ b/maven-model-builder/src/test/java/org/apache/maven/model/inheritance/DefaultInheritanceAssemblerTest.java @@ -28,6 +28,8 @@ import org.custommonkey.xmlunit.XMLAssert; import org.custommonkey.xmlunit.XMLUnit; +import junit.framework.AssertionFailedError; + import java.io.File; import java.io.FileInputStream; import java.io.IOException; @@ -71,29 +73,109 @@ private Model getModel( String name ) public void testPluginConfiguration() throws Exception { - Model parent = getModel( "plugin-configuration-parent" ); + testInheritance( "plugin-configuration" ); + } - Model child = getModel( "plugin-configuration-child" ); + /** + * Check most classical urls inheritance: directory structure where parent POM in parent directory + * and child directory == artifatId + * @throws Exception + */ + public void testUrls() + throws Exception + { + testInheritance( "urls" ); + } + + /** + * Flat directory structure: parent & child POMs in sibling directories, child directory == artifactId. + * @throws Exception + */ + public void testFlatUrls() + throws Exception + { + testInheritance( "flat-urls" ); + } + + /** + * Tricky case: flat directory structure, but child directory != artifactId. + * Model interpolation does not give same result when calculated from build or from repo... + * This is why MNG-5000 fix in code is marked as bad practice (uses file names) + * @throws Exception + */ + public void testFlatTrickyUrls() + throws Exception + { + // parent references child with artifactId (which is not directory name) + // then relative path calculation will fail during build from disk but success when calculated from repo + try + { + // build from disk expected to fail + testInheritance( "tricky-flat-artifactId-urls", false ); + fail( "should have failed since module reference == artifactId != directory name" ); + } + catch ( AssertionFailedError afe ) + { + // expected failure: wrong relative path calculation + assertTrue( afe.getMessage().contains( "http://www.apache.org/path/to/parent/child-artifact-id/" ) ); + } + // but ok from repo: local disk is ignored + testInheritance( "tricky-flat-artifactId-urls", true ); + + // parent references child with directory name (which is not artifact id) + // then relative path calculation will success during build from disk but failwhen calculated from repo + testInheritance( "tricky-flat-directory-urls", false ); + try + { + testInheritance( "tricky-flat-directory-urls", true ); + fail( "should have failed since module reference == directory name != artifactId" ); + } + catch ( AssertionFailedError afe ) + { + // expected failure + assertTrue( afe.getMessage().contains( "http://www.apache.org/path/to/parent/child-artifact-id/" ) ); + } + } + + public void testInheritance( String baseName ) + throws Exception + { + testInheritance( baseName, false ); + testInheritance( baseName, true ); + } + + public void testInheritance( String baseName, boolean fromRepo ) + throws Exception + { + Model parent = getModel( baseName + "-parent" ); + + Model child = getModel( baseName + "-child" ); + + if ( fromRepo ) + { + // when model is read from repo, a stream is used, then pomFile == null + // (has consequences in inheritance algorithm since getProjectDirectory() returns null) + parent.setPomFile( null ); + child.setPomFile( null ); + } SimpleProblemCollector problems = new SimpleProblemCollector(); assembler.assembleModelInheritance( child, parent, null, problems ); - File actual = getTestFile( "target/test-classes/poms/inheritance/plugin-configuration-actual.xml" ); - + // write baseName + "-actual" + File actual = getTestFile( "target/test-classes/poms/inheritance/" + baseName + + ( fromRepo ? "-build" : "-repo" ) + "-actual.xml" ); writer.write( actual, null, child ); - // check with getPom( "plugin-configuration-effective" ) - File expected = getPom( "plugin-configuration-expected" ); + // check with getPom( baseName + "-expected" ) + File expected = getPom( baseName + "-expected" ); try ( Reader control = new InputStreamReader( new FileInputStream( expected ), "UTF-8" ); Reader test = new InputStreamReader( new FileInputStream( actual ), "UTF-8" ) ) { - XMLUnit.setIgnoreComments( true ); XMLUnit.setIgnoreWhitespace( true ); XMLAssert.assertXMLEqual( control, test ); } - } - } diff --git a/maven-model-builder/src/test/resources/poms/inheritance/flat-urls-child.xml b/maven-model-builder/src/test/resources/poms/inheritance/flat-urls-child.xml new file mode 100644 index 0000000000..d7cc4d0b4f --- /dev/null +++ b/maven-model-builder/src/test/resources/poms/inheritance/flat-urls-child.xml @@ -0,0 +1,34 @@ + + + + + + 4.0.0 + + + inheritance + parent + 11-SNAPSHOT + + + inheritance + Model urls inheritance test child + \ No newline at end of file diff --git a/maven-model-builder/src/test/resources/poms/inheritance/flat-urls-expected.xml b/maven-model-builder/src/test/resources/poms/inheritance/flat-urls-expected.xml new file mode 100644 index 0000000000..88fb1e236d --- /dev/null +++ b/maven-model-builder/src/test/resources/poms/inheritance/flat-urls-expected.xml @@ -0,0 +1,50 @@ + + + + + + 4.0.0 + + + inheritance + parent + 11-SNAPSHOT + + + inheritance + inheritance + 11-SNAPSHOT + Model urls inheritance test child + Flat directory structure case: module = ../child-artifact-id + child directory path == child-artifact-id + + + http://www.apache.org/path/to/parent/../inheritance/ + + scm:my-scm:http://domain.org/base/../inheritance + scm:my-scm:https://domain.org/base/../inheritance/ + https://domain.org/base/../inheritance + + + + scp://scp.domain.org/base/../inheritance/ + + + diff --git a/maven-model-builder/src/test/resources/poms/inheritance/flat-urls-parent.xml b/maven-model-builder/src/test/resources/poms/inheritance/flat-urls-parent.xml new file mode 100644 index 0000000000..3bf6ea8de5 --- /dev/null +++ b/maven-model-builder/src/test/resources/poms/inheritance/flat-urls-parent.xml @@ -0,0 +1,49 @@ + + + + + + 4.0.0 + + inheritance + parent + 11-SNAPSHOT + + Model urls inheritance test parent + Flat directory structure case: module = ../child-artifact-id + child directory path == child-artifact-id + + + ../inheritance + + + + http://www.apache.org/path/to/parent/ + + scm:my-scm:http://domain.org/base + scm:my-scm:https://domain.org/base/ + https://domain.org/base + + + + scp://scp.domain.org/base/ + + + \ No newline at end of file diff --git a/maven-model-builder/src/test/resources/poms/inheritance/tricky-flat-artifactId-urls-child.xml b/maven-model-builder/src/test/resources/poms/inheritance/tricky-flat-artifactId-urls-child.xml new file mode 100644 index 0000000000..ac036af355 --- /dev/null +++ b/maven-model-builder/src/test/resources/poms/inheritance/tricky-flat-artifactId-urls-child.xml @@ -0,0 +1,34 @@ + + + + + + 4.0.0 + + + inheritance + parent + 11-SNAPSHOT + + + child-artifact-id + Model urls inheritance test child + \ No newline at end of file diff --git a/maven-model-builder/src/test/resources/poms/inheritance/tricky-flat-artifactId-urls-expected.xml b/maven-model-builder/src/test/resources/poms/inheritance/tricky-flat-artifactId-urls-expected.xml new file mode 100644 index 0000000000..f2a024eabd --- /dev/null +++ b/maven-model-builder/src/test/resources/poms/inheritance/tricky-flat-artifactId-urls-expected.xml @@ -0,0 +1,50 @@ + + + + + + 4.0.0 + + + inheritance + parent + 11-SNAPSHOT + + + inheritance + child-artifact-id + 11-SNAPSHOT + Model urls inheritance test child + Flat directory structure case: module = ../child-artifact-id + child directory path != child-artifact-id + + + http://www.apache.org/path/to/parent/../child-artifact-id/ + + scm:my-scm:http://domain.org/base/../child-artifact-id + scm:my-scm:https://domain.org/base/../child-artifact-id/ + https://domain.org/base/../child-artifact-id + + + + scp://scp.domain.org/base/../child-artifact-id/ + + + diff --git a/maven-model-builder/src/test/resources/poms/inheritance/tricky-flat-artifactId-urls-parent.xml b/maven-model-builder/src/test/resources/poms/inheritance/tricky-flat-artifactId-urls-parent.xml new file mode 100644 index 0000000000..8a1d354b79 --- /dev/null +++ b/maven-model-builder/src/test/resources/poms/inheritance/tricky-flat-artifactId-urls-parent.xml @@ -0,0 +1,49 @@ + + + + + + 4.0.0 + + inheritance + parent + 11-SNAPSHOT + + Model urls inheritance test parent + Flat directory structure case: module = ../child-artifact-id + child directory path != child-artifact-id + + + ../child-artifact-id + + + + http://www.apache.org/path/to/parent/ + + scm:my-scm:http://domain.org/base + scm:my-scm:https://domain.org/base/ + https://domain.org/base + + + + scp://scp.domain.org/base/ + + + \ No newline at end of file diff --git a/maven-model-builder/src/test/resources/poms/inheritance/tricky-flat-directory-urls-child.xml b/maven-model-builder/src/test/resources/poms/inheritance/tricky-flat-directory-urls-child.xml new file mode 100644 index 0000000000..ac036af355 --- /dev/null +++ b/maven-model-builder/src/test/resources/poms/inheritance/tricky-flat-directory-urls-child.xml @@ -0,0 +1,34 @@ + + + + + + 4.0.0 + + + inheritance + parent + 11-SNAPSHOT + + + child-artifact-id + Model urls inheritance test child + \ No newline at end of file diff --git a/maven-model-builder/src/test/resources/poms/inheritance/tricky-flat-directory-urls-expected.xml b/maven-model-builder/src/test/resources/poms/inheritance/tricky-flat-directory-urls-expected.xml new file mode 100644 index 0000000000..429ae38b98 --- /dev/null +++ b/maven-model-builder/src/test/resources/poms/inheritance/tricky-flat-directory-urls-expected.xml @@ -0,0 +1,50 @@ + + + + + + 4.0.0 + + + inheritance + parent + 11-SNAPSHOT + + + inheritance + child-artifact-id + 11-SNAPSHOT + Model urls inheritance test child + Flat directory structure case: module = ../child directory path + child directory path != child-artifact-id + + + http://www.apache.org/path/to/parent/../child-artifact-id/ + + scm:my-scm:http://domain.org/base/../child-artifact-id + scm:my-scm:https://domain.org/base/../child-artifact-id/ + https://domain.org/base/../child-artifact-id + + + + scp://scp.domain.org/base/../child-artifact-id/ + + + diff --git a/maven-model-builder/src/test/resources/poms/inheritance/tricky-flat-directory-urls-parent.xml b/maven-model-builder/src/test/resources/poms/inheritance/tricky-flat-directory-urls-parent.xml new file mode 100644 index 0000000000..211abbed80 --- /dev/null +++ b/maven-model-builder/src/test/resources/poms/inheritance/tricky-flat-directory-urls-parent.xml @@ -0,0 +1,49 @@ + + + + + + 4.0.0 + + inheritance + parent + 11-SNAPSHOT + + Model urls inheritance test parent + Flat directory structure case: module = ../child directory path + child directory path != child-artifact-id + + + ../inheritance + + + + http://www.apache.org/path/to/parent/ + + scm:my-scm:http://domain.org/base + scm:my-scm:https://domain.org/base/ + https://domain.org/base + + + + scp://scp.domain.org/base/ + + + \ No newline at end of file diff --git a/maven-model-builder/src/test/resources/poms/inheritance/urls-child.xml b/maven-model-builder/src/test/resources/poms/inheritance/urls-child.xml new file mode 100644 index 0000000000..ac036af355 --- /dev/null +++ b/maven-model-builder/src/test/resources/poms/inheritance/urls-child.xml @@ -0,0 +1,34 @@ + + + + + + 4.0.0 + + + inheritance + parent + 11-SNAPSHOT + + + child-artifact-id + Model urls inheritance test child + \ No newline at end of file diff --git a/maven-model-builder/src/test/resources/poms/inheritance/urls-expected.xml b/maven-model-builder/src/test/resources/poms/inheritance/urls-expected.xml new file mode 100644 index 0000000000..337063846f --- /dev/null +++ b/maven-model-builder/src/test/resources/poms/inheritance/urls-expected.xml @@ -0,0 +1,50 @@ + + + + + + 4.0.0 + + + inheritance + parent + 11-SNAPSHOT + + + inheritance + child-artifact-id + 11-SNAPSHOT + Model urls inheritance test child + Most classical case: child in direct subdirectory with directory name == child artifactId + + + http://www.apache.org/path/to/parent/child-artifact-id/ + + scm:my-scm:http://domain.org/base/child-artifact-id + scm:my-scm:https://domain.org/base/child-artifact-id/ + https://domain.org/base/child-artifact-id + + + + scp://scp.domain.org/base/child-artifact-id/ + + + diff --git a/maven-model-builder/src/test/resources/poms/inheritance/urls-parent.xml b/maven-model-builder/src/test/resources/poms/inheritance/urls-parent.xml new file mode 100644 index 0000000000..cbe768ac5b --- /dev/null +++ b/maven-model-builder/src/test/resources/poms/inheritance/urls-parent.xml @@ -0,0 +1,49 @@ + + + + + + 4.0.0 + + inheritance + parent + 11-SNAPSHOT + + Model urls inheritance test parent + Most classical case: child in direct subdirectory with directory name == child artifactId + + + child-artifact-id + + + + http://www.apache.org/path/to/parent/ + + scm:my-scm:http://domain.org/base + scm:my-scm:https://domain.org/base/ + https://domain.org/base + + + + scp://scp.domain.org/base/ + + + \ No newline at end of file