Derek Hulley efbd951a10 Merged DEV/SWIFT to HEAD
27125: Subtasks of ALF-7072: RSOLR 013: Remote API to get ACLs and readers
          - ALF-8334: RSOLR 013: Modify ACL schema to record change times
          - ALF-8336: RSOLR 013: DB upgrade scripts for ACL changes
          - TODO: Query APIs
   27128: Added TooManyResultsException as a concurrency detection trigger
          - Usually too many results indicates that the DB table key is not as specific as it should be,
            but it's AVM that showed this up.
   27132: Clean up: javadocs; non-javadocs; uncommented fields; @since tags; etc.
   27134: Removed empty directory
   27135: Fix for ALF-8333: CMIS query: JOIN on an aspect results in CmisInvalidArgumentException
          - incorrect scope used when building orderings
   27139: Fixed SORL transaction tracking queries
          - Queries were using incompatible boolean comparisons
          - Added SOLRDAO to test suite
          - Cleaned up code and reformatted code
   27141: Minor additions to CannedQuery interface
          - get parameter bean
          - construct sort details from a list
          - ALF-7167: Canned queries
   27146: RINF 09 / RINF 10: DB-based paged query for get children (DocLib & CMIS) 
          - milestone check-in for sprint demo & review (WIP)
          - added new FileFolderService (paged) list query (public API is subject to change)
          - moved temp JavaScript sorting to Java
          - example usage by DocLib (via ScriptNode) and CMIS (via AlfrescoCmisService)
          - implemented as demo "canned query" including embedded use of "list" permission interceptor
          - ALF-7402 / ALF-7168
   27150: RINF 09 / RINF 10: DB-based paged query for get children (DocLib & CMIS) 
          - missed file (follow-on to r27146)
   27158: ALF-7070, ALF-7072: SOLR tracking (node and changeset)
          - Pulled non-DAO code into SOLRTrackingComponent
          - DAO code and related tests just test basic CRUD
          - SOLRTrackingComponent does complex cross-schema manipulation
   27159: Fixed line ending and removed svn:eol-style
   27160: ALF-8334: RSOLR 013: Fixed SQL Server syntax
   27165: RINF 09 / RINF 10: DB-based paged query for get children (DocLib & CMIS) 
          - fix listDeepFolders (causing Imap*Test to fail) 
          - all private methods now order files followed by folders
		    (consistent with existing public APIs such as FileFolderService.search & ScriptNode.childFileFolders*)
          - follow-on to r27146
   28271: Consolidate diagnostic logging for max perm checks (ALF-8388 + ALF-8419)
          - note: this should be a trivial merge to HEAD

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@28292 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
2011-06-08 17:29:54 +00:00

358 lines
12 KiB
Java

