/*
 *    Copyright 2010 The myBatis Team
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.mybatis.spring.transaction;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.mybatis.spring.AbstractMyBatisSpringTest;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

/**
 * @version $Id: MapperFactoryBeanTest.java 3294 2010-11-28 04:01:46Z hpresnall $
 */
public final class SpringTransactionManagerTest extends AbstractMyBatisSpringTest {


    @Test
    public void testWithTx() throws Exception {
        DefaultTransactionDefinition txDef = new DefaultTransactionDefinition();
        txDef.setPropagationBehaviorName("PROPAGATION_REQUIRED");

        TransactionStatus status = txManager.getTransaction(txDef);
        SpringManagedTransactionFactory transactionFactory = new SpringManagedTransactionFactory(dataSource);
        SpringManagedTransaction transaction = (SpringManagedTransaction) transactionFactory.newTransaction(connection, false);
        transaction.commit();        
        assertEquals("should not call commit on Connection", 0, connection.getNumberCommits());

        txManager.commit(status);
    }
    
    @Test
    public void testWithNoTx() throws Exception {

        SpringManagedTransactionFactory transactionFactory = new SpringManagedTransactionFactory(dataSource);
        SpringManagedTransaction transaction = (SpringManagedTransaction) transactionFactory.newTransaction(connection, false);
        transaction.commit();
        
        assertEquals("should call commit on Connection", 1, connection.getNumberCommits());
        
        connection.close();
    }
    
}

@@ -1154,24 +1154,34 @@
 ic void test
+ShouldNoOp
 WithTx() thr
@@ -1197,16 +1197,16 @@
 tion %7B%0D%0A
-
         
@@ -1874,16 +1874,28 @@
 oid test
+ShouldCommit
 WithNoTx
@@ -2022,32 +2022,32 @@
 y(dataSource);%0D%0A
-
         SpringMa
@@ -2334,22 +2334,521 @@
 e();%0D%0A    %7D%0D
+%0A%0D%0A    @Test%0D%0A    public void testShouldIgnoreAutocommit() throws Exception %7B%0D%0A%0D%0A        SpringManagedTransactionFactory transactionFactory = new SpringManagedTransactionFactory(dataSource);%0D%0A        SpringManagedTransaction transaction = (SpringManagedTransaction) transactionFactory.newTransaction(connection, true);%0D%0A        transaction.commit();%0D%0A        %0D%0A        assertEquals(%22should call commit on Connection%22, 1, connection.getNumberCommits());%0D%0A        %0D%0A        connection.close();%0D%0A    %7D%0D
 %0A    %0D%0A%7D%0D%0A

test ignore autocommit