/*
 * Java Genetic Algorithm Library (@!identifier!@).
 * Copyright (c) @!year!@ Franz Wilhelmstötter
 *  
 * 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Author:
 *     Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
 *     
 */
package org.jenetics;

import java.io.Serializable;
import java.util.Random;

import javolution.xml.XMLSerializable;

import org.testng.Assert;
import org.testng.annotations.Test;

import org.jenetics.util.Array;
import org.jenetics.util.ConstantRandom;
import org.jenetics.util.Factory;
import org.jenetics.util.RandomRegistry;

/**
 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
 * @version $Id$
 */
public abstract class ObjectTester {
	
	protected abstract Factory<?> getFactory();
	
	private Array<Object> newSameObjects(final int nobjects) {
		final Array<Object> objects = new Array<Object>(nobjects);
		
		final Random constant = new ConstantRandom(12312309);
		final Random original = RandomRegistry.getRandom();
		RandomRegistry.setRandom(constant);
		try {
			for (int i = 0; i < nobjects; ++i) {
				objects.set(i, getFactory().newInstance());
			}
		} finally {
			RandomRegistry.setRandom(original);
		}
		
		return objects;
	}
	
	@Test
	public void equals() {
		final Array<Object> same = newSameObjects(5);
		
		final Object that = same.get(0);
		for (int i = 1; i < same.length(); ++i) {
			final Object other = same.get(i);
			
			Assert.assertEquals(other, other);
			Assert.assertEquals(other, that);
			Assert.assertEquals(that, other);
			Assert.assertFalse(other.equals(null));
		}
	}
	
	@Test
	public void notEquals() {
		for (int i = 0; i < 10; ++i) {
			final Object that = getFactory().newInstance();
			final Object other = getFactory().newInstance();
			
			if (that.equals(other)) {
				Assert.assertTrue(other.equals(that));
				Assert.assertEquals(that.hashCode(), other.hashCode());
			} else {
				Assert.assertFalse(other.equals(that));
			}
		}
	}
	
	@Test
	public void notEqualsDifferentType() {
		final Object that = getFactory().newInstance();
		Assert.assertFalse(that.equals(""));
		Assert.assertFalse(that.equals(23));
	}

	@Test
	public void hashcode() {
		final Array<Object> same = newSameObjects(5);
		
		final Object that = same.get(0);
		for (int i = 1; i < same.length(); ++i) {
			final Object other = same.get(i);
			
			Assert.assertEquals(that.hashCode(), other.hashCode());
		}
	}
	
	@Test
	public void tostring() {
		final Array<Object> same = newSameObjects(5);
		
		final Object that = same.get(0);
		for (int i = 1; i < same.length(); ++i) {
			final Object other = same.get(i);
			
			Assert.assertEquals(that.toString(), other.toString());
			Assert.assertNotNull(other.toString());
		}
	}
	
	@Test
	public void xmlSerialize() throws Exception {
		final Object object = getFactory().newInstance();
		
		if (object instanceof XMLSerializable) {
			for (int i = 0; i < 10; ++i) {
				final XMLSerializable serializable = 
					(XMLSerializable)getFactory().newInstance();
				
				SerializeUtils.testXMLSerialization(serializable);
			}
		}
	}
	
	@Test
	public void objectSerialize() throws Exception {
		final Object object = getFactory().newInstance();
		
		if (object instanceof Serializable) {
			for (int i = 0; i < 10; ++i) {
				final Serializable serializable = 
					(Serializable)getFactory().newInstance();
				
				SerializeUtils.testSerialization(serializable);
			}
		}
	}
	
}








@@ -976,16 +976,17 @@
 izable;%0A
+%0A
 import j
@@ -991,23 +991,37 @@
  jav
-a.util.Random;%0A
+olution.context.LocalContext;
 %0Aimp
@@ -1061,71 +1061,8 @@
 e;%0A%0A
-import org.testng.Assert;%0Aimport org.testng.annotations.Test;%0A%0A
 impo
@@ -1204,16 +1204,78 @@
 egistry;
+%0Aimport org.testng.Assert;%0Aimport org.testng.annotations.Test;
 %0A%0A/**%0A *
@@ -1591,88 +1591,41 @@
 %09%0A%09%09
-final Random constant = new ConstantRandom(12312309);%0A%09%09final Random original = 
+LocalContext.enter();%0A%09%09try %7B%0A%09%09%09
 Rand
@@ -1635,17 +1635,17 @@
 egistry.
-g
+s
 etRandom
@@ -1649,56 +1649,42 @@
 dom(
-);%0A%09%09RandomRegistry.setRandom(constant);%0A%09%09try %7B
+new ConstantRandom(12312309));%0A%09%09%09
 %0A%09%09%09
@@ -1794,41 +1794,26 @@
 %0A%09%09%09
-RandomRegistry.setRandom(original
+LocalContext.exit(
 );%0A%09

Fix ObjectTester test base class.