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

📄 exceptiontestcase.java

📁 一个java方面的消息订阅发送的源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                       Error.class);
    }

    /**
     * Verifies that a declared <code>RuntimeException</code> is propagated to
     * the client.
     *
     * @throws Exception for any error
     */
    public void testDeclaredRuntimeException() throws Exception {
        Protectable protectable = new Protectable() {
            public void protect() throws Throwable {
                _service.throwRuntimeException();
            }
        };
        checkException(protectable, RuntimeException.class, null);
    }

    /**
     * Verifies that an undeclared <code>RuntimeException</code> is propagated
     * to the client, wrapped in a {@link RemoteInvocationException}.
     *
     * @throws Exception for any error
     */
    public void testUndeclaredRuntimeException() throws Exception {
        Protectable protectable = new Protectable() {
            public void protect() throws Throwable {
                _service.throwUndeclaredRuntimeException();
            }
        };
        checkException(protectable, RemoteInvocationException.class,
                       RuntimeException.class);
    }

    /**
     * Verifies that a declared <code>RemoteException</code> is propagated to
     * the client.
     *
     * @throws Exception for any error
     */
    public void testDeclaredRemoteException() throws Exception {
        Protectable protectable = new Protectable() {
            public void protect() throws Throwable {
                _service.throwRemoteException();
            }
        };
        checkException(protectable, RemoteException.class, null);
    }

    /**
     * Verifies that an undeclared <code>Error</code> thrown by a method
     * declaring <code>RemoteException</code> is propagated to the client,
     * wrapped in a <code>RemoteException</code>.
     *
     * @throws Exception for any errror
     */
    public void testUndeclaredError2() throws Exception {
        Protectable protectable = new Protectable() {
            public void protect() throws Throwable {
                _service.throwUndeclaredError2();
            }
        };
        checkException(protectable, RemoteException.class, Error.class);
    }

    /**
     * Verifies that an undeclared <code>RuntimeException</code> thrown by a
     * method declaring <code>RemoteException</code> is propagated to the
     * client, wrapped in a <code>RemoteException</code>.
     *
     * @throws Exception for any errror
     */
    public void testUndeclaredRuntimeException2() throws Exception {
        Protectable protectable = new Protectable() {
            public void protect() throws Throwable {
                _service.throwUndeclaredRuntimeException2();
            }
        };
        checkException(protectable, RemoteException.class,
                       RuntimeException.class);
    }

    /**
     * Verifies that an undeclared <code>RemoteInvocationException</code> thrown
     * by a method is propagated to the client unchanged.
     *
     * @throws Exception for any errror
     */
    public void testUndeclaredRemoteInvocationException() throws Exception {
        Protectable protectable = new Protectable() {
            public void protect() throws Throwable {
                _service.throwUndeclaredRemoteInvocationException();
            }
        };
        checkException(protectable, RemoteInvocationException.class, null);
    }

    /**
     * Returns connection properties for establishing a connection to the remote
     * ORB.
     *
     * @return the connection properties, or <code>null</code> if the default
     *         connection properties should be used
     * @throws IOException if a store cannot be found
     */
    protected Map getConnectionProperties() throws IOException {
        Map properties = new HashMap();
        properties.put(ORB.PROVIDER_URI, getServerURI());
        if (_connectionProps != null) {
            properties.putAll(_connectionProps);
        }
        return properties;
    }

    /**
     * Returns the acceptor properties to use when accepting connections.
     *
     * @return the acceptor properties, or <code>null</code> if the default
     *         connection properties should be used
     * @throws Exception for any error
     */
    protected Map getAcceptorProperties() throws Exception {
        Map properties = new HashMap();
        properties.put(ORB.PROVIDER_URI, _uri);
        if (_acceptorProps != null) {
            properties.putAll(_acceptorProps);
        }
        return properties;
    }

    /**
     * Helper to return the server URI.
     *
     * @return the server URI
     */
    protected String getServerURI() {
        return (_routeURI != null) ? _routeURI : _uri;
    }

    /**
     * Sets up the test case.
     *
     * @throws Exception for any error
     */
    protected void setUp() throws Exception {
        _log.debug("setUp() [test=" + getName() + ", uri=" + _uri + "]");
        _orb = ORBFactory.createORB(getAcceptorProperties());
        if (_routeURI != null) {
            _orb.addRoute(_uri, _routeURI);
        }
        Registry serverRegistry = _orb.getRegistry();

        Proxy proxy = _orb.exportObject(new ExceptionServiceImpl());
        serverRegistry.bind(EXCEPTION_SERVICE, proxy);

        // get a proxy to the registry, and look up the service
        Registry clientRegistry = _orb.getRegistry(getConnectionProperties());
        _service = (ExceptionService) clientRegistry.lookup(EXCEPTION_SERVICE);
    }

    /**
     * Cleans up the test case.
     *
     * @throws Exception for any error
     */
    protected void tearDown() throws Exception {
        _log.debug("tearDown() [test=" + getName() + ", uri=" + _uri + "]");
        _orb.shutdown();

        // reset any SSL properties that may have been set.
        SSLUtil.clearProperties();
    }

    /**
     * Verifies that a method throws an exception of the expected type.
     *
     * @param protectable wraps the method to invoke
     * @param expected    the expected type of the exception
     * @param nested      the expected type of the nested exception, or
     *                    <code>null</code>, if no nested exception is expected
     */
    private void checkException(Protectable protectable, Class expected,
                                Class nested) {
        try {
            protectable.protect();
            fail("Expected exception of type=" + expected.getName()
                 + " to be thrown");
        } catch (RemoteInvocationException exception) {
            checkExceptionType(exception, expected);
            if (nested != null) {
                checkNestedExceptionType(exception.getTargetException(),
                                         nested);
            }
        } catch (RemoteException exception) {
            checkExceptionType(exception, expected);
            if (nested != null) {
                checkNestedExceptionType(exception.detail, nested);
            }
        } catch (AssertionFailedError error) {
            throw error;
        } catch (Throwable throwable) {
            checkExceptionType(throwable, expected);
        }
    }

    /**
     * Verifies that an exception is of the expected type.
     *
     * @param exception the exception to check
     * @param expected  the expected exception type
     */
    private void checkExceptionType(Throwable exception, Class expected) {
        Class actual = exception.getClass();
        if (!actual.equals(expected)) {
            fail("Expected exception of type=" + expected.getName()
                 + " to be thrown, but got type=" + actual.getName()
                 + ", message=" + exception.getMessage());
        }
    }

    /**
     * Verifies that a nested exception is of the expected type.
     *
     * @param exception the exception to check
     * @param expected  the expected exception type
     */
    private void checkNestedExceptionType(Throwable exception,
                                          Class expected) {
        Class actual = exception.getClass();
        if (!actual.equals(expected)) {
            fail("Expected nested exception of type=" + expected.getName()
                 + " but got type=" + actual.getClass().getName()
                 + ", message=" + exception.getMessage());
        }
    }

}

⌨️ 快捷键说明

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