/*
 * Copyright (C) 2004-2007  exedio GmbH (www.exedio.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package com.exedio.cope.util;

import com.exedio.cope.AbstractLibTest;
import com.exedio.cope.CacheIsolationTest;
import com.exedio.cope.Transaction;

public class TransactionSlicerTest extends AbstractLibTest
{
	public TransactionSlicerTest()
	{
		super(CacheIsolationTest.MODEL);
	}
	
	public void testIt()
	{
		final Transaction t1 = model.getCurrentTransaction();
		
		final TransactionSlicer ts = new TransactionSlicer(model, 3);
		assertEquals(0, ts.getSliceCount());
		assertSame(t1, model.getCurrentTransaction());
		
		assertEquals(false, ts.sliceIfNeeded());
		assertEquals(0, ts.getSliceCount());
		assertSame(t1, model.getCurrentTransaction());
		assertFalse(t1.isClosed());
		
		assertEquals(false, ts.sliceIfNeeded());
		assertEquals(0, ts.getSliceCount());
		assertSame(t1, model.getCurrentTransaction());
		assertFalse(t1.isClosed());
		
		assertEquals(true, ts.sliceIfNeeded());
		assertEquals(1, ts.getSliceCount());
		final Transaction t2 = model.getCurrentTransaction();
		assertNotSame(t1, t2);
		assertTrue(t1.isClosed());
		assertFalse(t2.isClosed());
		assertEquals(t1.getName() + "-slice1", t2.getName());
		
		assertEquals(false, ts.sliceIfNeeded());
		assertEquals(1, ts.getSliceCount());
		assertSame(t2, model.getCurrentTransaction());
		assertTrue(t1.isClosed());
		assertFalse(t2.isClosed());
		
		assertEquals(false, ts.sliceIfNeeded());
		assertEquals(1, ts.getSliceCount());
		assertSame(t2, model.getCurrentTransaction());
		assertTrue(t1.isClosed());
		assertFalse(t2.isClosed());
		
		assertEquals(true, ts.sliceIfNeeded());
		assertEquals(2, ts.getSliceCount());
		final Transaction t3 = model.getCurrentTransaction();
		assertNotSame(t1, t2);
		assertNotSame(t1, t3);
		assertNotSame(t2, t3);
		assertTrue(t1.isClosed());
		assertTrue(t2.isClosed());
		assertFalse(t3.isClosed());
		assertEquals(t1.getName() + "-slice2", t3.getName());
	}

	public void testEmpty()
	{
		final Transaction t1 = model.getCurrentTransaction();
		
		final TransactionSlicer ts = new TransactionSlicer(model, 0);
		assertEquals(0, ts.getSliceCount());
		assertSame(t1, model.getCurrentTransaction());
		
		assertEquals(true, ts.sliceIfNeeded());
		assertEquals(1, ts.getSliceCount());
		final Transaction t2 = model.getCurrentTransaction();
		assertNotSame(t1, t2);
		assertTrue(t1.isClosed());
		assertFalse(t2.isClosed());
		assertEquals(t1.getName() + "-slice1", t2.getName());
		
		assertEquals(true, ts.sliceIfNeeded());
		assertEquals(2, ts.getSliceCount());
		final Transaction t3 = model.getCurrentTransaction();
		assertNotSame(t1, t2);
		assertNotSame(t1, t3);
		assertNotSame(t2, t3);
		assertTrue(t1.isClosed());
		assertTrue(t2.isClosed());
		assertFalse(t3.isClosed());
		assertEquals(t1.getName() + "-slice2", t3.getName());
	}

	public void testNoname()
	{
		model.commit();
		model.startTransaction(null);
		
		final Transaction t1 = model.getCurrentTransaction();
		
		final TransactionSlicer ts = new TransactionSlicer(model, 0);
		assertEquals(0, ts.getSliceCount());
		assertSame(t1, model.getCurrentTransaction());
		
		assertEquals(true, ts.sliceIfNeeded());
		assertEquals(1, ts.getSliceCount());
		final Transaction t2 = model.getCurrentTransaction();
		assertNotSame(t1, t2);
		assertTrue(t1.isClosed());
		assertFalse(t2.isClosed());
		assertEquals("slice1", t2.getName());
		
		assertEquals(true, ts.sliceIfNeeded());
		assertEquals(2, ts.getSliceCount());
		final Transaction t3 = model.getCurrentTransaction();
		assertNotSame(t1, t2);
		assertNotSame(t1, t3);
		assertNotSame(t2, t3);
		assertTrue(t1.isClosed());
		assertTrue(t2.isClosed());
		assertFalse(t3.isClosed());
		assertEquals("slice2", t3.getName());
	}
}

@@ -3547,32 +3547,230 @@
  t3.getName());%0A
+%09%09%0A%09%09try%0A%09%09%7B%0A%09%09%09new TransactionSlicer(model, -1);%0A%09%09%09fail();%0A%09%09%7D%0A%09%09catch(IllegalArgumentException e)%0A%09%09%7B%0A%09%09%09assertEquals(%22transactionsInSlice must not be negative, but was -1%22, e.getMessage());%0A%09%09%7D%0A
 %09%7D%0A%0A%09public void

test negative slice size