mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-15 06:58:41 +00:00
feature: Add context object pattern (#2304)
* Add context object pattern and corresponding tests * Update context pattern * Add README, class diagram, puml file. Add toString method in ServiceContext.java. Add tests for coverage. * Improvements: Remove plugin in pom file Add comments in app.java Change local variable keyword to var in app.java Use lombok for getters, setters and tostring Change method signature in context object * Refreshing environment-1 * Reconfigure pom file. Co-authored-by: Alvis Chan <u7287079@edu.anu.au> Co-authored-by: u7287079 <u7287079@anu.edu.au>
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
package com.iluwatar.contect.object;
|
||||
|
||||
import com.iluwatar.context.object.LayerA;
|
||||
import com.iluwatar.context.object.LayerB;
|
||||
import com.iluwatar.context.object.LayerC;
|
||||
import com.iluwatar.context.object.ServiceContext;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
/**
|
||||
* Date: 10/24/2022 - 3:18
|
||||
*
|
||||
* @author Chak Chan
|
||||
*/
|
||||
public class ServiceContextTest {
|
||||
|
||||
private static final String SERVICE = "SERVICE";
|
||||
|
||||
private LayerA layerA;
|
||||
|
||||
@BeforeEach
|
||||
void initiateLayerA() {
|
||||
this.layerA = new LayerA();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSameContextPassedBetweenLayers() {
|
||||
ServiceContext context1 = layerA.getContext();
|
||||
var layerB = new LayerB(layerA);
|
||||
ServiceContext context2 = layerB.getContext();
|
||||
var layerC = new LayerC(layerB);
|
||||
ServiceContext context3 = layerC.getContext();
|
||||
|
||||
assertSame(context1, context2);
|
||||
assertSame(context2, context3);
|
||||
assertSame(context3, context1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testScopedDataPassedBetweenLayers() {
|
||||
layerA.addAccountInfo(SERVICE);
|
||||
var layerB = new LayerB(layerA);
|
||||
var layerC = new LayerC(layerB);
|
||||
layerC.addSearchInfo(SERVICE);
|
||||
ServiceContext context = layerC.getContext();
|
||||
|
||||
assertEquals(SERVICE,context.getAccountService());
|
||||
assertNull(context.getSessionService());
|
||||
assertEquals(SERVICE,context.getSearchService());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testToString() {
|
||||
assertEquals(layerA.getContext().toString(),"ServiceContext(AccountService=null, SessionService=null, SearchService=null)");
|
||||
layerA.addAccountInfo(SERVICE);
|
||||
assertEquals(layerA.getContext().toString(), "ServiceContext(AccountService=SERVICE, SessionService=null, SearchService=null)");
|
||||
var layerB = new LayerB(layerA);
|
||||
layerB.addSessionInfo(SERVICE);
|
||||
var layerC = new LayerC(layerB);
|
||||
assertEquals(layerC.getContext().toString(), "ServiceContext(AccountService=SERVICE, SessionService=SERVICE, SearchService=null)");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user