Skip to content

Commit

Permalink
Add support for JUnit 5 extensions (zapodot#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjansen committed Dec 26, 2022
1 parent 01b351a commit cdc9d2f
Show file tree
Hide file tree
Showing 14 changed files with 805 additions and 524 deletions.
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# embedded-ldap-junit
[![Build Status](https://github.com/zapodot/embedded-ldap-junit/actions/workflows/maven.yml/badge.svg)](https://github.com/zapodot/embedded-ldap-junit/actions/workflows/maven.yml) [![codecov](https://codecov.io/gh/zapodot/embedded-ldap-junit/branch/master/graph/badge.svg?token=2jm8uT1bJg)](https://codecov.io/gh/zapodot/embedded-ldap-junit) [![Maven Central](https://maven-badges.herokuapp.com/maven-central/org.zapodot/embedded-ldap-junit/badge.svg)](https://maven-badges.herokuapp.com/maven-central/org.zapodot/embedded-ldap-junit) [![Libraries.io for GitHub](https://img.shields.io/librariesio/github/zapodot/embedded-ldap-junit.svg)](https://libraries.io/github/zapodot/embedded-ldap-junit) [![GitHub](https://img.shields.io/github/license/zapodot/embedded-ldap-junit)](https://github.com/zapodot/embedded-ldap-junit/blob/master/LICENSE) [![Analytics](https://ga-beacon.appspot.com/UA-40926073-2/embedded-ldap-junit/README.md)](https://github.com/igrigorik/ga-beacon)

A [JUnit Rule](//github.com/junit-team/junit/wiki/Rules) for running an embedded LDAP server in your JUnit test based on the wonderful [UnboundID LDAP SDK](https://www.ldap.com/unboundid-ldap-sdk-for-java). Inspired by the [Embedded Database JUnit Rule](//github.com/zapodot/embedded-db-junit).
A [JUnit 4 Rule](//github.com/junit-team/junit/wiki/Rules) and [JUnit 5 Extension](https://junit.org/junit5/docs/current/user-guide/#extensions) for running an embedded LDAP server in your JUnit test based on the wonderful [UnboundID LDAP SDK](https://www.ldap.com/unboundid-ldap-sdk-for-java). Inspired by the [Embedded Database JUnit Rule](//github.com/zapodot/embedded-db-junit).

## Why?
* you want to test your LDAP integration code without affecting your LDAP server
Expand Down Expand Up @@ -29,7 +29,9 @@ See [releases](//github.com/zapodot/embedded-ldap-junit/releases)
</dependency>
```

### Add to Junit test
### Add to JUnit test

#### JUnit 4
```java
import com.unboundid.ldap.sdk.LDAPInterface;
import javax.naming.Context;
Expand Down Expand Up @@ -79,3 +81,19 @@ public void testContext() throws Exception {
assertNotNull(user);
}
```

#### JUnit 5
For JUnit 5 tests, simply use `EmbeddedLdapExtensionBuilder` instead of `EmbeddedLdapRuleBuilder`:

```java
@RegisterExtension
public EmbeddedLdapExtension embeddedLdapRule = EmbeddedLdapExtensionBuilder
.newInstance()
.usingDomainDsn("dc=example,dc=com")
.importingLdifs("example.ldif")
.build();

...
```

Both JUnit 4 and JUnit 5 builders share the same API, as do the rule and the extension.
10 changes: 9 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
</developers>
<properties>
<junit.version>4.13.2</junit.version>
<junit5.version>5.7.0</junit5.version>
<unboundid-ldapsdk.version>6.0.6</unboundid-ldapsdk.version>
<slf4j.version>1.7.36</slf4j.version>
<guava.version>31.1-jre</guava.version>
Expand Down Expand Up @@ -51,6 +52,12 @@
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit5.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand Down Expand Up @@ -270,4 +277,5 @@
</properties>
</profile>
</profiles>
</project>
</project>

57 changes: 1 addition & 56 deletions src/main/java/org/zapodot/junit/ldap/EmbeddedLdapRule.java
Original file line number Diff line number Diff line change
@@ -1,65 +1,10 @@
package org.zapodot.junit.ldap;

import com.unboundid.ldap.sdk.LDAPConnection;
import com.unboundid.ldap.sdk.LDAPException;
import com.unboundid.ldap.sdk.LDAPInterface;
import org.junit.rules.TestRule;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;

/**
* A JUnit rule that may be used as either a @Rule or a @ClassRule
*/
public interface EmbeddedLdapRule extends TestRule {

/**
* For tests depending on the UnboundID LDAP SDK. Returns a proxied version of an Unboundid interface that will be
* closed when the test(s) have been invoked
*
* @return a shared LDAPConnection
* @throws LDAPException if a connection can not be opened
*/
LDAPInterface ldapConnection() throws LDAPException;

/**
* For tests depending on the UnboundID LDAP SDK that needs access to an ${link LDAPConnection} object
* rather than the interface. If your code does not close the connection for you it will be closed on teardown
*
* @return a LDAPConnection connected to the embedded LDAP server
* @throws LDAPException if an exception occurred while establishing the connection
*/
LDAPConnection unsharedLdapConnection() throws LDAPException;

/**
* For tests depending on the standard Java JNDI API
*
* @return a shared Context connected to the in-memory LDAP server
* @throws NamingException if context can not be created
*/
Context context() throws NamingException;

/**
* Like {@link #context()}, but returns a DirContext
*
* @return a DirContext connected to the in-memory LDAP server
* @throws NamingException if a LDAP failure happens during DirContext creation
*/
DirContext dirContext() throws NamingException;

/**
* Gives access to the listening port for the currently running embedded LDAP server.
* This will make it easier to use other integration mechanisms
* <p>
* Note: the embedded LDAP server is by default configured to listen only on the loopback address
* (i.e <em>localhost/127.0.0.1</em>) unless another address has been provided to the builder when the rule was built
* </p>
*
* @return the port number that the embedded server is listening to
* @see org.zapodot.junit.ldap.EmbeddedLdapRuleBuilder#bindingToAddress(String)
*/
int embeddedServerPort();

public interface EmbeddedLdapRule extends EmbeddedLdapServer, TestRule {

}
Loading

0 comments on commit cdc9d2f

Please sign in to comment.