mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-10-13 08:15:38 +00:00
deps: Refactor dependencies (#3224)
* remove spring dep move junit, logging, mockito under dep mgmt * upgrade anti-corruption-layer deps * async method invocation * balking, bloc * bridge to bytecode * caching * callback - cqrs * component - health check * hexagonal - metadata mapping * rest of the patterns * remove checkstyle, take spotless into use
This commit is contained in:
1 parent
371439aeaa
commit
0ca162a55c
1863 files changed
+14403
-17632
No files matched your search
@@ -58,6 +58,14 @@
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
|
||||
@@ -41,9 +41,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
/**
|
||||
* SLOB Application using {@link LobSerializer} and H2 DB.
|
||||
*/
|
||||
/** SLOB Application using {@link LobSerializer} and H2 DB. */
|
||||
public class App {
|
||||
|
||||
public static final String CLOB = "CLOB";
|
||||
@@ -51,20 +49,20 @@ public class App {
|
||||
|
||||
/**
|
||||
* Main entry point to program.
|
||||
*
|
||||
* <p>In the SLOB pattern, the object graph is serialized into a single large object (a BLOB or
|
||||
* CLOB, for Binary Large Object or Character Large Object, respectively) and stored in the
|
||||
* database. When the object graph needs to be retrieved, it is read from the database and
|
||||
* deserialized back into the original object graph.</p>
|
||||
* deserialized back into the original object graph.
|
||||
*
|
||||
* <p>A Forest is created using {@link #createForest()} with Animals and Plants along with their
|
||||
* respective relationships.</p>
|
||||
* respective relationships.
|
||||
*
|
||||
* <p>Creates a {@link LobSerializer} using the method
|
||||
* {@link #createLobSerializer(String[])}.</p>
|
||||
* <p>Creates a {@link LobSerializer} using the method {@link #createLobSerializer(String[])}.
|
||||
*
|
||||
* <p>Once created the serializer is passed to the
|
||||
* {@link #executeSerializer(Forest, LobSerializer)} which handles the serialization,
|
||||
* deserialization and persisting and loading from DB.</p>
|
||||
* <p>Once created the serializer is passed to the {@link #executeSerializer(Forest,
|
||||
* LobSerializer)} which handles the serialization, deserialization and persisting and loading
|
||||
* from DB.
|
||||
*
|
||||
* @param args if first arg is CLOB then ClobSerializer is used else BlobSerializer is used.
|
||||
*/
|
||||
@@ -75,12 +73,13 @@ public class App {
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Creates a {@link LobSerializer} on the basis of input args. </p>
|
||||
* <p>If input args are not empty and the value equals {@link App#CLOB} then a
|
||||
* {@link ClobSerializer} is created else a {@link BlobSerializer} is created.</p>
|
||||
* Creates a {@link LobSerializer} on the basis of input args.
|
||||
*
|
||||
* <p>If input args are not empty and the value equals {@link App#CLOB} then a {@link
|
||||
* ClobSerializer} is created else a {@link BlobSerializer} is created.
|
||||
*
|
||||
* @param args if first arg is {@link App#CLOB} then ClobSerializer is instantiated else
|
||||
* BlobSerializer is instantiated.
|
||||
* BlobSerializer is instantiated.
|
||||
*/
|
||||
private static LobSerializer createLobSerializer(String[] args) throws SQLException {
|
||||
LobSerializer serializer;
|
||||
@@ -96,14 +95,14 @@ public class App {
|
||||
* Creates a Forest with {@link Animal} and {@link Plant} along with their respective
|
||||
* relationships.
|
||||
*
|
||||
* <p> The method creates a {@link Forest} with 2 Plants Grass and Oak of type Herb and tree
|
||||
* respectively.</p>
|
||||
* <p>The method creates a {@link Forest} with 2 Plants Grass and Oak of type Herb and tree
|
||||
* respectively.
|
||||
*
|
||||
* <p> It also creates 3 animals Zebra and Buffalo which eat the plant grass. Lion consumes the
|
||||
* Zebra and the Buffalo.</p>
|
||||
* <p>It also creates 3 animals Zebra and Buffalo which eat the plant grass. Lion consumes the
|
||||
* Zebra and the Buffalo.
|
||||
*
|
||||
* <p>With the above animals and plants and their relationships a forest
|
||||
* object is created which represents the Object Graph.</p>
|
||||
* <p>With the above animals and plants and their relationships a forest object is created which
|
||||
* represents the Object Graph.
|
||||
*
|
||||
* @return Forest Object
|
||||
*/
|
||||
@@ -122,7 +121,7 @@ public class App {
|
||||
* Serialize the input object using the input serializer and persist to DB. After this it loads
|
||||
* the same object back from DB and deserializes using the same serializer.
|
||||
*
|
||||
* @param forest Object to Serialize and Persist
|
||||
* @param forest Object to Serialize and Persist
|
||||
* @param lobSerializer Serializer to Serialize and Deserialize Object
|
||||
*/
|
||||
private static void executeSerializer(Forest forest, LobSerializer lobSerializer) {
|
||||
@@ -135,9 +134,12 @@ public class App {
|
||||
Forest forestFromDb = serializer.deSerialize(fromDb);
|
||||
|
||||
LOGGER.info(forestFromDb.toString());
|
||||
} catch (SQLException | IOException | TransformerException | ParserConfigurationException
|
||||
| SAXException
|
||||
| ClassNotFoundException e) {
|
||||
} catch (SQLException
|
||||
| IOException
|
||||
| TransformerException
|
||||
| ParserConfigurationException
|
||||
| SAXException
|
||||
| ClassNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,9 +30,7 @@ import javax.sql.DataSource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.h2.jdbcx.JdbcDataSource;
|
||||
|
||||
/**
|
||||
* Service to handle database operations.
|
||||
*/
|
||||
/** Service to handle database operations. */
|
||||
@Slf4j
|
||||
public class DatabaseService {
|
||||
|
||||
@@ -73,8 +71,7 @@ public class DatabaseService {
|
||||
*
|
||||
* @throws SQLException if any issue occurs while executing DROP Query
|
||||
*/
|
||||
public void shutDownService()
|
||||
throws SQLException {
|
||||
public void shutDownService() throws SQLException {
|
||||
try (var connection = dataSource.getConnection();
|
||||
var statement = connection.createStatement()) {
|
||||
statement.execute(DELETE_SCHEMA_SQL);
|
||||
@@ -82,14 +79,13 @@ public class DatabaseService {
|
||||
}
|
||||
|
||||
/**
|
||||
* Initaites startup sequence and executes the query
|
||||
* {@link DatabaseService#CREATE_BINARY_SCHEMA_DDL} if {@link DatabaseService#dataTypeDb} is
|
||||
* binary else will execute the query {@link DatabaseService#CREATE_TEXT_SCHEMA_DDL}.
|
||||
* Initaites startup sequence and executes the query {@link
|
||||
* DatabaseService#CREATE_BINARY_SCHEMA_DDL} if {@link DatabaseService#dataTypeDb} is binary else
|
||||
* will execute the query {@link DatabaseService#CREATE_TEXT_SCHEMA_DDL}.
|
||||
*
|
||||
* @throws SQLException if there are any issues during DDL execution
|
||||
*/
|
||||
public void startupService()
|
||||
throws SQLException {
|
||||
public void startupService() throws SQLException {
|
||||
try (var connection = dataSource.getConnection();
|
||||
var statement = connection.createStatement()) {
|
||||
if (dataTypeDb.equals(BINARY_DATA)) {
|
||||
@@ -103,14 +99,13 @@ public class DatabaseService {
|
||||
/**
|
||||
* Executes the insert query {@link DatabaseService#INSERT}.
|
||||
*
|
||||
* @param id with which row is to be inserted
|
||||
* @param id with which row is to be inserted
|
||||
* @param name name to be added in the row
|
||||
* @param data object data to be saved in the row
|
||||
* @throws SQLException if there are any issues in executing insert query
|
||||
* {@link DatabaseService#INSERT}
|
||||
* @throws SQLException if there are any issues in executing insert query {@link
|
||||
* DatabaseService#INSERT}
|
||||
*/
|
||||
public void insert(int id, String name, Object data)
|
||||
throws SQLException {
|
||||
public void insert(int id, String name, Object data) throws SQLException {
|
||||
try (var connection = dataSource.getConnection();
|
||||
var insert = connection.prepareStatement(INSERT)) {
|
||||
insert.setInt(1, id);
|
||||
@@ -121,22 +116,20 @@ public class DatabaseService {
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the select query {@link DatabaseService#SELECT} form the result set returns an
|
||||
* {@link java.io.InputStream} if {@link DatabaseService#dataTypeDb} is 'binary' else will return
|
||||
* the object as a {@link String}.
|
||||
* Runs the select query {@link DatabaseService#SELECT} form the result set returns an {@link
|
||||
* java.io.InputStream} if {@link DatabaseService#dataTypeDb} is 'binary' else will return the
|
||||
* object as a {@link String}.
|
||||
*
|
||||
* @param id with which row is to be selected
|
||||
* @param id with which row is to be selected
|
||||
* @param columnsName column in which the object is stored
|
||||
* @return object found from DB
|
||||
* @throws SQLException if there are any issues in executing insert query *
|
||||
* {@link DatabaseService#SELECT}
|
||||
* @throws SQLException if there are any issues in executing insert query * {@link
|
||||
* DatabaseService#SELECT}
|
||||
*/
|
||||
public Object select(final long id, String columnsName) throws SQLException {
|
||||
ResultSet resultSet = null;
|
||||
try (var connection = dataSource.getConnection();
|
||||
var preparedStatement =
|
||||
connection.prepareStatement(SELECT)
|
||||
) {
|
||||
var preparedStatement = connection.prepareStatement(SELECT)) {
|
||||
Object result = null;
|
||||
preparedStatement.setLong(1, id);
|
||||
resultSet = preparedStatement.executeQuery();
|
||||
|
||||
@@ -35,9 +35,7 @@ import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
|
||||
/**
|
||||
* Creates an object Animal with a list of animals and/or plants it consumes.
|
||||
*/
|
||||
/** Creates an object Animal with a list of animals and/or plants it consumes. */
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@@ -51,12 +49,12 @@ public class Animal implements Serializable {
|
||||
* Iterates over the input nodes recursively and adds new plants to {@link Animal#plantsEaten} or
|
||||
* animals to {@link Animal#animalsEaten} found to input sets respectively.
|
||||
*
|
||||
* @param childNodes contains the XML Node containing the Forest
|
||||
* @param childNodes contains the XML Node containing the Forest
|
||||
* @param animalsEaten set of Animals eaten
|
||||
* @param plantsEaten set of Plants eaten
|
||||
* @param plantsEaten set of Plants eaten
|
||||
*/
|
||||
protected static void iterateXmlForAnimalAndPlants(NodeList childNodes, Set<Animal> animalsEaten,
|
||||
Set<Plant> plantsEaten) {
|
||||
protected static void iterateXmlForAnimalAndPlants(
|
||||
NodeList childNodes, Set<Animal> animalsEaten, Set<Plant> plantsEaten) {
|
||||
for (int i = 0; i < childNodes.getLength(); i++) {
|
||||
Node child = childNodes.item(i);
|
||||
if (child.getNodeType() == Node.ELEMENT_NODE) {
|
||||
|
||||
@@ -37,12 +37,11 @@ import lombok.NoArgsConstructor;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.NodeList;
|
||||
|
||||
/**
|
||||
* Creates an object Forest which contains animals and plants as its constituents. Animals may eat
|
||||
* plants or other animals in the forest.
|
||||
*/
|
||||
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
|
||||
@@ -34,9 +34,7 @@ import org.w3c.dom.Element;
|
||||
import org.w3c.dom.NamedNodeMap;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
/**
|
||||
* Creates an object Plant which contains its name and type.
|
||||
*/
|
||||
/** Creates an object Plant which contains its name and type. */
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
|
||||
@@ -69,7 +69,7 @@ public class BlobSerializer extends LobSerializer {
|
||||
* @param toDeserialize Input Object to De-serialize
|
||||
* @return Deserialized Object
|
||||
* @throws ClassNotFoundException {@inheritDoc}
|
||||
* @throws IOException {@inheritDoc}
|
||||
* @throws IOException {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Forest deSerialize(Object toDeserialize) throws IOException, ClassNotFoundException {
|
||||
|
||||
@@ -76,7 +76,7 @@ public class ClobSerializer extends LobSerializer {
|
||||
* @param forest Object which is to be serialized
|
||||
* @return Serialized object
|
||||
* @throws ParserConfigurationException If any issues occur in parsing input object
|
||||
* @throws TransformerException If any issues occur in Transformation from Node to XML
|
||||
* @throws TransformerException If any issues occur in Transformation from Node to XML
|
||||
*/
|
||||
@Override
|
||||
public Object serialize(Forest forest) throws ParserConfigurationException, TransformerException {
|
||||
@@ -90,14 +90,14 @@ public class ClobSerializer extends LobSerializer {
|
||||
* @param toDeserialize Input Object to De-serialize
|
||||
* @return Deserialized Object
|
||||
* @throws ParserConfigurationException If any issues occur in parsing input object
|
||||
* @throws IOException if any issues occur during reading object
|
||||
* @throws SAXException If any issues occur in Transformation from Node to XML
|
||||
* @throws IOException if any issues occur during reading object
|
||||
* @throws SAXException If any issues occur in Transformation from Node to XML
|
||||
*/
|
||||
@Override
|
||||
public Forest deSerialize(Object toDeserialize)
|
||||
throws ParserConfigurationException, IOException, SAXException {
|
||||
DocumentBuilder documentBuilder = DocumentBuilderFactory.newDefaultInstance()
|
||||
.newDocumentBuilder();
|
||||
DocumentBuilder documentBuilder =
|
||||
DocumentBuilderFactory.newDefaultInstance().newDocumentBuilder();
|
||||
var stream = new ByteArrayInputStream(toDeserialize.toString().getBytes());
|
||||
Document parsed = documentBuilder.parse(stream);
|
||||
Forest forest = new Forest();
|
||||
|
||||
@@ -60,8 +60,8 @@ public abstract class LobSerializer implements Serializable, Closeable {
|
||||
* @param toSerialize Input Object to serialize
|
||||
* @return Serialized Object
|
||||
* @throws ParserConfigurationException if any issue occurs during parsing of input object
|
||||
* @throws TransformerException if any issue occurs during Transformation
|
||||
* @throws IOException if any issues occur during reading object
|
||||
* @throws TransformerException if any issue occurs during Transformation
|
||||
* @throws IOException if any issues occur during reading object
|
||||
*/
|
||||
public abstract Object serialize(Forest toSerialize)
|
||||
throws ParserConfigurationException, TransformerException, IOException;
|
||||
@@ -69,8 +69,8 @@ public abstract class LobSerializer implements Serializable, Closeable {
|
||||
/**
|
||||
* Saves the object to DB with the provided ID.
|
||||
*
|
||||
* @param id key to be sent to DB service
|
||||
* @param name Object name to store in DB
|
||||
* @param id key to be sent to DB service
|
||||
* @param name Object name to store in DB
|
||||
* @param object Object to store in DB
|
||||
* @return ID with which the object is stored in DB
|
||||
* @throws SQLException if any issue occurs while saving to DB
|
||||
@@ -83,7 +83,7 @@ public abstract class LobSerializer implements Serializable, Closeable {
|
||||
/**
|
||||
* Loads the object from db using the ID and column name.
|
||||
*
|
||||
* @param id to query the DB
|
||||
* @param id to query the DB
|
||||
* @param columnName column from which object is to be extracted
|
||||
* @return Object from DB
|
||||
* @throws SQLException if any issue occurs while loading from DB
|
||||
@@ -98,8 +98,8 @@ public abstract class LobSerializer implements Serializable, Closeable {
|
||||
* @param toDeserialize object to deserialize
|
||||
* @return Deserialized Object
|
||||
* @throws ParserConfigurationException If issue occurs during parsing of input object
|
||||
* @throws IOException if any issues occur during reading object
|
||||
* @throws SAXException if any issues occur during reading object for XML parsing
|
||||
* @throws IOException if any issues occur during reading object
|
||||
* @throws SAXException if any issues occur during reading object for XML parsing
|
||||
*/
|
||||
public abstract Forest deSerialize(Object toDeserialize)
|
||||
throws ParserConfigurationException, IOException, SAXException, ClassNotFoundException;
|
||||
|
||||
@@ -43,20 +43,20 @@ import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
/**
|
||||
* SLOB Application test
|
||||
*/
|
||||
/** SLOB Application test */
|
||||
@Slf4j
|
||||
class AppTest {
|
||||
|
||||
/**
|
||||
* Creates a Forest with Animals and Plants along with their respective relationships.
|
||||
* <p> The method creates a forest with 2 Plants Grass and Oak of type Herb and tree
|
||||
* respectively.</p>
|
||||
* <p> It also creates 3 animals Zebra and Buffalo which eat the plant grass. Lion consumes the
|
||||
* Zebra and the Buffalo.</p>
|
||||
* <p>With the above animals and plants and their relationships a forest
|
||||
* object is created which represents the Object Graph.</p>
|
||||
*
|
||||
* <p>The method creates a forest with 2 Plants Grass and Oak of type Herb and tree respectively.
|
||||
*
|
||||
* <p>It also creates 3 animals Zebra and Buffalo which eat the plant grass. Lion consumes the
|
||||
* Zebra and the Buffalo.
|
||||
*
|
||||
* <p>With the above animals and plants and their relationships a forest object is created which
|
||||
* represents the Object Graph.
|
||||
*
|
||||
* @return Forest Object
|
||||
*/
|
||||
@@ -72,28 +72,30 @@ class AppTest {
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the {@link App} without passing any argument in the args to test the
|
||||
* {@link ClobSerializer}.
|
||||
* Tests the {@link App} without passing any argument in the args to test the {@link
|
||||
* ClobSerializer}.
|
||||
*/
|
||||
@Test
|
||||
void shouldExecuteWithoutExceptionClob() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{"CLOB"}));
|
||||
assertDoesNotThrow(() -> App.main(new String[] {"CLOB"}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the {@link App} without passing any argument in the args to test the
|
||||
* {@link BlobSerializer}.
|
||||
* Tests the {@link App} without passing any argument in the args to test the {@link
|
||||
* BlobSerializer}.
|
||||
*/
|
||||
@Test
|
||||
void shouldExecuteWithoutExceptionBlob() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
assertDoesNotThrow(() -> App.main(new String[] {}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the serialization of the input object using the {@link ClobSerializer} and persists the
|
||||
* serialized object to DB, then load the object back from DB and deserializes it using the
|
||||
* provided {@link ClobSerializer}.<p>After loading the object back from DB the test matches the
|
||||
* hash of the input object with the hash of the object that was loaded from DB and deserialized.
|
||||
* provided {@link ClobSerializer}.
|
||||
*
|
||||
* <p>After loading the object back from DB the test matches the hash of the input object with the
|
||||
* hash of the object that was loaded from DB and deserialized.
|
||||
*/
|
||||
@Test
|
||||
void clobSerializerTest() {
|
||||
@@ -106,11 +108,16 @@ class AppTest {
|
||||
Object fromDb = serializer.loadFromDb(id, Forest.class.getSimpleName());
|
||||
Forest forestFromDb = serializer.deSerialize(fromDb);
|
||||
|
||||
Assertions.assertEquals(forest.hashCode(), forestFromDb.hashCode(),
|
||||
Assertions.assertEquals(
|
||||
forest.hashCode(),
|
||||
forestFromDb.hashCode(),
|
||||
"Hashes of objects after Serializing and Deserializing are the same");
|
||||
} catch (SQLException | IOException | TransformerException | ParserConfigurationException |
|
||||
SAXException |
|
||||
ClassNotFoundException e) {
|
||||
} catch (SQLException
|
||||
| IOException
|
||||
| TransformerException
|
||||
| ParserConfigurationException
|
||||
| SAXException
|
||||
| ClassNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
@@ -118,8 +125,10 @@ class AppTest {
|
||||
/**
|
||||
* Tests the serialization of the input object using the {@link BlobSerializer} and persists the
|
||||
* serialized object to DB, then loads the object back from DB and deserializes it using the
|
||||
* {@link BlobSerializer}.<p>After loading the object back from DB the test matches the hash of
|
||||
* the input object with the hash of the object that was loaded from DB and deserialized.
|
||||
* {@link BlobSerializer}.
|
||||
*
|
||||
* <p>After loading the object back from DB the test matches the hash of the input object with the
|
||||
* hash of the object that was loaded from DB and deserialized.
|
||||
*/
|
||||
@Test
|
||||
void blobSerializerTest() {
|
||||
@@ -132,11 +141,16 @@ class AppTest {
|
||||
Object fromDb = serializer.loadFromDb(id, Forest.class.getSimpleName());
|
||||
Forest forestFromDb = serializer.deSerialize(fromDb);
|
||||
|
||||
Assertions.assertEquals(forest.hashCode(), forestFromDb.hashCode(),
|
||||
Assertions.assertEquals(
|
||||
forest.hashCode(),
|
||||
forestFromDb.hashCode(),
|
||||
"Hashes of objects after Serializing and Deserializing are the same");
|
||||
} catch (SQLException | IOException | TransformerException | ParserConfigurationException |
|
||||
SAXException |
|
||||
ClassNotFoundException e) {
|
||||
} catch (SQLException
|
||||
| IOException
|
||||
| TransformerException
|
||||
| ParserConfigurationException
|
||||
| SAXException
|
||||
| ClassNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user