* Commit for Eval Article pull request

* Revert "Commit for Eval Article pull request"

* BAEL-46 - Deleted duplicate class.

* BAEL-46 - Fixed old script version

* BAEL-46 - Updated per editor review

* BAEL-46 - Updated JMX for corrected parameters

* BAEL-46 - Corrected Name and Directory

* Bael-3000 - initial commit

* Bael-3000 - update per review

* Bael-3000 - Updated per code review

* Bael-3000 - Updated per code review

* Update core-java-modules/core-java-jndi/pom.xml

Co-Authored-By: KevinGilmore <kpg102@gmail.com>

* Update core-java-modules/core-java-jndi/pom.xml

Co-Authored-By: KevinGilmore <kpg102@gmail.com>
This commit is contained in:
smokeyrobot 2019-08-15 03:55:05 -04:00 committed by ashleyfrieze
parent f7053358fe
commit e79a2858c3
3 changed files with 167 additions and 0 deletions

View File

@ -0,0 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.jndi</groupId>
<artifactId>core-java-jndi</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../../</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.9.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.199</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -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<String> 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());
}
}

View File

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