From 69910b5777985f2983bcb1955d371a7b64a51502 Mon Sep 17 00:00:00 2001 From: Benjamin Bentmann Date: Sat, 11 Oct 2008 16:57:50 +0000 Subject: [PATCH] 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 --- .../dep-a/pom.xml | 44 ++++ .../apache/maven/plugin/coreit/ClassA.java | 36 +++ .../apache/maven/plugin/coreit/SomeClass.java | 37 +++ .../apache/maven/plugin/coreit/a.properties | 2 + .../apache/maven/plugin/coreit/it.properties | 3 + .../dep-b/pom.xml | 53 ++++ .../apache/maven/plugin/coreit/ClassB.java | 36 +++ .../apache/maven/plugin/coreit/SomeClass.java | 37 +++ .../apache/maven/plugin/coreit/b.properties | 2 + .../apache/maven/plugin/coreit/it.properties | 3 + .../maven-it-plugin-class-loader/pom.xml | 59 +++++ .../apache/maven/plugin/coreit/LoadMojo.java | 227 ++++++++++++++++++ .../maven-it-plugin-class-loader/pom.xml | 41 ++++ its/core-it-support/core-it-plugins/pom.xml | 3 +- 14 files changed, 582 insertions(+), 1 deletion(-) create mode 100644 its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/dep-a/pom.xml create mode 100644 its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/dep-a/src/main/java/org/apache/maven/plugin/coreit/ClassA.java create mode 100644 its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/dep-a/src/main/java/org/apache/maven/plugin/coreit/SomeClass.java create mode 100644 its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/dep-a/src/main/resources/org/apache/maven/plugin/coreit/a.properties create mode 100644 its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/dep-a/src/main/resources/org/apache/maven/plugin/coreit/it.properties create mode 100644 its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/dep-b/pom.xml create mode 100644 its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/dep-b/src/main/java/org/apache/maven/plugin/coreit/ClassB.java create mode 100644 its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/dep-b/src/main/java/org/apache/maven/plugin/coreit/SomeClass.java create mode 100644 its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/dep-b/src/main/resources/org/apache/maven/plugin/coreit/b.properties create mode 100644 its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/dep-b/src/main/resources/org/apache/maven/plugin/coreit/it.properties create mode 100644 its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/maven-it-plugin-class-loader/pom.xml create mode 100644 its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/maven-it-plugin-class-loader/src/main/java/org/apache/maven/plugin/coreit/LoadMojo.java create mode 100644 its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/pom.xml diff --git a/its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/dep-a/pom.xml b/its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/dep-a/pom.xml new file mode 100644 index 0000000000..2c8a049027 --- /dev/null +++ b/its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/dep-a/pom.xml @@ -0,0 +1,44 @@ + + + + + + 4.0.0 + + + maven-it-plugins + org.apache.maven.its.plugins + 2.1-SNAPSHOT + + + org.apache.maven.its.plugins.class-loader + dep-a + jar + + Maven Integration Test Plugin :: Class Loader :: Dependency A + + A test dependency for the test plugin. + + 2008 + + + true + + diff --git a/its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/dep-a/src/main/java/org/apache/maven/plugin/coreit/ClassA.java b/its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/dep-a/src/main/java/org/apache/maven/plugin/coreit/ClassA.java new file mode 100644 index 0000000000..c262cc1272 --- /dev/null +++ b/its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/dep-a/src/main/java/org/apache/maven/plugin/coreit/ClassA.java @@ -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"; + } + +} diff --git a/its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/dep-a/src/main/java/org/apache/maven/plugin/coreit/SomeClass.java b/its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/dep-a/src/main/java/org/apache/maven/plugin/coreit/SomeClass.java new file mode 100644 index 0000000000..4b30f4a8cf --- /dev/null +++ b/its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/dep-a/src/main/java/org/apache/maven/plugin/coreit/SomeClass.java @@ -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 not 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"; + } + +} diff --git a/its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/dep-a/src/main/resources/org/apache/maven/plugin/coreit/a.properties b/its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/dep-a/src/main/resources/org/apache/maven/plugin/coreit/a.properties new file mode 100644 index 0000000000..894463b674 --- /dev/null +++ b/its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/dep-a/src/main/resources/org/apache/maven/plugin/coreit/a.properties @@ -0,0 +1,2 @@ +# A resource file that is unique to this artifact. +key = A diff --git a/its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/dep-a/src/main/resources/org/apache/maven/plugin/coreit/it.properties b/its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/dep-a/src/main/resources/org/apache/maven/plugin/coreit/it.properties new file mode 100644 index 0000000000..9c639391ef --- /dev/null +++ b/its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/dep-a/src/main/resources/org/apache/maven/plugin/coreit/it.properties @@ -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 diff --git a/its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/dep-b/pom.xml b/its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/dep-b/pom.xml new file mode 100644 index 0000000000..175e95b6bc --- /dev/null +++ b/its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/dep-b/pom.xml @@ -0,0 +1,53 @@ + + + + + + 4.0.0 + + + maven-it-plugins + org.apache.maven.its.plugins + 2.1-SNAPSHOT + + + org.apache.maven.its.plugins.class-loader + dep-b + jar + + Maven Integration Test Plugin :: Class Loader :: Dependency B + + A test dependency for the test plugin. This artifact has a dependency by itself to test transitive dependency + resolution. + + 2008 + + + true + + + + + ${project.groupId} + dep-a + ${project.version} + + + diff --git a/its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/dep-b/src/main/java/org/apache/maven/plugin/coreit/ClassB.java b/its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/dep-b/src/main/java/org/apache/maven/plugin/coreit/ClassB.java new file mode 100644 index 0000000000..4d0531fc66 --- /dev/null +++ b/its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/dep-b/src/main/java/org/apache/maven/plugin/coreit/ClassB.java @@ -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"; + } + +} diff --git a/its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/dep-b/src/main/java/org/apache/maven/plugin/coreit/SomeClass.java b/its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/dep-b/src/main/java/org/apache/maven/plugin/coreit/SomeClass.java new file mode 100644 index 0000000000..5dcdadac77 --- /dev/null +++ b/its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/dep-b/src/main/java/org/apache/maven/plugin/coreit/SomeClass.java @@ -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 not 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"; + } + +} diff --git a/its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/dep-b/src/main/resources/org/apache/maven/plugin/coreit/b.properties b/its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/dep-b/src/main/resources/org/apache/maven/plugin/coreit/b.properties new file mode 100644 index 0000000000..1181286331 --- /dev/null +++ b/its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/dep-b/src/main/resources/org/apache/maven/plugin/coreit/b.properties @@ -0,0 +1,2 @@ +# A resource file that is unique to this artifact. +key = B diff --git a/its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/dep-b/src/main/resources/org/apache/maven/plugin/coreit/it.properties b/its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/dep-b/src/main/resources/org/apache/maven/plugin/coreit/it.properties new file mode 100644 index 0000000000..93bbdc91a2 --- /dev/null +++ b/its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/dep-b/src/main/resources/org/apache/maven/plugin/coreit/it.properties @@ -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 diff --git a/its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/maven-it-plugin-class-loader/pom.xml b/its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/maven-it-plugin-class-loader/pom.xml new file mode 100644 index 0000000000..46a61a4186 --- /dev/null +++ b/its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/maven-it-plugin-class-loader/pom.xml @@ -0,0 +1,59 @@ + + + + + + 4.0.0 + + + maven-it-plugins + org.apache.maven.its.plugins + 2.1-SNAPSHOT + + + maven-it-plugin-class-loader + maven-plugin + + Maven Integration Test Plugin :: Class Loader + + A test plugin that tries to load classes and/or resources via the plugin class loader or the thread's context class + loader. + + 2008 + + + true + + + + + org.apache.maven + maven-plugin-api + 2.0 + + + + ${project.groupId}.class-loader + dep-b + ${project.version} + runtime + + + diff --git a/its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/maven-it-plugin-class-loader/src/main/java/org/apache/maven/plugin/coreit/LoadMojo.java b/its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/maven-it-plugin-class-loader/src/main/java/org/apache/maven/plugin/coreit/LoadMojo.java new file mode 100644 index 0000000000..3268288924 --- /dev/null +++ b/its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/maven-it-plugin-class-loader/src/main/java/org/apache/maven/plugin/coreit/LoadMojo.java @@ -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 QCN that was + * successfully loaded, the generated properties files will contain a key named QCN. The value of this + * key will be the hash code of the requested class. In addition, a key named QCN.methods 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 ARP that was + * successfully loaded, the generated properties files will contain a key named ARP whose value gives + * the URL to the resource. In addition, the keys ARP.count, ARP.0, ARP.1 + * 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 null. + * @param classLoader The class loader to use, must not be null. + * @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 ); + } + +} diff --git a/its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/pom.xml b/its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/pom.xml new file mode 100644 index 0000000000..01e035f0c1 --- /dev/null +++ b/its/core-it-support/core-it-plugins/maven-it-plugin-class-loader/pom.xml @@ -0,0 +1,41 @@ + + + + + + 4.0.0 + + + maven-it-plugins + org.apache.maven.its.plugins + 2.1-SNAPSHOT + + + maven-it-plugin-class-loader-aggregator + pom + + Maven Integration Test Plugin :: Class Loader :: Aggregator + + + maven-it-plugin-class-loader + dep-a + dep-b + + diff --git a/its/core-it-support/core-it-plugins/pom.xml b/its/core-it-support/core-it-plugins/pom.xml index 7c0ba86c2d..ba435ff410 100644 --- a/its/core-it-support/core-it-plugins/pom.xml +++ b/its/core-it-support/core-it-plugins/pom.xml @@ -36,6 +36,7 @@ under the License. maven-it-plugin-artifact + maven-it-plugin-class-loader maven-it-plugin-configuration maven-it-plugin-context-passing maven-it-plugin-core-stubs @@ -62,7 +63,7 @@ under the License. scm:svn:https://svn.apache.org/repos/asf/maven/core-integration-testing/trunk/core-it-support/core-it-plugins http://svn.apache.org/viewvc/maven/core-integration-testing/trunk/core-it-support/core-it-plugins - +