Skip to content

Commit 35972f1

Browse files
committed
Adding unit tests
1 parent 5ef4363 commit 35972f1

File tree

8 files changed

+258
-33
lines changed

8 files changed

+258
-33
lines changed

publish-subsribe/checkstyle.xml

Lines changed: 0 additions & 14 deletions
This file was deleted.

publish-subsribe/pom.xml

4.31 KB
Binary file not shown.

publish-subsribe/src/main/java/com/hungrydev399/publishsubscribe/jms/JmsUtil.java

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,39 @@ public final class JmsUtil {
1919
private static Connection defaultConnection;
2020
private static BrokerService broker;
2121
private static Map<String, Connection> clientConnections = new ConcurrentHashMap<>();
22-
23-
static {
24-
try {
25-
// Start embedded broker for self-contained messaging
26-
broker = new BrokerService();
27-
broker.addConnector(BROKER_URL);
28-
broker.setPersistent(false); // Messages won't survive broker restart
29-
broker.start();
30-
31-
// Default connection for non-durable subscribers
32-
factory = new ActiveMQConnectionFactory(BROKER_URL);
33-
defaultConnection = factory.createConnection();
34-
defaultConnection.start();
35-
} catch (Exception e) {
36-
System.err.println("Failed to initialize JMS: " + e.getMessage());
37-
throw new RuntimeException(e);
38-
}
39-
}
22+
private static boolean isInitialized = false;
4023

4124
private JmsUtil() {
4225
// Utility class, prevent instantiation
4326
}
4427

28+
public static synchronized void initialize() {
29+
if (!isInitialized) {
30+
try {
31+
broker = new BrokerService();
32+
broker.addConnector(BROKER_URL);
33+
broker.setPersistent(false);
34+
broker.start();
35+
36+
factory = new ActiveMQConnectionFactory(BROKER_URL);
37+
defaultConnection = factory.createConnection();
38+
defaultConnection.start();
39+
isInitialized = true;
40+
} catch (Exception e) {
41+
System.err.println("Failed to initialize JMS: " + e.getMessage());
42+
throw new RuntimeException(e);
43+
}
44+
}
45+
}
46+
4547
/**
4648
* Creates a JMS session, optionally with a client ID for durable subscriptions.
4749
* Each client ID gets its own dedicated connection to support durable subscribers.
4850
*/
4951
public static Session createSession(String clientId) throws JMSException {
52+
if (!isInitialized) {
53+
initialize();
54+
}
5055
if (clientId == null) {
5156
return defaultConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
5257
}
@@ -72,8 +77,9 @@ public static Session createSession() throws JMSException {
7277
/**
7378
* Closes all JMS resources.
7479
*/
75-
public static void closeConnection() {
80+
public static synchronized void closeConnection() {
7681
try {
82+
isInitialized = false;
7783
if (defaultConnection != null) {
7884
defaultConnection.close();
7985
}
@@ -90,4 +96,10 @@ public static void closeConnection() {
9096
System.err.println("Error closing JMS resources: " + e.getMessage());
9197
}
9298
}
99+
100+
public static synchronized void reset() {
101+
closeConnection();
102+
isInitialized = false;
103+
initialize();
104+
}
93105
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.hungrydev399.publishsubscribe;
2+
3+
import org.junit.jupiter.api.BeforeAll;
4+
import org.junit.jupiter.api.AfterAll;
5+
import com.hungrydev399.publishsubscribe.jms.JmsUtil;
6+
7+
/**
8+
* Base class for JMS-related tests.
9+
*
10+
* Provides:
11+
* - Common JMS broker initialization
12+
* - Resource cleanup after tests
13+
* - Shared configuration for all JMS tests
14+
*
15+
* Usage:
16+
* - Extend this class in any test that needs JMS functionality
17+
* - Ensures consistent JMS lifecycle across test classes
18+
* - Prevents connection/broker issues between tests
19+
*/
20+
public abstract class TestBase {
21+
@BeforeAll
22+
static void initializeJms() {
23+
JmsUtil.initialize();
24+
}
25+
26+
@AfterAll
27+
static void cleanupJms() {
28+
JmsUtil.closeConnection();
29+
}
30+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.hungrydev399.publishsubscribe.jms;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
import org.junit.jupiter.api.Test;
5+
import javax.jms.JMSException;
6+
import javax.jms.Session;
7+
8+
public class JmsUtilTest {
9+
10+
@Test
11+
void shouldCreateSessionWithoutClientId() throws JMSException {
12+
Session session = JmsUtil.createSession();
13+
assertNotNull(session);
14+
}
15+
16+
@Test
17+
void shouldCreateSessionWithClientId() throws JMSException {
18+
Session session = JmsUtil.createSession("test-client");
19+
assertNotNull(session);
20+
}
21+
22+
@Test
23+
void shouldCloseConnectionGracefully() {
24+
JmsUtil.closeConnection();
25+
// Verify no exceptions thrown
26+
assertTrue(true);
27+
}
28+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.hungrydev399.publishsubscribe.model;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
import org.junit.jupiter.api.Test;
5+
6+
/**
7+
* Tests for the Message model class.
8+
*
9+
* Test coverage:
10+
* - Message creation with content and topic
11+
* - String representation (toString)
12+
* - Getters functionality
13+
*/
14+
public class MessageTest {
15+
@Test
16+
void shouldCreateMessageWithContentAndTopic() {
17+
Message message = new Message("Test content", "test-topic");
18+
19+
assertEquals("Test content", message.getContent());
20+
assertEquals("test-topic", message.getTopic());
21+
}
22+
23+
@Test
24+
void shouldGenerateCorrectToString() {
25+
Message message = new Message("Test content", "test-topic");
26+
String toString = message.toString();
27+
28+
assertTrue(toString.contains("test-topic"));
29+
assertTrue(toString.contains("Test content"));
30+
}
31+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.hungrydev399.publishsubscribe.publisher;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
import org.junit.jupiter.api.*;
5+
6+
import com.hungrydev399.publishsubscribe.model.Message;
7+
import com.hungrydev399.publishsubscribe.subscriber.TopicSubscriber;
8+
import com.hungrydev399.publishsubscribe.subscriber.SubscriberType;
9+
import com.hungrydev399.publishsubscribe.TestBase;
10+
import com.hungrydev399.publishsubscribe.jms.JmsUtil;
11+
12+
/**
13+
* Tests for the TopicPublisher class.
14+
*
15+
* Test Strategy:
16+
* - Uses TestBase for JMS lifecycle management
17+
* - Creates both publisher and subscriber to verify message flow
18+
* - Tests null message handling
19+
* - Validates resource cleanup
20+
*
21+
* Test Coverage:
22+
* - Message publishing functionality
23+
* - Error handling (null messages)
24+
* - Resource cleanup (close)
25+
* - JMS connection management
26+
*/
27+
class TopicPublisherTest extends TestBase {
28+
private TopicPublisher publisher;
29+
private TopicSubscriber subscriber;
30+
private static final String TEST_TOPIC = "TEST_TOPIC";
31+
private static final String TEST_MESSAGE = "Test Message";
32+
33+
@BeforeEach
34+
void setUp() throws Exception {
35+
JmsUtil.reset(); // Reset JMS state before each test
36+
publisher = new TopicPublisher(TEST_TOPIC);
37+
subscriber = new TopicSubscriber("TestSub", TEST_TOPIC, SubscriberType.NONDURABLE, null);
38+
Thread.sleep(100); // Allow connection setup
39+
}
40+
41+
@AfterEach
42+
void tearDown() throws Exception {
43+
if (subscriber != null) {
44+
subscriber.close();
45+
}
46+
if (publisher != null) {
47+
publisher.close();
48+
}
49+
}
50+
51+
@Test
52+
void shouldPublishAndReceiveMessage() throws Exception {
53+
Message message = new Message(TEST_MESSAGE, TEST_TOPIC);
54+
publisher.publish(message);
55+
56+
Thread.sleep(500); // Allow message delivery
57+
// Verification is done through console output
58+
assertTrue(true); // Test passes if no exceptions thrown
59+
}
60+
61+
@Test
62+
void shouldHandleNullMessage() {
63+
assertThrows(NullPointerException.class, () -> publisher.publish(null));
64+
}
65+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.hungrydev399.publishsubscribe.subscriber;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
import org.junit.jupiter.api.*;
5+
6+
import com.hungrydev399.publishsubscribe.model.Message;
7+
import com.hungrydev399.publishsubscribe.publisher.TopicPublisher;
8+
import com.hungrydev399.publishsubscribe.TestBase;
9+
import com.hungrydev399.publishsubscribe.jms.JmsUtil;
10+
11+
/**
12+
* Tests for the TopicSubscriber class.
13+
*
14+
* Test Strategy:
15+
* - Tests different subscription types (durable, non-durable)
16+
* - Verifies message reception
17+
* - Ensures proper resource cleanup
18+
* - Uses TestBase for JMS infrastructure
19+
*
20+
* Test Coverage:
21+
* - Subscriber creation with different types
22+
* - Message reception functionality
23+
* - Resource cleanup
24+
* - Subscription type verification
25+
* - Error handling
26+
*/
27+
class TopicSubscriberTest extends TestBase {
28+
private TopicPublisher publisher;
29+
private TopicSubscriber subscriber;
30+
private static final String TEST_TOPIC = "TEST_TOPIC";
31+
32+
@BeforeEach
33+
void setUp() throws Exception {
34+
JmsUtil.reset(); // Reset JMS state before each test
35+
publisher = new TopicPublisher(TEST_TOPIC);
36+
}
37+
38+
@AfterEach
39+
void tearDown() throws Exception {
40+
if (subscriber != null) {
41+
subscriber.close();
42+
}
43+
if (publisher != null) {
44+
publisher.close();
45+
}
46+
}
47+
48+
@Test
49+
void shouldCreateNonDurableSubscriber() throws Exception {
50+
subscriber = new TopicSubscriber("test", TEST_TOPIC, SubscriberType.NONDURABLE, null);
51+
assertNotNull(subscriber);
52+
assertEquals(SubscriberType.NONDURABLE, subscriber.getType());
53+
}
54+
55+
@Test
56+
void shouldCreateDurableSubscriber() throws Exception {
57+
subscriber = new TopicSubscriber("test", TEST_TOPIC, SubscriberType.DURABLE, "client1");
58+
assertNotNull(subscriber);
59+
assertEquals(SubscriberType.DURABLE, subscriber.getType());
60+
}
61+
62+
@Test
63+
void shouldReceiveMessages() throws Exception {
64+
subscriber = new TopicSubscriber("test", TEST_TOPIC, SubscriberType.NONDURABLE, null);
65+
Thread.sleep(100); // Allow subscriber to initialize
66+
67+
publisher.publish(new Message("Test message", TEST_TOPIC));
68+
Thread.sleep(500); // Allow message delivery
69+
70+
// Verification is done through console output
71+
assertTrue(true); // Test passes if no exceptions thrown
72+
}
73+
}

0 commit comments

Comments
 (0)