From 1d42f7c9bdc879fff71806499584201caa4d590c Mon Sep 17 00:00:00 2001 From: DOHA Date: Mon, 26 Feb 2018 14:12:11 +0200 Subject: [PATCH] hamcrest text and file matchers --- guava/pom.xml | 8 ++ .../hamcrest/HamcrestFileUnitTest.java | 64 ++++++++++++ .../hamcrest/HamcrestTextUnitTest.java | 97 +++++++++++++++++++ 3 files changed, 169 insertions(+) create mode 100644 guava/src/test/java/org/baeldung/hamcrest/HamcrestFileUnitTest.java create mode 100644 guava/src/test/java/org/baeldung/hamcrest/HamcrestTextUnitTest.java diff --git a/guava/pom.xml b/guava/pom.xml index 4afb4da301..aa162b7d1b 100644 --- a/guava/pom.xml +++ b/guava/pom.xml @@ -43,6 +43,14 @@ ${assertj.version} test + + + org.hamcrest + java-hamcrest + 2.0.0.0 + test + + diff --git a/guava/src/test/java/org/baeldung/hamcrest/HamcrestFileUnitTest.java b/guava/src/test/java/org/baeldung/hamcrest/HamcrestFileUnitTest.java new file mode 100644 index 0000000000..652e315651 --- /dev/null +++ b/guava/src/test/java/org/baeldung/hamcrest/HamcrestFileUnitTest.java @@ -0,0 +1,64 @@ +package org.baeldung.hamcrest; + +import static org.hamcrest.core.StringContains.containsString; +import static org.hamcrest.io.FileMatchers.aFileNamed; +import static org.hamcrest.io.FileMatchers.aFileWithAbsolutePath; +import static org.hamcrest.io.FileMatchers.aFileWithCanonicalPath; +import static org.hamcrest.io.FileMatchers.aFileWithSize; +import static org.hamcrest.io.FileMatchers.aReadableFile; +import static org.hamcrest.io.FileMatchers.aWritableFile; +import static org.hamcrest.io.FileMatchers.anExistingDirectory; +import static org.hamcrest.io.FileMatchers.anExistingFile; +import static org.hamcrest.io.FileMatchers.anExistingFileOrDirectory; +import static org.hamcrest.number.OrderingComparison.greaterThan; +import static org.hamcrest.text.IsEqualIgnoringCase.equalToIgnoringCase; +import static org.junit.Assert.assertThat; + +import java.io.File; + +import org.junit.Test; + +public class HamcrestFileUnitTest { + + @Test + public final void whenVerifyingFileName_thenCorrect() { + File file = new File("src/test/resources/test1.in"); + + assertThat(file, aFileNamed(equalToIgnoringCase("test1.in"))); + } + + @Test + public final void whenVerifyingFileOrDirExist_thenCorrect() { + File file = new File("src/test/resources/test1.in"); + File dir = new File("src/test/resources"); + + assertThat(file, anExistingFile()); + assertThat(dir, anExistingDirectory()); + assertThat(file, anExistingFileOrDirectory()); + assertThat(dir, anExistingFileOrDirectory()); + } + + @Test + public final void whenVerifyingFileIsReadableAndWritable_thenCorrect() { + File file = new File("src/test/resources/test1.in"); + + assertThat(file, aReadableFile()); + assertThat(file, aWritableFile()); + } + + @Test + public final void whenVerifyingFileSize_thenCorrect() { + File file = new File("src/test/resources/test1.in"); + + assertThat(file, aFileWithSize(11)); + assertThat(file, aFileWithSize(greaterThan(1L)));; + } + + @Test + public final void whenVerifyingFilePath_thenCorrect() { + File file = new File("src/test/resources/test1.in"); + + assertThat(file, aFileWithCanonicalPath(containsString("src/test/resources"))); + assertThat(file, aFileWithAbsolutePath(containsString("src/test/resources"))); + } +} diff --git a/guava/src/test/java/org/baeldung/hamcrest/HamcrestTextUnitTest.java b/guava/src/test/java/org/baeldung/hamcrest/HamcrestTextUnitTest.java new file mode 100644 index 0000000000..e336b1bba3 --- /dev/null +++ b/guava/src/test/java/org/baeldung/hamcrest/HamcrestTextUnitTest.java @@ -0,0 +1,97 @@ +package org.baeldung.hamcrest; + +import static org.hamcrest.core.StringContains.containsString; +import static org.hamcrest.core.StringContains.containsStringIgnoringCase; +import static org.hamcrest.core.StringEndsWith.endsWith; +import static org.hamcrest.core.StringEndsWith.endsWithIgnoringCase; +import static org.hamcrest.core.StringStartsWith.startsWith; +import static org.hamcrest.core.StringStartsWith.startsWithIgnoringCase; +import static org.hamcrest.text.IsBlankString.blankOrNullString; +import static org.hamcrest.text.IsBlankString.blankString; +import static org.hamcrest.text.IsEmptyString.emptyOrNullString; +import static org.hamcrest.text.IsEmptyString.emptyString; +import static org.hamcrest.text.IsEqualIgnoringCase.equalToIgnoringCase; +import static org.hamcrest.text.IsEqualIgnoringWhiteSpace.equalToIgnoringWhiteSpace; +import static org.hamcrest.text.MatchesPattern.matchesPattern; +import static org.hamcrest.text.StringContainsInOrder.stringContainsInOrder; +import static org.junit.Assert.assertThat; + +import org.junit.Test; + +public class HamcrestTextUnitTest { + + @Test + public final void whenTwoStringsAreEqual_thenCorrect() { + String first = "hello"; + String second = "Hello"; + + assertThat(first, equalToIgnoringCase(second)); + } + + @Test + public final void whenTwoStringsAreEqualWithWhiteSpace_thenCorrect() { + String first = "hello"; + String second = " Hello "; + + assertThat(first, equalToIgnoringWhiteSpace(second)); + } + + @Test + public final void whenStringIsBlank_thenCorrect() { + String first = " "; + String second = null; + + assertThat(first, blankString()); + assertThat(first, blankOrNullString()); + assertThat(second, blankOrNullString()); + } + + @Test + public final void whenStringIsEmpty_thenCorrect() { + String first = ""; + String second = null; + + assertThat(first, emptyString()); + assertThat(first, emptyOrNullString()); + assertThat(second, emptyOrNullString()); + } + + @Test + public final void whenStringMatchPattern_thenCorrect() { + String first = "hello"; + + assertThat(first, matchesPattern("[a-z]+")); + } + + @Test + public final void whenVerifyStringContains_thenCorrect() { + String first = "hello"; + + assertThat(first, containsString("lo")); + assertThat(first, containsStringIgnoringCase("EL")); + } + + @Test + public final void whenVerifyStringContainsInOrder_thenCorrect() { + String first = "hello"; + + assertThat(first, stringContainsInOrder("e","l","o")); + } + + @Test + public final void whenVerifyStringStartsWith_thenCorrect() { + String first = "hello"; + + assertThat(first, startsWith("he")); + assertThat(first, startsWithIgnoringCase("HEL")); + } + + @Test + public final void whenVerifyStringEndsWith_thenCorrect() { + String first = "hello"; + + assertThat(first, endsWith("lo")); + assertThat(first, endsWithIgnoringCase("LO")); + } + +}