o Added new IT plugin to check class/resource loading in mojos

git-svn-id: https://svn.apache.org/repos/asf/maven/core-integration-testing/trunk@703683 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benjamin Bentmann 2008-10-11 16:57:50 +00:00
parent ed1f399793
commit 69910b5777
14 changed files with 582 additions and 1 deletions

View File

@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<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>
<parent>
<artifactId>maven-it-plugins</artifactId>
<groupId>org.apache.maven.its.plugins</groupId>
<version>2.1-SNAPSHOT</version>
</parent>
<groupId>org.apache.maven.its.plugins.class-loader</groupId>
<artifactId>dep-a</artifactId>
<packaging>jar</packaging>
<name>Maven Integration Test Plugin :: Class Loader :: Dependency A</name>
<description>
A test dependency for the test plugin.
</description>
<inceptionYear>2008</inceptionYear>
<properties>
<maven.test.skip>true</maven.test.skip>
</properties>
</project>

View File

@ -0,0 +1,36 @@
package org.apache.maven.plugin.coreit;
/*
* 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.
*/
/**
* A test class that is unique to this artifact.
*
* @author Benjamin Bentmann
* @version $Id$
*/
public class ClassA
{
public static String methodA()
{
return "A";
}
}

View File

@ -0,0 +1,37 @@
package org.apache.maven.plugin.coreit;
/*
* 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.
*/
/**
* A test class that is <em>not</em> unique to this artifact. Other artifacts will deliberately contain an equally named
* class (with different members) to check class path ordering of dependencies.
*
* @author Benjamin Bentmann
* @version $Id$
*/
public class SomeClass
{
public static String methodA()
{
return "A";
}
}

View File

@ -0,0 +1,2 @@
# A resource file that is unique to this artifact.
key = A

View File

@ -0,0 +1,3 @@
# A resource file that is *not* unique to this artifact but deliberately provided by other artifacts as well to test
# class path ordering of dependencies.
key = A

View File

@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<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>
<parent>
<artifactId>maven-it-plugins</artifactId>
<groupId>org.apache.maven.its.plugins</groupId>
<version>2.1-SNAPSHOT</version>
</parent>
<groupId>org.apache.maven.its.plugins.class-loader</groupId>
<artifactId>dep-b</artifactId>
<packaging>jar</packaging>
<name>Maven Integration Test Plugin :: Class Loader :: Dependency B</name>
<description>
A test dependency for the test plugin. This artifact has a dependency by itself to test transitive dependency
resolution.
</description>
<inceptionYear>2008</inceptionYear>
<properties>
<maven.test.skip>true</maven.test.skip>
</properties>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>dep-a</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,36 @@
package org.apache.maven.plugin.coreit;
/*
* 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.
*/
/**
* A test class that is unique to this artifact.
*
* @author Benjamin Bentmann
* @version $Id$
*/
public class ClassB
{
public static String methodB()
{
return "B";
}
}

View File

@ -0,0 +1,37 @@
package org.apache.maven.plugin.coreit;
/*
* 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.
*/
/**
* A test class that is <em>not</em> unique to this artifact. Other artifacts will deliberately contain an equally named
* class (with different members) to check class path ordering of dependencies.
*
* @author Benjamin Bentmann
* @version $Id$
*/
public class SomeClass
{
public static String methodB()
{
return "B";
}
}

View File

@ -0,0 +1,2 @@
# A resource file that is unique to this artifact.
key = B

View File

@ -0,0 +1,3 @@
# A resource file that is *not* unique to this artifact but deliberately provided by other artifacts as well to test
# class path ordering of dependencies.
key = B

View File

