/*
 * 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.container;

import java.util.HashMap;
import java.util.Map;

import junit.framework.TestCase;
import woolpack.fn.Fn;
import woolpack.fn.FnUtils;
import woolpack.utils.Utils;

public class ContainerUtilsTest extends TestCase {
	public void testNormal() throws Exception {
		final ContainerContext<Object> context = new ContainerContext<Object>();
		context.setFn(FnUtils.exec(FnUtils.join(
				ContainerUtils.getKey(),
				FnUtils.switching(
						Utils.<Object, Fn<? super ContainerContext<Object>, ?, ? extends Exception>>
						map("key0", FnUtils.fix("value0"))
						.map("key1", FnUtils.fix("value1"))
						.map("key2", new Fn<ContainerContext<Object>, Object, Exception>() {
							public Object exec(final ContainerContext<Object> c) throws Exception {
								final Map<String, Object> map = new HashMap<String, Object>();
								map.put("myKey0", c.visit("key0"));
								map.put("myKey1", c.visit("key1"));
								return map;
							}
						})
				))));
		assertEquals("value0", context.visit("key0"));
		assertEquals("value1", context.visit("key1"));
		assertEquals(Utils.map("myKey0", "value0").map("myKey1", "value1"), context.visit("key2"));
	}
	
	public void testCyclic() throws Exception {
		final ContainerContext<Object> context = new ContainerContext<Object>();
		context.setFn(FnUtils.exec(FnUtils.join(
				ContainerUtils.getKey(),
				FnUtils.switching(
						Utils.<Object, Fn<? super ContainerContext<Object>, ?, ? extends Exception>>
						map("key0", FnUtils.fix("value0"))
						.map("key1", new Fn<ContainerContext<Object>, Object, Exception>() {
							public Object exec(final ContainerContext<Object> c) throws Exception {
								final Map<String, Object> map = new HashMap<String, Object>();
								map.put("myKey0", c.visit("key0"));
								map.put("myKey2", c.visit("key2"));
								return map;
							}
						})
						.map("key2", new Fn<ContainerContext<Object>, Object, Exception>() {
							public Object exec(final ContainerContext<Object> c) throws Exception {
								final Map<String, Object> map = new HashMap<String, Object>();
								map.put("myKey0", c.visit("key0"));
								map.put("myKey1", c.visit("key1"));
								return map;
							}
						})
				))));
		
		assertEquals("value0", context.visit("key0"));
		try {
			context.visit("key1");
			fail();
		} catch (final IllegalStateException e) {
			assertEquals("cyclic key : [key1, key2]", e.getMessage());
		}
		try {
			context.visit("key2");
			fail();
		} catch (final IllegalStateException e) {
			assertEquals("cyclic key : [key2, key1]", e.getMessage());
		}
	}
	
	public void testDepth() throws Exception {
		final ContainerContext<Object> context = new ContainerContext<Object>();
		context.setDepth(2);
		context.setFn(FnUtils.exec(FnUtils.join(
				ContainerUtils.getKey(),
				FnUtils.switching(
						Utils.<Object, Fn<? super ContainerContext<Object>, ?, ? extends Exception>>
						map("key0", FnUtils.fix("value0"))
						.map("key1", new Fn<ContainerContext<Object>, Object, Exception>() {
							public Object exec(final ContainerContext<Object> c) throws Exception{
								final Map<String, Object> map = new HashMap<String, Object>();
								map.put("myKey0", c.visit("key0"));
								return map;
							}
						})
						.map("key2", new Fn<ContainerContext<Object>, Object, Exception>() {
							public Object exec(final ContainerContext<Object> c) throws Exception{
								final Map<String, Object> map = new HashMap<String, Object>();
								map.put("myKey1", c.visit("key1"));
								return map;
							}
						})
				))));
		
		assertEquals("value0", context.visit("key0"));
		assertEquals(Utils.map("myKey0", "value0"), context.visit("key1"));
		try {
			context.visit("key2");
			fail();
		} catch (final IllegalStateException e) {
			assertEquals("depth out of bounds : [key2, key1]", e.getMessage());
		}
	}
}
