1 package it.com.atlassian.plugin.osgi.performance;
2
3 import com.atlassian.plugin.DefaultModuleDescriptorFactory;
4 import com.atlassian.plugin.Plugin;
5 import com.atlassian.plugin.hostcontainer.DefaultHostContainer;
6 import com.atlassian.plugin.osgi.hostcomponents.ComponentRegistrar;
7 import com.atlassian.plugin.osgi.hostcomponents.HostComponentProvider;
8 import com.atlassian.plugin.osgi.PluginInContainerTestBase;
9 import com.atlassian.plugin.osgi.DummyModuleDescriptor;
10 import com.atlassian.plugin.osgi.SomeInterface;
11 import com.atlassian.plugin.test.PluginJarBuilder;
12
13 import org.apache.commons.io.FileUtils;
14 import org.osgi.framework.Bundle;
15 import org.osgi.framework.Constants;
16
17 import java.io.IOException;
18 import java.io.File;
19 import java.util.concurrent.ThreadPoolExecutor;
20 import java.util.concurrent.Executors;
21 import java.util.concurrent.ExecutorService;
22 import java.util.concurrent.TimeUnit;
23
24
25
26
27 public abstract class FrameworkRestartTestBase extends PluginInContainerTestBase
28 {
29 private static final int NUM_HOST_COMPONENTS = 200;
30 private static final int NUM_PLUGINS = 50;
31 HostComponentProvider prov = null;
32 DefaultModuleDescriptorFactory factory = null;
33
34 @Override
35 public void setUp() throws Exception
36 {
37 super.setUp();
38 factory = new DefaultModuleDescriptorFactory(new DefaultHostContainer());
39 factory.addModuleDescriptor("dummy", DummyModuleDescriptor.class);
40 prov = new HostComponentProvider()
41 {
42 public void provide(final ComponentRegistrar registrar)
43 {
44 for (int x = 0; x < NUM_HOST_COMPONENTS; x++)
45 {
46 registrar.register(SomeInterface.class).forInstance(new SomeInterface()
47 {});
48 }
49 }
50 };
51
52 ExecutorService executor = Executors.newFixedThreadPool(8);
53 for (int x = 0; x < NUM_PLUGINS; x++)
54 {
55 final int run = x;
56 executor.execute(new Runnable()
57 {
58 public void run()
59 {
60 try
61 {
62 addPlugin(pluginsDir, run);
63 }
64 catch (Exception e)
65 {
66 e.printStackTrace();
67 }
68 }
69 });
70
71 }
72 executor.shutdown();
73 executor.awaitTermination(300, TimeUnit.SECONDS);
74
75 initPluginManager(prov, factory);
76 pluginManager.shutdown();
77 }
78
79 protected abstract void addPlugin(File dir, int x) throws Exception;
80
81 protected void startPluginFramework() throws Exception
82 {
83 initPluginManager(prov, factory);
84 assertEquals(pluginManager.getPlugins().size(), pluginManager.getEnabledPlugins().size());
85
86 for (Bundle bundle : osgiContainerManager.getBundles())
87 {
88 final boolean isFragment = (bundle.getHeaders().get(Constants.FRAGMENT_HOST) != null);
89
90 if (isFragment)
91 {
92 assertEquals("Fragment Bundle " + bundle.getSymbolicName() + " was not resolved: " + bundle.getState(), Bundle.RESOLVED, bundle.getState());
93 }
94 else
95 {
96 assertEquals("Bundle " + bundle.getSymbolicName() + " was not active: " + bundle.getState(), Bundle.ACTIVE, bundle.getState());
97 }
98 }
99 }
100
101 @Override
102 public void tearDown() throws Exception
103 {
104 super.tearDown();
105 }
106
107 public void testMultiplePlugins() throws Exception
108 {
109 startPluginFramework();
110 pluginManager.shutdown();
111 }
112 }