mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-06-04 16:14:20 +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:
@@ -35,7 +35,6 @@ import lombok.extern.slf4j.Slf4j;
|
||||
*
|
||||
* <p>In this example we have sentences composed of words composed of letters. All of the objects
|
||||
* can be treated through the same interface ({@link LetterComposite}).
|
||||
*
|
||||
*/
|
||||
@Slf4j
|
||||
public class App {
|
||||
|
||||
@@ -26,9 +26,7 @@ package com.iluwatar.composite;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
/**
|
||||
* Letter.
|
||||
*/
|
||||
/** Letter. */
|
||||
@RequiredArgsConstructor
|
||||
public class Letter extends LetterComposite {
|
||||
|
||||
|
||||
@@ -27,9 +27,7 @@ package com.iluwatar.composite;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Composite interface.
|
||||
*/
|
||||
/** Composite interface. */
|
||||
public abstract class LetterComposite {
|
||||
|
||||
private final List<LetterComposite> children = new ArrayList<>();
|
||||
@@ -42,15 +40,11 @@ public abstract class LetterComposite {
|
||||
return children.size();
|
||||
}
|
||||
|
||||
protected void printThisBefore() {
|
||||
}
|
||||
protected void printThisBefore() {}
|
||||
|
||||
protected void printThisAfter() {
|
||||
}
|
||||
protected void printThisAfter() {}
|
||||
|
||||
/**
|
||||
* Print.
|
||||
*/
|
||||
/** Print. */
|
||||
public void print() {
|
||||
printThisBefore();
|
||||
children.forEach(LetterComposite::print);
|
||||
|
||||
@@ -26,42 +26,37 @@ package com.iluwatar.composite;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Messenger.
|
||||
*/
|
||||
/** Messenger. */
|
||||
public class Messenger {
|
||||
|
||||
LetterComposite messageFromOrcs() {
|
||||
|
||||
var words = List.of(
|
||||
new Word('W', 'h', 'e', 'r', 'e'),
|
||||
new Word('t', 'h', 'e', 'r', 'e'),
|
||||
new Word('i', 's'),
|
||||
new Word('a'),
|
||||
new Word('w', 'h', 'i', 'p'),
|
||||
new Word('t', 'h', 'e', 'r', 'e'),
|
||||
new Word('i', 's'),
|
||||
new Word('a'),
|
||||
new Word('w', 'a', 'y')
|
||||
);
|
||||
var words =
|
||||
List.of(
|
||||
new Word('W', 'h', 'e', 'r', 'e'),
|
||||
new Word('t', 'h', 'e', 'r', 'e'),
|
||||
new Word('i', 's'),
|
||||
new Word('a'),
|
||||
new Word('w', 'h', 'i', 'p'),
|
||||
new Word('t', 'h', 'e', 'r', 'e'),
|
||||
new Word('i', 's'),
|
||||
new Word('a'),
|
||||
new Word('w', 'a', 'y'));
|
||||
|
||||
return new Sentence(words);
|
||||
|
||||
}
|
||||
|
||||
LetterComposite messageFromElves() {
|
||||
|
||||
var words = List.of(
|
||||
new Word('M', 'u', 'c', 'h'),
|
||||
new Word('w', 'i', 'n', 'd'),
|
||||
new Word('p', 'o', 'u', 'r', 's'),
|
||||
new Word('f', 'r', 'o', 'm'),
|
||||
new Word('y', 'o', 'u', 'r'),
|
||||
new Word('m', 'o', 'u', 't', 'h')
|
||||
);
|
||||
var words =
|
||||
List.of(
|
||||
new Word('M', 'u', 'c', 'h'),
|
||||
new Word('w', 'i', 'n', 'd'),
|
||||
new Word('p', 'o', 'u', 'r', 's'),
|
||||
new Word('f', 'r', 'o', 'm'),
|
||||
new Word('y', 'o', 'u', 'r'),
|
||||
new Word('m', 'o', 'u', 't', 'h'));
|
||||
|
||||
return new Sentence(words);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -26,14 +26,10 @@ package com.iluwatar.composite;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Sentence.
|
||||
*/
|
||||
/** Sentence. */
|
||||
public class Sentence extends LetterComposite {
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
/** Constructor. */
|
||||
public Sentence(List<Word> words) {
|
||||
words.forEach(this::add);
|
||||
}
|
||||
|
||||
@@ -26,20 +26,17 @@ package com.iluwatar.composite;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Word.
|
||||
*/
|
||||
/** Word. */
|
||||
public class Word extends LetterComposite {
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
/** Constructor. */
|
||||
public Word(List<Letter> letters) {
|
||||
letters.forEach(this::add);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param letters to include
|
||||
*/
|
||||
public Word(char... letters) {
|
||||
|
||||
@@ -27,20 +27,17 @@ package com.iluwatar.composite;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Application test
|
||||
*/
|
||||
/** Application test */
|
||||
class AppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App#main(String[])}
|
||||
* throws an exception.
|
||||
* <p>Solution: Inserted assertion to check whether the execution of the main method in {@link
|
||||
* App#main(String[])} throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
Assertions.assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
Assertions.assertDoesNotThrow(() -> App.main(new String[] {}));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,20 +33,13 @@ import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* MessengerTest
|
||||
*
|
||||
*/
|
||||
/** MessengerTest */
|
||||
class MessengerTest {
|
||||
|
||||
/**
|
||||
* The buffer used to capture every write to {@link System#out}
|
||||
*/
|
||||
/** The buffer used to capture every write to {@link System#out} */
|
||||
private ByteArrayOutputStream stdOutBuffer = new ByteArrayOutputStream();
|
||||
|
||||
/**
|
||||
* Keep the original std-out so it can be restored after the test
|
||||
*/
|
||||
/** Keep the original std-out so it can be restored after the test */
|
||||
private final PrintStream realStdOut = System.out;
|
||||
|
||||
/**
|
||||
@@ -58,43 +51,31 @@ class MessengerTest {
|
||||
System.setOut(new PrintStream(stdOutBuffer));
|
||||
}
|
||||
|
||||
/**
|
||||
* Removed the mocked std-out {@link PrintStream} again from the {@link System} class
|
||||
*/
|
||||
/** Removed the mocked std-out {@link PrintStream} again from the {@link System} class */
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
System.setOut(realStdOut);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the message from the orcs
|
||||
*/
|
||||
/** Test the message from the orcs */
|
||||
@Test
|
||||
void testMessageFromOrcs() {
|
||||
final var messenger = new Messenger();
|
||||
testMessage(
|
||||
messenger.messageFromOrcs(),
|
||||
"Where there is a whip there is a way."
|
||||
);
|
||||
testMessage(messenger.messageFromOrcs(), "Where there is a whip there is a way.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the message from the elves
|
||||
*/
|
||||
/** Test the message from the elves */
|
||||
@Test
|
||||
void testMessageFromElves() {
|
||||
final var messenger = new Messenger();
|
||||
testMessage(
|
||||
messenger.messageFromElves(),
|
||||
"Much wind pours from your mouth."
|
||||
);
|
||||
testMessage(messenger.messageFromElves(), "Much wind pours from your mouth.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if the given composed message matches the expected message
|
||||
*
|
||||
* @param composedMessage The composed message, received from the messenger
|
||||
* @param message The expected message
|
||||
* @param message The expected message
|
||||
*/
|
||||
private void testMessage(final LetterComposite composedMessage, final String message) {
|
||||
// Test is the composed message has the correct number of words
|
||||
@@ -108,5 +89,4 @@ class MessengerTest {
|
||||
// ... and verify if the message matches with the expected one
|
||||
assertEquals(message, new String(this.stdOutBuffer.toByteArray()).trim());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user