package org.apache.deltaspike.test.jpa.api.transactional.multipleinjection.manual;

import org.apache.deltaspike.jpa.impl.transaction.ManagedUserTransactionResolver;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Specializes;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.NotSupportedException;
import javax.transaction.RollbackException;
import javax.transaction.Status;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;

/**
 * @author Mark Struberg
 */
@Specializes
@ApplicationScoped
public class MockUserTransactionResolver extends ManagedUserTransactionResolver {

    private MockUserTransaction mockTx;

    @PostConstruct
    public void resetTx() {
        mockTx = new MockUserTransaction();
    }

    @Override
    public MockUserTransaction resolveUserTransaction() {
        return mockTx;
    }

    public static class MockUserTransaction implements UserTransaction {
        private boolean begin = false;
        private boolean commit = false;
        private boolean rollback = false;
        private boolean rollBackOnly =false;

        private int status = Status.STATUS_NO_TRANSACTION;


        @Override
        public void begin() throws NotSupportedException, SystemException {
            this.begin = true;
            this.status = Status.STATUS_ACTIVE;
        }

        @Override
        public void commit() throws HeuristicMixedException, HeuristicRollbackException, IllegalStateException, RollbackException, SecurityException, SystemException {
            this.commit = true;
            this.status = Status.STATUS_COMMITTED;
        }

        @Override
        public int getStatus() throws SystemException {
            return status;
        }

        @Override
        public void rollback() throws IllegalStateException, SecurityException, SystemException {
            this.rollback = true;
            this.status = Status.STATUS_ROLLEDBACK;
        }

        @Override
        public void setRollbackOnly() throws IllegalStateException, SystemException {
            this.rollBackOnly = true;
            this.status = Status.STATUS_MARKED_ROLLBACK;
        }

        @Override
        public void setTransactionTimeout(int i) throws SystemException {
            // do nothing
        }

        public boolean isActive()
        {
            return begin && !(commit || rollback);
        }

        public boolean isBegin() {
            return begin;
        }

        public boolean isCommit() {
            return commit;
        }

        public boolean isRollback() {
            return rollback;
        }

        public boolean isRollBackOnly() {
            return rollBackOnly;
        }
    }
}

@@ -1,8 +1,810 @@
+/*%0A * Licensed to the Apache Software Foundation (ASF) under one%0A * or more contributor license agreements. See the NOTICE file%0A * distributed with this work for additional information%0A * regarding copyright ownership. The ASF licenses this file%0A * to you under the Apache License, Version 2.0 (the%0A * %22License%22); you may not use this file except in compliance%0A * with the License. You may obtain a copy of the License at%0A *%0A * http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing,%0A * software distributed under the License is distributed on an%0A * %22AS IS%22 BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY%0A * KIND, either express or implied. See the License for the%0A * specific language governing permissions and limitations%0A * under the License.%0A */%0A
 package 
@@ -1413,41 +1413,8 @@
 n;%0A%0A
-/**%0A * @author Mark Struberg%0A */%0A
 @Spe

add missing license header