package com.appslandia.common.formatters;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.appslandia.common.base.FormatProvider;
import com.appslandia.common.base.FormatProviderImpl;
import com.appslandia.common.base.Language;

public class BooleanFormatterTest {

	BooleanFormatter booleanFormatter;

	@Before
	public void initialize() {
		booleanFormatter = new BooleanFormatter();
		Assert.assertEquals(booleanFormatter.getArgumentType(), Boolean.class);
	}

	@Test
	public void test() {
		FormatProvider formatProvider = new FormatProviderImpl(Language.getCurrent());
		try {
			Boolean v = booleanFormatter.parse("true", formatProvider);
			Assert.assertTrue(v);
		} catch (Exception ex) {
			Assert.fail(ex.getMessage());
		}

		try {
			Boolean v = booleanFormatter.parse("false", formatProvider);
			Assert.assertFalse(v);
		} catch (Exception ex) {
			Assert.fail(ex.getMessage());
		}
	}

	@Test
	public void test_null() {
		FormatProvider formatProvider = new FormatProviderImpl(Language.getCurrent());
		try {
			Boolean val = booleanFormatter.parse(null, formatProvider);
			Assert.assertNull(val);
		} catch (Exception ex) {
			Assert.fail(ex.getMessage());
		}
		try {
			booleanFormatter.parse("", formatProvider);
			Assert.fail();
		} catch (Exception ex) {
			Assert.assertTrue(ex instanceof FormatterException);
		}
	}

	@Test
	public void test_invalid() {
		FormatProvider formatProvider = new FormatProviderImpl(Language.getCurrent());
		try {
			booleanFormatter.parse("tru-e", formatProvider);
			Assert.fail();
		} catch (Exception ex) {
			Assert.assertTrue(ex instanceof FormatterException);
		}
	}
}
