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