MPLUGINTESTING-33 better test plugin artifact file heuristics
Signed-off-by: Igor Fedorenko <ifedorenko@apache.org>
diff --git a/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/AbstractMojoTestCase.java b/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/AbstractMojoTestCase.java
index 3de20a3..7219bf3 100644
--- a/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/AbstractMojoTestCase.java
+++ b/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/AbstractMojoTestCase.java
@@ -22,10 +22,13 @@
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
+import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
+import java.net.MalformedURLException;
+import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
@@ -124,7 +127,8 @@
lookup( RepositorySystem.class ).createArtifact( pluginDescriptor.getGroupId(),
pluginDescriptor.getArtifactId(),
pluginDescriptor.getVersion(), ".jar" );
- artifact.setFile( new File( getBasedir() ).getCanonicalFile() );
+
+ artifact.setFile( getPluginArtifactFile() );
pluginDescriptor.setPluginArtifact( artifact );
pluginDescriptor.setArtifacts( Arrays.asList( artifact ) );
@@ -140,6 +144,63 @@
}
}
+ /**
+ * Returns best-effort plugin artifact file.
+ * <p>
+ * First, attempts to determine parent directory of META-INF directory holding the plugin descriptor. If META-INF
+ * parent directory cannot be determined, falls back to test basedir.
+ */
+ private File getPluginArtifactFile()
+ throws IOException
+ {
+ final String pluginDescriptorLocation = getPluginDescriptorLocation();
+ final URL resource = getClass().getResource( "/" + pluginDescriptorLocation );
+
+ File file = null;
+
+ // attempt to resolve relative to META-INF/maven/plugin.xml first
+ if ( resource != null )
+ {
+ if ( "file".equalsIgnoreCase( resource.getProtocol() ) )
+ {
+ String path = resource.getPath();
+ if ( path.endsWith( pluginDescriptorLocation ) )
+ {
+ file = new File( path.substring( 0, path.length() - pluginDescriptorLocation.length() ) );
+ }
+ }
+ else if ( "jar".equalsIgnoreCase( resource.getProtocol() ) )
+ {
+ // TODO is there a helper for this somewhere?
+ try
+ {
+ URL jarfile = new URL( resource.getPath() );
+ if ( "file".equalsIgnoreCase( jarfile.getProtocol() ) )
+ {
+ String path = jarfile.getPath();
+ if ( path.endsWith( pluginDescriptorLocation ) )
+ {
+ file =
+ new File( path.substring( 0, path.length() - pluginDescriptorLocation.length() - 2 ) );
+ }
+ }
+ }
+ catch ( MalformedURLException e )
+ {
+ // not jar:file:/ URL, too bad
+ }
+ }
+ }
+
+ // fallback to test project basedir if couldn't resolve relative to META-INF/maven/plugin.xml
+ if ( file == null || ! file.exists() )
+ {
+ file = new File( getBasedir() );
+ }
+
+ return file.getCanonicalFile();
+ }
+
protected InputStream getPublicDescriptorStream()
throws Exception
{
diff --git a/maven-plugin-testing-harness/src/test/java/org/apache/maven/plugin/testing/PluginArtifactFileTest.java b/maven-plugin-testing-harness/src/test/java/org/apache/maven/plugin/testing/PluginArtifactFileTest.java
new file mode 100644
index 0000000..c94fb35
--- /dev/null
+++ b/maven-plugin-testing-harness/src/test/java/org/apache/maven/plugin/testing/PluginArtifactFileTest.java
@@ -0,0 +1,47 @@
+package org.apache.maven.plugin.testing;
+
+/*
+ * 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.
+ */
+
+import java.util.List;
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.plugin.MojoExecution;
+
+public class PluginArtifactFileTest
+ extends AbstractMojoTestCase
+{
+ public void testArtifact()
+ throws Exception
+ {
+ MojoExecution execution = newMojoExecution( "parameters" ); // TODO dedicated test mojo
+
+ List<Artifact> artifacts = execution.getMojoDescriptor().getPluginDescriptor().getArtifacts();
+
+ assertEquals( 1, artifacts.size() );
+
+ Artifact artifact = artifacts.get( 0 );
+ assertEquals( "test", artifact.getGroupId() );
+ assertEquals( "test-plugin", artifact.getArtifactId() );
+ assertEquals( "0.0.1-SNAPSHOT", artifact.getBaseVersion() );
+ assertTrue( artifact.getFile().getAbsolutePath().endsWith( "target/test-classes" ) );
+ }
+
+ // TODO find a way to automate testing of jar:file:/ test plugin URLs
+}