Modify class GuavaFilterCollectionsTest
Change class name, add new methods
This commit is contained in:
parent
56388e1b07
commit
1cd3d2e617
|
@ -1,9 +1,13 @@
|
|||
package org.baeldung.guava;
|
||||
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.containsInAnyOrder;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
@ -14,20 +18,32 @@ import com.google.common.base.Predicate;
|
|||
import com.google.common.base.Predicates;
|
||||
import com.google.common.collect.Collections2;
|
||||
import com.google.common.collect.FluentIterable;
|
||||
import com.google.common.collect.ListMultimap;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Multimaps;
|
||||
import com.google.common.collect.Ordering;
|
||||
|
||||
public class GuavaFilterCollectionsTest {
|
||||
|
||||
public class GuavaFilterTransformCollectionsTest {
|
||||
|
||||
@Test
|
||||
public void whenFilterCollection_thenFiltered() {
|
||||
public void whenFilterWithCollections2_thenFiltered() {
|
||||
final List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom");
|
||||
final Collection<String> result = Collections2.filter(names, Predicates.containsPattern("a"));
|
||||
|
||||
assertEquals(2, result.size());
|
||||
assertEquals("[Jane, Adam]", result.toString());
|
||||
assertThat(result, containsInAnyOrder("Jane", "Adam"));
|
||||
|
||||
result.add("anna");
|
||||
assertEquals(5, names.size());
|
||||
result.remove("Adam");
|
||||
assertEquals(4, names.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFilterWithIterables_thenFiltered() {
|
||||
final List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom");
|
||||
final Iterable<String> result = Iterables.filter(names, Predicates.containsPattern("a"));
|
||||
|
||||
assertThat(result, containsInAnyOrder("Jane", "Adam"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -43,7 +59,7 @@ public class GuavaFilterCollectionsTest {
|
|||
final Collection<String> result = Collections2.filter(names, predicate);
|
||||
|
||||
assertEquals(3, result.size());
|
||||
assertEquals("[John, Jane, Adam]", result.toString());
|
||||
assertThat(result, containsInAnyOrder("John", "Jane", "Adam"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -52,11 +68,46 @@ public class GuavaFilterCollectionsTest {
|
|||
final Collection<String> result = Collections2.filter(names, Predicates.or(Predicates.containsPattern("J"), Predicates.not(Predicates.containsPattern("a"))));
|
||||
|
||||
assertEquals(3, result.size());
|
||||
assertEquals("[John, Jane, Tom]", result.toString());
|
||||
assertThat(result, containsInAnyOrder("John", "Jane", "Tom"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenTransformCollection_thenTransformed() {
|
||||
public void whenRemoveNullFromCollection_thenRemoved() {
|
||||
final List<String> names = Lists.newArrayList("John", null, "Jane", null, "Adam", "Tom");
|
||||
final Collection<String> result = Collections2.filter(names, Predicates.notNull());
|
||||
|
||||
assertEquals(4, result.size());
|
||||
assertThat(result, containsInAnyOrder("John", "Jane", "Adam", "Tom"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckAllElementsForCondition_thenChecked() {
|
||||
final List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom");
|
||||
|
||||
boolean result = Iterables.all(names, Predicates.containsPattern("n|m"));
|
||||
assertTrue(result);
|
||||
|
||||
result = Iterables.all(names, Predicates.containsPattern("a"));
|
||||
assertFalse(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenTransformWithIterables_thenTransformed() {
|
||||
final Function<String, Integer> function = new Function<String, Integer>() {
|
||||
@Override
|
||||
public final Integer apply(final String input) {
|
||||
return input.length();
|
||||
}
|
||||
};
|
||||
|
||||
final List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom");
|
||||
final Iterable<Integer> result = Iterables.transform(names, function);
|
||||
|
||||
assertThat(result, contains(4, 4, 4, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenTransformWithCollections2_thenTransformed() {
|
||||
final Function<String, Integer> function = new Function<String, Integer>() {
|
||||
@Override
|
||||
public final Integer apply(final String input) {
|
||||
|
@ -68,7 +119,10 @@ public class GuavaFilterCollectionsTest {
|
|||
final Collection<Integer> result = Collections2.transform(names, function);
|
||||
|
||||
assertEquals(4, result.size());
|
||||
assertEquals("[4, 4, 4, 3]", result.toString());
|
||||
assertThat(result, contains(4, 4, 4, 3));
|
||||
|
||||
result.remove(3);
|
||||
assertEquals(3, names.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -91,7 +145,16 @@ public class GuavaFilterCollectionsTest {
|
|||
final Collection<Boolean> result = Collections2.transform(names, Functions.compose(f2, f1));
|
||||
|
||||
assertEquals(4, result.size());
|
||||
assertEquals("[true, true, true, false]", result.toString());
|
||||
assertThat(result, contains(true, true, true, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreateFunctionFromPredicate_thenCreated() {
|
||||
final List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom");
|
||||
final Collection<Boolean> result = Collections2.transform(names, Functions.forPredicate(Predicates.containsPattern("m")));
|
||||
|
||||
assertEquals(4, result.size());
|
||||
assertThat(result, contains(false, false, true, true));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -114,44 +177,10 @@ public class GuavaFilterCollectionsTest {
|
|||
final Collection<Integer> result = FluentIterable.from(names).filter(predicate).transform(func).toList();
|
||||
|
||||
assertEquals(2, result.size());
|
||||
assertEquals("[4, 3]", result.toString());
|
||||
assertThat(result, containsInAnyOrder(4, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSortCollection_thenSorted() {
|
||||
final List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom");
|
||||
Collections.sort(names, Ordering.natural());
|
||||
assertEquals("[Adam, Jane, John, Tom]", names.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSortCollectionUsingCustomOrdering_thenSorted() {
|
||||
final Ordering<String> ordering = Ordering.natural().onResultOf(new Function<String, Integer>() {
|
||||
@Override
|
||||
public final Integer apply(final String input) {
|
||||
return input.length();
|
||||
}
|
||||
});
|
||||
|
||||
final List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom");
|
||||
Collections.sort(names, ordering);
|
||||
assertEquals("[Tom, John, Jane, Adam]", names.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGroupCollection_thenGrouped() {
|
||||
final List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom");
|
||||
final ListMultimap<Integer, String> groups = Multimaps.index(names, new Function<String, Integer>() {
|
||||
@Override
|
||||
public final Integer apply(final String input) {
|
||||
return input.length();
|
||||
}
|
||||
});
|
||||
|
||||
assertEquals(2, groups.keySet().size());
|
||||
assertEquals(3, groups.get(4).size());
|
||||
assertEquals("[Tom]", groups.get(3).toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue