[MNG-8230] Add tests for CI-friendly versions (#366)

diff --git a/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng8230CIFriendlyTest.java b/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng8230CIFriendlyTest.java
new file mode 100644
index 0000000..271bd7f
--- /dev/null
+++ b/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng8230CIFriendlyTest.java
@@ -0,0 +1,183 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.it;
+
+import java.io.File;
+import java.util.List;
+import java.util.Objects;
+import java.util.Properties;
+
+import org.apache.maven.shared.verifier.VerificationException;
+import org.apache.maven.shared.verifier.Verifier;
+import org.apache.maven.shared.verifier.util.ResourceExtractor;
+import org.junit.jupiter.api.Test;
+
+/**
+ * This is a test set for <a href="https://issues.apache.org/jira/browse/MNG-8230">MNG-8230</a>.
+ */
+class MavenITmng8230CIFriendlyTest extends AbstractMavenIntegrationTestCase {
+
+    private static final String PROPERTIES = "target/expression.properties";
+
+    MavenITmng8230CIFriendlyTest() {
+        super("[4.0.0-beta-5,)");
+    }
+
+    /**
+     *  Verify that CI friendly version work when using project properties
+     *
+     * @throws Exception in case of failure
+     */
+    @Test
+    void testitCiFriendlyWithProjectProperties() throws Exception {
+        File testDir = ResourceExtractor.simpleExtractResources(getClass(), "/mng-8230-ci-friendly-and-gav");
+
+        File basedir = new File(testDir, "cif-with-project-props");
+        Verifier verifier = newVerifier(basedir.getAbsolutePath());
+        verifier.addCliArgument("-Dexpression.outputFile=" + new File(basedir, PROPERTIES).getPath());
+        verifier.addCliArgument("-Dexpression.expressions=project/version");
+        verifier.addCliArgument("org.apache.maven.its.plugins:maven-it-plugin-expression:2.1-SNAPSHOT:eval");
+        verifier.execute();
+        verifier.verifyErrorFreeLog();
+        verifier.verifyFilePresent(PROPERTIES);
+        Properties props = verifier.loadProperties(PROPERTIES);
+        assertEquals(props.getProperty("project.version"), "1.0-SNAPSHOT");
+    }
+
+    /**
+     *  Verify that CI friendly version work when using project properties
+     *
+     * @throws Exception in case of failure
+     */
+    @Test
+    void testitCiFriendlyWithProjectPropertiesOverride() throws Exception {
+        File testDir = ResourceExtractor.simpleExtractResources(getClass(), "/mng-8230-ci-friendly-and-gav");
+
+        File basedir = new File(testDir, "cif-with-project-props");
+        Verifier verifier = newVerifier(basedir.getAbsolutePath());
+        verifier.addCliArgument("-Dexpression.outputFile=" + new File(basedir, PROPERTIES).getPath());
+        verifier.addCliArgument("-Dexpression.expressions=project/version");
+        verifier.addCliArgument("org.apache.maven.its.plugins:maven-it-plugin-expression:2.1-SNAPSHOT:eval");
+        verifier.addCliArgument("-Dci-version=1.1-SNAPSHOT");
+        verifier.execute();
+        verifier.verifyErrorFreeLog();
+        verifier.verifyFilePresent(PROPERTIES);
+        Properties props = verifier.loadProperties(PROPERTIES);
+        assertEquals(props.getProperty("project.version"), "1.1-SNAPSHOT");
+    }
+
+    /**
+     *  Verify that CI friendly version work when using user properties
+     *
+     * @throws Exception in case of failure
+     */
+    @Test
+    void testitCiFriendlyWithUserProperties() throws Exception {
+        File testDir = ResourceExtractor.simpleExtractResources(getClass(), "/mng-8230-ci-friendly-and-gav");
+
+        File basedir = new File(testDir, "cif-with-user-props");
+        Verifier verifier = newVerifier(basedir.getAbsolutePath());
+
+        verifier.addCliArgument("-Dexpression.outputFile=" + new File(basedir, PROPERTIES).getPath());
+        verifier.addCliArgument("-Dexpression.expressions=project/version");
+        verifier.addCliArgument("org.apache.maven.its.plugins:maven-it-plugin-expression:2.1-SNAPSHOT:eval");
+        verifier.addCliArgument("-Dci-version=1.1-SNAPSHOT");
+        verifier.execute();
+        verifier.verifyErrorFreeLog();
+        verifier.verifyFilePresent(PROPERTIES);
+        Properties props = verifier.loadProperties(PROPERTIES);
+        assertEquals(props.getProperty("project.version"), "1.1-SNAPSHOT");
+    }
+
+    /**
+     *  Verify that CI friendly version fails if the properties are not given
+     *
+     * @throws Exception in case of failure
+     */
+    @Test
+    void testitCiFriendlyWithUserPropertiesNotGiven() throws Exception {
+        File testDir = ResourceExtractor.simpleExtractResources(getClass(), "/mng-8230-ci-friendly-and-gav");
+
+        File basedir = new File(testDir, "cif-with-user-props");
+        Verifier verifier = newVerifier(basedir.getAbsolutePath());
+        verifier.addCliArgument("-Dexpression.outputFile=" + new File(basedir, PROPERTIES).getPath());
+        verifier.addCliArgument("-Dexpression.expressions=project/version");
+        verifier.addCliArgument("org.apache.maven.its.plugins:maven-it-plugin-expression:2.1-SNAPSHOT:eval");
+        try {
+            verifier.execute();
+            fail("Expected failure");
+        } catch (VerificationException e) {
+            assertTrue(
+                    e.getMessage(),
+                    e.getMessage()
+                            .contains(
+                                    "'version' contains an expression but should be a constant. @ myGroup:parent:${ci-version}"));
+        }
+    }
+
+    @Test
+    void testitExpressionInGroupId() throws Exception {
+        File testDir = ResourceExtractor.simpleExtractResources(getClass(), "/mng-8230-ci-friendly-and-gav");
+
+        File basedir = new File(testDir, "exp-in-groupid");
+        Verifier verifier = newVerifier(basedir.getAbsolutePath());
+        verifier.addCliArgument("-Dexpression.outputFile=" + new File(basedir, PROPERTIES).getPath());
+        verifier.addCliArgument("-Dexpression.expressions=project/version");
+        verifier.addCliArgument("org.apache.maven.its.plugins:maven-it-plugin-expression:2.1-SNAPSHOT:eval");
+        try {
+            verifier.execute();
+            fail("Expected failure");
+        } catch (VerificationException e) {
+            assertTrue(
+                    e.getMessage(),
+                    e.getMessage()
+                            .contains(
+                                    "'groupId' contains an expression but should be a constant. @ ${foo}:myArtifact:1.0-SNAPSHOT"));
+        }
+    }
+
+    @Test
+    void testitExpressionInArtifactId() throws Exception {
+        File testDir = ResourceExtractor.simpleExtractResources(getClass(), "/mng-8230-ci-friendly-and-gav");
+
+        File basedir = new File(testDir, "exp-in-artifactid");
+        Verifier verifier = newVerifier(basedir.getAbsolutePath());
+        verifier.addCliArgument("-Dexpression.outputFile=" + new File(basedir, PROPERTIES).getPath());
+        verifier.addCliArgument("-Dexpression.expressions=project/version");
+        verifier.addCliArgument("org.apache.maven.its.plugins:maven-it-plugin-expression:2.1-SNAPSHOT:eval");
+        try {
+            verifier.execute();
+            fail("Expected failure");
+        } catch (VerificationException e) {
+            assertTrue(
+                    e.getMessage(),
+                    e.getMessage()
+                            .contains(
+                                    "'artifactId' contains an expression but should be a constant. @ myGroup:${foo}:1.0-SNAPSHOT"));
+        }
+    }
+
+    void verifyExactLine(Verifier verifier, String line) throws Exception {
+        List<String> lines = verifier.loadFile(verifier.getBasedir(), verifier.getLogFileName(), false);
+        if (lines.stream().noneMatch(s -> Objects.equals(s, line))) {
+            throw new VerificationException(
+                    "Could not find line: '" + line + "' in output log:\n" + String.join("\n", lines));
+        }
+    }
+}
diff --git a/core-it-suite/src/test/java/org/apache/maven/it/TestSuiteOrdering.java b/core-it-suite/src/test/java/org/apache/maven/it/TestSuiteOrdering.java
index 3524aae..1619516 100644
--- a/core-it-suite/src/test/java/org/apache/maven/it/TestSuiteOrdering.java
+++ b/core-it-suite/src/test/java/org/apache/maven/it/TestSuiteOrdering.java
@@ -120,6 +120,7 @@
          * the tests are to finishing. Newer tests are also more likely to fail, so this is
          * a fail fast technique as well.
          */
+        suite.addTestSuite(MavenITmng8230CIFriendlyTest.class);
         suite.addTestSuite(MavenITmng7255InferredGroupIdTest.class);
         suite.addTestSuite(MavenITmng8220ExtensionWithDITest.class);
         suite.addTestSuite(MavenITmng8181CentralRepoTest.class);
diff --git a/core-it-suite/src/test/resources/mng-8230-ci-friendly-and-gav/cif-with-project-props/child/pom.xml b/core-it-suite/src/test/resources/mng-8230-ci-friendly-and-gav/cif-with-project-props/child/pom.xml
new file mode 100644
index 0000000..6d3d27d
--- /dev/null
+++ b/core-it-suite/src/test/resources/mng-8230-ci-friendly-and-gav/cif-with-project-props/child/pom.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project>
+  <modelVersion>4.1.0</modelVersion>
+  <parent />
+  <artifactId>child</artifactId>
+</project>
diff --git a/core-it-suite/src/test/resources/mng-8230-ci-friendly-and-gav/cif-with-project-props/pom.xml b/core-it-suite/src/test/resources/mng-8230-ci-friendly-and-gav/cif-with-project-props/pom.xml
new file mode 100644
index 0000000..32b2047
--- /dev/null
+++ b/core-it-suite/src/test/resources/mng-8230-ci-friendly-and-gav/cif-with-project-props/pom.xml
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project root="true">
+  <modelVersion>4.1.0</modelVersion>
+
+  <groupId>myGroup</groupId>
+  <artifactId>parent</artifactId>
+  <version>${ci-version}</version>
+  <packaging>pom</packaging>
+
+  <properties>
+    <ci-version>1.0-SNAPSHOT</ci-version>
+  </properties>
+</project>
diff --git a/core-it-suite/src/test/resources/mng-8230-ci-friendly-and-gav/cif-with-user-props/child/pom.xml b/core-it-suite/src/test/resources/mng-8230-ci-friendly-and-gav/cif-with-user-props/child/pom.xml
new file mode 100644
index 0000000..6d3d27d
--- /dev/null
+++ b/core-it-suite/src/test/resources/mng-8230-ci-friendly-and-gav/cif-with-user-props/child/pom.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project>
+  <modelVersion>4.1.0</modelVersion>
+  <parent />
+  <artifactId>child</artifactId>
+</project>
diff --git a/core-it-suite/src/test/resources/mng-8230-ci-friendly-and-gav/cif-with-user-props/pom.xml b/core-it-suite/src/test/resources/mng-8230-ci-friendly-and-gav/cif-with-user-props/pom.xml
new file mode 100644
index 0000000..6d2c7e5
--- /dev/null
+++ b/core-it-suite/src/test/resources/mng-8230-ci-friendly-and-gav/cif-with-user-props/pom.xml
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project root="true">
+  <modelVersion>4.1.0</modelVersion>
+
+  <groupId>myGroup</groupId>
+  <artifactId>parent</artifactId>
+  <version>${ci-version}</version>
+  <packaging>pom</packaging>
+</project>
diff --git a/core-it-suite/src/test/resources/mng-8230-ci-friendly-and-gav/exp-in-artifactid/pom.xml b/core-it-suite/src/test/resources/mng-8230-ci-friendly-and-gav/exp-in-artifactid/pom.xml
new file mode 100644
index 0000000..5952bd8
--- /dev/null
+++ b/core-it-suite/src/test/resources/mng-8230-ci-friendly-and-gav/exp-in-artifactid/pom.xml
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project root="true">
+  <modelVersion>4.1.0</modelVersion>
+
+  <groupId>myGroup</groupId>
+  <artifactId>${foo}</artifactId>
+  <version>1.0-SNAPSHOT</version>
+</project>
diff --git a/core-it-suite/src/test/resources/mng-8230-ci-friendly-and-gav/exp-in-groupid/pom.xml b/core-it-suite/src/test/resources/mng-8230-ci-friendly-and-gav/exp-in-groupid/pom.xml
new file mode 100644
index 0000000..0e0cbed
--- /dev/null
+++ b/core-it-suite/src/test/resources/mng-8230-ci-friendly-and-gav/exp-in-groupid/pom.xml
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project root="true">
+  <modelVersion>4.1.0</modelVersion>
+
+  <groupId>${foo}</groupId>
+  <artifactId>myArtifact</artifactId>
+  <version>1.0-SNAPSHOT</version>
+</project>