Commit b50dd4a6 authored by mindthegab's avatar mindthegab
Browse files

-- added testing with maven-plugin-test-harness

git-svn-id: http://maven-alfresco-archetypes.googlecode.com/svn/trunk@157 04253f4f-3451-0410-a141-5562f1e59037
parent 576a3e56
Loading
Loading
Loading
Loading
+16 −2
Original line number Diff line number Diff line
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.sourcesense.maven</groupId>
	<artifactId>maven-nosnapshot-plugin</artifactId>
	<packaging>maven-plugin</packaging>
	<version>0.0.4-SNAPSHOT</version>
	<name>maven-nosnapshot-plugin Maven Mojo</name>
	<name>Custom version manipulator Maven Plugin. It's non invasive and puts modified versions in configurable pom properties</name>
	<url>http://maven.apache.org</url>
	<scm>
		<developerConnection>scm:svn:https://maven-alfresco-archetypes.googlecode.com/svn/trunk/plugins/maven-nosnapshot-plugin</developerConnection>
@@ -27,9 +28,22 @@
			<artifactId>maven-project</artifactId>
			<version>2.0.9</version>
		</dependency>
		<dependency>
			<groupId>org.apache.maven.shared</groupId>
			<artifactId>maven-plugin-testing-harness</artifactId>
			<version>1.0-beta-1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.5</source>
					<target>1.5</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-release-plugin</artifactId>
+48 −16
Original line number Diff line number Diff line
@@ -17,49 +17,81 @@ package com.sourcesense.maven;
 */

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 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 NoSnapshotVersionMojo
    extends AbstractMojo
{
public class StripMojo extends AbstractMojo {
	private static final String RESULT_PROPERTY_NAME = "noSnapshotVersion";
	
	/**
	 * Current version of the project
     * @parameter expression="${project.version}"
	 * 
	 * @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;

    public void execute()
        throws MojoExecutionException
    {
    	int indexOfDash = version.indexOf("-");
	/**
	 * 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(Pattern.matches("[a-zA-Z]", version))
    	{
    		noSnapshotVersion = version.substring(0, indexOfDash);
		if (separatorIndex > -1) {
			noSnapshotVersion = version.substring(0, separatorIndex);
		}
		getLog().info(
                MessageFormat.format( "Storing noSnapshotVersion: {0} ", new Object[] {
                    noSnapshotVersion} ) );
    	project.getProperties().put("noSnapshotVersion", noSnapshotVersion);
				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;
	}
}
+104 −0
Original line number Diff line number Diff line
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"));
	}

}
+16 −0
Original line number Diff line number Diff line
<project>
	<build>
		<plugins>
			<plugin>
				<artifactId>maven-nosnapshot-plugin
				</artifactId>
				<configuration>
					<separator>-</separator>
					<propertyName>foobar</propertyName>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<properties>
	</properties>
</project>
 No newline at end of file
+16 −0
Original line number Diff line number Diff line
<project>
	<build>
		<plugins>
			<plugin>
				<artifactId>maven-nosnapshot-plugin
				</artifactId>
				<configuration>
					<separator>-</separator>
					<propertyName>noSnapshotVersion</propertyName>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<properties>
	</properties>
</project>
 No newline at end of file
Loading