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

📄 privilegedaccessortest.java

📁 软件测试和junit实践一书的源代码
💻 JAVA
字号:
package com.fastpoint.book;

import junit.framework.*;

public class PrivilegedAccessorTest extends junit.framework.TestCase {

    public void testParent() throws Exception {
        MockParent parent = new MockParent("Charlie");
        assertEquals(PrivilegedAccessor.getValue(parent, "m_name"), "Charlie");

        PrivilegedAccessor.invokeMethod(parent, "setName", "Timmah!");

        assertEquals(PrivilegedAccessor.getValue(parent,"m_name"), "Timmah!");
    }

    public void testChild() throws Exception {
        MockChild child = new MockChild("Charlie",8);
        assertEquals(PrivilegedAccessor.getValue(child, "m_name"), "Charlie");
        assertEquals(PrivilegedAccessor.getValue(child, "m_number"), new Integer(8));

        PrivilegedAccessor.invokeMethod(child, "setName", "Timmah!");
        PrivilegedAccessor.invokeMethod(child, "setNumber", new Integer(3));

        assertEquals(PrivilegedAccessor.getValue(child,"m_name"), "Timmah!");
        assertEquals(PrivilegedAccessor.getValue(child, "m_number"), new Integer(3));
    }

    public void testChildWithParentReference() throws Exception {
        MockParent parent = new MockChild("Charlie",8);
        assertEquals(PrivilegedAccessor.getValue(parent, "m_name"), "Charlie");
        assertEquals(PrivilegedAccessor.getValue(parent, "m_number"), new Integer(8));

        Object args[] = {"Timmah!", new Integer(3)};
        PrivilegedAccessor.invokeMethod(parent, "setData", args);

        assertEquals(PrivilegedAccessor.getValue(parent,"m_name"), "Timmah!");
        assertEquals(PrivilegedAccessor.getValue(parent, "m_number"), new Integer(3));

        PrivilegedAccessor.invokeMethod(parent, "setName", "prashant");
        assertEquals(PrivilegedAccessor.getValue(parent,"m_name"), "prashant");
    }

    public void testInvalidField() throws Exception {
        MockParent parent = new MockParent("Charlie");
        try {
            Object value = PrivilegedAccessor.getValue(parent, "zzz");
        }
        catch(NoSuchFieldException e) {
            return;
        }
        fail("Should throw NoSuchFieldException");
    }

    public void testInvalidMethodName() throws Exception {
        MockChild child = new MockChild("Charlie",8);
        try {
            PrivilegedAccessor.invokeMethod(child, "zzz", "Timmah!");
        }
        catch(NoSuchMethodException e) {
            return;
        }
        fail("Should throw NoSuchMethodException");
    }

    public void testInvalidArguments() throws Exception {
        MockChild child = new MockChild("Charlie",8);
        try {
            PrivilegedAccessor.invokeMethod(child, "setData", "Timmah!");
        }
        catch(NoSuchMethodException e) {
            return;
        }
        fail("Should throw NoSuchMethodException");
    }


    public PrivilegedAccessorTest( String name ) {
        super( name );
    }

    public static void main(String args[]) {
        junit.textui.TestRunner.run(PrivilegedAccessorTest.suite());
    }

    public static Test suite() {
        return new TestSuite(PrivilegedAccessorTest.class);
    }

    // Test utility classes

    public class MockChild extends MockParent {
        private int m_number;

        public MockChild( String name, int number ) {
            super( name );
            m_number = number;
        }

        public int getNumber() {
            return m_number;
        }

        private void setNumber( Integer number ) {
            m_number = number.intValue();
        }
        private void setData( String name, Integer number ) {
            setName(name);
            m_number = number.intValue();
        }
    }

    public class MockParent {
        private String m_name;

        public MockParent() {
        }
        public MockParent(String name) {
            m_name = name;
        }

        public String getName() {
            return m_name;
        }

        protected void setName( String newName ) {
            m_name = newName;
        }
    }
}

⌨️ 快捷键说明

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