/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.apache.aries.transaction.itests;

import static org.junit.Assert.assertEquals;

import java.sql.SQLException;
import javax.inject.Inject;
import javax.transaction.Status;

import org.apache.aries.transaction.test.RollbackOnBean;
import org.apache.aries.transaction.test.TestBean;
import org.junit.Before;
import org.junit.Test;
import org.ops4j.pax.exam.util.Filter;

public class RollbackOnTest extends AbstractIntegrationTest {

	@Inject
	@Filter(timeout = 120000, value = "(tranAttribute=Required)")
	TestBean rBean;

	@Inject
	RollbackOnBean rollbackOnBean;

	@Before
	public void setUp() throws Exception {
		rollbackOnBean.setrBean(rBean);
	}

	//default behavior, doesn't rollback on application exception
	@Test
	public void testNoRollbackOnDefaultAppException() throws Exception {
		tran.begin();
		try {
			rBean.insertRow("test", 1, new SQLException());
		} catch (SQLException e) {
			// Ignore expected
		}
		int txStatus = tran.getStatus();
		tran.rollback();
		assertEquals("tx was rolled back", Status.STATUS_ACTIVE, txStatus);
	}

	//default behavior, doesn't rollback on exception
	@Test
	public void testNoRollbackOnDefaultException() throws Exception {
		tran.begin();
		try {
			rollbackOnBean.throwException("test", 2);
		} catch (Exception e) {
			// Ignore expected
		}
		int txStatus = tran.getStatus();
		tran.rollback();
		assertEquals("tx was rolled back", Status.STATUS_ACTIVE, txStatus);
	}

	@Test
	public void testExceptionRollbackOnException() throws Exception {
		tran.begin();
		try {
			rollbackOnBean.throwExceptionRollbackOnException("noAnnotationDefaultException", -1);
		} catch (Exception e) {
			// Ignore expected
		}
		int txStatus = tran.getStatus();
		tran.rollback();
		assertEquals("tx was not rolled back", Status.STATUS_MARKED_ROLLBACK, txStatus);
	}

	@Test
	public void testNoRollbackOnDefaultRuntimeException() throws Exception {
		tran.begin();
		try {
			rBean.insertRow("test", 1, new RuntimeException());
		} catch (Exception e) {
			// Ignore expected
		}
		int txStatus = tran.getStatus();
		tran.rollback();
		assertEquals("tx was not rolled back", Status.STATUS_MARKED_ROLLBACK, txStatus);
	}

	//throw Runtime / handle Exception
	@Test
	public void testDontRollbackOnRuntimeException() throws Exception {
		int initialRows = counter.countRows();
		tran.begin();
		try {
			rollbackOnBean.throwRuntimeExceptionDontRollbackOnException("testDontRollbackOnRuntimeException", 3);
		} catch (Exception e) {
			//ignore
		}
		try {
			tran.commit();
		} catch (Exception e) {
			if (Status.STATUS_ACTIVE == tran.getStatus()) {
				tran.rollback();
			}
			//expected
		}
		int finalRows = counter.countRows();
		assertEquals("Added rows", 0, finalRows - initialRows);
	}

	@Test
	public void testRollbackOnRuntimeException() throws Exception {
		int initialRows = counter.countRows();
		tran.begin();
		try {
			rollbackOnBean.throwRuntimeExceptionRollbackOnException("testRollbackOnRuntimeException", 4);
		} catch (Exception e) {
			//ignore
		}
		try {
			tran.commit();
		} catch (Exception e) {
			if (Status.STATUS_ACTIVE == tran.getStatus()) {
				tran.rollback();
			}
			//expected
		}
		int finalRows = counter.countRows();
		assertEquals("Added rows", 0, finalRows - initialRows);
	}

	//throw runtime / handle AppException
	@Test
	public void testThrowRuntimeDontRollbackOnAppException() throws Exception {
		int initialRows = counter.countRows();
		tran.begin();
		try {
			rollbackOnBean.throwRuntimeExceptionDontRollbackOnAppException("testThrowRuntimeDontRollbackOnAppException", 5);
			tran.commit();
		} catch (Exception e) {
			//ignore
		}
		try {
			tran.commit();
		} catch (Exception e) {
			if (Status.STATUS_ACTIVE == tran.getStatus()) {
				tran.rollback();
			}
			//expected
		}
		int finalRows = counter.countRows();
		assertEquals("Added rows", 0, finalRows - initialRows);
	}

	@Test
	public void testThrowRuntimeRollbackOnException() throws Exception {
		int initialRows = counter.countRows();
		tran.begin();
		try {
			rollbackOnBean.throwRuntimeExceptionRollbackOnAppException("testThrowRuntimeRollbackOnException", 6);
		} catch (Exception e) {
			//ignore
		}
		try {
			tran.commit();
		} catch (Exception e) {
			if (Status.STATUS_ACTIVE == tran.getStatus()) {
				tran.rollback();
			}
			//expected
		}
		int finalRows = counter.countRows();
		assertEquals("Added rows", 0, finalRows - initialRows);
	}


