ALF-9845 Allow newly created sub categories to be found from within the same transaction. Also includes tests for this in Java, and new JavaScript tests for this and similar areas

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@31381 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Nick Burch
2011-10-20 14:01:55 +00:00
parent c801a1e571
commit 77dfff3c0c
3 changed files with 135 additions and 4 deletions

View File

@@ -23,6 +23,7 @@ import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Random; import java.util.Random;
import javax.transaction.UserTransaction; import javax.transaction.UserTransaction;
@@ -35,6 +36,8 @@ import org.alfresco.repo.dictionary.IndexTokenisationMode;
import org.alfresco.repo.dictionary.M2Aspect; import org.alfresco.repo.dictionary.M2Aspect;
import org.alfresco.repo.dictionary.M2Model; import org.alfresco.repo.dictionary.M2Model;
import org.alfresco.repo.dictionary.M2Property; import org.alfresco.repo.dictionary.M2Property;
import org.alfresco.repo.jscript.CategoryNode;
import org.alfresco.repo.jscript.ClasspathScriptLocation;
import org.alfresco.repo.search.IndexerAndSearcher; import org.alfresco.repo.search.IndexerAndSearcher;
import org.alfresco.repo.search.impl.lucene.fts.FullTextSearchIndexer; import org.alfresco.repo.search.impl.lucene.fts.FullTextSearchIndexer;
import org.alfresco.repo.security.authentication.AuthenticationUtil; import org.alfresco.repo.security.authentication.AuthenticationUtil;
@@ -45,6 +48,8 @@ import org.alfresco.service.cmr.dictionary.DictionaryService;
import org.alfresco.service.cmr.repository.ChildAssociationRef; import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService; import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.ScriptLocation;
import org.alfresco.service.cmr.repository.ScriptService;
import org.alfresco.service.cmr.repository.StoreRef; import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.search.CategoryService; import org.alfresco.service.cmr.search.CategoryService;
import org.alfresco.service.cmr.search.ResultSet; import org.alfresco.service.cmr.search.ResultSet;
@@ -70,7 +75,9 @@ public class ADMLuceneCategoryTest extends TestCase
static ApplicationContext ctx = ApplicationContextHelper.getApplicationContext(); static ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
NodeService nodeService; NodeService nodeService;
NodeService publicNodeService;
DictionaryService dictionaryService; DictionaryService dictionaryService;
ScriptService scriptService;
private NodeRef rootNodeRef; private NodeRef rootNodeRef;
private NodeRef n1; private NodeRef n1;
private NodeRef n2; private NodeRef n2;
@@ -133,6 +140,7 @@ public class ADMLuceneCategoryTest extends TestCase
public void setUp() throws Exception public void setUp() throws Exception
{ {
nodeService = (NodeService)ctx.getBean("dbNodeService"); nodeService = (NodeService)ctx.getBean("dbNodeService");
publicNodeService = (NodeService)ctx.getBean("NodeService");
dictionaryService = (DictionaryService)ctx.getBean("dictionaryService"); dictionaryService = (DictionaryService)ctx.getBean("dictionaryService");
luceneFTS = (FullTextSearchIndexer) ctx.getBean("LuceneFullTextSearchIndexer"); luceneFTS = (FullTextSearchIndexer) ctx.getBean("LuceneFullTextSearchIndexer");
dictionaryDAO = (DictionaryDAO) ctx.getBean("dictionaryDAO"); dictionaryDAO = (DictionaryDAO) ctx.getBean("dictionaryDAO");
@@ -142,6 +150,7 @@ public class ADMLuceneCategoryTest extends TestCase
categoryService = (CategoryService) ctx.getBean("categoryService"); categoryService = (CategoryService) ctx.getBean("categoryService");
serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY); serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);
tenantService = (TenantService) ctx.getBean("tenantService"); tenantService = (TenantService) ctx.getBean("tenantService");
scriptService = (ScriptService) ctx.getBean("scriptService");
AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getSystemUserName()); AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getSystemUserName());
@@ -383,7 +392,7 @@ public class ADMLuceneCategoryTest extends TestCase
searcher.setNodeService(nodeService); searcher.setNodeService(nodeService);
searcher.setDictionaryService(dictionaryService); searcher.setDictionaryService(dictionaryService);
searcher.setTenantService(tenantService); searcher.setTenantService(tenantService);
searcher.setNamespacePrefixResolver(getNamespacePrefixReolsver("")); searcher.setNamespacePrefixResolver(getNamespacePrefixResolver(""));
searcher.setQueryLanguages(indexerAndSearcher.getQueryLanguages()); searcher.setQueryLanguages(indexerAndSearcher.getQueryLanguages());
return searcher; return searcher;
} }
@@ -599,7 +608,8 @@ public class ADMLuceneCategoryTest extends TestCase
LuceneCategoryServiceImpl impl = new LuceneCategoryServiceImpl(); LuceneCategoryServiceImpl impl = new LuceneCategoryServiceImpl();
impl.setNodeService(nodeService); impl.setNodeService(nodeService);
impl.setNamespacePrefixResolver(getNamespacePrefixReolsver("")); impl.setPublicNodeService(publicNodeService);
impl.setNamespacePrefixResolver(getNamespacePrefixResolver(""));
impl.setIndexerAndSearcher(indexerAndSearcher); impl.setIndexerAndSearcher(indexerAndSearcher);
impl.setTenantService(tenantService); impl.setTenantService(tenantService);
impl.setDictionaryService(dictionaryService); impl.setDictionaryService(dictionaryService);
@@ -640,10 +650,57 @@ public class ADMLuceneCategoryTest extends TestCase
Collection<QName> aspects = impl.getClassificationAspects(); Collection<QName> aspects = impl.getClassificationAspects();
assertEquals(7, aspects.size()); assertEquals(7, aspects.size());
// Add an additional subcategory, and check it shows up
result = impl.getChildren(catACBase , CategoryService.Mode.SUB_CATEGORIES, CategoryService.Depth.ANY);
assertEquals(3, result.size());
String newName = "TestSub";
impl.createCategory(catACBase, newName);
result = impl.getChildren(catACBase , CategoryService.Mode.SUB_CATEGORIES, CategoryService.Depth.ANY);
assertEquals(4, result.size());
// Tidy up
tx.rollback(); tx.rollback();
} }
private NamespacePrefixResolver getNamespacePrefixReolsver(String defaultURI) /**
* JavaScript tests for the Category Service.
* Note that this test lives here because that's where the Java
* CategeoryService tests are, and this also has all the required
* SetUp and TearDown to make it easier.
*/
public void testJavascriptAPI() throws Exception
{
TransactionService transactionService = serviceRegistry.getTransactionService();
UserTransaction tx = transactionService.getUserTransaction();
tx.begin();
buildBaseIndex();
ServiceRegistry services = (ServiceRegistry)ctx.getBean("ServiceRegistry");
// Call the test
Map<String, Object> model = new HashMap<String, Object>();
model.put("catRoot", new CategoryNode(catRoot, services));
model.put("catACBase", new CategoryNode(catACBase, services));
model.put("catACOne", new CategoryNode(catACOne, services));
model.put("catACTwo", new CategoryNode(catACTwo, services));
model.put("catACThree", new CategoryNode(catACThree, services));
model.put("catRBase", new CategoryNode(catRBase, services));
model.put("catROne", new CategoryNode(catROne, services));
model.put("catRTwo", new CategoryNode(catRTwo, services));
model.put("catRThree", new CategoryNode(catRThree, services));
ScriptLocation location = new ClasspathScriptLocation("org/alfresco/repo/search/impl/lucene/test_categoryService.js");
this.scriptService.executeScript(location, model);
// Tidy up
tx.rollback();
}
private NamespacePrefixResolver getNamespacePrefixResolver(String defaultURI)
{ {
DynamicNamespacePrefixResolver nspr = new DynamicNamespacePrefixResolver(null); DynamicNamespacePrefixResolver nspr = new DynamicNamespacePrefixResolver(null);
nspr.registerNamespace(NamespaceService.CONTENT_MODEL_PREFIX, NamespaceService.CONTENT_MODEL_1_0_URI); nspr.registerNamespace(NamespaceService.CONTENT_MODEL_PREFIX, NamespaceService.CONTENT_MODEL_1_0_URI);

View File

@@ -187,8 +187,13 @@ public class LuceneCategoryServiceImpl implements CategoryService
break; break;
} }
resultSet = indexerAndSearcher.getSearcher(categoryRef.getStoreRef(), false).query(categoryRef.getStoreRef(), "lucene", luceneQuery.toString(), null); // Get a searcher that will include Categories added in this transaction
SearchService searcher = indexerAndSearcher.getSearcher(categoryRef.getStoreRef(), true);
// Perform the search
resultSet = searcher.query(categoryRef.getStoreRef(), "lucene", luceneQuery.toString(), null);
// Convert from search results to the required Child Assocs
return resultSetToChildAssocCollection(resultSet); return resultSetToChildAssocCollection(resultSet);
} }
finally finally

