1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v2.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  
15  package ch.qos.logback.core.util;
16  
17  import java.util.Hashtable;
18  
19  import javax.naming.Context;
20  import javax.naming.NamingException;
21  
22  import org.junit.jupiter.api.Assertions;
23  import org.junit.jupiter.api.Test;
24  
25  import ch.qos.logback.core.testUtil.CoreTestConstants;
26  import ch.qos.logback.core.testUtil.MockInitialContextFactory;
27  
28  import static org.junit.jupiter.api.Assertions.fail;
29  
30  public class JNDIUtilTest {
31  
32      @Test
33      public void ensureJavaNameSpace() throws NamingException {
34  
35          try {
36              Context ctxt = JNDIUtil.getInitialContext();
37              JNDIUtil.lookupString(ctxt, "ldap:...");
38          } catch (NamingException e) {
39              String excaptionMsg = e.getMessage();
40              if (excaptionMsg.startsWith(JNDIUtil.RESTRICTION_MSG))
41                  return;
42              else {
43                  fail("unexpected exception " + e);
44              }
45          }
46  
47          fail("Should aNot yet implemented");
48      }
49  
50      @Test
51      public void testToStringCast() throws NamingException {
52          Hashtable<String, String> props = new Hashtable<String, String>();
53          props.put(CoreTestConstants.JAVA_NAMING_FACTORY_INITIAL, MockInitialContextFactory.class.getCanonicalName());
54          Context ctxt = JNDIUtil.getInitialContext(props);
55          String x = JNDIUtil.lookupString(ctxt, "java:comp:/inexistent");
56          Assertions.assertNull(x);
57      }
58  
59      public String castToString(Object input) {
60          String a = (String) input;
61          return a;
62      }
63  
64  }