/*
 * Copyright (c) 2005-2010 Grameen Foundation USA
 * All rights reserved.
 *
 * 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.
 *
 * See also http://www.apache.org/licenses/LICENSE-2.0.html for an
 * explanation of the license and how it is applied.
 */

package org.mifos.ui.ftl;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;

import org.joda.time.DateTime;
import org.junit.Test;

import freemarker.ext.beans.BeansWrapper;
import freemarker.template.TemplateModelException;

public class DateFormatterTest {

    @Test
    public void formatJavaDateInEnglish() throws TemplateModelException {
        List<Object> args = getJavaDateArgs(Locale.ENGLISH);
        DateFormatter formatter = new DateFormatter();
        String result = (String) formatter.exec(args);
        String expected = "January";
        assertEquals(expected, result);
    }

    @Test
    public void formatJavaDateInFrench() throws TemplateModelException {
        List<Object> args = getJavaDateArgs(Locale.FRENCH);
        DateFormatter formatter = new DateFormatter();
        String result = (String) formatter.exec(args);
        String expected = "janvier";
        assertEquals(expected, result);
    }

    @Test
    public void formatJodaDateInEnglish() throws TemplateModelException {
        List<Object> args = getJodaDateArgs(Locale.ENGLISH);
        DateFormatter formatter = new DateFormatter();
        String result = (String) formatter.exec(args);
        String expected = "January";
        assertEquals(expected, result);
    }

    @Test
    public void formatJodaDateInFrench() throws TemplateModelException {
        List<Object> args = getJodaDateArgs(Locale.FRENCH);
        DateFormatter formatter = new DateFormatter();
        String result = (String) formatter.exec(args);
        String expected = "janvier";
        assertEquals(expected, result);
    }

    @Test
    @SuppressWarnings("PMD.EmptyCatchBlock")
    public void wrongDateTypeShouldFail() throws TemplateModelException {
        List<Object> args = getFormatterArgs("not a date!", Locale.ENGLISH);
        DateFormatter formatter = new DateFormatter();
        try {
            formatter.exec(args);
            fail("Passing a non-date object should have raised an exception");
        } catch (IllegalArgumentException e) {
            // good! this is expected
        }
    }

    @Test
    @SuppressWarnings("PMD.EmptyCatchBlock")
    public void wrongNumberOfArgumentsShouldFail() throws TemplateModelException {
        DateFormatter formatter = new DateFormatter();
        List<Object> args = new ArrayList<Object>();
        try {
            formatter.exec(args);
            fail("Incorrect number of arguments should have raised an exception");
        } catch (IllegalArgumentException e) {
            // this is expected
        }
    }

    private List<Object> getFormatterArgs(Object date, Locale locale) throws TemplateModelException {
        BeansWrapper wrapper = BeansWrapper.getDefaultInstance();
        List<Object> args = new ArrayList<Object>();
        args.add(wrapper.wrap(date));
        args.add(wrapper.wrap("MMMM"));
        args.add(wrapper.wrap(locale));
        return args;
    }

    private List<Object> getJavaDateArgs(Locale locale) throws TemplateModelException {
        return getFormatterArgs(new GregorianCalendar(2010, Calendar.JANUARY, 1).getTime(), locale);
    }

    private List<Object> getJodaDateArgs(Locale locale) throws TemplateModelException {
        return getFormatterArgs(new DateTime(2010, 1, 1, 0, 0, 0, 0), locale);
    }
}
@@ -2542,32 +2542,73 @@
     %7D%0A%0A    @Test
+(expected=IllegalArgumentException.class)
 %0A    @SuppressWa
@@ -2843,34 +2843,16 @@
 tter();%0A
-        try %7B%0A    
         
@@ -2881,194 +2881,61 @@
     
-        fail(%22Passing a non-date object should have raised an exception%22);%0A        %7D catch (IllegalArgumentException e) %7B%0A            // good! this is expected%0A        %7D%0A    %7D%0A%0A    @Test
+%7D%0A%0A    @Test(expected=IllegalArgumentException.class)
 %0A   
@@ -3171,26 +3171,8 @@
 ();%0A
-        try %7B%0A    
     
@@ -3201,180 +3201,8 @@
 s);%0A
-            fail(%22Incorrect number of arguments should have raised an exception%22);%0A        %7D catch (IllegalArgumentException e) %7B%0A            // this is expected%0A        %7D%0A
     
@@ -3941,8 +3941,9 @@
 %0A    %7D%0A%7D
+%0A

Use standard JUnit 4 expected exception syntax.