⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datelocaleconvertertestcase.java

📁 这是一个有关common beanutils 的源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.commons.beanutils.locale.converters;

import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.text.DateFormatSymbols;

import java.util.Locale;

import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.locale.BaseLocaleConverter;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;

/**
 * Test Case for the DateLocaleConverter class.
 *
 * @author Robert Burrell Donkin
 * @author Niall Pemberton
 * @version $Revision: 658830 $ $Date: 2008-05-21 20:56:21 +0100 (Wed, 21 May 2008) $
 */

public class DateLocaleConverterTestCase extends BaseLocaleConverterTestCase {

    /** All logging goes through this logger */
    private Log log = LogFactory.getLog(DateLocaleConverterTestCase.class);

    protected String localizedDatePattern; 
    protected String localizedDateValue;
    protected String localizedShortDateValue;
    protected String defaultDatePattern; 
    protected String defaultDateValue;
    protected String defaultShortDateValue;

    protected boolean validLocalDateSymbols;

    // ------------------------------------------------------------------------

    public DateLocaleConverterTestCase(String name) {
        super(name);
    }
    
    // -------------------------------------------------- Overall Test Methods

    /**
     * Set up instance variables required by this test case.
     */
    public void setUp() throws Exception {

        super.setUp();

        String version = System.getProperty("java.specification.version");
        log.debug("JDK Version "+version);


        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
            expectedValue      = format.parse("20041001");
            defaultValue       = format.parse("19670316");
        } catch (Exception ex) {
            log.error("Error creating expected/default dates", ex);
        }

        // Default Locale (Use US)
        defaultLocale           = Locale.US;
        defaultDatePattern      = "d MMMM yyyy";
        defaultDateValue        = "1 October 2004";
        defaultShortDateValue   = "10/01/04";

        // Use German Locale
//        localizedLocale         = Locale.GERMAN;  // N.B. doesn't work for dates
//        localizedLocale         = Locale.GERMANY; // N.B. doesn't work for dates
        localizedLocale         = new Locale("de", "AT"); // Austria/German works
        localizedDatePattern    = "t MMMM uuuu";
        localizedDateValue      = "1 Oktober 2004";
        localizedShortDateValue = "01.10.04";

        // Test whether the "local pattern characters" are what we
        // are expecting - Locale.GERMAN and Locale.GERMANY, Locale.FRENCH all
        // returned the standard "English" pattern characters on my machine
        // for JDK 1.4 (JDK 1.3 was OK). The Austria/German locale was OK though
        String expectedChars = "GuMtkHmsSEDFwWahKzZ";
        DateFormatSymbols localizedSymbols = new DateFormatSymbols(localizedLocale);
        String localChars    = localizedSymbols.getLocalPatternChars();

        // different JDK versions seem to have different numbers of pattern characters 
        int lth = localChars.length() > expectedChars.length() ? expectedChars.length() :
                     localChars.length() < expectedChars.length() ? localChars.length() : expectedChars.length();
        validLocalDateSymbols = expectedChars.substring(0, lth).equals(localChars.substring(0, lth));

    }

    /**
     * Tear down instance variables required by this test case.
     */
    public void tearDown() {
        super.tearDown();
    }


    // ------------------------------------------------------------------------

    public void testSetLenient() {
        // make sure that date format works as expected
        SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd, yyyy", Locale.UK);
        
        // test with no leniency
        dateFormat.setLenient(false);
        
        try {
            
            dateFormat.parse("Feb 10, 2001");
            
        } catch (ParseException e) {
            fail("Could not parse date (1) - " + e.getMessage());
        }
        
        try {
        
            dateFormat.parse("Feb 31, 2001");
            fail("Parsed illegal date (1)");
        
        } catch (ParseException e) {
            // that's what we expected
        }
        
        // test with leniency
        dateFormat.setLenient(true);
        
        try {
            
            dateFormat.parse("Feb 10, 2001");
            
        } catch (ParseException e) {
            fail("Could not parse date (2) - " + e.getMessage());
        }
        
        try {
        
            dateFormat.parse("Feb 31, 2001");
        
        } catch (ParseException e) {
            fail("Could not parse date (3) - " + e.getMessage());
        }
        
        // now repeat tests for converter
        DateLocaleConverter converter = new DateLocaleConverter(Locale.UK, "MMM dd, yyyy");
        
        // test with no leniency
        converter.setLenient(false);
        assertEquals("Set lenient failed", converter.isLenient(), false);
        
        try {
            
            converter.convert("Feb 10, 2001");
            
        } catch (ConversionException e) {
            fail("Could not parse date (4) - " + e.getMessage());
        }
        
        try {
        
            converter.convert("Feb 31, 2001");
            assertEquals("Set lenient failed", converter.isLenient(), false);
            fail("Parsed illegal date (2)");
        
        } catch (ConversionException e) {
            // that's what we expected
        }
        
        // test with leniency
        converter.setLenient(true);
        assertEquals("Set lenient failed", converter.isLenient(), true);
        
        try {
            
            converter.convert("Feb 10, 2001");
            
        } catch (ConversionException e) {
            fail("Could not parse date (5) - " + e.getMessage());
        }
        
        try {
        
            converter.convert("Feb 31, 2001");
        
        } catch (ConversionException e) {
            fail("Could not parse date (6) - " + e.getMessage());
        }
    }

    /**
     * Test Converter(defaultValue, locale, pattern, localizedPattern) constructor
     */
    public void testConstructorMain() {

        // Skip this test if no valid symbols for the locale
        if (!validLocalDateSymbols) {
            log.error("Invalid locale symbols *** skipping testConstructorMain() **");
            return;
        } 

        // ------------- Construct with localized pattern ------------
        converter = new DateLocaleConverter(defaultValue,
                                            localizedLocale,
                                            localizedDatePattern,
                                            true);


        convertValueNoPattern(converter, "(A)", localizedDateValue, expectedValue);
        convertValueWithPattern(converter, "(A)", localizedDateValue, localizedDatePattern, expectedValue);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -