From b50dd4a69ed3a1f6012eb9848e42a89b1e5e5c3b Mon Sep 17 00:00:00 2001 From: mindthegab Date: Sun, 22 Mar 2009 10:26:17 +0000 Subject: [PATCH] -- added testing with maven-plugin-test-harness git-svn-id: http://maven-alfresco-archetypes.googlecode.com/svn/trunk@157 04253f4f-3451-0410-a141-5562f1e59037 --- plugins/maven-nosnapshot-plugin/pom.xml | 18 ++- .../maven/NoSnapshotVersionMojo.java | 65 ----------- .../java/com/sourcesense/maven/StripMojo.java | 97 ++++++++++++++++ .../maven/plugins/test/StripTest.java | 104 ++++++++++++++++++ .../src/test/resources/customprop-pom.xml | 16 +++ .../src/test/resources/relcandidate-pom.xml | 16 +++ .../src/test/resources/release-pom.xml | 16 +++ .../src/test/resources/snapshot-pom.xml | 15 +++ .../src/test/resources/underscore-pom.xml | 16 +++ 9 files changed, 296 insertions(+), 67 deletions(-) delete mode 100644 plugins/maven-nosnapshot-plugin/src/main/java/com/sourcesense/maven/NoSnapshotVersionMojo.java create mode 100644 plugins/maven-nosnapshot-plugin/src/main/java/com/sourcesense/maven/StripMojo.java create mode 100644 plugins/maven-nosnapshot-plugin/src/test/java/com/sourcesense/maven/plugins/test/StripTest.java create mode 100644 plugins/maven-nosnapshot-plugin/src/test/resources/customprop-pom.xml create mode 100644 plugins/maven-nosnapshot-plugin/src/test/resources/relcandidate-pom.xml create mode 100644 plugins/maven-nosnapshot-plugin/src/test/resources/release-pom.xml create mode 100644 plugins/maven-nosnapshot-plugin/src/test/resources/snapshot-pom.xml create mode 100644 plugins/maven-nosnapshot-plugin/src/test/resources/underscore-pom.xml diff --git a/plugins/maven-nosnapshot-plugin/pom.xml b/plugins/maven-nosnapshot-plugin/pom.xml index 55574120..c1b8608c 100644 --- a/plugins/maven-nosnapshot-plugin/pom.xml +++ b/plugins/maven-nosnapshot-plugin/pom.xml @@ -1,10 +1,11 @@ - + 4.0.0 com.sourcesense.maven maven-nosnapshot-plugin maven-plugin 0.0.4-SNAPSHOT - maven-nosnapshot-plugin Maven Mojo + Custom version manipulator Maven Plugin. It's non invasive and puts modified versions in configurable pom properties http://maven.apache.org scm:svn:https://maven-alfresco-archetypes.googlecode.com/svn/trunk/plugins/maven-nosnapshot-plugin @@ -27,9 +28,22 @@ maven-project 2.0.9 + + org.apache.maven.shared + maven-plugin-testing-harness + 1.0-beta-1 + test + + + maven-compiler-plugin + + 1.5 + 1.5 + + org.apache.maven.plugins maven-release-plugin diff --git a/plugins/maven-nosnapshot-plugin/src/main/java/com/sourcesense/maven/NoSnapshotVersionMojo.java b/plugins/maven-nosnapshot-plugin/src/main/java/com/sourcesense/maven/NoSnapshotVersionMojo.java deleted file mode 100644 index 97be2342..00000000 --- a/plugins/maven-nosnapshot-plugin/src/main/java/com/sourcesense/maven/NoSnapshotVersionMojo.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.sourcesense.maven; - -/* - * Copyright 2001-2005 The Apache Software Foundation. - * - * Licensed 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. - */ - -import java.text.MessageFormat; -import java.util.regex.Pattern; - -import org.apache.maven.plugin.AbstractMojo; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.project.MavenProject; - -/** - * Goal which pushes a stripped version number in the maven project properties (any -SNAPSHOT suffix is removed) - * - * @goal strip - * @phase initialize - * - */ -public class NoSnapshotVersionMojo - extends AbstractMojo -{ - /** - * Current version of the project - * @parameter expression="${project.version}" - * @required - */ - private String version; - - /** - * The Maven project - * @parameter expression="${project}" - * @required - */ - private MavenProject project; - - public void execute() - throws MojoExecutionException - { - int indexOfDash = version.indexOf("-"); - - String noSnapshotVersion = version; - if(Pattern.matches("[a-zA-Z]", version)) - { - noSnapshotVersion = version.substring(0, indexOfDash); - } - getLog().info( - MessageFormat.format( "Storing noSnapshotVersion: {0} ", new Object[] { - noSnapshotVersion} ) ); - project.getProperties().put("noSnapshotVersion", noSnapshotVersion); - } -} diff --git a/plugins/maven-nosnapshot-plugin/src/main/java/com/sourcesense/maven/StripMojo.java b/plugins/maven-nosnapshot-plugin/src/main/java/com/sourcesense/maven/StripMojo.java new file mode 100644 index 00000000..dcb2e6df --- /dev/null +++ b/plugins/maven-nosnapshot-plugin/src/main/java/com/sourcesense/maven/StripMojo.java @@ -0,0 +1,97 @@ +package com.sourcesense.maven; + +/* + * Copyright 2001-2005 The Apache Software Foundation. + * + * Licensed 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. + */ + +import java.text.MessageFormat; + +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.project.MavenProject; + +/** + * Goal which pushes a stripped version number in the maven project properties, + * removing the suffix after the configured separator into the project variable + * noSnapshotVersion + * + * @goal strip + * @phase initialize + * + */ +public class StripMojo extends AbstractMojo { + private static final String RESULT_PROPERTY_NAME = "noSnapshotVersion"; + + /** + * Current version of the project + * + * @parameter expression="${project.version}" default-value="3.0.0-SNAPSHOT" + * @required + */ + private String version; + + public MavenProject getProject() { + return project; + } + + /** + * The Maven project + * + * @parameter expression="${project}" + * @required + */ + private MavenProject project; + + /** + * The separator used to identify and strip the suffix + * + * @parameter expression="${separator}" default-value="-" + * @required + */ + private String separator; + + /** + * The Maven Projec property the stripped version is pushed into + * + * @parameter expression="${propertyName}" default-value="noSnapshotVersion" + * @required + */ + private String propertyName; + + + public void execute() throws MojoExecutionException { + int separatorIndex = version.indexOf(separator); + String noSnapshotVersion = version; + if (separatorIndex > -1) { + noSnapshotVersion = version.substring(0, separatorIndex); + } + getLog().info( + MessageFormat.format("Storing " + propertyName + + ": {0} ", new Object[] { noSnapshotVersion })); + project.getProperties().put(propertyName, noSnapshotVersion); + } + + public String getPropertyName() { + return propertyName; + } + + public void setPropertyName(String propertyName) { + this.propertyName = propertyName; + } + + public void setSeparator(String separator) { + this.separator = separator; + } +} diff --git a/plugins/maven-nosnapshot-plugin/src/test/java/com/sourcesense/maven/plugins/test/StripTest.java b/plugins/maven-nosnapshot-plugin/src/test/java/com/sourcesense/maven/plugins/test/StripTest.java new file mode 100644 index 00000000..20c9f588 --- /dev/null +++ b/plugins/maven-nosnapshot-plugin/src/test/java/com/sourcesense/maven/plugins/test/StripTest.java @@ -0,0 +1,104 @@ +package com.sourcesense.maven.plugins.test; + + +import java.io.File; + +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.project.MavenProject; + +import com.sourcesense.maven.StripMojo; + +public class StripTest extends org.apache.maven.plugin.testing.AbstractMojoTestCase { + + File testPom; + MavenProject project = new MavenProject(); + + protected void setUp() throws Exception { + // required for mojo lookups to work + super.setUp(); + + } + + /** + * tests the proper discovery and configuration of the mojo + * + * @throws Exception + */ + public void testMojoDefaultEnvironment() throws Exception { + testPom = new File( getBasedir(), "target/test-classes/snapshot-pom.xml" ); + StripMojo mojo = (StripMojo) lookupMojo ("strip", testPom ); + assertNotNull( mojo ); + + } + + public void testSnapshotVersion() throws Exception { + testPom = new File( getBasedir(), "target/test-classes/snapshot-pom.xml" ); + StripMojo mojo = (StripMojo) lookupMojo ("strip", testPom ); + setVariableValueToObject(mojo, "version", "3.0.0-SNAPSHOT"); + setVariableValueToObject(mojo, "project", project); + try { + mojo.execute(); + } catch (MojoExecutionException e) { + fail("Mojo execution exception" + e); + } + assertEquals("3.0.0", mojo.getProject().getProperties().getProperty( + mojo.getPropertyName())); + } + + public void testReleaseCandidateVersion() throws Exception { + testPom = new File( getBasedir(), "target/test-classes/relcandidate-pom.xml" ); + StripMojo mojo = (StripMojo) lookupMojo ("strip", testPom ); + setVariableValueToObject(mojo, "version", "3.0.0-RC1"); + setVariableValueToObject(mojo, "project", project); + try { + mojo.execute(); + } catch (MojoExecutionException e) { + fail("Mojo execution exception" + e); + } + assertEquals("3.0.0", mojo.getProject().getProperties().getProperty( + mojo.getPropertyName())); + } + + public void testIdempotent() throws Exception { + testPom = new File( getBasedir(), "target/test-classes/snapshot-pom.xml" ); + StripMojo mojo = (StripMojo) lookupMojo ("strip", testPom ); + setVariableValueToObject(mojo, "version", "3.0.0"); + setVariableValueToObject(mojo, "project", project); + try { + mojo.execute(); + } catch (MojoExecutionException e) { + fail("Mojo execution exception" + e); + } + assertEquals("3.0.0", mojo.getProject().getProperties().getProperty( + mojo.getPropertyName())); + } + + public void testUnderscore() throws Exception { + testPom = new File( getBasedir(), "target/test-classes/underscore-pom.xml" ); + StripMojo mojo = (StripMojo) lookupMojo ("strip", testPom ); + setVariableValueToObject(mojo, "version", "3.0.0_RC1"); + setVariableValueToObject(mojo, "project", project); + try { + mojo.execute(); + } catch (MojoExecutionException e) { + fail("Mojo execution exception" + e); + } + assertEquals("3.0.0", mojo.getProject().getProperties().getProperty( + mojo.getPropertyName())); + } + + public void testCustomPropName() throws Exception { + testPom = new File( getBasedir(), "target/test-classes/customprop-pom.xml" ); + StripMojo mojo = (StripMojo) lookupMojo ("strip", testPom ); + setVariableValueToObject(mojo, "version", "3.0.0-RC1"); + setVariableValueToObject(mojo, "project", project); + try { + mojo.execute(); + } catch (MojoExecutionException e) { + fail("Mojo execution exception" + e); + } + assertEquals("3.0.0", mojo.getProject().getProperties().getProperty( + "foobar")); + } + +} diff --git a/plugins/maven-nosnapshot-plugin/src/test/resources/customprop-pom.xml b/plugins/maven-nosnapshot-plugin/src/test/resources/customprop-pom.xml new file mode 100644 index 00000000..a19d1c8f --- /dev/null +++ b/plugins/maven-nosnapshot-plugin/src/test/resources/customprop-pom.xml @@ -0,0 +1,16 @@ + + + + + maven-nosnapshot-plugin + + + - + foobar + + + + + + + \ No newline at end of file diff --git a/plugins/maven-nosnapshot-plugin/src/test/resources/relcandidate-pom.xml b/plugins/maven-nosnapshot-plugin/src/test/resources/relcandidate-pom.xml new file mode 100644 index 00000000..0822cd88 --- /dev/null +++ b/plugins/maven-nosnapshot-plugin/src/test/resources/relcandidate-pom.xml @@ -0,0 +1,16 @@ + + + + + maven-nosnapshot-plugin + + + - + noSnapshotVersion + + + + + + + \ No newline at end of file diff --git a/plugins/maven-nosnapshot-plugin/src/test/resources/release-pom.xml b/plugins/maven-nosnapshot-plugin/src/test/resources/release-pom.xml new file mode 100644 index 00000000..0822cd88 --- /dev/null +++ b/plugins/maven-nosnapshot-plugin/src/test/resources/release-pom.xml @@ -0,0 +1,16 @@ + + + + + maven-nosnapshot-plugin + + + - + noSnapshotVersion + + + + + + + \ No newline at end of file diff --git a/plugins/maven-nosnapshot-plugin/src/test/resources/snapshot-pom.xml b/plugins/maven-nosnapshot-plugin/src/test/resources/snapshot-pom.xml new file mode 100644 index 00000000..13b3851f --- /dev/null +++ b/plugins/maven-nosnapshot-plugin/src/test/resources/snapshot-pom.xml @@ -0,0 +1,15 @@ + + + + + maven-nosnapshot-plugin + + - + noSnapshotVersion + + + + + + + \ No newline at end of file diff --git a/plugins/maven-nosnapshot-plugin/src/test/resources/underscore-pom.xml b/plugins/maven-nosnapshot-plugin/src/test/resources/underscore-pom.xml new file mode 100644 index 00000000..0436b22b --- /dev/null +++ b/plugins/maven-nosnapshot-plugin/src/test/resources/underscore-pom.xml @@ -0,0 +1,16 @@ + + + + + maven-nosnapshot-plugin + + + _ + noSnapshotVersion + + + + + + + \ No newline at end of file