Skip to content

Commit e79a285

Browse files
smokeyrobotKevinGilmore
authored andcommitted
Bael 3000 (eugenp#7384)
* 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 <[email protected]> * Update core-java-modules/core-java-jndi/pom.xml Co-Authored-By: KevinGilmore <[email protected]>
1 parent f705335 commit e79a285

3 files changed

Lines changed: 167 additions & 0 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.baeldung.jndi</groupId>
8+
<artifactId>core-java-jndi</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<parent>
12+
<groupId>com.baeldung</groupId>
13+
<artifactId>parent-modules</artifactId>
14+
<version>1.0.0-SNAPSHOT</version>
15+
<relativePath>../../</relativePath>
16+
</parent>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>org.junit.jupiter</groupId>
21+
<artifactId>junit-jupiter</artifactId>
22+
<version>5.5.1</version>
23+
<scope>test</scope>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.springframework</groupId>
27+
<artifactId>spring-core</artifactId>
28+
<version>5.0.9.RELEASE</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.springframework</groupId>
32+
<artifactId>spring-context</artifactId>
33+
<version>5.0.9.RELEASE</version>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.springframework</groupId>
37+
<artifactId>spring-jdbc</artifactId>
38+
<version>5.0.9.RELEASE</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.springframework</groupId>
42+
<artifactId>spring-test</artifactId>
43+
<version>5.0.9.RELEASE</version>
44+
<scope>test</scope>
45+
</dependency>
46+
<dependency>
47+
<groupId>com.h2database</groupId>
48+
<artifactId>h2</artifactId>
49+
<version>1.4.199</version>
50+
</dependency>
51+
</dependencies>
52+
53+
<build>
54+
<plugins>
55+
<plugin>
56+
<groupId>org.apache.maven.plugins</groupId>
57+
<artifactId>maven-compiler-plugin</artifactId>
58+
<configuration>
59+
<source>1.8</source>
60+
<target>1.8</target>
61+
</configuration>
62+
</plugin>
63+
</plugins>
64+
</build>
65+
</project>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.jndi;
2+
3+
import org.junit.jupiter.api.BeforeAll;
4+
import org.junit.jupiter.api.Test;
5+
import org.springframework.jdbc.datasource.DriverManagerDataSource;
6+
import org.springframework.jndi.JndiTemplate;
7+
import org.springframework.mock.jndi.SimpleNamingContextBuilder;
8+
9+
import javax.naming.*;
10+
import javax.sql.DataSource;
11+
12+
import java.util.Enumeration;
13+
14+
import static org.junit.jupiter.api.Assertions.*;
15+
16+
class JndiUnitTest {
17+
18+
private static InitialContext ctx;
19+
private static DriverManagerDataSource ds;
20+
21+
@BeforeAll
22+
static void setUp() throws Exception {
23+
SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
24+
ds = new DriverManagerDataSource("jdbc:h2:mem:mydb");
25+
builder.activate();
26+
27+
JndiTemplate jndiTemplate = new JndiTemplate();
28+
ctx = (InitialContext) jndiTemplate.getContext();
29+
}
30+
31+
@Test
32+
void givenACompositeName_whenAddingAnElement_thenNameIncludesIt() throws Exception {
33+
Name objectName = new CompositeName("java:comp/env/jdbc");
34+
35+
Enumeration<String> elements = objectName.getAll();
36+
while(elements.hasMoreElements()) {
37+
System.out.println(elements.nextElement());
38+
}
39+
40+
objectName.add("example");
41+
42+
assertEquals("env", objectName.get(1));
43+
assertEquals("example", objectName.get(objectName.size() - 1));
44+
}
45+
46+
@Test
47+
void givenADataSource_whenAddingDriver_thenBind() throws Exception {
48+
ds.setDriverClassName("org.h2.Driver");
49+
ctx.bind("java:comp/env/jdbc/datasource", ds);
50+
}
51+
52+
@Test
53+
void givenContext_whenLookupByName_thenValidDataSource() throws Exception {
54+
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/datasource");
55+
56+
assertNotNull(ds);
57+
assertNotNull(ds.getConnection());
58+
}
59+
60+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.jndi.exceptions;
2+
3+
import org.junit.jupiter.api.MethodOrderer;
4+
import org.junit.jupiter.api.Order;
5+
import org.junit.jupiter.api.Test;
6+
import org.junit.jupiter.api.TestMethodOrder;
7+
import org.springframework.jndi.JndiTemplate;
8+
import org.springframework.mock.jndi.SimpleNamingContextBuilder;
9+
10+
import javax.naming.InitialContext;
11+
import javax.naming.NameNotFoundException;
12+
import javax.naming.NoInitialContextException;
13+
14+
import static org.junit.jupiter.api.Assertions.assertThrows;
15+
16+
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
17+
public class JndiExceptionsUnitTest {
18+
19+
@Test
20+
@Order(1)
21+
void givenNoContext_whenLookupObject_thenThrowNoInitialContext() {
22+
assertThrows(NoInitialContextException.class, () -> {
23+
JndiTemplate jndiTemplate = new JndiTemplate();
24+
InitialContext ctx = (InitialContext) jndiTemplate.getContext();
25+
ctx.lookup("java:comp/env/jdbc/datasource");
26+
}).printStackTrace();
27+
}
28+
29+
@Test
30+
@Order(2)
31+
void givenEmptyContext_whenLookupNotBounds_thenThrowNameNotFound() {
32+
assertThrows(NameNotFoundException.class, () -> {
33+
SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
34+
builder.activate();
35+
36+
JndiTemplate jndiTemplate = new JndiTemplate();
37+
InitialContext ctx = (InitialContext) jndiTemplate.getContext();
38+
ctx.lookup("badJndiName");
39+
}).printStackTrace();
40+
}
41+
42+
}

0 commit comments

Comments
 (0)