BAEL-4200: JNDI - What Is java:comp/env? (#15081)

This commit is contained in:
Palaniappan Arunachalam 2023-11-02 09:28:54 +05:30 committed by GitHub
parent d8fb8b381f
commit 57840bb083
1 changed files with 72 additions and 0 deletions

View File

@ -0,0 +1,72 @@
package com.baeldung.jndi;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.jndi.JndiTemplate;
import org.springframework.mock.jndi.SimpleNamingContextBuilder;
import javax.naming.*;
import javax.sql.DataSource;
import java.util.Enumeration;
import static org.junit.jupiter.api.Assertions.*;
class JndiNamingUnitTest {
private static InitialContext context;
private static DriverManagerDataSource dataSource;
@BeforeAll
static void setUp() throws Exception {
SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
dataSource = new DriverManagerDataSource("jdbc:h2:mem:mydb");
builder.activate();
JndiTemplate jndiTemplate = new JndiTemplate();
context = (InitialContext) jndiTemplate.getContext();
dataSource.setDriverClassName("org.h2.Driver");
context.bind("java:comp/env/jdbc/datasource", dataSource);
}
@Test
void givenACompositeName_whenAddingAnElement_thenNameIsAdded() throws Exception {
Name objectName = new CompositeName("java:comp/env/jdbc");
Enumeration<String> items = objectName.getAll();
while(items.hasMoreElements()) {
System.out.println(items.nextElement());
}
objectName.add("New Name");
assertEquals("env", objectName.get(1));
assertEquals("New Name", objectName.get(objectName.size() - 1));
}
@Test
void givenContext_whenLookupByName_thenReturnsValidObject() throws Exception {
DataSource ds = (DataSource) context.lookup("java:comp/env/jdbc/datasource");
assertNotNull(ds);
assertNotNull(ds.getConnection());
}
@Test
void givenSubContext_whenLookupByName_thenReturnsValidObject() throws Exception {
Context subContext = (Context) context.lookup("java:comp/env");
DataSource ds = (DataSource) subContext.lookup("jdbc/datasource");
assertNotNull(ds);
assertNotNull(ds.getConnection());
}
@AfterAll
static void tearDown() throws Exception {
context.close();
}
}