forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master'
- Loading branch information
Showing
204 changed files
with
8,568 additions
and
893 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.baeldung</groupId> | ||
<artifactId>assertj</artifactId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.guava</groupId> | ||
<artifactId>guava</artifactId> | ||
<version>19.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.12</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.assertj</groupId> | ||
<artifactId>assertj-core</artifactId> | ||
<version>3.4.1</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.assertj</groupId> | ||
<artifactId>assertj-guava</artifactId> | ||
<version>3.0.0</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.3</version> | ||
<configuration> | ||
<source>1.8</source> | ||
<target>1.8</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
19 changes: 19 additions & 0 deletions
19
assertj/src/main/java/com/baeldung/assertj/introduction/domain/Dog.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.baeldung.assertj.introduction.domain; | ||
|
||
public class Dog { | ||
private String name; | ||
private Float weight; | ||
|
||
public Dog(String name, Float weight) { | ||
this.name = name; | ||
this.weight = weight; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public Float getWeight() { | ||
return weight; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
assertj/src/main/java/com/baeldung/assertj/introduction/domain/Person.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.baeldung.assertj.introduction.domain; | ||
|
||
public class Person { | ||
private String name; | ||
private Integer age; | ||
|
||
public Person(String name, Integer age) { | ||
this.name = name; | ||
this.age = age; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public Integer getAge() { | ||
return age; | ||
} | ||
} |
143 changes: 143 additions & 0 deletions
143
assertj/src/test/java/com/baeldung/assertj/introduction/AssertJCoreTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package com.baeldung.assertj.introduction; | ||
|
||
import com.baeldung.assertj.introduction.domain.Dog; | ||
import com.baeldung.assertj.introduction.domain.Person; | ||
import org.assertj.core.util.Maps; | ||
import org.junit.Ignore; | ||
import org.junit.Test; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.File; | ||
import java.io.InputStream; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.NoSuchElementException; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.entry; | ||
import static org.assertj.core.api.Assertions.withPrecision; | ||
|
||
public class AssertJCoreTest { | ||
|
||
@Test | ||
public void whenComparingReferences_thenNotEqual() throws Exception { | ||
Dog fido = new Dog("Fido", 5.15f); | ||
Dog fidosClone = new Dog("Fido", 5.15f); | ||
|
||
assertThat(fido).isNotEqualTo(fidosClone); | ||
} | ||
|
||
@Test | ||
public void whenComparingFields_thenEqual() throws Exception { | ||
Dog fido = new Dog("Fido", 5.15f); | ||
Dog fidosClone = new Dog("Fido", 5.15f); | ||
|
||
assertThat(fido).isEqualToComparingFieldByFieldRecursively(fidosClone); | ||
} | ||
|
||
@Test | ||
public void whenCheckingForElement_thenContains() throws Exception { | ||
List<String> list = Arrays.asList("1", "2", "3"); | ||
|
||
assertThat(list) | ||
.contains("1"); | ||
} | ||
|
||
@Test | ||
public void whenCheckingForElement_thenMultipleAssertions() throws Exception { | ||
List<String> list = Arrays.asList("1", "2", "3"); | ||
|
||
assertThat(list).isNotEmpty(); | ||
assertThat(list).startsWith("1"); | ||
assertThat(list).doesNotContainNull(); | ||
|
||
assertThat(list) | ||
.isNotEmpty() | ||
.contains("1") | ||
.startsWith("1") | ||
.doesNotContainNull() | ||
.containsSequence("2", "3"); | ||
} | ||
|
||
@Test | ||
public void whenCheckingRunnable_thenIsInterface() throws Exception { | ||
assertThat(Runnable.class).isInterface(); | ||
} | ||
|
||
@Test | ||
public void whenCheckingCharacter_thenIsUnicode() throws Exception { | ||
char someCharacter = 'c'; | ||
|
||
assertThat(someCharacter) | ||
.isNotEqualTo('a') | ||
.inUnicode() | ||
.isGreaterThanOrEqualTo('b') | ||
.isLowerCase(); | ||
} | ||
|
||
@Test | ||
public void whenAssigningNSEExToException_thenIsAssignable() throws Exception { | ||
assertThat(Exception.class).isAssignableFrom(NoSuchElementException.class); | ||
} | ||
|
||
@Test | ||
public void whenComparingWithOffset_thenEquals() throws Exception { | ||
assertThat(5.1).isEqualTo(5, withPrecision(1d)); | ||
} | ||
|
||
@Test | ||
public void whenCheckingString_then() throws Exception { | ||
assertThat("".isEmpty()).isTrue(); | ||
} | ||
|
||
@Test | ||
public void whenCheckingFile_then() throws Exception { | ||
final File someFile = File.createTempFile("aaa", "bbb"); | ||
someFile.deleteOnExit(); | ||
|
||
assertThat(someFile) | ||
.exists() | ||
.isFile() | ||
.canRead() | ||
.canWrite(); | ||
} | ||
|
||
@Test | ||
public void whenCheckingIS_then() throws Exception { | ||
InputStream given = new ByteArrayInputStream("foo".getBytes()); | ||
InputStream expected = new ByteArrayInputStream("foo".getBytes()); | ||
|
||
assertThat(given).hasSameContentAs(expected); | ||
} | ||
|
||
@Test | ||
public void whenGivenMap_then() throws Exception { | ||
Map<Integer, String> map = Maps.newHashMap(2, "a"); | ||
|
||
assertThat(map) | ||
.isNotEmpty() | ||
.containsKey(2) | ||
.doesNotContainKeys(10) | ||
.contains(entry(2, "a")); | ||
} | ||
|
||
@Test | ||
public void whenGivenException_then() throws Exception { | ||
Exception ex = new Exception("abc"); | ||
|
||
assertThat(ex) | ||
.hasNoCause() | ||
.hasMessageEndingWith("c"); | ||
} | ||
|
||
@Ignore // IN ORDER TO TEST, REMOVE THIS LINE | ||
@Test | ||
public void whenRunningAssertion_thenDescribed() throws Exception { | ||
Person person = new Person("Alex", 34); | ||
|
||
assertThat(person.getAge()) | ||
.as("%s's age should be equal to 100") | ||
.isEqualTo(100); | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
assertj/src/test/java/com/baeldung/assertj/introduction/AssertJGuavaTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package com.baeldung.assertj.introduction; | ||
|
||
import com.google.common.base.Optional; | ||
import com.google.common.collect.HashBasedTable; | ||
import com.google.common.collect.Multimap; | ||
import com.google.common.collect.Multimaps; | ||
import com.google.common.collect.Range; | ||
import com.google.common.collect.Sets; | ||
import com.google.common.collect.Table; | ||
import com.google.common.collect.TreeRangeMap; | ||
import com.google.common.io.Files; | ||
import org.assertj.guava.data.MapEntry; | ||
import org.junit.Test; | ||
|
||
import java.io.File; | ||
import java.util.HashMap; | ||
|
||
import static org.assertj.guava.api.Assertions.assertThat; | ||
import static org.assertj.guava.api.Assertions.entry; | ||
|
||
public class AssertJGuavaTest { | ||
|
||
@Test | ||
public void givenTwoEmptyFiles_whenComparingContent_thenEqual() throws Exception { | ||
final File temp = File.createTempFile("bael", "dung"); | ||
final File temp2 = File.createTempFile("bael", "dung2"); | ||
|
||
assertThat(Files.asByteSource(temp)) | ||
.hasSize(0) | ||
.hasSameContentAs(Files.asByteSource(temp2)); | ||
} | ||
|
||
@Test | ||
public void givenMultimap_whenVerifying_thenCorrect() throws Exception { | ||
final Multimap<Integer, String> mmap = Multimaps.newMultimap(new HashMap<>(), Sets::newHashSet); | ||
mmap.put(1, "one"); | ||
mmap.put(1, "1"); | ||
|
||
assertThat(mmap) | ||
.hasSize(2) | ||
.containsKeys(1) | ||
.contains(entry(1, "one")) | ||
.contains(entry(1, "1")); | ||
} | ||
|
||
@Test | ||
public void givenOptional_whenVerifyingContent_thenShouldBeEqual() throws Exception { | ||
final Optional<String> something = Optional.of("something"); | ||
|
||
assertThat(something) | ||
.isPresent() | ||
.extractingValue() | ||
.isEqualTo("something"); | ||
} | ||
|
||
@Test | ||
public void givenRange_whenVerifying_thenShouldBeCorrect() throws Exception { | ||
final Range<String> range = Range.openClosed("a", "g"); | ||
|
||
assertThat(range) | ||
.hasOpenedLowerBound() | ||
.isNotEmpty() | ||
.hasClosedUpperBound() | ||
.contains("b"); | ||
} | ||
|
||
@Test | ||
public void givenRangeMap_whenVerifying_thenShouldBeCorrect() throws Exception { | ||
final TreeRangeMap<Integer, String> map = TreeRangeMap.create(); | ||
|
||
map.put(Range.closed(0, 60), "F"); | ||
map.put(Range.closed(61, 70), "D"); | ||
|
||
assertThat(map) | ||
.isNotEmpty() | ||
.containsKeys(0) | ||
.contains(MapEntry.entry(34, "F")); | ||
} | ||
|
||
@Test | ||
public void givenTable_whenVerifying_thenShouldBeCorrect() throws Exception { | ||
final Table<Integer, String, String> table = HashBasedTable.create(2, 2); | ||
|
||
table.put(1, "A", "PRESENT"); | ||
table.put(1, "B", "ABSENT"); | ||
|
||
assertThat(table) | ||
.hasRowCount(1) | ||
.containsValues("ABSENT") | ||
.containsCell(1, "B", "ABSENT"); | ||
} | ||
|
||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
core-java-8/src/test/java/com/baeldung/dateapi/ConversionExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.baeldung.dateapi; | ||
|
||
import java.time.Instant; | ||
import java.time.ZoneId; | ||
import java.time.ZonedDateTime; | ||
import java.util.Date; | ||
import java.util.GregorianCalendar; | ||
import java.util.TimeZone; | ||
|
||
public class ConversionExample { | ||
public static void main(String[] args) { | ||
Instant instantFromCalendar = GregorianCalendar.getInstance().toInstant(); | ||
ZonedDateTime zonedDateTimeFromCalendar = new GregorianCalendar().toZonedDateTime(); | ||
Date dateFromInstant = Date.from(Instant.now()); | ||
GregorianCalendar calendarFromZonedDateTime = GregorianCalendar.from(ZonedDateTime.now()); | ||
Instant instantFromDate = new Date().toInstant(); | ||
ZoneId zoneIdFromTimeZone = TimeZone.getTimeZone("PST").toZoneId(); | ||
} | ||
} |
Oops, something went wrong.