	//throw AppException / handle Exception
	@Test
	public void testThrowAppExceptionRollbackOnException() throws Exception {
		int initialRows = counter.countRows();
		tran.begin();
		try {
			rollbackOnBean.throwApplicationExceptionRollbackOnException("testThrowAppExceptionRollbackOnException", 7);
		} catch (SQLException e) {
			//ignore
		}
		try {
			tran.commit();
		} catch (Exception e) {
			if (Status.STATUS_ACTIVE == tran.getStatus()) {
				tran.rollback();
			}
			//expected
		}
		int finalRows = counter.countRows();
		assertEquals("Added rows", 0, finalRows - initialRows);
	}

	@Test
	public void testThrowAppExceptionDontRollbackOnException() throws Exception {
		int initialRows = counter.countRows();
		tran.begin();
		try {
			rollbackOnBean.throwApplicationExceptionDontRollbackOnException("testThrowAppExceptionDontRollbackOnException", 8);
		} catch (SQLException e) {
			//ignore
		}
		tran.commit();
		int finalRows = counter.countRows();
		assertEquals("Added rows", 1, finalRows - initialRows);
	}


	//throw AppException / handle AppException
	@Test
	public void testThrowAppExceptionDontRollbackOnAppException() throws Exception {
		int initialRows = counter.countRows();
		tran.begin();
		try {
			rollbackOnBean.throwApplicationExceptionDontRollbackOnAppException("testThrowAppExceptionDontRollbackOnAppException", 9);
		} catch (SQLException e) {
			//ignore
		}
		tran.commit();
		int finalRows = counter.countRows();
		assertEquals("Added rows", 1, finalRows - initialRows);
	}

	@Test
	public void testThrowAppExceptionRollbackOnAppException() throws Exception {
		int initialRows = counter.countRows();
		tran.begin();
		try {
			rollbackOnBean.throwApplicationExceptionRollbackOnAppException("testThrowAppExceptionRollbackOnAppException", 10);
		} catch (SQLException e) {
			//ignore
		}
		try {
			tran.commit();
		} catch (Exception e) {
			if (Status.STATUS_ACTIVE == tran.getStatus()) {
				tran.rollback();
			}
			//expected
		}
		int finalRows = counter.countRows();
		assertEquals("Added rows", 0, finalRows - initialRows);
	}


	//throw Exception, handle Exception + DontRollBackOn
	@Test
	public void testThrowRuntimeExceptionRollbackExceptionDontRollbackOnAppException() throws Exception {
		int initialRows = counter.countRows();
		tran.begin();
		try {
			rollbackOnBean
					.throwExceptionRollbackOnExceptionDontRollbackOnAppException("testThrowRuntimeExceptionRollbackExceptionDontRollbackOnAppException", 11);
		} catch (Exception e) {
			//ignore
		}
		try {
			tran.commit();
		} catch (Exception e) {
			if (Status.STATUS_ACTIVE == tran.getStatus()) {
				tran.rollback();
			}
			//expected
		}
		int finalRows = counter.countRows();
		assertEquals("Added rows", 0, finalRows - initialRows);
	}

	@Test
	public void testThrowRuntimeExceptionDontRollbackExceptionRollbackOnAppException() throws Exception {
		int initialRows = counter.countRows();
		tran.begin();
		try {
			rollbackOnBean
					.throwExceptionRollbackOnAppExceptionDontRollbackOnException("testThrowRuntimeExceptionDontRollbackExceptionRollbackOnAppException", 12);
		} catch (Exception e) {
			//ignore
		}
		try {
			tran.commit();
		} catch (Exception e) {
			if (Status.STATUS_ACTIVE == tran.getStatus()) {
				tran.rollback();
			}
			//expected
		}
		int finalRows = counter.countRows();
		assertEquals("Added rows", 0, finalRows - initialRows);
	}

	@Test
	public void testThrowAppExceptionDontRollbackExceptionRollbackOnAppException() throws Exception {
		int initialRows = counter.countRows();
		tran.begin();
		try {
			rollbackOnBean
					.throwAppExceptionRollbackOnAppExceptionDontRollbackOnException("testThrowAppExceptionDontRollbackExceptionRollbackOnAppException", 13);
		} catch (SQLException e) {
			//ignore
		}
		try {
			tran.commit();
		} catch (Exception e) {
			//expected
			if (Status.STATUS_ACTIVE == tran.getStatus()) {
				tran.rollback();
			}
		}
		int finalRows = counter.countRows();
		assertEquals("Added rows", 1, finalRows - initialRows);
	}

	@Test
	public void testThrowAppExceptionRollbackExceptionDontRollbackOnAppException() throws Exception {
		int initialRows = counter.countRows();
		tran.begin();
		try {
			rollbackOnBean
					.throwAppExceptionRollbackOnExceptionDontRollbackOnAppException("testThrowAppExceptionRollbackExceptionDontRollbackOnAppException", 14);
		} catch (Exception e) {
			//ignore
		}
		try {
			tran.commit();
		} catch (Exception e) {
			//expected
			if (Status.STATUS_ACTIVE == tran.getStatus()) {
				tran.rollback();
			}
		}
		int finalRows = counter.countRows();
		assertEquals("Added rows", 1, finalRows - initialRows);
	}


	@Override
	protected TestBean getBean() {
		return rBean;
	}

}
