diff --git a/persistence-modules/spring-persistence-simple-2/pom.xml b/persistence-modules/spring-persistence-simple-2/pom.xml index 8380d635de..b8f3b384a2 100644 --- a/persistence-modules/spring-persistence-simple-2/pom.xml +++ b/persistence-modules/spring-persistence-simple-2/pom.xml @@ -32,6 +32,13 @@ ${h2.version} test + + + + com.github.h-thurow + simple-jndi + ${simple-jndi.version} + @@ -53,6 +60,8 @@ 5.2.4.RELEASE 1.4.200 + + 0.23.0 3.3.3 diff --git a/persistence-modules/spring-persistence-simple-2/src/main/resources/jndi.properties b/persistence-modules/spring-persistence-simple-2/src/main/resources/jndi.properties new file mode 100644 index 0000000000..d976f16c02 --- /dev/null +++ b/persistence-modules/spring-persistence-simple-2/src/main/resources/jndi.properties @@ -0,0 +1,6 @@ +java.naming.factory.initial=org.osjava.sj.SimpleContextFactory +org.osjava.sj.jndi.shared=true +org.osjava.sj.delimiter=. +jndi.syntax.separator=/ +org.osjava.sj.space=java:/comp/env +org.osjava.sj.root=src/main/resources/jndi diff --git a/persistence-modules/spring-persistence-simple-2/src/main/resources/jndi/datasource.properties b/persistence-modules/spring-persistence-simple-2/src/main/resources/jndi/datasource.properties new file mode 100644 index 0000000000..8be94daf36 --- /dev/null +++ b/persistence-modules/spring-persistence-simple-2/src/main/resources/jndi/datasource.properties @@ -0,0 +1,5 @@ +ds.type=javax.sql.DataSource +ds.driver=org.h2.Driver +ds.url=jdbc:jdbc:h2:mem:testdb +ds.user=sa +ds.password=password diff --git a/persistence-modules/spring-persistence-simple-2/src/test/java/com/baeldung/jndi/datasource/SimpleJNDIUnitTest.java b/persistence-modules/spring-persistence-simple-2/src/test/java/com/baeldung/jndi/datasource/SimpleJNDIUnitTest.java new file mode 100644 index 0000000000..37f33b1192 --- /dev/null +++ b/persistence-modules/spring-persistence-simple-2/src/test/java/com/baeldung/jndi/datasource/SimpleJNDIUnitTest.java @@ -0,0 +1,39 @@ +package com.baeldung.jndi.datasource; + +import static org.junit.Assert.assertEquals; + +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.sql.DataSource; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class SimpleJNDIUnitTest { + + private InitialContext initContext; + + @BeforeEach + public void setup() throws Exception { + this.initContext = new InitialContext(); + } + + @Test + public void whenMockJndiDataSource_thenReturnJndiDataSource() throws Exception { + String dsString = "org.h2.Driver::::jdbc:jdbc:h2:mem:testdb::::sa"; + Context envContext = (Context) this.initContext.lookup("java:/comp/env"); + DataSource ds = (DataSource) envContext.lookup("datasource/ds"); + + assertEquals(dsString, ds.toString()); + } + + @AfterEach + public void tearDown() throws Exception { + if (this.initContext != null) { + this.initContext.close(); + this.initContext = null; + } + } + +} diff --git a/persistence-modules/spring-persistence-simple-2/src/test/java/com/baeldung/jndi/datasource/SimpleNamingContextBuilderUnitTest.java b/persistence-modules/spring-persistence-simple-2/src/test/java/com/baeldung/jndi/datasource/SimpleNamingContextBuilderUnitTest.java new file mode 100644 index 0000000000..d0a06dd2bc --- /dev/null +++ b/persistence-modules/spring-persistence-simple-2/src/test/java/com/baeldung/jndi/datasource/SimpleNamingContextBuilderUnitTest.java @@ -0,0 +1,41 @@ +package com.baeldung.jndi.datasource; + +import static org.junit.Assert.assertNotNull; + +import javax.naming.InitialContext; +import javax.sql.DataSource; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.jdbc.datasource.DriverManagerDataSource; +import org.springframework.mock.jndi.SimpleNamingContextBuilder; + +@SuppressWarnings("deprecation") +public class SimpleNamingContextBuilderUnitTest { + + private InitialContext initContext; + + @BeforeEach + public void init() throws Exception { + SimpleNamingContextBuilder.emptyActivatedContextBuilder(); + this.initContext = new InitialContext(); + } + + @Test + public void whenMockJndiDataSource_thenReturnJndiDataSource() throws Exception { + this.initContext.bind("java:comp/env/jdbc/datasource", new DriverManagerDataSource("jdbc:h2:mem:testdb")); + DataSource ds = (DataSource) this.initContext.lookup("java:comp/env/jdbc/datasource"); + + assertNotNull(ds.getConnection()); + } + + @AfterEach + public void tearDown() throws Exception { + if (this.initContext != null) { + this.initContext.close(); + this.initContext = null; + } + } + +} \ No newline at end of file diff --git a/persistence-modules/spring-persistence-simple-2/src/test/resources/datasource.properties b/persistence-modules/spring-persistence-simple-2/src/test/resources/datasource.properties new file mode 100644 index 0000000000..a86a6c6ce2 --- /dev/null +++ b/persistence-modules/spring-persistence-simple-2/src/test/resources/datasource.properties @@ -0,0 +1,5 @@ +ds.type=javax.sql.DataSource +ds.driver=org.h2.Driver +ds.url=jdbc:jdbc:h2:mem:testdb +ds.user=sa +ds.password=password \ No newline at end of file