/*
 * Copyright 2006 Takahiro Nakamura.
 *
 * Licensed 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 woolpack.validator;

import junit.framework.TestCase;

public class AddressedMessageTest extends TestCase {
	
	public void testEquals(){
		{
			final AddressedMessage obj = new AddressedMessage("key0", 1, "message0", new Integer(1));
			assertTrue(obj.equals(obj));
		}
		assertEquals(new AddressedMessage("key0", 1, "message0", new Integer(1)), new AddressedMessage("key0", 1, "message0", new Integer(1)));
		assertFalse(new AddressedMessage("key0", 1, "message0", new Integer(1)).equals(new AddressedMessage("key1", 1, "message0", new Integer(1))));
		assertFalse(new AddressedMessage("key0", 1, "message0", new Integer(1)).equals(new AddressedMessage("key0", 2, "message0", new Integer(1))));
		assertFalse(new AddressedMessage("key0", 1, "message0", new Integer(1)).equals(new AddressedMessage("key0", 1, "message1", new Integer(1))));
		assertFalse(new AddressedMessage("key0", 1, "message0", new Integer(1)).equals(new AddressedMessage("key0", 1, "message0", new Integer(2))));
		assertFalse(new AddressedMessage("key0", 1, "message0", new Integer(1)).equals(new Object()));
		assertFalse(new AddressedMessage("key0", 1, "message0", new Integer(1)).equals(null));
	}
	
	public void testHashCode(){
		assertTrue(new AddressedMessage("key0", 1, "message0", new Integer(1)).hashCode() == new AddressedMessage("key0", 1, "message0", new Integer(1)).hashCode());
		assertTrue(new AddressedMessage("key0", 1, "message0", new Integer(1)).hashCode() != new AddressedMessage("key1", 1, "message0", new Integer(1)).hashCode());
		assertTrue(new AddressedMessage("key0", 1, "message0", new Integer(1)).hashCode() != new AddressedMessage("key0", 2, "message0", new Integer(1)).hashCode());
		assertTrue(new AddressedMessage("key0", 1, "message0", new Integer(1)).hashCode() != new AddressedMessage("key0", 1, "message1", new Integer(1)).hashCode());
		assertTrue(new AddressedMessage("key0", 1, "message0", new Integer(1)).hashCode() != new AddressedMessage("key0", 1, "message0", new Integer(2)).hashCode());
	}

	public void testToString(){
		{
			final AddressedMessage m = new AddressedMessage();
			m.setKey("key0");
			m.setIndex(1);
			m.setMessage("message0");
			m.setValue(new Integer(1));
			assertEquals("key0", m.getKey());
			assertEquals(1, m.getIndex());
			assertEquals("message0", m.getMessage());
			assertEquals(new Integer(1), m.getValue());
			assertEquals(
					"woolpack.validator.AddressedMessage@@[key=key0,index=1,message=message0,value=1]", 
					m.toString().replaceAll("@[a-z0-9]+", "@@"));
		}
		{
			final AddressedMessage m = new AddressedMessage("key0", 1, "message0", new Integer(1));
			assertEquals("key0", m.getKey());
			assertEquals(1, m.getIndex());
			assertEquals("message0", m.getMessage());
			assertEquals(new Integer(1), m.getValue());
			assertEquals(
					"woolpack.validator.AddressedMessage@@[key=key0,index=1,message=message0,value=1]", 
					m.toString().replaceAll("@[a-z0-9]+", "@@"));
		}
	}
}
