/*
 * Copyright 2004-2009 the original author or authors.
 * 
 * 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.compass.spring.test.transaction;

import javax.transaction.UserTransaction;

import junit.framework.TestCase;
import org.compass.core.Compass;
import org.compass.core.CompassSession;
import org.compass.core.config.CompassConfiguration;
import org.compass.core.config.CompassEnvironment;
import org.compass.core.impl.ExistingCompassSession;
import org.compass.spring.transaction.SpringSyncTransactionFactory;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.jta.JotmFactoryBean;
import org.springframework.transaction.jta.JtaTransactionManager;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

/**
 * @author kimchy
 */
public class SpringSyncNoExplicitTransactionTests extends TestCase {

    private Compass compass;

    private JotmFactoryBean jotmFactoryBean;

    private JtaTransactionManager transactionManager;

    protected void setUp() throws Exception {

        jotmFactoryBean = new JotmFactoryBean();

        transactionManager = new JtaTransactionManager();
        transactionManager.setUserTransaction((UserTransaction) jotmFactoryBean.getObject());
        transactionManager.afterPropertiesSet();

        CompassConfiguration conf = new CompassConfiguration()
                .configure("/org/compass/spring/test/transaction/compass.springsync.cfg.xml");
        conf.getSettings().setBooleanSetting(CompassEnvironment.DEBUG, true);
        SpringSyncTransactionFactory.setTransactionManager(transactionManager);
        compass = conf.buildCompass();
        compass.getSearchEngineIndexManager().deleteIndex();
        compass.getSearchEngineIndexManager().verifyIndex();
    }

    protected void tearDown() throws Exception {
        compass.close();
        compass.getSearchEngineIndexManager().deleteIndex();

        jotmFactoryBean.destroy();
    }

    public void testWithCommitNoSessionOrTransactionManagment() throws Exception {

        final long id = 1;

        TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
        transactionTemplate.execute(new TransactionCallbackWithoutResult() {
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                CompassSession session = compass.openSession();
                A a = new A();
                a.setId(id);
                session.save(a);
                a = session.get(A.class, id);
                assertNotNull(a);

                CompassSession oldSession = session;
                session = compass.openSession();
                assertTrue(session instanceof ExistingCompassSession);
                assertTrue(oldSession == ((ExistingCompassSession) session).getActualSession());
                a = session.get(A.class, id);
                assertNotNull(a);
            }
        });

        transactionTemplate.execute(new TransactionCallbackWithoutResult() {
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                CompassSession session = compass.openSession();
                A a = session.get(A.class, id);
                assertNotNull(a);
            }
        });
    }


    public void testWithCommit() throws Exception {

        final long id = 1;

        TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
        transactionTemplate.execute(new TransactionCallbackWithoutResult() {
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                CompassSession session = compass.openSession();
                A a = new A();
                a.setId(id);
                session.save(a);
                a = session.get(A.class, id);
                assertNotNull(a);
                session.commit();

                CompassSession oldSession = session;
                session = compass.openSession();
                assertTrue(session instanceof ExistingCompassSession);
                assertTrue(oldSession == ((ExistingCompassSession) session).getActualSession());
                a = session.get(A.class, id);
                assertNotNull(a);
                session.commit();
            }
        });

        transactionTemplate.execute(new TransactionCallbackWithoutResult() {
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                CompassSession session = compass.openSession();
                A a = session.get(A.class, id);
                assertNotNull(a);
                session.commit();
            }
        });
    }

    public void testWithRollback() throws Exception {

        final long id = 1;

        TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
        transactionTemplate.execute(new TransactionCallbackWithoutResult() {
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                CompassSession session = compass.openSession();
                A a = new A();
                a.setId(id);
                session.save(a);
                a = session.get(A.class, id);
                assertNotNull(a);
                session.commit();

                CompassSession oldSession = session;
                session = compass.openSession();
                assertTrue(session instanceof ExistingCompassSession);
                assertTrue(oldSession == ((ExistingCompassSession) session).getActualSession());
                a = session.get(A.class, id);
                assertNotNull(a);
                session.commit();

                status.setRollbackOnly();
            }
        });

        transactionTemplate.execute(new TransactionCallbackWithoutResult() {
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                CompassSession session = compass.openSession();
                A a = session.get(A.class, id);
                assertNull(a);
                session.commit();
            }
        });
    }

    public void testWithSuspend() throws Exception {

        final long id = 1;

        TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
        transactionTemplate.execute(new TransactionCallbackWithoutResult() {
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                final CompassSession session = compass.openSession();
                A a = new A();
                a.setId(id);
                session.save(a);
                a = session.get(A.class, id);
                assertNotNull(a);
                session.commit();

                // start a new transaction
                TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
                transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
                transactionTemplate.execute(new TransactionCallbackWithoutResult() {
                    protected void doInTransactionWithoutResult(TransactionStatus status) {
                        CompassSession innerSession = compass.openSession();
                        assertTrue(session != innerSession);
                        A a = innerSession.get(A.class, id);
                        assertNull(a);
                        innerSession.commit();
                    }
                });
            }
        });
    }
}
@@ -962,32 +962,81 @@
 CompassSession;%0A
+import org.compass.core.util.FileHandlerMonitor;%0A
 import org.compa
@@ -1087,16 +1087,16 @@
 actory;%0A
-
 import o
@@ -1609,16 +1609,68 @@
 mpass;%0A%0A
+    private FileHandlerMonitor fileHandlerMonitor;%0A%0A
     priv
@@ -2412,24 +2412,153 @@
 dCompass();%0A
+%0A        fileHandlerMonitor = FileHandlerMonitor.getFileHandlerMonitor(compass);%0A        fileHandlerMonitor.verifyNoHandlers();%0A%0A
         comp
@@ -2723,16 +2723,16 @@
 ption %7B%0A
-
         
@@ -2744,24 +2744,73 @@
 ss.close();%0A
+%0A        fileHandlerMonitor.verifyNoHandlers();%0A%0A
         comp

test file handlers for tests