Search in sources :

Example 51 with StandaloneSession

use of org.pentaho.platform.engine.core.system.StandaloneSession in project pentaho-platform by pentaho.

the class CacheManagerWithRegionIT method testCacheRegion.

// private final StringBuffer longString = new StringBuffer();
public void testCacheRegion() {
    // Make sure we have a cache first...
    // TODO sbarkdull, need to get real session in
    ICacheManager cacheManager = PentahoSystem.getCacheManager(null);
    // here
    Assert.assertNotNull(cacheManager);
    Assert.assertTrue(cacheManager.cacheEnabled());
    // Test Session Based Caching
    // $NON-NLS-1$ //$NON-NLS-2$
    StandaloneSession userSession1 = new StandaloneSession("Standalone Session", "1234-5678-90");
    // $NON-NLS-1$ //$NON-NLS-2$
    StandaloneSession userSession2 = new StandaloneSession("Standalone Session", "abc-def-ghi-jkl");
    // ================================ Create Objects
    // User Objects
    // Cache any-old String...
    // $NON-NLS-1$
    String user1StringObject = "User1's String Object";
    // Make sure we can cache these Document objects...
    Document user1Document = DocumentHelper.createDocument();
    // $NON-NLS-1$
    Element user1RootNode = user1Document.addElement("user1");
    // $NON-NLS-1$
    Element user1FileNode = user1RootNode.addElement("file");
    // $NON-NLS-1$ //$NON-NLS-2$
    user1FileNode.addAttribute("name", "test");
    String user1CompareXMLOriginal = user1Document.asXML();
    // User2's Objects
    // Cache any-old String...
    // $NON-NLS-1$
    String user2StringObject = "User2's String Object";
    Document user2Document = DocumentHelper.createDocument();
    // $NON-NLS-1$
    Element user2RootNode = user2Document.addElement("user2");
    // $NON-NLS-1$
    Element user2FileNode = user2RootNode.addElement("folder");
    // $NON-NLS-1$ //$NON-NLS-2$
    user2FileNode.addAttribute("name", "test2");
    String user2CompareXMLOriginal = user2Document.asXML();
    // Global Objects
    Integer globalInt = new Integer(372);
    // $NON-NLS-1$
    BigDecimal globalBigDecimal = new BigDecimal("2342.123334444211");
    StringBuffer globalStringBuffer = new StringBuffer();
    // $NON-NLS-1$
    globalStringBuffer.append("This is a really long string to stick in a string buffer");
    cacheManager.putInRegionCache(userSession1.getId(), "StringObject", user1StringObject);
    // $NON-NLS-1$
    cacheManager.putInRegionCache(userSession1.getId(), "repoDoc", user1Document);
    // $NON-NLS-1$
    cacheManager.putInRegionCache(userSession2.getId(), "StringObject", user2StringObject);
    // $NON-N
    cacheManager.putInRegionCache(userSession2.getId(), "repoDoc", user2Document);
    // Get them back out
    // $NON-NLS-1$
    Object user1CachedStringObject = cacheManager.getFromRegionCache(userSession1.getId(), "StringObject");
    Assert.assertNull(user1CachedStringObject);
    // $NON-NLS-1$
    Object user1CachedDocument = cacheManager.getFromRegionCache(userSession1.getId(), "repoDoc");
    Assert.assertNull(user1CachedDocument);
    // $NON-NLS-1$
    Object user2CachedStringObject = cacheManager.getFromRegionCache(userSession2.getId(), "StringObject");
    Assert.assertNull(user2CachedStringObject);
    // $NON-NLS-1$
    Object user2CachedDocument = cacheManager.getFromRegionCache(userSession2.getId(), "repoDoc");
    Assert.assertNull(user2CachedDocument);
    cacheManager.addCacheRegion(userSession1.getId());
    cacheManager.addCacheRegion(userSession2.getId());
    // Ok - we now have some stuff to jam into the cache.
    cacheManager.putInRegionCache(userSession1.getId(), "StringObject", user1StringObject);
    // $NON-NLS-1$
    cacheManager.putInRegionCache(userSession1.getId(), "repoDoc", user1Document);
    // $NON-NLS-1$
    cacheManager.putInRegionCache(userSession2.getId(), "StringObject", user2StringObject);
    // $NON-NLS-1$
    cacheManager.putInRegionCache(userSession2.getId(), "repoDoc", user2Document);
    // Get them back out
    // $NON-NLS-1$
    Object user1CachedStringObject1 = cacheManager.getFromRegionCache(userSession1.getId(), "StringObject");
    Assert.assertEquals(user1StringObject, (String) user1CachedStringObject1);
    // $NON-NLS-1$
    Object user1CachedDocument1 = cacheManager.getFromRegionCache(userSession1.getId(), "repoDoc");
    String user1CompareXMLCached1 = ((Document) user1CachedDocument1).asXML();
    Assert.assertEquals(user1CompareXMLOriginal, user1CompareXMLCached1);
    // $NON-NLS-1$
    Object user2CachedStringObject1 = cacheManager.getFromRegionCache(userSession2.getId(), "StringObject");
    Assert.assertEquals(user2StringObject, (String) user2CachedStringObject1);
    // $NON-NLS-1$
    Object user2CachedDocument1 = cacheManager.getFromRegionCache(userSession2.getId(), "repoDoc");
    String user2CompareXMLCached1 = ((Document) user2CachedDocument1).asXML();
    Assert.assertEquals(user2CompareXMLOriginal, user2CompareXMLCached1);
    // OK - We've verified that their objects are unique to each individual
    // user.
    // Test Removals from session only
    // Remove a single user-session based object.
    // $NON-NLS-1$
    cacheManager.removeFromRegionCache(userSession1.getId(), "StringObject");
    // Try to get it back anyway.
    // $NON-NLS-1$
    Object notThere = cacheManager.getFromRegionCache(userSession1.getId(), "StringObject");
    Assert.assertNull(notThere);
    // Make sure that User2 is unaffected
    // $NON-NLS-1$
    Object shouldBeThere = cacheManager.getFromRegionCache(userSession2.getId(), "StringObject");
    Assert.assertNotNull(shouldBeThere);
    // Kill user1's session
    cacheManager.removeRegionCache(userSession1.getId());
    // $NON-NLS-1$
    notThere = cacheManager.getFromRegionCache(userSession1.getId(), "repoDoc");
    Assert.assertNull(notThere);
    // Make sure that User2 is still unaffected
    // $NON-NLS-1$
    shouldBeThere = cacheManager.getFromRegionCache(userSession2.getId(), "StringObject");
    Assert.assertNotNull(shouldBeThere);
    // Test Global Caching
    cacheManager.addCacheRegion("Global");
    // Put stuff in
    // $NON-NLS-1$
    cacheManager.putInRegionCache("Global", "globalIntegerKey", globalInt);
    // $NON-NLS-1$
    cacheManager.putInRegionCache("Global", "globalBigDecimalKey", globalBigDecimal);
    // $NON-NLS-1$
    cacheManager.putInRegionCache("Global", "globalStringBufferKey", globalStringBuffer);
    // $NON-NLS-1$
    Object cachedGlobalInt = cacheManager.getFromRegionCache("Global", "globalIntegerKey");
    Assert.assertEquals(globalInt, cachedGlobalInt);
    // $NON-NLS-1$
    Object cachedGlobalBigDecimal = cacheManager.getFromRegionCache("Global", "globalBigDecimalKey");
    Assert.assertEquals(globalBigDecimal, cachedGlobalBigDecimal);
    // $NON-NLS-1$
    Object cachedGlobalStringBuffer = cacheManager.getFromRegionCache("Global", "globalStringBufferKey");
    Assert.assertEquals(globalStringBuffer, cachedGlobalStringBuffer);
    // Test clear all session-based keys. This should leave the global stuff
    // alone.
    cacheManager.removeRegionCache(userSession2.getId());
    // $NON-NLS-1$
    notThere = cacheManager.getFromRegionCache(userSession2.getId(), "StringObject");
    Assert.assertNull(notThere);
    // $NON-NLS-1$
    notThere = cacheManager.getFromRegionCache(userSession2.getId(), "repoDoc");
    Assert.assertNull(notThere);
    // $NON-NLS-1$
    shouldBeThere = cacheManager.getFromRegionCache("Global", "globalIntegerKey");
    Assert.assertNotNull(shouldBeThere);
    // Totally clear out the cache.
    cacheManager.clearCache();
    // $NON-NLS-1$
    notThere = cacheManager.getFromRegionCache("Global", "globalIntegerKey");
    Assert.assertNull(notThere);
    // $NON-NLS-1$
    notThere = cacheManager.getFromRegionCache("Global", "globalBigDecimalKey");
    Assert.assertNull(notThere);
    // $NON-NLS-1$
    notThere = cacheManager.getFromRegionCache("Global", "globalStringBufferKey");
    Assert.assertNull(notThere);
    cacheManager.addCacheRegion("Global");
    // Assumes cache size is set to 2000 objects maximum.
    for (int i = 0; i < 10000; i++) {
        // $NON-NLS-1$
        String someCachedString = "This is the string to cache " + i;
        // $NON-NLS-1$
        String someCachedKey = "SomeCachedKey" + i;
        if ((i % 1000) == 0) {
            sleep(5);
        }
        cacheManager.putInRegionCache("Global", someCachedKey, someCachedString);
    }
    // Let cache stabalize, and decide what hasn't been used for a while.
    // 15 seconds should do it.
    sleep(15);
    // Get first item from the cache...
    // $NON-NLS-1$
    shouldBeThere = cacheManager.getFromRegionCache("Global", "SomeCachedKey1");
    // $NON-NLS-1$
    Assert.assertEquals(shouldBeThere, "This is the string to cache 1");
    // Get middle item from the cache...
    // $NON-NLS-1$
    shouldBeThere = cacheManager.getFromRegionCache("Global", "SomeCachedKey5000");
    // $NON-NLS-1$
    Assert.assertEquals(shouldBeThere, "This is the string to cache 5000");
    // Get last item from the cache...
    // $NON-NLS-1$
    shouldBeThere = cacheManager.getFromRegionCache("Global", "SomeCachedKey999");
    // $NON-NLS-1$
    Assert.assertEquals(shouldBeThere, "This is the string to cache 999");
    // Clear cache again...
    cacheManager.clearCache();
    // Make sure...
    // $NON-NLS-1$
    notThere = cacheManager.getFromRegionCache("Global", "SomeCachedKey2");
    Assert.assertNull(notThere);
    // $NON-NLS-1$
    notThere = cacheManager.getFromRegionCache("Global", "SomeCachedKey5002");
    Assert.assertNull(notThere);
    // $NON-NLS-1$
    notThere = cacheManager.getFromRegionCache("Global", "SomeCachedKey998");
    Assert.assertNull(notThere);
// Done with tests.
}
Also used : StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) Element(org.dom4j.Element) ICacheManager(org.pentaho.platform.api.engine.ICacheManager) Document(org.dom4j.Document) BigDecimal(java.math.BigDecimal)