View File

@@ -0,0 +1,69 @@
// Checks the finding and adding of Root Categories
function testRootCategories()
{
// TODO
// CategoryNode[] getAllCategoryNodes(String aspect)
// string[] getAllClassificationAspects()
// CategoryNode createRootCategory(string aspect, string name)
// CategoryNode[] getRootCategories(string aspect)
}
// Checks that we can correctly query a test category
// that ADMLuceneCategoryTest setup for us
function testCategoryListings()
{
// Check things are correctly detected as categories
test.assertEquals(true, catACBase.isCategory);
test.assertEquals(true, catACOne.isCategory);
test.assertEquals(true, catACTwo.isCategory);
test.assertEquals(true, catACThree.isCategory);
// These tests are taken from testCategoryServiceImpl()
//result = impl.getChildren(catACBase , CategoryService.Mode.MEMBERS, CategoryService.Depth.IMMEDIATE);
test.assertEquals(1, catACBase.immediateCategoryMembers.length);
//result = impl.getChildren(catACBase , CategoryService.Mode.ALL, CategoryService.Depth.IMMEDIATE);
test.assertEquals(3, catACBase.immediateMembersAndSubCategories.length);
//result = impl.getChildren(catACBase , CategoryService.Mode.SUB_CATEGORIES, CategoryService.Depth.IMMEDIATE);
test.assertEquals(2, catACBase.immediateSubCategories.length);
//result = impl.getChildren(catACBase , CategoryService.Mode.MEMBERS, CategoryService.Depth.ANY);
test.assertEquals(14, catACBase.categoryMembers.length);
//result = impl.getChildren(catACBase , CategoryService.Mode.ALL, CategoryService.Depth.ANY);
test.assertEquals(17, catACBase.membersAndSubCategories.length);
//result = impl.getChildren(catACBase , CategoryService.Mode.SUB_CATEGORIES, CategoryService.Depth.ANY);
test.assertEquals(3, catACBase.subCategories.length);
}
// Tests that we can add and remove sub-categories
function testSubCategories()
{
// catACBase starts with 3
test.assertEquals(3, catACBase.subCategories.length);
// Add a 4th
var testCat = catACBase.createSubCategory("testCat");
test.assertEquals(4, catACBase.subCategories.length);
// Delete it
testCat.removeCategory();
test.assertEquals(3, catACBase.subCategories.length);
// Add 2 more, as parent/child
var testCatA = catACBase.createSubCategory("testCatA");
var testCatB = testCatA.createSubCategory("testCatB");
test.assertEquals(5, catACBase.subCategories.length);
// Delete the parent
testCatA.removeCategory();
test.assertEquals(3, catACBase.subCategories.length);
}
// Execute Tests
testRootCategories();
testCategoryListings();
testSubCategories();