diff --git a/core-java-modules/core-java-jndi/pom.xml b/core-java-modules/core-java-jndi/pom.xml new file mode 100644 index 0000000000..eb363f8598 --- /dev/null +++ b/core-java-modules/core-java-jndi/pom.xml @@ -0,0 +1,65 @@ + + + 4.0.0 + + com.baeldung.jndi + core-java-jndi + 1.0-SNAPSHOT + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + ../../ + + + + + org.junit.jupiter + junit-jupiter + 5.5.1 + test + + + org.springframework + spring-core + 5.0.9.RELEASE + + + org.springframework + spring-context + 5.0.9.RELEASE + + + org.springframework + spring-jdbc + 5.0.9.RELEASE + + + org.springframework + spring-test + 5.0.9.RELEASE + test + + + com.h2database + h2 + 1.4.199 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + diff --git a/core-java-modules/core-java-jndi/src/test/java/com/baeldung/jndi/JndiUnitTest.java b/core-java-modules/core-java-jndi/src/test/java/com/baeldung/jndi/JndiUnitTest.java new file mode 100644 index 0000000000..9eea420e7a --- /dev/null +++ b/core-java-modules/core-java-jndi/src/test/java/com/baeldung/jndi/JndiUnitTest.java @@ -0,0 +1,60 @@ +package com.baeldung.jndi; + +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 JndiUnitTest { + + private static InitialContext ctx; + private static DriverManagerDataSource ds; + + @BeforeAll + static void setUp() throws Exception { + SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder(); + ds = new DriverManagerDataSource("jdbc:h2:mem:mydb"); + builder.activate(); + + JndiTemplate jndiTemplate = new JndiTemplate(); + ctx = (InitialContext) jndiTemplate.getContext(); + } + + @Test + void givenACompositeName_whenAddingAnElement_thenNameIncludesIt() throws Exception { + Name objectName = new CompositeName("java:comp/env/jdbc"); + + Enumeration elements = objectName.getAll(); + while(elements.hasMoreElements()) { + System.out.println(elements.nextElement()); + } + + objectName.add("example"); + + assertEquals("env", objectName.get(1)); + assertEquals("example", objectName.get(objectName.size() - 1)); + } + + @Test + void givenADataSource_whenAddingDriver_thenBind() throws Exception { + ds.setDriverClassName("org.h2.Driver"); + ctx.bind("java:comp/env/jdbc/datasource", ds); + } + + @Test + void givenContext_whenLookupByName_thenValidDataSource() throws Exception { + DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/datasource"); + + assertNotNull(ds); + assertNotNull(ds.getConnection()); + } + +} diff --git a/core-java-modules/core-java-jndi/src/test/java/com/baeldung/jndi/exceptions/JndiExceptionsUnitTest.java b/core-java-modules/core-java-jndi/src/test/java/com/baeldung/jndi/exceptions/JndiExceptionsUnitTest.java new file mode 100644 index 0000000000..49d4facffb --- /dev/null +++ b/core-java-modules/core-java-jndi/src/test/java/com/baeldung/jndi/exceptions/JndiExceptionsUnitTest.java @@ -0,0 +1,42 @@ +package com.baeldung.jndi.exceptions; + +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.springframework.jndi.JndiTemplate; +import org.springframework.mock.jndi.SimpleNamingContextBuilder; + +import javax.naming.InitialContext; +import javax.naming.NameNotFoundException; +import javax.naming.NoInitialContextException; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +public class JndiExceptionsUnitTest { + + @Test + @Order(1) + void givenNoContext_whenLookupObject_thenThrowNoInitialContext() { + assertThrows(NoInitialContextException.class, () -> { + JndiTemplate jndiTemplate = new JndiTemplate(); + InitialContext ctx = (InitialContext) jndiTemplate.getContext(); + ctx.lookup("java:comp/env/jdbc/datasource"); + }).printStackTrace(); + } + + @Test + @Order(2) + void givenEmptyContext_whenLookupNotBounds_thenThrowNameNotFound() { + assertThrows(NameNotFoundException.class, () -> { + SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder(); + builder.activate(); + + JndiTemplate jndiTemplate = new JndiTemplate(); + InitialContext ctx = (InitialContext) jndiTemplate.getContext(); + ctx.lookup("badJndiName"); + }).printStackTrace(); + } + +}