[BAEL-16643] Split guava module

This commit is contained in:
Sjmillington
2019-09-10 16:52:28 +01:00
parent dc1cd98cd0
commit 4a3ca68047
15 changed files with 46 additions and 16 deletions
+2 -7
View File
@@ -1,11 +1,8 @@
=========
## Guava and Hamcrest Cookbooks and Examples
## Guava Examples
### Relevant Articles:
- [Guava Functional Cookbook](http://www.baeldung.com/guava-functions-predicates)
- [Guava Write to File, Read from File](http://www.baeldung.com/guava-write-to-file-read-from-file)
- [Guide to Guavas Ordering](http://www.baeldung.com/guava-ordering)
- [Guide to Guavas PreConditions](http://www.baeldung.com/guava-preconditions)
- [Introduction to Guava CacheLoader](http://www.baeldung.com/guava-cacheloader)
@@ -14,7 +11,5 @@
- [Guide to Guavas Reflection Utilities](http://www.baeldung.com/guava-reflection)
- [Guide to Mathematical Utilities in Guava](http://www.baeldung.com/guava-math)
- [Bloom Filter in Java using Guava](http://www.baeldung.com/guava-bloom-filter)
- [Using Guava CountingOutputStream](http://www.baeldung.com/guava-counting-outputstream)
- [Hamcrest Text Matchers](http://www.baeldung.com/hamcrest-text-matchers)
- [Quick Guide to the Guava RateLimiter](http://www.baeldung.com/guava-rate-limiter)
- [Hamcrest File Matchers](https://www.baeldung.com/hamcrest-file-matchers)
+1 -8
View File
@@ -27,13 +27,7 @@
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hamcrest/java-hamcrest -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>java-hamcrest</artifactId>
<version>${java-hamcrest.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
@@ -52,7 +46,6 @@
<!-- testing -->
<assertj.version>3.6.1</assertj.version>
<java-hamcrest.version>2.0.0.0</java-hamcrest.version>
</properties>
</project>
@@ -1,29 +0,0 @@
package org.baeldung.guava;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import org.junit.Test;
import com.google.common.io.CountingOutputStream;
public class GuavaCountingOutputStreamUnitTest {
public static final int MAX = 5;
@Test(expected = RuntimeException.class)
public void givenData_whenCountReachesLimit_thenThrowException() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
CountingOutputStream cos = new CountingOutputStream(out);
byte[] data = new byte[1024];
ByteArrayInputStream in = new ByteArrayInputStream(data);
int b;
while ((b = in.read()) != -1) {
cos.write(b);
if (cos.getCount() >= MAX) {
throw new RuntimeException("Write limit reached");
}
}
}
}
@@ -1,210 +0,0 @@
package org.baeldung.guava;
import static org.hamcrest.Matchers.contains;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import org.junit.Test;
import com.google.common.base.Charsets;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.io.ByteSink;
import com.google.common.io.ByteSource;
import com.google.common.io.ByteStreams;
import com.google.common.io.CharSink;
import com.google.common.io.CharSource;
import com.google.common.io.CharStreams;
import com.google.common.io.Files;
import com.google.common.io.LittleEndianDataInputStream;
import com.google.common.io.LittleEndianDataOutputStream;
import com.google.common.io.Resources;
public class GuavaIOUnitTest {
@Test
public void whenWriteUsingFiles_thenWritten() throws IOException {
final String expectedValue = "Hello world";
final File file = new File("src/test/resources/test.out");
Files.write(expectedValue, file, Charsets.UTF_8);
final String result = Files.toString(file, Charsets.UTF_8);
assertEquals(expectedValue, result);
}
@Test
public void whenWriteUsingCharSink_thenWritten() throws IOException {
final String expectedValue = "Hello world";
final File file = new File("src/test/resources/test.out");
final CharSink sink = Files.asCharSink(file, Charsets.UTF_8);
sink.write(expectedValue);
final String result = Files.toString(file, Charsets.UTF_8);
assertEquals(expectedValue, result);
}
@Test
public void whenWriteMultipleLinesUsingCharSink_thenWritten() throws IOException {
final List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom");
final File file = new File("src/test/resources/test.out");
final CharSink sink = Files.asCharSink(file, Charsets.UTF_8);
sink.writeLines(names, " ");
final String result = Files.toString(file, Charsets.UTF_8);
final String expectedValue = Joiner.on(" ").join(names);
assertEquals(expectedValue, result.trim());
}
@Test
public void whenWriteUsingByteSink_thenWritten() throws IOException {
final String expectedValue = "Hello world";
final File file = new File("src/test/resources/test.out");
final ByteSink sink = Files.asByteSink(file);
sink.write(expectedValue.getBytes());
final String result = Files.toString(file, Charsets.UTF_8);
assertEquals(expectedValue, result);
}
@Test
public void whenReadUsingFiles_thenRead() throws IOException {
final String expectedValue = "Hello world";
final File file = new File("src/test/resources/test1.in");
final String result = Files.toString(file, Charsets.UTF_8);
assertEquals(expectedValue, result);
}
@Test
public void whenReadMultipleLinesUsingFiles_thenRead() throws IOException {
final File file = new File("src/test/resources/test2.in");
final List<String> result = Files.readLines(file, Charsets.UTF_8);
assertThat(result, contains("John", "Jane", "Adam", "Tom"));
}
@Test
public void whenReadUsingCharSource_thenRead() throws IOException {
final String expectedValue = "Hello world";
final File file = new File("src/test/resources/test1.in");
final CharSource source = Files.asCharSource(file, Charsets.UTF_8);
final String result = source.read();
assertEquals(expectedValue, result);
}
@Test
public void whenReadMultipleCharSources_thenRead() throws IOException {
final String expectedValue = "Hello worldTest";
final File file1 = new File("src/test/resources/test1.in");
final File file2 = new File("src/test/resources/test1_1.in");
final CharSource source1 = Files.asCharSource(file1, Charsets.UTF_8);
final CharSource source2 = Files.asCharSource(file2, Charsets.UTF_8);
final CharSource source = CharSource.concat(source1, source2);
final String result = source.read();
assertEquals(expectedValue, result);
}
@Test
public void whenReadUsingCharStream_thenRead() throws IOException {
final String expectedValue = "Hello world";
final FileReader reader = new FileReader("src/test/resources/test1.in");
final String result = CharStreams.toString(reader);
assertEquals(expectedValue, result);
reader.close();
}
@Test
public void whenReadUsingByteSource_thenRead() throws IOException {
final String expectedValue = "Hello world";
final File file = new File("src/test/resources/test1.in");
final ByteSource source = Files.asByteSource(file);
final byte[] result = source.read();
assertEquals(expectedValue, new String(result));
}
@Test
public void whenReadAfterOffsetUsingByteSource_thenRead() throws IOException {
final String expectedValue = "lo world";
final File file = new File("src/test/resources/test1.in");
final long offset = 3;
final long length = 1000;
final ByteSource source = Files.asByteSource(file).slice(offset, length);
final byte[] result = source.read();
assertEquals(expectedValue, new String(result));
}
@Test
public void whenReadUsingByteStream_thenRead() throws IOException {
final String expectedValue = "Hello world";
final FileInputStream reader = new FileInputStream("src/test/resources/test1.in");
final byte[] result = ByteStreams.toByteArray(reader);
assertEquals(expectedValue, new String(result));
reader.close();
}
@Test
public void whenReadUsingResources_thenRead() throws IOException {
final String expectedValue = "Hello world";
final URL url = Resources.getResource("test1.in");
final String result = Resources.toString(url, Charsets.UTF_8);
assertEquals(expectedValue, result);
}
@Test
public void whenCopyFileUsingFiles_thenCopied() throws IOException {
final String expectedValue = "Hello world";
final File file1 = new File("src/test/resources/test1.in");
final File file2 = new File("src/test/resources/test_copy.in");
Files.copy(file1, file2);
final String result = Files.toString(file2, Charsets.UTF_8);
assertEquals(expectedValue, result);
}
@Test
public void whenWriteReadLittleEndian_thenCorrect() throws IOException {
final int value = 100;
final LittleEndianDataOutputStream writer = new LittleEndianDataOutputStream(new FileOutputStream("src/test/resources/test_le.txt"));
writer.writeInt(value);
writer.close();
final LittleEndianDataInputStream reader = new LittleEndianDataInputStream(new DataInputStream(new FileInputStream("src/test/resources/test_le.txt")));
final int result = reader.readInt();
reader.close();
assertEquals(value, result);
}
}
@@ -1,33 +0,0 @@
package org.baeldung.hamcrest;
public class Animal {
String name;
boolean wild;
String sound;
public Animal(String name, boolean wild, String sound) {
super();
this.name = name;
this.wild = wild;
this.sound = sound;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isWild() {
return wild;
}
public void setWild(boolean wild) {
this.wild = wild;
}
public String getSound() {
return sound;
}
public void setSound(String sound) {
this.sound = sound;
}
}
@@ -1,13 +0,0 @@
package org.baeldung.hamcrest;
public class Cat extends Animal {
public Cat() {
super("cat", false, "meow");
}
public String makeSound() {
return getSound();
}
}
@@ -1,331 +0,0 @@
package org.baeldung.hamcrest;
import org.junit.Test;
import java.util.*;
import static org.baeldung.hamcrest.IsPositiveInteger.isAPositiveInteger;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.beans.HasProperty.hasProperty;
import static org.hamcrest.beans.HasPropertyWithValue.hasProperty;
import static org.hamcrest.beans.SamePropertyValuesAs.samePropertyValuesAs;
import static org.hamcrest.collection.IsArrayContaining.hasItemInArray;
import static org.hamcrest.collection.IsArrayContainingInAnyOrder.arrayContainingInAnyOrder;
import static org.hamcrest.collection.IsArrayContainingInOrder.arrayContaining;
import static org.hamcrest.collection.IsArrayWithSize.arrayWithSize;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.hamcrest.collection.IsEmptyCollection.empty;
import static org.hamcrest.collection.IsIn.isIn;
import static org.hamcrest.collection.IsIn.isOneOf;
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
import static org.hamcrest.collection.IsMapContaining.hasEntry;
import static org.hamcrest.collection.IsMapContaining.hasKey;
import static org.hamcrest.collection.IsMapContaining.hasValue;
import static org.hamcrest.core.AllOf.allOf;
import static org.hamcrest.core.AnyOf.anyOf;
import static org.hamcrest.core.Every.everyItem;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.hamcrest.core.IsNot.not;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.hamcrest.core.IsSame.sameInstance;
import static org.hamcrest.core.StringContains.containsString;
import static org.hamcrest.core.StringEndsWith.endsWith;
import static org.hamcrest.core.StringStartsWith.startsWith;
import static org.hamcrest.object.HasToString.hasToString;
import static org.hamcrest.object.IsCompatibleType.typeCompatibleWith;
import static org.hamcrest.text.IsEmptyString.isEmptyOrNullString;
import static org.hamcrest.text.IsEmptyString.isEmptyString;
import static org.hamcrest.text.IsEqualIgnoringCase.equalToIgnoringCase;
import static org.hamcrest.text.IsEqualIgnoringWhiteSpace.equalToIgnoringWhiteSpace;
import static org.hamcrest.text.StringContainsInOrder.stringContainsInOrder;
public class HamcrestMatcherUnitTest {
@Test
public void given2Strings_whenEqual_thenCorrect() {
String a = "foo";
String b = "FOO";
assertThat(a, equalToIgnoringCase(b));
}
@Test
public void givenBean_whenHasValue_thenCorrect() {
Person person = new Person("Baeldung", "New York");
assertThat(person, hasProperty("name"));
}
@Test
public void givenBean_whenHasCorrectValue_thenCorrect() {
Person person = new Person("Baeldung", "New York");
assertThat(person, hasProperty("address", equalTo("New York")));
}
@Test
public void given2Beans_whenHavingSameValues_thenCorrect() {
Person person1 = new Person("Baeldung", "New York");
Person person2 = new Person("Baeldung", "New York");
assertThat(person1, samePropertyValuesAs(person2));
}
@Test
public void givenAList_whenChecksSize_thenCorrect() {
List<String> hamcrestMatchers = Arrays.asList("collections", "beans",
"text", "number");
assertThat(hamcrestMatchers, hasSize(4));
}
@Test
public void givenArray_whenChecksSize_thenCorrect() {
String[] hamcrestMatchers = { "collections", "beans", "text", "number" };
assertThat(hamcrestMatchers, arrayWithSize(4));
}
@Test
public void givenAListAndValues_whenChecksListForGivenValues_thenCorrect() {
List<String> hamcrestMatchers = Arrays.asList("collections", "beans",
"text", "number");
assertThat(hamcrestMatchers,
containsInAnyOrder("beans", "text", "collections", "number"));
}
@Test
public void givenAListAndValues_whenChecksListForGivenValuesWithOrder_thenCorrect() {
List<String> hamcrestMatchers = Arrays.asList("collections", "beans",
"text", "number");
assertThat(hamcrestMatchers,
contains("collections", "beans", "text", "number"));
}
@Test
public void givenArrayAndValue_whenValueFoundInArray_thenCorrect() {
String[] hamcrestMatchers = { "collections", "beans", "text", "number" };
assertThat(hamcrestMatchers, hasItemInArray("text"));
}
@Test
public void givenValueAndArray_whenValueIsOneOfArrayElements_thenCorrect() {
String[] hamcrestMatchers = { "collections", "beans", "text", "number" };
assertThat("text", isOneOf(hamcrestMatchers));
}
@Test
public void givenArrayAndValues_whenValuesFoundInArray_thenCorrect() {
String[] hamcrestMatchers = { "collections", "beans", "text", "number" };
assertThat(
hamcrestMatchers,
arrayContainingInAnyOrder("beans", "collections", "number",
"text"));
}
@Test
public void givenArrayAndValues_whenValuesFoundInArrayInOrder_thenCorrect() {
String[] hamcrestMatchers = { "collections", "beans", "text", "number" };
assertThat(hamcrestMatchers,
arrayContaining("collections", "beans", "text", "number"));
}
@Test
public void givenCollection_whenEmpty_thenCorrect() {
List<String> emptyList = new ArrayList<>();
assertThat(emptyList, empty());
}
@Test
public void givenValueAndArray_whenValueFoundInArray_thenCorrect() {
String[] array = new String[] { "collections", "beans", "text",
"number" };
assertThat("beans", isIn(array));
}
@Test
public void givenMapAndKey_whenKeyFoundInMap_thenCorrect() {
Map<String, String> map = new HashMap<>();
map.put("blogname", "baeldung");
assertThat(map, hasKey("blogname"));
}
@Test
public void givenMapAndEntry_whenEntryFoundInMap_thenCorrect() {
Map<String, String> map = new HashMap<>();
map.put("blogname", "baeldung");
assertThat(map, hasEntry("blogname", "baeldung"));
}
@Test
public void givenMapAndValue_whenValueFoundInMap_thenCorrect() {
Map<String, String> map = new HashMap<>();
map.put("blogname", "baeldung");
assertThat(map, hasValue("baeldung"));
}
@Test
public void givenString_whenEmpty_thenCorrect() {
String str = "";
assertThat(str, isEmptyString());
}
@Test
public void givenString_whenEmptyOrNull_thenCorrect() {
String str = null;
assertThat(str, isEmptyOrNullString());
}
@Test
public void given2Strings_whenEqualRegardlessWhiteSpace_thenCorrect() {
String str1 = "text";
String str2 = " text ";
assertThat(str1, equalToIgnoringWhiteSpace(str2));
}
@Test
public void givenString_whenContainsGivenSubstring_thenCorrect() {
String str = "calligraphy";
assertThat(str, stringContainsInOrder(Arrays.asList("call", "graph")));
}
@Test
public void givenBean_whenToStringReturnsRequiredString_thenCorrect() {
Person person = new Person("Barrack", "Washington");
String str = person.toString();
assertThat(person, hasToString(str));
}
@Test
public void given2Classes_whenOneInheritsFromOther_thenCorrect() {
assertThat(Cat.class, typeCompatibleWith(Animal.class));
}
@Test
public void given2Strings_whenIsEqualRegardlessWhiteSpace_thenCorrect() {
String str1 = "text";
String str2 = " text ";
assertThat(str1, is(equalToIgnoringWhiteSpace(str2)));
}
@Test
public void given2Strings_whenIsNotEqualRegardlessWhiteSpace_thenCorrect() {
String str1 = "text";
String str2 = " texts ";
assertThat(str1, not(equalToIgnoringWhiteSpace(str2)));
}
@Test
public void given2Strings_whenNotEqual_thenCorrect() {
String str1 = "text";
String str2 = "texts";
assertThat(str1, not(str2));
}
@Test
public void given2Strings_whenIsEqual_thenCorrect() {
String str1 = "text";
String str2 = "text";
assertThat(str1, is(str2));
}
@Test
public void givenAStrings_whenContainsAnotherGivenString_thenCorrect() {
String str1 = "calligraphy";
String str2 = "call";
assertThat(str1, containsString(str2));
}
@Test
public void givenAString_whenEndsWithAnotherGivenString_thenCorrect() {
String str1 = "calligraphy";
String str2 = "phy";
assertThat(str1, endsWith(str2));
}
@Test
public void givenAString_whenStartsWithAnotherGivenString_thenCorrect() {
String str1 = "calligraphy";
String str2 = "call";
assertThat(str1, startsWith(str2));
}
@Test
public void given2Objects_whenSameInstance_thenCorrect() {
Cat cat = new Cat();
assertThat(cat, sameInstance(cat));
}
@Test
public void givenAnObject_whenInstanceOfGivenClass_thenCorrect() {
Cat cat = new Cat();
assertThat(cat, instanceOf(Cat.class));
}
@Test
public void givenList_whenEachElementGreaterThan0_thenCorrect() {
List<Integer> list = Arrays.asList(1, 2, 3);
int baseCase = 0;
assertThat(list, everyItem(greaterThan(baseCase)));
}
@Test
public void givenString_whenNotNull_thenCorrect() {
String str = "notnull";
assertThat(str, notNullValue());
}
@Test
public void givenString_whenMeetsAnyOfGivenConditions_thenCorrect() {
String str = "calligraphy";
String start = "call";
String end = "foo";
assertThat(str, anyOf(startsWith(start), containsString(end)));
}
@Test
public void givenString_whenMeetsAllOfGivenConditions_thenCorrect() {
String str = "calligraphy";
String start = "call";
String end = "phy";
assertThat(str, allOf(startsWith(start), endsWith(end)));
}
@Test
public void givenInteger_whenAPositiveValue_thenCorrect() {
int num = 1;
assertThat(num, isAPositiveInteger());
}
@Test
public void givenAnInteger_whenGreaterThan0_thenCorrect() {
int num = 1;
assertThat(num, greaterThan(0));
}
@Test
public void givenAnInteger_whenGreaterThanOrEqTo5_thenCorrect() {
int num = 5;
assertThat(num, greaterThanOrEqualTo(5));
}
@Test
public void givenAnInteger_whenLessThan0_thenCorrect() {
int num = -1;
assertThat(num, lessThan(0));
}
@Test
public void givenAnInteger_whenLessThanOrEqTo5_thenCorrect() {
assertThat(-1, lessThanOrEqualTo(5));
}
@Test
public void givenADouble_whenCloseTo_thenCorrect() {
assertThat(1.2, closeTo(1, 0.5));
}
}
@@ -1,24 +0,0 @@
package org.baeldung.hamcrest;
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
public class IsPositiveInteger extends TypeSafeMatcher<Integer> {
public void describeTo(Description description) {
description.appendText("a positive integer");
}
@Factory
public static Matcher<Integer> isAPositiveInteger() {
return new IsPositiveInteger();
}
@Override
protected boolean matchesSafely(Integer integer) {
return integer > 0;
}
}
@@ -1,37 +0,0 @@
package org.baeldung.hamcrest;
public class Person {
String name;
String address;
public Person(String personName, String personAddress) {
name = personName;
address = personAddress;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
String str="[address:"+address+",name:"+name+"]";
return str;
}
}
@@ -1,2 +0,0 @@
### Relevant Articles:
- [Testing with Hamcrest](http://www.baeldung.com/java-junit-hamcrest-guide)