mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-16 18:26:16 +01:00
added unit test for PluginProcessor
This commit is contained in:
@@ -27,28 +27,380 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.plugin;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.io.Resources;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
import java.net.URL;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class PluginProcessorTest
|
||||
{
|
||||
|
||||
|
||||
/** Field description */
|
||||
private static final PluginResource PLUGIN_A =
|
||||
new PluginResource("sonia/scm/plugin/scm-a-plugin.smp", "scm-a-plugin.smp",
|
||||
"sonia.scm.plugins:scm-a-plugin:1.0.0-SNAPSHOT");
|
||||
|
||||
/** Field description */
|
||||
private static final PluginResource PLUGIN_B =
|
||||
new PluginResource("sonia/scm/plugin/scm-b-plugin.smp", "scm-b-plugin.smp",
|
||||
"sonia.scm.plugins:scm-b-plugin:1.0.0-SNAPSHOT");
|
||||
|
||||
/** Field description */
|
||||
private static final PluginResource PLUGIN_C =
|
||||
new PluginResource("sonia/scm/plugin/scm-c-plugin.smp", "scm-c-plugin.smp",
|
||||
"sonia.scm.plugins:scm-c-plugin:1.0.0-SNAPSHOT");
|
||||
|
||||
/** Field description */
|
||||
private static final PluginResource PLUGIN_D =
|
||||
new PluginResource("sonia/scm/plugin/scm-d-plugin.smp", "scm-d-plugin.smp",
|
||||
"sonia.scm.plugins:scm-d-plugin:1.0.0-SNAPSHOT");
|
||||
|
||||
/** Field description */
|
||||
private static final PluginResource PLUGIN_E =
|
||||
new PluginResource("sonia/scm/plugin/scm-e-plugin.smp", "scm-e-plugin.smp",
|
||||
"sonia.scm.plugins:scm-e-plugin:1.0.0-SNAPSHOT");
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Test(expected = PluginCircularDependencyException.class)
|
||||
public void testCircularDependencies() throws IOException
|
||||
{
|
||||
copySmps(PLUGIN_C, PLUGIN_D, PLUGIN_E);
|
||||
collectPlugins();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Test
|
||||
public void testCollectPlugins() throws IOException
|
||||
{
|
||||
copySmp(PLUGIN_A);
|
||||
|
||||
PluginWrapper plugin = collectAndGetFirst();
|
||||
|
||||
assertThat(plugin.getId(), is(PLUGIN_A.id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Test
|
||||
public void testCollectPluginsWithDependencies() throws IOException
|
||||
{
|
||||
copySmps(PLUGIN_A, PLUGIN_B);
|
||||
|
||||
Set<PluginWrapper> plugins = collectPlugins();
|
||||
|
||||
assertThat(plugins, hasSize(2));
|
||||
|
||||
PluginWrapper a = findPlugin(plugins, PLUGIN_A.id);
|
||||
|
||||
assertNotNull(a);
|
||||
|
||||
PluginWrapper b = findPlugin(plugins, PLUGIN_B.id);
|
||||
|
||||
assertNotNull(b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @throws ClassNotFoundException
|
||||
* @throws IOException
|
||||
* @throws IllegalAccessException
|
||||
* @throws IllegalArgumentException
|
||||
* @throws InstantiationException
|
||||
* @throws InvocationTargetException
|
||||
* @throws NoSuchMethodException
|
||||
*/
|
||||
@Test
|
||||
public void testPluginClassLoader()
|
||||
throws IOException, ClassNotFoundException, InstantiationException,
|
||||
IllegalAccessException, NoSuchMethodException, IllegalArgumentException,
|
||||
InvocationTargetException
|
||||
{
|
||||
copySmp(PLUGIN_A);
|
||||
|
||||
PluginWrapper plugin = collectAndGetFirst();
|
||||
ClassLoader cl = plugin.getClassLoader();
|
||||
|
||||
// load parent class
|
||||
Class<?> clazz = cl.loadClass(PluginResource.class.getName());
|
||||
|
||||
assertSame(PluginResource.class, clazz);
|
||||
|
||||
// load packaged class
|
||||
clazz = cl.loadClass("sonia.scm.plugins.HelloService");
|
||||
assertNotNull(clazz);
|
||||
|
||||
Object instance = clazz.newInstance();
|
||||
Object result = clazz.getMethod("sayHello").invoke(instance);
|
||||
|
||||
assertEquals("hello", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @throws ClassNotFoundException
|
||||
* @throws IOException
|
||||
* @throws IllegalAccessException
|
||||
* @throws IllegalArgumentException
|
||||
* @throws InstantiationException
|
||||
* @throws InvocationTargetException
|
||||
* @throws NoSuchMethodException
|
||||
*/
|
||||
@Test
|
||||
public void testPluginClassLoaderWithDependencies()
|
||||
throws IOException, ClassNotFoundException, InstantiationException,
|
||||
IllegalAccessException, NoSuchMethodException, IllegalArgumentException,
|
||||
InvocationTargetException
|
||||
{
|
||||
copySmps(PLUGIN_A, PLUGIN_B);
|
||||
|
||||
Set<PluginWrapper> plugins = collectPlugins();
|
||||
|
||||
PluginWrapper plugin = findPlugin(plugins, PLUGIN_B.id);
|
||||
ClassLoader cl = plugin.getClassLoader();
|
||||
|
||||
// load parent class
|
||||
Class<?> clazz = cl.loadClass(PluginResource.class.getName());
|
||||
|
||||
assertSame(PluginResource.class, clazz);
|
||||
|
||||
// load packaged class
|
||||
clazz = cl.loadClass("sonia.scm.plugins.HelloAgainService");
|
||||
assertNotNull(clazz);
|
||||
|
||||
Object instance = clazz.newInstance();
|
||||
Object result = clazz.getMethod("sayHelloAgain").invoke(instance);
|
||||
|
||||
assertEquals("hello again", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Test
|
||||
public void testPluginWebResourceLoader() throws IOException
|
||||
{
|
||||
copySmp(PLUGIN_A);
|
||||
|
||||
PluginWrapper plugin = collectAndGetFirst();
|
||||
WebResourceLoader wrl = plugin.getWebResourceLoader();
|
||||
|
||||
assertNotNull(wrl);
|
||||
|
||||
URL url = wrl.getResource("hello");
|
||||
|
||||
assertNotNull(url);
|
||||
assertThat(Resources.toString(url, Charsets.UTF_8), is("hello"));
|
||||
}
|
||||
|
||||
//~--- set methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Before
|
||||
public void setUp() throws IOException
|
||||
{
|
||||
pluginDirectory = temp.newFolder();
|
||||
processor = new PluginProcessor(pluginDirectory.toPath());
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
private PluginWrapper collectAndGetFirst() throws IOException
|
||||
{
|
||||
Set<PluginWrapper> plugins = collectPlugins();
|
||||
|
||||
assertThat(plugins, hasSize(1));
|
||||
|
||||
return Iterables.get(plugins, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
private Set<PluginWrapper> collectPlugins() throws IOException
|
||||
{
|
||||
return processor.collectPlugins(PluginProcessorTest.class.getClassLoader());
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param plugin
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
private void copySmp(PluginResource plugin) throws IOException
|
||||
{
|
||||
URL resource = Resources.getResource(plugin.path);
|
||||
File file = new File(pluginDirectory, plugin.name);
|
||||
|
||||
try (OutputStream out = new FileOutputStream(file))
|
||||
{
|
||||
Resources.copy(resource, out);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param plugins
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
private void copySmps(PluginResource... plugins) throws IOException
|
||||
{
|
||||
for (PluginResource plugin : plugins)
|
||||
{
|
||||
copySmp(plugin);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param plugin
|
||||
* @param id
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private PluginWrapper findPlugin(Iterable<PluginWrapper> plugin,
|
||||
final String id)
|
||||
{
|
||||
return Iterables.find(plugin, new Predicate<PluginWrapper>()
|
||||
{
|
||||
|
||||
@Override
|
||||
public boolean apply(PluginWrapper input)
|
||||
{
|
||||
return id.equals(input.getId());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
//~--- inner classes --------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Class description
|
||||
*
|
||||
*
|
||||
* @version Enter version here..., 14/12/06
|
||||
* @author Enter your name here...
|
||||
*/
|
||||
private static class PluginResource
|
||||
{
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
* @param path
|
||||
* @param name
|
||||
* @param id
|
||||
*/
|
||||
public PluginResource(String path, String name, String id)
|
||||
{
|
||||
this.path = path;
|
||||
this.name = name;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
//~--- fields -------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private final String id;
|
||||
|
||||
/** Field description */
|
||||
private final String name;
|
||||
|
||||
/** Field description */
|
||||
private final String path;
|
||||
}
|
||||
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
@Rule
|
||||
public TemporaryFolder temp = new TemporaryFolder();
|
||||
|
||||
@Test
|
||||
public void testBuildPluginTree() throws IOException
|
||||
{
|
||||
PluginProcessor processor = new PluginProcessor(temp.newFolder().toPath());
|
||||
}
|
||||
|
||||
/** Field description */
|
||||
private File pluginDirectory;
|
||||
|
||||
/** Field description */
|
||||
private PluginProcessor processor;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user