fix: Fix context object (#2415)

* fix pr builder goals

* update links

* add context-object to build

* add autogenerated content

* fix checkstyle findings
This commit is contained in:
Ilkka Seppälä authored and GitHub committed 2023-01-01 12:28:51 +02:00
1 parent 3d30e92cb2
commit 8877ba70d3
17 files changed
+254 -94

No files matched your search

+1 -1
View File
@@ -61,7 +61,7 @@ jobs:
run: sudo apt-get install -y xvfb run: sudo apt-get install -y xvfb
- name: Build with Maven and run SonarQube analysis - name: Build with Maven and run SonarQube analysis
run: xvfb-run ./mvnw clean -Dsonar.host.url=https://sonarcloud.io -Dsonar.organization=iluwatar -Dsonar.projectKey=iluwatar_java-design-patterns -Dsonar.pullrequest.branch=${{ github.head_ref }} -Dsonar.pullrequest.base=${{ github.base_ref }} -Dsonar.pullrequest.key=${{ github.event.pull_request.number }} run: xvfb-run ./mvnw clean verify -Dsonar.host.url=https://sonarcloud.io -Dsonar.organization=iluwatar -Dsonar.projectKey=iluwatar_java-design-patterns -Dsonar.pullrequest.branch=${{ github.head_ref }} -Dsonar.pullrequest.base=${{ github.base_ref }} -Dsonar.pullrequest.key=${{ github.event.pull_request.number }}
env: env:
# These two env variables are needed for sonar analysis # These two env variables are needed for sonar analysis
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
@@ -0,0 +1,51 @@
@startuml
package com.iluwatar.client.session {
class App {
+ App()
+ main(args : String[]) {static}
}
class Request {
- data : String
- session : Session
+ Request(data : String, session : Session)
# canEqual(other : Object) : boolean
+ equals(o : Object) : boolean
+ getData() : String
+ getSession() : Session
+ hashCode() : int
+ setData(data : String)
+ setSession(session : Session)
+ toString() : String
}
class Server {
- LOGGER : Logger {static}
- host : String
- port : int
+ Server(host : String, port : int)
# canEqual(other : Object) : boolean
+ equals(o : Object) : boolean
+ getHost() : String
+ getPort() : int
+ getSession(name : String) : Session
+ hashCode() : int
+ process(request : Request)
+ setHost(host : String)
+ setPort(port : int)
+ toString() : String
}
class Session {
- clientName : String
- id : String
+ Session(id : String, clientName : String)
# canEqual(other : Object) : boolean
+ equals(o : Object) : boolean
+ getClientName() : String
+ getId() : String
+ hashCode() : int
+ setClientName(clientName : String)
+ setId(id : String)
+ toString() : String
}
}
Request --> "-session" Session
@enduml
+3 -2
View File
@@ -179,6 +179,7 @@ Use the Context Object pattern for:
## Credits ## Credits
* [J2EE Design Patterns](http://corej2eepatterns.com/ContextObject.htm) * [Core J2EE Design Patterns](https://amzn.to/3IhcY9w)
* [Core J2EE Design Patterns website - Context Object](http://corej2eepatterns.com/ContextObject.htm)
* [Allan Kelly - The Encapsulate Context Pattern](https://accu.org/journals/overload/12/63/kelly_246/) * [Allan Kelly - The Encapsulate Context Pattern](https://accu.org/journals/overload/12/63/kelly_246/)
* [Arvid S. Krishna et al. - Context Object](https://www.dre.vanderbilt.edu/~schmidt/PDF/Context-Object-Pattern.pdf) * [Arvid S. Krishna et al. - Context Object](https://www.dre.vanderbilt.edu/~schmidt/PDF/Context-Object-Pattern.pdf)
@@ -13,29 +13,30 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j @Slf4j
public class App { public class App {
private static final String SERVICE = "SERVICE"; private static final String SERVICE = "SERVICE";
/** /**
* Program entry point. * Program entry point.
* @param args command line args *
*/ * @param args command line args
public static void main(String[] args) { */
//Initiate first layer and add service information into context public static void main(String[] args) {
var layerA = new LayerA(); //Initiate first layer and add service information into context
layerA.addAccountInfo(SERVICE); var layerA = new LayerA();
layerA.addAccountInfo(SERVICE);
LOGGER.info("Context = {}",layerA.getContext()); LOGGER.info("Context = {}", layerA.getContext());
//Initiate second layer and preserving information retrieved in first layer through passing context object //Initiate second layer and preserving information retrieved in first layer through passing context object
var layerB = new LayerB(layerA); var layerB = new LayerB(layerA);
layerB.addSessionInfo(SERVICE); layerB.addSessionInfo(SERVICE);
LOGGER.info("Context = {}",layerB.getContext()); LOGGER.info("Context = {}", layerB.getContext());
//Initiate third layer and preserving information retrieved in first and second layer through passing context object //Initiate third layer and preserving information retrieved in first and second layer through passing context object
var layerC = new LayerC(layerB); var layerC = new LayerC(layerB);
layerC.addSearchInfo(SERVICE); layerC.addSearchInfo(SERVICE);
LOGGER.info("Context = {}",layerC.getContext()); LOGGER.info("Context = {}", layerC.getContext());
} }
} }
@@ -5,13 +5,13 @@ import lombok.Getter;
@Getter @Getter
public class LayerA { public class LayerA {
private ServiceContext context; private ServiceContext context;
public LayerA() { public LayerA() {
context = ServiceContextFactory.createContext(); context = ServiceContextFactory.createContext();
} }
public void addAccountInfo(String accountService) { public void addAccountInfo(String accountService) {
context.setAccountService(accountService); context.setAccountService(accountService);
} }
} }
@@ -5,13 +5,13 @@ import lombok.Getter;
@Getter @Getter
public class LayerB { public class LayerB {
private ServiceContext context; private ServiceContext context;
public LayerB(LayerA layerA) { public LayerB(LayerA layerA) {
this.context = layerA.getContext(); this.context = layerA.getContext();
} }
public void addSessionInfo(String sessionService) { public void addSessionInfo(String sessionService) {
context.setSessionService(sessionService); context.setSessionService(sessionService);
} }
} }
@@ -5,13 +5,13 @@ import lombok.Getter;
@Getter @Getter
public class LayerC { public class LayerC {
public ServiceContext context; public ServiceContext context;
public LayerC(LayerB layerB) { public LayerC(LayerB layerB) {
this.context = layerB.getContext(); this.context = layerB.getContext();
} }
public void addSearchInfo(String searchService) { public void addSearchInfo(String searchService) {
context.setSearchService(searchService); context.setSearchService(searchService);
} }
} }
@@ -12,5 +12,7 @@ import lombok.ToString;
@Setter @Setter
public class ServiceContext { public class ServiceContext {
String AccountService, SessionService, SearchService; String accountService;
String sessionService;
String searchService;
} }
@@ -5,7 +5,7 @@ package com.iluwatar.context.object;
*/ */
public class ServiceContextFactory { public class ServiceContextFactory {
public static ServiceContext createContext() { public static ServiceContext createContext() {
return new ServiceContext(); return new ServiceContext();
} }
} }
@@ -7,11 +7,11 @@ import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
public class AppTest { public class AppTest {
/** /**
* Test example app runs without error. * Test example app runs without error.
*/ */
@Test @Test
void shouldExecuteWithoutException() { void shouldExecuteWithoutException() {
assertDoesNotThrow(() -> App.main(new String[]{})); assertDoesNotThrow(() -> App.main(new String[] {}));
} }
} }
@@ -18,49 +18,52 @@ import static org.junit.jupiter.api.Assertions.assertNull;
*/ */
public class ServiceContextTest { public class ServiceContextTest {
private static final String SERVICE = "SERVICE"; private static final String SERVICE = "SERVICE";
private LayerA layerA; private LayerA layerA;
@BeforeEach @BeforeEach
void initiateLayerA() { void initiateLayerA() {
this.layerA = new LayerA(); this.layerA = new LayerA();
} }
@Test @Test
void testSameContextPassedBetweenLayers() { void testSameContextPassedBetweenLayers() {
ServiceContext context1 = layerA.getContext(); ServiceContext context1 = layerA.getContext();
var layerB = new LayerB(layerA); var layerB = new LayerB(layerA);
ServiceContext context2 = layerB.getContext(); ServiceContext context2 = layerB.getContext();
var layerC = new LayerC(layerB); var layerC = new LayerC(layerB);
ServiceContext context3 = layerC.getContext(); ServiceContext context3 = layerC.getContext();
assertSame(context1, context2); assertSame(context1, context2);
assertSame(context2, context3); assertSame(context2, context3);
assertSame(context3, context1); assertSame(context3, context1);
} }
@Test @Test
void testScopedDataPassedBetweenLayers() { void testScopedDataPassedBetweenLayers() {
layerA.addAccountInfo(SERVICE); layerA.addAccountInfo(SERVICE);
var layerB = new LayerB(layerA); var layerB = new LayerB(layerA);
var layerC = new LayerC(layerB); var layerC = new LayerC(layerB);
layerC.addSearchInfo(SERVICE); layerC.addSearchInfo(SERVICE);
ServiceContext context = layerC.getContext(); ServiceContext context = layerC.getContext();
assertEquals(SERVICE,context.getAccountService()); assertEquals(SERVICE, context.getAccountService());
assertNull(context.getSessionService()); assertNull(context.getSessionService());
assertEquals(SERVICE,context.getSearchService()); assertEquals(SERVICE, context.getSearchService());
} }
@Test @Test
void testToString() { void testToString() {
assertEquals(layerA.getContext().toString(),"ServiceContext(AccountService=null, SessionService=null, SearchService=null)"); assertEquals(layerA.getContext().toString(),
layerA.addAccountInfo(SERVICE); "ServiceContext(AccountService=null, SessionService=null, SearchService=null)");
assertEquals(layerA.getContext().toString(), "ServiceContext(AccountService=SERVICE, SessionService=null, SearchService=null)"); layerA.addAccountInfo(SERVICE);
var layerB = new LayerB(layerA); assertEquals(layerA.getContext().toString(),
layerB.addSessionInfo(SERVICE); "ServiceContext(AccountService=SERVICE, SessionService=null, SearchService=null)");
var layerC = new LayerC(layerB); var layerB = new LayerB(layerA);
assertEquals(layerC.getContext().toString(), "ServiceContext(AccountService=SERVICE, SessionService=SERVICE, SearchService=null)"); layerB.addSessionInfo(SERVICE);
} var layerC = new LayerC(layerB);
assertEquals(layerC.getContext().toString(),
"ServiceContext(AccountService=SERVICE, SessionService=SERVICE, SearchService=null)");
}
} }
@@ -1,6 +1,8 @@
# #
# This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
#
# The MIT License # The MIT License
# Copyright © 2014-2021 Ilkka Seppälä # Copyright © 2014-2022 Ilkka Seppälä
# #
# Permission is hereby granted, free of charge, to any person obtaining a copy # Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal # of this software and associated documentation files (the "Software"), to deal
@@ -20,6 +22,5 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE. # THE SOFTWARE.
# #
#spring.thymeleaf.mode=HTML
server.port=51515 server.port=51515
@@ -1,3 +1,29 @@
<!--
This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
The MIT License
Copyright © 2014-2022 Ilkka Seppälä
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<!DOCTYPE HTML> <!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org"> <html xmlns:th="http://www.thymeleaf.org">
<head> <head>
@@ -1,3 +1,29 @@
<!--
This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
The MIT License
Copyright © 2014-2022 Ilkka Seppälä
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<!DOCTYPE HTML> <!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org"> <html xmlns:th="http://www.thymeleaf.org">
<head> <head>
@@ -1,3 +1,27 @@
/*
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
*
* The MIT License
* Copyright © 2014-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.page.controller; package com.iluwatar.page.controller;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@@ -1,3 +1,27 @@
/*
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
*
* The MIT License
* Copyright © 2014-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.page.controller; package com.iluwatar.page.controller;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
+1
View File
@@ -204,6 +204,7 @@
<module>serialized-entity</module> <module>serialized-entity</module>
<module>identity-map</module> <module>identity-map</module>
<module>component</module> <module>component</module>
<module>context-object</module>
</modules> </modules>
<repositories> <repositories>
<repository> <repository>