
package com.exedio.cope.lib;

import com.exedio.cope.lib.database.Database;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import junit.framework.TestCase;

public class LibTest extends TestCase
{
	
	final Type[] types = new Type[]
	{
		ItemWithSingleUnique.TYPE,
		ItemWithSingleUniqueReadOnly.TYPE,
		ItemWithSingleUniqueNotNull.TYPE,
	};
	
	public LibTest()
	{}
	
	public void setUp()
	{
		Database.theInstance.createTables();
	}
	
	public void tearDown()
	{
		Database.theInstance.dropTables();
	}
	
	public void testLib()
	{
		// BEWARE:
		// if something does not compile,
		// it may be an error in the 
		// instrumentor as well.
		
		
		// ItemWithSingleUnique
		{
			assertEquals(null, ItemWithSingleUnique.findByUniqueString("uniqueString"));

			final ItemWithSingleUnique itemWithSingleUnique = new ItemWithSingleUnique();
			assertEquals(null, itemWithSingleUnique.getUniqueString());
			assertEquals(null, ItemWithSingleUnique.findByUniqueString("uniqueString"));

			try
			{
				itemWithSingleUnique.setUniqueString("uniqueString");
			}
			catch(UniqueViolationException e)
			{
				throw new SystemException(e);
			}
			assertEquals(null, itemWithSingleUnique.getUniqueString());
			assertEquals(null, ItemWithSingleUnique.findByUniqueString("uniqueString"));
		}
		

		// ItemWithSingleUniqueReadOnly
		{
			assertEquals(null, ItemWithSingleUniqueReadOnly.findByUniqueReadOnlyString("uniqueString"));

			final ItemWithSingleUniqueReadOnly itemWithSingleUniqueReadOnly;
			try
			{
				itemWithSingleUniqueReadOnly = new ItemWithSingleUniqueReadOnly("uniqueString");
			}
			catch(UniqueViolationException e)
			{
				throw new SystemException(e);
			}
			assertEquals(null, itemWithSingleUniqueReadOnly.getUniqueReadOnlyString());
			assertEquals(null, ItemWithSingleUniqueReadOnly.findByUniqueReadOnlyString("uniqueString"));
			
			try
			{
				itemWithSingleUniqueReadOnly.setAttribute(itemWithSingleUniqueReadOnly.uniqueReadOnlyString, "zapp");
				fail("should have thrown ReadOnlyViolationException");
			}
			catch(ReadOnlyViolationException e)
			{
				assertEquals(itemWithSingleUniqueReadOnly.uniqueReadOnlyString, e.getReadOnlyAttribute());
				assertEquals(itemWithSingleUniqueReadOnly, e.getItem());
			}
			catch(ConstraintViolationException e)
			{
				throw new SystemException(e);
			}
		}

	
		// ItemWithSingleNotNull
		{
			assertEquals(null, ItemWithSingleUniqueNotNull.findByUniqueNotNullString("uniqueString"));
			assertEquals(null, ItemWithSingleUniqueNotNull.findByUniqueNotNullString("uniqueString2"));

			final ItemWithSingleUniqueNotNull item;
			try
			{
				item = new ItemWithSingleUniqueNotNull("uniqueString");
			}
			catch(UniqueViolationException e)
			{
				throw new SystemException(e);
			}
			catch(NotNullViolationException e)
			{
				throw new SystemException(e);
			}
			assertEquals(null, item.getUniqueNotNullString());
			assertEquals(null, ItemWithSingleUniqueNotNull.findByUniqueNotNullString("uniqueString"));
			assertEquals(null, ItemWithSingleUniqueNotNull.findByUniqueNotNullString("uniqueString2"));

			try
			{
				item.setUniqueNotNullString("uniqueString2");
			}
			catch(UniqueViolationException e)
			{
				throw new SystemException(e);
			}
			catch(NotNullViolationException e)
			{
				throw new SystemException(e);
			}
			assertEquals(null, item.getUniqueNotNullString());
			assertEquals(null, ItemWithSingleUniqueNotNull.findByUniqueNotNullString("uniqueString"));
			assertEquals(null, ItemWithSingleUniqueNotNull.findByUniqueNotNullString("uniqueString2"));
		}
	}

	
	protected Set toSet(final Collection collection)
	{
		return new HashSet(collection);
	}

	protected Set set()
	{
		return Collections.EMPTY_SET;
	}

}

@@ -843,32 +843,16 @@
 que item
-WithSingleUnique
  = new I
@@ -900,32 +900,16 @@
 ll, item
-WithSingleUnique
 .getUniq
@@ -943,36 +943,20 @@
 s(null, 
-I
+i
 tem
-WithSingleUnique
 .findByU
@@ -1006,32 +1006,16 @@
 %09%09%09%09item
-WithSingleUnique
 .setUniq
@@ -1152,32 +1152,16 @@
 ll, item
-WithSingleUnique
 .getUniq
@@ -1442,78 +1442,30 @@
 item
-WithSingleUniqueReadOnly;%0A%09%09%09try%0A%09%09%09%7B%0A%09%09%09%09itemWithSingleUniqueReadOnly
+;%0A%09%09%09try%0A%09%09%09%7B%0A%09%09%09%09item
  = n
@@ -1629,32 +1629,8 @@
 item
-WithSingleUniqueReadOnly
 .get
@@ -1778,74 +1778,26 @@
 item
-WithSingleUniqueReadOnly.setAttribute(itemWithSingleUniqueReadOnly
+.setAttribute(item
 .uni
@@ -1957,32 +1957,8 @@
 item
-WithSingleUniqueReadOnly
 .uni
@@ -2028,32 +2028,8 @@
 item
-WithSingleUniqueReadOnly
 , e.

rename item variables to item