BAEL-588 Guava table (#1066)

* BAL-36 File size api in java and apache commons IO

* BAEL-282 grep in java - fixes after code review

* BAEL-519 Added support for disruptor library

* BAEL-519 Added support for disruptor library

* BAEL-519 Added support for disruptor library

* BAEL-519 Added support for disruptor library

* BAEL-519 Added support for disruptor library

* BAEL-519 Added support for disruptor library

* BAEL-519 Added support for disruptor

* BAEL-519 Moved all supporting classes to main source

* BAEL-519 Moved all supporting classes to main source

* BAEL-519 Moved asserts and test classes in test folder.

* BAEL-519 moved test related producer and consumer to src.

* BAEL-586 Guide to Guava BiMap.

* BAEL-587 formatted code.

* BAEL-519 LMAX Disruptor

* BAEL-587 resolved merge

* BAEL-587 Resolved merge

* BAEL-519 Removed disruptor link.

* BAEL-519 Reverted Guava changes

* RFQ-587 Added disruptor as a separate module.

* BAEL-519 Disruptor changes.

* BAEL-519 Removed disruptor from core-java module.

* BAEL-588 guide to guava's Table

* BAEL-588 Guide to Guava's Table.

* BAEL-588 Added support for assertJ.

* BAEL-588 Test to support Guava's Table.

* BAEL-588 Added test to support Guava's Table.
This commit is contained in:
Muhammed Almas 2017-01-30 02:43:30 +05:30 committed by Zeger Hendrikse
parent 1c4fc19868
commit beae8eb58c
1 changed files with 173 additions and 0 deletions

View File

@ -0,0 +1,173 @@
package org.baeldung.guava;
import static org.assertj.core.api.Assertions.*;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.junit.Test;
import com.google.common.collect.ArrayTable;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.ImmutableTable;
import com.google.common.collect.Lists;
import com.google.common.collect.Table;
import com.google.common.collect.TreeBasedTable;
public class GuavaTableTest {
@Test
public void givenTable_whenGet_returnsSuccessfully() {
final Table<String, String, Integer> universityCourseSeatTable = HashBasedTable.create();
universityCourseSeatTable.put("Mumbai", "Chemical", 120);
universityCourseSeatTable.put("Mumbai", "IT", 60);
universityCourseSeatTable.put("Harvard", "Electrical", 60);
universityCourseSeatTable.put("Harvard", "IT", 120);
final int seatCount = universityCourseSeatTable.get("Mumbai", "IT");
final Integer seatCountForNoEntry = universityCourseSeatTable.get("Oxford", "IT");
assertThat(seatCount).isEqualTo(60);
assertThat(seatCountForNoEntry).isEqualTo(null);
}
@Test
public void givenTable_whenContains_returnsSuccessfully() {
final Table<String, String, Integer> universityCourseSeatTable = HashBasedTable.create();
universityCourseSeatTable.put("Mumbai", "Chemical", 120);
universityCourseSeatTable.put("Mumbai", "IT", 60);
universityCourseSeatTable.put("Harvard", "Electrical", 60);
universityCourseSeatTable.put("Harvard", "IT", 120);
final boolean entryIsPresent = universityCourseSeatTable.contains("Mumbai", "IT");
final boolean entryIsAbsent = universityCourseSeatTable.contains("Oxford", "IT");
final boolean courseIsPresent = universityCourseSeatTable.containsColumn("IT");
final boolean universityIsPresent = universityCourseSeatTable.containsRow("Mumbai");
final boolean seatCountIsPresent = universityCourseSeatTable.containsValue(60);
assertThat(entryIsPresent).isEqualTo(true);
assertThat(entryIsAbsent).isEqualTo(false);
assertThat(courseIsPresent).isEqualTo(true);
assertThat(universityIsPresent).isEqualTo(true);
assertThat(seatCountIsPresent).isEqualTo(true);
}
@Test
public void givenTable_whenRemove_returnsSuccessfully() {
final Table<String, String, Integer> universityCourseSeatTable = HashBasedTable.create();
universityCourseSeatTable.put("Mumbai", "Chemical", 120);
universityCourseSeatTable.put("Mumbai", "IT", 60);
final int seatCount = universityCourseSeatTable.remove("Mumbai", "IT");
assertThat(seatCount).isEqualTo(60);
assertThat(universityCourseSeatTable.remove("Mumbai", "IT")).isEqualTo(null);
}
@Test
public void givenTable_whenColumn_returnsSuccessfully() {
final Table<String, String, Integer> universityCourseSeatTable = HashBasedTable.create();
universityCourseSeatTable.put("Mumbai", "Chemical", 120);
universityCourseSeatTable.put("Mumbai", "IT", 60);
universityCourseSeatTable.put("Harvard", "Electrical", 60);
universityCourseSeatTable.put("Harvard", "IT", 120);
final Map<String, Integer> universitySeatMap = universityCourseSeatTable.column("IT");
assertThat(universitySeatMap).hasSize(2);
assertThat(universitySeatMap.get("Mumbai")).isEqualTo(60);
assertThat(universitySeatMap.get("Harvard")).isEqualTo(120);
}
@Test
public void givenTable_whenColumnMap_returnsSuccessfully() {
final Table<String, String, Integer> universityCourseSeatTable = HashBasedTable.create();
universityCourseSeatTable.put("Mumbai", "Chemical", 120);
universityCourseSeatTable.put("Mumbai", "IT", 60);
universityCourseSeatTable.put("Harvard", "Electrical", 60);
universityCourseSeatTable.put("Harvard", "IT", 120);
final Map<String, Map<String, Integer>> courseKeyUniversitySeatMap = universityCourseSeatTable.columnMap();
assertThat(courseKeyUniversitySeatMap).hasSize(3);
assertThat(courseKeyUniversitySeatMap.get("IT")).hasSize(2);
assertThat(courseKeyUniversitySeatMap.get("Electrical")).hasSize(1);
assertThat(courseKeyUniversitySeatMap.get("Chemical")).hasSize(1);
}
@Test
public void givenTable_whenRow_returnsSuccessfully() {
final Table<String, String, Integer> universityCourseSeatTable = HashBasedTable.create();
universityCourseSeatTable.put("Mumbai", "Chemical", 120);
universityCourseSeatTable.put("Mumbai", "IT", 60);
universityCourseSeatTable.put("Harvard", "Electrical", 60);
universityCourseSeatTable.put("Harvard", "IT", 120);
final Map<String, Integer> courseSeatMap = universityCourseSeatTable.row("Mumbai");
assertThat(courseSeatMap).hasSize(2);
assertThat(courseSeatMap.get("IT")).isEqualTo(60);
assertThat(courseSeatMap.get("Chemical")).isEqualTo(120);
}
@Test
public void givenTable_whenRowKeySet_returnsSuccessfully() {
final Table<String, String, Integer> universityCourseSeatTable = HashBasedTable.create();
universityCourseSeatTable.put("Mumbai", "Chemical", 120);
universityCourseSeatTable.put("Mumbai", "IT", 60);
universityCourseSeatTable.put("Harvard", "Electrical", 60);
universityCourseSeatTable.put("Harvard", "IT", 120);
final Set<String> universitySet = universityCourseSeatTable.rowKeySet();
assertThat(universitySet).hasSize(2);
}
@Test
public void givenTable_whenColKeySet_returnsSuccessfully() {
final Table<String, String, Integer> universityCourseSeatTable = HashBasedTable.create();
universityCourseSeatTable.put("Mumbai", "Chemical", 120);
universityCourseSeatTable.put("Mumbai", "IT", 60);
universityCourseSeatTable.put("Harvard", "Electrical", 60);
universityCourseSeatTable.put("Harvard", "IT", 120);
final Set<String> courseSet = universityCourseSeatTable.columnKeySet();
assertThat(courseSet).hasSize(3);
}
@Test
public void givenTreeTable_whenGet_returnsSuccessfully() {
final Table<String, String, Integer> universityCourseSeatTable = TreeBasedTable.create();
universityCourseSeatTable.put("Mumbai", "Chemical", 120);
universityCourseSeatTable.put("Mumbai", "IT", 60);
universityCourseSeatTable.put("Harvard", "Electrical", 60);
universityCourseSeatTable.put("Harvard", "IT", 120);
final int seatCount = universityCourseSeatTable.get("Mumbai", "IT");
assertThat(seatCount).isEqualTo(60);
}
@Test
public void givenImmutableTable_whenGet_returnsSuccessfully() {
final Table<String, String, Integer> universityCourseSeatTable = ImmutableTable.<String, String, Integer> builder().put("Mumbai", "Chemical", 120).put("Mumbai", "IT", 60).put("Harvard", "Electrical", 60).put("Harvard", "IT", 120).build();
final int seatCount = universityCourseSeatTable.get("Mumbai", "IT");
assertThat(seatCount).isEqualTo(60);
}
@Test
public void givenArrayTable_whenGet_returnsSuccessfully() {
final List<String> universityRowTable = Lists.newArrayList("Mumbai", "Harvard");
final List<String> courseColumnTables = Lists.newArrayList("Chemical", "IT", "Electrical");
final Table<String, String, Integer> universityCourseSeatTable = ArrayTable.create(universityRowTable, courseColumnTables);
universityCourseSeatTable.put("Mumbai", "Chemical", 120);
universityCourseSeatTable.put("Mumbai", "IT", 60);
universityCourseSeatTable.put("Harvard", "Electrical", 60);
universityCourseSeatTable.put("Harvard", "IT", 120);
final int seatCount = universityCourseSeatTable.get("Mumbai", "IT");
assertThat(seatCount).isEqualTo(60);
}
}