]> git.somenet.org - pub/jan/dst18.git/blob - ass2-aop/src/test/java/dst/ass2/aop/tests/Ass2_4_1Test.java
Add template for assignment 2
[pub/jan/dst18.git] / ass2-aop / src / test / java / dst / ass2 / aop / tests / Ass2_4_1Test.java
1 package dst.ass2.aop.tests;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotSame;
5 import static org.junit.Assert.assertTrue;
6
7 import java.io.IOException;
8 import java.util.List;
9
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;
17 import org.junit.*;
18
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();
23
24     @BeforeClass
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());
28     }
29
30     @AfterClass
31     public static void afterClass() throws IOException {
32         FileUtils.forceDeleteOnExit(PluginUtils.PLUGINS_DIR);
33     }
34
35     @Before
36     public void before() {
37         PluginUtils.cleanPluginDirectory();
38         executor = PluginExecutorFactory.createPluginExecutor();
39         executor.monitor(PluginUtils.PLUGINS_DIR);
40         executor.start();
41         eventBus.reset();
42     }
43
44     @After
45     public void after() {
46         executor.stop();
47         eventBus.reset();
48         PluginUtils.cleanPluginDirectory();
49     }
50
51     /**
52      * Executing plugin copied to plugin directory.
53      */
54     @Test(timeout = PluginUtils.PLUGIN_TEST_TIMEOUT)
55     public void copiedPlugin_isExecutedCorrectly() throws Exception {
56         // Preparing new plugin
57         PluginUtils.preparePlugin(PluginUtils.SIMPLE_FILE);
58
59         // Periodically check for the plugin to be executed
60         while (eventBus.size() != 2) {
61             Thread.sleep(100);
62         }
63
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));
67     }
68
69     /**
70      * Checking that each plugin JAR uses its own ClassLoader.
71      */
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);
77
78         // Periodically check for the plugins to be executed
79         while (eventBus.size() != 4) {
80             Thread.sleep(100);
81         }
82
83                 /*
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.
88                  */
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());
93     }
94
95     /**
96      * Checking whether two plugins in a single JAR are executed concurrently.
97      */
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);
102
103         // Periodically check for the plugins to be executed
104         while (eventBus.size() != 4) {
105             Thread.sleep(100);
106         }
107
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());
113
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());
119             }
120         }
121     }
122
123     /**
124      * Checking whether two plugin JARs are executed concurrently.
125      */
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);
131
132         // Periodically check for the plugins to be executed
133         while (eventBus.size() != 4) {
134             Thread.sleep(100);
135         }
136
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());
142
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());
148             }
149         }
150     }
151 }