Author: sascharodekamp
Date: Thu Dec 8 13:16:40 2011 New Revision: 1211864 URL: http://svn.apache.org/viewvc?rev=1211864&view=rev Log: New Tests and code refactoring Modified: ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/access/jackrabbit/ContentWriterJackrabbit.java ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/access/jackrabbit/JackrabbitRepositoryAccessor.java ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/loader/JCRFactory.java ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/loader/JCRFactoryUtil.java ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/loader/jackrabbit/JCRFactoryImpl.java ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/test/JackrabbitTests.java Modified: ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/access/jackrabbit/ContentWriterJackrabbit.java URL: http://svn.apache.org/viewvc/ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/access/jackrabbit/ContentWriterJackrabbit.java?rev=1211864&r1=1211863&r2=1211864&view=diff ============================================================================== --- ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/access/jackrabbit/ContentWriterJackrabbit.java (original) +++ ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/access/jackrabbit/ContentWriterJackrabbit.java Thu Dec 8 13:16:40 2011 @@ -4,6 +4,10 @@ import javax.jcr.ItemExistsException; import javax.jcr.Node; import javax.jcr.PathNotFoundException; import javax.jcr.RepositoryException; +import javax.jcr.lock.LockException; +import javax.jcr.nodetype.ConstraintViolationException; +import javax.jcr.nodetype.NoSuchNodeTypeException; +import javax.jcr.version.VersionException; import org.apache.jackrabbit.ocm.exception.ObjectContentManagerException; import org.apache.jackrabbit.ocm.manager.ObjectContentManager; @@ -56,57 +60,17 @@ public class ContentWriterJackrabbit imp return; } - // We have to check if the node structure (the sub nodes of the passed - // ORM Object) exist, otherwise they will be created. - String path = orm.getPath(); - String[] nodeStructure = path.split(ConstantsJackrabbit.NODEPATHDELIMITER); - Node parentNode = null; + // create all nodes in the node structure which do not exist yet try { - parentNode = this.ocm.getSession().getRootNode(); + createNodeStructure(orm.getPath(), orm.getClass().getAnnotation(org.apache.jackrabbit.ocm.mapper.impl.annotation.Node.class).jcrType()); + } catch (PathNotFoundException e) { + Debug.logError(e, "The new node could not be created: " + orm.getPath(), module); + return; } catch (RepositoryException e) { Debug.logError(e, "The new node could not be created: " + orm.getPath(), module); return; } - // Read a primary node type from the mapping annotation class - // TODO Check for a better solution because here we have a cross - // reference to the Jackrabbit ORM Package. - org.apache.jackrabbit.ocm.mapper.impl.annotation.Node annotationNode = orm.getClass().getAnnotation(org.apache.jackrabbit.ocm.mapper.impl.annotation.Node.class); - String primNodeType = annotationNode.jcrType(); - - // We loop only over the sub nodes. - for (int i = 0; i < (nodeStructure.length - 1); i++) { - String node = nodeStructure[i]; - if (UtilValidate.isEmpty(node)) { - continue; - } - - try { - if (parentNode.hasNode(node)) { - parentNode = parentNode.getNode(node); - versioningManager.checkOutContentObject(parentNode.getPath()); - } else { - versioningManager.checkOutContentObject(parentNode.getPath()); - - Node newNode = parentNode.addNode(node, primNodeType); - newNode.addMixin(ConstantsJackrabbit.MIXIN_VERSIONING); - if (!ConstantsJackrabbit.ROOTPATH.equals(parentNode.getPath())) { - newNode.setPrimaryType(parentNode.getPrimaryNodeType().getName()); - } - - versioningManager.addContentToCheckInList(newNode.getPath()); - parentNode = newNode; - } - } catch (PathNotFoundException e) { - Debug.logError(e, "The new node could not be created: " + orm.getPath(), module); - return; - } catch (RepositoryException e) { - Debug.logError(e, "The new node could not be created: " + orm.getPath(), module); - return; - } - - } - ocm.insert(orm); versioningManager.addContentToCheckInList(orm.getPath()); @@ -145,4 +109,95 @@ public class ContentWriterJackrabbit imp private void saveState() { versioningManager.checkInContentAndSaveState(); } + + /** + * Create the new Node structure. + * + * @param completeNodePath + * @param primaryNodeType + * a primary node type from the mapping annotation class, that + * will be used as primary type for the created nodes + * @throws PathNotFoundException + * @throws RepositoryException + */ + private void createNodeStructure(String completeNodePath, String primaryNodeType) throws PathNotFoundException, RepositoryException { + // We have to check if the node structure (the sub nodes of the passed + // ORM Object) exist, otherwise they will be created. + String[] nodeStructure = completeNodePath.split(ConstantsJackrabbit.NODEPATHDELIMITER); + Node parentNode = null; + try { + parentNode = this.ocm.getSession().getRootNode(); + } catch (RepositoryException e) { + Debug.logError(e, "The new node could not be created: " + completeNodePath, module); + return; + } + + // We loop only over the sub nodes. + for (int i = 0; i < (nodeStructure.length - 1); i++) { + String node = nodeStructure[i]; + if (UtilValidate.isEmpty(node)) { + continue; + } + + parentNode = createNewSubNodeIfNotExist(primaryNodeType, parentNode, node); + } + } + + /** + * Checks if the new node already exist, otherwise it will be created. + * + * @param primaryNodeType + * @param parentNode + * @param node + * @return + * @throws RepositoryException + * @throws PathNotFoundException + * @throws ItemExistsException + * @throws NoSuchNodeTypeException + * @throws LockException + * @throws VersionException + * @throws ConstraintViolationException + */ + private Node createNewSubNodeIfNotExist(String primaryNodeType, Node parentNode, String node) throws RepositoryException, PathNotFoundException, ItemExistsException, NoSuchNodeTypeException, LockException, VersionException, + ConstraintViolationException { + + if (parentNode.hasNode(node)) { + parentNode = parentNode.getNode(node); + versioningManager.checkOutContentObject(parentNode.getPath()); + } else { + versioningManager.checkOutContentObject(parentNode.getPath()); + + Node newNode = addNewNode(primaryNodeType, parentNode, node); + + versioningManager.addContentToCheckInList(newNode.getPath()); + parentNode = newNode; + } + + return parentNode; + } + + /** + * The method adds a new node to its parent, write the versioning mixin and + * set the primary node type. + * + * @param primaryNodeType + * @param parentNode + * @param node + * @return + * @throws ItemExistsException + * @throws PathNotFoundException + * @throws NoSuchNodeTypeException + * @throws LockException + * @throws VersionException + * @throws ConstraintViolationException + * @throws RepositoryException + */ + private Node addNewNode(String primaryNodeType, Node parentNode, String node) throws ItemExistsException, PathNotFoundException, NoSuchNodeTypeException, LockException, VersionException, ConstraintViolationException, RepositoryException { + Node newNode = parentNode.addNode(node, primaryNodeType); + newNode.addMixin(ConstantsJackrabbit.MIXIN_VERSIONING); + if (!ConstantsJackrabbit.ROOTPATH.equals(parentNode.getPath())) { + newNode.setPrimaryType(parentNode.getPrimaryNodeType().getName()); + } + return newNode; + } } Modified: ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/access/jackrabbit/JackrabbitRepositoryAccessor.java URL: http://svn.apache.org/viewvc/ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/access/jackrabbit/JackrabbitRepositoryAccessor.java?rev=1211864&r1=1211863&r2=1211864&view=diff ============================================================================== --- ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/access/jackrabbit/JackrabbitRepositoryAccessor.java (original) +++ ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/access/jackrabbit/JackrabbitRepositoryAccessor.java Thu Dec 8 13:16:40 2011 @@ -51,7 +51,7 @@ public class JackrabbitRepositoryAccesso this.session = session; - this.ocm = new ObjectContentManagerImpl(session, JCRFactoryImpl.getMapper()); + this.ocm = new ObjectContentManagerImpl(session, JCRFactoryImpl.getMapper()); return; } Modified: ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/loader/JCRFactory.java URL: http://svn.apache.org/viewvc/ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/loader/JCRFactory.java?rev=1211864&r1=1211863&r2=1211864&view=diff ============================================================================== --- ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/loader/JCRFactory.java (original) +++ ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/loader/JCRFactory.java Thu Dec 8 13:16:40 2011 @@ -54,13 +54,11 @@ public interface JCRFactory { */ public Session createSession() throws RepositoryException; - // public static final String WORKSPACE_NAME = - // PropsUtil.get(PropsKeys.JCR_WORKSPACE_NAME); - // - // public static final String NODE_DOCUMENTLIBRARY = - // PropsUtil.get(PropsKeys.JCR_NODE_DOCUMENTLIBRARY); - + /** + * Should return an instance of the repository implementation. + * + * @return + */ public Repository getInstance(); - public Repository getRepository(); } Modified: ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/loader/JCRFactoryUtil.java URL: http://svn.apache.org/viewvc/ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/loader/JCRFactoryUtil.java?rev=1211864&r1=1211863&r2=1211864&view=diff ============================================================================== --- ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/loader/JCRFactoryUtil.java (original) +++ ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/loader/JCRFactoryUtil.java Thu Dec 8 13:16:40 2011 @@ -18,7 +18,6 @@ *******************************************************************************/ package org.ofbiz.jcr.loader; -import javax.jcr.Repository; import javax.jcr.RepositoryException; import javax.jcr.Session; @@ -71,8 +70,4 @@ public class JCRFactoryUtil { return session; } - - public static Repository getRepository() { - return getJCRFactory().getRepository(); - } } Modified: ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/loader/jackrabbit/JCRFactoryImpl.java URL: http://svn.apache.org/viewvc/ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/loader/jackrabbit/JCRFactoryImpl.java?rev=1211864&r1=1211863&r2=1211864&view=diff ============================================================================== --- ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/loader/jackrabbit/JCRFactoryImpl.java (original) +++ ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/loader/jackrabbit/JCRFactoryImpl.java Thu Dec 8 13:16:40 2011 @@ -171,16 +171,6 @@ public class JCRFactoryImpl implements J */ @Override public Repository getInstance() { - return null; - } - - /* - * (non-Javadoc) - * - * @see org.ofbiz.jcr.JCRFactory#getRepository() - */ - @Override - public Repository getRepository() { return repository; } Modified: ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/test/JackrabbitTests.java URL: http://svn.apache.org/viewvc/ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/test/JackrabbitTests.java?rev=1211864&r1=1211863&r2=1211864&view=diff ============================================================================== --- ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/test/JackrabbitTests.java (original) +++ ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/test/JackrabbitTests.java Thu Dec 8 13:16:40 2011 @@ -25,16 +25,29 @@ import java.util.GregorianCalendar; import java.util.List; import java.util.Map; +import javax.jcr.RepositoryException; +import javax.jcr.Session; +import javax.jcr.query.QueryResult; + import javolution.util.FastMap; +import net.sf.json.JSONArray; +import org.apache.jackrabbit.ocm.manager.ObjectContentManager; +import org.apache.jackrabbit.ocm.manager.impl.ObjectContentManagerImpl; +import org.apache.jackrabbit.ocm.mapper.Mapper; import org.ofbiz.base.util.UtilMisc; import org.ofbiz.entity.GenericValue; +import org.ofbiz.jcr.access.ContentWriter; import org.ofbiz.jcr.access.JcrRepositoryAccessor; +import org.ofbiz.jcr.access.jackrabbit.ContentWriterJackrabbit; import org.ofbiz.jcr.access.jackrabbit.JackrabbitRepositoryAccessor; import org.ofbiz.jcr.api.JcrDataHelper; import org.ofbiz.jcr.api.JcrFileHelper; import org.ofbiz.jcr.api.jackrabbit.JackrabbitArticleHelper; import org.ofbiz.jcr.api.jackrabbit.JackrabbitFileHelper; +import org.ofbiz.jcr.loader.JCRFactory; +import org.ofbiz.jcr.loader.JCRFactoryUtil; +import org.ofbiz.jcr.loader.jackrabbit.JCRFactoryImpl; import org.ofbiz.jcr.orm.jackrabbit.JackrabbitArticle; import org.ofbiz.jcr.util.jackrabbit.JcrUtilJackrabbit; import org.ofbiz.service.ServiceUtil; @@ -63,6 +76,87 @@ public class JackrabbitTests extends OFB assertNotNull(repositoryAccess); } + /* + * Base Method Tests + */ + public void testFactoryGetMapper() { + assertNotNull(JCRFactoryImpl.getMapper()); + assertTrue(JCRFactoryImpl.getMapper() instanceof Mapper); + } + + public void testFactoryUtilGetJcrFactory() { + JCRFactory factory = JCRFactoryUtil.getJCRFactory(); + assertNotNull(factory); + assertTrue((factory instanceof JCRFactoryImpl)); + } + + public void testUtilGetSession() { + Session session = JCRFactoryUtil.getSession(); + assertNotNull(session); + assertTrue((session instanceof Session)); + } + + // + // Jackrabbit Accessor tests + // + + public void testAccessorConstructor() throws RepositoryException { + JcrRepositoryAccessor accessor = new JackrabbitRepositoryAccessor(userLogin); + + assertNotNull(accessor); + assertEquals("/", accessor.getSession().getRootNode().getPath()); + + accessor.closeAccess(); + } + + public void testAccessorDataTree() throws RepositoryException { + JcrRepositoryAccessor accessor = new JackrabbitRepositoryAccessor(userLogin); + + JSONArray array = accessor.getJsonDataTree(); + assertEquals(0, array.size()); // should be 0 because there are no + // entries in the repository yet + + accessor.closeAccess(); + } + + public void testAccessorFileTree() throws RepositoryException { + JcrRepositoryAccessor accessor = new JackrabbitRepositoryAccessor(userLogin); + + JSONArray array = accessor.getJsonFileTree(); + assertEquals(0, array.size()); // should be 0 because there are no + // entries in the repository yet + accessor.closeAccess(); + } + + public void testAccessorQuery() throws RepositoryException { + JcrRepositoryAccessor accessor = new JackrabbitRepositoryAccessor(userLogin); + QueryResult results = accessor.queryForRepositoryData("SELECT * FROM [rep:root]"); + + assertNotNull(results); + assertEquals(1, results.getNodes().getSize()); + + accessor.closeAccess(); + } + + // + // Content Writer + // + + public void testWriterConsturctor() { + + Session session = JCRFactoryUtil.getSession(); + ObjectContentManager ocm = new ObjectContentManagerImpl(session, JCRFactoryImpl.getMapper()); + ContentWriter writer = new ContentWriterJackrabbit(ocm); + + assertNotNull(writer); + + ocm.logout(); + } + + /* + * Functional Integration Tests + */ + public void testCrudArticleNode() throws Exception { // Create New Object JcrDataHelper helper = new JackrabbitArticleHelper(userLogin); @@ -154,7 +248,8 @@ public class JackrabbitTests extends OFB List<Map<String, String>> queryResult = helper.queryData("SELECT * FROM [nt:unstructured]"); - assertEquals(3, queryResult.size()); // the list should contain 3 result sets + assertEquals(3, queryResult.size()); // the list should contain 3 result + // sets assertEquals("/", queryResult.get(0).get("path")); assertEquals("/query", queryResult.get(1).get("path")); |
Free forum by Nabble | Edit this page |