From 361ed655d936b9ded9b88bdf4d6e50a60e741f27 Mon Sep 17 00:00:00 2001 From: watcharapong Date: Sat, 17 Feb 2024 17:08:25 +0700 Subject: [PATCH] Test commit --- .idea/.gitignore | 3 + .idea/.name | 1 + .idea/gradle.xml | 16 +++ .idea/misc.xml | 5 + .idea/vcs.xml | 6 + git-hooks/pre-commit.sh | 2 +- src/main/java/BirthdayChecker.java | 16 +-- src/main/java/Note.java | 74 ++++++------ src/main/java/TextFile.java | 76 ++++++------ src/test/java/NoteTest.java | 186 ++++++++++++++--------------- 10 files changed, 208 insertions(+), 177 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/.name create mode 100644 .idea/gradle.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..eaf91e2 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..606cc6e --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +chapter-10 \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..167d83b --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,16 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..56684c1 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..9661ac7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/git-hooks/pre-commit.sh b/git-hooks/pre-commit.sh index e8e3042..0bee225 100644 --- a/git-hooks/pre-commit.sh +++ b/git-hooks/pre-commit.sh @@ -1 +1 @@ -.gradlew test \ No newline at end of file +gradlew spotlessCheck test \ No newline at end of file diff --git a/src/main/java/BirthdayChecker.java b/src/main/java/BirthdayChecker.java index d09d159..b1f5e5f 100644 --- a/src/main/java/BirthdayChecker.java +++ b/src/main/java/BirthdayChecker.java @@ -1,8 +1,8 @@ -import java.time.LocalDateTime; - -public class BirthdayChecker { - boolean isBirthday() { - LocalDateTime today = LocalDateTime.now(); - return today.getDayOfMonth() == 1 && today.getMonthValue() == 1; - } -} +import java.time.LocalDateTime; + +public class BirthdayChecker { + boolean isBirthday() { + LocalDateTime today = LocalDateTime.now(); + return today.getDayOfMonth() == 1 && today.getMonthValue() == 1; + } +} diff --git a/src/main/java/Note.java b/src/main/java/Note.java index 77d8c04..aef836f 100644 --- a/src/main/java/Note.java +++ b/src/main/java/Note.java @@ -1,37 +1,37 @@ -import io.reactivex.rxjava3.core.Completable; -import io.reactivex.rxjava3.core.Scheduler; - -public class Note { - private final String DEFAULT_FILE_NAME = "note.txt"; - private final TextFile textFile; - private BirthdayChecker birthdayChecker; - - public Note(TextFile textFile) { - this.textFile = textFile; - this.create(); - } - - public Note(TextFile textFile, BirthdayChecker birthdayChecker) { - this.textFile = textFile; - this.birthdayChecker = birthdayChecker; - } - - private void create() { - textFile.create(DEFAULT_FILE_NAME); - } - - public void write(String content) { - if (birthdayChecker != null && birthdayChecker.isBirthday()) { - content += " 🎂"; - } - textFile.write(DEFAULT_FILE_NAME, content); - } - - public Completable writeAsync(String content, Scheduler scheduler) { - return Completable.fromAction(() -> write(content)).subscribeOn(scheduler); - } - - public String read() { - return textFile.read(DEFAULT_FILE_NAME); - } -} +import io.reactivex.rxjava3.core.Completable; +import io.reactivex.rxjava3.core.Scheduler; + +public class Note { + private final String DEFAULT_FILE_NAME = "note.txt"; + private final TextFile textFile; + private BirthdayChecker birthdayChecker; + + public Note(TextFile textFile) { + this.textFile = textFile; + this.create(); + } + + public Note(TextFile textFile, BirthdayChecker birthdayChecker) { + this.textFile = textFile; + this.birthdayChecker = birthdayChecker; + } + + private void create() { + textFile.create(DEFAULT_FILE_NAME); + } + + public void write(String content) { + if (birthdayChecker != null && birthdayChecker.isBirthday()) { + content += " 🎂"; + } + textFile.write(DEFAULT_FILE_NAME, content); + } + + public Completable writeAsync(String content, Scheduler scheduler) { + return Completable.fromAction(() -> write(content)).subscribeOn(scheduler); + } + + public String read() { + return textFile.read(DEFAULT_FILE_NAME); + } +} diff --git a/src/main/java/TextFile.java b/src/main/java/TextFile.java index a4c9677..1f559f1 100644 --- a/src/main/java/TextFile.java +++ b/src/main/java/TextFile.java @@ -1,38 +1,38 @@ -import java.io.FileReader; -import java.io.FileWriter; - -public class TextFile { - - public void create(String fileName) { - try { - FileWriter fileWriter = new FileWriter(fileName); - fileWriter.close(); - } catch (Exception ignored) { - - } - } - - public void write(String fileName, String content) { - try { - FileWriter fileWriter = new FileWriter(fileName, true); - fileWriter.write(content); - fileWriter.write("\n"); - fileWriter.close(); - } catch (Exception ignored) { - } - } - - public String read(String fileName) { - StringBuilder content = new StringBuilder(); - try { - FileReader fileReader = new FileReader(fileName); - int i; - while ((i = fileReader.read()) != -1) { - content.append((char) i); - } - fileReader.close(); - } catch (Exception ignored) { - } - return content.toString().trim(); - } -} +import java.io.FileReader; +import java.io.FileWriter; + +public class TextFile { + + public void create(String fileName) { + try { + FileWriter fileWriter = new FileWriter(fileName); + fileWriter.close(); + } catch (Exception ignored) { + + } + } + + public void write(String fileName, String content) { + try { + FileWriter fileWriter = new FileWriter(fileName, true); + fileWriter.write(content); + fileWriter.write("\n"); + fileWriter.close(); + } catch (Exception ignored) { + } + } + + public String read(String fileName) { + StringBuilder content = new StringBuilder(); + try { + FileReader fileReader = new FileReader(fileName); + int i; + while ((i = fileReader.read()) != -1) { + content.append((char) i); + } + fileReader.close(); + } catch (Exception ignored) { + } + return content.toString().trim(); + } +} diff --git a/src/test/java/NoteTest.java b/src/test/java/NoteTest.java index 32af7cd..1bc3907 100644 --- a/src/test/java/NoteTest.java +++ b/src/test/java/NoteTest.java @@ -1,93 +1,93 @@ -import io.reactivex.rxjava3.schedulers.Schedulers; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.*; - -class NoteTest { - - @Test - @DisplayName("given initial note, my note should be empty") - void initialNoteShouldBeEmpty() { - TextFile textFile = mock(TextFile.class); - when(textFile.read(any())).thenReturn(""); - Note note = new Note(textFile); - - String expected = ""; - String actual = note.read(); - assertEquals(expected, actual); - } - - @Test - @DisplayName("create reading book note, my note should return reading book") - void createReadingNote() { - TextFile textFile = mock(TextFile.class); - when(textFile.read(any())).thenReturn("Reading book"); - Note note = new Note(textFile); - - note.write("Reading book"); - - String expected = "Reading book"; - assertEquals(expected, note.read()); - } - - @Test - @DisplayName("my default note file should be note.txt") - void defaultNoteFile() { - TextFile textFile = mock(TextFile.class); - Note note = new Note(textFile); - - String expected = "note.txt"; - verify(textFile).create(expected); - } - - @Test - @DisplayName("initial note should create note.txt only one file") - void createNoteFileOnlyOneTime() { - TextFile textFile = mock(TextFile.class); - Note note = new Note(textFile); - - int expected = 1; - verify(textFile, times(1)).create(any()); - } - - @Test - @DisplayName("write reading note should call write method") - void writeNote() { - TextFile textFile = mock(TextFile.class); - Note note = new Note(textFile); - - note.write("Reading book"); - - verify(textFile).write("note.txt", "Reading book"); - } - - @Test - @DisplayName("given today is my birthday write reading note and my note should contain 🎂") - void writeNoteOnMyBirthday() { - TextFile textFile = mock(TextFile.class); - BirthdayChecker birthdayChecker = mock(BirthdayChecker.class); - when(birthdayChecker.isBirthday()).thenReturn(true); - Note note = new Note(textFile, birthdayChecker); - - note.write("Reading book"); - - String expected = "Reading book"; - verify(textFile).write("note.txt", expected); - } - - @Test - @DisplayName("given write reading note async should success") - void writeAsyncNoteShouldSuccess() { - TextFile textFile = mock(TextFile.class); - BirthdayChecker birthdayChecker = mock(BirthdayChecker.class); - when(birthdayChecker.isBirthday()).thenReturn(true); - Note note = new Note(textFile, birthdayChecker); - - note.writeAsync("Reading book", Schedulers.trampoline()) - .test() - .assertComplete(); - } -} +import io.reactivex.rxjava3.schedulers.Schedulers; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; + +class NoteTest { + + @Test + @DisplayName("given initial note, my note should be empty") + void initialNoteShouldBeEmpty() { + TextFile textFile = mock(TextFile.class); + when(textFile.read(any())).thenReturn(""); + Note note = new Note(textFile); + + String expected = ""; + String actual = note.read(); + assertEquals(expected, actual); + } + + @Test + @DisplayName("create reading book note, my note should return reading book") + void createReadingNote() { + TextFile textFile = mock(TextFile.class); + when(textFile.read(any())).thenReturn("Reading book"); + Note note = new Note(textFile); + + note.write("Reading book"); + + String expected = "Reading book"; + assertEquals(expected, note.read()); + } + + @Test + @DisplayName("my default note file should be note.txt") + void defaultNoteFile() { + TextFile textFile = mock(TextFile.class); + Note note = new Note(textFile); + + String expected = "note.txt"; + verify(textFile).create(expected); + } + + @Test + @DisplayName("initial note should create note.txt only one file") + void createNoteFileOnlyOneTime() { + TextFile textFile = mock(TextFile.class); + Note note = new Note(textFile); + + int expected = 1; + verify(textFile, times(1)).create(any()); + } + + @Test + @DisplayName("write reading note should call write method") + void writeNote() { + TextFile textFile = mock(TextFile.class); + Note note = new Note(textFile); + + note.write("Reading book"); + + verify(textFile).write("note.txt", "Reading book"); + } + + @Test + @DisplayName("given today is my birthday write reading note and my note should contain 🎂") + void writeNoteOnMyBirthday() { + TextFile textFile = mock(TextFile.class); + BirthdayChecker birthdayChecker = mock(BirthdayChecker.class); + when(birthdayChecker.isBirthday()).thenReturn(true); + Note note = new Note(textFile, birthdayChecker); + + note.write("Reading book"); + + String expected = "Reading book 🎂"; + verify(textFile).write("note.txt", expected); + } + + @Test + @DisplayName("given write reading note async should success") + void writeAsyncNoteShouldSuccess() { + TextFile textFile = mock(TextFile.class); + BirthdayChecker birthdayChecker = mock(BirthdayChecker.class); + when(birthdayChecker.isBirthday()).thenReturn(true); + Note note = new Note(textFile, birthdayChecker); + + note.writeAsync("Reading book", Schedulers.trampoline()) + .test() + .assertComplete(); + } +}