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

Java tutorial

Introduction

Here is the source code for de.perdian.commons.lang.conversion.impl.converters.TestStringToNumberConverter.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.DecimalFormat;
import java.util.Locale;

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

import de.perdian.commons.lang.conversion.ConverterException;

public class TestStringToNumberConverter {

    @Test
    public void testConvertWithEmptyConstructor() {
        StringToNumberConverter converter = new StringToNumberConverter();
        Assert.assertEquals(1, converter.convert("1").intValue());
    }

    @Test(expected = ConverterException.class)
    public void testConverteWithEmptyConstructorInvalidSource() {
        StringToNumberConverter converter = new StringToNumberConverter();
        converter.convert("INVALID");
    }

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

    @Test
    public void testConvertWithFormat() {
        StringToNumberConverter converter = new StringToNumberConverter(new DecimalFormat("00"));
        Assert.assertEquals(42, converter.convert("42").intValue());
    }

    @Test
    public void testConvertWithPattern() {
        StringToNumberConverter converter = new StringToNumberConverter("000");
        Assert.assertEquals(42, converter.convert("042").intValue());
    }

    @Test
    public void testConvertWithPatternAndLocaleDE() {
        StringToNumberConverter converter = new StringToNumberConverter("0.0",
                new SimpleLocaleContext(Locale.GERMANY));
        Assert.assertEquals(2.2d, converter.convert("2,2").doubleValue(), 0);
    }

    @Test
    public void testConvertWithPatternAndLocaleEN() {
        StringToNumberConverter converter = new StringToNumberConverter("0.0",
                new SimpleLocaleContext(Locale.ENGLISH));
        Assert.assertEquals(2.2d, converter.convert("2.2").doubleValue(), 0);
    }

}