Skip to content

Commit 8cc31d8

Browse files
committed
more tests for coverage
1 parent 019a3c4 commit 8cc31d8

File tree

3 files changed

+69
-5
lines changed

3 files changed

+69
-5
lines changed

implicitlock/src/main/java/com/iluwatar/BusinessTransaction.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package com.iluwatar;
22

3+
import java.util.logging.Level;
4+
import java.util.logging.Logger;
5+
36
/**
47
* BusinessTransaction class handles the logic of processing customer transactions.
58
* It works with the Framework to acquire and release locks for resources.
69
*/
710
public class BusinessTransaction {
811

12+
private static final Logger logger = Logger.getLogger(BusinessTransaction.class.getName());
913
private final Framework framework;
1014

1115
/**
@@ -24,10 +28,11 @@ public BusinessTransaction(Framework framework) {
2428
* @param resource the resource to be locked during the transaction
2529
* @param customerId the ID of the customer being processed
2630
* @param customerData the data related to the customer being processed
31+
* @return true if the transaction was processed successfully, false otherwise
2732
*/
28-
public void processCustomer(Resource resource, String customerId, String customerData) {
29-
// Print a message indicating which customer is being processed
30-
System.out.println("Processing customer " + customerId + " with data: " + customerData);
33+
public boolean processCustomer(Resource resource, String customerId, String customerData) {
34+
// Log a message indicating which customer is being processed
35+
logger.log(Level.INFO, "Processing customer {0} with data: {1}", new Object[]{customerId, customerData});
3136

3237
// Try to acquire the lock for the resource
3338
if (framework.tryLockResource(resource)) {
@@ -36,11 +41,18 @@ public void processCustomer(Resource resource, String customerId, String custome
3641
Thread.sleep(500);
3742
} catch (InterruptedException e) {
3843
Thread.currentThread().interrupt(); // Handle interruption
44+
return false; // Return false if the thread was interrupted
3945
}
46+
4047
// Release the lock after processing is done
4148
framework.notifyReleaseLock(resource);
49+
50+
// Log success and return true to indicate the transaction was completed
51+
logger.log(Level.INFO, "Customer {0} processed successfully.", customerId);
52+
return true;
4253
} else {
43-
System.out.println("Failed to acquire lock for resource: " + resource.getId());
54+
logger.log(Level.WARNING, "Failed to acquire lock for resource: {0}", resource.getId());
55+
return false; // Return false if the lock was not acquired
4456
}
4557
}
4658
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.iluwatar.implicitlock;
2+
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.Test;
5+
import com.iluwatar.Framework;
6+
import com.iluwatar.LockManager;
7+
import com.iluwatar.Resource;
8+
import com.iluwatar.BusinessTransaction;
9+
10+
import static org.junit.jupiter.api.Assertions.*;
11+
12+
class BusinessTransactionTest {
13+
14+
private LockManager lockManager;
15+
private Framework framework;
16+
private Resource resource1;
17+
private BusinessTransaction transaction;
18+
19+
@BeforeEach
20+
void setUp() {
21+
lockManager = new LockManager();
22+
framework = new Framework(lockManager);
23+
transaction = new BusinessTransaction(framework);
24+
resource1 = new Resource("Resource1");
25+
}
26+
27+
@Test
28+
void verifyProcessCustomerSuccess() {
29+
// Process a customer transaction and assert it returns true (indicating success)
30+
boolean result = transaction.processCustomer(resource1, "456", "Customer data for 456");
31+
assertTrue(result, "The transaction should be processed successfully and return true");
32+
}
33+
@Test
34+
void verifyProcessCustomerWithInterruption() {
35+
// Simulate an interrupted transaction and ensure it returns false
36+
Thread transactionThread = new Thread(() -> {
37+
boolean result = transaction.processCustomer(resource1, "456", "Customer data for 456");
38+
assertFalse(result, "The transaction should return false if interrupted");
39+
});
40+
41+
// Start and interrupt the transaction thread
42+
transactionThread.start();
43+
transactionThread.interrupt();
44+
45+
try {
46+
transactionThread.join(); // Wait for thread to finish
47+
} catch (InterruptedException e) {
48+
Thread.currentThread().interrupt();
49+
}
50+
}
51+
}

implicitlock/src/test/java/com/iluwatar/implicitlock/ImplicitlockTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,5 @@ void verifyResourceLockedByDifferentThreads() throws InterruptedException {
6464
thread2.start();
6565
thread2.join();
6666
}
67-
}
67+
68+
}

0 commit comments

Comments
 (0)