@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<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>
<parent>
<artifactId>maven-it-plugins</artifactId>
<groupId>org.apache.maven.its.plugins</groupId>
<version>2.1-SNAPSHOT</version>
</parent>
<artifactId>maven-it-plugin-class-loader</artifactId>
<packaging>maven-plugin</packaging>
<name>Maven Integration Test Plugin :: Class Loader</name>
<description>
A test plugin that tries to load classes and/or resources via the plugin class loader or the thread's context class
loader.
</description>
<inceptionYear>2008</inceptionYear>
<properties>
<maven.test.skip>true</maven.test.skip>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>2.0</version>
</dependency>
<!-- dedicated IT artifact that is surely not shadowed by the Maven core -->
<dependency>
<groupId>${project.groupId}.class-loader</groupId>
<artifactId>dep-b</artifactId>
<version>${project.version}</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,227 @@
package org.apache.maven.plugin.coreit;
/*
* 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 org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
/**
* Loads classes and/or resources from the plugin class path and records the results in a properties file.
*
* @goal load
* @phase initialize
*
* @author Benjamin Bentmann
* @version $Id$
*/
public class LoadMojo
extends AbstractMojo
{
/**
* The path to the properties file used to track the results of the class/resource loading via the plugin class
* loader.
*
* @parameter expression="${clsldr.pluginClassLoaderOutput}"
*/
private File pluginClassLoaderOutput;
/**
* The path to the properties file used to track the results of the class/resource loading via the thread's context
* class loader.
*
* @parameter expression="${clsldr.contextClassLoaderOutput}"
*/
private File contextClassLoaderOutput;
/**
* The comma separated set of classes to load. For each specified qualified class name <code>QCN</code> that was
* successfully loaded, the generated properties files will contain a key named <code>QCN</code>. The value of this
* key will be the hash code of the requested class. In addition, a key named <code>QCN.methods</code> holds the
* comma separated list of all public methods declared directly in that class, in alphabetic order and possibly with
* duplicates to account for overloaded methods.
*
* @parameter expression="${clsldr.classNames}"
*/
private String classNames;
/**
* The comma separated set of resources to load. For each specified absolute resource path <code>ARP</code> that was
* successfully loaded, the generated properties files will contain a key named <code>ARP</code> whose value gives
* the URL to the resource. In addition, the keys <code>ARP.count</code>, <code>ARP.0</code>, <code>ARP.1</code>
* etc. will enumerate all URLs matching the resource name.
*
* @parameter expression="${clsldr.resourcePaths}"
*/
private String resourcePaths;
/**
* Runs this mojo.
*
* @throws MojoExecutionException If the output file could not be created.
*/
public void execute()
throws MojoExecutionException, MojoFailureException
{
if ( pluginClassLoaderOutput != null )
{
execute( pluginClassLoaderOutput, getClass().getClassLoader() );
}
if ( contextClassLoaderOutput != null )
{
execute( contextClassLoaderOutput, Thread.currentThread().getContextClassLoader() );
}
}
/**
* Loads the classes/resources.
*
* @param outputFile The path to the properties file to generate, must not be <code>null</code>.
* @param classLoader The class loader to use, must not be <code>null</code>.
* @throws MojoExecutionException If the output file could not be created.
*/
private void execute( File outputFile, ClassLoader classLoader )
throws MojoExecutionException
{
getLog().info( "[MAVEN-CORE-IT-LOG] Using class loader " + classLoader );
Properties loaderProperties = new Properties();
if ( classNames != null && classNames.length() > 0 )
{
String[] names = classNames.split( "," );
for ( int i = 0; i < names.length; i++ )
{
String name = names[i];
getLog().info( "[MAVEN-CORE-IT-LOG] Loading class " + name );
// test ClassLoader.loadClass()
try
{
Class type = classLoader.loadClass( name );
loaderProperties.setProperty( name, "" + type.hashCode() );
Method[] methods = type.getDeclaredMethods();
List methodNames = new ArrayList();
for ( int j = 0; j < methods.length; j++ )
{
if ( Modifier.isPublic( methods[j].getModifiers() ) )
{
methodNames.add( methods[j].getName() );
}
}
Collections.sort( methodNames );
StringBuffer buffer = new StringBuffer( 1024 );
for ( Iterator it = methodNames.iterator(); it.hasNext(); )
{
if ( buffer.length() > 0 )
{
buffer.append( ',' );
}
buffer.append( it.next() );
}
loaderProperties.setProperty( name + ".methods", buffer.toString() );
}
catch ( ClassNotFoundException e )
{
// ignore, will be reported by means of missing keys in the properties file
}
}
}
if ( resourcePaths != null && resourcePaths.length() > 0 )
{
String[] paths = resourcePaths.split( "," );
for ( int i = 0; i < paths.length; i++ )
{
String path = paths[i];
getLog().info( "[MAVEN-CORE-IT-LOG] Loading resource " + path );
// test ClassLoader.getResource()
URL url = classLoader.getResource( path );
if ( url != null )
{
loaderProperties.setProperty( path, url.toString() );
}
// test ClassLoader.getResources()
try
{
List urls = Collections.list( classLoader.getResources( path ) );
loaderProperties.setProperty( path + ".count", "" + urls.size() );
for ( int j = 0; j < urls.size(); j++ )
{
loaderProperties.setProperty( path + "." + j, urls.get( j ).toString() );
}
}
catch ( IOException e )
{
throw new MojoExecutionException( "Resources could not be enumerated: " + path, e );
}
}
}
getLog().info( "[MAVEN-CORE-IT-LOG] Creating output file " + outputFile );
OutputStream out = null;
try
{
outputFile.getParentFile().mkdirs();
out = new FileOutputStream( outputFile );
loaderProperties.store( out, "MAVEN-CORE-IT-LOG" );
}
catch ( IOException e )
{
throw new MojoExecutionException( "Output file could not be created: " + outputFile, e );
}
finally
{
if ( out != null )
{
try
{
out.close();
}
catch ( IOException e )
{
// just ignore
}
}
}
getLog().info( "[MAVEN-CORE-IT-LOG] Created output file " + outputFile );
}
}

View File

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<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>
<parent>
<artifactId>maven-it-plugins</artifactId>
<groupId>org.apache.maven.its.plugins</groupId>
<version>2.1-SNAPSHOT</version>
</parent>
<artifactId>maven-it-plugin-class-loader-aggregator</artifactId>
<packaging>pom</packaging>
<name>Maven Integration Test Plugin :: Class Loader :: Aggregator</name>
<modules>
<module>maven-it-plugin-class-loader</module>
<module>dep-a</module>
<module>dep-b</module>
</modules>
</project>

View File

@ -36,6 +36,7 @@ under the License.
<modules>
<module>maven-it-plugin-artifact</module>
<module>maven-it-plugin-class-loader</module>
<module>maven-it-plugin-configuration</module>
<module>maven-it-plugin-context-passing</module>
<module>maven-it-plugin-core-stubs</module>
@ -62,7 +63,7 @@ under the License.
<developerConnection>scm:svn:https://svn.apache.org/repos/asf/maven/core-integration-testing/trunk/core-it-support/core-it-plugins</developerConnection>
<url>http://svn.apache.org/viewvc/maven/core-integration-testing/trunk/core-it-support/core-it-plugins</url>
</scm>
<build>
<pluginManagement>
<plugins>