The Command Pattern in Java - BAEL-1722 (#4048)
* Initial Commit * Update pom.xml * Fix unit tests format * Fix unit test class name * Update OpenTextFileOPerationUnitTest.java * Delete OpenTextFileOPerationUnitTest.java * Delete OpenTextFileOPerationUnitTest.java * Add unit test class * Update TextFileOperationExecutorUnitTest.java * Update TextFileUnitTest.java
This commit is contained in:
parent
fd76b01251
commit
1e1142b145
@ -19,6 +19,18 @@
|
|||||||
<version>4.12</version>
|
<version>4.12</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hamcrest</groupId>
|
||||||
|
<artifactId>hamcrest-core</artifactId>
|
||||||
|
<version>1.3</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.assertj</groupId>
|
||||||
|
<artifactId>assertj-core</artifactId>
|
||||||
|
<version>3.8.0</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<properties>
|
<properties>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
@ -0,0 +1,19 @@
|
|||||||
|
package com.baeldung.pattern.command.client;
|
||||||
|
|
||||||
|
import com.baeldung.pattern.command.command.OpenTextFileOperation;
|
||||||
|
import com.baeldung.pattern.command.command.SaveTextFileOperation;
|
||||||
|
import com.baeldung.pattern.command.command.TextFileOperation;
|
||||||
|
import com.baeldung.pattern.command.invoker.TextFileOperationExecutor;
|
||||||
|
import com.baeldung.pattern.command.receiver.TextFile;
|
||||||
|
|
||||||
|
public class TextFileApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
TextFileOperation openTextFileOperation = new OpenTextFileOperation(new TextFile("file1.txt"));
|
||||||
|
TextFileOperation saveTextFileOperation = new SaveTextFileOperation(new TextFile("file2.txt"));
|
||||||
|
TextFileOperationExecutor textFileOperationExecutor = new TextFileOperationExecutor();
|
||||||
|
System.out.println(textFileOperationExecutor.executeOperation(openTextFileOperation));
|
||||||
|
System.out.println(textFileOperationExecutor.executeOperation(saveTextFileOperation));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package com.baeldung.pattern.command.command;
|
||||||
|
|
||||||
|
import com.baeldung.pattern.command.receiver.TextFile;
|
||||||
|
|
||||||
|
public class OpenTextFileOperation implements TextFileOperation {
|
||||||
|
|
||||||
|
private final TextFile textFile;
|
||||||
|
|
||||||
|
public OpenTextFileOperation(TextFile textFile) {
|
||||||
|
this.textFile = textFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String execute() {
|
||||||
|
return textFile.open();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package com.baeldung.pattern.command.command;
|
||||||
|
|
||||||
|
import com.baeldung.pattern.command.receiver.TextFile;
|
||||||
|
|
||||||
|
public class SaveTextFileOperation implements TextFileOperation {
|
||||||
|
|
||||||
|
private final TextFile textFile;
|
||||||
|
|
||||||
|
public SaveTextFileOperation(TextFile textFile) {
|
||||||
|
this.textFile = textFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String execute() {
|
||||||
|
return textFile.save();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package com.baeldung.pattern.command.command;
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface TextFileOperation {
|
||||||
|
|
||||||
|
String execute();
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.baeldung.pattern.command.invoker;
|
||||||
|
|
||||||
|
import com.baeldung.pattern.command.command.TextFileOperation;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class TextFileOperationExecutor {
|
||||||
|
|
||||||
|
private final List<TextFileOperation> textFileOperations = new ArrayList<>();
|
||||||
|
|
||||||
|
public String executeOperation(TextFileOperation textFileOperation) {
|
||||||
|
textFileOperations.add(textFileOperation);
|
||||||
|
return textFileOperation.execute();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
package com.baeldung.pattern.command.receiver;
|
||||||
|
|
||||||
|
public class TextFile {
|
||||||
|
|
||||||
|
private final String name;
|
||||||
|
|
||||||
|
public TextFile(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String open() {
|
||||||
|
return "Opening file " + name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String read() {
|
||||||
|
return "Reading file " + name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String write() {
|
||||||
|
return "Writing to file " + name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String save() {
|
||||||
|
return "Saving file " + name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String copy() {
|
||||||
|
return "Copying file " + name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String paste() {
|
||||||
|
return "Pasting file " + name;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
package com.baeldung.pattern.command.test;
|
||||||
|
|
||||||
|
import com.baeldung.pattern.command.command.OpenTextFileOperation;
|
||||||
|
import com.baeldung.pattern.command.command.TextFileOperation;
|
||||||
|
import com.baeldung.pattern.command.receiver.TextFile;
|
||||||
|
import org.junit.Test;
|
||||||
|
import static org.assertj.core.api.Assertions.*;
|
||||||
|
|
||||||
|
public class OpenTextFileOperationUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenOpenTextFileOperationIntance_whenCalledExecuteMethod_thenOneAssertion() {
|
||||||
|
TextFileOperation openTextFileOperation = new OpenTextFileOperation(new TextFile("file1.txt"));
|
||||||
|
assertThat(openTextFileOperation.execute()).isEqualTo("Opening file file1.txt");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
package com.baeldung.pattern.command.test;
|
||||||
|
|
||||||
|
import com.baeldung.pattern.command.command.SaveTextFileOperation;
|
||||||
|
import com.baeldung.pattern.command.command.TextFileOperation;
|
||||||
|
import com.baeldung.pattern.command.receiver.TextFile;
|
||||||
|
import org.junit.Test;
|
||||||
|
import static org.assertj.core.api.Assertions.*;
|
||||||
|
|
||||||
|
public class SaveTextFileOperationUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenSaveTextFileOperationIntance_whenCalledExecuteMethod_thenOneAssertion() {
|
||||||
|
TextFileOperation openTextFileOperation = new SaveTextFileOperation(new TextFile("file1.txt"));
|
||||||
|
assertThat(openTextFileOperation.execute()).isEqualTo("Saving file file1.txt");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,68 @@
|
|||||||
|
package com.baeldung.pattern.command.test;
|
||||||
|
|
||||||
|
import com.baeldung.pattern.command.command.OpenTextFileOperation;
|
||||||
|
import com.baeldung.pattern.command.command.SaveTextFileOperation;
|
||||||
|
import com.baeldung.pattern.command.command.TextFileOperation;
|
||||||
|
import com.baeldung.pattern.command.invoker.TextFileOperationExecutor;
|
||||||
|
import com.baeldung.pattern.command.receiver.TextFile;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import org.junit.Test;
|
||||||
|
import static org.assertj.core.api.Assertions.*;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
|
||||||
|
public class TextFileOperationExecutorUnitTest {
|
||||||
|
|
||||||
|
private static TextFileOperationExecutor textFileOperationExecutor;
|
||||||
|
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void setUpTextFileOperationExecutor() {
|
||||||
|
textFileOperationExecutor = new TextFileOperationExecutor();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTextFileOPerationExecutorInstance_whenCalledexecuteOperationWithOpenTextOperation_thenOneAssertion() {
|
||||||
|
TextFileOperation textFileOperation = new OpenTextFileOperation(new TextFile("file1.txt"));
|
||||||
|
assertThat(textFileOperationExecutor.executeOperation(textFileOperation)).isEqualTo("Opening file file1.txt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTextFileOPerationExecutorInstance_whenCalledexecuteOperationWithSaveTextOperation_thenOneAssertion() {
|
||||||
|
TextFileOperation textFileOperation = new SaveTextFileOperation(new TextFile("file1.txt"));
|
||||||
|
assertThat(textFileOperationExecutor.executeOperation(textFileOperation)).isEqualTo("Saving file file1.txt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTextFileOperationExecutorInstance_whenCalledexecuteOperationWithTextFileOpenLambda_thenOneAssertion() {
|
||||||
|
assertThat(textFileOperationExecutor.executeOperation(() -> {return "Opening file file1.txt";})).isEqualTo("Opening file file1.txt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTextFileOperationExecutorInstance_whenCalledexecuteOperationWithTextFileSaveLambda_thenOneAssertion() {
|
||||||
|
assertThat(textFileOperationExecutor.executeOperation(() -> {return "Saving file file1.txt";})).isEqualTo("Saving file file1.txt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTextFileOperationExecutorInstance_whenCalledexecuteOperationWithTextFileOpenMethodReferenceOfExistingObject_thenOneAssertion() {
|
||||||
|
TextFile textFile = new TextFile("file1.txt");
|
||||||
|
assertThat(textFileOperationExecutor.executeOperation(textFile::open)).isEqualTo("Opening file file1.txt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTextFileOperationExecutorInstance_whenCalledexecuteOperationWithTextFileSaveMethodReferenceOfExistingObject_thenOneAssertion() {
|
||||||
|
TextFile textFile = new TextFile("file1.txt");
|
||||||
|
assertThat(textFileOperationExecutor.executeOperation(textFile::save)).isEqualTo("Saving file file1.txt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenOpenTextFileOperationExecuteMethodReference_whenCalledApplyMethod_thenOneAssertion() {
|
||||||
|
Function<OpenTextFileOperation, String> executeMethodReference = OpenTextFileOperation::execute;
|
||||||
|
assertThat(executeMethodReference.apply(new OpenTextFileOperation(new TextFile("file1.txt")))).isEqualTo("Opening file file1.txt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenSaveTextFileOperationExecuteMethodReference_whenCalledApplyMethod_thenOneAssertion() {
|
||||||
|
Function<SaveTextFileOperation, String> executeMethodReference = SaveTextFileOperation::execute;
|
||||||
|
assertThat(executeMethodReference.apply(new SaveTextFileOperation(new TextFile("file1.txt")))).isEqualTo("Saving file file1.txt");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,42 @@
|
|||||||
|
package com.baeldung.pattern.command.test;
|
||||||
|
|
||||||
|
import com.baeldung.pattern.command.receiver.TextFile;
|
||||||
|
import org.junit.Test;
|
||||||
|
import static org.assertj.core.api.Assertions.*;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
|
||||||
|
public class TextFileUnitTest {
|
||||||
|
|
||||||
|
private static TextFile textFile;
|
||||||
|
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void setUpTextFileInstance() {
|
||||||
|
textFile = new TextFile("file1.txt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTextFileInstance_whenCalledopenMethod_thenOneAssertion() {
|
||||||
|
assertThat(textFile.open()).isEqualTo("Opening file file1.txt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTextFileInstance_whenCalledwriteMethod_thenOneAssertion() {
|
||||||
|
assertThat(textFile.write()).isEqualTo("Writing to file file1.txt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTextFileInstance_whenCalledsaveMethod_thenOneAssertion() {
|
||||||
|
assertThat(textFile.save()).isEqualTo("Saving file file1.txt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTextFileInstance_whenCalledcopyMethod_thenOneAssertion() {
|
||||||
|
assertThat(textFile.copy()).isEqualTo("Copying file file1.txt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTextFileInstance_whenCalledpasteMethod_thenOneAssertion() {
|
||||||
|
assertThat(textFile.paste()).isEqualTo("Pasting file file1.txt");
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user