下面列出了org.springframework.test.context.transaction.TestTransaction#start ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void rollbackTxAndStartNewTx() {
assertInTransaction(true);
assertTrue(TestTransaction.isActive());
assertUsers("Dilbert");
deleteFromTables("user");
assertUsers();
// Rollback (automatically)
assertTrue(TestTransaction.isFlaggedForRollback());
TestTransaction.end();
assertFalse(TestTransaction.isActive());
assertInTransaction(false);
assertUsers("Dilbert");
// Start new transaction with default rollback semantics
TestTransaction.start();
assertInTransaction(true);
assertTrue(TestTransaction.isFlaggedForRollback());
assertTrue(TestTransaction.isActive());
executeSqlScript("classpath:/org/springframework/test/context/jdbc/data-add-dogbert.sql", false);
assertUsers("Dilbert", "Dogbert");
}
@Test
@Commit
public void rollbackTxAndStartNewTxWithDefaultCommitSemantics() {
assertInTransaction(true);
assertTrue(TestTransaction.isActive());
assertUsers("Dilbert");
deleteFromTables("user");
assertUsers();
// Rollback
TestTransaction.flagForRollback();
assertTrue(TestTransaction.isFlaggedForRollback());
TestTransaction.end();
assertFalse(TestTransaction.isActive());
assertInTransaction(false);
assertUsers("Dilbert");
// Start new transaction with default commit semantics
TestTransaction.start();
assertInTransaction(true);
assertFalse(TestTransaction.isFlaggedForRollback());
assertTrue(TestTransaction.isActive());
executeSqlScript("classpath:/org/springframework/test/context/jdbc/data-add-dogbert.sql", false);
assertUsers("Dilbert", "Dogbert");
}
@Test
public void rollbackTxAndStartNewTx() {
assertInTransaction(true);
assertTrue(TestTransaction.isActive());
assertUsers("Dilbert");
deleteFromTables("user");
assertUsers();
// Rollback (automatically)
assertTrue(TestTransaction.isFlaggedForRollback());
TestTransaction.end();
assertFalse(TestTransaction.isActive());
assertInTransaction(false);
assertUsers("Dilbert");
// Start new transaction with default rollback semantics
TestTransaction.start();
assertInTransaction(true);
assertTrue(TestTransaction.isFlaggedForRollback());
assertTrue(TestTransaction.isActive());
executeSqlScript("classpath:/org/springframework/test/context/jdbc/data-add-dogbert.sql", false);
assertUsers("Dilbert", "Dogbert");
}
@Test
public void rollbackTxAndStartNewTx() {
assertInTransaction(true);
assertTrue(TestTransaction.isActive());
assertUsers("Dilbert");
deleteFromTables("user");
assertUsers();
// Rollback (automatically)
assertTrue(TestTransaction.isFlaggedForRollback());
TestTransaction.end();
assertFalse(TestTransaction.isActive());
assertInTransaction(false);
assertUsers("Dilbert");
// Start new transaction with default rollback semantics
TestTransaction.start();
assertInTransaction(true);
assertTrue(TestTransaction.isFlaggedForRollback());
assertTrue(TestTransaction.isActive());
executeSqlScript("classpath:/org/springframework/test/context/jdbc/data-add-dogbert.sql", false);
assertUsers("Dilbert", "Dogbert");
}
@Test
public void rollbackTxAndStartNewTx() {
assertInTransaction(true);
assertTrue(TestTransaction.isActive());
assertUsers("Dilbert");
deleteFromTables("user");
assertUsers();
// Rollback (automatically)
assertTrue(TestTransaction.isFlaggedForRollback());
TestTransaction.end();
assertFalse(TestTransaction.isActive());
assertInTransaction(false);
assertUsers("Dilbert");
// Start new transaction with default rollback semantics
TestTransaction.start();
assertInTransaction(true);
assertTrue(TestTransaction.isFlaggedForRollback());
assertTrue(TestTransaction.isActive());
executeSqlScript("classpath:/org/springframework/test/context/jdbc/data-add-dogbert.sql", false);
assertUsers("Dilbert", "Dogbert");
}
@Test
public void commitTxAndStartNewTx() {
assertInTransaction(true);
assertTrue(TestTransaction.isActive());
assertUsers("Dilbert");
deleteFromTables("user");
assertUsers();
// Commit
TestTransaction.flagForCommit();
assertFalse(TestTransaction.isFlaggedForRollback());
TestTransaction.end();
assertInTransaction(false);
assertFalse(TestTransaction.isActive());
assertUsers();
executeSqlScript("classpath:/org/springframework/test/context/jdbc/data-add-dogbert.sql", false);
assertUsers("Dogbert");
TestTransaction.start();
assertInTransaction(true);
assertTrue(TestTransaction.isActive());
}
private void restartAuditableTxn() throws Exception
{
TestTransaction.end();
// Wait long enough that AuditablePropertiesEntity.setAuditModified
// will recognize subsequent changes as needing new audit entries
try
{
Thread.sleep(1250L);
}
catch(InterruptedException e)
{
}
TestTransaction.start();
}
@Test(expected = IllegalStateException.class)
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void startTxWithNonExistentTransactionContext() {
TestTransaction.start();
}
/**
* Check that <b>d:encrypted</b> properties work correctly.
*/
@Test
public void testEncryptedProperties() throws Exception
{
QName property = PROP_QNAME_CONTENT_VALUE;
Map<QName, Serializable> properties = new HashMap<QName, Serializable>(17);
properties.put(PROP_QNAME_ENCRYPTED_VALUE, property);
// We have encrypted properties, so encrypt them
properties = metadataEncryptor.encrypt(properties);
Serializable checkProperty = properties.get(PROP_QNAME_ENCRYPTED_VALUE);
assertTrue("Properties not encrypted", checkProperty instanceof SealedObject);
// create node
final NodeRef nodeRef = nodeService.createNode(
rootNodeRef,
ASSOC_TYPE_QNAME_TEST_CHILDREN,
QName.createQName("pathA"),
ContentModel.TYPE_CONTAINER,
properties).getChildRef();
// persist
TestTransaction.flagForCommit();
TestTransaction.end();
TestTransaction.start();
// get the properties back
Map<QName, Serializable> checkProperties = nodeService.getProperties(nodeRef);
checkProperty = checkProperties.get(PROP_QNAME_ENCRYPTED_VALUE);
assertTrue("Encrypted property not persisted", checkProperty instanceof SealedObject);
// Decrypt individual property
checkProperty = metadataEncryptor.decrypt(PROP_QNAME_ENCRYPTED_VALUE, checkProperty);
assertEquals("Bulk property decryption failed", property, checkProperty);
// Now decrypt en-masse
checkProperties = metadataEncryptor.decrypt(checkProperties);
checkProperty = checkProperties.get(PROP_QNAME_ENCRYPTED_VALUE);
assertEquals("Bulk property decryption failed", property, checkProperty);
// Now make sure that the value can be null
RetryingTransactionCallback<Void> setNullPropCallback = new RetryingTransactionCallback<Void>()
{
@Override
public Void execute() throws Throwable
{
nodeService.setProperty(nodeRef, PROP_QNAME_ENCRYPTED_VALUE, null);
return null;
}
};
retryingTransactionHelper.doInTransaction(setNullPropCallback);
// Finally, make sure that it fails if we don't encrypt
try
{
RetryingTransactionCallback<Void> setUnencryptedPropCallback = new RetryingTransactionCallback<Void>()
{
@Override
public Void execute() throws Throwable
{
nodeService.setProperty(nodeRef, PROP_QNAME_ENCRYPTED_VALUE, "No encrypted");
return null;
}
};
retryingTransactionHelper.doInTransaction(setUnencryptedPropCallback);
fail("Failed to detect unencrypted property"); // This behaviour may change
}
catch (RuntimeException e)
{
// Expected
}
}
@Test(expectedExceptions = IllegalStateException.class)
public void startTxWithExistingTransaction() {
TestTransaction.start();
}
/**
* This test is similar to the test above but onDelete does nasty stuff such as
* creating siblings of the soon to be deleted children.
*
* In particular, it verifies that we don't get stuck in an infinite loop.
*/
@Test
public void testDeleteWithBadlyBehavedOnDeletePolicies() throws Exception
{
final List<NodeRef> beforeDeleteNodeRefs = new ArrayList<NodeRef>(5);
final List<NodeRef> deletedNodeRefs = new ArrayList<NodeRef>(5);
BadOnDeleteNodePolicy nasty = new BadOnDeleteNodePolicy(nodeService, beforeDeleteNodeRefs, deletedNodeRefs);
try
{
nasty.setOnDeleteCreateChild(true);
nasty.setBeforeDeleteCreateChild(false);
NodeServicePolicies.OnDeleteNodePolicy policy = nasty;
// bind to listen to the deletion of a node
policyComponent.bindClassBehaviour(
QName.createQName(NamespaceService.ALFRESCO_URI, "onDeleteNode"),
policy,
new JavaBehaviour(policy, "onDeleteNode"));
policyComponent.bindClassBehaviour(
QName.createQName(NamespaceService.ALFRESCO_URI, "beforeDeleteNode"),
policy,
new JavaBehaviour(policy, "beforeDeleteNode"));
// build the node and commit the node graph
Map<QName, ChildAssociationRef> 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);
fail("test has not detected orphan children");
// commit to check
TestTransaction.start();
}
catch (Exception e)
{
// We expect to get here with this test.
//e.printStackTrace();
}
finally
{
// turn off nasty policy - may upset other tests
nasty.setOnDeleteCreateChild(false);
nasty.setBeforeDeleteCreateChild(false);
}
}
@Test(expected = IllegalStateException.class)
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void startTxWithNonExistentTransactionContext() {
TestTransaction.start();
}
@Test(expected = IllegalStateException.class)
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void startTxWithNonExistentTransactionContext() {
TestTransaction.start();
}
@Test(expected = IllegalStateException.class)
public void startTxWithExistingTransaction() {
TestTransaction.start();
}
@Test(expectedExceptions = IllegalStateException.class)
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void startTxWithNonExistentTransactionContext() {
TestTransaction.start();
}
@Test(expectedExceptions = IllegalStateException.class)
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void startTxWithNonExistentTransactionContext() {
TestTransaction.start();
}
@Test
@SuppressWarnings("unchecked")
public void testMultiProp() throws Exception
{
QName undeclaredPropQName = QName.createQName(NAMESPACE, getClass().getName());
// create node
NodeRef nodeRef = nodeService.createNode(
rootNodeRef,
ASSOC_TYPE_QNAME_TEST_CHILDREN,
QName.createQName("pathA"),
TYPE_QNAME_TEST_MULTIPLE_TESTER).getChildRef();
ArrayList<Serializable> values = new ArrayList<Serializable>(1);
values.add("ABC");
values.add("DEF");
// test allowable conditions
nodeService.setProperty(nodeRef, PROP_QNAME_STRING_PROP_SINGLE, "ABC");
// nodeService.setProperty(nodeRef, PROP_QNAME_STRING_PROP_SINGLE, values); -- should fail
nodeService.setProperty(nodeRef, PROP_QNAME_STRING_PROP_MULTIPLE, "ABC");
nodeService.setProperty(nodeRef, PROP_QNAME_STRING_PROP_MULTIPLE, values);
nodeService.setProperty(nodeRef, PROP_QNAME_ANY_PROP_SINGLE, "ABC");
nodeService.setProperty(nodeRef, PROP_QNAME_ANY_PROP_SINGLE, values);
nodeService.setProperty(nodeRef, PROP_QNAME_ANY_PROP_MULTIPLE, "ABC");
nodeService.setProperty(nodeRef, PROP_QNAME_ANY_PROP_MULTIPLE, values);
nodeService.setProperty(nodeRef, undeclaredPropQName, "ABC");
nodeService.setProperty(nodeRef, undeclaredPropQName, values);
// commit as we will be breaking the transaction in the next test
TestTransaction.flagForCommit();
TestTransaction.end();
TestTransaction.start();
try
{
// this should fail as we are passing multiple values into a non-any that is multiple=false
nodeService.setProperty(nodeRef, PROP_QNAME_STRING_PROP_SINGLE, values);
}
catch (DictionaryException e)
{
// expected
}
finally
{
TestTransaction.flagForRollback();
TestTransaction.end();
}
TestTransaction.start();
try
{
// Check that multi-valued d:mltext can be collections of MLText
values.clear();
values.add(new MLText("ABC"));
values.add(new MLText("DEF"));
nodeService.setProperty(nodeRef, PROP_QNAME_MULTI_ML_VALUE, values);
List<Serializable> checkValues = (List<Serializable>) nodeService.getProperty(
nodeRef, PROP_QNAME_MULTI_ML_VALUE);
assertEquals("Expected 2 MLText values back", 2, checkValues.size());
assertTrue("Incorrect type in collection", checkValues.get(0) instanceof String);
assertTrue("Incorrect type in collection", checkValues.get(1) instanceof String);
// Check that multi-valued d:any properties can be collections of collections (empty)
// We put ArrayLists and HashSets into the Collection of d:any, so that is exactly what should come out
values.clear();
ArrayList<Serializable> arrayListVal = new ArrayList<Serializable>(2);
HashSet<Serializable> hashSetVal = new HashSet<Serializable>(2);
values.add(arrayListVal);
values.add(hashSetVal);
nodeService.setProperty(nodeRef, PROP_QNAME_ANY_PROP_MULTIPLE, values);
checkValues = (List<Serializable>) nodeService.getProperty(
nodeRef, PROP_QNAME_ANY_PROP_MULTIPLE);
assertEquals("Expected 2 Collection values back", 2, checkValues.size());
assertTrue("Incorrect type in collection", checkValues.get(0) instanceof ArrayList); // ArrayList in - ArrayList out
assertTrue("Incorrect type in collection", checkValues.get(1) instanceof HashSet); // HashSet in - HashSet out
// Check that multi-valued d:any properties can be collections of collections (with values)
// We put ArrayLists and HashSets into the Collection of d:any, so that is exactly what should come out
arrayListVal.add("ONE");
arrayListVal.add("TWO");
hashSetVal.add("ONE");
hashSetVal.add("TWO");
values.clear();
values.add(arrayListVal);
values.add(hashSetVal);
nodeService.setProperty(nodeRef, PROP_QNAME_ANY_PROP_MULTIPLE, values);
checkValues = (List<Serializable>) nodeService.getProperty(
nodeRef, PROP_QNAME_ANY_PROP_MULTIPLE);
assertEquals("Expected 2 Collection values back", 2, checkValues.size());
assertTrue("Incorrect type in collection", checkValues.get(0) instanceof ArrayList); // ArrayList in - ArrayList out
assertTrue("Incorrect type in collection", checkValues.get(1) instanceof HashSet); // HashSet in - HashSet out
assertEquals("First collection incorrect", 2, ((Collection)checkValues.get(0)).size());
assertEquals("Second collection incorrect", 2, ((Collection)checkValues.get(1)).size());
}
finally
{
TestTransaction.flagForRollback();
TestTransaction.end();
}
}
@Test
public void testImportingSameNamespaceFails() throws Exception
{
// just to make sure we don't regress ACE-5852, some tests should run as System
AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getSystemUserName());
// Create model
final NodeRef modelNode = createActiveModel("dictionary/testModel.xml");
TestTransaction.flagForCommit();
TestTransaction.end();
// add second model to introduce self referencing dependency
TestTransaction.start();
PropertyMap properties = new PropertyMap(1);
properties.put(ContentModel.PROP_MODEL_ACTIVE, true);
final NodeRef modelNode2 = this.nodeService.createNode(
this.rootNodeRef,
ContentModel.ASSOC_CHILDREN,
QName.createQName(NamespaceService.ALFRESCO_URI, "dictionaryModels"),
ContentModel.TYPE_DICTIONARY_MODEL,
properties).getChildRef();
TestTransaction.flagForCommit();
TestTransaction.end();
try
{
transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {
public Void execute() throws Exception {
ContentWriter contentWriter = DictionaryModelTypeTest.this.contentService.getWriter(modelNode2, ContentModel.PROP_CONTENT, true);
contentWriter.putContent(Thread.currentThread().getContextClassLoader().getResourceAsStream("dictionary/testModel2.xml"));
return null;
}
});
fail("Validation should fail as a circular dependency was introduced");
} catch(AlfrescoRuntimeException e)
{
assertTrue(e.getCause().getMessage().contains("URI mage.model cannot be imported as it is already contained in the model's namespaces"));
}
//delete model
finally
{
transactionService.getRetryingTransactionHelper().doInTransaction(
new RetryingTransactionCallback<Object>() {
public Object execute() throws Exception {
// Delete the model
DictionaryModelTypeTest.this.nodeService.deleteNode(modelNode);
return null;
}
});
}
}
@Test
@Commit
public void givenTransactionCommitDefault_whenProgrammaticTransactionCommit_thenEntityIsInDatabase() {
assertTrue(TestTransaction.isActive());
//Save an entity and commit.
Session session = sessionFactory.getCurrentSession();
TestEntity newEntity = new TestEntity();
newEntity.setId(1);
session.save(newEntity);
TestEntity searchEntity = session.find(TestEntity.class, 1);
Assert.assertNotNull(searchEntity);
assertFalse(TestTransaction.isFlaggedForRollback());
TestTransaction.end();
assertFalse(TestTransaction.isFlaggedForRollback());
assertFalse(TestTransaction.isActive());
//Check that the entity is still there in a new transaction,
//then delete it, but don't commit.
TestTransaction.start();
assertFalse(TestTransaction.isFlaggedForRollback());
assertTrue(TestTransaction.isActive());
session = sessionFactory.getCurrentSession();
searchEntity = session.find(TestEntity.class, 1);
Assert.assertNotNull(searchEntity);
session.delete(searchEntity);
session.flush();
TestTransaction.flagForRollback();
TestTransaction.end();
assertFalse(TestTransaction.isActive());
//Check that the entity is still there in a new transaction,
//then delete it and commit.
TestTransaction.start();
session = sessionFactory.getCurrentSession();
searchEntity = session.find(TestEntity.class, 1);
Assert.assertNotNull(searchEntity);
session.delete(searchEntity);
session.flush();
assertTrue(TestTransaction.isActive());
TestTransaction.end();
assertFalse(TestTransaction.isActive());
//Check that the entity is no longer there in a new transaction.
TestTransaction.start();
assertTrue(TestTransaction.isActive());
session = sessionFactory.getCurrentSession();
searchEntity = session.find(TestEntity.class, 1);
Assert.assertNull(searchEntity);
}
@Test
public void whenProgrammaticTransactionCommit_thenEntityIsInDatabase() {
assertTrue(TestTransaction.isActive());
//Save an entity and commit.
Session session = sessionFactory.getCurrentSession();
TestEntity newEntity = new TestEntity();
newEntity.setId(1);
session.save(newEntity);
TestEntity searchEntity = session.find(TestEntity.class, 1);
Assert.assertNotNull(searchEntity);
assertTrue(TestTransaction.isFlaggedForRollback());
TestTransaction.flagForCommit();
TestTransaction.end();
assertFalse(TestTransaction.isFlaggedForRollback());
assertFalse(TestTransaction.isActive());
//Check that the entity is still there in a new transaction,
//then delete it, but don't commit.
TestTransaction.start();
assertTrue(TestTransaction.isFlaggedForRollback());
assertTrue(TestTransaction.isActive());
session = sessionFactory.getCurrentSession();
searchEntity = session.find(TestEntity.class, 1);
Assert.assertNotNull(searchEntity);
session.delete(searchEntity);
session.flush();
TestTransaction.end();
assertFalse(TestTransaction.isActive());
//Check that the entity is still there in a new transaction,
//then delete it and commit.
TestTransaction.start();
session = sessionFactory.getCurrentSession();
searchEntity = session.find(TestEntity.class, 1);
Assert.assertNotNull(searchEntity);
session.delete(searchEntity);
session.flush();
assertTrue(TestTransaction.isActive());
TestTransaction.flagForCommit();
TestTransaction.end();
assertFalse(TestTransaction.isActive());
//Check that the entity is no longer there in a new transaction.
TestTransaction.start();
assertTrue(TestTransaction.isActive());
session = sessionFactory.getCurrentSession();
searchEntity = session.find(TestEntity.class, 1);
Assert.assertNull(searchEntity);
}