/**
 * License Agreement.
 *
 *  JBoss RichFaces - Ajax4jsf Component Library
 *
 * Copyright (C) 2007  Exadel, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation.
 *
 * 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 Street, Fifth Floor, Boston, MA 02110-1301  USA
 */

package org.richfaces.component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import junit.framework.TestCase;

import org.apache.commons.collections.iterators.ArrayIterator;
import org.richfaces.model.ListRowKey;
import org.richfaces.model.TreeRowKey;

/**
 * @author Nick Belaevski - nbelaevski@exadel.com
 * created 09.04.2007
 * 
 */
public class ListRowKeyTest extends TestCase {

	public void testConstructors() {
		ListRowKey key = new ListRowKey();
		
		assertEquals(0, key.depth());
		assertEquals(0, key.getPath().length());

		List list = new LinkedList();
		list.add(new Long(2));
		list.add(new Long(4));

		key = new ListRowKey(list);
	
		assertEquals(2, key.depth());
		Iterator iterator = key.iterator();
		assertTrue(iterator.hasNext());
		assertEquals(new Long(2), iterator.next());

		assertTrue(iterator.hasNext());
		assertEquals(new Long(4), iterator.next());
		
		assertFalse(iterator.hasNext());
	
		key = new ListRowKey(new Long(5));
		assertEquals(1, key.depth());
		iterator = key.iterator();
		assertTrue(iterator.hasNext());
		assertEquals(new Long(5), iterator.next());

		assertFalse(iterator.hasNext());
		
		key = new ListRowKey(new ListRowKey(new Long(6)), new Long(7));
		assertEquals(2, key.depth());
		iterator = key.iterator();
		assertTrue(iterator.hasNext());
		assertEquals(new Long(6), iterator.next());

		assertTrue(iterator.hasNext());
		assertEquals(new Long(7), iterator.next());
		
		assertFalse(iterator.hasNext());

		key = new ListRowKey(key);
		assertEquals(2, key.depth());
		iterator = key.iterator();
		assertTrue(iterator.hasNext());
		assertEquals(new Long(6), iterator.next());

		assertTrue(iterator.hasNext());
		assertEquals(new Long(7), iterator.next());
		
		assertFalse(iterator.hasNext());
	
		key = new ListRowKey("12_:34:56__78_:9");
		assertEquals(2, key.depth());
		iterator = key.iterator();
		assertTrue(iterator.hasNext());
		assertEquals("12:34", iterator.next());

		assertTrue(iterator.hasNext());
		assertEquals("56_78:9", iterator.next());
		
		assertFalse(iterator.hasNext());

	
		key = new ListRowKey("12_:34:56__78_:9:");
		assertEquals(2, key.depth());
		iterator = key.iterator();
		assertTrue(iterator.hasNext());
		assertEquals("12:34", iterator.next());

		assertTrue(iterator.hasNext());
		assertEquals("56_78:9", iterator.next());
		
		assertFalse(iterator.hasNext());
	}
	
	public void testObjectMethods() {
		String[] data = new String[] {
			"test1", "test:2", "test3"	
		};
		
		ListRowKey key1 = new ListRowKey(Arrays.asList(data));
		
		ListRowKey key2 = new ListRowKey();
		for (int i = 0; i < data.length; i++) {
			String string = data[i];
		
			key2 = new ListRowKey(key2, string);
		}
		
		assertTrue(key1.hashCode() == key2.hashCode());
		assertTrue(key1.equals(key2));
		assertTrue(key1.toString().equals(key2.toString()));
	}

