Merge pull request #3734 from Doha2012/master
hamcrest text and file matchers
This commit is contained in:
commit
2c8473a7e0
|
@ -43,6 +43,14 @@
|
|||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>java-hamcrest</artifactId>
|
||||
<version>2.0.0.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -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")));
|
||||
}
|
||||
}
|
|
@ -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"));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue