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

📄 modelmaptests.java

📁 struts+spring 源码 希望能给大家带来帮助
💻 JAVA
字号:
/*
 * Copyright 2002-2006 the original author or authors.
 *
 * 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 org.springframework.ui;

import junit.framework.TestCase;
import org.springframework.beans.TestBean;
import org.springframework.test.AssertThrows;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * Unit tests for the ModelMap class.
 *
 * @author Rick Evans
 */
public final class ModelMapTests extends TestCase {

    public void testNoArgCtorYieldsEmptyModel() throws Exception {
        assertEquals(0, new ModelMap().size());
    }

    /* 
     * SPR-2185 - Null model assertion causes backwards compatibility issue
     */
    public void testAddNullObjectWithExplicitKey() throws Exception {
        ModelMap model = new ModelMap();
        model.addObject("foo", null);
        assertTrue(model.containsKey("foo"));
        assertNull(model.get("foo"));
    }

    /* 
     * SPR-2185 - Null model assertion causes backwards compatibility issue
     */
    public void testAddNullObjectViaCtorWithExplicitKey() throws Exception {
        ModelMap model = new ModelMap("foo", null);
        assertTrue(model.containsKey("foo"));
        assertNull(model.get("foo"));
    }

    public void testNamedObjectCtor() throws Exception {
        ModelMap model = new ModelMap("foo", "bing");
        assertEquals(1, model.size());
        String bing = (String) model.get("foo");
        assertNotNull(bing);
        assertEquals("bing", bing);
    }

    public void testUnnamedCtorScalar() throws Exception {
        ModelMap model = new ModelMap("foo", "bing");
        assertEquals(1, model.size());
        String bing = (String) model.get("foo");
        assertNotNull(bing);
        assertEquals("bing", bing);
    }

    public void testOneArgCtorWithScalar() throws Exception {
        ModelMap model = new ModelMap("bing");
        assertEquals(1, model.size());
        String string = (String) model.get("string");
        assertNotNull(string);
        assertEquals("bing", string);
    }

    public void testOneArgCtorWithNull() throws Exception {
        new AssertThrows(IllegalArgumentException.class, "Null model arguments added without a name being explicitly supplied are not allowed.") {
            public void test() throws Exception {
                new ModelMap(null);
            }
        }.runTest();
    }

    public void testOneArgCtorWithCollection() throws Exception {
        ModelMap model = new ModelMap(new String []{"foo", "boing"});
        assertEquals(1, model.size());
        String[] strings = (String[]) model.get("stringList");
        assertNotNull(strings);
        assertEquals(2, strings.length);
        assertEquals("foo", strings[0]);
        assertEquals("boing", strings[1]);
    }

    public void testOneArgCtorWithEmptyCollection() throws Exception {
        ModelMap model = new ModelMap(new HashSet());
        // must not add if collection is empty...
        assertEquals(0, model.size());
    }

    public void testAddObjectWithNull() throws Exception {
        new AssertThrows(IllegalArgumentException.class, "Null model arguments added without a name being explicitly supplied are not allowed.") {
            public void test() throws Exception {
                ModelMap model = new ModelMap();
                model.addObject(null);
            }
        }.runTest();
    }

    public void testAddObjectWithEmptyArray() throws Exception {
        ModelMap model = new ModelMap(new int[]{});
        assertEquals(1, model.size());
        int[] ints = (int[]) model.get("intList");
        assertNotNull(ints);
        assertEquals(0, ints.length);
    }

    public void testAddAllObjectsWithNullMap() throws Exception {
        ModelMap model = new ModelMap();
        model.addAllObjects((Map) null);
        assertEquals(0, model.size());
    }

    public void testAddAllObjectsWithNullCollection() throws Exception {
        ModelMap model = new ModelMap();
        model.addAllObjects((Collection) null);
        assertEquals(0, model.size());
    }

    public void testAddAllObjectsWithSparseArrayList() throws Exception {
        new AssertThrows(IllegalArgumentException.class, "Null model arguments added without a name being explicitly supplied are not allowed.") {
            public void test() throws Exception {
                ModelMap model = new ModelMap();
                ArrayList list = new ArrayList();
                list.add("bing");
                list.add(null);
                model.addAllObjects(list);
            }
        }.runTest();
    }

    public void testAddMap() throws Exception {
        Map map = new HashMap();
        map.put("one", "one-value");
        map.put("two", "two-value");

        ModelMap model = new ModelMap();
        model.addObject(map);

        assertEquals(1, model.size());
        String key = StringUtils.uncapitalize(ClassUtils.getShortName(map.getClass()));
        assertTrue(model.containsKey(key));
    }

    public void testAddObjectNoKeyOfSameTypeOverrides() throws Exception {
        ModelMap model = new ModelMap();
        model.addObject("foo");
        model.addObject("bar");
        assertEquals(1, model.size());
        String bar = (String) model.get("string");
        assertNotNull(bar);
        assertEquals("bar", bar);
    }

    public void testAddListOfTheSameObjects() throws Exception {
        List beans = new ArrayList();
        beans.add(new TestBean("one"));
        beans.add(new TestBean("two"));
        beans.add(new TestBean("three"));

        ModelMap model = new ModelMap();
        model.addAllObjects(beans);

        assertEquals(1, model.size());
    }

}

⌨️ 快捷键说明

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