/*
* 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 .
*/
package org.alfresco.repo.avm;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import javax.transaction.UserTransaction;
import org.alfresco.config.JNDIConstants;
import org.alfresco.model.ContentModel;
import org.alfresco.model.WCMModel;
import org.alfresco.repo.action.ActionImpl;
import org.alfresco.repo.avm.actions.AVMRevertListAction;
import org.alfresco.repo.avm.actions.AVMRevertStoreAction;
import org.alfresco.repo.avm.actions.AVMRevertToVersionAction;
import org.alfresco.repo.avm.actions.AVMUndoSandboxListAction;
import org.alfresco.repo.avm.actions.SimpleAVMPromoteAction;
import org.alfresco.repo.avm.actions.SimpleAVMSubmitAction;
import org.alfresco.repo.avm.util.BulkLoader;
import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.repo.domain.PropertyValue;
import org.alfresco.repo.search.impl.lucene.LuceneQueryParser;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.transaction.RetryingTransactionHelper;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.service.cmr.avm.AVMBadArgumentException;
import org.alfresco.service.cmr.avm.AVMCycleException;
import org.alfresco.service.cmr.avm.AVMException;
import org.alfresco.service.cmr.avm.AVMExistsException;
import org.alfresco.service.cmr.avm.AVMNodeDescriptor;
import org.alfresco.service.cmr.avm.AVMNotFoundException;
import org.alfresco.service.cmr.avm.AVMService;
import org.alfresco.service.cmr.avm.AVMStoreDescriptor;
import org.alfresco.service.cmr.avm.LayeringDescriptor;
import org.alfresco.service.cmr.avm.VersionDescriptor;
import org.alfresco.service.cmr.avmsync.AVMDifference;
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
import org.alfresco.service.cmr.model.FileFolderService;
import org.alfresco.service.cmr.remote.RepoRemote;
import org.alfresco.service.cmr.repository.ContentData;
import org.alfresco.service.cmr.repository.ContentWriter;
import org.alfresco.service.cmr.repository.CrossRepositoryCopyService;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.search.ResultSet;
import org.alfresco.service.cmr.search.ResultSetRow;
import org.alfresco.service.cmr.search.SearchService;
import org.alfresco.service.cmr.security.AccessPermission;
import org.alfresco.service.cmr.security.PermissionService;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.alfresco.service.transaction.TransactionService;
import org.alfresco.util.GUID;
import org.alfresco.util.Pair;
import org.alfresco.wcm.sandbox.SandboxConstants;
/**
* Big test of AVM behavior.
*
* @author britt
*/
public class AVMServiceTest extends AVMServiceTestBase
{
public void testSetup() throws Exception
{
super.testSetup();
}
public void testSetupAndQueriesAgainstBasicTree() throws Exception
{
setupBasicTree();
runQueriesAgainstBasicTree("main");
}
public void test_ALF_786() throws Exception
{
int threads= 4;
int loops = 10;
int snapshotsPerLoop = 4;
int startVersion;
UserTransaction testTX = fTransactionService.getUserTransaction();
testTX.begin();
fService.createDirectory("main:/", "test");
startVersion = fService.createSnapshot("main", null, null).get("main");
testTX.commit();
testTX = fTransactionService.getUserTransaction();
testTX.begin();
StoreRef storeRef = AVMNodeConverter.ToStoreRef("main");
SearchService searchService = fIndexerAndSearcher.getSearcher(AVMNodeConverter.ToStoreRef("main"), true);
ResultSet results = searchService.query(storeRef, "lucene", "PATH:\"/test/*\"");
assertEquals(0, results.length());
results.close();
testTX.commit();
Thread runner = null;
for (int i = 0; i < threads; i++)
{
runner = new Nester("Concurrent-" + i, runner, false, snapshotsPerLoop, Nester.Mode.CREATE, loops);
}
if (runner != null)
{
runner.start();
try
{
runner.join();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
testTX = fTransactionService.getUserTransaction();
testTX.begin();
// snap
testTX.commit();
testTX = fTransactionService.getUserTransaction();
testTX.begin();;
SortedMap listing = fService.getDirectoryListing(-1, "main:/test");
assertEquals(loops, listing.size());
for(AVMNodeDescriptor node : listing.values())
{
System.out.println("Listed: "+node.getPath()+" "+node.getVersionID());
}
List diffs = fSyncService.compare(startVersion, "main:/", -1, "main:/", null);
assertEquals(loops, diffs.size());
for(AVMDifference diff : diffs)
{
AVMNodeDescriptor desc = fService.lookup(diff.getDestinationVersion(), diff.getDestinationPath(), true);
assertFalse(desc.isDeleted());
}
searchService = fIndexerAndSearcher.getSearcher(AVMNodeConverter.ToStoreRef("main"), true);
results = searchService.query(storeRef, "lucene", "PATH:\"/test/*\"");
for(ResultSetRow row : results)
{
System.out.println("Found: "+row.getNodeRef());
}
assertEquals(loops, results.length());
results.close();
testTX.commit();
// update
runner = null;
for (int i = 0; i < threads; i++)
{
runner = new Nester("Concurrent-" + i, runner, false, snapshotsPerLoop, Nester.Mode.UPDATE, loops);
}
if (runner != null)
{
runner.start();
try
{
runner.join();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
testTX = fTransactionService.getUserTransaction();
testTX.begin();
searchService = fIndexerAndSearcher.getSearcher(AVMNodeConverter.ToStoreRef("main"), true);
results = searchService.query(storeRef, "lucene", "PATH:\"/test/*\"");
for(ResultSetRow row : results)
{
System.out.println("Found: "+row.getNodeRef());
}
assertEquals(loops, results.length());
results.close();
testTX.commit();
// delete
runner = null;
for (int i = 0; i < threads; i++)
{
runner = new Nester("Concurrent-" + i, runner, false, snapshotsPerLoop, Nester.Mode.DELETE, loops);
}
if (runner != null)
{
runner.start();
try
{
runner.join();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
testTX = fTransactionService.getUserTransaction();
testTX.begin();
searchService = fIndexerAndSearcher.getSearcher(AVMNodeConverter.ToStoreRef("main"), true);
results = searchService.query(storeRef, "lucene", "PATH:\"/test/*\"");
for(ResultSetRow row : results)
{
System.out.println("Found: "+row.getNodeRef());
}
assertEquals(0, results.length());
results.close();
testTX.commit();
// recreate
runner = null;
for (int i = 0; i < threads; i++)
{
runner = new Nester("Concurrent-" + i, runner, false, snapshotsPerLoop, Nester.Mode.CREATE, loops);
}
if (runner != null)
{
runner.start();
try
{
runner.join();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
testTX = fTransactionService.getUserTransaction();
testTX.begin();
searchService = fIndexerAndSearcher.getSearcher(AVMNodeConverter.ToStoreRef("main"), true);
results = searchService.query(storeRef, "lucene", "PATH:\"/test/*\"");
for(ResultSetRow row : results)
{
System.out.println("Found: "+row.getNodeRef());
}
assertEquals(loops, results.length());
results.close();
testTX.commit();
//move
runner = null;
for (int i = 0; i < threads; i++)
{
runner = new Nester("Concurrent-" + i, runner, false, snapshotsPerLoop, Nester.Mode.MOVE, loops);
}
if (runner != null)
{
runner.start();
try
{
runner.join();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
testTX = fTransactionService.getUserTransaction();
testTX.begin();
searchService = fIndexerAndSearcher.getSearcher(AVMNodeConverter.ToStoreRef("main"), true);
results = searchService.query(storeRef, "lucene", "PATH:\"/test/*\"");
for(ResultSetRow row : results)
{
System.out.println("Found: "+row.getNodeRef());
}
assertEquals(loops, results.length());
results.close();
testTX.commit();
}
public void xtest_ALF_786_PLUS() throws Exception
{
int startVersion;
UserTransaction testTX = fTransactionService.getUserTransaction();
testTX.begin();
fService.createDirectory("main:/", "test");
startVersion = fService.createSnapshot("main", null, null).get("main");
testTX.commit();
testTX = fTransactionService.getUserTransaction();
testTX.begin();
StoreRef storeRef = AVMNodeConverter.ToStoreRef("main");
SearchService searchService = fIndexerAndSearcher.getSearcher(AVMNodeConverter.ToStoreRef("main"), true);
ResultSet results = searchService.query(storeRef, "lucene", "PATH:\"/test/*\"");
assertEquals(0, results.length());
results.close();
testTX.commit();
Thread runner = null;
for (int i = 0; i < 10; i++)
{
runner = new Nester("Concurrent-" + i, runner, true, 10, Nester.Mode.CREATE, 10 );
}
if (runner != null)
{
runner.start();
try
{
runner.join();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
testTX = fTransactionService.getUserTransaction();
testTX.begin();
// snap
testTX.commit();
testTX = fTransactionService.getUserTransaction();
testTX.begin();;
SortedMap listing = fService.getDirectoryListing(-1, "main:/test");
assertEquals(100, listing.size());
for(AVMNodeDescriptor node : listing.values())
{
System.out.println("Listed: "+node.getPath()+" "+node.getVersionID());
}
List diffs = fSyncService.compare(startVersion, "main:/", -1, "main:/", null);
assertEquals(100, diffs.size());
for(AVMDifference diff : diffs)
{
AVMNodeDescriptor desc = fService.lookup(diff.getDestinationVersion(), diff.getDestinationPath(), true);
assertFalse(desc.isDeleted());
}
searchService = fIndexerAndSearcher.getSearcher(AVMNodeConverter.ToStoreRef("main"), true);
results = searchService.query(storeRef, "lucene", "PATH:\"/test/*\"");
for(ResultSetRow row : results)
{
System.out.println("Found: "+row.getNodeRef());
}
assertEquals(100, results.length());
results.close();
testTX.commit();
}
static class Nester extends Thread
{
enum Mode {CREATE, UPDATE, DELETE, MOVE};
Thread waiter;
int i;
boolean multiThread;
int snapshotCount;
Mode mode;
int loopCount;
Nester(String name, Thread waiter, boolean multiThread, int snapshotCount, Mode mode, int loopCount)
{
super(name);
this.setDaemon(true);
this.waiter = waiter;
this.multiThread = multiThread;
this.snapshotCount = snapshotCount;
this.mode = mode;
this.loopCount = loopCount;
}
public void run()
{
fAuthenticationComponent.setSystemUserAsCurrentUser();
if (waiter != null)
{
waiter.start();
}
try
{
System.out.println("Start " + this.getName());
for(i = 0; i < loopCount; i++)
{
RetryingTransactionCallback create = new RetryingTransactionCallback()
{
public Void execute() throws Throwable
{
fService.createFile("main:/test", getName()+"-"+i);
return null;
}
};
RetryingTransactionCallback update = new RetryingTransactionCallback()
{
public Void execute() throws Throwable
{
fService.setMimeType("main:/test/"+getName()+"-"+i, "text/plain");
return null;
}
};
RetryingTransactionCallback delete = new RetryingTransactionCallback()
{
public Void execute() throws Throwable
{
fService.removeNode("main:/test/"+getName()+"-"+i);
return null;
}
};
RetryingTransactionCallback move = new RetryingTransactionCallback()
{
public Void execute() throws Throwable
{
fService.rename("main:/test/", getName()+"-"+i, "main:/test/", "MOVED-"+getName()+"-"+i);
return null;
}
};
if(multiThread || (waiter == null))
{
// only one thread creates for 786
switch(mode)
{
case CREATE:
fRetryingTransactionHelper.doInTransaction(create);
System.out.println(getName()+i);
break;
case UPDATE:
fRetryingTransactionHelper.doInTransaction(update);
break;
case DELETE:
fRetryingTransactionHelper.doInTransaction(delete);
break;
case MOVE:
fRetryingTransactionHelper.doInTransaction(move);
break;
default:
}
}
RetryingTransactionCallback snap = new RetryingTransactionCallback()
{
public Void execute() throws Throwable
{
fService.createSnapshot("main", null, null);
return null;
}
};
for(int s = 0; s < snapshotCount; s++)
{
fRetryingTransactionHelper.doInTransaction(snap);
}
}
System.out.println("End " + this.getName());
}
catch (Exception e)
{
System.out.println("End " + this.getName() + " with error " + e.getMessage());
e.printStackTrace();
}
finally
{
fAuthenticationComponent.clearCurrentSecurityContext();
}
if (waiter != null)
{
try
{
waiter.join();
System.out.println("Waited for " + waiter.getName()+" by "+this.getName());
}
catch (InterruptedException e)
{
}
}
}
}
public void testDiffOrder()
{
try
{
fService.createStore("Bottom");
fService.createStore("Top");
fService.createDirectory("Bottom:/", "www");
fService.createLayeredDirectory("Bottom:/www", "Top:/", "www");
fService.createFile("Bottom:/www", "newInBottom");
fService.createSnapshot("Bottom", null, null);
fService.createFile("Top:/www", "newInTop");
fService.createSnapshot("Top", null, null);
fService.createFile("Bottom:/www", "file");
fService.createSnapshot("Bottom", null, null);
fService.forceCopy("Top:/www/file");
fService.createSnapshot("Top", null, null);
fService.forceCopy("Bottom:/www/file");
fService.createSnapshot("Bottom", null, null);
List diffs = fSyncService.compare(-1, "Top:/", -1, "Bottom:/", null);
assertEquals(
2, diffs.size());
Collections.sort(diffs);
AVMDifference last = null;
for(AVMDifference current : diffs)
{
if(last != null)
{
assert(last.getOrderValue() < current.getOrderValue());
}
last = current;
}
diffs.add(new AVMDifference(1, null, -1, null, 0));
diffs.add(new AVMDifference(1, null, -1, null, 1));
diffs.add(new AVMDifference(1, null, -1, null, 2));
diffs.add(new AVMDifference(1, null, -1, null, 3));
diffs.add(new AVMDifference(1, null, -1, null, 4));
diffs.add(new AVMDifference(1, null, -1, null, 5));
diffs.add(new AVMDifference(1, null, -1, null, 6));
Collections.sort(diffs);
last = null;
for(AVMDifference current : diffs)
{
if(last != null)
{
assert(last.getOrderValue() < current.getOrderValue());
}
last = current;
}
}
finally
{
fService.purgeStore("Bottom");
fService.purgeStore("Top");
}
}
public void test_ETWOTWO_570() throws Exception
{
// Check that read-write methods are properly intercepted
RetryingTransactionCallback