de.perdian.commons.lang.conversion.impl.converters.TestDateToStringConverter.java Source code

Java tutorial

Introduction

Here is the source code for de.perdian.commons.lang.conversion.impl.converters.TestDateToStringConverter.java

Source

/*
 * Copyright 2013 Christian Robert
 *
 * 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 de.perdian.commons.lang.conversion.impl.converters;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import org.junit.Assert;
import org.junit.Test;
import org.springframework.context.i18n.SimpleLocaleContext;

public class TestDateToStringConverter {

    @Test
    public void testConvertWithFormat() throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateToStringConverter converter = new DateToStringConverter(dateFormat);
        Assert.assertEquals("2012-02-08 12:34:56", converter.convert(dateFormat.parse("2012-02-08 12:34:56")));
    }

    @Test
    public void testConvertWithEmptyConstructorNull() {
        DateToStringConverter converter = new DateToStringConverter();
        Assert.assertNull(converter.convert(null));
    }

    @Test
    public void testConvertWithPattern() {
        Date sourceDate = new Date(1362736759873L);
        DateToStringConverter converter = new DateToStringConverter("yyyy-MM-dd");
        Assert.assertEquals("2013-03-08", converter.convert(sourceDate));
    }

    @Test
    public void testConvertWithPatternAndLocaleDE() {
        Date sourceDate = new Date(1362736759873L);
        DateToStringConverter converter = new DateToStringConverter("EEEE",
                new SimpleLocaleContext(Locale.GERMANY));
        Assert.assertEquals("Freitag", converter.convert(sourceDate));
    }

    @Test
    public void testConvertWithPatternAndLocaleEN() {
        Date sourceDate = new Date(1362736759873L);
        DateToStringConverter converter = new DateToStringConverter("EEEE",
                new SimpleLocaleContext(Locale.ENGLISH));
        Assert.assertEquals("Friday", converter.convert(sourceDate));
    }

}