Skip to content

Commit

Permalink
Filter out abstract plugin classes
Browse files Browse the repository at this point in the history
  • Loading branch information
wendigo authored and findepi committed Apr 1, 2020
1 parent 034979c commit 5348a85
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 9 deletions.
11 changes: 9 additions & 2 deletions src/main/java/io/prestosql/maven/ServiceDescriptorGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import java.util.jar.JarOutputStream;

import static java.lang.String.format;
import static java.lang.reflect.Modifier.isAbstract;
import static java.lang.reflect.Modifier.isInterface;
import static java.nio.charset.StandardCharsets.UTF_8;

/**
Expand Down Expand Up @@ -125,9 +127,9 @@ private List<Class<?>> findPluginImplementations(URLClassLoader searchRealm)
for (String classPath : classes) {
String className = classPath.substring(0, classPath.length() - 6).replace(File.separatorChar, '.');
try {
Class<?> implementation = searchRealm.loadClass(pluginClassName);
Class<?> pluginClass = searchRealm.loadClass(pluginClassName);
Class<?> clazz = searchRealm.loadClass(className);
if (implementation.isAssignableFrom(clazz)) {
if (isImplementation(clazz, pluginClass)) {
implementations.add(clazz);
}
}
Expand All @@ -147,4 +149,9 @@ private static void mkdirs(File file)
throw new MojoExecutionException(format("%n%nFailed to create directory: %s", file));
}
}

private static boolean isImplementation(Class<?> clazz, Class<?> pluginClass)
{
return pluginClass.isAssignableFrom(clazz) && !isAbstract(clazz.getModifiers()) && !isInterface(clazz.getModifiers());
}
}
20 changes: 20 additions & 0 deletions src/test/java/io/prestosql/maven/CheckerIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,26 @@ public void testBasic()
.assertErrorFreeLog();
}

@Test
public void testAbstractPluginClass()
throws Exception
{
File basedir = resources.getBasedir("abstract-plugin-class");
maven.forProject(basedir)
.execute("verify")
.assertErrorFreeLog();
}

@Test
public void testInterfacePluginClass()
throws Exception
{
File basedir = resources.getBasedir("interface-plugin-class");
maven.forProject(basedir)
.execute("verify")
.assertErrorFreeLog();
}

@Test
public void testInvalidExtraProvided()
throws Exception
Expand Down
35 changes: 28 additions & 7 deletions src/test/java/io/prestosql/maven/GeneratorIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import static java.lang.String.format;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Collections.list;
import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -44,12 +45,32 @@ public GeneratorIntegrationTest(MavenRuntimeBuilder mavenBuilder)
public void testBasic()
throws Exception
{
File basedir = resources.getBasedir("basic");
testProjectPackaging("basic", "its.BasicPlugin");
}

@Test
public void testAbstractPlugin()
throws Exception
{
testProjectPackaging("abstract-plugin-class", "its.TestPlugin");
}

@Test
public void testInterfacePlugin()
throws Exception
{
testProjectPackaging("interface-plugin-class", "its.TestPlugin");
}

protected void testProjectPackaging(String projectId, String expectedPluginClass)
throws Exception
{
File basedir = resources.getBasedir(projectId);
maven.forProject(basedir)
.execute("package")
.assertErrorFreeLog();

File mainJarFile = new File(basedir, "target/basic-1.0.jar");
File mainJarFile = new File(basedir, format("target/%s-1.0.jar", projectId));
assertThat(mainJarFile).isFile();

try (JarFile jar = new JarFile(mainJarFile)) {
Expand All @@ -58,26 +79,26 @@ public void testBasic()
.doesNotContain(DESCRIPTOR);
}

File servicesJarFile = new File(basedir, "target/basic-1.0-services.jar");
File servicesJarFile = new File(basedir, format("target/%s-1.0-services.jar", projectId));
assertThat(servicesJarFile).isFile();

try (JarFile jar = new JarFile(servicesJarFile)) {
JarEntry entry = jar.getJarEntry(DESCRIPTOR);
assertNotNull(entry);
try (InputStream in = jar.getInputStream(entry)) {
String contents = new String(toByteArray(in), UTF_8);
assertThat(contents).isEqualTo("its.BasicPlugin\n");
assertThat(contents).isEqualTo(expectedPluginClass + "\n");
}
}

File pluginZipFile = new File(basedir, "target/basic-1.0.zip");
File pluginZipFile = new File(basedir, format("target/%s-1.0.zip", projectId));
assertThat(pluginZipFile).isFile();

try (ZipFile zip = new ZipFile(pluginZipFile)) {
assertThat(list(zip.entries()))
.extracting(ZipEntry::getName)
.contains("basic-1.0/basic-1.0.jar")
.contains("basic-1.0/basic-1.0-services.jar");
.contains(format("%1$s-1.0/%1$s-1.0.jar", projectId))
.contains(format("%1$s-1.0/%1$s-1.0-services.jar", projectId));
}
}
}
33 changes: 33 additions & 0 deletions src/test/projects/abstract-plugin-class/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>io.prestosql.maven.its</groupId>
<artifactId>abstract-plugin-class</artifactId>
<version>1.0</version>
<packaging>presto-plugin</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>io.prestosql</groupId>
<artifactId>presto-spi</artifactId>
<version>300</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>io.prestosql</groupId>
<artifactId>presto-maven-plugin</artifactId>
<version>${it-plugin.version}</version>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package its;

import io.prestosql.spi.Plugin;

public abstract class AbstractPlugin
implements Plugin
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package its;

import io.prestosql.spi.Plugin;

public class TestPlugin
extends AbstractPlugin
{}
33 changes: 33 additions & 0 deletions src/test/projects/interface-plugin-class/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>io.prestosql.maven.its</groupId>
<artifactId>interface-plugin-class</artifactId>
<version>1.0</version>
<packaging>presto-plugin</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>io.prestosql</groupId>
<artifactId>presto-spi</artifactId>
<version>300</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>io.prestosql</groupId>
<artifactId>presto-maven-plugin</artifactId>
<version>${it-plugin.version}</version>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package its;

import io.prestosql.spi.Plugin;

public interface MyPlugin
extends Plugin {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package its;

public class TestPlugin
implements MyPlugin
{}

0 comments on commit 5348a85

Please sign in to comment.