/*
 * 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 java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import junit.framework.TestCase;
import woolpack.test.TestUtils;
import woolpack.utils.MapBuilder;

public class ValidatorContextTest extends TestCase {

	public void testAppendToInitial() throws IOException {
		final ValidatorContext context = new ValidatorContext();
		final StringBuilder sb = new StringBuilder();
		context.appendTo(sb);
		assertEquals(
				"ValidatorContext dump information:\n,id:\n,inputMap:\n,collectable:"
						+ "\n,tmpKey:\n,tmpIndex:0", sb.toString());
	}

	public void testAppendToFull() throws IOException {
		final ValidatorContext context = new ValidatorContext();
		context.setCollectable(new SimpleMessageCollector());
		context.setId("id0");
		context.setInputMap(MapBuilder.get(new HashMap<String, List<Object>>())
				.put("inputKey0", Arrays.asList((Object) "inputValue0")).get());
		context.setTmpKey("tmpKey0");
		context.setTmpIndex(1);
		context.add("message0");

		final StringBuilder sb = new StringBuilder();
		context.appendTo(sb);
		assertEquals("ValidatorContext dump information:" + "\n,id:id0"
				+ "\n,inputMap:{inputKey0=[inputValue0]}"
				+ "\n,collectable:[message0]" + "\n,tmpKey:tmpKey0"
				+ "\n,tmpIndex:1", sb.toString());
	}

	public void testTmp() {
		final ValidatorContext context = new ValidatorContext();
		final Map<String, List<Object>> map = new HashMap<String, List<Object>>();
		context.setInputMap(map);

		context.setTmpKey("key0");
		assertNull(context.getTmpValue());

		context.setTmpValue("value0");
		assertEquals("value0", context.getTmpValue());
		assertTrue(TestUtils.equals(MapBuilder.get(
				new HashMap<String, Object>()).put("key0",
				Arrays.asList("value0")).get(), context.getInputMap()));

		context.setTmpValue("value1");
		assertEquals("value1", context.getTmpValue());
		assertTrue(TestUtils.equals(MapBuilder.get(
				new HashMap<String, Object>()).put("key0",
				Arrays.asList("value1")).get(), context.getInputMap()));

		context.setTmpKey("key1");
		assertNull(context.getTmpValue());

		context.setTmpValue("value0");
		assertEquals("value0", context.getTmpValue());
		assertTrue(TestUtils.equals(MapBuilder.get(
				new HashMap<String, Object>()).put("key0",
				Arrays.asList("value1")).put("key1", Arrays.asList("value0"))
				.get(), context.getInputMap()));

		context.setTmpValue("value1");
		assertEquals("value1", context.getTmpValue());
		assertTrue(TestUtils.equals(MapBuilder.get(
				new HashMap<String, Object>()).put("key0",
				Arrays.asList("value1")).put("key1", Arrays.asList("value1"))
				.get(), context.getInputMap()));

		map.get("key1").add("value2");
		assertTrue(TestUtils
				.equals(MapBuilder.get(new HashMap<String, Object>()).put(
						"key0", Arrays.asList("value1")).put("key1",
						Arrays.asList("value1", "value2")).get(), context
						.getInputMap()));
		context.setTmpIndex(1);
		context.setTmpValue("value3");
		assertEquals("value3", context.getTmpValue());
		assertTrue(TestUtils
				.equals(MapBuilder.get(new HashMap<String, Object>()).put(
						"key0", Arrays.asList("value1")).put("key1",
						Arrays.asList("value1", "value3")).get(), context
						.getInputMap()));
	}

	public void testCopy() {
		final ValidatorContext context0 = new ValidatorContext();
		context0.setId("id0");
		context0.setInputMap(MapBuilder
				.get(new HashMap<String, List<Object>>()).put("inputKey0",
						Arrays.asList((Object) "inputValue0")).get());
		context0.setTmpKey("tmpKey0");
		context0.setTmpIndex(1);
		context0.setCollectable(new SimpleMessageCollector());
		context0.add("message0");

		final ValidatorContext context1 = context0.copy();
		assertEquals(context0.getId(), context1.getId());
		assertEquals(context0.getInputMap(), context1.getInputMap());
		assertEquals(context0.getCollectable(), context1.getCollectable());

		assertEquals(context0.getTmpKey(), context1.getTmpKey());
		assertEquals(context0.getTmpIndex(), context1.getTmpIndex());
	}
}
