private static Extractor<Location, String> lastUriPart() { return new Extractor<Location, String>() { @Override public String extract(Location input) { return input.asURI().toString() .replaceAll("/$", "") .replaceAll(".*/", ""); } }; }
private Extractor<JavaMember, String> memberIdentifier() { return new Extractor<JavaMember, String>() { @Override public String extract(JavaMember input) { return input.getOwner().getSimpleName() + "#" + input.getName(); } }; }
private Extractor<PathParameter, Optional<String>> patternToOptionalOfString() { return new Extractor<PathParameter, Optional<String>>() { @Override public Optional<String> extract(PathParameter pathParameter) { return pathParameter.getRegex().isPresent()? Optional.of(pathParameter.getRegex().get().pattern()): Optional.<String>absent(); } }; }
/** * Behavior is described in {@link AbstractIterableAssert#extracting(Extractor)} * @param <F> type of elements to extract a value from * @param <T> the extracted value type * @param objects the elements to extract a value from * @param extractor the extractor function * @return the extracted values */ public static <F, T> List<T> extract(Iterable<? extends F> objects, Extractor<? super F, T> extractor) { checkObjectToExtractFromIsNotNull(objects); List<T> result = newArrayList(); for (F object : objects) { final T newValue = extractor.extract(object); result.add(newValue); } return result; }
@Override public Tuple extract(T input) { checkArgument(fieldsOrProperties != null, "The names of the fields/properties to read should not be null"); checkArgument(fieldsOrProperties.length > 0, "The names of the fields/properties to read should not be empty"); checkArgument(input != null, "The object to extract fields/properties from should not be null"); List<Extractor<T, Object>> extractors = buildExtractors(); List<Object> values = extractValues(input, extractors); return new Tuple(values.toArray()); }
private List<Object> extractValues(T input, List<Extractor<T, Object>> singleExtractors) { List<Object> values = new ArrayList<>(); for (Extractor<T, Object> extractor : singleExtractors) { values.add(extractor.extract(input)); } return values; }
private List<Extractor<T, Object>> buildExtractors() { List<Extractor<T, Object>> result = new ArrayList<>(); for (String name : fieldsOrProperties) { result.add(new ByNameSingleExtractor<T>(name)); } return result; }
private <V> AbstractListAssert<?, List<? extends V>, V, ObjectAssert<V>> doFlatExtracting(Extractor<? super ELEMENT, ? extends Collection<V>> extractor) { List<V> result = newArrayList(); final List<? extends Collection<V>> extractedValues = FieldsOrPropertiesExtractor.extract(actual, extractor); for (Collection<? extends V> iterable : extractedValues) { result.addAll(iterable); } return newListAssertInstance(result); }
private <V, C extends Collection<V>> AbstractListAssert<?, List<? extends V>, V, ObjectAssert<V>> doFlatExtracting(Extractor<? super ELEMENT, C> extractor) { final List<C> extractedValues = FieldsOrPropertiesExtractor.extract(Arrays.asList(actual), extractor); final List<V> result = newArrayList(); for (C e : extractedValues) { result.addAll(e); } return newListAssertInstance(result); }
private <U, C extends Collection<U>> ObjectArrayAssert<U> doFlatExtracting(Extractor<? super T, C> extractor) { final List<C> extractedValues = FieldsOrPropertiesExtractor.extract(Arrays.asList(array), extractor); final List<U> result = newArrayList(); for (C e : extractedValues) { result.addAll(e); } return new ObjectArrayAssert<>(IterableUtil.toArray(result)); }
@Test public void should_extract_tuples_from_fields_or_properties() { Extractor<Employee, Tuple> extractor = new ByNameMultipleExtractor<>("id", "age"); Tuple extractedValue = extractor.extract(yoda); assertThat(extractedValue).isEqualTo(tuple(1L, 800)); }
@Test public void should_extract_tuples_with_consistent_iteration_order() { Extractor<Employee, Tuple> extractor = new ByNameMultipleExtractor<>("id", "name.first", "age"); Tuple extractedValues = extractor.extract(yoda); assertThat(extractedValues).isEqualTo(tuple(1L, "Yoda", 800)); }
@Test public void should_let_anonymous_class_extractor_runtime_exception_bubble_up() { thrown.expect(RuntimeException.class, "age > 100"); assertThat(employees).extracting(new Extractor<Employee, String>() { @Override public String extract(Employee employee) { if (employee.getAge() > 100) throw new RuntimeException("age > 100"); return employee.getName().getFirst(); } }); }
@Test public void should_allow_assertions_on_extractor_assertions_extracted_from_given_array_compatibility() { assertThat(employees).extracting(new Extractor<Employee, String>() { @Override public String extract(Employee input) { return input.getName().getFirst(); } }).containsOnly("Yoda", "Luke"); }
@Test public void should_allow_assertions_on_extractor_assertions_extracted_from_given_array_compatibility_runtimeexception() { thrown.expect(RuntimeException.class); assertThat(employees).extracting(new Extractor<Employee, String>() { @Override public String extract(Employee input) { if (input.getAge() > 100) { throw new RuntimeException("age > 100"); } return input.getName().getFirst(); } }); }
@Override @SafeVarargs public final AbstractListAssert<?, List<? extends Object>, Object, ObjectAssert<Object>> flatExtracting(Extractor<? super ELEMENT, ?>... extractors) { return super.flatExtracting(extractors); }
/** * Extract multiple values from each {@code Iterable}'s element according to the given {@code Extractor}s * and concatenate/flatten the extracted values in a list that is used as the new object under test. * <p> * If extracted values were not flattened, instead of a simple list like (given 2 extractors) : * <pre>element1.value1, element1.value2, element2.value1, element2.value2, ... </pre> * we would get a list of list like : * <pre>list(element1.value1, element1.value2), list(element2.value1, element2.value2), ... </pre> * <p> * Code example: * <pre><code class='java'> // fellowshipOfTheRing is a List<TolkienCharacter> * * // values are extracted in order and flattened : age1, name1, age2, name2, age3 ... * assertThat(fellowshipOfTheRing).flatExtracting(TolkienCharacter::getAge, * TolkienCharacter::getName) * .contains(33 ,"Frodo", * 1000, "Legolas", * 87, "Aragorn");</code></pre> * * The resulting extracted values list is ordered by {@code Iterable}'s element first and then extracted values, * this is why is in the example that age values come before names. * * @param extractors all the extractors to apply on each actual {@code Iterable}'s elements * @return a new assertion object whose object under test is a flattened list of all extracted values. */ @CheckReturnValue public AbstractListAssert<?, List<? extends Object>, Object, ObjectAssert<Object>> flatExtracting(@SuppressWarnings("unchecked") Extractor<? super ELEMENT, ?>... extractors) { Stream<? extends ELEMENT> actualStream = stream(actual.spliterator(), false); List<Object> result = actualStream.flatMap(element -> Stream.of(extractors) .map(extractor -> extractor.extract(element))) .collect(Collectors.toList()); return newListAssertInstance(result); }
/** * Extract the values from the array's elements by applying an extracting function on them. The returned * array becomes a new object under test. * <p> * It allows to test values from the elements in safer way than by using {@link #extracting(String)}, as it * doesn't utilize introspection. * <p> * Let's take a look an example: * <pre><code class='java'> // Build a list of TolkienCharacter, a TolkienCharacter has a name, and age and a Race (a specific class) * // they can be public field or properties, both can be extracted. * AtomicReferenceArray<TolkienCharacter> fellowshipOfTheRing = new AtomicReferenceArray<>(new TolkienCharacter[]{ * new TolkienCharacter("Frodo", 33, HOBBIT), * new TolkienCharacter("Sam", 38, HOBBIT), * new TolkienCharacter("Gandalf", 2020, MAIA), * new TolkienCharacter("Legolas", 1000, ELF), * new TolkienCharacter("Pippin", 28, HOBBIT), * new TolkienCharacter("Gimli", 139, DWARF), * new TolkienCharacter("Aragorn", 87, MAN, * new TolkienCharacter("Boromir", 37, MAN) * }; * * * // this extracts the race * Extractor<TolkienCharacter, Race> race = new Extractor<TolkienCharacter, Race>() { * {@literal @}Override * public Race extract(TolkienCharacter input) { * return input.getRace(); * } * } * * // fellowship has hobbits, right, my presioussss? * assertThat(fellowshipOfTheRing).extracting(race).contains(HOBBIT);</code></pre> * * Note that the order of extracted property/field values is consistent with the iteration order of the Iterable under * test, for example if it's a {@link HashSet}, you won't be able to make any assumptions on the extracted values * order. * * @param <U> the extracted values type * @param extractor the object transforming input object to desired one * @return a new assertion object whose object under test is the list of values extracted * @since 2.7.0 / 3.7.0 */ @CheckReturnValue public <U> ObjectArrayAssert<U> extracting(Extractor<? super T, U> extractor) { U[] extracted = FieldsOrPropertiesExtractor.extract(array, extractor); return new ObjectArrayAssert<>(extracted); }
/** * Get selenium extractor. * * @return {@code Extractor<WebElement, String>} */ public static Extractor<WebElement, String> linkText() { return new SeleniumExtractor(); }
/** * Call {@link #extract(Iterable, Extractor)} after converting objects to an iterable. * <p> * Behavior is described in javadoc {@link AbstractObjectArrayAssert#extracting(Extractor)} * @param <F> type of elements to extract a value from * @param <T> the extracted value type * @param objects the elements to extract a value from * @param extractor the extractor function * @return the extracted values */ public static <F, T> T[] extract(F[] objects, Extractor<? super F, T> extractor) { checkObjectToExtractFromIsNotNull(objects); List<T> result = extract(newArrayList(objects), extractor); return toArray(result); }
/** * Provides extractor for extracting {@link java.lang.Object#toString} from any object * @return the built {@link Extractor} */ public static Extractor<Object, String> toStringMethod() { return new ToStringExtractor(); }
/** * Provides extractor for extracting single field or property from any object using reflection * @param <F> type to extract property from * @param fieldOrProperty the name of the field/property to extract * @return the built {@link Extractor} */ public static <F> Extractor<F, Object> byName(String fieldOrProperty) { return new ByNameSingleExtractor<>(fieldOrProperty); }
/** * Provides extractor for extracting multiple fields or properties from any object using reflection * @param <F> type to extract property from * @param fieldsOrProperties the name of the fields/properties to extract * @return the built {@link Extractor} */ public static <F> Extractor<F, Tuple> byName(String... fieldsOrProperties) { return new ByNameMultipleExtractor<>(fieldsOrProperties); }
/** * Provides extractor for extracting values by method name from any object using reflection * @param <F> type to extract property from * @param methodName the name of the method to execute * @return the built {@link Extractor} */ public static <F> Extractor<F, Object> resultOf(String methodName) { return new ResultOfExtractor<>(methodName); }
/** * Extract the values from Iterable's elements under test by applying an extracting function on them. The returned * iterable becomes a new object under test. * <p> * It allows to test values from the elements in more safe way than by using {@link #extracting(String)}, as it * doesn't utilize introspection. * <p> * Let's have a look at an example : * <pre><code class='java'> // Build a list of TolkienCharacter, a TolkienCharacter has a name, and age and a Race (a specific class) * // they can be public field or properties, both can be extracted. * List<TolkienCharacter> fellowshipOfTheRing = new ArrayList<TolkienCharacter>(); * * fellowshipOfTheRing.add(new TolkienCharacter("Frodo", 33, HOBBIT)); * fellowshipOfTheRing.add(new TolkienCharacter("Sam", 38, HOBBIT)); * fellowshipOfTheRing.add(new TolkienCharacter("Gandalf", 2020, MAIA)); * fellowshipOfTheRing.add(new TolkienCharacter("Legolas", 1000, ELF)); * fellowshipOfTheRing.add(new TolkienCharacter("Pippin", 28, HOBBIT)); * fellowshipOfTheRing.add(new TolkienCharacter("Gimli", 139, DWARF)); * fellowshipOfTheRing.add(new TolkienCharacter("Aragorn", 87, MAN); * fellowshipOfTheRing.add(new TolkienCharacter("Boromir", 37, MAN)); * * // this extracts the race * Extractor<TolkienCharacter, Race> race = new Extractor<TolkienCharacter, Race>() { * {@literal @}Override * public Race extract(TolkienCharacter input) { * return input.getRace(); * } * } * * // fellowship has hobbitses, right, my presioussss? * assertThat(fellowshipOfTheRing).extracting(TolkienCharacter::getRace).contains(HOBBIT);</code></pre> * * Note that the order of extracted property/field values is consistent with the iteration order of the Iterable under * test, for example if it's a {@link HashSet}, you won't be able to make any assumptions on the extracted values * order. * * @param <V> the type of elements extracted. * @param extractor the object transforming input object to desired one * @return a new assertion object whose object under test is the list of values extracted */ @CheckReturnValue public <V> AbstractListAssert<?, List<? extends V>, V, ObjectAssert<V>> extracting(Extractor<? super ELEMENT, V> extractor) { List<V> values = FieldsOrPropertiesExtractor.extract(actual, extractor); return newListAssertInstance(values); }
/** * Extract the Iterable values from Iterable's elements under test by applying an Iterable extracting function on them * and concatenating the result lists. The returned iterable becomes a new object under test. * <p> * It allows testing the results of extracting values that are represented by Iterables. * <p> * For example: * <pre><code class='java'> CartoonCharacter bart = new CartoonCharacter("Bart Simpson"); * CartoonCharacter lisa = new CartoonCharacter("Lisa Simpson"); * CartoonCharacter maggie = new CartoonCharacter("Maggie Simpson"); * CartoonCharacter homer = new CartoonCharacter("Homer Simpson"); * homer.addChildren(bart, lisa, maggie); * * CartoonCharacter pebbles = new CartoonCharacter("Pebbles Flintstone"); * CartoonCharacter fred = new CartoonCharacter("Fred Flintstone"); * fred.getChildren().add(pebbles); * * Extractor<CartoonCharacter, List<CartoonCharacter>> childrenOf = new Extractor<CartoonChildren, List<CartoonChildren>>() { * {@literal @}Override * public List<CartoonChildren> extract(CartoonCharacter input) { * return input.getChildren(); * } * } * * List<CartoonCharacter> parents = newArrayList(homer, fred); * // check children * assertThat(parent).flatExtracting(CartoonCharacter::getChildren) * .containsOnly(bart, lisa, maggie, pebbles);</code></pre> * * The order of extracted values is consistent with both the order of the collection itself, as well as the extracted * collections. * * @param <V> the type of elements extracted. * @param extractor the object transforming input object to an {@code Iterable} of desired ones * @return a new assertion object whose object under test is the list of values extracted * @throws NullPointerException if one of the {@code Iterable}'s element is null. */ @CheckReturnValue public <V> AbstractListAssert<?, List<? extends V>, V, ObjectAssert<V>> flatExtracting(Extractor<? super ELEMENT, ? extends Collection<V>> extractor) { return doFlatExtracting(extractor); }
/** * Extract the values from the array's elements by applying an extracting function on them, the resulting list becomes * the new object under test. * <p> * This method is similar to {@link #extracting(String)} but more refactoring friendly as it does not use introspection. * <p> * Let's take a look an example: * <pre><code class='java'> // Build a list of TolkienCharacter, a TolkienCharacter has a name, and age and a Race (a specific class) * // they can be public field or properties, both can be extracted. * TolkienCharacter[] fellowshipOfTheRing = new TolkienCharacter[] { * new TolkienCharacter("Frodo", 33, HOBBIT), * new TolkienCharacter("Sam", 38, HOBBIT), * new TolkienCharacter("Gandalf", 2020, MAIA), * new TolkienCharacter("Legolas", 1000, ELF), * new TolkienCharacter("Pippin", 28, HOBBIT), * new TolkienCharacter("Gimli", 139, DWARF), * new TolkienCharacter("Aragorn", 87, MAN, * new TolkienCharacter("Boromir", 37, MAN) * }; * * // fellowship has hobbitses, right, my presioussss? * assertThat(fellowshipOfTheRing).extracting(TolkienCharacter::getRace).contains(HOBBIT);</code></pre> * * Note that the order of extracted property/field values is consistent with the iteration order of the Iterable under * test, for example if it's a {@link HashSet}, you won't be able to make any assumptions on the extracted values * order. * * @param <U> the type of elements to extract. * @param extractor the object transforming input object to desired one * @return a new assertion object whose object under test is the list of extracted values. */ @CheckReturnValue public <U> AbstractListAssert<?, List<? extends U>, U, ObjectAssert<U>> extracting(Extractor<? super ELEMENT, U> extractor) { List<U> values = FieldsOrPropertiesExtractor.extract(Arrays.asList(actual), extractor); return newListAssertInstance(values); }
/** * Extract the Iterable values from arrays elements under test by applying an Iterable extracting function on them * and concatenating the result lists into an array which becomes the new object under test. * <p> * It allows testing the results of extracting values that are represented by Iterables. * <p> * For example: * <pre><code class='java'> CartoonCharacter bart = new CartoonCharacter("Bart Simpson"); * CartoonCharacter lisa = new CartoonCharacter("Lisa Simpson"); * CartoonCharacter maggie = new CartoonCharacter("Maggie Simpson"); * CartoonCharacter homer = new CartoonCharacter("Homer Simpson"); * homer.addChildren(bart, lisa, maggie); * * CartoonCharacter pebbles = new CartoonCharacter("Pebbles Flintstone"); * CartoonCharacter fred = new CartoonCharacter("Fred Flintstone"); * fred.getChildren().add(pebbles); * * CartoonCharacter[] parents = new CartoonCharacter[] { homer, fred }; * // check children * assertThat(parents).flatExtracting(CartoonCharacter::getChildren) * .containsOnly(bart, lisa, maggie, pebbles);</code></pre> * * The order of extracted values is consisted with both the order of the collection itself, as well as the extracted * collections. * * @param <V> the type of elements to extract. * @param <C> the type of collection to flat/extract. * @param extractor the object transforming input object to an Iterable of desired ones * @return a new assertion object whose object under test is the list of values extracted */ @CheckReturnValue public <V, C extends Collection<V>> AbstractListAssert<?, List<? extends V>, V, ObjectAssert<V>> flatExtracting(Extractor<? super ELEMENT, C> extractor) { return doFlatExtracting(extractor); }
/** * Extract the Iterable values from the array's elements by applying an Iterable extracting function on them * and concatenating the result lists into an array which becomes the new object under test. * <p> * It allows testing the results of extracting values that are represented by Iterables. * <p> * For example: * <pre><code class='java'> CartoonCharacter bart = new CartoonCharacter("Bart Simpson"); * CartoonCharacter lisa = new CartoonCharacter("Lisa Simpson"); * CartoonCharacter maggie = new CartoonCharacter("Maggie Simpson"); * CartoonCharacter homer = new CartoonCharacter("Homer Simpson"); * homer.addChildren(bart, lisa, maggie); * * CartoonCharacter pebbles = new CartoonCharacter("Pebbles Flintstone"); * CartoonCharacter fred = new CartoonCharacter("Fred Flintstone"); * fred.getChildren().add(pebbles); * * Extractor<CartoonCharacter, List<CartoonCharacter>> childrenOf = new Extractor<CartoonCharacter, List<CartoonCharacter>>() { * {@literal @}Override * public List<CartoonChildren> extract(CartoonCharacter input) { * return input.getChildren(); * } * } * * AtomicReferenceArray<CartoonCharacter> parents = new AtomicReferenceArray<>(new CartoonCharacter[]{ homer, fred }); * // check children * assertThat(parents).flatExtracting(childrenOf) * .containsOnly(bart, lisa, maggie, pebbles);</code></pre> * * The order of extracted values is consisted with both the order of the collection itself, as well as the extracted * collections. * * @param <U> the type of elements to extract. * @param <C> the type of collection to flat/extract. * @param extractor the object transforming input object to an Iterable of desired ones * @return a new assertion object whose object under test is the list of values extracted * @since 2.7.0 / 3.7.0 */ @CheckReturnValue public <U, C extends Collection<U>> ObjectArrayAssert<U> flatExtracting(Extractor<? super T, C> extractor) { return doFlatExtracting(extractor); }