|
| 1 | +package com.baeldung.assertj.introduction; |
| 2 | + |
| 3 | +import com.google.common.base.Optional; |
| 4 | +import com.google.common.collect.HashBasedTable; |
| 5 | +import com.google.common.collect.ImmutableMap; |
| 6 | +import com.google.common.collect.Multimaps; |
| 7 | +import com.google.common.collect.Range; |
| 8 | +import com.google.common.collect.SetMultimap; |
| 9 | +import com.google.common.collect.TreeRangeMap; |
| 10 | +import com.google.common.io.Files; |
| 11 | +import org.assertj.guava.data.MapEntry; |
| 12 | +import org.junit.Test; |
| 13 | + |
| 14 | +import java.io.File; |
| 15 | + |
| 16 | +import static org.assertj.guava.api.Assertions.assertThat; |
| 17 | +import static org.assertj.guava.api.Assertions.entry; |
| 18 | + |
| 19 | +public class AssertJGuavaTest { |
| 20 | + |
| 21 | + @Test |
| 22 | + public void givenTwoEmptyFiles_whenComparingContent_thenEqual() throws Exception { |
| 23 | + final File temp = File.createTempFile("bael", "dung"); |
| 24 | + final File temp2 = File.createTempFile("bael", "dung2"); |
| 25 | + |
| 26 | + assertThat(Files.asByteSource(temp)) |
| 27 | + .hasSize(0) |
| 28 | + .hasSameContentAs(Files.asByteSource(temp2)); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + public void givenMultimap_whenVerifying_thenCorrect() throws Exception { |
| 33 | + final SetMultimap<Object, Object> mmap = Multimaps.forMap(ImmutableMap.of(1, "one")); |
| 34 | + |
| 35 | + assertThat(mmap) |
| 36 | + .hasSize(1) |
| 37 | + .containsKeys(1) |
| 38 | + .contains(entry(1, "one")); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void givenOptional_whenVerifyingContent_thenShouldBeEqual() throws Exception { |
| 43 | + final Optional<String> something = Optional.of("something"); |
| 44 | + |
| 45 | + assertThat(something) |
| 46 | + .isPresent() |
| 47 | + .extractingValue() |
| 48 | + .isEqualTo("something"); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void givenRange_whenVerifying_thenShouldBeCorrect() throws Exception { |
| 53 | + final Range<String> range = Range.openClosed("a", "g"); |
| 54 | + |
| 55 | + assertThat(range) |
| 56 | + .hasOpenedLowerBound() |
| 57 | + .isNotEmpty() |
| 58 | + .hasClosedUpperBound() |
| 59 | + .contains("b"); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void givenRangeMap_whenVerifying_thenShouldBeCorrect() throws Exception { |
| 64 | + final TreeRangeMap<Integer, String> map = TreeRangeMap.create(); |
| 65 | + |
| 66 | + map.put(Range.closed(0, 60), "F"); |
| 67 | + map.put(Range.closed(61, 70), "D"); |
| 68 | + |
| 69 | + assertThat(map) |
| 70 | + .isNotEmpty() |
| 71 | + .containsKeys(0) |
| 72 | + .contains(MapEntry.entry(34, "F")); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void givenTable_whenVerifying_thenShouldBeCorrect() throws Exception { |
| 77 | + final HashBasedTable<Integer, String, String> table = HashBasedTable.create(2, 2); |
| 78 | + |
| 79 | + table.put(1, "A", "PRESENT"); |
| 80 | + table.put(1, "B", "ABSENT"); |
| 81 | + |
| 82 | + assertThat(table) |
| 83 | + .hasRowCount(1) |
| 84 | + .containsValues("ABSENT") |
| 85 | + .containsCell(1, "B", "ABSENT"); |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | +} |
0 commit comments