/*
* Copyright (C) 2005-2007 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* As a special exception to the terms and conditions of version 2.0 of
* the GPL, you may redistribute this Program in connection with Free/Libre
* and Open Source Software ("FLOSS") applications as described in Alfresco's
* FLOSS exception. You should have recieved a copy of the text describing
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing"
*/
package org.alfresco.repo.node;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import javax.transaction.UserTransaction;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.repo.dictionary.DictionaryComponent;
import org.alfresco.repo.dictionary.DictionaryDAO;
import org.alfresco.repo.dictionary.M2Model;
import org.alfresco.repo.domain.hibernate.ChildAssocImpl;
import org.alfresco.repo.domain.hibernate.NodeImpl;
import org.alfresco.repo.node.db.NodeDaoService;
import org.alfresco.repo.node.integrity.IntegrityChecker;
import org.alfresco.repo.policy.JavaBehaviour;
import org.alfresco.repo.policy.PolicyComponent;
import org.alfresco.repo.security.authentication.AuthenticationComponent;
import org.alfresco.repo.transaction.AlfrescoTransactionSupport;
import org.alfresco.repo.transaction.RetryingTransactionHelper;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.service.cmr.dictionary.ClassDefinition;
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
import org.alfresco.service.cmr.dictionary.DictionaryException;
import org.alfresco.service.cmr.dictionary.DictionaryService;
import org.alfresco.service.cmr.dictionary.PropertyDefinition;
import org.alfresco.service.cmr.repository.AssociationExistsException;
import org.alfresco.service.cmr.repository.AssociationRef;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.ContentData;
import org.alfresco.service.cmr.repository.CyclicChildRelationshipException;
import org.alfresco.service.cmr.repository.DuplicateChildNodeNameException;
import org.alfresco.service.cmr.repository.InvalidNodeRefException;
import org.alfresco.service.cmr.repository.MLText;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.Path;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.repository.datatype.TypeConversionException;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.alfresco.service.namespace.RegexQNamePattern;
import org.alfresco.service.transaction.TransactionService;
import org.alfresco.util.BaseSpringTest;
import org.alfresco.util.GUID;
import org.alfresco.util.PropertyMap;
import org.hibernate.Session;
import org.springframework.context.ApplicationContext;
/**
* Provides a base set of tests of the various {@link org.alfresco.service.cmr.repository.NodeService}
* implementations.
*
* To test a specific incarnation of the service, the methods {@link #getStoreService()} and
* {@link #getNodeService()} must be implemented.
*
* @see #nodeService
* @see #rootNodeRef
* @see #buildNodeGraph()
*
* @author Derek Hulley
*/
@SuppressWarnings("unused") /* its just a test */
public abstract class BaseNodeServiceTest extends BaseSpringTest
{
public static final String NAMESPACE = "http://www.alfresco.org/test/BaseNodeServiceTest";
public static final String TEST_PREFIX = "test";
public static final QName TYPE_QNAME_TEST_CONTENT = QName.createQName(NAMESPACE, "content");
public static final QName TYPE_QNAME_TEST_MANY_PROPERTIES = QName.createQName(NAMESPACE, "many-properties");
public static final QName TYPE_QNAME_EXTENDED_CONTENT = QName.createQName(NAMESPACE, "extendedcontent");
public static final QName ASPECT_QNAME_TEST_TITLED = QName.createQName(NAMESPACE, "titled");
public static final QName ASPECT_QNAME_TEST_MARKER = QName.createQName(NAMESPACE, "marker");
public static final QName ASPECT_QNAME_TEST_MARKER2 = QName.createQName(NAMESPACE, "marker2");
public static final QName ASPECT_QNAME_MANDATORY = QName.createQName(NAMESPACE, "mandatoryaspect");
public static final QName ASPECT_QNAME_WITH_DEFAULT_VALUE = QName.createQName(NAMESPACE, "withDefaultValue");
public static final QName PROP_QNAME_TEST_TITLE = QName.createQName(NAMESPACE, "title");
public static final QName PROP_QNAME_TEST_CONTENT = QName.createQName(NAMESPACE, "content");
public static final QName PROP_QNAME_BOOLEAN_VALUE = QName.createQName(NAMESPACE, "booleanValue");
public static final QName PROP_QNAME_INTEGER_VALUE = QName.createQName(NAMESPACE, "integerValue");
public static final QName PROP_QNAME_LONG_VALUE = QName.createQName(NAMESPACE, "longValue");
public static final QName PROP_QNAME_FLOAT_VALUE = QName.createQName(NAMESPACE, "floatValue");
public static final QName PROP_QNAME_DOUBLE_VALUE = QName.createQName(NAMESPACE, "doubleValue");
public static final QName PROP_QNAME_STRING_VALUE = QName.createQName(NAMESPACE, "stringValue");
public static final QName PROP_QNAME_ML_TEXT_VALUE = QName.createQName(NAMESPACE, "mlTextValue");
public static final QName PROP_QNAME_DATE_VALUE = QName.createQName(NAMESPACE, "dateValue");
public static final QName PROP_QNAME_SERIALIZABLE_VALUE = QName.createQName(NAMESPACE, "serializableValue");
public static final QName PROP_QNAME_NODEREF_VALUE = QName.createQName(NAMESPACE, "nodeRefValue");
public static final QName PROP_QNAME_QNAME_VALUE = QName.createQName(NAMESPACE, "qnameValue");
public static final QName PROP_QNAME_CONTENT_VALUE = QName.createQName(NAMESPACE, "contentValue");
public static final QName PROP_QNAME_PATH_VALUE = QName.createQName(NAMESPACE, "pathValue");
public static final QName PROP_QNAME_CATEGORY_VALUE = QName.createQName(NAMESPACE, "categoryValue");
public static final QName PROP_QNAME_LOCALE_VALUE = QName.createQName(NAMESPACE, "localeValue");
public static final QName PROP_QNAME_NULL_VALUE = QName.createQName(NAMESPACE, "nullValue");
public static final QName PROP_QNAME_MULTI_VALUE = QName.createQName(NAMESPACE, "multiValue");
public static final QName PROP_QNAME_PROP1 = QName.createQName(NAMESPACE, "prop1");
public static final QName PROP_QNAME_PROP2 = QName.createQName(NAMESPACE, "prop2");
public static final QName ASSOC_TYPE_QNAME_TEST_CHILDREN = ContentModel.ASSOC_CHILDREN;
public static final QName ASSOC_TYPE_QNAME_TEST_CONTAINS = ContentModel.ASSOC_CONTAINS;
public static final QName ASSOC_TYPE_QNAME_TEST_NEXT = QName.createQName(NAMESPACE, "next");
public static final QName ASPECT_WITH_ASSOCIATIONS = QName.createQName(NAMESPACE, "withAssociations");
public static final QName ASSOC_ASPECT_CHILD_ASSOC = QName.createQName(NAMESPACE, "aspect-child-assoc");
public static final QName ASSOC_ASPECT_NORMAL_ASSOC = QName.createQName(NAMESPACE, "aspect-normal-assoc");
public static final QName TYPE_QNAME_TEST_MULTIPLE_TESTER = QName.createQName(NAMESPACE, "multiple-tester");
public static final QName PROP_QNAME_STRING_PROP_SINGLE = QName.createQName(NAMESPACE, "stringprop-single");
public static final QName PROP_QNAME_STRING_PROP_MULTIPLE = QName.createQName(NAMESPACE, "stringprop-multiple");
public static final QName PROP_QNAME_ANY_PROP_SINGLE = QName.createQName(NAMESPACE, "anyprop-single");
public static final QName PROP_QNAME_ANY_PROP_MULTIPLE = QName.createQName(NAMESPACE, "anyprop-multiple");
protected PolicyComponent policyComponent;
protected DictionaryService dictionaryService;
protected TransactionService transactionService;
protected RetryingTransactionHelper retryingTransactionHelper;
protected AuthenticationComponent authenticationComponent;
protected NodeDaoService nodeDaoService;
protected NodeService nodeService;
/** populated during setup */
protected NodeRef rootNodeRef;
private NodeRef cat;
@Override
protected void onSetUpInTransaction() throws Exception
{
super.onSetUpInTransaction();
transactionService = (TransactionService) applicationContext.getBean("transactionComponent");
retryingTransactionHelper = (RetryingTransactionHelper) applicationContext.getBean("retryingTransactionHelper");
policyComponent = (PolicyComponent) applicationContext.getBean("policyComponent");
authenticationComponent = (AuthenticationComponent) applicationContext.getBean("authenticationComponent");
authenticationComponent.setSystemUserAsCurrentUser();
DictionaryDAO dictionaryDao = (DictionaryDAO) applicationContext.getBean("dictionaryDAO");
// load the system model
ClassLoader cl = BaseNodeServiceTest.class.getClassLoader();
InputStream modelStream = cl.getResourceAsStream("alfresco/model/contentModel.xml");
assertNotNull(modelStream);
M2Model model = M2Model.createModel(modelStream);
dictionaryDao.putModel(model);
// load the test model
modelStream = cl.getResourceAsStream("org/alfresco/repo/node/BaseNodeServiceTest_model.xml");
assertNotNull(modelStream);
model = M2Model.createModel(modelStream);
dictionaryDao.putModel(model);
DictionaryComponent dictionary = new DictionaryComponent();
dictionary.setDictionaryDAO(dictionaryDao);
dictionaryService = loadModel(applicationContext);
nodeService = getNodeService();
// create a first store directly
StoreRef storeRef = nodeService.createStore(
StoreRef.PROTOCOL_WORKSPACE,
"Test_" + System.currentTimeMillis());
rootNodeRef = nodeService.getRootNode(storeRef);
StoreRef catStoreRef = nodeService.createStore(
StoreRef.PROTOCOL_WORKSPACE,
"Test_cat_" + System.currentTimeMillis());
NodeRef catRootNodeRef = nodeService.getRootNode(catStoreRef);
cat = nodeService.createNode(catRootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName("{namespace}cat"), ContentModel.TYPE_CATEGORY).getChildRef();
// downgrade integrity checks
IntegrityChecker.setWarnInTransaction();
}
@Override
protected void onTearDownInTransaction() throws Exception
{
try
{
authenticationComponent.clearCurrentSecurityContext();
}
catch (Throwable e)
{
// do nothing
}
super.onTearDownInTransaction();
}
/**
* Loads the test model required for building the node graphs
*/
public static DictionaryService loadModel(ApplicationContext applicationContext)
{
DictionaryDAO dictionaryDao = (DictionaryDAO) applicationContext.getBean("dictionaryDAO");
// load the system model
ClassLoader cl = BaseNodeServiceTest.class.getClassLoader();
InputStream modelStream = cl.getResourceAsStream("alfresco/model/contentModel.xml");
assertNotNull(modelStream);
M2Model model = M2Model.createModel(modelStream);
dictionaryDao.putModel(model);
// load the test model
modelStream = cl.getResourceAsStream("org/alfresco/repo/node/BaseNodeServiceTest_model.xml");
assertNotNull(modelStream);
model = M2Model.createModel(modelStream);
dictionaryDao.putModel(model);
DictionaryComponent dictionary = new DictionaryComponent();
dictionary.setDictionaryDAO(dictionaryDao);
// done
return dictionary;
}
/**
* Usually just implemented by fetching the bean directly from the bean factory,
* for example:
*
*
* @return Returns the implementation of NodeService to be
* used for this test. It must have transaction demarcation.
*/
protected abstract NodeService getNodeService();
public void testSetUp() throws Exception
{
assertNotNull("StoreService not set", nodeService);
assertNotNull("NodeService not set", nodeService);
assertNotNull("rootNodeRef not created", rootNodeRef);
}
/**
* @see #buildNodeGraph(NodeService, NodeRef)
*/
public Map buildNodeGraph() throws Exception
{
return BaseNodeServiceTest.buildNodeGraph(nodeService, rootNodeRef);
}
/**
* Builds a graph of child associations as follows:
*
Apart from the root node having the root aspect, node 6 (n6) also has the
* root aspect.
*
n3 has properties animal = monkey and
* reference = n2.toString().
*
All nodes are of type {@link ContentModel#TYPE_CONTAINER container}
* with the exception of n8, which is of type {@link #TYPE_QNAME_TEST_CONTENT test:content}
*
*
* The namespace URI for all associations is {@link BaseNodeServiceTest#NAMESPACE}.
*
* The naming convention is:
*
* n2_p_n5
* n4_n5
* where
* n5 is the node number of the node
* n2 is the primary parent node number
* n4 is any other non-primary parent
*
*
* The session is flushed to ensure that persistence occurs correctly. It is
* cleared to ensure that fetches against the created data are correct.
*
* @return Returns a map ChildAssocRef instances keyed by qualified assoc name
*/
public static Map buildNodeGraph(
NodeService nodeService,
NodeRef rootNodeRef) throws Exception
{
String ns = BaseNodeServiceTest.NAMESPACE;
QName qname = null;
ChildAssociationRef assoc = null;
Map properties = new HashMap();
Map ret = new HashMap(13);
// LEVEL 0
// LEVEL 1
qname = QName.createQName(ns, "root_p_n1");
assoc = nodeService.createNode(rootNodeRef, ASSOC_TYPE_QNAME_TEST_CHILDREN, qname, ContentModel.TYPE_CONTAINER);
ret.put(qname, assoc);
NodeRef n1 = assoc.getChildRef();
qname = QName.createQName(ns, "root_p_n2");
assoc = nodeService.createNode(rootNodeRef, ASSOC_TYPE_QNAME_TEST_CHILDREN, qname, ContentModel.TYPE_CONTAINER);
ret.put(qname, assoc);
NodeRef n2 = assoc.getChildRef();
// LEVEL 2
properties.clear();
properties.put(QName.createQName(ns, "animal"), "monkey");
properties.put(QName.createQName(ns, "UPPERANIMAL"), "MONKEY");
properties.put(QName.createQName(ns, "reference"), n2.toString());
properties.put(QName.createQName(ns, "text1"), "bun");
properties.put(QName.createQName(ns, "text2"), "cake");
properties.put(QName.createQName(ns, "text3"), "biscuit");
properties.put(QName.createQName(ns, "text12"), "bun, cake");
properties.put(QName.createQName(ns, "text13"), "bun, biscuit");
properties.put(QName.createQName(ns, "text23"), "cake, biscuit");
properties.put(QName.createQName(ns, "text123"), "bun, cake, biscuit");
ArrayList slist = new ArrayList();
slist.add("first");
slist.add("second");
slist.add("third");
properties.put(QName.createQName(ns, "mvp"), slist);
ArrayList ilist = new ArrayList();
ilist.add(new Integer(1));
ilist.add(new Integer(2));
ilist.add(new Integer(3));
properties.put(QName.createQName(ns, "mvi"), ilist);
qname = QName.createQName(ns, "n1_p_n3");
assoc = nodeService.createNode(n1, ASSOC_TYPE_QNAME_TEST_CHILDREN, qname, ContentModel.TYPE_CONTAINER, properties);
ret.put(qname, assoc);
NodeRef n3 = assoc.getChildRef();
qname = QName.createQName(ns, "n2_p_n4");
assoc = nodeService.createNode(n2, ASSOC_TYPE_QNAME_TEST_CHILDREN, qname, ContentModel.TYPE_CONTAINER);
ret.put(qname, assoc);
NodeRef n4 = assoc.getChildRef();
qname = QName.createQName(ns, "n1_n4");
assoc = nodeService.addChild(n1, n4, ASSOC_TYPE_QNAME_TEST_CHILDREN, qname);
ret.put(qname, assoc);
qname = QName.createQName(ns, "n2_p_n5");
assoc = nodeService.createNode(n2, ASSOC_TYPE_QNAME_TEST_CHILDREN, qname, ContentModel.TYPE_CONTAINER);
ret.put(qname, assoc);
NodeRef n5 = assoc.getChildRef();
// LEVEL 3
qname = QName.createQName(ns, "n3_p_n6");
assoc = nodeService.createNode(n3, ASSOC_TYPE_QNAME_TEST_CHILDREN, qname, ContentModel.TYPE_CONTAINER);
ret.put(qname, assoc);
NodeRef n6 = assoc.getChildRef();
nodeService.addAspect(n6,
ContentModel.ASPECT_ROOT,
Collections.emptyMap());
qname = QName.createQName(ns, "n4_n6");
assoc = nodeService.addChild(n4, n6, ASSOC_TYPE_QNAME_TEST_CHILDREN, qname);
ret.put(qname, assoc);
qname = QName.createQName(ns, "n5_p_n7");
assoc = nodeService.createNode(n5, ASSOC_TYPE_QNAME_TEST_CHILDREN, qname, ContentModel.TYPE_CONTAINER);
ret.put(qname, assoc);
NodeRef n7 = assoc.getChildRef();
// LEVEL 4
properties.clear();
properties.put(PROP_QNAME_TEST_CONTENT, new ContentData(null, MimetypeMap.MIMETYPE_TEXT_PLAIN, 0L, null));
properties.put(PROP_QNAME_TEST_TITLE, "node8");
qname = QName.createQName(ns, "n6_p_n8");
assoc = nodeService.createNode(n6, ASSOC_TYPE_QNAME_TEST_CHILDREN, qname, TYPE_QNAME_TEST_CONTENT, properties);
ret.put(qname, assoc);
NodeRef n8 = assoc.getChildRef();
qname = QName.createQName(ns, "n7_n8");
assoc = nodeService.addChild(n7, n8, ASSOC_TYPE_QNAME_TEST_CHILDREN, qname);
ret.put(qname, assoc);
// // flush and clear
// getSession().flush();
// getSession().clear();
// done
return ret;
}
private int countNodesByReference(NodeRef nodeRef)
{
String query =
"select count(node.uuid)" +
" from " +
NodeImpl.class.getName() + " node" +
" where" +
" node.uuid = ? and" +
" node.store.key.protocol = ? and" +
" node.store.key.identifier = ?";
Session session = getSession();
List results = session.createQuery(query)
.setString(0, nodeRef.getId())
.setString(1, nodeRef.getStoreRef().getProtocol())
.setString(2, nodeRef.getStoreRef().getIdentifier())
.list();
Long count = (Long) results.get(0);
return count.intValue();
}
/**
* @return Returns a reference to the created store
*/
private StoreRef createStore() throws Exception
{
StoreRef storeRef = nodeService.createStore(
StoreRef.PROTOCOL_WORKSPACE,
getName() + "_" + System.nanoTime());
assertNotNull("No reference returned", storeRef);
// done
return storeRef;
}
public void testCreateStore() throws Exception
{
StoreRef storeRef = createStore();
// check that it exists
assertTrue("NodeService reports that store doesn't exist", nodeService.exists(storeRef));
// get the root node
NodeRef storeRootNode = nodeService.getRootNode(storeRef);
// make sure that it has the root aspect
boolean isRoot = nodeService.hasAspect(storeRootNode, ContentModel.ASPECT_ROOT);
assertTrue("Root node of store does not have root aspect", isRoot);
// and is of the correct type
QName rootType = nodeService.getType(storeRootNode);
assertEquals("Store root node of incorrect type", ContentModel.TYPE_STOREROOT, rootType);
}
public void testGetStores() throws Exception
{
StoreRef storeRef = createStore();
// get all stores
List storeRefs = nodeService.getStores();
// check that the store ref is present
assertTrue("New store not present is list of stores", storeRefs.contains(storeRef));
}
public void testExists() throws Exception
{
StoreRef storeRef = createStore();
boolean exists = nodeService.exists(storeRef);
assertEquals("Exists failed", true, exists);
// create bogus ref
StoreRef bogusRef = new StoreRef("What", "the");
exists = nodeService.exists(bogusRef);
assertEquals("Exists failed", false, exists);
}
public void testGetRootNode() throws Exception
{
StoreRef storeRef = createStore();
// get the root node
NodeRef rootNodeRef = nodeService.getRootNode(storeRef);
assertNotNull("No root node reference returned", rootNodeRef);
// get the root node again
NodeRef rootNodeRefCheck = nodeService.getRootNode(storeRef);
assertEquals("Root nodes returned different refs", rootNodeRef, rootNodeRefCheck);
}
public void testCreateNode() throws Exception
{
ChildAssociationRef assocRef = nodeService.createNode(rootNodeRef,
ASSOC_TYPE_QNAME_TEST_CHILDREN,
QName.createQName("pathA"),
ContentModel.TYPE_CONTAINER);
assertEquals("Assoc type qname not set", ASSOC_TYPE_QNAME_TEST_CHILDREN, assocRef.getTypeQName());
assertEquals("Assoc qname not set", QName.createQName("pathA"), assocRef.getQName());
NodeRef childRef = assocRef.getChildRef();
QName checkType = nodeService.getType(childRef);
assertEquals("Child node type incorrect", ContentModel.TYPE_CONTAINER, checkType);
}
/**
* Tests node creation with a pre-determined {@link ContentModel#PROP_NODE_UUID uuid}.
*/
public void testCreateNodeWithId() throws Exception
{
String uuid = GUID.generate();
// create a node with an explicit UUID
Map properties = new HashMap(5);
properties.put(ContentModel.PROP_NODE_UUID, uuid);
ChildAssociationRef assocRef = nodeService.createNode(
rootNodeRef,
ASSOC_TYPE_QNAME_TEST_CHILDREN,
QName.createQName("pathA"),
ContentModel.TYPE_CONTAINER,
properties);
// check it
NodeRef expectedNodeRef = new NodeRef(rootNodeRef.getStoreRef(), uuid);
NodeRef checkNodeRef = assocRef.getChildRef();
assertEquals("Failed to create node with a chosen ID", expectedNodeRef, checkNodeRef);
}
public void testGetType() throws Exception
{
ChildAssociationRef assocRef = nodeService.createNode(
rootNodeRef,
ASSOC_TYPE_QNAME_TEST_CHILDREN,
QName.createQName("pathA"),
ContentModel.TYPE_CONTAINER);
NodeRef nodeRef = assocRef.getChildRef();
// get the type
QName type = nodeService.getType(nodeRef);
assertEquals("Type mismatch", ContentModel.TYPE_CONTAINER, type);
}
public void testSetType() throws Exception
{
NodeRef nodeRef = nodeService.createNode(
rootNodeRef,
ASSOC_TYPE_QNAME_TEST_CHILDREN,
QName.createQName("setTypeTest"),
TYPE_QNAME_TEST_CONTENT).getChildRef();
assertEquals(TYPE_QNAME_TEST_CONTENT, this.nodeService.getType(nodeRef));
// Now change the type
this.nodeService.setType(nodeRef, TYPE_QNAME_EXTENDED_CONTENT);
assertEquals(TYPE_QNAME_EXTENDED_CONTENT, this.nodeService.getType(nodeRef));
}
/**
* Fills the given property map with some values according to the property definitions on the given class
*/
protected void fillProperties(QName qname, Map properties)
{
ClassDefinition classDef = dictionaryService.getClass(qname);
if (classDef == null)
{
throw new RuntimeException("No such class: " + qname);
}
Map propertyDefs = classDef.getProperties();
// make up a property value for each property
for (Map.Entry entry : propertyDefs.entrySet())
{
QName propertyQName = entry.getKey();
QName propertyTypeQName = entry.getValue().getDataType().getName();
// Get the property type
Serializable value = null;
if (propertyTypeQName.equals(DataTypeDefinition.CONTENT))
{
value = new ContentData(null, MimetypeMap.EXTENSION_BINARY, 0L, "UTF-8");
}
else
{
value = new Long(System.currentTimeMillis());
}
// add it
properties.put(propertyQName, value);
}
}
/**
* Checks that aspects can be added, removed and queried. Failure to detect
* inadequate properties is also checked.
*/
public void testAspects() throws Exception
{
// create a regular base node
ChildAssociationRef assocRef = nodeService.createNode(
rootNodeRef,
ASSOC_TYPE_QNAME_TEST_CHILDREN,
QName.createQName(BaseNodeServiceTest.NAMESPACE, "test-container"),
ContentModel.TYPE_CONTAINER);
NodeRef nodeRef = assocRef.getChildRef();
// add the titled aspect to the node, but don't supply any properties
Map properties = new HashMap(20);
nodeService.addAspect(nodeRef, BaseNodeServiceTest.ASPECT_QNAME_TEST_TITLED, properties);
// get the properties required for the aspect
fillProperties(BaseNodeServiceTest.ASPECT_QNAME_TEST_TITLED, properties);
// get the node properties before
Map propertiesBefore = nodeService.getProperties(nodeRef);
// add the aspect
nodeService.addAspect(nodeRef, BaseNodeServiceTest.ASPECT_QNAME_TEST_TITLED, properties);
// get the properties after and check
Map propertiesAfter = nodeService.getProperties(nodeRef);
assertEquals("Aspect properties not added",
propertiesBefore.size() + 2,
propertiesAfter.size());
// check that we know that the aspect is present
Set aspects = nodeService.getAspects(nodeRef);
assertTrue("Titled aspect not present",
aspects.contains(BaseNodeServiceTest.ASPECT_QNAME_TEST_TITLED));
// check that hasAspect works
boolean hasAspect = nodeService.hasAspect(nodeRef, BaseNodeServiceTest.ASPECT_QNAME_TEST_TITLED);
assertTrue("Aspect not confirmed to be on node", hasAspect);
// remove the aspect
nodeService.removeAspect(nodeRef, BaseNodeServiceTest.ASPECT_QNAME_TEST_TITLED);
hasAspect = nodeService.hasAspect(nodeRef, BaseNodeServiceTest.ASPECT_QNAME_TEST_TITLED);
assertFalse("Aspect not removed from node", hasAspect);
// check that the associated properties were removed
propertiesAfter = nodeService.getProperties(nodeRef);
assertEquals("Aspect properties not removed",
propertiesBefore.size(),
propertiesAfter.size());
}
public void testAspectRemoval() throws Exception
{
// Create a node to add the aspect to
NodeRef sourceNodeRef = nodeService.createNode(
rootNodeRef,
ASSOC_TYPE_QNAME_TEST_CHILDREN,
QName.createQName(BaseNodeServiceTest.NAMESPACE, "testAspectRemoval-source"),
ContentModel.TYPE_CONTAINER).getChildRef();
// Create a target for the associations
NodeRef targetNodeRef = nodeService.createNode(
rootNodeRef,
ASSOC_TYPE_QNAME_TEST_CHILDREN,
QName.createQName(BaseNodeServiceTest.NAMESPACE, "testAspectRemoval-target"),
ContentModel.TYPE_CONTAINER).getChildRef();
// Add the aspect to the source
nodeService.addAspect(sourceNodeRef, ASPECT_WITH_ASSOCIATIONS, null);
// Make the associations
nodeService.addChild(
sourceNodeRef,
targetNodeRef,
ASSOC_ASPECT_CHILD_ASSOC,
QName.createQName(NAMESPACE, "aspect-child"));
nodeService.createAssociation(sourceNodeRef, targetNodeRef, ASSOC_ASPECT_NORMAL_ASSOC);
// Check that the correct associations are present
assertEquals("Expected exactly one child",
1, nodeService.getChildAssocs(sourceNodeRef).size());
assertEquals("Expected exactly one target",
1, nodeService.getTargetAssocs(sourceNodeRef, RegexQNamePattern.MATCH_ALL).size());
// Now remove the aspect
nodeService.removeAspect(sourceNodeRef, ASPECT_WITH_ASSOCIATIONS);
// Check that the associations were removed
assertEquals("Expected exactly one child",
0, nodeService.getChildAssocs(sourceNodeRef).size());
assertEquals("Expected exactly one target",
0, nodeService.getTargetAssocs(sourceNodeRef, RegexQNamePattern.MATCH_ALL).size());
}
public void testCreateNodeNoProperties() throws Exception
{
// flush to ensure that the pure JDBC query will work
ChildAssociationRef assocRef = nodeService.createNode(rootNodeRef,
ASSOC_TYPE_QNAME_TEST_CHILDREN,
QName.createQName("path1"),
ContentModel.TYPE_CONTAINER);
NodeRef nodeRef = assocRef.getChildRef();
// count the nodes with the given id
int count = countNodesByReference(nodeRef);
assertEquals("Unexpected number of nodes present", 1, count);
}
/**
* @see #ASPECT_QNAME_TEST_TITLED
*/
public void testCreateNodeWithProperties() throws Exception
{
Map properties = new HashMap(5);
// fill properties
fillProperties(TYPE_QNAME_TEST_CONTENT, properties);
fillProperties(ASPECT_QNAME_TEST_TITLED, properties);
// create node for real
ChildAssociationRef assocRef = nodeService.createNode(
rootNodeRef,
ASSOC_TYPE_QNAME_TEST_CHILDREN,
QName.createQName("MyContent"),
TYPE_QNAME_TEST_CONTENT,
properties);
NodeRef nodeRef = assocRef.getChildRef();
// check that the titled aspect is present
assertTrue("Titled aspect not present",
nodeService.hasAspect(nodeRef, ASPECT_QNAME_TEST_TITLED));
}
public void testCascadeDelete() throws Exception
{
// build the node and commit the node graph
Map assocRefs = buildNodeGraph(nodeService, rootNodeRef);
NodeRef n3Ref = assocRefs.get(QName.createQName(BaseNodeServiceTest.NAMESPACE, "n1_p_n3")).getChildRef();
NodeRef n4Ref = assocRefs.get(QName.createQName(BaseNodeServiceTest.NAMESPACE, "n2_p_n4")).getChildRef();
NodeRef n6Ref = assocRefs.get(QName.createQName(BaseNodeServiceTest.NAMESPACE, "n3_p_n6")).getChildRef();
NodeRef n7Ref = assocRefs.get(QName.createQName(BaseNodeServiceTest.NAMESPACE, "n5_p_n7")).getChildRef();
NodeRef n8Ref = assocRefs.get(QName.createQName(BaseNodeServiceTest.NAMESPACE, "n6_p_n8")).getChildRef();
// control checks
assertEquals("n6 not present", 1, countNodesByReference(n6Ref));
assertEquals("n8 not present", 1, countNodesByReference(n8Ref));
assertTrue("n8 exists failure", nodeService.exists(n8Ref));
assertEquals("n6 primary parent association not present on n3", 1, countChildrenOfNode(n3Ref));
assertEquals("n6 secondary parent association not present on n4", 1, countChildrenOfNode(n4Ref));
assertEquals("n8 secondary parent association not present on n7", 1, countChildrenOfNode(n7Ref));
// delete n6
nodeService.deleteNode(n6Ref);
// ensure that we can't see cascaded nodes in-transaction
assertFalse("n8 not cascade deleted in-transaction", nodeService.exists(n8Ref));
// commit to check
setComplete();
endTransaction();
assertEquals("n6 not directly deleted", 0, countNodesByReference(n6Ref));
assertEquals("n8 not cascade deleted", 0, countNodesByReference(n8Ref));
assertEquals("n6 primary parent association not removed from n3", 0, countChildrenOfNode(n3Ref));
assertEquals("n6 secondary parent association not removed from n4", 0, countChildrenOfNode(n4Ref));
assertEquals("n8 secondary parent association not removed from n7", 0, countChildrenOfNode(n7Ref));
}
public static class BadOnDeleteNodePolicy implements
NodeServicePolicies.OnDeleteNodePolicy,
NodeServicePolicies.BeforeDeleteNodePolicy
{
private NodeService nodeService;
private List deletedNodeRefs;
public BadOnDeleteNodePolicy(NodeService nodeService, List deletedNodeRefs)
{
this.nodeService = nodeService;
this.deletedNodeRefs = deletedNodeRefs;
}
public void beforeDeleteNode(NodeRef nodeRef)
{
// add a new child to the child, i.e. just before it is deleted
ChildAssociationRef assocRef = nodeService.createNode(
nodeRef,
ASSOC_TYPE_QNAME_TEST_CHILDREN,
QName.createQName("pre-delete new child"),
ContentModel.TYPE_CONTAINER);
// set some child node properties
nodeService.setProperty(nodeRef, PROP_QNAME_BOOLEAN_VALUE, "true");
// add an aspect to the child
nodeService.addAspect(nodeRef, ASPECT_QNAME_TEST_TITLED, null);
}
public void onDeleteNode(ChildAssociationRef childAssocRef, boolean isArchivedNode)
{
// add the child to the list
deletedNodeRefs.add(childAssocRef.getChildRef());
// now perform some nasties on the node's parent, i.e. add a new child
NodeRef parentRef = childAssocRef.getParentRef();
NodeRef childRef = childAssocRef.getChildRef();
ChildAssociationRef assocRef = nodeService.createNode(
parentRef,
ASSOC_TYPE_QNAME_TEST_CHILDREN,
QName.createQName("post-delete new child"),
ContentModel.TYPE_CONTAINER);
}
}
public void testDelete() throws Exception
{
final List deletedNodeRefs = new ArrayList(5);
NodeServicePolicies.OnDeleteNodePolicy policy = new BadOnDeleteNodePolicy(nodeService, deletedNodeRefs);
// bind to listen to the deletion of a node
policyComponent.bindClassBehaviour(
QName.createQName(NamespaceService.ALFRESCO_URI, "onDeleteNode"),
policy,
new JavaBehaviour(policy, "onDeleteNode"));
// build the node and commit the node graph
Map assocRefs = buildNodeGraph(nodeService, rootNodeRef);
NodeRef n1Ref = assocRefs.get(QName.createQName(BaseNodeServiceTest.NAMESPACE, "root_p_n1")).getChildRef();
NodeRef n3Ref = assocRefs.get(QName.createQName(BaseNodeServiceTest.NAMESPACE, "n1_p_n3")).getChildRef();
NodeRef n4Ref = assocRefs.get(QName.createQName(BaseNodeServiceTest.NAMESPACE, "n2_p_n4")).getChildRef();
NodeRef n6Ref = assocRefs.get(QName.createQName(BaseNodeServiceTest.NAMESPACE, "n3_p_n6")).getChildRef();
NodeRef n8Ref = assocRefs.get(QName.createQName(BaseNodeServiceTest.NAMESPACE, "n6_p_n8")).getChildRef();
// delete n1
nodeService.deleteNode(n1Ref);
assertEquals("Node not directly deleted", 0, countNodesByReference(n1Ref));
assertEquals("Node not cascade deleted", 0, countNodesByReference(n3Ref));
assertEquals("Node incorrectly cascade deleted", 1, countNodesByReference(n4Ref));
assertEquals("Node not cascade deleted", 0, countNodesByReference(n6Ref));
assertEquals("Node not cascade deleted", 0, countNodesByReference(n8Ref));
// commit to check
setComplete();
endTransaction();
}
private int countChildrenOfNode(NodeRef nodeRef)
{
String query =
"select childAssoc" +
" from " +
ChildAssocImpl.class.getName() + " childAssoc" +
" join childAssoc.parent node" +
" where node.uuid = ? and node.store.key.protocol = ? and node.store.key.identifier = ?";
Session session = getSession();
List results = session.createQuery(query)
.setString(0, nodeRef.getId())
.setString(1, nodeRef.getStoreRef().getProtocol())
.setString(2, nodeRef.getStoreRef().getIdentifier())
.list();
int count = results.size();
return count;
}
public void testAddBogusChild() throws Exception
{
// create a bogus reference
NodeRef bogusChildRef = new NodeRef(rootNodeRef.getStoreRef(), "BOGUS");
try
{
nodeService.addChild(rootNodeRef, bogusChildRef, ASSOC_TYPE_QNAME_TEST_CHILDREN, QName.createQName("BOGUS_PATH"));
fail("Failed to detect invalid child node reference");
}
catch (InvalidNodeRefException e)
{
// expected
}
}
public void testAddChild() throws Exception
{
NodeRef childNodeRef1 = nodeService.createNode(
rootNodeRef,
ASSOC_TYPE_QNAME_TEST_CHILDREN,
QName.createQName("C1"),
ContentModel.TYPE_CONTAINER).getChildRef();
int count = countChildrenOfNode(rootNodeRef);
assertEquals("Root children count incorrect", 1, count);
NodeRef childNodeRef2 = nodeService.createNode(
childNodeRef1,
ASSOC_TYPE_QNAME_TEST_CHILDREN,
QName.createQName("C2"),
ContentModel.TYPE_CONTAINER).getChildRef();
count = countChildrenOfNode(rootNodeRef);
assertEquals("Root children count incorrect", 1, count);
// associate the two nodes
nodeService.addChild(
rootNodeRef,
childNodeRef2,
ASSOC_TYPE_QNAME_TEST_CHILDREN,
QName.createQName("pathB"));
// there should now be 2 child assocs on the root
int countAfter = countChildrenOfNode(rootNodeRef);
assertEquals("Root children count incorrect", 2, countAfter);
// now attempt to create a cyclical relationship
try
{
nodeService.addChild(
childNodeRef1,
rootNodeRef,
ASSOC_TYPE_QNAME_TEST_CHILDREN,
QName.createQName("backToRoot"));
fail("Failed to detect cyclic child relationship during addition of child");
}
catch (CyclicChildRelationshipException e)
{
// expected
}
}
public void testRemoveSpecificChild() throws Exception
{
ChildAssociationRef pathPrimaryRef = nodeService.createNode(
rootNodeRef,
ASSOC_TYPE_QNAME_TEST_CHILDREN,
QName.createQName("parent_child"),
ContentModel.TYPE_CONTAINER);
NodeRef parentRef = pathPrimaryRef.getParentRef();
ChildAssociationRef pathARef = nodeService.createNode(
parentRef,
ASSOC_TYPE_QNAME_TEST_CHILDREN,
QName.createQName("pathA"),
ContentModel.TYPE_CONTAINER);
ChildAssociationRef pathBRef = nodeService.addChild(
parentRef,
pathARef.getChildRef(),
ASSOC_TYPE_QNAME_TEST_CHILDREN,
QName.createQName("pathB"));
ChildAssociationRef pathCRef = nodeService.addChild(
parentRef,
pathARef.getChildRef(),
ASSOC_TYPE_QNAME_TEST_CHILDREN,
QName.createQName("pathC"));
// remove the path B association
boolean removedB = nodeService.removeChildAssociation(pathBRef);
assertTrue("Association was not removed", removedB);
removedB = nodeService.removeChildAssociation(pathBRef);
assertFalse("Non-existent association was apparently removed", removedB);
// remove the path C association
boolean removedC = nodeService.removeChildAssociation(pathCRef);
assertTrue("Association was not removed", removedC);
removedC = nodeService.removeSeconaryChildAssociation(pathCRef);
assertFalse("Non-existent association was apparently removed", removedC);
// Now verify that primary associations are caught
try
{
nodeService.removeSeconaryChildAssociation(pathPrimaryRef);
fail("Primary association not detected");
}
catch (IllegalArgumentException e)
{
// Expected
}
}
public void testRemoveChildByRef() throws Exception
{
NodeRef parentRef = nodeService.createNode(
rootNodeRef,
ASSOC_TYPE_QNAME_TEST_CHILDREN,
QName.createQName("parent_child"),
ContentModel.TYPE_CONTAINER).getChildRef();
ChildAssociationRef pathARef = nodeService.createNode(
parentRef,
ASSOC_TYPE_QNAME_TEST_CHILDREN,
QName.createQName("pathA"),
ContentModel.TYPE_CONTAINER);
NodeRef childARef = pathARef.getChildRef();
AssociationRef pathDRef = nodeService.createAssociation(
parentRef, childARef, ASSOC_TYPE_QNAME_TEST_NEXT);
// remove the child - this must cascade
nodeService.removeChild(parentRef, childARef);
assertFalse("Primary child not deleted", nodeService.exists(childARef));
assertEquals("Child assocs not removed",
0, nodeService.getChildAssocs(
parentRef,
ASSOC_TYPE_QNAME_TEST_CHILDREN,
new RegexQNamePattern(".*", "path*")).size());
assertEquals("Node assoc not removed",
0, nodeService.getTargetAssocs(parentRef, RegexQNamePattern.MATCH_ALL).size());
}
public enum TestEnum
{
TEST_ONE,
TEST_TWO
}
public void testProperties() throws Exception
{
// create a node to play with
ChildAssociationRef assocRef = nodeService.createNode(
rootNodeRef,
ASSOC_TYPE_QNAME_TEST_CHILDREN,
QName.createQName("playThing"),
ContentModel.TYPE_CONTAINER);
NodeRef nodeRef = assocRef.getChildRef();
QName qnameProperty1 = QName.createQName("PROPERTY1");
String valueProperty1 = "VALUE1";
QName qnameProperty2 = QName.createQName("PROPERTY2");
String valueProperty2 = "VALUE2";
QName qnameProperty3 = QName.createQName("PROPERTY3");
QName qnameProperty4 = QName.createQName("PROPERTY4");
Map properties = new HashMap(5);
properties.put(qnameProperty1, valueProperty1);
// add some properties to the root node
nodeService.setProperties(nodeRef, properties);
// set a single property
nodeService.setProperty(nodeRef, qnameProperty2, valueProperty2);
// set a null property
nodeService.setProperty(nodeRef, qnameProperty3, null);
// set an enum property
nodeService.setProperty(nodeRef, qnameProperty4, TestEnum.TEST_ONE);
// force a flush
getSession().flush();
getSession().clear();
// now get them back
Map checkMap = nodeService.getProperties(nodeRef);
assertNotNull("Properties were not set/retrieved", checkMap);
assertNotNull("Name property not set automatically", checkMap.get(ContentModel.PROP_NAME));
assertEquals("Name property to set to ID of node", nodeRef.getId(), checkMap.get(ContentModel.PROP_NAME));
assertEquals("Property value incorrect", valueProperty1, checkMap.get(qnameProperty1));
assertEquals("Property value incorrect", valueProperty2, checkMap.get(qnameProperty2));
assertTrue("Null property not persisted", checkMap.containsKey(qnameProperty3));
assertNull("Null value not persisted correctly", checkMap.get(qnameProperty3));
assertEquals("Enum property not retrieved", TestEnum.TEST_ONE, checkMap.get(qnameProperty4));
// get a single property direct from the node
Serializable valueCheck = nodeService.getProperty(nodeRef, qnameProperty2);
assertNotNull("Property value not set", valueCheck);
assertEquals("Property value incorrect", "VALUE2", valueCheck);
// Remove a property
nodeService.removeProperty(nodeRef, qnameProperty2);
valueCheck = nodeService.getProperty(nodeRef, qnameProperty2);
assertNull("Property not removed", valueCheck);
// set the property value to null
try
{
nodeService.setProperty(nodeRef, qnameProperty2, null);
}
catch (IllegalArgumentException e)
{
fail("Null property values are allowed");
}
// try setting null value as part of complete set
try
{
properties = nodeService.getProperties(nodeRef);
properties.put(qnameProperty1, null);
nodeService.setProperties(nodeRef, properties);
}
catch (IllegalArgumentException e)
{
fail("Null property values are allowed in the map");
}
}
/**
* Makes a read-only transaction and then looks for a property using a non-existent QName.
* The QName persistence must not lazily create QNameEntity instances for queries.
*/
public void testGetUnknownProperty() throws Exception
{
// commit to keep the root node
setComplete();
endTransaction();
RetryingTransactionCallback createCallback = new RetryingTransactionCallback()
{
public NodeRef execute() throws Throwable
{
NodeRef nodeRef = nodeService.createNode(
rootNodeRef,
ASSOC_TYPE_QNAME_TEST_CHILDREN,
QName.createQName("pathA"),
ContentModel.TYPE_CONTAINER).getChildRef();
return nodeRef;
}
};
final NodeRef nodeRef = retryingTransactionHelper.doInTransaction(createCallback, false, true);
RetryingTransactionCallback