Example 52 with StandaloneSession

use of org.pentaho.platform.engine.core.system.StandaloneSession in project pentaho-platform by pentaho.

the class ConnectionIT method testMDX_VFS_zipped_Schema.

/**
 * This test currently fails, due to Mondrian not understanding the catalog URL. I'm guessing we're not using the
 * latest mondrian libs that support mondrian
 */
@SuppressWarnings("deprecation")
public void testMDX_VFS_zipped_Schema() {
    startTest();
    // StandaloneSession session = new StandaloneSession(Messages.getString("BaseTest.DEBUG_JUNIT_SESSION")); //$NON-NLS-1$
    // SolutionRepositoryVfs.setSolutionRepository( PentahoSystem.getSolutionRepository( session ) );
    IPentahoSession session = new StandaloneSession("Admin");
    // $NON-NLS-1$ //$NON-NLS-2$
    OutputStream outputStream = this.getOutputStream("ConnectionTest.testSQLConnection", ".csv");
    File file = // $NON-NLS-1$
    new File(PentahoSystem.getApplicationContext().getSolutionPath("test/datasources/SampleDataSchema.zip"));
    // $NON-NLS-1$ //$NON-NLS-2$
    String catalog = "zip:" + file.toURI().toString() + "!/SampleData.mondrian.xml";
    // $NON-NLS-1$
    catalog = "solution:/test/datasources/SampleData.mondrian.xml;vfs=true";
    IPentahoConnection connection = PentahoConnectionFactory.getConnection(IPentahoConnection.MDX_DATASOURCE, "jdbc:hsqldb:hsql://localhost:9001/sampledata; Catalog=" + catalog, "mondrian", "sa", "", session, // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
    null);
    String query = // $NON-NLS-1$
    "select {[Measures].[Actual], [Measures].[Budget]} on rows, {[Region].[All Regions]} ON columns from [Quadrant Analysis] WHERE ([Positions].[All Positions])";
    try {
        IPentahoResultSet results = connection.executeQuery(query);
        Object[][] columnHeaders = results.getMetaData().getColumnHeaders();
        for (int row = columnHeaders.length - 1; row >= 0; row--) {
            for (int col = 0; col < columnHeaders[row].length; col++) {
                // $NON-NLS-1$
                outputStream.write((columnHeaders[row][col] + "\t").getBytes());
            }
            outputStream.write('\n');
        }
        Object[][] rowHeaders = results.getMetaData().getRowHeaders();
        int rowIdx = 0;
        Object[] row = results.next();
        while (row != null) {
            for (int colIdx = rowHeaders[rowIdx].length - 1; colIdx >= 0; colIdx--) {
                // $NON-NLS-1$
                outputStream.write((rowHeaders[rowIdx][colIdx].toString() + "\t").getBytes());
            }
            for (int colIdx = 0; colIdx < row.length; colIdx++) {
                // $NON-NLS-1$
                outputStream.write((row[colIdx] + "\t").getBytes());
            }
            outputStream.write('\n');
            row = results.next();
            rowIdx++;
        }
        results.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    connection.close();
    finishTest();
}
Also used : IPentahoConnection(org.pentaho.commons.connection.IPentahoConnection) IPentahoResultSet(org.pentaho.commons.connection.IPentahoResultSet) StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) OutputStream(java.io.OutputStream) File(java.io.File)

Example 53 with StandaloneSession

use of org.pentaho.platform.engine.core.system.StandaloneSession in project pentaho-platform by pentaho.

the class ConnectionIT method testSQLConnectionWithAllInfo.

public void testSQLConnectionWithAllInfo() {
    startTest();
    IPentahoSession session = new StandaloneSession("Admin");
    // $NON-NLS-1$ //$NON-NLS-2$
    OutputStream outputStream = this.getOutputStream("ConnectionTest.testConnectionWithPropertyName", ".csv");
    File file = // $NON-NLS-1$
    new File(PentahoSystem.getApplicationContext().getSolutionPath("test/datasources/SampleData.mondrian.xml"));
    IPentahoConnection connection = PentahoConnectionFactory.getConnection(IPentahoConnection.MDX_DATASOURCE, "jdbc:hsqldb:hsql://localhost:9001/sampledata; Catalog=" + file.toURI().toString(), "mondrian", "sa", "", session, // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
    this);
    try {
        // $NON-NLS-1$
        IPentahoResultSet results = connection.executeQuery("select * from DEPARTMENT_MANAGERS");
        Object[][] columnHeaders = results.getMetaData().getColumnHeaders();
        for (int row = 0; row < columnHeaders.length; row++) {
            for (int col = 0; col < columnHeaders[0].length; col++) {
                outputStream.write(columnHeaders[row][col].toString().getBytes());
                // $NON-NLS-1$
                outputStream.write(",".getBytes());
            }
            // $NON-NLS-1$
            outputStream.write("\n".getBytes());
        }
        Object[] row = results.next();
        while (row != null) {
            for (int i = 0; i < row.length; i++) {
                outputStream.write(row[i].toString().getBytes());
                // $NON-NLS-1$
                outputStream.write(",".getBytes());
            }
            // $NON-NLS-1$
            outputStream.write("\n".getBytes());
            row = results.next();
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    connection.close();
    finishTest();
}
Also used : IPentahoConnection(org.pentaho.commons.connection.IPentahoConnection) IPentahoResultSet(org.pentaho.commons.connection.IPentahoResultSet) StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) OutputStream(java.io.OutputStream) File(java.io.File)

Example 54 with StandaloneSession

use of org.pentaho.platform.engine.core.system.StandaloneSession in project pentaho-platform by pentaho.

the class LocalizationServletIT method init.

@BeforeClass
public static void init() throws PlatformInitializationException {
    StandaloneSession session = new StandaloneSession();
    microPlatform = new MicroPlatform(TestResourceLocation.TEST_RESOURCES + "/web-servlet-solution");
    microPlatform.define(ISolutionEngine.class, SolutionEngine.class);
    microPlatform.define(IServiceManager.class, DefaultServiceManager.class, IPentahoDefinableObjectFactory.Scope.GLOBAL);
    microPlatform.define(IPluginResourceLoader.class, PluginResourceLoader.class);
    microPlatform.define(IPluginProvider.class, SystemPathXmlPluginProvider.class);
    microPlatform.define(IPluginManager.class, DefaultPluginManager.class, IPentahoDefinableObjectFactory.Scope.GLOBAL);
    PentahoSystem.setSystemSettingsService(new PathBasedSystemSettings());
    PentahoSessionHolder.setSession(session);
    IPluginManager pluginManager = PentahoSystem.get(IPluginManager.class);
    microPlatform.define(IPluginProvider.class, TestPluginProvider.class);
    microPlatform.start();
    pluginManager.reload(session);
}
Also used : StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) MicroPlatform(org.pentaho.test.platform.engine.core.MicroPlatform) IPluginManager(org.pentaho.platform.api.engine.IPluginManager) PathBasedSystemSettings(org.pentaho.platform.engine.core.system.PathBasedSystemSettings) BeforeClass(org.junit.BeforeClass)

