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

📄 methodutilstestcase.java

📁 apache beanutils开源项目源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Copyright 2001-2004 The Apache Software Foundation. *  * 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.apache.commons.beanutils;import java.io.OutputStream;import java.io.PrintStream;import java.lang.reflect.Method;import java.lang.reflect.Modifier;import org.apache.commons.beanutils.priv.PrivateBeanFactory;import org.apache.commons.beanutils.priv.PublicSubBean;import junit.framework.TestCase;import junit.framework.Test;import junit.framework.TestSuite;/** * <p> Test case for <code>MethodUtils</code> </p> * */public class MethodUtilsTestCase extends TestCase {    // ---------------------------------------------------- Instance Variables    protected PrivateBeanFactory privateBeanFactory;    // ---------------------------------------------------------- Constructors    /**     * Construct a new instance of this test case.     *     * @param name Name of the test case     */    public MethodUtilsTestCase(String name) {        super(name);    }    // -------------------------------------------------- Overall Test Methods    /**     * Set up instance variables required by this test case.     */    public void setUp() {        privateBeanFactory = new PrivateBeanFactory();    }    /**     * Return the tests included in this test suite.     */    public static Test suite() {        return (new TestSuite(MethodUtilsTestCase.class));    }    /**     * Tear down instance variables required by this test case.     */    public void tearDown() {        privateBeanFactory = null;    }    // ------------------------------------------------ Individual Test Methods    /**     * <p> Test <code>getAccessibleMethod</code>.     */    public void testGetAccessibleMethod() {        // test MethodUtils.getAccessibleMethod        // we'll make things easier by using the convenience methods        // easy bit first - find a public method        // METHOD ONE        Method method = MethodUtils.getAccessibleMethod                (TestBean.class, "setStringProperty", String.class);        // check that we've found one that matches        assertNotNull(method);        assertEquals("method ONE is named correctly",                "setStringProperty", method.getName());        assertTrue("Method ONE is public",                Modifier.isPublic(method.getModifiers()));        // trickier this one - find a method in a direct interface        // METHOD TWO        method = MethodUtils.getAccessibleMethod                (privateBeanFactory.create().getClass(),                        "methodBar",                        String.class);        // check that we've found one that matches        assertNotNull(method);        assertEquals("Method TWO is named correctly",                "methodBar", method.getName());        assertTrue("Method TWO is public",                Modifier.isPublic(method.getModifiers()));        // trickier this one - find a method in a indirect interface        // METHOD THREE        method = MethodUtils.getAccessibleMethod                (privateBeanFactory.createSubclass().getClass(),                        "methodBaz",                        String.class);        // check that we've found one that matches        assertNotNull(method);        assertEquals("Method THREE is named correctly",                "methodBaz", method.getName());        assertTrue("Method THREE is public",                Modifier.isPublic(method.getModifiers()));    }    /**     * <p> Test <code>invokeExactMethod</code>.     */    public void testInvokeExactMethod() {        // test MethodUtils.invokeExactMethod        // easy bit first - invoke a public method        // METHOD ONE        try {            TestBean bean = new TestBean();            Object ret = MethodUtils.invokeExactMethod(bean, "setStringProperty", "TEST");            // check that the return's right and that the properties been set            assertNull(ret);            assertEquals("Method ONE was invoked", "TEST", bean.getStringProperty());        } catch (Throwable t) {            // ONE            fail("Exception in method ONE prevented invokation: " + t.toString());        }        // trickier this one - find a method in a direct interface        // METHOD TWO FAILURE        try {            Object ret = MethodUtils.invokeExactMethod(                    privateBeanFactory.create(),                    "methodBar",                    "ANOTHER TEST");            // check that we've found one that matches            assertEquals("Method TWO was invoked correctly", "ANOTHER TEST", ret);        } catch (Throwable t) {            // METHOD TWO FAILURE            fail("Exception in method TWO prevented invokation: " + t.toString());        }        // trickier this one - find a method in a indirect interface        // METHOD THREE        try {            Object ret = MethodUtils.invokeExactMethod(                    privateBeanFactory.createSubclass(),                    "methodBaz",                    "YET ANOTHER TEST");            // check that we've found one that matches            assertEquals("Method TWO was invoked correctly", "YET ANOTHER TEST", ret);        } catch (Throwable t) {            // METHOD THREE FAILURE            fail("Exception in method THREE prevented invokation: " + t.toString());        }    }        /**     * <p> Test <code>invokeMethod</code>.     */    public void testInvokeMethod() throws Exception {        // i'm going to test that the actual calls work first and then try them via reflection                AbstractParent parent = new AlphaBean("parent");                // try testAddChild through abstract superclass        BetaBean childOne = new BetaBean("ChildOne");                assertEquals("Oh no! Badly coded test case! (1)", "ChildOne", parent.testAddChild(childOne));                // let's try MethodUtils version        assertEquals(                        "Cannot invoke through abstract class (1)",                         "ChildOne",                         MethodUtils.invokeMethod(parent, "testAddChild", childOne));                // try adding through interface        AlphaBean childTwo = new AlphaBean("ChildTwo");                assertEquals("Oh no! Badly coded test case! (2)", "ChildTwo", parent.testAddChild(childTwo));                // let's try MethodUtils version        assertEquals(                        "Cannot invoke through interface (1)",                         "ChildTwo",                         MethodUtils.invokeMethod(parent, "testAddChild", childTwo));                       Object[] params = new Object[2];        assertEquals("Oh no! Badly coded test case! (3)", "ChildOne", parent.testAddChild2("parameter", childOne));                        // let's try MethodUtils version        params[0] = "parameter";        params[1] = childOne;                assertEquals(                        "Cannot invoke through abstract class (1)",                         "ChildOne",                         MethodUtils.invokeMethod(parent, "testAddChild2", params));                                assertEquals("Oh no! Badly coded test case! (4)", "ChildTwo", parent.testAddChild2("parameter", childTwo));                // let's try MethodUtils version        params[0] = "parameter";        params[1] = childTwo;               assertEquals(                        "Cannot invoke through abstract class (1)",                         "ChildTwo",                         MethodUtils.invokeMethod(parent, "testAddChild2", params));                // test that exception is correctly thrown when a method cannot be found with matching params        try {            // the next line            parent = new AlphaBean("parent");            childOne = new BetaBean("ChildOne");            MethodUtils.invokeMethod(parent, "bogus", childOne);            // should get here!            fail("No exception thrown when no appropriate method exists");                    } catch (NoSuchMethodException e) {            // this is what we're expecting!        }                MethodUtils.invokeMethod(parent, "getName", null);        MethodUtils.invokeMethod(parent, "getName", null, null);        MethodUtils.invokeExactMethod(parent, "getName", null);        MethodUtils.invokeExactMethod(parent, "getName", null, null);            }        /**     * <p> Test <code>invokeMethod</code> with a primitive.     */    public void testInvokeMethodWithPrimitives() throws Exception {        // first test that the bean works         PrimitiveBean bean = new PrimitiveBean();        bean.setFloat(20.0f);        bean.setLong(10l);        bean.setBoolean(true);        bean.setInt(12);        bean.setDouble(25.5d);        

⌨️ 快捷键说明

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