	public void testEqualsAndHash() {
		List keyBase = new ArrayList();
		keyBase.add(new Long(12));
		keyBase.add("string");
		
		List keyBase1 = new ArrayList();
		keyBase1.add(keyBase);
		keyBase1.add(new Double(23.56));
		keyBase1.add("moreStrings");
		
		List keyBase2 = new ArrayList();
		keyBase2.add(keyBase);
		keyBase2.add(new Double(23.56));
		keyBase2.add("moreStrings");
		
		ListRowKey rowKey = new ListRowKey(keyBase);
		ListRowKey rowKey1 = new ListRowKey(keyBase1);
		ListRowKey rowKey2 = new ListRowKey(keyBase2);
		
		assertTrue(rowKey.equals(rowKey));
		assertTrue(rowKey1.equals(rowKey1));
		assertTrue(rowKey2.equals(rowKey2));

		assertTrue(rowKey1.equals(rowKey2));
		assertTrue(rowKey2.equals(rowKey1));

		assertFalse(rowKey.equals(rowKey1));
		assertFalse(rowKey.equals(rowKey2));
		assertFalse(rowKey1.equals(rowKey));
		assertFalse(rowKey2.equals(rowKey));

		assertFalse(rowKey.equals(null));
		assertFalse(rowKey1.equals(null));
		assertFalse(rowKey2.equals(null));

		TreeRowKey treeKey = new TreeRowKey() {

			/**
			 * 
			 */
			private static final long serialVersionUID = 3323949145821721122L;

			public int depth() {
				throw new UnsupportedOperationException();
			}

			public int getCommonPathLength(TreeRowKey otherRowKey) {
				throw new UnsupportedOperationException();
			}

			public String getPath() {
				throw new UnsupportedOperationException();
			}

			public Iterator getSubPathIterator(int fromIndex) {
				throw new UnsupportedOperationException();
			}

			public Iterator iterator() {
				throw new UnsupportedOperationException();
			}
			
		};		

		assertFalse(rowKey.equals(treeKey));
		assertFalse(rowKey1.equals(treeKey));
		assertFalse(rowKey2.equals(rowKey));
		
		assertTrue(rowKey1.hashCode() == rowKey2.hashCode());

		assertFalse(rowKey.hashCode() == rowKey1.hashCode());
		assertFalse(rowKey.hashCode() == rowKey2.hashCode());
	}

	public void testSubPathIterator() throws Exception {
		List keyBase = new ArrayList();
		keyBase.add(new Long(13));
		keyBase.add("string");
		
		ListRowKey listRowKey = new ListRowKey(keyBase);
		Iterator iterator = listRowKey.getSubPathIterator(0);
		assertTrue(iterator.hasNext());
		assertEquals(new Long(13), iterator.next());
		assertTrue(iterator.hasNext());
		assertEquals("string", iterator.next());
		assertFalse(iterator.hasNext());
		
		iterator = listRowKey.getSubPathIterator(1);
		assertTrue(iterator.hasNext());
		assertEquals("string", iterator.next());
		assertFalse(iterator.hasNext());

		iterator = listRowKey.getSubPathIterator(2);
		assertFalse(iterator.hasNext());
	}

	public void testIsSubkey() throws Exception {
		List keyBase = new ArrayList();
		keyBase.add(new Long(12));

		ListRowKey listRowKey = new ListRowKey(keyBase);
		
		keyBase.add("string");
		
		ListRowKey listRowKey1 = new ListRowKey(keyBase);
	
		assertTrue(new ListRowKey().isSubKey(listRowKey1));
		assertTrue(new ListRowKey().isSubKey(listRowKey));
		assertFalse(listRowKey.isSubKey(new ListRowKey()));
		assertFalse(listRowKey1.isSubKey(new ListRowKey()));

		assertTrue(listRowKey.isSubKey(listRowKey1));
		assertFalse(listRowKey1.isSubKey(listRowKey));

		assertTrue(listRowKey.isSubKey(listRowKey));
		assertTrue(listRowKey1.isSubKey(listRowKey1));

		assertFalse(listRowKey.isSubKey(null));
		assertFalse(listRowKey1.isSubKey(null));
	
		TreeRowKey treeRowKey = new TreeRowKey() {

			/**
			 * 
			 */
			private static final long serialVersionUID = -6975483562771738488L;

			private Object[] data = new Object[] {
				new Long(12),
				"string",
				new Double(3.14159265)
			};
			
			public int depth() {
				return 3;
			}

			public int getCommonPathLength(TreeRowKey otherRowKey) {
				throw new UnsupportedOperationException();
			}

			public String getPath() {
				return "12:string:3.14159265";
			}

			public Iterator getSubPathIterator(int fromIndex) {
				throw new UnsupportedOperationException();
			}

			public Iterator iterator() {
				return new ArrayIterator(data);
			}
			
		};

		assertTrue(listRowKey.isSubKey(treeRowKey));
		assertTrue(listRowKey1.isSubKey(treeRowKey));

		assertFalse(treeRowKey.isSubKey(listRowKey));
		assertFalse(treeRowKey.isSubKey(listRowKey));
	}
}
