commit
e2eb27a5ad
|
@ -0,0 +1,13 @@
|
||||||
|
*.class
|
||||||
|
|
||||||
|
#folders#
|
||||||
|
/target
|
||||||
|
/neoDb*
|
||||||
|
/data
|
||||||
|
/src/main/webapp/WEB-INF/classes
|
||||||
|
*/META-INF/*
|
||||||
|
|
||||||
|
# Packaged files #
|
||||||
|
*.jar
|
||||||
|
*.war
|
||||||
|
*.ear
|
|
@ -0,0 +1,37 @@
|
||||||
|
<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>guava-collections-set</artifactId>
|
||||||
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
|
<name>guava-collections-set</name>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-java</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<relativePath>../parent-java</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- test scoped -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.assertj</groupId>
|
||||||
|
<artifactId>assertj-core</artifactId>
|
||||||
|
<version>${assertj.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<finalName>guava-collections-set</finalName>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<!-- util -->
|
||||||
|
<guava.version>27.1-jre</guava.version>
|
||||||
|
<!-- testing -->
|
||||||
|
<assertj.version>3.6.1</assertj.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,92 @@
|
||||||
|
package org.baeldung.guava;
|
||||||
|
|
||||||
|
import com.google.common.collect.HashMultiset;
|
||||||
|
import com.google.common.collect.Multiset;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||||
|
|
||||||
|
public class GuavaMultiSetUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenMultiSet_whenAddingValues_shouldReturnCorrectCount() {
|
||||||
|
Multiset<String> bookStore = HashMultiset.create();
|
||||||
|
bookStore.add("Potter");
|
||||||
|
bookStore.add("Potter");
|
||||||
|
bookStore.add("Potter");
|
||||||
|
|
||||||
|
assertThat(bookStore.contains("Potter")).isTrue();
|
||||||
|
assertThat(bookStore.count("Potter")).isEqualTo(3);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenMultiSet_whenRemovingValues_shouldReturnCorrectCount() {
|
||||||
|
Multiset<String> bookStore = HashMultiset.create();
|
||||||
|
bookStore.add("Potter");
|
||||||
|
bookStore.add("Potter");
|
||||||
|
|
||||||
|
bookStore.remove("Potter");
|
||||||
|
assertThat(bookStore.contains("Potter")).isTrue();
|
||||||
|
assertThat(bookStore.count("Potter")).isEqualTo(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenMultiSet_whenSetCount_shouldReturnCorrectCount() {
|
||||||
|
Multiset<String> bookStore = HashMultiset.create();
|
||||||
|
bookStore.setCount("Potter", 50);
|
||||||
|
assertThat(bookStore.count("Potter")).isEqualTo(50);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenMultiSet_whenSettingNegativeCount_shouldThrowException() {
|
||||||
|
Multiset<String> bookStore = HashMultiset.create();
|
||||||
|
assertThatThrownBy(() -> bookStore.setCount("Potter", -1))
|
||||||
|
.isInstanceOf(IllegalArgumentException.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenMultiSet_whenSettingCountWithEmptySet_shouldBeSuccessful() {
|
||||||
|
Multiset<String> bookStore = HashMultiset.create();
|
||||||
|
assertThat(bookStore.setCount("Potter", 0, 2)).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenMultiSet_whenSettingCountWithCorrectValue_shouldBeSuccessful() {
|
||||||
|
Multiset<String> bookStore = HashMultiset.create();
|
||||||
|
bookStore.add("Potter");
|
||||||
|
bookStore.add("Potter");
|
||||||
|
|
||||||
|
assertThat(bookStore.setCount("Potter", 2, 52)).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenMultiSet_whenSettingCountWithIncorrectValue_shouldFail() {
|
||||||
|
Multiset<String> bookStore = HashMultiset.create();
|
||||||
|
bookStore.add("Potter");
|
||||||
|
bookStore.add("Potter");
|
||||||
|
|
||||||
|
assertThat(bookStore.setCount("Potter", 5, 52)).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenMap_compareMultiSetOperations() {
|
||||||
|
Map<String, Integer> bookStore = new HashMap<>();
|
||||||
|
bookStore.put("Potter", 3);
|
||||||
|
|
||||||
|
assertThat(bookStore.containsKey("Potter")).isTrue();
|
||||||
|
assertThat(bookStore.get("Potter")).isEqualTo(3);
|
||||||
|
|
||||||
|
bookStore.put("Potter", 2);
|
||||||
|
assertThat(bookStore.get("Potter")).isEqualTo(2);
|
||||||
|
|
||||||
|
bookStore.put("Potter", null);
|
||||||
|
assertThat(bookStore.containsKey("Potter")).isTrue();
|
||||||
|
|
||||||
|
bookStore.put("Potter", -1);
|
||||||
|
assertThat(bookStore.containsKey("Potter")).isTrue();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue