[exo-jcr-commits] exo-jcr SVN: r593 - jcr/branches/1.12.0-JBC/component/core/src/test/java/org/exoplatform/services/jcr/impl/storage/jbosscache.

do-not-reply at jboss.org do-not-reply at jboss.org
Thu Nov 12 06:14:24 EST 2009


Author: sergiykarpenko
Date: 2009-11-12 06:14:23 -0500 (Thu, 12 Nov 2009)
New Revision: 593

Added:
   jcr/branches/1.12.0-JBC/component/core/src/test/java/org/exoplatform/services/jcr/impl/storage/jbosscache/IndexerCacheLoaderRuntimeTest.java
Log:
EXOJCR-202: IndexerCacheLoaderRuntimeTest added

Added: jcr/branches/1.12.0-JBC/component/core/src/test/java/org/exoplatform/services/jcr/impl/storage/jbosscache/IndexerCacheLoaderRuntimeTest.java
===================================================================
--- jcr/branches/1.12.0-JBC/component/core/src/test/java/org/exoplatform/services/jcr/impl/storage/jbosscache/IndexerCacheLoaderRuntimeTest.java	                        (rev 0)
+++ jcr/branches/1.12.0-JBC/component/core/src/test/java/org/exoplatform/services/jcr/impl/storage/jbosscache/IndexerCacheLoaderRuntimeTest.java	2009-11-12 11:14:23 UTC (rev 593)
@@ -0,0 +1,397 @@
+/*
+ * Copyright (C) 2003-2009 eXo Platform SAS.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Affero General Public License
+ * as published by the Free Software Foundation; either version 3
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see<http://www.gnu.org/licenses/>.
+ */
+package org.exoplatform.services.jcr.impl.storage.jbosscache;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.dbcp.BasicDataSourceFactory;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.Term;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.search.TermQuery;
+import org.exoplatform.container.configuration.ConfigurationManager;
+import org.exoplatform.container.configuration.ConfigurationManagerImpl;
+import org.exoplatform.services.document.DocumentReaderService;
+import org.exoplatform.services.idgenerator.impl.IDGeneratorServiceImpl;
+import org.exoplatform.services.jcr.access.AccessControlList;
+import org.exoplatform.services.jcr.config.CacheEntry;
+import org.exoplatform.services.jcr.config.ContainerEntry;
+import org.exoplatform.services.jcr.config.LockManagerEntry;
+import org.exoplatform.services.jcr.config.LockPersisterEntry;
+import org.exoplatform.services.jcr.config.QueryHandlerEntry;
+import org.exoplatform.services.jcr.config.RepositoryEntry;
+import org.exoplatform.services.jcr.config.SimpleParameterEntry;
+import org.exoplatform.services.jcr.config.ValueStorageEntry;
+import org.exoplatform.services.jcr.config.ValueStorageFilterEntry;
+import org.exoplatform.services.jcr.config.WorkspaceEntry;
+import org.exoplatform.services.jcr.core.nodetype.NodeTypeDataManager;
+import org.exoplatform.services.jcr.datamodel.InternalQName;
+import org.exoplatform.services.jcr.datamodel.NodeData;
+import org.exoplatform.services.jcr.datamodel.QPath;
+import org.exoplatform.services.jcr.impl.Constants;
+import org.exoplatform.services.jcr.impl.core.LocationFactory;
+import org.exoplatform.services.jcr.impl.core.NamespaceRegistryImpl;
+import org.exoplatform.services.jcr.impl.core.nodetype.NodeTypeDataManagerImpl;
+import org.exoplatform.services.jcr.impl.core.nodetype.registration.JcrNodeTypeDataPersister;
+import org.exoplatform.services.jcr.impl.core.nodetype.registration.NodeTypeDataPersister;
+import org.exoplatform.services.jcr.impl.core.query.RepositoryIndexSearcherHolder;
+import org.exoplatform.services.jcr.impl.core.query.SearchManager;
+import org.exoplatform.services.jcr.impl.core.query.SearchManagerImpl;
+import org.exoplatform.services.jcr.impl.core.query.SystemSearchManagerHolder;
+import org.exoplatform.services.jcr.impl.core.query.lucene.SearchIndex;
+import org.exoplatform.services.jcr.impl.dataflow.TransientNodeData;
+import org.exoplatform.services.jcr.impl.dataflow.TransientPropertyData;
+import org.exoplatform.services.jcr.impl.dataflow.TransientValueData;
+import org.exoplatform.services.jcr.impl.dataflow.persistent.JBossCacheWorkspaceDataManager;
+import org.exoplatform.services.jcr.impl.dataflow.persistent.WorkspacePersistentDataManager;
+import org.exoplatform.services.jcr.impl.storage.SystemDataContainerHolder;
+import org.exoplatform.services.jcr.impl.storage.jdbc.JDBCWorkspaceDataContainer;
+import org.exoplatform.services.jcr.impl.storage.value.StandaloneStoragePluginProvider;
+import org.exoplatform.services.jcr.storage.WorkspaceDataContainer;
+import org.exoplatform.services.jcr.storage.WorkspaceStorageConnection;
+import org.exoplatform.services.jcr.storage.value.ValueStoragePluginProvider;
+import org.exoplatform.services.jcr.util.IdGenerator;
+import org.jboss.cache.Cache;
+import org.jboss.cache.CacheSPI;
+import org.jboss.cache.DefaultCacheFactory;
+import org.jboss.cache.Fqn;
+import org.jboss.cache.Node;
+
+import java.io.Serializable;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
+
+import javax.jcr.PropertyType;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.sql.DataSource;
+
+/**
+ * Created by The eXo Platform SAS.
+ * 
+ * <br/>Date: 
+ *
+ * @author <a href="karpenko.sergiy at gmail.com">Karpenko Sergiy</a> 
+ * @version $Id: IndexerCacheLoaderRuntimeTest.java 111 2008-11-11 11:11:11Z serg $
+ */
+public class IndexerCacheLoaderRuntimeTest extends TestCase
+{
+
+   protected JBossCacheStorageConnection conn;
+
+   protected Cache<Serializable, Object> cache;
+
+   protected Node<Serializable, Object> nodes;
+
+   protected Node<Serializable, Object> props;
+
+   protected Node<Serializable, Object> session;
+
+   protected String jbcConfig;
+
+   JDBCWorkspaceDataContainer persistentContainer;
+
+   SearchManager manager;
+
+   /**
+    * {@inheritDoc}
+    */
+   protected void setUp() throws Exception
+   {
+      try
+      {
+         super.setUp();
+
+         // JBossCache 
+         initJBCConfig();
+
+         cache = new DefaultCacheFactory<Serializable, Object>().createCache(jbcConfig, false);
+
+         initJBC();
+
+         // run cache
+         cache.create();
+         cache.start();
+
+         Node<Serializable, Object> cacheRoot = cache.getRoot();
+
+         // prepare cache structures
+
+         nodes = cacheRoot.addChild(Fqn.fromString(JBossCacheStorage.NODES));
+         props = cacheRoot.addChild(Fqn.fromString(JBossCacheStorage.PROPS));
+         session = cacheRoot.addChild(Fqn.fromString(JBossCacheStorage.SESSION));
+
+         // JCR connection
+         conn = new JBossCacheStorageConnection(cache, nodes, props, session);
+         // add root (/)
+         conn.add(new TransientNodeData(Constants.ROOT_PATH, Constants.ROOT_UUID, 1, Constants.NT_UNSTRUCTURED,
+            new InternalQName[0], 0, null, new AccessControlList()));
+
+         String propId1 = "rootf111111";
+         QPath propPath1 = QPath.makeChildPath(Constants.ROOT_PATH, Constants.JCR_PRIMARYTYPE);
+         TransientPropertyData propData1 =
+            new TransientPropertyData(propPath1, propId1, 1, PropertyType.NAME, Constants.ROOT_UUID, false);
+         propData1.setValue(new TransientValueData(Constants.NT_UNSTRUCTURED));
+         conn.add(propData1);
+
+         conn.commit();
+         ((SearchManagerImpl)manager).start();
+      }
+      catch (Exception e)
+      {
+         e.printStackTrace();
+         throw e;
+      }
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   //   protected void tearDown() throws Exception
+   //   {
+   //
+   //      cache.stop();
+   //      cache.destroy();
+   //
+   //      super.tearDown();
+   //   }
+
+   protected void initJBC() throws Exception
+   {
+
+      // Create WorkspaceEntry
+      ContainerEntry containerEntry = new ContainerEntry();
+      List<SimpleParameterEntry> params = new ArrayList<SimpleParameterEntry>();
+      params.add(new SimpleParameterEntry(JDBCWorkspaceDataContainer.DB_DIALECT, "hsqldb"));
+      params.add(new SimpleParameterEntry(JDBCWorkspaceDataContainer.SWAPDIR_PROP, "/target/temp/swap/ws"));
+      containerEntry.setParameters(params);
+
+      // Initialize id generator.
+      new IdGenerator(new IDGeneratorServiceImpl());
+
+      // Set system property Context.INITIAL_CONTEXT_FACTORY to initial context. 
+      System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.exoplatform.services.naming.SimpleContextFactory");
+
+      // Create data source. Will be created new data source to new test. 
+      String dataSourceName = "jdbcjcr_" + IdGenerator.generate();
+      createNewDataSource(dataSourceName);
+
+      WorkspaceEntry ws =
+         getNewWs("ws_to_jbdc_cache_loader", true, dataSourceName, "/target/temp/ws_to_jbdc_cache_loader/values",
+            containerEntry);
+
+      RepositoryEntry re = new RepositoryEntry();
+      re.addWorkspace(ws);
+
+      ValueStoragePluginProvider valueStoragePluginProvider = new StandaloneStoragePluginProvider(ws);
+
+      persistentContainer = new JDBCWorkspaceDataContainer(ws, re, null, valueStoragePluginProvider);
+
+      persistentContainer.start();
+
+      NamespaceRegistryImpl nsReg = new NamespaceRegistryImpl();
+      nsReg.start();
+      SystemDataContainerHolder<WorkspaceDataContainer> systemDataContainerHolder =
+         new SystemDataContainerHolder(persistentContainer);
+
+      WorkspacePersistentDataManager itemMgr =
+         new JBossCacheWorkspaceDataManager(persistentContainer, systemDataContainerHolder);
+
+      RepositoryIndexSearcherHolder indexSearcherHolder = new RepositoryIndexSearcherHolder();
+
+      NodeTypeDataPersister persister = new JcrNodeTypeDataPersister(itemMgr, false);
+      NodeTypeDataManager ntReg =
+         new NodeTypeDataManagerImpl(re, new LocationFactory(nsReg), nsReg, persister, itemMgr, indexSearcherHolder);
+
+      SystemSearchManagerHolder parentSearchManager = null;
+      DocumentReaderService extractor = null;
+      ConfigurationManager cfm = new ConfigurationManagerImpl();
+
+      manager =
+         new SearchManagerImpl(ws.getQueryHandler(), nsReg, ntReg, itemMgr, parentSearchManager, extractor, cfm,
+            indexSearcherHolder);
+
+      ((CacheSPI<Serializable, Object>)cache).getComponentRegistry().registerComponent(persistentContainer,
+         WorkspaceDataContainer.class);
+
+      ((CacheSPI<Serializable, Object>)cache).getComponentRegistry().registerComponent(manager, SearchManager.class);
+
+   }
+
+   protected void initJBCConfig()
+   {
+      jbcConfig = "conf/standalone/test-jbosscache-config-exoloader.xml";
+   }
+
+   public void testAddNode() throws Exception
+   {
+      // prepare
+      //super.testAddNode();
+      String node1id = "111111111111";
+      QPath node1path = QPath.makeChildPath(Constants.ROOT_PATH, "[]node:1");
+      conn.add(new TransientNodeData(node1path, node1id, 1, Constants.NT_UNSTRUCTURED, new InternalQName[0], 0,
+         Constants.ROOT_UUID, new AccessControlList()));
+
+      String propId1 = "111111222222";
+      QPath propPath1 = QPath.makeChildPath(node1path, Constants.JCR_PRIMARYTYPE);
+      TransientPropertyData propData1 =
+         new TransientPropertyData(propPath1, propId1, 1, PropertyType.NAME, node1id, false);
+      propData1.setValue(new TransientValueData(Constants.NT_UNSTRUCTURED));
+      conn.add(propData1);
+
+      conn.commit();
+
+      // check do we really have node
+      WorkspaceStorageConnection connection = persistentContainer.openConnection();
+
+      NodeData nd = (NodeData)connection.getItemData(node1id);
+      assertNotNull(nd);
+      assertEquals("[]:1[]node:1", nd.getQPath().getAsString());
+
+      // find node by search engine
+
+      SearchIndex index = ((SearchIndex)manager.getHandler());
+      IndexReader reader = index.getIndexReader();
+
+      IndexSearcher searcher = new IndexSearcher(reader);
+
+      Query query = new TermQuery(new Term("", ""));
+      searcher.search(query);
+
+   }
+
+   protected WorkspaceEntry getNewWs(String wsName, boolean isMultiDb, String dsName, String vsPath,
+      ContainerEntry entry) throws Exception
+   {
+
+      List params = new ArrayList();
+
+      params.add(new SimpleParameterEntry("sourceName", dsName));
+      params.add(new SimpleParameterEntry("db-type", "generic"));
+      params.add(new SimpleParameterEntry("multi-db", isMultiDb ? "true" : "false"));
+      params.add(new SimpleParameterEntry("update-storage", "true"));
+      params.add(new SimpleParameterEntry("max-buffer-size", "204800"));
+
+      if (entry.getParameterValue(JDBCWorkspaceDataContainer.DB_DIALECT) != null)
+      {
+         params.add(new SimpleParameterEntry(JDBCWorkspaceDataContainer.DB_DIALECT, entry
+            .getParameterValue(JDBCWorkspaceDataContainer.DB_DIALECT)));
+      }
+
+      String oldSwap = entry.getParameterValue("swap-directory");
+      String newSwap = oldSwap.substring(0, oldSwap.lastIndexOf('/')) + '/' + wsName;
+
+      params.add(new SimpleParameterEntry("swap-directory", newSwap));
+
+      ContainerEntry containerEntry =
+         new ContainerEntry("org.exoplatform.services.jcr.impl.storage.jdbc.JDBCWorkspaceDataContainer",
+            (ArrayList)params);
+      containerEntry.setParameters(params);
+
+      if (vsPath != null)
+      {
+
+         ArrayList<ValueStorageFilterEntry> vsparams = new ArrayList<ValueStorageFilterEntry>();
+         ValueStorageFilterEntry filterEntry = new ValueStorageFilterEntry();
+         filterEntry.setPropertyType("Binary");
+         vsparams.add(filterEntry);
+
+         ValueStorageEntry valueStorageEntry =
+            new ValueStorageEntry("org.exoplatform.services.jcr.impl.storage.value.fs.SimpleFileValueStorage", vsparams);
+         ArrayList<SimpleParameterEntry> spe = new ArrayList<SimpleParameterEntry>();
+         spe.add(new SimpleParameterEntry("path", vsPath));
+         valueStorageEntry.setId(IdGenerator.generate());
+         valueStorageEntry.setParameters(spe);
+         valueStorageEntry.setFilters(vsparams);
+
+         // containerEntry.setValueStorages();
+         containerEntry.setParameters(params);
+         ArrayList list = new ArrayList(1);
+         list.add(valueStorageEntry);
+
+         containerEntry.setValueStorages(list);
+
+      }
+
+      // Indexer
+      ArrayList qParams = new ArrayList();
+      qParams.add(new SimpleParameterEntry("indexDir", "target/temp/index/db1/ws" + IdGenerator.generate()));
+      QueryHandlerEntry qEntry =
+         new QueryHandlerEntry("org.exoplatform.services.jcr.impl.core.query.lucene.SearchIndex", qParams);
+
+      WorkspaceEntry workspaceEntry =
+         new WorkspaceEntry(wsName != null ? wsName : IdGenerator.generate(), "nt:unstructured");
+      workspaceEntry.setContainer(containerEntry);
+
+      ArrayList cacheParams = new ArrayList();
+
+      cacheParams.add(new SimpleParameterEntry("maxSize", "2000"));
+      cacheParams.add(new SimpleParameterEntry("liveTime", "20m"));
+      CacheEntry cacheEntry = new CacheEntry(cacheParams);
+      cacheEntry.setType("org.exoplatform.services.jcr.impl.dataflow.persistent.LinkedWorkspaceStorageCacheImpl");
+
+      workspaceEntry.setCache(cacheEntry);
+
+      workspaceEntry.setQueryHandler(qEntry);
+
+      LockManagerEntry lockManagerEntry = new LockManagerEntry();
+      lockManagerEntry.setTimeout(900000);
+      LockPersisterEntry persisterEntry = new LockPersisterEntry();
+      persisterEntry.setType("org.exoplatform.services.jcr.impl.core.lock.FileSystemLockPersister");
+      ArrayList lpParams = new ArrayList();
+      lpParams.add(new SimpleParameterEntry("path", "../temp/lock"));
+      persisterEntry.setParameters(lpParams);
+      lockManagerEntry.setPersister(persisterEntry);
+      workspaceEntry.setLockManager(lockManagerEntry);
+
+      // workspaceEntry
+      return workspaceEntry;
+   }
+
+   public void createNewDataSource(String dataSourceName) throws Exception
+   {
+
+      Properties properties = new Properties();
+
+      properties.setProperty("driverClassName", "org.hsqldb.jdbcDriver");
+      String newurl = "jdbc:hsqldb:file:target/temp/data/" + dataSourceName;
+
+      properties.setProperty("url", newurl);
+      properties.setProperty("username", "sa");
+      properties.setProperty("password", "");
+      DataSource bds = BasicDataSourceFactory.createDataSource(properties);
+      if (!newurl.contains("hsqldb"))
+      {
+         createDatabase(bds, dataSourceName);
+      }
+
+      new InitialContext().rebind(dataSourceName, bds);
+   }
+
+   private void createDatabase(DataSource ds, String dbName) throws SQLException
+   {
+      Connection connection = ds.getConnection();
+      PreparedStatement st = connection.prepareStatement("create database " + dbName);
+      st.executeQuery();
+   }
+
+}



More information about the exo-jcr-commits mailing list