public void testMakeObjectConnectsAndOtherwiseInitializesConnections() throws LDAPException, UnsupportedEncodingException { setConnectionManagerConfigExpectations(); factory.setConnectionManager(connMgr); mockConn.expects(once()).method("setConnectionManager").with(same(connMgr)); mockConn.expects(once()).method("setConstraints").with(NOT_NULL); // TODO make more specific mockConn .expects(once()) .method("connect") .with(eq(connMgrConfig.getLdapHost()), eq(connMgrConfig.getLdapPort())) .after("setConstraints"); mockConn.expects(once()).method("startTLS").after("connect"); mockConn .expects(once()) .method("bind") .with( eq(LDAPConnection.LDAP_V3), eq(connMgrConfig.getLdapUser()), eq(connMgrConfig.getLdapPassword().getBytes("UTF-8"))) .after("connect"); mockConn.expects(once()).method("setBindAttempted").with(eq(false)).after("bind"); // the actual code exercise assertSame(conn, factory.makeObject()); // TODO how to test socket factory assignment (static call) }
/** * Connects the specified <code>LDAPConnection</code> to the currently configured host and port. * * @param conn an <code>LDAPConnection</code> * @throws LDAPConnection if the connect attempt fails */ protected void connect(LDAPConnection conn) throws LDAPException { if (M_log.isDebugEnabled()) { M_log.debug("connect()"); } conn.connect(config.getLdapHost(), config.getLdapPort()); try { postConnect(conn); } catch (LDAPException e) { M_log.error( "Failed to completely initialize a connection [host = " + config.getLdapHost() + "][port = " + config.getLdapPort() + "]", e); try { conn.disconnect(); } catch (LDAPException ee) { } throw e; } catch (Throwable e) { M_log.error( "Failed to completely initialize a connection [host = " + config.getLdapHost() + "][port = " + config.getLdapPort() + "]", e); try { conn.disconnect(); } catch (LDAPException ee) { } if (e instanceof Error) { throw (Error) e; } if (e instanceof RuntimeException) { throw (RuntimeException) e; } throw new RuntimeException("LDAPConnection allocation failure", e); } }