@Test public void testPluginWithHostComponentUsingOldPackageImport() throws Exception { final PluginJarBuilder firstBuilder = new PluginJarBuilder("oldpkgfirst"); firstBuilder .addFormattedResource( "maera-plugin.xml", "<maera-plugin name='Test' key='first' pluginsVersion='2'>", " <plugin-info>", " <version>1.0</version>", " <bundle-instructions>", " <Export-Package>first</Export-Package>", " </bundle-instructions>", " </plugin-info>", " <servlet key='foo' class='second.MyServlet'>", " <url-pattern>/foo</url-pattern>", " </servlet>", "</maera-plugin>") .addFormattedJava("first.MyInterface", "package first;", "public interface MyInterface {}") .build(pluginsDir); new PluginJarBuilder("oldpkgsecond", firstBuilder.getClassLoader()) .addPluginInformation("second", "Some name", "1.0") .addFormattedJava( "second.MyImpl", "package second;", "public class MyImpl implements first.MyInterface {}") .build(pluginsDir); initPluginManager(); assertEquals(2, pluginManager.getEnabledPlugins().size()); assertNotNull(pluginManager.getPlugin("first")); assertNotNull(pluginManager.getPlugin("second")); }
@Test public void testPersistTimeoutSystemProperty() throws Exception { System.setProperty(PluginUtils.MAERA_PLUGINS_ENABLE_WAIT, "300"); try { final File propertiesFile = new File(cacheDir, ".properties"); assertFalse(propertiesFile.exists()); final PluginJarBuilder firstBuilder = new PluginJarBuilder("first"); final File jar = firstBuilder .addPluginInformation("first", "Some name", "1.0") .addFormattedJava( "first.MyInterface", "package first;", "public interface MyInterface {}") .addFormattedResource( "META-INF/MANIFEST.MF", "Manifest-Version: 1.0", "Bundle-SymbolicName: first", "Bundle-Version: 1.0", "Export-Package: first", "") .build(); initPluginManager(); pluginManager.installPlugin(new JarPluginArtifact(jar)); Bundle bundle = findBundleByName("first"); Dictionary headers = bundle.getHeaders(); assertEquals("*;timeout:=300", headers.get("Spring-Context")); assertTrue(propertiesFile.exists()); Properties properties = new Properties(); properties.load(new FileInputStream(propertiesFile)); assertEquals("300", properties.getProperty("spring.timeout")); final File testFile = new File(new File(cacheDir, "transformed-plugins"), ".test"); assertTrue(testFile.createNewFile()); assertTrue(testFile.exists()); initPluginManager(); pluginManager.installPlugin(new JarPluginArtifact(jar)); bundle = findBundleByName("first"); headers = bundle.getHeaders(); assertEquals("*;timeout:=300", headers.get("Spring-Context")); assertTrue(propertiesFile.exists()); assertTrue(testFile.exists()); } finally { System.clearProperty(PluginUtils.MAERA_PLUGINS_ENABLE_WAIT); } }
@Test public void testInstallPluginTakesTooLong() throws Exception { System.setProperty(PluginUtils.MAERA_PLUGINS_ENABLE_WAIT, "3"); try { final PluginJarBuilder builder = new PluginJarBuilder("first") .addFormattedResource( "maera-plugin.xml", "<maera-plugin name='Test' key='test.plugin' pluginsVersion='2'>", " <plugin-info>", " <version>1.0</version>", " </plugin-info>", " <component key='svc' class='my.ServiceImpl' public='true'>", " <interface>my.Service</interface>", " </component>", "</maera-plugin>") .addFormattedJava( "my.Service", "package my;", "public interface Service {", " public Object call() throws Exception;", "}") .addFormattedJava( "my.ServiceImpl", "package my;", "public class ServiceImpl implements Service {", "public ServiceImpl(){", "try{", "Thread.sleep(10000);", "}catch(Exception e){}", "}", " public Object call() throws Exception { ", " return 'hi';}", "}"); final File jar = builder.build(); initPluginManager(); pluginManager.installPlugin(new JarPluginArtifact(jar)); assertEquals(0, pluginManager.getEnabledPlugins().size()); } finally { System.clearProperty(PluginUtils.MAERA_PLUGINS_ENABLE_WAIT); } }
@Test public void testDeleteTimeoutFileWhenNoSystemPropertySpecified() throws Exception { System.setProperty(PluginUtils.MAERA_PLUGINS_ENABLE_WAIT, "300"); try { final File propertiesFile = new File(cacheDir, ".properties"); assertFalse(propertiesFile.exists()); final PluginJarBuilder firstBuilder = new PluginJarBuilder("first"); final File jar = firstBuilder .addPluginInformation("first", "Some name", "1.0") .addFormattedJava( "first.MyInterface", "package first;", "public interface MyInterface {}") .addFormattedResource( "META-INF/MANIFEST.MF", "Manifest-Version: 1.0", "Bundle-SymbolicName: first", "Bundle-Version: 1.0", "Export-Package: first", "Spring-Context: *;create-asynchrously:=false", "") .build(); initPluginManager(); pluginManager.installPlugin(new JarPluginArtifact(jar)); assertTrue(propertiesFile.exists()); System.clearProperty(PluginUtils.MAERA_PLUGINS_ENABLE_WAIT); initPluginManager(); pluginManager.installPlugin(new JarPluginArtifact(jar)); assertFalse(propertiesFile.exists()); Bundle bundle = findBundleByName("first"); Dictionary headers = bundle.getHeaders(); assertEquals("*;create-asynchrously:=false", headers.get("Spring-Context")); } finally { System.clearProperty(PluginUtils.MAERA_PLUGINS_ENABLE_WAIT); } }
@Test public void testPluginWithServletDependentOnPackageImport() throws Exception { final PluginJarBuilder firstBuilder = new PluginJarBuilder("first"); firstBuilder .addPluginInformation("first", "Some name", "1.0") .addFormattedJava("first.MyInterface", "package first;", "public interface MyInterface {}") .addFormattedResource( "META-INF/MANIFEST.MF", "Manifest-Version: 1.0", "Bundle-SymbolicName: first", "Bundle-Version: 1.0", "Export-Package: first", "") .build(pluginsDir); new PluginJarBuilder("asecond", firstBuilder.getClassLoader()) .addFormattedResource( "maera-plugin.xml", "<maera-plugin name='Test' key='asecond' pluginsVersion='2'>", " <plugin-info>", " <version>1.0</version>", " </plugin-info>", " <servlet key='foo' class='second.MyServlet'>", " <url-pattern>/foo</url-pattern>", " </servlet>", "</maera-plugin>") .addFormattedJava( "second.MyServlet", "package second;", "public class MyServlet extends javax.servlet.http.HttpServlet implements first.MyInterface {}") .build(pluginsDir); initPluginManager( null, new SingleModuleDescriptorFactory( new DefaultHostContainer(), "servlet", StubServletModuleDescriptor.class)); assertEquals(2, pluginManager.getEnabledPlugins().size()); assertTrue(pluginManager.getPlugin("first").getPluginState() == PluginState.ENABLED); assertNotNull(pluginManager.getPlugin("asecond").getPluginState() == PluginState.ENABLED); }
@Test public void testPluginWithServletRefreshedAfterOtherPluginUpgraded() throws Exception { final PluginJarBuilder firstBuilder = new PluginJarBuilder("first"); firstBuilder .addPluginInformation("first", "Some name", "1.0") .addFormattedJava("first.MyInterface", "package first;", "public interface MyInterface {}") .addFormattedResource( "META-INF/MANIFEST.MF", "Manifest-Version: 1.0", "Bundle-SymbolicName: first", "Bundle-Version: 1.0", "Export-Package: first", "") .build(pluginsDir); new PluginJarBuilder("asecond", firstBuilder.getClassLoader()) .addFormattedResource( "maera-plugin.xml", "<maera-plugin name='Test' key='asecond' pluginsVersion='2'>", " <plugin-info>", " <version>1.0</version>", " </plugin-info>", " <servlet key='foo' class='second.MyServlet'>", " <url-pattern>/foo</url-pattern>", " </servlet>", "</maera-plugin>") .addFormattedJava( "second.MyServlet", "package second;", "import org.maera.plugin.osgi.Callable2;", "public class MyServlet extends javax.servlet.http.HttpServlet implements first.MyInterface {", " private Callable2 callable;", " public MyServlet(Callable2 cal) { this.callable = cal; }", " public String getServletInfo() {", " try {return callable.call() + ' bob';} catch (Exception ex) { throw new RuntimeException(ex);}", " }", "}") .build(pluginsDir); HostComponentProvider prov = new HostComponentProvider() { public void provide(ComponentRegistrar registrar) { registrar .register(Callable2.class) .forInstance( new Callable2() { public String call() { return "hi"; } }); } }; ServletContext ctx = mock(ServletContext.class); when(ctx.getInitParameterNames()).thenReturn(Collections.enumeration(Collections.emptyList())); ServletConfig servletConfig = mock(ServletConfig.class); when(servletConfig.getServletContext()).thenReturn(ctx); ServletModuleManager mgr = new DefaultServletModuleManager(pluginEventManager); hostContainer = createHostContainer( Collections.<Class<?>, Object>singletonMap(ServletModuleManager.class, mgr)); initPluginManager( prov, new SingleModuleDescriptorFactory(hostContainer, "servlet", ServletModuleDescriptor.class)); assertEquals(2, pluginManager.getEnabledPlugins().size()); assertTrue(pluginManager.getPlugin("first").getPluginState() == PluginState.ENABLED); assertNotNull(pluginManager.getPlugin("asecond").getPluginState() == PluginState.ENABLED); assertEquals("hi bob", mgr.getServlet("/foo", servletConfig).getServletInfo()); final File updatedJar = new PluginJarBuilder("first-updated") .addPluginInformation("foo", "Some name", "1.0") .addFormattedJava( "first.MyInterface", "package first;", "public interface MyInterface {}") .addFormattedResource( "META-INF/MANIFEST.MF", "Manifest-Version: 1.0", "Bundle-SymbolicName: foo", "Bundle-Version: 1.0", "Export-Package: first", "") .build(); pluginManager.installPlugin(new JarPluginArtifact(updatedJar)); WaitUntil.invoke( new BasicWaitCondition() { public boolean isFinished() { return pluginManager.isPluginEnabled("asecond"); } }); assertEquals("hi bob", mgr.getServlet("/foo", servletConfig).getServletInfo()); }
@Test public void testPersistTimeoutSystemPropertyUpdateExistingTimeout() throws Exception { System.setProperty(PluginUtils.MAERA_PLUGINS_ENABLE_WAIT, "300"); try { final File dummySpringXMLFile = File.createTempFile( "temp", "account-data-context.xml", new File(System.getProperty("java.io.tmpdir"))); FileWriter fileWriter = new FileWriter(dummySpringXMLFile); fileWriter.write( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<beans xmlns=\"http://www.springframework.org/schema/beans\"\n" + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + " xmlns:osgi=\"http://www.springframework.org/schema/osgi\"\n" + " xsi:schemaLocation=\"http://www.springframework.org/schema/beans\n" + " http://www.springframework.org/schema/beans/spring-beans-3.0.xsd\n" + " http://www.springframework.org/schema/osgi\n" + " http://www.springframework.org/schema/osgi/spring-osgi.xsd\"\n" + ">\n" + "</beans>"); fileWriter.close(); final File propertiesFile = new File(cacheDir, ".properties"); assertFalse(propertiesFile.exists()); final PluginJarBuilder firstBuilder = new PluginJarBuilder("first"); final File jar = firstBuilder .addPluginInformation("first", "Some name", "1.0") .addFormattedJava( "first.MyInterface", "package first;", "public interface MyInterface {}") .addFormattedResource( "META-INF/MANIFEST.MF", "Manifest-Version: 1.0", "Bundle-SymbolicName: first", "Bundle-Version: 1.0", "Export-Package: first", "Spring-Context: config/account-data-context.xml;create-asynchrously:=false", "") .addFile("config/account-data-context.xml", dummySpringXMLFile) .build(); initPluginManager(); pluginManager.installPlugin(new JarPluginArtifact(jar)); Bundle bundle = findBundleByName("first"); Dictionary headers = bundle.getHeaders(); assertEquals( "config/account-data-context.xml;create-asynchrously:=false;timeout:=300", headers.get("Spring-Context")); assertTrue(propertiesFile.exists()); Properties properties = new Properties(); properties.load(new FileInputStream(propertiesFile)); assertEquals("300", properties.getProperty("spring.timeout")); final File testFile = new File(new File(cacheDir, "transformed-plugins"), ".test"); assertTrue(testFile.createNewFile()); assertTrue(testFile.exists()); System.setProperty(PluginUtils.MAERA_PLUGINS_ENABLE_WAIT, "301"); initPluginManager(); pluginManager.installPlugin(new JarPluginArtifact(jar)); bundle = findBundleByName("first"); headers = bundle.getHeaders(); assertEquals( "config/account-data-context.xml;create-asynchrously:=false;timeout:=301", headers.get("Spring-Context")); assertTrue(propertiesFile.exists()); properties.load(new FileInputStream(propertiesFile)); assertEquals("301", properties.getProperty("spring.timeout")); } finally { System.clearProperty(PluginUtils.MAERA_PLUGINS_ENABLE_WAIT); } }