DefaultJndiSupport extends JndiLocatorSupport implements
        JndiSupport { public void initialize() { if (jndiEnvironmentMap != null) { String factory = jndiEnvironmentMap.get(JNDI_FACTORY_KEY); String url = jndiEnvironmentMap.get(JNDI_URL_KEY); String username = jndiEnvironmentMap.get(JNDI_USERNAME_KEY); String password = jndiEnvironmentMap.get(JNDI_PASSWORD_KEY); Properties environment = new Properties(); environment.put(Context.INITIAL_CONTEXT_FACTORY, factory); environment.put(Context.PROVIDER_URL, url); if (!"".equals(username) && username != null) { environment.put(Context.SECURITY_PRINCIPAL, username); if (password == null) { password = ""; } environment.put(Context.SECURITY_CREDENTIALS, password); } getJndiTemplate().setEnvironment(environment); if (log.isInfoEnabled()) { log.info("Initialize Weblogic JNDI Resource"); log.info(Context.INITIAL_CONTEXT_FACTORY + " = " + factory); log.info(Context.PROVIDER_URL + " = " + url); log.info(Context.SECURITY_PRINCIPAL + " = " + username); log.info(Context.SECURITY_CREDENTIALS + " = " + password); } } } DefaultJndiSupport(); void setJndiPrefix(boolean jndiPrefix); boolean isJndiPrefix(); Map<String, String> getJndiEnvironmentMap(); void setJndiEnvironmentMap(Map<String, String> jndiEnvironmentMap); void initialize(); @Override void rebind(String name, Object obj); @Override void unbind(String name); @Override Object lookup(String name);  }
@Test public void testInitialize01() throws Exception { DefaultJndiSupport support = new DefaultJndiSupport(); ReflectionTestUtils.setField(support, "jndiEnvironmentMap", null); support.initialize(); Object result = ReflectionTestUtils.getField(support, "jndiEnvironmentMap"); assertNull(result); }
@Test public void testInitialize02() throws Exception { Map<String, String> map = new HashMap<String, String>(); map.put("factory", "factory"); map.put("url", "url"); DefaultJndiSupport support = new DefaultJndiSupport(); ReflectionTestUtils.setField(support, "jndiEnvironmentMap", map); logger.clear(); support.initialize(); Object result = ReflectionTestUtils.getField(support, "jndiEnvironmentMap"); assertSame(map, result); assertThat(logger.getLoggingEvents(), is(asList(info( "Initialize Weblogic JNDI Resource"), info( "java.naming.factory.initial = factory"), info( "java.naming.provider.url = url"), info( "java.naming.security.principal = null"), info("java.naming.security.credentials = null")))); Properties props = support.getJndiEnvironment(); assertEquals("factory", props.get(Context.INITIAL_CONTEXT_FACTORY)); assertEquals("url", props.get(Context.PROVIDER_URL)); assertNull(props.get(Context.SECURITY_PRINCIPAL)); assertNull(props.get(Context.SECURITY_CREDENTIALS)); }
@Test public void testInitialize03() throws Exception { Map<String, String> map = new HashMap<String, String>(); map.put("factory", "factory"); map.put("url", "url"); map.put("username", ""); DefaultJndiSupport support = new DefaultJndiSupport(); ReflectionTestUtils.setField(support, "jndiEnvironmentMap", map); support.initialize(); Object result = ReflectionTestUtils.getField(support, "jndiEnvironmentMap"); assertSame(map, result); assertThat(logger.getLoggingEvents(), is(asList(info( "Initialize Weblogic JNDI Resource"), info( "java.naming.factory.initial = factory"), info( "java.naming.provider.url = url"), info( "java.naming.security.principal = "), info("java.naming.security.credentials = null")))); Properties props = support.getJndiEnvironment(); assertEquals("factory", props.get(Context.INITIAL_CONTEXT_FACTORY)); assertEquals("url", props.get(Context.PROVIDER_URL)); assertNull(props.get(Context.SECURITY_PRINCIPAL)); assertNull(props.get(Context.SECURITY_CREDENTIALS)); }
@Test public void testInitialize04() throws Exception { Map<String, String> map = new HashMap<String, String>(); map.put("factory", "factory"); map.put("url", "url"); map.put("username", "username"); DefaultJndiSupport support = new DefaultJndiSupport(); ReflectionTestUtils.setField(support, "jndiEnvironmentMap", map); support.initialize(); Object result = ReflectionTestUtils.getField(support, "jndiEnvironmentMap"); assertSame(map, result); assertThat(logger.getLoggingEvents(), is(asList(info( "Initialize Weblogic JNDI Resource"), info( "java.naming.factory.initial = factory"), info( "java.naming.provider.url = url"), info( "java.naming.security.principal = username"), info("java.naming.security.credentials = ")))); Properties props = support.getJndiEnvironment(); assertEquals("factory", props.get(Context.INITIAL_CONTEXT_FACTORY)); assertEquals("url", props.get(Context.PROVIDER_URL)); assertEquals("username", props.get(Context.SECURITY_PRINCIPAL)); assertEquals("", props.get(Context.SECURITY_CREDENTIALS)); }
@Test public void testInitialize05() throws Exception { Map<String, String> map = new HashMap<String, String>(); map.put("factory", "factory"); map.put("url", "url"); map.put("username", "username"); map.put("password", ""); DefaultJndiSupport support = new DefaultJndiSupport(); ReflectionTestUtils.setField(support, "jndiEnvironmentMap", map); support.initialize(); Object result = ReflectionTestUtils.getField(support, "jndiEnvironmentMap"); assertSame(map, result); assertThat(logger.getLoggingEvents(), is(asList(info( "Initialize Weblogic JNDI Resource"), info( "java.naming.factory.initial = factory"), info( "java.naming.provider.url = url"), info( "java.naming.security.principal = username"), info("java.naming.security.credentials = ")))); Properties props = support.getJndiEnvironment(); assertEquals("factory", props.get(Context.INITIAL_CONTEXT_FACTORY)); assertEquals("url", props.get(Context.PROVIDER_URL)); assertEquals("username", props.get(Context.SECURITY_PRINCIPAL)); assertEquals("", props.get(Context.SECURITY_CREDENTIALS)); }
@Test public void testInitialize06() throws Exception { Map<String, String> map = new HashMap<String, String>(); map.put("factory", "factory"); map.put("url", "url"); map.put("username", "username"); map.put("password", "password"); DefaultJndiSupport support = new DefaultJndiSupport(); ReflectionTestUtils.setField(support, "jndiEnvironmentMap", map); support.initialize(); Object result = ReflectionTestUtils.getField(support, "jndiEnvironmentMap"); assertSame(map, result); assertThat(logger.getLoggingEvents(), is(asList(info( "Initialize Weblogic JNDI Resource"), info( "java.naming.factory.initial = factory"), info( "java.naming.provider.url = url"), info( "java.naming.security.principal = username"), info("java.naming.security.credentials = password")))); Properties props = support.getJndiEnvironment(); assertEquals("factory", props.get(Context.INITIAL_CONTEXT_FACTORY)); assertEquals("url", props.get(Context.PROVIDER_URL)); assertEquals("username", props.get(Context.SECURITY_PRINCIPAL)); assertEquals("password", props.get(Context.SECURITY_CREDENTIALS)); }