Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Schreiber2015-04-17 10:05:14 -0400
committerGerrit Code Review @ Eclipse.org2015-04-27 09:16:32 -0400
commitd95d53744a6f1804816960edb12398e697e386ca (patch)
tree34492b3a8850346a96c4694fa1ad3e6a6a1ceb71
parent3283fdca602b1aee0c24c504d84147d74c27c7f1 (diff)
downloadorg.eclipse.tycho-d95d53744a6f1804816960edb12398e697e386ca.tar.gz
org.eclipse.tycho-d95d53744a6f1804816960edb12398e697e386ca.tar.xz
org.eclipse.tycho-d95d53744a6f1804816960edb12398e697e386ca.zip
464887 Verify the build qualifier
_Verify the qualifier that it is a valid OSGi qualifier by parsing 1.0.0.<qualifier> as OSGi version. In case of an Exception, fail the build with an appropriate error message. _Both cases are checked, when the qualifier is forced by the "forceContextQualifier" or by the "format" parameter _Adding tests for malformed qualifier Bug: 464887 Change-Id: I46236eef1612ac4456fe2765f8140d0b721c4fdd
-rw-r--r--tycho-packaging-plugin/src/main/java/org/eclipse/tycho/buildversion/BuildQualifierMojo.java12
-rw-r--r--tycho-packaging-plugin/src/test/java/org/eclipse/tycho/buildversion/BuildQualifierTest.java137
2 files changed, 67 insertions, 82 deletions
diff --git a/tycho-packaging-plugin/src/main/java/org/eclipse/tycho/buildversion/BuildQualifierMojo.java b/tycho-packaging-plugin/src/main/java/org/eclipse/tycho/buildversion/BuildQualifierMojo.java
index 63ed5df26..dc207d0c1 100644
--- a/tycho-packaging-plugin/src/main/java/org/eclipse/tycho/buildversion/BuildQualifierMojo.java
+++ b/tycho-packaging-plugin/src/main/java/org/eclipse/tycho/buildversion/BuildQualifierMojo.java
@@ -163,6 +163,8 @@ public class BuildQualifierMojo extends AbstractVersionMojo {
qualifier = getQualifier(timestamp);
}
+ validateQualifier(qualifier);
+
return new TychoProjectVersion(getUnqualifiedVersion(), qualifier);
}
@@ -179,6 +181,16 @@ public class BuildQualifierMojo extends AbstractVersionMojo {
}
}
+ void validateQualifier(String qualifier) throws MojoFailureException {
+ // parse a valid version with the given qualifier to check if the qualifier is valid
+ try {
+ Version.parseVersion("1.0.0." + qualifier);
+ } catch (IllegalArgumentException e) {
+ throw new MojoFailureException(
+ "Invalid build qualifier, it does not match the OSGi qualifier constraint ([0..9]|[a..zA..Z]|'_'|'-')");
+ }
+ }
+
String getQualifier(Date timestamp) {
return format.format(timestamp);
}
diff --git a/tycho-packaging-plugin/src/test/java/org/eclipse/tycho/buildversion/BuildQualifierTest.java b/tycho-packaging-plugin/src/test/java/org/eclipse/tycho/buildversion/BuildQualifierTest.java
index 383ddcc52..80c659b58 100644
--- a/tycho-packaging-plugin/src/test/java/org/eclipse/tycho/buildversion/BuildQualifierTest.java
+++ b/tycho-packaging-plugin/src/test/java/org/eclipse/tycho/buildversion/BuildQualifierTest.java
@@ -10,7 +10,11 @@
*******************************************************************************/
package org.eclipse.tycho.buildversion;
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.junit.Assert.assertThat;
+
import java.io.File;
+import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@@ -44,22 +48,9 @@ public class BuildQualifierTest extends AbstractTychoMojoTestCase {
* property and setting forceContextQualifier using explicit mojo configuration.
*/
- File basedir = getBasedir("projects/buildqualifier");
-
- File pom = new File(basedir, "p001/pom.xml");
-
- MavenExecutionRequest request = newMavenExecutionRequest(pom);
- request.getProjectBuildingRequest().setProcessPlugins(false);
-
- MavenProject project = getProject(request);
+ MavenProject project = getProject("projects/buildqualifier", "p001/pom.xml");
project.getProperties().put(BUILD_QUALIFIER_PROPERTY, "garbage");
-
- ArrayList<MavenProject> projects = new ArrayList<MavenProject>();
- projects.add(project);
-
- MavenSession session = new MavenSession(getContainer(), request, null, projects);
-
- BuildQualifierMojo mojo = getMojo(project, session);
+ BuildQualifierMojo mojo = createMojoWithProject(project);
setVariableValueToObject(mojo, "forceContextQualifier", "foo-bar");
@@ -69,22 +60,9 @@ public class BuildQualifierTest extends AbstractTychoMojoTestCase {
}
public void testBuildProperties() throws Exception {
- File basedir = getBasedir("projects/buildqualifier");
-
- File pom = new File(basedir, "p002/pom.xml");
-
- MavenExecutionRequest request = newMavenExecutionRequest(pom);
- request.getProjectBuildingRequest().setProcessPlugins(false);
-
- MavenProject project = getProject(request);
+ MavenProject project = getProject("projects/buildqualifier", "p002/pom.xml");
project.getProperties().put(BUILD_QUALIFIER_PROPERTY, "garbage");
-
- ArrayList<MavenProject> projects = new ArrayList<MavenProject>();
- projects.add(project);
-
- MavenSession session = new MavenSession(getContainer(), request, null, projects);
-
- BuildQualifierMojo mojo = getMojo(project, session);
+ BuildQualifierMojo mojo = createMojoWithProject(project);
mojo.execute();
@@ -92,22 +70,14 @@ public class BuildQualifierTest extends AbstractTychoMojoTestCase {
}
public void testTimestamp() throws Exception {
- File basedir = getBasedir("projects/buildqualifier");
- File pom = new File(basedir, "p001/pom.xml");
-
- MavenExecutionRequest request = newMavenExecutionRequest(pom);
- request.getProjectBuildingRequest().setProcessPlugins(false);
-
- MavenProject project = getProject(request);
+ MavenProject project = getProject("projects/buildqualifier", "p001/pom.xml");
project.getProperties().put(BUILD_QUALIFIER_PROPERTY, "garbage");
ArrayList<MavenProject> projects = new ArrayList<MavenProject>();
projects.add(project);
- MavenSession session = new MavenSession(getContainer(), request, null, projects);
-
- BuildQualifierMojo mojo = getMojo(project, session);
+ BuildQualifierMojo mojo = createMojoWithProject(project);
mojo.execute();
@@ -116,28 +86,17 @@ public class BuildQualifierTest extends AbstractTychoMojoTestCase {
// lets do it again
Thread.sleep(500L);
- project = getProject(request);
+ project = getProject("projects/buildqualifier", "p001/pom.xml");
assertNull(project.getProperties().get(BUILD_QUALIFIER_PROPERTY));
- mojo = getMojo(project, session);
+ mojo = createMojoWithProject(project);
mojo.execute();
assertEquals(firstTimestamp, project.getProperties().get(BUILD_QUALIFIER_PROPERTY));
}
public void testUnqualifiedVersion() throws Exception {
- File basedir = getBasedir("projects/buildqualifier");
- File pom = new File(basedir, "p002/pom.xml");
-
- MavenExecutionRequest request = newMavenExecutionRequest(pom);
- request.getProjectBuildingRequest().setProcessPlugins(false);
-
- MavenProject project = getProject(request);
-
- ArrayList<MavenProject> projects = new ArrayList<MavenProject>();
- projects.add(project);
- MavenSession session = new MavenSession(getContainer(), request, null, projects);
-
- BuildQualifierMojo mojo = getMojo(project, session);
+ MavenProject project = getProject("projects/buildqualifier", "p002/pom.xml");
+ BuildQualifierMojo mojo = createMojoWithProject(project);
mojo.execute();
@@ -145,19 +104,8 @@ public class BuildQualifierTest extends AbstractTychoMojoTestCase {
}
public void testFullyQualifiedVersion() throws Exception {
- File basedir = getBasedir("projects/buildqualifier/fullyqualified");
- File pom = new File(basedir, "pom.xml");
-
- MavenExecutionRequest request = newMavenExecutionRequest(pom);
- request.getProjectBuildingRequest().setProcessPlugins(false);
-
- MavenProject project = getProject(request);
-
- ArrayList<MavenProject> projects = new ArrayList<MavenProject>();
- projects.add(project);
- MavenSession session = new MavenSession(getContainer(), request, null, projects);
-
- BuildQualifierMojo mojo = getMojo(project, session);
+ MavenProject project = getProject("projects/buildqualifier/fullyqualified", "pom.xml");
+ BuildQualifierMojo mojo = createMojoWithProject(project);
mojo.execute();
@@ -167,19 +115,8 @@ public class BuildQualifierTest extends AbstractTychoMojoTestCase {
}
public void testNoQualifiedVersion() throws Exception {
- File basedir = getBasedir("projects/buildqualifier/noqualifier");
- File pom = new File(basedir, "pom.xml");
-
- MavenExecutionRequest request = newMavenExecutionRequest(pom);
- request.getProjectBuildingRequest().setProcessPlugins(false);
-
- MavenProject project = getProject(request);
-
- ArrayList<MavenProject> projects = new ArrayList<MavenProject>();
- projects.add(project);
- MavenSession session = new MavenSession(getContainer(), request, null, projects);
-
- BuildQualifierMojo mojo = getMojo(project, session);
+ MavenProject project = getProject("projects/buildqualifier/noqualifier", "pom.xml");
+ BuildQualifierMojo mojo = createMojoWithProject(project);
mojo.execute();
@@ -265,6 +202,38 @@ public class BuildQualifierTest extends AbstractTychoMojoTestCase {
assertQualifier("201206180600", projects, "attachedfeature");
}
+ public void testWithInvalidQualifierFormat() throws Exception {
+ MavenProject project = getProject("projects/buildqualifier", "p001/pom.xml");
+ BuildQualifierMojo mojo = createMojoWithProject(project);
+ mojo.setFormat("yyyyMMdd HHmm");
+ try {
+ mojo.execute();
+ fail();
+ } catch (MojoFailureException e) {
+ assertThat(e.getMessage(), containsString("Invalid build qualifier"));
+ }
+ }
+
+ public void testWithInvalidForcedQualifier() throws Exception {
+ MavenProject project = getProject("projects/buildqualifier", "p001/pom.xml");
+ BuildQualifierMojo mojo = createMojoWithProject(project);
+ setVariableValueToObject(mojo, "forceContextQualifier", "invalid:Qualifier");
+ try {
+ mojo.execute();
+ fail();
+ } catch (MojoFailureException e) {
+ assertThat(e.getMessage(), containsString("Invalid build qualifier"));
+ }
+ }
+
+ private BuildQualifierMojo createMojoWithProject(MavenProject project) throws IOException, Exception {
+ ArrayList<MavenProject> projects = new ArrayList<MavenProject>();
+ projects.add(project);
+ MavenSession session = newMavenSession(projects.get(0), projects);
+ BuildQualifierMojo mojo = getMojo(project, session);
+ return mojo;
+ }
+
private void assertQualifier(String expected, List<MavenProject> projects, String artifactId) {
MavenProject project = getProject(projects, artifactId);
assertEquals(expected, project.getProperties().getProperty(BUILD_QUALIFIER_PROPERTY));
@@ -288,7 +257,11 @@ public class BuildQualifierTest extends AbstractTychoMojoTestCase {
return mojo.getQualifier(date);
}
- private MavenProject getProject(MavenExecutionRequest request) throws Exception {
+ private MavenProject getProject(String baseDir, String pom) throws Exception {
+ File basedirFile = getBasedir(baseDir);
+ File pomFile = new File(basedirFile, pom);
+ MavenExecutionRequest request = newMavenExecutionRequest(pomFile);
+ request.getProjectBuildingRequest().setProcessPlugins(false);
MavenExecutionResult result = maven.execute(request);
return result.getProject();
}

Back to the top