├── embedded-jms-junit
├── src
│ ├── main
│ │ └── java
│ │ │ └── org
│ │ │ └── zapodot
│ │ │ └── junit
│ │ │ └── jms
│ │ │ ├── package-info.java
│ │ │ ├── impl
│ │ │ ├── package-info.java
│ │ │ └── EmbeddedJmsRuleImpl.java
│ │ │ ├── EmbeddedJmsRuleBuilder.java
│ │ │ └── EmbeddedJmsRule.java
│ └── test
│ │ └── java
│ │ └── org
│ │ └── zapodot
│ │ └── junit
│ │ └── jms
│ │ ├── EmbeddedJmsRuleMarshalEnabledTest.java
│ │ ├── impl
│ │ └── EmbeddedJmsRuleImplTest.java
│ │ ├── SendReceivable.java
│ │ ├── EmbeddedJmsRuleSpringJmsTest.java
│ │ ├── EmbeddedJmsRuleCamelTest.java
│ │ ├── CallablesForSendingAndReceiving.java
│ │ └── EmbeddedJmsRuleStandardSetupTest.java
└── pom.xml
├── embedded-jms-junit5
├── src
│ ├── main
│ │ └── java
│ │ │ └── org
│ │ │ └── zapodot
│ │ │ └── junit5
│ │ │ └── jms
│ │ │ ├── package-info.java
│ │ │ ├── internal
│ │ │ ├── package-info.java
│ │ │ ├── InjectableFieldsAccessor.java
│ │ │ ├── BrokerConfiguration.java
│ │ │ ├── BrokerConfigurationBuilder.java
│ │ │ └── FieldInjector.java
│ │ │ ├── annotations
│ │ │ ├── package-info.java
│ │ │ ├── EmbeddedJms.java
│ │ │ └── BrokerConfig.java
│ │ │ └── EmbeddedJmsBroker.java
│ └── test
│ │ └── java
│ │ └── org
│ │ └── zapodot
│ │ └── junit5
│ │ └── jms
│ │ ├── AbstractJmsTest.java
│ │ ├── EmbeddedJmsBrokerSubclassTest.java
│ │ ├── EmbeddedJmsBrokerRegisterExtensionStaticFieldTest.java
│ │ ├── EmbeddedJmsDefaultSetupTest.java
│ │ ├── internal
│ │ ├── FieldInjectorTest.java
│ │ └── BrokerConfigurationTest.java
│ │ ├── EmbeddedJmsBrokerWithConfigTest.java
│ │ ├── EmbeddedJmsBrokerRegisterExtensionFieldTest.java
│ │ ├── EmbeddedJmsBrokerNestedTest.java
│ │ ├── EmbeddedJmsBrokerWithParameterTest.java
│ │ ├── EmbeddedJmsBrokerPersistenceTest.java
│ │ ├── EmbeddedJmsBrokerRequestReplySpringTest.java
│ │ └── EmbeddedJmsBrokerCamelTest.java
└── pom.xml
├── embedded-jms-core
├── src
│ ├── main
│ │ └── java
│ │ │ └── org
│ │ │ └── zapodot
│ │ │ └── jms
│ │ │ └── common
│ │ │ ├── BrokerURIAccessor.java
│ │ │ ├── package-info.java
│ │ │ ├── ConnectionFactoryAccessor.java
│ │ │ ├── ActiveMQConnectionFactoryAccessor.java
│ │ │ ├── BrokerSettings.java
│ │ │ └── EmbeddedJMSBrokerHolder.java
│ └── test
│ │ └── java
│ │ └── org
│ │ └── zapodot
│ │ └── jms
│ │ └── common
│ │ ├── TemporaryDirectory.java
│ │ ├── EmbeddedJMSBrokerHolderTest.java
│ │ └── EmbeddedJMSBrokerHolderConfigurerTest.java
└── pom.xml
├── .gitignore
├── .github
├── dependabot.yml
└── workflows
│ └── maven.yml
├── old.travis.yml
├── CODE_OF_CONDUCT.md
├── README.md
├── LICENSE
└── pom.xml
/embedded-jms-junit/src/main/java/org/zapodot/junit/jms/package-info.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Public API
3 | *
4 | * @author zapodot at gmail dot com
5 | */
6 | package org.zapodot.junit.jms;
--------------------------------------------------------------------------------
/embedded-jms-junit5/src/main/java/org/zapodot/junit5/jms/package-info.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Public API
3 | *
4 | * @author zapodot at gmail dot com
5 | */
6 | package org.zapodot.junit5.jms;
--------------------------------------------------------------------------------
/embedded-jms-core/src/main/java/org/zapodot/jms/common/BrokerURIAccessor.java:
--------------------------------------------------------------------------------
1 | package org.zapodot.jms.common;
2 |
3 | import java.net.URI;
4 |
5 | public interface BrokerURIAccessor {
6 | URI getBrokerUri();
7 | }
8 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # maven files #
2 | target/
3 | pom.xml.tag
4 | pom.xml.releaseBackup
5 | pom.xml.versionsBackup
6 | pom.xml.next
7 | release.properties
8 |
9 | # idea Files #
10 | .idea
11 | *.ipr
12 | *.iml
13 | *.iws
14 |
--------------------------------------------------------------------------------
/embedded-jms-core/src/main/java/org/zapodot/jms/common/package-info.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Internal API. May be removed, moved or changed without prior deprecation
3 | *
4 | * @author zapodot at gmail dot com
5 | */
6 | package org.zapodot.jms.common;
--------------------------------------------------------------------------------
/embedded-jms-junit/src/main/java/org/zapodot/junit/jms/impl/package-info.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Internal API. May be removed, moved or changed without prior deprecation
3 | *
4 | * @author zapodot at gmail dot com
5 | */
6 | package org.zapodot.junit.jms.impl;
--------------------------------------------------------------------------------
/embedded-jms-junit5/src/main/java/org/zapodot/junit5/jms/internal/package-info.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Internal API. May be removed, moved or changed without prior deprecation
3 | *
4 | * @author zapodot at gmail dot com
5 | */
6 | package org.zapodot.junit5.jms.internal;
--------------------------------------------------------------------------------
/embedded-jms-core/src/main/java/org/zapodot/jms/common/ConnectionFactoryAccessor.java:
--------------------------------------------------------------------------------
1 | package org.zapodot.jms.common;
2 |
3 | import javax.jms.ConnectionFactory;
4 |
5 | public interface ConnectionFactoryAccessor {
6 | ConnectionFactory getConnectionFactory();
7 | }
8 |
--------------------------------------------------------------------------------
/embedded-jms-junit5/src/main/java/org/zapodot/junit5/jms/annotations/package-info.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Public API - annotations used to support declarative style configuration and injection
3 | *
4 | * @author zapodot at gmail dot com
5 | */
6 | package org.zapodot.junit5.jms.annotations;
--------------------------------------------------------------------------------
/embedded-jms-core/src/main/java/org/zapodot/jms/common/ActiveMQConnectionFactoryAccessor.java:
--------------------------------------------------------------------------------
1 | package org.zapodot.jms.common;
2 |
3 | import org.apache.activemq.ActiveMQConnectionFactory;
4 |
5 | public interface ActiveMQConnectionFactoryAccessor {
6 | ActiveMQConnectionFactory getActiveMQConnectionFactory();
7 | }
8 |
--------------------------------------------------------------------------------
/.github/dependabot.yml:
--------------------------------------------------------------------------------
1 | # To get started with Dependabot version updates, you'll need to specify which
2 | # package ecosystems to update and where the package manifests are located.
3 | # Please see the documentation for all configuration options:
4 | # https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file
5 |
6 | version: 2
7 | updates:
8 | - package-ecosystem: "maven" # See documentation for possible values
9 | directory: "/" # Location of package manifests
10 | schedule:
11 | interval: "daily"
12 |
--------------------------------------------------------------------------------
/embedded-jms-junit5/src/main/java/org/zapodot/junit5/jms/annotations/EmbeddedJms.java:
--------------------------------------------------------------------------------
1 | package org.zapodot.junit5.jms.annotations;
2 |
3 | import java.lang.annotation.Documented;
4 | import java.lang.annotation.ElementType;
5 | import java.lang.annotation.Inherited;
6 | import java.lang.annotation.Retention;
7 | import java.lang.annotation.RetentionPolicy;
8 | import java.lang.annotation.Target;
9 |
10 | /**
11 | * Inject a JMS ConnectionFactory to the given field or parameter.
12 | */
13 | @Target({ElementType.FIELD, ElementType.PARAMETER})
14 | @Retention(RetentionPolicy.RUNTIME)
15 | @Inherited
16 | @Documented
17 | public @interface EmbeddedJms {
18 | }
19 |
--------------------------------------------------------------------------------
/embedded-jms-junit5/src/test/java/org/zapodot/junit5/jms/AbstractJmsTest.java:
--------------------------------------------------------------------------------
1 | package org.zapodot.junit5.jms;
2 |
3 | import org.junit.jupiter.api.extension.ExtendWith;
4 | import org.zapodot.junit5.jms.annotations.BrokerConfig;
5 | import org.zapodot.junit5.jms.annotations.EmbeddedJms;
6 |
7 | import javax.jms.ConnectionFactory;
8 |
9 | @ExtendWith(EmbeddedJmsBroker.class)
10 | @BrokerConfig(name = AbstractJmsTest.BROKER_SUPER_BROKER)
11 | public abstract class AbstractJmsTest {
12 |
13 | protected static final String BROKER_SUPER_BROKER = "SuperDuperBroker";
14 |
15 | @EmbeddedJms
16 | protected ConnectionFactory connectionFactory;
17 | }
18 |
--------------------------------------------------------------------------------
/embedded-jms-junit5/src/test/java/org/zapodot/junit5/jms/EmbeddedJmsBrokerSubclassTest.java:
--------------------------------------------------------------------------------
1 | package org.zapodot.junit5.jms;
2 |
3 | import org.junit.jupiter.api.Test;
4 | import org.zapodot.junit5.jms.annotations.EmbeddedJms;
5 |
6 | import javax.jms.ConnectionFactory;
7 | import java.net.URI;
8 |
9 | import static org.junit.jupiter.api.Assertions.assertEquals;
10 | import static org.junit.jupiter.api.Assertions.assertNotNull;
11 |
12 | public class EmbeddedJmsBrokerSubclassTest extends AbstractJmsTest {
13 |
14 | @EmbeddedJms
15 | private ConnectionFactory subConnectionFactory;
16 |
17 | @EmbeddedJms
18 | private URI brokerUri;
19 |
20 | @Test
21 | void testIfInjectionWorksIfDefinedInSuperClass() {
22 | assertNotNull(super.connectionFactory);
23 | assertNotNull(subConnectionFactory);
24 | assertEquals("vm://" + AbstractJmsTest.BROKER_SUPER_BROKER, brokerUri.toString());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/embedded-jms-junit5/src/test/java/org/zapodot/junit5/jms/EmbeddedJmsBrokerRegisterExtensionStaticFieldTest.java:
--------------------------------------------------------------------------------
1 | package org.zapodot.junit5.jms;
2 |
3 | import org.junit.jupiter.api.DisplayName;
4 | import org.junit.jupiter.api.Test;
5 | import org.junit.jupiter.api.extension.RegisterExtension;
6 | import org.zapodot.junit5.jms.annotations.EmbeddedJms;
7 |
8 | import javax.jms.ConnectionFactory;
9 |
10 | import static org.junit.jupiter.api.Assertions.assertNotNull;
11 |
12 | @DisplayName("Injection using @RegisterExtension on static field")
13 | class EmbeddedJmsBrokerRegisterExtensionStaticFieldTest {
14 |
15 | @RegisterExtension
16 | static EmbeddedJmsBroker embeddedJMSBroker = new EmbeddedJmsBroker();
17 |
18 | @EmbeddedJms
19 | private ConnectionFactory connectionFactory;
20 |
21 | @DisplayName("injection on fields works")
22 | @Test
23 | void checkInjectionViaRegisterExtension() {
24 | assertNotNull(connectionFactory);
25 | }
26 | }
--------------------------------------------------------------------------------
/embedded-jms-junit/src/test/java/org/zapodot/junit/jms/EmbeddedJmsRuleMarshalEnabledTest.java:
--------------------------------------------------------------------------------
1 | package org.zapodot.junit.jms;
2 |
3 | import org.junit.Rule;
4 | import org.junit.Test;
5 |
6 | import javax.jms.ConnectionFactory;
7 |
8 | import static org.junit.Assert.assertEquals;
9 |
10 | public class EmbeddedJmsRuleMarshalEnabledTest implements SendReceivable {
11 | private static final String TEST_QUEUE = EmbeddedJmsRuleMarshalEnabledTest.class.getSimpleName();
12 | private static final String TEST_MESSAGE = "Test with wire format marshal";
13 | @Rule
14 | public EmbeddedJmsRule embeddedJmsRule = EmbeddedJmsRule.builder().withMarshalEnabled().withName("named").build();
15 |
16 | @Test
17 | public void sendAndReceiveUsingWireFormat() throws Exception {
18 | final ConnectionFactory connectionFactory = embeddedJmsRule.connectionFactory();
19 | assertEquals(TEST_MESSAGE, sendReceive(connectionFactory, TEST_QUEUE, TEST_MESSAGE));
20 | }
21 | }
--------------------------------------------------------------------------------
/embedded-jms-junit/src/test/java/org/zapodot/junit/jms/impl/EmbeddedJmsRuleImplTest.java:
--------------------------------------------------------------------------------
1 | package org.zapodot.junit.jms.impl;
2 |
3 | import org.junit.Test;
4 |
5 | /**
6 | * @author sondre
7 | */
8 | public class EmbeddedJmsRuleImplTest {
9 |
10 | @Test(expected = IllegalStateException.class)
11 | public void connectionFactory() throws Exception {
12 | final EmbeddedJmsRuleImpl rule = new EmbeddedJmsRuleImpl("predefined", true, false);
13 | rule.connectionFactory();
14 | }
15 |
16 | @Test(expected = IllegalStateException.class)
17 | public void activeMqConnectionFactory() throws Exception {
18 | final EmbeddedJmsRuleImpl rule = new EmbeddedJmsRuleImpl("predefined", true, false);
19 | rule.activeMqConnectionFactory();
20 | }
21 |
22 | @Test(expected = IllegalStateException.class)
23 | public void brokerUri() throws Exception {
24 | final EmbeddedJmsRuleImpl rule = new EmbeddedJmsRuleImpl("predefined", true, false);
25 | rule.brokerUri();
26 | }
27 |
28 | }
--------------------------------------------------------------------------------
/embedded-jms-core/src/main/java/org/zapodot/jms/common/BrokerSettings.java:
--------------------------------------------------------------------------------
1 | package org.zapodot.jms.common;
2 |
3 | import java.io.File;
4 |
5 | /**
6 | * Internal broker configuration holder.
7 | *
8 | * This is part of the internal API and may be changed or removed without prior notice.
9 | */
10 | class BrokerSettings {
11 | private final String name;
12 |
13 | private final boolean marshal;
14 |
15 | private final boolean persistent;
16 |
17 | private final File tempDir;
18 |
19 | BrokerSettings(final String name, final boolean marshal, final boolean persistent, final File tempDir) {
20 | this.name = name;
21 | this.marshal = marshal;
22 | this.persistent = persistent;
23 | this.tempDir = tempDir;
24 | }
25 |
26 | String getName() {
27 | return name;
28 | }
29 |
30 | boolean isMarshal() {
31 | return marshal;
32 | }
33 |
34 | boolean isPersistent() {
35 | return persistent;
36 | }
37 |
38 | File getTempDir() {
39 | return tempDir;
40 | }
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/embedded-jms-junit5/src/test/java/org/zapodot/junit5/jms/EmbeddedJmsDefaultSetupTest.java:
--------------------------------------------------------------------------------
1 | package org.zapodot.junit5.jms;
2 |
3 | import org.apache.activemq.ActiveMQConnectionFactory;
4 | import org.junit.jupiter.api.Test;
5 | import org.junit.jupiter.api.extension.ExtendWith;
6 | import org.zapodot.junit5.jms.annotations.EmbeddedJms;
7 |
8 | import javax.jms.ConnectionFactory;
9 | import java.net.URI;
10 |
11 | import static org.junit.jupiter.api.Assertions.assertEquals;
12 | import static org.junit.jupiter.api.Assertions.assertNotNull;
13 |
14 | @ExtendWith(EmbeddedJmsBroker.class)
15 | class EmbeddedJmsDefaultSetupTest {
16 |
17 | @EmbeddedJms
18 | private ConnectionFactory connectionFactory;
19 |
20 | @EmbeddedJms
21 | private ActiveMQConnectionFactory activeMQConnectionFactory;
22 |
23 | @EmbeddedJms
24 | private URI brokerUri;
25 |
26 |
27 | @Test
28 | void fieldsAreInjected() {
29 | assertNotNull(connectionFactory);
30 | assertNotNull(activeMQConnectionFactory);
31 | assertNotNull(brokerUri);
32 | }
33 |
34 | @Test
35 | void brokerUriGenerated() {
36 | assertEquals("vm://" + getClass().getSimpleName(), brokerUri.toString());
37 | }
38 | }
--------------------------------------------------------------------------------
/embedded-jms-junit5/src/main/java/org/zapodot/junit5/jms/annotations/BrokerConfig.java:
--------------------------------------------------------------------------------
1 | package org.zapodot.junit5.jms.annotations;
2 |
3 | import java.lang.annotation.Documented;
4 | import java.lang.annotation.ElementType;
5 | import java.lang.annotation.Inherited;
6 | import java.lang.annotation.Retention;
7 | import java.lang.annotation.RetentionPolicy;
8 | import java.lang.annotation.Target;
9 |
10 | /**
11 | * Configuration for the Embedded JMS Broker
12 | */
13 | @Target({ElementType.TYPE, ElementType.METHOD})
14 | @Retention(RetentionPolicy.RUNTIME)
15 | @Inherited
16 | @Documented
17 | public @interface BrokerConfig {
18 | /**
19 | * The name of the broker. If not specified it will default to the name of the test method
20 | *
21 | * @return the provided
22 | */
23 | String name() default "";
24 |
25 | /**
26 | * Whether marshalling of messages should be enabled or not (default: false)
27 | *
28 | * @return a boolean string
29 | */
30 | String marshall() default "";
31 |
32 | /**
33 | * Whether message persistence should be enabled or not. Default is false
34 | *
35 | * @return a boolean string
36 | */
37 | String persistence() default "";
38 | }
39 |
--------------------------------------------------------------------------------
/.github/workflows/maven.yml:
--------------------------------------------------------------------------------
1 | name: Build, test and deploy
2 |
3 | on: [push, pull_request]
4 |
5 | jobs:
6 | build:
7 | runs-on: ubuntu-latest
8 | strategy:
9 | matrix:
10 | java: [8, 11]
11 | name: Java ${{ matrix.java }}
12 | env:
13 | CC_TEST_REPORTER_ID: 47b239597faea3da257baa211d7438ff8ba9047319a2c687110f4f8e41bb07f1
14 | steps:
15 | - uses: actions/checkout@v1
16 | - name: Set up Java ${{ matrix.java }}
17 | uses: actions/setup-java@v1
18 | with:
19 | java-version: ${{ matrix.java }}
20 | - name: Build with Maven
21 | run: mvn install -DskipTests=true -Dmaven.javadoc.skip=true -B -V
22 | id: mvnInstall
23 | - name: Run tests
24 | run: mvn test -B
25 | id: mvnTest
26 | - name: Record coverage
27 | run: |
28 | mvn clean cobertura:cobertura coveralls:report
29 | # if: startsWith(matrix.java, 8)
30 | # - name: Deploy to Github packages
31 | # env:
32 | # token: ${{ secrets.GITHUB_TOKEN }}
33 | # packagesUrl: https://maven.pkg.github.com/zapodot
34 | # run: |
35 | # mvn deploy -Dregistry=$packagesUrl -Dtoken=$token
36 | # id: deployGithubPackages
37 | # if: startsWith(matrix.java, 11)
38 |
39 |
--------------------------------------------------------------------------------
/embedded-jms-junit5/src/test/java/org/zapodot/junit5/jms/internal/FieldInjectorTest.java:
--------------------------------------------------------------------------------
1 | package org.zapodot.junit5.jms.internal;
2 |
3 | import org.junit.jupiter.api.Test;
4 |
5 | import java.lang.reflect.Constructor;
6 | import java.lang.reflect.InvocationTargetException;
7 |
8 | import static org.junit.jupiter.api.Assertions.assertFalse;
9 | import static org.junit.jupiter.api.Assertions.assertNotNull;
10 | import static org.junit.jupiter.api.Assertions.assertThrows;
11 |
12 | class FieldInjectorTest {
13 |
14 | @Test
15 | void instantiationNotAllowed() throws NoSuchMethodException {
16 | final Constructor constructor = FieldInjector.class.getDeclaredConstructor();
17 | assertNotNull(constructor);
18 | assertFalse(constructor.isAccessible());
19 | assertThrows(IllegalAccessException.class, constructor::newInstance);
20 | }
21 |
22 | @Test
23 | void instantiateAnyway() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
24 | final Constructor constructor = FieldInjector.class.getDeclaredConstructor();
25 | assertNotNull(constructor);
26 | try {
27 | constructor.setAccessible(true);
28 | assertNotNull(constructor.newInstance());
29 | } finally {
30 | constructor.setAccessible(false);
31 | }
32 | }
33 | }
--------------------------------------------------------------------------------
/embedded-jms-junit5/src/test/java/org/zapodot/junit5/jms/EmbeddedJmsBrokerWithConfigTest.java:
--------------------------------------------------------------------------------
1 | package org.zapodot.junit5.jms;
2 |
3 | import org.junit.jupiter.api.DisplayName;
4 | import org.junit.jupiter.api.Test;
5 | import org.junit.jupiter.api.extension.ExtendWith;
6 | import org.zapodot.junit5.jms.annotations.BrokerConfig;
7 | import org.zapodot.junit5.jms.annotations.EmbeddedJms;
8 |
9 | import java.net.URI;
10 |
11 | import static org.junit.jupiter.api.Assertions.assertEquals;
12 | import static org.junit.jupiter.api.Assertions.assertNotNull;
13 |
14 | @DisplayName("Extension with custom configuration")
15 | @ExtendWith(EmbeddedJmsBroker.class)
16 | @BrokerConfig(name = EmbeddedJmsBrokerWithConfigTest.STUFF_BROKER)
17 | class EmbeddedJmsBrokerWithConfigTest {
18 |
19 | static final String STUFF_BROKER = "StuffBroker";
20 |
21 | private static final String PRETTY_BROKER = "PrettyBroker";
22 |
23 | @EmbeddedJms
24 | private URI uri;
25 |
26 | @DisplayName("class level")
27 | @Test
28 | void configuredUsingBrokerConfig() {
29 | assertNotNull(uri);
30 | assertEquals("vm://" + STUFF_BROKER, uri.toString());
31 | }
32 |
33 | @DisplayName("method level configuration which overrides class level")
34 | @BrokerConfig(name = PRETTY_BROKER)
35 | @Test
36 | void brokerConfigOverriddenOnMethod() {
37 | assertEquals("vm://" + PRETTY_BROKER, uri.toString());
38 | }
39 | }
--------------------------------------------------------------------------------
/embedded-jms-junit/src/main/java/org/zapodot/junit/jms/EmbeddedJmsRuleBuilder.java:
--------------------------------------------------------------------------------
1 | package org.zapodot.junit.jms;
2 |
3 | import org.zapodot.junit.jms.impl.EmbeddedJmsRuleImpl;
4 |
5 | /**
6 | * Builder that allows the setup of the Embedded JMS broker to be tweaked to individual needs
7 | */
8 | public class EmbeddedJmsRuleBuilder {
9 | private String predefinedName;
10 | private boolean marshal = false;
11 | private boolean persistent = false;
12 |
13 | /**
14 | * Explicitly sets the name of the broker. Convenient if more then one broker is configured per test
15 | *
16 | * @param name the name to use for the broker
17 | * @return the same {@link EmbeddedJmsRuleBuilder} with the name property set
18 | */
19 | public EmbeddedJmsRuleBuilder withName(final String name) {
20 | this.predefinedName = name;
21 | return this;
22 | }
23 |
24 | /**
25 | * Enables marshaling, meaning that all commands are sent and received using a {@link org.apache.activemq.wireformat.WireFormat}
26 | *
27 | * @return the same {@link EmbeddedJmsRuleBuilder} with the marshal property set to true
28 | */
29 | public EmbeddedJmsRuleBuilder withMarshalEnabled() {
30 | this.marshal = true;
31 | return this;
32 | }
33 |
34 | public EmbeddedJmsRuleImpl build() {
35 | return new EmbeddedJmsRuleImpl(predefinedName, marshal, persistent);
36 | }
37 | }
--------------------------------------------------------------------------------
/embedded-jms-junit/src/test/java/org/zapodot/junit/jms/SendReceivable.java:
--------------------------------------------------------------------------------
1 | package org.zapodot.junit.jms;
2 |
3 | import javax.jms.ConnectionFactory;
4 | import javax.jms.JMSException;
5 | import java.util.concurrent.*;
6 |
7 | import static org.zapodot.junit.jms.CallablesForSendingAndReceiving.messagesFromTestQueue;
8 | import static org.zapodot.junit.jms.CallablesForSendingAndReceiving.sendMessageToQueue;
9 |
10 | public interface SendReceivable {
11 | default String sendReceive(final ConnectionFactory connectionFactory,
12 | final String queueName,
13 | final String textMessage) throws JMSException {
14 | final ExecutorService executorService = Executors.newFixedThreadPool(2);
15 | try {
16 | final Future messageFuture = executorService.submit(messagesFromTestQueue(connectionFactory,
17 | queueName));
18 | executorService.submit(sendMessageToQueue(connectionFactory, queueName, textMessage));
19 | return messageFuture.get(30L, TimeUnit.SECONDS);
20 | } catch (InterruptedException | ExecutionException | TimeoutException e) {
21 | throw new IllegalStateException("An error occurred during send/receive from JMS", e);
22 | } finally {
23 | executorService.shutdownNow();
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/embedded-jms-junit5/src/main/java/org/zapodot/junit5/jms/internal/InjectableFieldsAccessor.java:
--------------------------------------------------------------------------------
1 | package org.zapodot.junit5.jms.internal;
2 |
3 | import org.apache.activemq.ActiveMQConnectionFactory;
4 | import org.junit.platform.commons.util.AnnotationUtils;
5 | import org.zapodot.junit5.jms.annotations.EmbeddedJms;
6 |
7 | import javax.jms.ConnectionFactory;
8 | import java.lang.reflect.Field;
9 | import java.net.URI;
10 | import java.util.List;
11 |
12 | /**
13 | * InjectableFieldsAccessor - part of the internal API. May be removed, moved or changed without prior deprecation
14 | */
15 | class InjectableFieldsAccessor {
16 |
17 | private InjectableFieldsAccessor() {
18 | }
19 |
20 |
21 | static List findInjectableFields(final Class> type) {
22 | return AnnotationUtils.findAnnotatedFields(type,
23 | EmbeddedJms.class,
24 | field -> ConnectionFactory.class
25 | .isAssignableFrom(field.getType()) ||
26 | ActiveMQConnectionFactory.class
27 | .isAssignableFrom(field.getType()) ||
28 | URI.class
29 | .isAssignableFrom(field.getType()));
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/embedded-jms-junit5/src/test/java/org/zapodot/junit5/jms/internal/BrokerConfigurationTest.java:
--------------------------------------------------------------------------------
1 | package org.zapodot.junit5.jms.internal;
2 |
3 | import org.junit.jupiter.api.Test;
4 |
5 | import static org.junit.jupiter.api.Assertions.assertEquals;
6 | import static org.junit.jupiter.api.Assertions.assertNotEquals;
7 |
8 | class BrokerConfigurationTest {
9 |
10 | @Test
11 | void equalsNull() {
12 | assertNotEquals(BrokerConfigurationBuilder.instance().build(), null);
13 | }
14 |
15 | @Test
16 | void equalsOtherType() {
17 | assertNotEquals(BrokerConfigurationBuilder.instance().build(), "");
18 | }
19 |
20 | @Test
21 | void equalsSame() {
22 | final BrokerConfiguration brokerConfiguration = BrokerConfigurationBuilder.instance().build();
23 | assertEquals(brokerConfiguration, brokerConfiguration);
24 | }
25 |
26 | @Test
27 | void equalsSimilarInstances() {
28 | assertEquals(BrokerConfigurationBuilder.instance().build(),
29 | BrokerConfigurationBuilder.instance().build());
30 | }
31 |
32 | @Test
33 | void testHashCode() {
34 | assertEquals(BrokerConfigurationBuilder.instance().build().hashCode(),
35 | BrokerConfigurationBuilder.instance().build().hashCode());
36 | }
37 |
38 | @Test
39 | void testToString() {
40 | assertEquals(BrokerConfigurationBuilder.instance().build().toString(),
41 | BrokerConfigurationBuilder.instance().build().toString());
42 | }
43 | }
--------------------------------------------------------------------------------
/embedded-jms-junit5/src/test/java/org/zapodot/junit5/jms/EmbeddedJmsBrokerRegisterExtensionFieldTest.java:
--------------------------------------------------------------------------------
1 | package org.zapodot.junit5.jms;
2 |
3 | import org.junit.jupiter.api.DisplayName;
4 | import org.junit.jupiter.api.Test;
5 | import org.junit.jupiter.api.extension.RegisterExtension;
6 | import org.zapodot.junit5.jms.annotations.BrokerConfig;
7 | import org.zapodot.junit5.jms.annotations.EmbeddedJms;
8 |
9 | import javax.jms.ConnectionFactory;
10 | import java.net.URI;
11 |
12 | import static org.junit.jupiter.api.Assertions.assertNotEquals;
13 | import static org.junit.jupiter.api.Assertions.assertNotNull;
14 |
15 | @DisplayName("Injection using @RegisterExtension on field")
16 | @BrokerConfig(name = EmbeddedJmsBrokerRegisterExtensionFieldTest.BROKER_NAME)
17 | class EmbeddedJmsBrokerRegisterExtensionFieldTest {
18 |
19 | static final String BROKER_NAME = "StaticRegisterExtensionBroker";
20 |
21 | @RegisterExtension
22 | final EmbeddedJmsBroker embeddedJMSBroker = new EmbeddedJmsBroker();
23 |
24 | @EmbeddedJms
25 | private ConnectionFactory connectionFactory;
26 |
27 | @EmbeddedJms
28 | private URI brokerUri;
29 |
30 | @DisplayName("injection on fields works")
31 | @Test
32 | void checkInjectionViaRegisterExtension() {
33 | assertNotNull(connectionFactory);
34 | }
35 |
36 | @DisplayName("class level configuration not used")
37 | @Test
38 | void classLevelConfigurationNotUsed() {
39 | assertNotNull(brokerUri);
40 | assertNotEquals("vm://" + BROKER_NAME, brokerUri.toString());
41 | }
42 | }
--------------------------------------------------------------------------------
/embedded-jms-junit5/src/test/java/org/zapodot/junit5/jms/EmbeddedJmsBrokerNestedTest.java:
--------------------------------------------------------------------------------
1 | package org.zapodot.junit5.jms;
2 |
3 | import org.apache.activemq.ActiveMQConnectionFactory;
4 | import org.junit.jupiter.api.DisplayName;
5 | import org.junit.jupiter.api.Nested;
6 | import org.junit.jupiter.api.Test;
7 | import org.junit.jupiter.api.extension.ExtendWith;
8 | import org.zapodot.junit5.jms.annotations.EmbeddedJms;
9 |
10 | import javax.jms.ConnectionFactory;
11 |
12 | import static org.junit.jupiter.api.Assertions.assertNotNull;
13 |
14 | @DisplayName("An embedded JMS ActiveMQ broker")
15 | @ExtendWith(EmbeddedJmsBroker.class)
16 | class EmbeddedJmsBrokerNestedTest {
17 |
18 | @EmbeddedJms
19 | private ConnectionFactory connectionFactory;
20 |
21 | @DisplayName("when javax.jms.ConnectionFactory is injected")
22 | @Test
23 | void injectionWorks() {
24 | assertNotNull(connectionFactory);
25 | }
26 |
27 | @Nested
28 | @DisplayName("in a nested test class")
29 | class WhenNested {
30 |
31 | @EmbeddedJms
32 | private ActiveMQConnectionFactory activeMQConnectionFactory;
33 |
34 | @DisplayName("the injection fields from the outer class is available")
35 | @Test
36 | void injectionWorksForNestedClasses() {
37 | assertNotNull(connectionFactory);
38 | }
39 |
40 | @DisplayName("injection works also for the internal class")
41 | @Test
42 | void injectionIntoFieldInNestedClass() {
43 | assertNotNull(activeMQConnectionFactory);
44 |
45 | }
46 | }
47 | }
--------------------------------------------------------------------------------
/embedded-jms-junit/src/main/java/org/zapodot/junit/jms/EmbeddedJmsRule.java:
--------------------------------------------------------------------------------
1 | package org.zapodot.junit.jms;
2 |
3 | import org.apache.activemq.ActiveMQConnectionFactory;
4 | import org.junit.rules.TestRule;
5 |
6 | import javax.jms.ConnectionFactory;
7 | import java.net.URI;
8 |
9 | /**
10 | * The general contract for the EmbeddedJmsRule.
11 | *
12 | * Example of use:
13 | *
14 | * public class MyTests {
15 | *
16 | * @Rule
17 | * public EmbeddedJmsRule embeddedJmsRule = EmbeddedJmsRule.builder().build();
18 | *
19 | * @Test
20 | * public void testJmsIntegration() {
21 | * final ConnectionFactory connectionFactory = embeddedJmsRule.connectionFactory();
22 | * // Do stuff
23 | * // ...
24 | * }
25 | *
26 | * }
27 | *
28 | */
29 | public interface EmbeddedJmsRule extends TestRule {
30 |
31 | static EmbeddedJmsRuleBuilder builder() {
32 | return new EmbeddedJmsRuleBuilder();
33 | }
34 |
35 | /**
36 | * Creates a ConnectionFactory that connects to the embedded broker
37 | *
38 | * @return a fresh instance of {@link ConnectionFactory}
39 | */
40 | ConnectionFactory connectionFactory();
41 |
42 | /**
43 | * Creates a {@link ActiveMQConnectionFactory} for those who needs access to the AMQ specific interface
44 | *
45 | * @return a fresh instance of {@link ActiveMQConnectionFactory}
46 | */
47 | ActiveMQConnectionFactory activeMqConnectionFactory();
48 |
49 | /**
50 | * An URI that may be used to connect to the embedded broker
51 | *
52 | * @return a {@link URI}
53 | */
54 | URI brokerUri();
55 | }
56 |
--------------------------------------------------------------------------------
/embedded-jms-junit5/src/test/java/org/zapodot/junit5/jms/EmbeddedJmsBrokerWithParameterTest.java:
--------------------------------------------------------------------------------
1 | package org.zapodot.junit5.jms;
2 |
3 | import org.apache.activemq.ActiveMQConnectionFactory;
4 | import org.junit.jupiter.api.DisplayName;
5 | import org.junit.jupiter.api.Test;
6 | import org.junit.jupiter.api.extension.ExtendWith;
7 | import org.zapodot.junit5.jms.annotations.EmbeddedJms;
8 |
9 | import javax.jms.ConnectionFactory;
10 | import java.net.URI;
11 | import java.sql.Connection;
12 |
13 | import static org.junit.jupiter.api.Assertions.assertNotNull;
14 | import static org.junit.jupiter.api.Assertions.assertNull;
15 |
16 | @ExtendWith(EmbeddedJmsBroker.class)
17 | @DisplayName("Inject parameter to test")
18 | class EmbeddedJmsBrokerWithParameterTest {
19 |
20 | @DisplayName("supports parameter type javax.jms.ConnectionFactory")
21 | @Test
22 | void connectionFactoryParameter(@EmbeddedJms ConnectionFactory connectionFactory) {
23 | assertNotNull(connectionFactory);
24 | }
25 |
26 | @DisplayName("supports parameter type org.apache.activemq.ActiveMQConnectionFactory")
27 | @Test
28 | void activeMQConnectionFactoryParameter(@EmbeddedJms ActiveMQConnectionFactory activeMQConnectionFactory) {
29 | assertNotNull(activeMQConnectionFactory);
30 | }
31 |
32 | @DisplayName("supports parameter type java.net.URI")
33 | @Test
34 | void brokerUriParameter(@EmbeddedJms URI brokerURI) {
35 | assertNotNull(brokerURI);
36 | }
37 |
38 | @DisplayName("not supported parameter type java.sql.Connection")
39 | @Test
40 | void otherType(@EmbeddedJms Connection connection) {
41 | // If the parameter is not supported it will neither be injected nor fail
42 | assertNull(connection);
43 | }
44 | }
--------------------------------------------------------------------------------
/old.travis.yml:
--------------------------------------------------------------------------------
1 | sudo: false
2 | language: java
3 | env:
4 | global:
5 | - secure: 3YqqNwvMtdT4vOKa224ofn596Dcvks35cUrjInYaBN/y8Oq7TmNQn1thTwuZ3ZrS774xsOJ2inAbZ3aa9OJZEeV31oWjaupShNMk+K8gZ+Aq0LGcGgdcB1iwAOdsslDjzZlhEfPwdCUt3w01D2YRbqjhQiJThP5dZHMf7NfGyD49h04PYlbuDpmzj3DampQUCMmCZ1H2pro7O7khySHkxmZIgy2hUAVzoAAgpQqimE5jo+kMqZJtAbz7eAlKWYUy5kwFjyA6Nt46PuMUAAOYRLmZnskyuimsJUSbA0hbTLd9nYqM2cNhtq9fzslD2mnotTmeXbcXUyTEIgWofXSzDf9N1DOkGNz8G/GFvudv5+b0oyUtFW1+QOldjY8zoI0w6YJT+r6NcfXItK8/ENl0zCLlushldK0+GATE4ipZh6BiAUgP7jRcilg3DAklY3BZ4zVIJcxO7szslNkAyykCOAzna8GsFsOYYWoaBXqEXY2QQnTBZxmpYpiU4aYYSxTTnf6ursSU/YpQYSjS93b3WnKqpoZWmJbXIdARCjHt+KXWrU4oBL1HV6OSdnFmPhR3zD6w6xTeeYaCX8dnthwIDouoN18QTDZy860fPC0BnfxEZSrq2avN0ib1vmeljeHgawvFF3NBhNbtMRB78Y1Fyy4Wn97Ia0H2VHAi2Uem3GE=
6 | - secure: Zm5ZJNOfg+SSSF+9ZcX70Mm0soWfsV4kJbHGDyIqxnSn2SKSpaT91lR7yT15fccC2wihtWUCU8mpLeon/tJ9L4etnVc/VjF9bXd73msk987io5h7q08XbEp+H+JL5GFdMp/9iPvFYyi+tshUZBLjaeSd6MKwJ90jtcrS75t4iqI0FEhMtwHCUALLpkjubYWZJfGhIB2DviRwyHDqXWZlgQYJKzdJDZ5XKpbz06qz+dO0mnQelou3988bDY64yNahHoS5ZRiQu4748DKCWzsaJwD5HsWE1taGVjFC933AuKWW8F2obnNXTlLixzDaIidZ+GyBkC8dRIBAVZy6e1vgm0A4H6k7Z6qg4GYvfTObLK/xi3CfepSGdnXBHRGRVePpC6bMqngYh+ljsyvwmKxMXshtJD6ITltvGNqM4SoyrrSChhp6VSNqTC32HjIp9I/O7wUbhUlmKntaZg7cZ7g2UztzPGHgJCUp3SFKuItAmAOw4oA0QuWMOBf5BcJjtPV85fgocSpucG/JGSYs06IvkSjpphhP0Gxot76eMKdvcWroBfebjCun8SEEEuSVztzi+Q2zr2cWwJklBCArpeFTNo99z1lzrMaOavmXeZsEYuiGYxHE81jJUzXZQ7g5uYgJ+kiAggkrs5tJSb40ectu9nNrdS3VYZsQR+NWVdj0kxE=
7 | jdk:
8 | - openjdk8
9 | - openjdk11
10 | after_success:
11 | - mvn cobertura:cobertura coveralls:report
12 | - echo "sonatype-nexus-snapshots\${env.REPO_USER}\${env.REPO_KEY}" > ~/settings.xml
13 | - mvn deploy --settings ~/settings.xml
14 |
--------------------------------------------------------------------------------
/embedded-jms-junit/src/test/java/org/zapodot/junit/jms/EmbeddedJmsRuleSpringJmsTest.java:
--------------------------------------------------------------------------------
1 | package org.zapodot.junit.jms;
2 |
3 | import org.hamcrest.CoreMatchers;
4 | import org.junit.Rule;
5 | import org.junit.Test;
6 | import org.springframework.jms.core.JmsOperations;
7 | import org.springframework.jms.core.JmsTemplate;
8 |
9 | import javax.jms.JMSException;
10 | import javax.jms.TextMessage;
11 | import java.util.concurrent.*;
12 |
13 | import static org.junit.Assert.assertThat;
14 |
15 |
16 | public class EmbeddedJmsRuleSpringJmsTest {
17 |
18 | private static final String TEST_MESSAGE = "Test message";
19 | @Rule
20 | public EmbeddedJmsRule embeddedJmsRule = EmbeddedJmsRule.builder().build();
21 |
22 | @Test
23 | public void jmsOperation() throws Exception {
24 |
25 | final JmsOperations jmsOperations = createSpringJmsOperation();
26 | final ExecutorService executorService = Executors.newFixedThreadPool(1);
27 | try {
28 | final Future messageFuture = executorService.submit(receiveMessage(jmsOperations));
29 | jmsOperations.convertAndSend(TEST_MESSAGE);
30 | final String message = messageFuture.get(30L, TimeUnit.SECONDS);
31 | assertThat(message, CoreMatchers.equalTo(TEST_MESSAGE));
32 | } finally {
33 | executorService.shutdownNow();
34 | }
35 | }
36 |
37 | private JmsOperations createSpringJmsOperation() {
38 | final JmsTemplate jmsTemplate = new JmsTemplate(embeddedJmsRule.connectionFactory());
39 | jmsTemplate.setDefaultDestinationName(getClass().getSimpleName());
40 | jmsTemplate.setReceiveTimeout(TimeUnit.SECONDS.toMillis(10L));
41 | jmsTemplate.afterPropertiesSet();
42 | return jmsTemplate;
43 | }
44 |
45 | private Callable receiveMessage(final JmsOperations jmsOperations) {
46 | return () -> {
47 |
48 | final TextMessage textMessage = (TextMessage) jmsOperations.receive();
49 | try {
50 | return textMessage.getText();
51 | } catch (JMSException e) {
52 | throw new IllegalStateException("Could not receive message", e);
53 | }
54 | };
55 | }
56 | }
--------------------------------------------------------------------------------
/embedded-jms-junit5/src/main/java/org/zapodot/junit5/jms/internal/BrokerConfiguration.java:
--------------------------------------------------------------------------------
1 | package org.zapodot.junit5.jms.internal;
2 |
3 | import java.util.Objects;
4 |
5 | /**
6 | * BrokerConfiguration - part of the internal API. May be removed, moved or changed without prior deprecation
7 | */
8 | public class BrokerConfiguration {
9 |
10 | public static final BrokerConfiguration DEFAULT = BrokerConfigurationBuilder.instance().withMarshal(false)
11 | .withPersistenceEnabled(false).build();
12 |
13 | private final String name;
14 |
15 | private final Boolean marshal;
16 |
17 | private final Boolean persistenceEnabled;
18 |
19 | public BrokerConfiguration(final String name, final Boolean marshal, final Boolean persistenceEnabled) {
20 | this.name = name;
21 | this.marshal = marshal;
22 | this.persistenceEnabled = persistenceEnabled;
23 | }
24 |
25 | public String getName() {
26 | return name;
27 | }
28 |
29 | public Boolean getMarshal() {
30 | return marshal;
31 | }
32 |
33 | public Boolean getPersistenceEnabled() {
34 | return persistenceEnabled;
35 | }
36 |
37 | @Override
38 | public boolean equals(final Object o) {
39 | if (this == o) {
40 | return true;
41 | }
42 | if (o == null || getClass() != o.getClass()) {
43 | return false;
44 | }
45 | final BrokerConfiguration that = (BrokerConfiguration) o;
46 | return Objects.equals(name, that.name) &&
47 | Objects.equals(marshal, that.marshal) &&
48 | Objects.equals(persistenceEnabled, that.persistenceEnabled);
49 | }
50 |
51 | @Override
52 | public int hashCode() {
53 | return Objects.hash(name, marshal, persistenceEnabled);
54 | }
55 |
56 | @Override
57 | public String toString() {
58 | final StringBuilder sb = new StringBuilder("BrokerConfiguration{");
59 | sb.append("name='").append(name).append('\'');
60 | sb.append(", marshal=").append(marshal);
61 | sb.append(", persistenceEnabled=").append(persistenceEnabled);
62 | sb.append('}');
63 | return sb.toString();
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/embedded-jms-junit5/src/test/java/org/zapodot/junit5/jms/EmbeddedJmsBrokerPersistenceTest.java:
--------------------------------------------------------------------------------
1 | package org.zapodot.junit5.jms;
2 |
3 | import org.junit.jupiter.api.Test;
4 | import org.junit.jupiter.api.extension.ExtendWith;
5 | import org.zapodot.junit5.jms.annotations.BrokerConfig;
6 | import org.zapodot.junit5.jms.annotations.EmbeddedJms;
7 |
8 | import javax.jms.Connection;
9 | import javax.jms.ConnectionFactory;
10 | import javax.jms.JMSException;
11 | import javax.jms.MessageProducer;
12 | import javax.jms.Queue;
13 | import javax.jms.QueueBrowser;
14 | import javax.jms.Session;
15 | import javax.jms.TextMessage;
16 |
17 | import static org.junit.jupiter.api.Assertions.assertEquals;
18 | import static org.junit.jupiter.api.Assertions.assertNotNull;
19 |
20 | @ExtendWith(EmbeddedJmsBroker.class)
21 | @BrokerConfig(persistence = "true")
22 | class EmbeddedJmsBrokerPersistenceTest {
23 |
24 | private static final String PERSISTED_MESSAGE = "PersistedMessage";
25 |
26 | @EmbeddedJms
27 | private ConnectionFactory connectionFactory;
28 |
29 | @Test
30 | void name() throws JMSException {
31 | assertNotNull(connectionFactory);
32 |
33 | Connection connection = null;
34 | Session session = null;
35 | try {
36 | connection = connectionFactory.createConnection();
37 | connection.start();
38 | session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
39 |
40 | final Queue queue = session.createQueue("test.queue");
41 | final MessageProducer producer = session.createProducer(queue);
42 | final QueueBrowser queueBrowser = session.createBrowser(queue);
43 | try {
44 | final TextMessage textMessage = session.createTextMessage(PERSISTED_MESSAGE);
45 |
46 | producer.send(textMessage);
47 | assertEquals(PERSISTED_MESSAGE, ((TextMessage) queueBrowser.getEnumeration().nextElement()).getText());
48 |
49 | } finally {
50 | queueBrowser.close();
51 | producer.close();
52 | }
53 | } finally {
54 | if (session != null) {
55 | session.close();
56 | }
57 | if (connection != null) {
58 | connection.close();
59 | }
60 | }
61 | }
62 | }
--------------------------------------------------------------------------------
/embedded-jms-junit/src/test/java/org/zapodot/junit/jms/EmbeddedJmsRuleCamelTest.java:
--------------------------------------------------------------------------------
1 | package org.zapodot.junit.jms;
2 |
3 | import org.apache.activemq.camel.component.ActiveMQComponent;
4 | import org.apache.camel.LoggingLevel;
5 | import org.apache.camel.RoutesBuilder;
6 | import org.apache.camel.builder.RouteBuilder;
7 | import org.apache.camel.impl.JndiRegistry;
8 | import org.apache.camel.test.junit4.CamelTestSupport;
9 | import org.junit.Rule;
10 | import org.junit.Test;
11 |
12 | public class EmbeddedJmsRuleCamelTest extends CamelTestSupport {
13 |
14 | private static final String MOCK_ENDPOINT_URI = "mock:output";
15 | private static final String JMS_DESTINATION_URI = "activemq:queue:inputQueue";
16 | @Rule
17 | public EmbeddedJmsRule embeddedJmsRule = EmbeddedJmsRule.builder().build();
18 |
19 | private ActiveMQComponent activeMQComponent() {
20 | final ActiveMQComponent activeMQComponent = new ActiveMQComponent();
21 | activeMQComponent.setConnectionFactory(embeddedJmsRule.connectionFactory());
22 | return activeMQComponent;
23 | }
24 |
25 | @Override
26 | protected JndiRegistry createRegistry() throws Exception {
27 | final JndiRegistry registry = super.createRegistry();
28 | registry.bind("activemq", activeMQComponent());
29 | return registry;
30 | }
31 |
32 | @Override
33 | protected RoutesBuilder createRouteBuilder() throws Exception {
34 | return new RouteBuilder() {
35 | @Override
36 | public void configure() throws Exception {
37 | from(JMS_DESTINATION_URI)
38 | .id("testRoute")
39 | .convertBodyTo(String.class)
40 | .log(LoggingLevel.INFO, "Received message ${id} with body \"${body}\"")
41 | .to(MOCK_ENDPOINT_URI);
42 |
43 | }
44 | };
45 | }
46 |
47 | @Override
48 | public boolean isUseRouteBuilder() {
49 | return true;
50 | }
51 |
52 | @Override
53 | protected int getShutdownTimeout() {
54 | return 5;
55 | }
56 |
57 | @Test
58 | public void sendAndReceiveUsingCamel() throws Exception {
59 |
60 | final String messageBody = "Hello Camel!";
61 | getMockEndpoint(MOCK_ENDPOINT_URI).expectedBodiesReceived(messageBody);
62 |
63 | template.sendBody(JMS_DESTINATION_URI, messageBody);
64 |
65 | assertMockEndpointsSatisfied();
66 |
67 | }
68 | }
--------------------------------------------------------------------------------
/embedded-jms-junit/src/test/java/org/zapodot/junit/jms/CallablesForSendingAndReceiving.java:
--------------------------------------------------------------------------------
1 | package org.zapodot.junit.jms;
2 |
3 | import javax.jms.*;
4 | import java.lang.IllegalStateException;
5 | import java.util.concurrent.Callable;
6 | import java.util.concurrent.TimeUnit;
7 |
8 | public interface CallablesForSendingAndReceiving {
9 |
10 | static Callable messagesFromTestQueue(final ConnectionFactory connectionFactory, final String queueName) {
11 | return () -> {
12 | final Connection connection = connectionFactory.createConnection();
13 | connection.start();
14 | try {
15 | final Session consumerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
16 | try {
17 | final Queue queue = consumerSession.createQueue(queueName);
18 | final MessageConsumer messageConsumer = consumerSession.createConsumer(queue);
19 | final Message message = messageConsumer.receive(TimeUnit.SECONDS.toMillis(30L));
20 | if (message instanceof TextMessage) {
21 | return ((TextMessage) message).getText();
22 | } else {
23 | throw new IllegalStateException("Illegal message type received");
24 | }
25 | } finally {
26 | consumerSession.close();
27 | }
28 | } finally {
29 | connection.close();
30 | }
31 | };
32 | }
33 |
34 | static Callable sendMessageToQueue(final ConnectionFactory connectionFactory,
35 | final String testQueueName,
36 | final String testMessage) throws JMSException {
37 | return () -> {
38 | final Connection connection = connectionFactory.createConnection();
39 | connection.start();
40 |
41 | final Session senderSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
42 |
43 | final Queue senderQueue = senderSession.createQueue(testQueueName);
44 | final MessageProducer messageProducer = senderSession.createProducer(senderQueue);
45 | final TextMessage textMessage = senderSession.createTextMessage(testMessage);
46 | messageProducer.send(textMessage);
47 | messageProducer.close();
48 | senderSession.close();
49 | connection.close();
50 | return Boolean.TRUE;
51 | };
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/embedded-jms-junit5/src/test/java/org/zapodot/junit5/jms/EmbeddedJmsBrokerRequestReplySpringTest.java:
--------------------------------------------------------------------------------
1 | package org.zapodot.junit5.jms;
2 |
3 | import org.junit.jupiter.api.DisplayName;
4 | import org.junit.jupiter.api.Test;
5 | import org.junit.jupiter.api.extension.ExtendWith;
6 | import org.springframework.jms.core.JmsOperations;
7 | import org.springframework.jms.core.JmsTemplate;
8 | import org.zapodot.junit5.jms.annotations.EmbeddedJms;
9 |
10 | import javax.jms.ConnectionFactory;
11 | import javax.jms.JMSException;
12 | import javax.jms.TextMessage;
13 | import java.util.concurrent.Callable;
14 | import java.util.concurrent.ExecutorService;
15 | import java.util.concurrent.Executors;
16 | import java.util.concurrent.Future;
17 | import java.util.concurrent.TimeUnit;
18 |
19 | import static org.junit.jupiter.api.Assertions.assertEquals;
20 |
21 | @SuppressWarnings("squid:S00112")
22 | @ExtendWith(EmbeddedJmsBroker.class)
23 | class EmbeddedJmsBrokerRequestReplySpringTest {
24 |
25 | private static final String TEST_MESSAGE = "Test message";
26 |
27 | private static final String DESTINATION = "queue:destination";
28 |
29 | @EmbeddedJms
30 | private ConnectionFactory connectionFactory;
31 |
32 | @DisplayName("Request/reply using Spring JMS Template")
33 | @Test
34 | void requestReply() throws Exception {
35 | final JmsOperations jmsOperations = createJmsTemplate();
36 | final ExecutorService executorService = Executors.newFixedThreadPool(1);
37 | try {
38 | final Future messageFuture = executorService.submit(receiveMessage(jmsOperations));
39 | jmsOperations.convertAndSend(DESTINATION, TEST_MESSAGE);
40 | final String message = messageFuture.get(30L, TimeUnit.SECONDS);
41 | assertEquals(TEST_MESSAGE, message);
42 | } finally {
43 | executorService.shutdownNow();
44 | }
45 | }
46 |
47 | private JmsOperations createJmsTemplate() {
48 | final JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
49 | jmsTemplate.afterPropertiesSet();
50 | return jmsTemplate;
51 | }
52 |
53 | private Callable receiveMessage(final JmsOperations jmsOperations) {
54 | return () -> {
55 |
56 | final TextMessage textMessage = (TextMessage) jmsOperations.receive(DESTINATION);
57 | try {
58 | return textMessage.getText();
59 | } catch (JMSException e) {
60 | throw new IllegalStateException("Could not receive message", e);
61 | }
62 | };
63 | }
64 | }
--------------------------------------------------------------------------------
/embedded-jms-core/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | embedded-jms-parent
7 | org.zapodot
8 | 0.3-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | embedded-jms-core
13 | Embedded JMS Core
14 |
15 |
16 | org.apache.activemq
17 | activemq-broker
18 |
19 |
20 | com.google.guava
21 | guava
22 |
23 |
24 | org.slf4j
25 | slf4j-api
26 |
27 |
28 | org.slf4j
29 | slf4j-simple
30 | test
31 |
32 |
33 | org.mockito
34 | mockito-junit-jupiter
35 | ${mockito.version}
36 | test
37 |
38 |
39 | org.junit.jupiter
40 | junit-jupiter-api
41 | test
42 |
43 |
44 | org.junit.jupiter
45 | junit-jupiter-engine
46 | test
47 |
48 |
49 |
50 |
51 |
52 | maven-enforcer-plugin
53 |
54 |
55 | maven-compiler-plugin
56 |
57 |
58 | maven-surefire-plugin
59 |
60 |
61 | maven-source-plugin
62 |
63 |
64 | maven-javadoc-plugin
65 |
66 |
67 |
68 |
--------------------------------------------------------------------------------
/embedded-jms-junit/src/test/java/org/zapodot/junit/jms/EmbeddedJmsRuleStandardSetupTest.java:
--------------------------------------------------------------------------------
1 | package org.zapodot.junit.jms;
2 |
3 | import org.apache.activemq.ActiveMQConnectionFactory;
4 | import org.junit.Rule;
5 | import org.junit.Test;
6 |
7 | import javax.jms.*;
8 |
9 | import static org.hamcrest.CoreMatchers.equalTo;
10 | import static org.hamcrest.CoreMatchers.notNullValue;
11 | import static org.junit.Assert.assertThat;
12 |
13 | public class EmbeddedJmsRuleStandardSetupTest implements SendReceivable {
14 |
15 | private static final String TEST_QUEUE = "test.queue";
16 | private static final String TEST_MESSAGE = "Test message";
17 | @Rule
18 | public EmbeddedJmsRule embeddedJmsRule = EmbeddedJmsRule.builder().build();
19 |
20 | @Test
21 | public void connectionFactory() throws Exception {
22 |
23 | final ConnectionFactory connectionFactory = embeddedJmsRule.connectionFactory();
24 | assertThat(connectionFactory, notNullValue(ConnectionFactory.class));
25 | }
26 |
27 | @Test
28 | public void connectAndSendAndReceive() throws Exception {
29 | final ConnectionFactory connectionFactory = embeddedJmsRule.connectionFactory();
30 | assertThat(sendReceive(connectionFactory, TEST_QUEUE, TEST_MESSAGE), equalTo(TEST_MESSAGE));
31 |
32 | }
33 |
34 | @Test
35 | public void usingUri() throws Exception {
36 | final ActiveMQConnectionFactory mqConnectionFactory = new ActiveMQConnectionFactory(embeddedJmsRule.brokerUri());
37 | final Connection connection = mqConnectionFactory.createConnection();
38 | connection.start();
39 | try {
40 | final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
41 | try {
42 | final Queue queue = session.createQueue(TEST_QUEUE);
43 |
44 | final MessageProducer producer = session.createProducer(queue);
45 | final QueueBrowser queueBrowser = session.createBrowser(queue);
46 | try {
47 |
48 | final String text = "Fire and forget";
49 | final TextMessage textMessage = session.createTextMessage(text);
50 | producer.send(textMessage);
51 | assertThat(((TextMessage) queueBrowser.getEnumeration().nextElement()).getText(), equalTo(text));
52 | } finally {
53 | queueBrowser.close();
54 | producer.close();
55 | }
56 |
57 | } finally {
58 | session.close();
59 | }
60 | } finally {
61 | connection.close();
62 | }
63 | }
64 | }
--------------------------------------------------------------------------------
/embedded-jms-core/src/test/java/org/zapodot/jms/common/TemporaryDirectory.java:
--------------------------------------------------------------------------------
1 | package org.zapodot.jms.common;
2 |
3 | import com.google.common.io.Files;
4 | import com.google.common.io.MoreFiles;
5 | import com.google.common.io.RecursiveDeleteOption;
6 | import org.junit.jupiter.api.extension.AfterEachCallback;
7 | import org.junit.jupiter.api.extension.ExtensionContext;
8 | import org.junit.jupiter.api.extension.ParameterContext;
9 | import org.junit.jupiter.api.extension.ParameterResolutionException;
10 | import org.junit.jupiter.api.extension.ParameterResolver;
11 |
12 | import java.io.File;
13 | import java.lang.annotation.Documented;
14 | import java.lang.annotation.ElementType;
15 | import java.lang.annotation.Retention;
16 | import java.lang.annotation.RetentionPolicy;
17 | import java.lang.annotation.Target;
18 |
19 | public class TemporaryDirectory implements ParameterResolver, AfterEachCallback {
20 |
21 | private static final ExtensionContext.Namespace STORE = ExtensionContext.Namespace.create(TemporaryDirectory.class);
22 |
23 | private static final String KEY = "tempDir";
24 |
25 | @Target(ElementType.PARAMETER)
26 | @Retention(RetentionPolicy.RUNTIME)
27 | @Documented
28 | public @interface TempDir {
29 | }
30 |
31 | @Override
32 | public void afterEach(final ExtensionContext context) throws Exception {
33 | final File tempDir = context.getStore(STORE).remove(KEY, File.class);
34 | if (tempDir != null && tempDir.isDirectory()) {
35 | MoreFiles.deleteRecursively(tempDir.toPath(), RecursiveDeleteOption.ALLOW_INSECURE);
36 | }
37 | }
38 |
39 | @Override
40 | public boolean supportsParameter(final ParameterContext parameterContext,
41 | final ExtensionContext extensionContext) throws ParameterResolutionException {
42 |
43 | return parameterContext.isAnnotated(TempDir.class) && parameterContext.getParameter().getType()
44 | .isAssignableFrom(
45 | File.class);
46 | }
47 |
48 | @Override
49 | public Object resolveParameter(final ParameterContext parameterContext,
50 | final ExtensionContext extensionContext) throws ParameterResolutionException {
51 | return extensionContext
52 | .getStore(STORE).getOrComputeIfAbsent(
53 | KEY, key -> createTemporaryDir(), File.class);
54 | }
55 |
56 | private File createTemporaryDir() {
57 | return Files.createTempDir();
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/embedded-jms-core/src/test/java/org/zapodot/jms/common/EmbeddedJMSBrokerHolderTest.java:
--------------------------------------------------------------------------------
1 | package org.zapodot.jms.common;
2 |
3 | import org.apache.activemq.broker.BrokerService;
4 | import org.junit.jupiter.api.DisplayName;
5 | import org.junit.jupiter.api.Test;
6 |
7 | import static org.junit.jupiter.api.Assertions.assertFalse;
8 | import static org.junit.jupiter.api.Assertions.assertNotNull;
9 | import static org.junit.jupiter.api.Assertions.assertThrows;
10 | import static org.junit.jupiter.api.Assertions.assertTrue;
11 |
12 | @SuppressWarnings("squid:S00112")
13 | class EmbeddedJMSBrokerHolderTest {
14 |
15 | @DisplayName("Create EmbeddedJMSBrokerHolder instance")
16 | @Test
17 | void create() throws Exception {
18 | try (final EmbeddedJMSBrokerHolder embeddedJmsBrokerHolder = EmbeddedJMSBrokerHolder
19 | .create("name", false, false)) {
20 | assertNotNull(embeddedJmsBrokerHolder.getBrokerService());
21 | assertFalse(embeddedJmsBrokerHolder.getBrokerService().isStarted());
22 | }
23 | }
24 |
25 | @DisplayName("Creating a broker using an illegal URI as name should fail")
26 | @Test
27 | void createFails() {
28 | assertThrows(IllegalStateException.class, () -> EmbeddedJMSBrokerHolder
29 | .create("\\\\\\", false, false));
30 | }
31 |
32 | @DisplayName("Creates two brokers with the same name should cause the second one to fail")
33 | @Test
34 | void createDouble() {
35 |
36 | final String name = "name";
37 | assertThrows(IllegalStateException.class, () -> {
38 | try (EmbeddedJMSBrokerHolder broker1 = EmbeddedJMSBrokerHolder.create(name, false, false)) {
39 | broker1.start();
40 | try (EmbeddedJMSBrokerHolder broker2 = EmbeddedJMSBrokerHolder.create(name, false, false)) {
41 | broker2.start();
42 | }
43 | }
44 |
45 | });
46 |
47 | }
48 |
49 | @DisplayName("Create EmbeddedJMSBrokerHolder instance, start it and then stop it again")
50 | @Test
51 | @SuppressWarnings("squid:S4087")
52 | void startStop() throws Exception {
53 | try (final EmbeddedJMSBrokerHolder embeddedJmsBrokerHolder = EmbeddedJMSBrokerHolder
54 | .create("name", false, false)) {
55 | final BrokerService brokerService = embeddedJmsBrokerHolder.getBrokerService();
56 | assertNotNull(brokerService);
57 | embeddedJmsBrokerHolder.start();
58 | assertTrue(brokerService.isStarted());
59 | embeddedJmsBrokerHolder.close();
60 | assertTrue(embeddedJmsBrokerHolder.getBrokerService().isStopped());
61 | }
62 | }
63 | }
--------------------------------------------------------------------------------
/embedded-jms-junit5/src/main/java/org/zapodot/junit5/jms/internal/BrokerConfigurationBuilder.java:
--------------------------------------------------------------------------------
1 | package org.zapodot.junit5.jms.internal;
2 |
3 | import com.google.common.base.Strings;
4 | import org.zapodot.junit5.jms.annotations.BrokerConfig;
5 |
6 | import java.util.Optional;
7 |
8 | /**
9 | * BrokerConfigurationBuilder - part of the internal API. May be removed, moved or changed without prior deprecation
10 | */
11 | public final class BrokerConfigurationBuilder {
12 | private String name;
13 |
14 | private Boolean marshal;
15 |
16 | private Boolean persistenceEnabled;
17 |
18 | private BrokerConfigurationBuilder() {
19 | }
20 |
21 | public static BrokerConfigurationBuilder instance() {
22 | return new BrokerConfigurationBuilder();
23 | }
24 |
25 | public static BrokerConfigurationBuilder fromInstance(final BrokerConfiguration brokerConfiguration) {
26 | return Optional.ofNullable(brokerConfiguration)
27 | .map(b -> instance()
28 | .withName(b.getName())
29 | .withPersistenceEnabled(b.getPersistenceEnabled())
30 | .withMarshal(b.getMarshal()))
31 | .orElse(BrokerConfigurationBuilder.instance());
32 | }
33 |
34 | public static BrokerConfigurationBuilder fromBrokerConfigAnnotation(BrokerConfig brokerConfig) {
35 | return instance()
36 | .withName(Strings.emptyToNull(brokerConfig.name()))
37 | .withMarshal(convertFromString(brokerConfig.marshall()))
38 | .withPersistenceEnabled(convertFromString(brokerConfig.persistence()));
39 | }
40 |
41 | public BrokerConfigurationBuilder withName(String name) {
42 | this.name = name;
43 | return this;
44 | }
45 |
46 | public BrokerConfigurationBuilder withMarshal(Boolean marshal) {
47 | this.marshal = marshal;
48 | return this;
49 | }
50 |
51 | public BrokerConfigurationBuilder withPersistenceEnabled(Boolean persistenceEnabled) {
52 | this.persistenceEnabled = persistenceEnabled;
53 | return this;
54 | }
55 |
56 | public BrokerConfigurationBuilder mergeWithBrokerConfiguration(final BrokerConfiguration brokerConfiguration) {
57 | Optional.ofNullable(brokerConfiguration)
58 | .ifPresent(b -> {
59 | withName(b.getName());
60 | withMarshal(b.getMarshal());
61 | withPersistenceEnabled(b.getPersistenceEnabled());
62 | });
63 | return this;
64 | }
65 |
66 | private static Boolean convertFromString(final String value) {
67 | return Optional.ofNullable(Strings.emptyToNull(value)).map(Boolean::valueOf).orElse(null);
68 | }
69 |
70 | public BrokerConfiguration build() {
71 | return new BrokerConfiguration(name, marshal, persistenceEnabled);
72 | }
73 |
74 | }
75 |
--------------------------------------------------------------------------------
/embedded-jms-junit5/src/main/java/org/zapodot/junit5/jms/internal/FieldInjector.java:
--------------------------------------------------------------------------------
1 | package org.zapodot.junit5.jms.internal;
2 |
3 | import org.apache.activemq.ActiveMQConnectionFactory;
4 | import org.junit.platform.commons.util.ReflectionUtils;
5 | import org.slf4j.Logger;
6 | import org.slf4j.LoggerFactory;
7 | import org.zapodot.jms.common.EmbeddedJMSBrokerHolder;
8 |
9 | import javax.jms.ConnectionFactory;
10 | import java.lang.reflect.Field;
11 | import java.net.URI;
12 | import java.util.Optional;
13 |
14 | /**
15 | * FieldInjector - part of the internal API. May be removed, moved or changed without prior deprecation
16 | */
17 | public class FieldInjector {
18 |
19 | private static final Logger LOGGER = LoggerFactory.getLogger(FieldInjector.class);
20 |
21 | private FieldInjector() {
22 | // Constructor added to avoid instantiation
23 | }
24 |
25 | public static void injectToInstance(final Object instance, final EmbeddedJMSBrokerHolder embeddedJMSBrokerHolder) {
26 | if (instance.getClass().isMemberClass()) {
27 | tryToFindOuterInstance(instance)
28 | .ifPresent(i -> injectToInstance(i, embeddedJMSBrokerHolder));
29 | }
30 | InjectableFieldsAccessor.findInjectableFields(instance.getClass())
31 | .stream()
32 | .forEach(field -> injectConnectionFactoryOrURI(instance,
33 | field,
34 | embeddedJMSBrokerHolder));
35 | }
36 |
37 | private static Optional