### Question:
ManagedConnectionImpl extends AbstractManagedConnectionImpl implements CXFManagedConnection, Connection { public void associateConnection(Object connection) throws ResourceException { try { CXFInvocationHandler handler = (CXFInvocationHandler)Proxy.getInvocationHandler(connection); Object managedConnection = handler.getData().getManagedConnection(); if (managedConnection != this) { handler.getData().setManagedConnection(this); ((ManagedConnectionImpl)managedConnection).disassociateConnectionHandle(connection); if (getCXFService() == null) { cxfService = connection; connectionHandleActive = true; } } } catch (Exception ex) { throw new ResourceAdapterInternalException( new Message("ASSOCIATED_ERROR", BUNDLE).toString(), ex); } } ManagedConnectionImpl(ManagedConnectionFactoryImpl managedFactory, ConnectionRequestInfo crInfo,
                                 Subject subject); void associateConnection(Object connection); CXFManagedConnectionFactory getManagedConnectionFactory(); Object getCXFService(); Object getConnection(Subject subject, ConnectionRequestInfo crInfo); synchronized Object getCXFConnection(Subject subject, ConnectionRequestInfo crInfo); ManagedConnectionMetaData getMetaData(); boolean isBound(); void close(); void close(Object closingHandle); void destroy(); CXFTransaction getCXFTransaction(); XAResource getXAResource(); LocalTransaction getLocalTransaction();  }### Answer:
@Test public void testAssociateConnectionThrowsException() throws Throwable { InvocationHandler ih = EasyMock.createMock(InvocationHandler.class); Object dodgyHandle = Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] {Foo.class}, ih); try { mci.associateConnection(dodgyHandle); fail("Except exception on call with ClassCast Exception"); } catch (ResourceAdapterInternalException raie) { } }
@Test public void testAssociateConnection() throws Exception { CXFConnectionRequestInfo cri2 = new CXFConnectionRequestInfo(Greeter.class, new URL("file:/tmp/foo2"), new QName("service2"), new QName("fooPort2")); ManagedConnectionImpl mci2 = new ManagedConnectionImpl(factory, cri2, new Subject()); mci2.addConnectionEventListener(mockListener); Object o = mci.getConnection(subj, cri); assertTrue("Returned connection does not implement Connection interface", o instanceof Connection); assertTrue("Returned connection does not implement Connection interface", o instanceof Greeter); assertTrue("Returned connection is not a java.lang.reflect.Proxy instance", o instanceof Proxy); InvocationHandler handler = Proxy.getInvocationHandler(o); assertTrue("Asserting handler class: " + handler.getClass(), handler instanceof CXFInvocationHandler); Object assocMci = ((CXFInvocationHandler)handler).getData().getManagedConnection(); assertTrue("Asserting associated ManagedConnection.", mci == assocMci); assertTrue("Asserting associated ManagedConnection.", mci2 != assocMci); mci2.associateConnection(o); assocMci = ((CXFInvocationHandler)handler).getData().getManagedConnection(); assertTrue("Asserting associated ManagedConnection.", mci2 == assocMci); assertTrue("Asserting associated ManagedConnection.", mci != assocMci); }
@Test public void testAssociateConnectionThrowsException() throws Throwable { InvocationHandler ih = EasyMock.createMock(InvocationHandler.class); Object dodgyHandle = Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] {Foo.class}, ih); try { mci.associateConnection(dodgyHandle); fail("Except exception on call with ClassCast Exception"); } catch (ResourceAdapterInternalException raie) { assertTrue(true); } }