DefaultDirObjectFactory implements DirObjectFactory { @Override public final Object getObjectInstance( Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment, Attributes attrs) throws Exception { try { String nameInNamespace; if (nameCtx != null) { nameInNamespace = nameCtx.getNameInNamespace(); } else { nameInNamespace = ""; } return constructAdapterFromName(attrs, name, nameInNamespace); } finally { if (obj instanceof Context) { Context ctx = (Context) obj; try { ctx.close(); } catch (Exception e) { } } } }  @Override final Object getObjectInstance(
            Object obj,
            Name name,
            Context nameCtx,
            Hashtable<?, ?> environment,
            Attributes attrs); @Override Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment); static final String JNDI_ENV_BASE_PATH_KEY; }
@Test public void testGetObjectInstance() throws Exception { Attributes expectedAttributes = new NameAwareAttributes(); expectedAttributes.put("someAttribute", "someValue"); DirContextAdapter adapter = (DirContextAdapter) tested.getObjectInstance(contextMock, DN, null, new Hashtable(), expectedAttributes); verify(contextMock).close(); assertThat(adapter.getDn()).isEqualTo(DN); assertThat(adapter.getAttributes()).isEqualTo(expectedAttributes); }
@Test public void testGetObjectInstance_CompositeName() throws Exception { Attributes expectedAttributes = new NameAwareAttributes(); expectedAttributes.put("someAttribute", "someValue"); CompositeName name = new CompositeName(); name.add(DN_STRING); DirContextAdapter adapter = (DirContextAdapter) tested.getObjectInstance(contextMock, name, null, new Hashtable(), expectedAttributes); verify(contextMock).close(); assertThat(adapter.getDn()).isEqualTo(DN); assertThat(adapter.getAttributes()).isEqualTo(expectedAttributes); }
@Test public void testGetObjectInstance_nullObject() throws Exception { Attributes expectedAttributes = new NameAwareAttributes(); expectedAttributes.put("someAttribute", "someValue"); DirContextAdapter adapter = (DirContextAdapter) tested.getObjectInstance(null, DN, null, new Hashtable(), expectedAttributes); assertThat(adapter.getDn()).isEqualTo(DN); assertThat(adapter.getAttributes()).isEqualTo(expectedAttributes); }
@Test public void testGetObjectInstance_ObjectNotContext() throws Exception { Attributes expectedAttributes = new NameAwareAttributes(); expectedAttributes.put("someAttribute", "someValue"); DirContextAdapter adapter = (DirContextAdapter) tested.getObjectInstance(new Object(), DN, null, new Hashtable(), expectedAttributes); assertThat(adapter.getDn()).isEqualTo(DN); assertThat(adapter.getAttributes()).isEqualTo(expectedAttributes); }
@Test public void testGetObjectInstance_BaseSet() throws Exception { Attributes expectedAttributes = new NameAwareAttributes(); expectedAttributes.put("someAttribute", "someValue"); when(contextMock2.getNameInNamespace()).thenReturn("dc=jayway, dc=se"); DirContextAdapter adapter = (DirContextAdapter) tested.getObjectInstance(contextMock, LdapUtils.newLdapName( "ou=some unit"), contextMock2, new Hashtable(), expectedAttributes); verify(contextMock).close(); assertThat(adapter.getDn().toString()).isEqualTo("ou=some unit"); assertThat(adapter.getNameInNamespace()).isEqualTo("ou=some unit,dc=jayway,dc=se"); assertThat(adapter.getAttributes()).isEqualTo(expectedAttributes); }