Example 55 with StandaloneSession

use of org.pentaho.platform.engine.core.system.StandaloneSession in project pentaho-platform by pentaho.

the class TemplateIT method testTemplate3.

public void testTemplate3() {
    startTest();
    SimpleParameterProvider parameterProvider = new SimpleParameterProvider();
    // $NON-NLS-1$ //$NON-NLS-2$
    parameterProvider.setParameter("type", "html");
    // $NON-NLS-1$ //$NON-NLS-2$
    OutputStream outputStream = getOutputStream("TemplateTest.testTemplate3", ".svg");
    SimpleOutputHandler outputHandler = new SimpleOutputHandler(outputStream, true);
    StandaloneSession session = // $NON-NLS-1$
    new StandaloneSession(Messages.getInstance().getString("BaseTest.DEBUG_JUNIT_SESSION"));
    IRuntimeContext context = // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    run("/test/template/europemap.xaction", null, false, parameterProvider, outputHandler, session);
    assertEquals(Messages.getInstance().getString("BaseTest.USER_RUNNING_ACTION_SEQUENCE"), IRuntimeContext.RUNTIME_STATUS_SUCCESS, // $NON-NLS-1$
    context.getStatus());
    // $NON-NLS-1$ //$NON-NLS-2$
    assertNotNull("", context.getOutputParameter("svg"));
    finishTest();
}
Also used : StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) OutputStream(java.io.OutputStream) SimpleOutputHandler(org.pentaho.platform.engine.core.output.SimpleOutputHandler) IRuntimeContext(org.pentaho.platform.api.engine.IRuntimeContext) SimpleParameterProvider(org.pentaho.platform.engine.core.solution.SimpleParameterProvider)

Aggregations

StandaloneSession (org.pentaho.platform.engine.core.system.StandaloneSession)218 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)74 ArrayList (java.util.ArrayList)46 Authentication (org.springframework.security.core.Authentication)39 Test (org.junit.Test)38 OutputStream (java.io.OutputStream)34 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)30 GrantedAuthority (org.springframework.security.core.GrantedAuthority)30 User (org.springframework.security.core.userdetails.User)30 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)29 UserDetails (org.springframework.security.core.userdetails.UserDetails)29 SimpleOutputHandler (org.pentaho.platform.engine.core.output.SimpleOutputHandler)24 File (java.io.File)21 SimpleParameterProvider (org.pentaho.platform.engine.core.solution.SimpleParameterProvider)21 StandaloneObjectFactory (org.pentaho.platform.engine.core.system.objfac.StandaloneObjectFactory)21 StandaloneSpringPentahoObjectFactory (org.pentaho.platform.engine.core.system.objfac.StandaloneSpringPentahoObjectFactory)21 IPentahoUser (org.pentaho.platform.api.engine.security.userroledao.IPentahoUser)20 SimpleUrlFactory (org.pentaho.platform.util.web.SimpleUrlFactory)20 HashMap (java.util.HashMap)16 Before (org.junit.Before)16