alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Struts example source code file (StrutsTypeConverterTest.java)

This example Struts source code file (StrutsTypeConverterTest.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Struts tags/keywords

class, exception, exception, hashmap, internalstrutstypeconverter, internalstrutstypeconverter, object, object, string, string, strutstypeconverter, strutstypeconvertertest, testcase, util

The Struts StrutsTypeConverterTest.java source code

/*
 * $Id: StrutsTypeConverterTest.java 651946 2008-04-27 13:41:38Z apetrelli $
 *
 * 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.struts2.util;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import junit.framework.TestCase;


/**
 * Test case for Struts Type Converter.
 *
 */
public class StrutsTypeConverterTest extends TestCase {

    /**
     * Typically form Struts -> html
     *
     * @throws Exception
     */
    public void testConvertToString() throws Exception {
        InternalStrutsTypeConverter strutsTypeConverter = new InternalStrutsTypeConverter();
        strutsTypeConverter.convertValue(new HashMap(), "", String.class);
        assertTrue(strutsTypeConverter.isConvertToString);
        assertEquals(strutsTypeConverter.objToBeConverted, "");
    }

    /**
     * Typically form html -> Struts
     *
     * @throws Exception
     */
    public void testConvertFromString() throws Exception {
        InternalStrutsTypeConverter strutsTypeConverter = new InternalStrutsTypeConverter();
        strutsTypeConverter.convertValue(new HashMap(), "12/12/1997", Date.class);
        assertTrue(strutsTypeConverter.isConvertFromString);
        assertTrue(strutsTypeConverter.objToBeConverted instanceof String[]);
        assertEquals(((String[])strutsTypeConverter.objToBeConverted).length, 1);
    }

    /**
     * Typically from html -> Struts (in array due to the nature of html, param
     * being able to have many values).
     *
     * @throws Exception
     */
    public void testConvertFromStringInArrayForm() throws Exception {
        InternalStrutsTypeConverter strutsTypeConverter = new InternalStrutsTypeConverter();
        strutsTypeConverter.convertValue(new HashMap(), new String[] { "12/12/1997", "1/1/1977" }, Date.class);
        assertTrue(strutsTypeConverter.isConvertFromString);
        assertTrue(strutsTypeConverter.objToBeConverted instanceof String[]);
        assertEquals(((String[])strutsTypeConverter.objToBeConverted).length, 2);
    }


    public void testFallbackConversion() throws Exception {
        InternalStrutsTypeConverter strutsTypeConverter = new InternalStrutsTypeConverter();
        strutsTypeConverter.convertValue(new HashMap(), new Object(), Date.class);
        assertTrue(strutsTypeConverter.fallbackConversion);
    }

    // === internal class for testing
    class InternalStrutsTypeConverter extends StrutsTypeConverter {

        boolean isConvertFromString = false;
        boolean isConvertToString = false;
        boolean fallbackConversion = false;

        Object objToBeConverted;

        public Object convertFromString(Map context, String[] values, Class toClass) {
            isConvertFromString = true;
            objToBeConverted = values;
            return null;
        }

        public String convertToString(Map context, Object o) {
            isConvertToString = true;
            objToBeConverted = o;
            return null;
        }

        protected Object performFallbackConversion(Map context, Object o, Class toClass) {
            fallbackConversion = true;
            return null;
        }

    }

}

Other Struts examples (source code examples)

Here is a short list of links related to this Struts StrutsTypeConverterTest.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.