This page will be a work in progress for now. The intent is to show how you can quickly embed a BI Platform in a JUnit test without extending any classes, such as BaseTest. The ability to startup a BI Platform in embedded mode gives you much more control in how your unit test will perform, not to mention it makes maintaining your unit test much easier since you have virtually no configuration files to maintain along with the test.
PentahoBoot and PentahoSystemBoot
TODO put a brief overview here of how to use these classes to configure and initialize an embedded Pentaho BI platform
Various example configurations (shown using JUnit)
Run a minimal action sequence
This configuration will setup everything necessary to run a very simplistic action sequence which accesses no data, but rather runs with hardcoded values provided within the action sequence itself.
A few things to note:
- We are providing a relative dir to the MicroPlatform called "solutions". This folder must exist at the root of your project.
public class EchoActionTest { private MicroPlatform booter; StandaloneSession session; @Before public void init() { booter = new MicroPlatform("solutions"); booter.define(ISolutionEngine.class, SolutionEngine.class, Scope.GLOBAL); booter.define(ISolutionRepository.class, FileBasedSolutionRepository.class, Scope.GLOBAL); booter.define(IServiceManager.class, DefaultServiceManager.class, Scope.GLOBAL); booter.define(IPluginManager.class, DefaultPluginManager.class, Scope.GLOBAL); booter.define("systemStartupSession", StandaloneSession.class, Scope.GLOBAL); session = new StandaloneSession(); } @Test public void testEchoActionExecute() throws PlatformInitializationException, FileNotFoundException { booter.define(IPluginProvider.class, TestPluginProvider.class); booter.addLifecycleListener(new PluginAdapter()); booter.start(); OutputStream outputStream = new FileOutputStream(new File("testEchoActionExecute.html")); ISolutionEngine engine = SolutionHelper.execute("testing EchoAction", "testuser", "bi-developers/EchoPlugin/echoAction.xaction", new HashMap(), outputStream); // TODO: add a convenience method in SolutionHelper to access the messages object } public static class TestPluginProvider implements IPluginProvider { public List<IPlatformPlugin> getPlugins(IPentahoSession session) throws PlatformPluginRegistrationException { PlatformPlugin p = new PlatformPlugin(); //need to set source description - classloader needs it p.setId("test-echoAction-plugin"); p.addBean(new PluginBeanDefinition("EchoAction", "org.pentaho.samples.EchoAction")); return Arrays.asList((IPlatformPlugin) p); } } }
Run a chartbeans actions sequence
Things to note about this example:
- The xactions tested used SqlLookupRule, so DB connections were necessary, thus the repository and db defines were required
- Currently ChartComponent (generates chartbeans content) is part of plugins-actions and not supplied by a plugin, consequently the plugin layer is not enabled in this embedded configuration.
A couple gotchas found when running chartbeans action sequences in embedded mode:
- There was enough file-based config required that I decided just to reference a full sample solution as my solutions folder, even though lots of it is not required, e.g. Spring config.
- In trying to run chartbeans samples I got chartbeans output which I belieive is valid, written to a file which I then load in FF, but flash is deciding to not render the chart. I tried changing the allowScriptAccess to be "always", but that still didn't help.
/** * INSTRUCTIONS: * <ol> * <li> point SOLUTION_PATH to the bi-platform-sample-solution project (or other solutions folder) * Note: not all system files in the solution are really needed. In fact, you may remove any spring config files * and expect everything to work just fine. * <li> comment out the PentahoUser.hbm.xml and PentahoRole.hbm.xml mapping file entries in <dialect>.hibernate.cfg.xml * These are supplied by the user console and will not be used in this embedded appliction. * <li> set XACTION to the relative path to the xaction file you wish to run (relative to SOLUTION_PATH) * </ol> */ public class ActionSequenceRunnerTest { public static final String XACTION = "bi-developers/chartbeans/chartbeans_simple_bar.xaction"; public static final String SOLUTION_PATH = "/home/aaron/workspaces/pentaho/bi-platform-sample-solution"; private MicroPlatform booter; StandaloneSession session; @Before public void init() { booter = new MicroPlatform(SOLUTION_PATH); booter.define(ISolutionEngine.class, SolutionEngine.class, Scope.LOCAL); booter.define(ISolutionRepository.class, FileBasedSolutionRepository.class, Scope.GLOBAL); booter.define(IServiceManager.class, DefaultServiceManager.class, Scope.GLOBAL); booter.define(IPluginManager.class, DefaultPluginManager.class, Scope.GLOBAL); booter.define("systemStartupSession", StandaloneSession.class, Scope.GLOBAL); booter.define( "connection-XML", XQConnection.class, Scope.LOCAL ); booter.define( "connection-SQL", SQLConnection.class, Scope.LOCAL ); booter.define( "file", FileOutputHandler.class, Scope.LOCAL ); booter.define(IChartBeansGenerator.class, DefaultChartBeansGenerator.class, Scope.GLOBAL); //db & repository stuff booter.define(IVersionHelper.class, VersionHelper.class, Scope.GLOBAL); booter.define(IDatasourceService.class, NonPooledDatasourceService.class, Scope.GLOBAL); booter.define(IDatasourceMgmtService.class, DatasourceMgmtService.class, Scope.LOCAL); booter.define(IDatasource.class, Datasource.class, Scope.LOCAL); booter.define(IPasswordService.class, Base64PasswordService.class, Scope.GLOBAL); booter.setSettingsProvider(new PathBasedSystemSettings()); session = new StandaloneSession(); } @Test public void executeActionSequence() throws PlatformInitializationException, FileNotFoundException { booter.start(); OutputStream outputStream = new FileOutputStream(new File("actionSequenceOutput.html")); ISolutionEngine engine = SolutionHelper.execute("testing ChartBeans component", "testuser", XACTION, new HashMap(), outputStream); } }