Skip to content

Commit 2fff273

Browse files
author
oborkovskyi
committed
Added guava19 test project
1 parent 6c83190 commit 2fff273

6 files changed

Lines changed: 289 additions & 0 deletions

File tree

‎guava19/pom.xml‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.baeldung</groupId>
8+
<artifactId>guava</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>com.google.guava</groupId>
14+
<artifactId>guava</artifactId>
15+
<version>19.0</version>
16+
</dependency>
17+
<dependency>
18+
<groupId>junit</groupId>
19+
<artifactId>junit</artifactId>
20+
<version>4.12</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.hamcrest</groupId>
24+
<artifactId>hamcrest-all</artifactId>
25+
<version>1.3</version>
26+
</dependency>
27+
</dependencies>
28+
<build>
29+
<plugins>
30+
<plugin>
31+
<artifactId>maven-compiler-plugin</artifactId>
32+
<version>3.3</version>
33+
<configuration>
34+
<debug>true</debug>
35+
<optimize>true</optimize>
36+
<source>1.8</source>
37+
<target>1.8</target>
38+
<encoding>UTF-8</encoding>
39+
<showDeprecation>true</showDeprecation>
40+
<showWarnings>true</showWarnings>
41+
</configuration>
42+
</plugin>
43+
</plugins>
44+
</build>
45+
46+
</project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.guava.entity;
2+
3+
import com.google.common.base.MoreObjects;
4+
5+
public class User{
6+
private long id;
7+
private String name;
8+
private int age;
9+
10+
public User(long id, String name, int age) {
11+
this.id = id;
12+
this.name = name;
13+
this.age = age;
14+
}
15+
16+
public long getId() {
17+
return id;
18+
}
19+
20+
public String getName() {
21+
return name;
22+
}
23+
24+
public int getAge() {
25+
return age;
26+
}
27+
28+
@Override
29+
public String toString() {
30+
return MoreObjects.toStringHelper(User.class)
31+
.add("id", id)
32+
.add("name", name)
33+
.add("age", age)
34+
.toString();
35+
}
36+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.guava;
2+
3+
import com.google.common.base.CharMatcher;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.assertEquals;
7+
import static org.junit.Assert.assertTrue;
8+
9+
public class CharMatcherTest {
10+
@Test
11+
public void whenMatchingLetterOrString_ShouldReturnTrueForCorrectString() throws Exception {
12+
String inputString = "someString789";
13+
boolean result = CharMatcher.javaLetterOrDigit().matchesAllOf(inputString);
14+
15+
assertTrue(result);
16+
}
17+
18+
@Test
19+
public void whenCollapsingString_ShouldReturnStringWithDashesInsteadOfWhitespaces() throws Exception {
20+
String inputPhoneNumber = "8 123 456 123";
21+
String result = CharMatcher.whitespace().collapseFrom(inputPhoneNumber, '-');
22+
23+
assertEquals("8-123-456-123", result);
24+
}
25+
26+
@Test
27+
public void whenCountingDigitsInString_ShouldReturnActualCountOfDigits() throws Exception {
28+
String inputPhoneNumber = "8 123 456 123";
29+
int result = CharMatcher.digit().countIn(inputPhoneNumber);
30+
31+
assertEquals(10, result);
32+
}
33+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.baeldung.guava;
2+
3+
import com.baeldung.guava.entity.User;
4+
import com.google.common.base.CharMatcher;
5+
import com.google.common.base.Throwables;
6+
import com.google.common.collect.*;
7+
import com.google.common.hash.HashCode;
8+
import com.google.common.hash.HashFunction;
9+
import com.google.common.hash.Hashing;
10+
import com.google.common.reflect.TypeToken;
11+
import org.junit.Test;
12+
13+
import java.util.ArrayList;
14+
import java.util.Arrays;
15+
import java.util.List;
16+
17+
import static org.hamcrest.MatcherAssert.assertThat;
18+
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
19+
import static org.hamcrest.core.AnyOf.anyOf;
20+
import static org.junit.Assert.*;
21+
22+
public class GuavaMiscUtilsTest {
23+
24+
@Test
25+
public void whenGettingLazyStackTrace_ListShouldBeReturned() throws Exception {
26+
IllegalArgumentException e = new IllegalArgumentException("Some argument is incorrect");
27+
28+
List<StackTraceElement> stackTraceElements = Throwables.lazyStackTrace(e);
29+
30+
assertEquals(27, stackTraceElements.size());
31+
}
32+
33+
@Test
34+
public void multisetShouldCountHitsOfMultipleDuplicateObjects() throws Exception {
35+
List<String> userNames = Arrays.asList("David", "Eugene", "Alex", "Alex", "David", "David", "David");
36+
37+
Multiset<String> userNamesMultiset = HashMultiset.create(userNames);
38+
39+
assertEquals(7, userNamesMultiset.size());
40+
assertEquals(4, userNamesMultiset.count("David"));
41+
assertEquals(2, userNamesMultiset.count("Alex"));
42+
assertEquals(1, userNamesMultiset.count("Eugene"));
43+
assertThat(userNamesMultiset.elementSet(), anyOf(containsInAnyOrder("Alex", "David", "Eugene")));
44+
}
45+
46+
@Test
47+
public void whenAddingNewConnectedRange_RangesShouldBeMerged() throws Exception {
48+
RangeSet<Integer> rangeSet = TreeRangeSet.create();
49+
50+
rangeSet.add(Range.closed(1, 10));
51+
rangeSet.add(Range.closed(5, 15));
52+
rangeSet.add(Range.closedOpen(10, 17));
53+
54+
assertTrue(rangeSet.encloses(Range.closedOpen(1, 17)));
55+
assertTrue(rangeSet.encloses(Range.closed(2, 3)));
56+
assertTrue(rangeSet.contains(15));
57+
assertFalse(rangeSet.contains(17));
58+
assertEquals(1, rangeSet.asDescendingSetOfRanges().size());
59+
}
60+
61+
@Test
62+
public void cartesianProductShouldReturnAllPossibleCombinations() throws Exception {
63+
List<String> first = Lists.newArrayList("value1", "value2");
64+
List<String> second = Lists.newArrayList("value3", "value4");
65+
66+
List<List<String>> cartesianProduct = Lists.cartesianProduct(first, second);
67+
68+
List<String> pair1 = Lists.newArrayList("value2", "value3");
69+
List<String> pair2 = Lists.newArrayList("value2", "value4");
70+
List<String> pair3 = Lists.newArrayList("value1", "value3");
71+
List<String> pair4 = Lists.newArrayList("value1", "value4");
72+
73+
assertThat(cartesianProduct, anyOf(containsInAnyOrder(pair1, pair2, pair3, pair4)));
74+
}
75+
76+
@Test
77+
public void multisetShouldRemoveOccurrencesOfSpecifiedObjects() throws Exception {
78+
Multiset<String> multisetToModify = HashMultiset.create();
79+
Multiset<String> occurrencesToRemove = HashMultiset.create();
80+
81+
multisetToModify.add("John");
82+
multisetToModify.add("Max");
83+
multisetToModify.add("Alex");
84+
85+
occurrencesToRemove.add("Alex");
86+
occurrencesToRemove.add("John");
87+
88+
Multisets.removeOccurrences(multisetToModify, occurrencesToRemove);
89+
90+
assertEquals(1, multisetToModify.size());
91+
assertTrue(multisetToModify.contains("Max"));
92+
assertFalse(multisetToModify.contains("John"));
93+
assertFalse(multisetToModify.contains("Alex"));
94+
}
95+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.guava;
2+
3+
import com.google.common.hash.HashCode;
4+
import com.google.common.hash.HashFunction;
5+
import com.google.common.hash.Hashing;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class HashingTest {
11+
@Test
12+
public void whenHashingInSha384_hashFunctionShouldBeReturned() throws Exception {
13+
int inputData = 15;
14+
15+
HashFunction hashFunction = Hashing.sha384();
16+
HashCode hashCode = hashFunction.hashInt(inputData);
17+
18+
assertEquals("0904b6277381dcfbdddd6b6c66e4e3e8f83d4690718d8e6f272c891f24773a12feaf8c449fa6e42240a621b2b5e3cda8",
19+
hashCode.toString());
20+
}
21+
22+
@Test
23+
public void whenConcatenatingHashFunction_concatenatedHashShouldBeReturned() throws Exception {
24+
int inputData = 15;
25+
26+
HashFunction hashFunction = Hashing.concatenating(Hashing.crc32(), Hashing.crc32());
27+
HashFunction crc32Function = Hashing.crc32();
28+
29+
HashCode hashCode = hashFunction.hashInt(inputData);
30+
HashCode crc32HashCode = crc32Function.hashInt(inputData);
31+
32+
assertEquals(crc32HashCode.toString() + crc32HashCode.toString(), hashCode.toString());
33+
}
34+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.guava;
2+
3+
import com.google.common.reflect.TypeToken;
4+
import org.junit.Test;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
import static org.junit.Assert.assertFalse;
10+
import static org.junit.Assert.assertTrue;
11+
12+
public class TypeTokenTest {
13+
@Test
14+
public void whenCheckingIsAssignableFrom_shouldReturnTrueEvenIfGenericIsSpecified() throws Exception {
15+
ArrayList<String> stringList = new ArrayList<>();
16+
ArrayList<Integer> intList = new ArrayList<>();
17+
boolean isAssignableFrom = stringList.getClass().isAssignableFrom(intList.getClass());
18+
19+
assertTrue(isAssignableFrom);
20+
}
21+
22+
@Test
23+
public void whenCheckingIsSupertypeOf_shouldReturnFalseIfGenericIsSpecified() throws Exception {
24+
TypeToken<ArrayList<String>> listString = new TypeToken<ArrayList<String>>() {
25+
};
26+
TypeToken<ArrayList<Integer>> integerString = new TypeToken<ArrayList<Integer>>() {
27+
};
28+
29+
boolean isSupertypeOf = listString.isSupertypeOf(integerString);
30+
31+
assertFalse(isSupertypeOf);
32+
}
33+
34+
@Test
35+
public void whenCheckingIsSubtypeOf_shouldReturnTrueIfClassIsExtendedFrom() throws Exception {
36+
TypeToken<ArrayList<String>> stringList = new TypeToken<ArrayList<String>>() {
37+
};
38+
TypeToken<List> list = new TypeToken<List>() {
39+
};
40+
41+
boolean isSubtypeOf = stringList.isSubtypeOf(list);
42+
43+
assertTrue(isSubtypeOf);
44+
}
45+
}

0 commit comments

Comments
 (0)