类javax.naming.AuthenticationNotSupportedException源码实例Demo

下面列出了怎么用javax.naming.AuthenticationNotSupportedException的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: athenz   文件: LDAPAuthorityTest.java
@Test
public void testLDAPAuthorityConnection() throws NamingException {

    setProperties();
    ldapAuthority = new LDAPAuthority();
    ldapAuthority.initialize();
    errMsg = new StringBuilder();
    // naming exception
    principal = ldapAuthority.authenticate("Basic dGVzdHVzZXI6dGVzdHB3ZA==", "", "", errMsg);
    assertNull(principal);

    //authentication exception - wrong username password combination
    errMsg = new StringBuilder();
    ldapAuthority = mock(LDAPAuthority.class);
    doCallRealMethod().when(ldapAuthority).initialize();
    ldapAuthority.initialize();
    when(ldapAuthority.getDirContext("cn=testuser,dc=example,dc=com", "wrongpwd")).thenThrow(new AuthenticationException());
    when(ldapAuthority.authenticate("Basic dGVzdHVzZXI6d3Jvbmdwd2Q=", "", "", errMsg)).thenCallRealMethod();
    principal = ldapAuthority.authenticate("Basic dGVzdHVzZXI6d3Jvbmdwd2Q=", "", "", errMsg);
    assertNull(principal);

    //authentication not supported exception
    errMsg = new StringBuilder();
    ldapAuthority = mock(LDAPAuthority.class);
    doCallRealMethod().when(ldapAuthority).initialize();
    ldapAuthority.initialize();
    when(ldapAuthority.getDirContext("cn=testuser,dc=example,dc=com", "wrongpwd")).thenThrow(new AuthenticationNotSupportedException());
    when(ldapAuthority.authenticate("Basic dGVzdHVzZXI6d3Jvbmdwd2Q=", "", "", errMsg)).thenCallRealMethod();
    principal = ldapAuthority.authenticate("Basic dGVzdHVzZXI6d3Jvbmdwd2Q=", "", "", errMsg);
    assertNull(principal);

    //success case
    errMsg = new StringBuilder();
    ldapAuthority = mock(LDAPAuthority.class);
    doCallRealMethod().when(ldapAuthority).initialize();
    doCallRealMethod().when(ldapAuthority).getDomain();
    doCallRealMethod().when(ldapAuthority).getSimplePrincipal("Basic dGVzdHVzZXI6d3Jvbmdwd2Q=", "testuser");
    ldapAuthority.initialize();

    when(ldapAuthority.getDirContext("cn=testuser,dc=example,dc=com", "wrongpwd")).thenReturn(new InitialDirContext());
    when(ldapAuthority.authenticate("Basic dGVzdHVzZXI6d3Jvbmdwd2Q=", "", "", errMsg)).thenCallRealMethod();
    when(ldapAuthority.authenticate("Basic dGVzdHVzZXIK", "", "", errMsg)).thenCallRealMethod();
    principal = ldapAuthority.authenticate("Basic dGVzdHVzZXI6d3Jvbmdwd2Q=", "", "", errMsg);
    assertNotNull(principal);
    assertEquals(principal.getName(), "testuser");
    assertEquals(principal.getDomain(), "user");
    assertEquals(principal.getCredentials(), "Basic dGVzdHVzZXI6d3Jvbmdwd2Q=");
    assertEquals(principal.getUnsignedCredentials(), "testuser");

    // pass credentials without password component

    principal = ldapAuthority.authenticate("Basic dGVzdHVzZXIK", "", "", errMsg);
    assertNull(principal);

    //null principal s returned from function
    System.setProperty(baseDNProp,"dc=example,dc=com");
    System.setProperty(portNumberProp,"389");
    errMsg = new StringBuilder();
    ldapAuthority = mock(LDAPAuthority.class);
    doCallRealMethod().when(ldapAuthority).initialize();
    doCallRealMethod().when(ldapAuthority).getDomain();
    when(ldapAuthority.getSimplePrincipal("Basic dGVzdHVzZXI6d3Jvbmdwd2Q=", "testuser")).thenReturn(null);
    ldapAuthority.initialize();
    when(ldapAuthority.getDirContext("cn=testuser,dc=example,dc=com", "wrongpwd")).thenReturn(new InitialDirContext());
    when(ldapAuthority.authenticate("Basic dGVzdHVzZXI6d3Jvbmdwd2Q=", "", "", errMsg)).thenCallRealMethod();
    principal = ldapAuthority.authenticate("Basic dGVzdHVzZXI6d3Jvbmdwd2Q=", "", "", errMsg);
    assertNull(principal);

    resetProperties();
}
 
 类所在包
 同包方法