nz.co.senanque.vaadin.formatting.FormatterDoubleTest.java Source code

Java tutorial

Introduction

Here is the source code for nz.co.senanque.vaadin.formatting.FormatterDoubleTest.java

Source

/*******************************************************************************
 * Copyright (c)2014 Prometheus Consulting
 *
 * 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 nz.co.senanque.vaadin.formatting;

import static org.junit.Assert.assertEquals;

import java.text.NumberFormat;

import nz.co.senanque.vaadinsupport.formatting.FormatterDouble;

import org.junit.Test;
import org.springframework.context.i18n.LocaleContextHolder;

public class FormatterDoubleTest {

    private NumberFormat nf = NumberFormat.getInstance(LocaleContextHolder.getLocale());

    private FormatterDouble fd = new FormatterDouble();

    @Test
    public void testParse() throws Exception {
        parse("10,000.50");
        parse("10000.50");
        parse("x1000");
        parse("1000x");
        parse("1000..");
        parse(null);
    }

    private Object parse(String n) {
        try {
            return fd.parse(n);
        } catch (Exception e) {
            System.out.println(n + " error");
            return null;
        }
        //      ParsePosition parsePosition = new ParsePosition(0);
        //        Object o = nf.parseObject(n,parsePosition);
        //        if (parsePosition.getIndex() != n.length())
        //        {
        //           System.out.println(n+" error");
        //        }
        //        else
        //        {
        //           System.out.println(n+" OK");
        //        }
    }

    private class TestIdentityObject {
        long m_long;
        String m_string;

        TestIdentityObject(long l) {
            m_long = l;
        }

        public long getLong() {
            return m_long;
        }

        public void setLong(long l) {
            m_long = l;
        }

        public String getString() {
            return m_string;
        }

        public void setString(String string) {
            m_string = string;
        }

        public int hashCode() {
            return 15;
        }
    }

    @Test
    public void testIdentity() {
        TestIdentityObject l = new TestIdentityObject(100L);
        int i1 = System.identityHashCode(l);
        l.setLong(100L);
        l.setString("hello");
        int i2 = System.identityHashCode(l);
        assertEquals(i1, i2);
    }

}