/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.repo.domain.permissions;
import java.util.ArrayList;
import java.util.List;
import junit.framework.TestCase;
import org.alfresco.repo.security.permissions.PermissionReference;
import org.alfresco.repo.security.permissions.impl.SimplePermissionReference;
import org.alfresco.repo.transaction.RetryingTransactionHelper;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.namespace.QName;
import org.alfresco.service.transaction.TransactionService;
import org.alfresco.util.ApplicationContextHelper;
import org.springframework.context.ApplicationContext;
/**
* @see AclCrudDAO
*
* @author janv
* @since 3.4
*/
public class AclCrudDAOTest extends TestCase
{
private ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
private TransactionService transactionService;
private RetryingTransactionHelper txnHelper;
private AclCrudDAO aclCrudDAO;
@Override
public void setUp() throws Exception
{
ServiceRegistry serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);
transactionService = serviceRegistry.getTransactionService();
txnHelper = transactionService.getRetryingTransactionHelper();
aclCrudDAO = (AclCrudDAO)ctx.getBean("aclCrudDAO");
}
// TODO - alf_access_control_list, alf_acl_member, alf_access_control_entry
//
// alf_acl_change_set
//
private long createAclChangeSet() throws Exception
{
RetryingTransactionCallback<Long> callback = new RetryingTransactionCallback<Long>()
{
public Long execute() throws Throwable
{
return aclCrudDAO.createAclChangeSet();
}
};
return txnHelper.doInTransaction(callback);
}
private void updateAclChangeSet(final Long aclChangeSetId) throws Exception
{
RetryingTransactionCallback<Void> callback = new RetryingTransactionCallback<Void>()
{
public Void execute() throws Throwable
{
aclCrudDAO.updateAclChangeSet(aclChangeSetId, System.currentTimeMillis());
return null;
}
};
txnHelper.doInTransaction(callback);
}
private void deleteAclChangeSet(final Long aclChangeSetId) throws Exception
{
RetryingTransactionCallback<Void> callback = new RetryingTransactionCallback<Void>()
{
public Void execute() throws Throwable
{
aclCrudDAO.deleteAclChangeSet(aclChangeSetId);
return null;
}
};
txnHelper.doInTransaction(callback);
}
private AclChangeSet getAclChangeSet(final long aclChangeSetId) throws Exception
{
RetryingTransactionCallback<AclChangeSet> callback = new RetryingTransactionCallback<AclChangeSet>()
{
public AclChangeSet execute() throws Throwable
{
return aclCrudDAO.getAclChangeSet(aclChangeSetId);
}
};
return txnHelper.doInTransaction(callback);
}
public void testCreateAndDeleteAclChangeSet() throws Exception
{
Long aclChangeSetId = createAclChangeSet();
AclChangeSet acsEntity= getAclChangeSet(aclChangeSetId);
assertNotNull(acsEntity);
assertEquals(aclChangeSetId, acsEntity.getId());
updateAclChangeSet(aclChangeSetId);
deleteAclChangeSet(aclChangeSetId);
assertNull(getAclChangeSet(aclChangeSetId));
}
public void testCreateAclChangeSetWithRollback() throws Exception
{
final List<Long> tmp = new ArrayList<Long>(1);
RetryingTransactionCallback<Void> callback = new RetryingTransactionCallback<Void>()
{
public Void execute() throws Throwable
{
long acsEntityId = createAclChangeSet();
tmp.add(acsEntityId);
// Now force a rollback
throw new RuntimeException("Forced");
}
};
try
{
txnHelper.doInTransaction(callback);
fail("Transaction didn't roll back");
}
catch (RuntimeException e)
{
// Expected
}
assertEquals(1, tmp.size());
// Check that it doesn't exist
assertNull(getAclChangeSet(tmp.get(0)));
}
//
// alf_authority
//
private Authority createAuth(final String authName) throws Exception
{
RetryingTransactionCallback<Authority> callback = new RetryingTransactionCallback<Authority>()
{
public Authority execute() throws Throwable
{
return aclCrudDAO.getOrCreateAuthority(authName);
}
};
return txnHelper.doInTransaction(callback);
}
private void deleteAuth(final long authId) throws Exception
{
RetryingTransactionCallback<Void> callback = new RetryingTransactionCallback<Void>()
{
public Void execute() throws Throwable
{
aclCrudDAO.deleteAuthority(authId);
return null;
}
};
txnHelper.doInTransaction(callback);
}
private void updateAuth(final String before, final String after) throws Exception
{
RetryingTransactionCallback<Void> callback = new RetryingTransactionCallback<Void>()
{
public Void execute() throws Throwable
{
aclCrudDAO.renameAuthority(before, after);
return null;
}
};
txnHelper.doInTransaction(callback);
}
private Authority getAuth(final String authName) throws Exception
{
RetryingTransactionCallback<Authority> callback = new RetryingTransactionCallback<Authority>()
{
public Authority execute() throws Throwable
{
return aclCrudDAO.getAuthority(authName);
}
};
return txnHelper.doInTransaction(callback);
}
public void testCreateUpdateAndDeleteAuth() throws Exception
{
final String authName = getName() + "-" + System.currentTimeMillis();
Authority authEntity= getAuth(authName);
assertNull(authEntity);
Authority createAuthEntity = createAuth(authName);
assertNotNull(createAuthEntity);
authEntity= getAuth(authName);
assertEquals(createAuthEntity, authEntity);
String newAuthName = authName+"-new";
updateAuth(authName, newAuthName);
assertNull(getAuth(authName));
authEntity = getAuth(newAuthName);
assertNotNull(authEntity);
assertEquals(createAuthEntity.getId(), authEntity.getId());
assertEquals(newAuthName, authEntity.getAuthority());
deleteAuth(authEntity.getId());
assertNull(getAuth(newAuthName));
}
public void testCreateAuthWithRollback() throws Exception
{
final String authName = getName() + "-" + System.currentTimeMillis();
RetryingTransactionCallback<Void> callback = new RetryingTransactionCallback<Void>()
{
public Void execute() throws Throwable
{
createAuth(authName);
// Now force a rollback
throw new RuntimeException("Forced");
}
};
try
{
txnHelper.doInTransaction(callback);
fail("Transaction didn't roll back");
}
catch (RuntimeException e)
{
// Expected
}
// Check that it doesn't exist
assertNull(getAuth(authName));
}
//
// alf_permission
//
private Permission createPermission(final PermissionReference permissionReference) throws Exception
{
RetryingTransactionCallback<Permission> callback = new RetryingTransactionCallback<Permission>()
{
public Permission execute() throws Throwable
{
return aclCrudDAO.getOrCreatePermission(permissionReference);
}
};
return txnHelper.doInTransaction(callback);
}
private void deletePermission(final long permissionId) throws Exception
{
RetryingTransactionCallback<Void> callback = new RetryingTransactionCallback<Void>()
{
public Void execute() throws Throwable
{
aclCrudDAO.deletePermission(permissionId);
return null;
}
};
txnHelper.doInTransaction(callback);
}
private Permission getPermission(final PermissionReference permissionReference) throws Exception
{
RetryingTransactionCallback<Permission> callback = new RetryingTransactionCallback<Permission>()
{
public Permission execute() throws Throwable
{
return aclCrudDAO.getPermission(permissionReference);
}
};
return txnHelper.doInTransaction(callback);
}
public void testCreateAndDeletePermission() throws Exception
{
String name = getName() + "-" + System.currentTimeMillis();
final SimplePermissionReference permRef = SimplePermissionReference.getPermissionReference(QName.createQName("cm:cmobject"), name);
Permission createdPermEntity = createPermission(permRef);
assertNotNull(createdPermEntity);
Permission permEntity = getPermission(permRef);
assertEquals(createdPermEntity, permEntity);
deletePermission(permEntity.getId());
assertNull(getPermission(permRef));
}
public void testCreatePermissionWithRollback() throws Exception
{
String name = getName() + "-" + System.currentTimeMillis();
final SimplePermissionReference permRef = SimplePermissionReference.getPermissionReference(QName.createQName("cm:cmobject"), name);
RetryingTransactionCallback<Void> callback = new RetryingTransactionCallback<Void>()
{
public Void execute() throws Throwable
{
createPermission(permRef);
// Now force a rollback
throw new RuntimeException("Forced");
}
};
try
{
txnHelper.doInTransaction(callback);
fail("Transaction didn't roll back");
}
catch (RuntimeException e)
{
// Expected
}
// Check that it doesn't exist
assertNull(getPermission(permRef));
}
}