Example usage for org.apache.commons.collections4.iterators PermutationIterator forEachRemaining

List of usage examples for org.apache.commons.collections4.iterators PermutationIterator forEachRemaining

Introduction

In this page you can find the example usage for org.apache.commons.collections4.iterators PermutationIterator forEachRemaining.

Prototype

default void forEachRemaining(Consumer<? super E> action) 

Source Link

Document

Performs the given action for each remaining element until all elements have been processed or the action throws an exception.

Usage

From source file:de.richtercloud.validation.tools.ValidationToolsTest.java

@Property
public void testBuildConstraintVioloationMessage(@ForAll boolean bean2Null, @ForAll int bean2Property0,
        @ForAll boolean bean1Null, @ForAll boolean bean1Bean2sNull, @ForAll boolean bean1bean2sEmpty,
        @ForAll @WithNull(0.5) String bean0Property0, @ForAll boolean bean0Valid, @ForAll boolean bean2Valid)
        throws Exception {
    LOGGER.info("testBuildConstraintVioloationMessage");
    LOGGER.trace("testBuildConstraintVioloationMessage bean2Null: {}", bean2Null);
    LOGGER.trace("testBuildConstraintVioloationMessage bean2Property0: {}", bean2Property0);
    LOGGER.trace("testBuildConstraintVioloationMessage bean1Null: {}", bean1Null);
    LOGGER.trace("testBuildConstraintVioloationMessage bean1Bean2sNull: {}", bean1Bean2sNull);
    LOGGER.trace("testBuildConstraintVioloationMessage bean1bean2sEmpty: {}", bean1bean2sEmpty);
    LOGGER.trace("testBuildConstraintVioloationMessage bean0Property0: {}", bean0Property0);
    LOGGER.trace("testBuildConstraintVioloationMessage bean0Valid: {}", bean0Valid);
    LOGGER.trace("testBuildConstraintVioloationMessage bean2Valid: {}", bean2Valid);
    Bean0Validator.retValue = bean0Valid;
    Bean2Validator.retValue = bean2Valid;
    Bean2 bean2;/*from w  w w  .  j  av  a  2 s  .  com*/
    if (bean2Null) {
        bean2 = null;
    } else {
        bean2 = new Bean2(bean2Property0);
    }
    Bean1 bean1;
    if (bean1Null) {
        bean1 = null;
    } else {
        List<Bean2> bean1Bean2s;
        if (bean1Bean2sNull) {
            bean1Bean2s = null;
        } else {
            //Bean1.bean2s has @Min(1)
            bean1Bean2s = new ArrayList<>();
            if (!bean1bean2sEmpty) {
                bean1Bean2s.add(bean2);
            }
        }
        bean1 = new Bean1(bean1Bean2s);
    }
    Bean0 bean0 = new Bean0(bean1, bean0Property0);
    Set<ConstraintViolation<Object>> violations = Validation.buildDefaultValidatorFactory().getValidator()
            .validate(bean0);
    FieldRetriever fieldRetriever = new CachedFieldRetriever();
    FieldNameLambda fieldNameLambda = field -> "+++" + field.getName() + ",,,";
    Map<Path, String> pathDescriptionMap = new HashMap<>();
    OutputMode outputMode = OutputMode.PLAIN_TEXT;
    LOGGER.debug("validations.size: {}", violations.size());
    if (violations.isEmpty()) {
        Assertions.assertThrows(IllegalArgumentException.class,
                () -> ValidationTools.buildConstraintVioloationMessage(violations, bean0, fieldRetriever,
                        pathDescriptionMap, fieldNameLambda, false, //skipPathes
                        outputMode));
        return;
    }
    Set<String> messages = new HashSet<>();
    if (!bean0Valid) {
        messages.add("invalid Bean0");
    }
    if (bean0Property0 == null) {
        messages.add("+++property0,,,: darf nicht null sein");
    }
    if (bean1Null) {
        messages.add("+++bean1,,,: darf nicht null sein");
    } else {
        if (!bean1Bean2sNull) {
            //bean1bean2sNull is valid
            if (!bean1bean2sEmpty) {
                if (!bean2Null) {
                    if (!bean2Valid) {
                        messages.add("+++bean1,,,: +++bean2s,,,: invalid Bean2");
                    }
                    if (bean2Property0 < 1) {
                        messages.add(
                                "+++bean1,,,: +++bean2s,,,: +++property0,,,: muss grer oder gleich 1 sein");
                    }
                }
            } else {
                messages.add("+++bean1,,,: +++bean2s,,,: Bean1.bean2s mustn't be empty");
            }
        }
    }
    Set<List<String>> permutations = new HashSet<>();
    PermutationIterator<String> textsPermutationIterator = new PermutationIterator<>(messages);
    textsPermutationIterator.forEachRemaining(permutation -> permutations.add(permutation));
    Matcher<String> expResult = anyOf(permutations.stream()
            .map(permutation -> equalTo(
                    StreamEx.of(permutation).joining("\n", "The following constraints are violated:\n",
                            "\nFix the corresponding values in the components.")))
            .collect(Collectors.toSet()));
    String result = ValidationTools.buildConstraintVioloationMessage(violations, bean0, fieldRetriever,
            pathDescriptionMap, fieldNameLambda, false, //skipPathes
            outputMode);
    assertTrue(String.format("result was: %s; expResult was: %s", result, expResult),
            expResult.matches(result));
}