Merge pull request #9367 from azhwani/jndidatasource

BAEL-3830: Test a mock JNDI datasource with Spring
This commit is contained in:
Jonathan Cook 2020-06-18 15:29:02 +02:00 committed by GitHub
commit 8834be5209
6 changed files with 105 additions and 0 deletions

View File

@ -32,6 +32,13 @@
<version>${h2.version}</version>
<scope>test</scope>
</dependency>
<!-- simple-jndi -->
<dependency>
<groupId>com.github.h-thurow</groupId>
<artifactId>simple-jndi</artifactId>
<version>${simple-jndi.version}</version>
</dependency>
<!-- test scoped -->
<dependency>
@ -53,6 +60,8 @@
<org.springframework.version>5.2.4.RELEASE</org.springframework.version>
<!-- persistence -->
<h2.version>1.4.200</h2.version>
<!-- simple-jndi -->
<simple-jndi.version>0.23.0</simple-jndi.version>
<!-- test scoped -->
<mockito.version>3.3.3</mockito.version>
</properties>

View File

@ -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

View File

@ -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

View File

@ -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;
}
}
}

View File

@ -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;
}
}
}

View File

@ -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