1 package dst.ass2.aop.tests;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotSame;
5 import static org.junit.Assert.assertTrue;
7 import java.io.IOException;
10 import dst.ass2.aop.IPluginExecutor;
11 import dst.ass2.aop.PluginExecutorFactory;
12 import dst.ass2.aop.event.Event;
13 import dst.ass2.aop.event.EventBus;
14 import dst.ass2.aop.event.EventType;
15 import dst.ass2.aop.util.PluginUtils;
16 import org.apache.commons.io.FileUtils;
19 public class Ass2_4_1Test {
20 static final String SIMPLE_PLUGIN = "dst.ass2.aop.sample.SimplePluginExecutable";
21 IPluginExecutor executor;
22 EventBus eventBus = EventBus.getInstance();
25 public static void beforeClass() {
26 Assert.assertEquals("Cannot create temporary plugin directory: " + PluginUtils.PLUGINS_DIR.getAbsolutePath(),
27 true, PluginUtils.PLUGINS_DIR.isDirectory() || PluginUtils.PLUGINS_DIR.mkdirs());
31 public static void afterClass() throws IOException {
32 FileUtils.forceDeleteOnExit(PluginUtils.PLUGINS_DIR);
36 public void before() {
37 PluginUtils.cleanPluginDirectory();
38 executor = PluginExecutorFactory.createPluginExecutor();
39 executor.monitor(PluginUtils.PLUGINS_DIR);
48 PluginUtils.cleanPluginDirectory();
52 * Executing plugin copied to plugin directory.
54 @Test(timeout = PluginUtils.PLUGIN_TEST_TIMEOUT)
55 public void copiedPlugin_isExecutedCorrectly() throws Exception {
56 // Preparing new plugin
57 PluginUtils.preparePlugin(PluginUtils.SIMPLE_FILE);
59 // Periodically check for the plugin to be executed
60 while (eventBus.size() != 2) {
64 // Verify that the plugin was started and stopped orderly
65 assertTrue(SIMPLE_PLUGIN + " was not started properly.", eventBus.has(SIMPLE_PLUGIN, EventType.PLUGIN_START));
66 assertTrue(SIMPLE_PLUGIN + " did not finish properly.", eventBus.has(SIMPLE_PLUGIN, EventType.PLUGIN_END));
70 * Checking that each plugin JAR uses its own ClassLoader.
72 @Test(timeout = PluginUtils.PLUGIN_TEST_TIMEOUT)
73 public void samePlugins_useSeparateClassLoaders() throws Exception {
74 // Preparing two plugins
75 PluginUtils.preparePlugin(PluginUtils.SIMPLE_FILE);
76 PluginUtils.preparePlugin(PluginUtils.SIMPLE_FILE);
78 // Periodically check for the plugins to be executed
79 while (eventBus.size() != 4) {
84 * Verify that the plugins were loaded by different classloaders.
85 * This can be checked by comparing the ClassLoaders or comparing the classes themselves.
86 * In other words, if a class is loaded by two different ClassLoaders, it holds that
87 * a.getClass() != b.getClass() even if the byte code is identical.
89 List<Event> events = eventBus.getEvents(EventType.PLUGIN_START);
90 String msg = "Both plugins where loaded by the same ClassLoader";
91 assertNotSame(msg, events.get(0).getPluginClass().getClassLoader(), events.get(1).getPluginClass().getClassLoader());
92 assertNotSame(msg, events.get(0).getPluginClass(), events.get(1).getPluginClass());
96 * Checking whether two plugins in a single JAR are executed concurrently.
98 @Test(timeout = PluginUtils.PLUGIN_TEST_TIMEOUT)
99 public void allPlugins_executeConcurrently() throws Exception {
100 // Start a plugin containing two IPluginExecutable classes
101 PluginUtils.preparePlugin(PluginUtils.ALL_FILE);
103 // Periodically check for the plugins to be executed
104 while (eventBus.size() != 4) {
108 // Check that there is exactly one start and end event each
109 List<Event> starts = eventBus.getEvents(EventType.PLUGIN_START);
110 List<Event> ends = eventBus.getEvents(EventType.PLUGIN_END);
111 assertEquals("EventBus must contain exactly 2 start events.", 2, starts.size());
112 assertEquals("EventBus must contain exactly 2 end events.", 2, ends.size());
114 // Verify that the plugins were started concurrently
115 String msg = "All plugins should have been started before the first ended - %d was after %d.";
116 for (Event end : ends) {
117 for (Event start : starts) {
118 assertTrue(String.format(msg, start.getTime(), end.getTime()), start.getTime() < end.getTime());
124 * Checking whether two plugin JARs are executed concurrently.
126 @Test(timeout = PluginUtils.PLUGIN_TEST_TIMEOUT)
127 public void multiplePlugins_executeConcurrently() throws Exception {
128 // Start two plugins at once
129 PluginUtils.preparePlugin(PluginUtils.SIMPLE_FILE);
130 PluginUtils.preparePlugin(PluginUtils.SIMPLE_FILE);
132 // Periodically check for the plugins to be executed
133 while (eventBus.size() != 4) {
137 // Check that there is exactly one start and end event each
138 List<Event> starts = eventBus.getEvents(EventType.PLUGIN_START);
139 List<Event> ends = eventBus.getEvents(EventType.PLUGIN_END);
140 assertEquals("EventBus must contain exactly 2 start events.", 2, starts.size());
141 assertEquals("EventBus must contain exactly 2 end events.", 2, ends.size());
143 // Verify that the plugins were started concurrently.
144 String msg = "All plugins should have been started before the first ended - %d was after %d.";
145 for (Event end : ends) {
146 for (Event start : starts) {
147 assertTrue(String.format(msg, start.getTime(), end.getTime()), start.getTime() < end.getTime());