Java Code Examples for org.springframework.test.context.transaction.TestTransaction#flagForRollback()

The following examples show how to use org.springframework.test.context.transaction.TestTransaction#flagForRollback() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProgrammaticTxMgmtTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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");
}
 
Example 2
Source File: ProgrammaticTxMgmtTestNGTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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");
}
 
Example 3
Source File: ProgrammaticTxMgmtTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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");
}
 
Example 4
Source File: ProgrammaticTxMgmtTestNGTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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");
}
 
Example 5
Source File: ProgrammaticTxMgmtTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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");
}
 
Example 6
Source File: ProgrammaticTxMgmtTestNGTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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");
}
 
Example 7
Source File: ProgrammaticTxMgmtTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test(expected = IllegalStateException.class)
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void flagForRollbackWithNonExistentTransactionContext() {
	TestTransaction.flagForRollback();
}
 
Example 8
Source File: ProgrammaticTxMgmtTestNGTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test(expectedExceptions = IllegalStateException.class)
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void flagForRollbackWithNonExistentTransactionContext() {
	TestTransaction.flagForRollback();
}
 
Example 9
Source File: ProgrammaticTxMgmtTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test(expected = IllegalStateException.class)
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void flagForRollbackWithNonExistentTransactionContext() {
	TestTransaction.flagForRollback();
}
 
Example 10
Source File: ProgrammaticTxMgmtTestNGTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test(expectedExceptions = IllegalStateException.class)
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void flagForRollbackWithNonExistentTransactionContext() {
	TestTransaction.flagForRollback();
}
 
Example 11
Source File: FullNodeServiceTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
@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();
    }
}
 
Example 12
Source File: ProgrammaticTxMgmtTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test(expected = IllegalStateException.class)
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void flagForRollbackWithNonExistentTransactionContext() {
	TestTransaction.flagForRollback();
}
 
Example 13
Source File: ProgrammaticTxMgmtTestNGTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test(expectedExceptions = IllegalStateException.class)
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void flagForRollbackWithNonExistentTransactionContext() {
	TestTransaction.flagForRollback();
}
 
Example 14
Source File: HibernateBootstrapIntegrationTest.java    From tutorials with MIT License 2 votes vote down vote up
@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);
}
 
Example 15
Source File: HibernateBootstrapIntegrationTest.java    From tutorials with MIT License 2 votes vote down vote up
@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);
}