Skip to content

Commit

Permalink
Build new version based on JUnit 5 (#3)
Browse files Browse the repository at this point in the history
* Refactor SharedResource into JUnit5 extension
* Refactor ZookeeperSharedResource instance into JUnit5 extension
* Refactoring into modules
* add kafka-junit4 module
* refactor common code into core module
* update changelog
  • Loading branch information
Crim authored Apr 10, 2018
1 parent d501d6e commit 5285678
Show file tree
Hide file tree
Showing 25 changed files with 1,372 additions and 168 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## 1.0.0 (11/09/2017)
## 2.0.0 (4/10/2018)
- Created new modules to support both JUnit4 and JUnit 5.

## 1.0.0 (09/11/2017)
- Initial release!
- Based off of Kafka Server and Kafka-Clients version 0.11.0.1
- Built for JUnit 4.x

2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2017, Salesforce.com, Inc.
Copyright (c) 2017-2018, Salesforce.com, Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
Expand Down
92 changes: 7 additions & 85 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,91 +5,13 @@
This library wraps Kafka Test Server and allows you to easily create and run tests against
a "real" kafka server running within your tests, no more needing to stand up an external kafka cluster!

## Usage & Examples

Include this in your project with scope test.

```
<dependency>
<groupId>com.salesforce.kafka.test</groupId>
<artifactId>kafka-junit</artifactId>
<version>1.0.0</version>
<scope>test</scope>
</dependency>
```

### KafkaTestServer

A great example of how to use this can be found within our tests! Check out [KafkaTestServerTest.java](src/test/java/com/salesforce/kafka/test/KafkaTestServerTest.java)

Add the following to your JUnit test file and it will handle automatically starting and stopping the embedded Kafka
instance for you.

```java
/**
* We have a single embedded kafka server that gets started when this test class is initialized.
*
* It's automatically started before any methods are run via the @ClassRule annotation.
* It's automatically stopped after all of the tests are completed via the @ClassRule annotation.
*/
@ClassRule
public static final SharedKafkaTestResource sharedKafkaTestResource = new SharedKafkaTestResource();
```

SharedKafkaTestResource has two accessors that you can make use of in your tests to interact with the service.

```java
/**
* @return Shared Kafka Test server instance.
*/
public KafkaTestServer getKafkaTestServer();

/**
* @return Instance of KafkaTestUtils configured and ready to go.
*/
public KafkaTestUtils getKafkaTestUtils();
```

### KafkaTestUtils

Often times you'll end up rebuilding the same patterns around producing and consuming data from this internal
kafka server. We've tried to collect some of these within [KafkaTestUtils](src/main/java/com/salesforce/kafka/test/KafkaTestUtils.java)!

For usage and examples, check out it's test at [KafkaTestUtilsTest](src/test/java/com/salesforce/kafka/test/KafkaTestUtilsTest.java).

### Zookeeper Test Server

**Note** Since Kafka depends on Zookeeper, you get this for *free* if you use the SharedKafkaTestResource, you do not, and should not, use
both of these together within the same Test class.

If you need to run tests against an **only** embedded Zookeeper server and not all of Kafka, we have you covered as well. Add the following
to your JUnit test file
and it will handle automatically start and stopping the embedded Zookeeper instance for you.

```java
/**
* We have a single embedded zookeeper server that gets started when this test class is initialized.
*
* It's automatically started before any methods are run via the @ClassRule annotation.
* It's automatically stopped after all of the tests are completed via the @ClassRule annotation.
*/
@ClassRule
public static final SharedZookeeperTestResource sharedZookeeperTestResource = new SharedZookeeperTestResource();
```

SharedZookeeperTestResource has the following accessors that you can make use of in your tests to interact with the Zookeeper instance.

```java
/**
* @return Shared Zookeeper test server instance.
*/
public TestingServer getZookeeperTestServer();

/**
* @return Connection string to connect to the Zookeeper instance.
*/
public String getZookeeperConnectString();
```
## Using Kafka-JUnit with JUnit 4.

Please review [Kafka-JUnit4 Readme](kafka-junit4/) for instructions.

## Using Kafka-JUnit with JUnit 5.

Please review [Kafka-JUnit5 Readme](kafka-junit5/) for instructions.

## Changelog

Expand Down
16 changes: 16 additions & 0 deletions kafka-junit-core/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?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">
<parent>
<artifactId>kafka-junit</artifactId>
<groupId>com.salesforce.kafka.test</groupId>
<version>2.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>kafka-junit-core</artifactId>
<version>1.0.0</version>


</project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2017, Salesforce.com, Inc.
* Copyright (c) 2017-2018, Salesforce.com, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2017, Salesforce.com, Inc.
* Copyright (c) 2017-2018, Salesforce.com, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2017, Salesforce.com, Inc.
* Copyright (c) 2017-2018, Salesforce.com, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
Expand Down
96 changes: 96 additions & 0 deletions kafka-junit4/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Kafka-JUnit4

This library wraps Kafka Test Server and allows you to easily create and run tests against
a "real" kafka server running within your tests, no more needing to stand up an external kafka cluster!

Kafka-JUnit4 is built on-top of **JUnit 4** as a SharedResource using the **@ClassRule** annotation.

For usage with JUnit5 or more project information please review top level [README](../README.md).

## Using Kafka-JUnit with JUnit 4.

### Usage & Examples

Include this in your project with scope test.

```
<dependency>
<groupId>com.salesforce.kafka.test</groupId>
<artifactId>kafka-junit4</artifactId>
<version>1.0.0</version>
<scope>test</scope>
</dependency>
```

#### KafkaTestServer

A great example of how to use this can be found within our tests! Check out [KafkaTestServerTest.java](src/test/java/com/salesforce/kafka/test/KafkaTestServerTest.java)

Add the following to your JUnit test file and it will handle automatically starting and stopping the embedded Kafka
instance for you.

```java
/**
* We have a single embedded kafka server that gets started when this test class is initialized.
*
* It's automatically started before any methods are run via the @ClassRule annotation.
* It's automatically stopped after all of the tests are completed via the @ClassRule annotation.
*/
@ClassRule
public static final SharedKafkaTestResource sharedKafkaTestResource = new SharedKafkaTestResource();
```

SharedKafkaTestResource has two accessors that you can make use of in your tests to interact with the service.

```java
/**
* @return Shared Kafka Test server instance.
*/
public KafkaTestServer getKafkaTestServer();

/**
* @return Instance of KafkaTestUtils configured and ready to go.
*/
public KafkaTestUtils getKafkaTestUtils();
```

#### KafkaTestUtils

Often times you'll end up rebuilding the same patterns around producing and consuming data from this internal
kafka server. We've tried to collect some of these within [KafkaTestUtils](src/main/java/com/salesforce/kafka/test/KafkaTestUtils.java)!

For usage and examples, check out it's test at [KafkaTestUtilsTest](src/test/java/com/salesforce/kafka/test/KafkaTestUtilsTest.java).

#### Zookeeper Test Server

**Note** Since Kafka depends on Zookeeper, you get this for *free* if you use the SharedKafkaTestResource, you do not, and should not, use
both of these together within the same Test class.

If you need to run tests against an **only** embedded Zookeeper server and not all of Kafka, we have you covered as well. Add the following
to your JUnit test file
and it will handle automatically start and stopping the embedded Zookeeper instance for you.

```java
/**
* We have a single embedded zookeeper server that gets started when this test class is initialized.
*
* It's automatically started before any methods are run via the @ClassRule annotation.
* It's automatically stopped after all of the tests are completed via the @ClassRule annotation.
*/
@ClassRule
public static final SharedZookeeperTestResource sharedZookeeperTestResource = new SharedZookeeperTestResource();
```

SharedZookeeperTestResource has the following accessors that you can make use of in your tests to interact with the Zookeeper instance.

```java
/**
* @return Shared Zookeeper test server instance.
*/
public TestingServer getZookeeperTestServer();

/**
* @return Connection string to connect to the Zookeeper instance.
*/
public String getZookeeperConnectString();
```
115 changes: 115 additions & 0 deletions kafka-junit4/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2017-2018, Salesforce.com, Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following
disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of Salesforce.com nor the names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->
<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">

<parent>
<artifactId>kafka-junit</artifactId>
<groupId>com.salesforce.kafka.test</groupId>
<version>2.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>kafka-junit4</artifactId>
<version>1.0.0</version>

<!-- defined properties -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<!-- Define which JUnit version -->
<junit.version>4.12</junit.version>
</properties>

<dependencies>
<!-- Core module -->
<dependency>
<groupId>com.salesforce.kafka.test</groupId>
<artifactId>kafka-junit-core</artifactId>
<version>1.0.0</version>
</dependency>

<!-- JUnit is Required -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
</dependencies>

<build>
<sourceDirectory>src/main/java</sourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>

<plugins>
<!-- Build Jar with Dependencies -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass/>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

<!-- Surefire plugin for running tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.19</version>
</dependency>
</dependencies>
<configuration>
<argLine>-Xmx512M</argLine>
<skipTests>${skipTests}</skipTests>
<reportFormat>plain</reportFormat>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2017, Salesforce.com, Inc.
* Copyright (c) 2017-2018, Salesforce.com, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
Expand All @@ -23,10 +23,10 @@
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.salesforce.kafka.test.junit;
package com.salesforce.kafka.test.junit4;

import com.salesforce.kafka.test.KafkaTestUtils;
import com.salesforce.kafka.test.KafkaTestServer;
import com.salesforce.kafka.test.KafkaTestUtils;
import org.apache.curator.test.TestingServer;
import org.junit.rules.ExternalResource;
import org.slf4j.Logger;
Expand Down
Loading

0 comments on commit 5285678

Please sign in to comment.