Internal used predicate.
- */
- private final Predicate predicate;
-
- /**
- * Constructor using a predicate that will be used to test the value.
- *
- * @param predicate The predicate function
- * @throws NullPointerException If the predicate is {@code null}
- */
- public Constraint(final Predicate predicate) {
- if (null == predicate) {
- throw new NullPointerException("Invalid predicate (not null expected)");
- }
- this.predicate = predicate;
- }
-
- /**
- * Check if the value passes the predicate.
- *
- * @param value The value to check
- * @return {@code true} if the value passes the predicate
- */
- public boolean check(final V value) {
- return predicate.test(value);
- }
-
- @Override
- public double calculate(final V value) {
- return predicate.test(value) ? 1.0d : 0.0d;
- }
-
- /**
- * Vanilla constructor for easiest uses with generic.
- *
- * @param value's type
- * @param predicate The predicate function
- * @return The instantiated {@code Constraint}
- */
- public static Constraint of(final Predicate predicate) {
- return new Constraint<>(predicate);
- }
-}
\ No newline at end of file
diff --git a/src/main/java/org/mender/criteria/Criterion.java b/src/main/java/org/mender/criteria/Criterion.java
deleted file mode 100644
index 1742167..0000000
--- a/src/main/java/org/mender/criteria/Criterion.java
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
-MIT License
-
-Copyright (c) 2018 Alexis Jehan
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all
-copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-SOFTWARE.
-*/
-package org.mender.criteria;
-
-/**
- * A {@code Criterion} can be used to calculate a score from a value.
- *
- * @param value's type
- * @since 1.0
- */
-@FunctionalInterface
-public interface Criterion {
-
- /**
- * Calculate value's score.
- *
- * @param value The value to get the score from
- * @return The calculated score
- */
- double calculate(final V value);
-}
\ No newline at end of file
diff --git a/src/main/java/org/mender/criteria/Estimation.java b/src/main/java/org/mender/criteria/Estimation.java
deleted file mode 100644
index 35742bb..0000000
--- a/src/main/java/org/mender/criteria/Estimation.java
+++ /dev/null
@@ -1,107 +0,0 @@
-/*
-MIT License
-
-Copyright (c) 2018 Alexis Jehan
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all
-copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-SOFTWARE.
-*/
-package org.mender.criteria;
-
-import java.util.HashMap;
-import java.util.Map;
-import java.util.concurrent.atomic.AtomicLong;
-import java.util.function.Function;
-
-/**
- * A {@link Criterion} that collects values, the score is equal to the frequency among all collected values.
- *
- * Note: Values are transformed using a function to be stored and collected
- * (example: collect by features).
- *
- * @param value's type
- * @param value's store type
- * @since 1.0
- */
-public class Estimation implements Criterion {
-
- /**
- * Internal used function.
- */
- private final Function function;
-
- /**
- * Bag of stored values.
- */
- private final Map bag = new HashMap<>();
-
- /**
- * Total values in the bag (different of the total of unique values).
- */
- private long total = 0;
-
- /**
- * Constructor using a function that will be used to be applied to values.
- *
- * @param function The function
- * @throws NullPointerException If the function is {@code null}
- */
- public Estimation(final Function function) {
- if (null == function) {
- throw new NullPointerException("Invalid function (not null expected)");
- }
- this.function = function;
- }
-
- /**
- * Adjust by adding a value.
- *
- * @param value The value to add
- */
- public void adjust(final V value) {
- final S key = function.apply(value);
- if (bag.containsKey(key)) {
- bag.get(key).incrementAndGet();
- } else {
- bag.put(key, new AtomicLong(1L));
- }
- ++total;
- }
-
- @Override
- public double calculate(final V value) {
- final S key = function.apply(value);
- if (0 < total && bag.containsKey(key)) {
- return (double) bag.get(key).get() / total;
- } else {
- return 0.0d;
- }
- }
-
- /**
- * Vanilla constructor for easiest uses with generic.
- *
- * @param value's type
- * @param value's store type
- * @param function The function
- * @return The instantiated {@code Estimation}
- */
- public static Estimation of(final Function function) {
- return new Estimation<>(function);
- }
-}
\ No newline at end of file
diff --git a/src/main/java/org/mender/criteria/package-info.java b/src/main/java/org/mender/criteria/package-info.java
deleted file mode 100644
index 10ac908..0000000
--- a/src/main/java/org/mender/criteria/package-info.java
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
-MIT License
-
-Copyright (c) 2018 Alexis Jehan
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all
-copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-SOFTWARE.
-*/
-/**
- * Criteria components that can be used by an {@link org.mender.Evaluator} to compute a score.
- *
- * @since 1.0
- */
-package org.mender.criteria;
\ No newline at end of file
diff --git a/src/main/java/org/mender/dsv/DsvBuilder.java b/src/main/java/org/mender/dsv/DsvBuilder.java
deleted file mode 100644
index 43d852c..0000000
--- a/src/main/java/org/mender/dsv/DsvBuilder.java
+++ /dev/null
@@ -1,602 +0,0 @@
-/*
-MIT License
-
-Copyright (c) 2018 Alexis Jehan
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all
-copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-SOFTWARE.
-*/
-package org.mender.dsv;
-
-import java.util.function.Function;
-import java.util.function.Predicate;
-import java.util.regex.Pattern;
-import java.util.stream.IntStream;
-
-import org.mender.criteria.Constraint;
-import org.mender.criteria.Estimation;
-
-/**
- * Builder pattern implementation to easily create new {@link DsvMender} instances.
- *
- * @since 1.0
- */
-public final class DsvBuilder {
-
- /**
- * {@code DsvEvaluator} object.
- */
- private final DsvEvaluator evaluator = new DsvEvaluator();
-
- /**
- * String delimiter of the DSV data.
- */
- private final String delimiter;
-
- /**
- * Number of columns of the DSV data.
- */
- private final int nbColumns;
-
- /**
- * Maximum depth of the nodes tree.
- */
- private final int maxDepth;
-
- /**
- * Construct a new {@code DsvBuilder}.
- *
- * @param delimiter {@code String} delimiter of the DSV data
- * @param nbColumns Number of columns of the DSV data
- * @param maxDepth Maximum depth of the nodes tree
- */
- DsvBuilder(final String delimiter, final int nbColumns, final int maxDepth) {
- this.delimiter = delimiter;
- this.nbColumns = nbColumns;
- this.maxDepth = maxDepth;
- }
-
- // EMPTY
- /**
- * Add {@code String.isEmpty()} estimations to all columns of the DSV data.
- *
- * @return The {@code DsvBuilder} instance
- */
- public DsvBuilder withEmptyEstimations() {
- return withEstimations(String::isEmpty);
- }
-
- /**
- * Add a {@code String.isEmpty()} estimation to the column at specified index of the DSV data.
- *
- * @param index Index of the column
- * @return The {@code DsvBuilder} instance
- */
- public DsvBuilder withEmptyEstimation(final int index) {
- return withEstimation(index, String::isEmpty);
- }
-
- /**
- * Add a {@code String.isEmpty()} constraint to the column at specified index of the DSV data. In others words
- * that means this column should always be empty.
- *
- * @param index Index of the column
- * @return The {@code DsvBuilder} instance
- */
- public DsvBuilder withEmptyConstraint(final int index) {
- return withConstraint(index, String::isEmpty);
- }
-
- // NON EMPTY
- /**
- * Add {@code String} non-empty constraints to all columns of the DSV data. In others words that means all
- * columns should never be empty.
- *
- * @return The {@code DsvBuilder} instance
- */
- public DsvBuilder withNonEmptyConstraints() {
- return withConstraints(value -> !value.isEmpty());
- }
-
- /**
- * Add a {@code String} non-empty constraint to the column at specified index of the DSV data. In others words
- * that means this column should never be empty.
- *
- * @param index Index of the column
- * @return The {@code DsvBuilder} instance
- */
- public DsvBuilder withNonEmptyConstraint(final int index) {
- return withConstraint(index, value -> !value.isEmpty());
- }
-
- // LENGTH
- /**
- * Add {@code String.length()} estimations to all columns of the DSV data.
- *
- * @return The {@code DsvBuilder} instance
- */
- public DsvBuilder withLengthEstimations() {
- return withEstimations(String::length);
- }
-
- /**
- * Add a {@code String.length()} estimation to the column at specified index of the DSV data.
- *
- * @param index Index of the column
- * @return The {@code DsvBuilder} instance
- */
- public DsvBuilder withLengthEstimation(final int index) {
- return withEstimation(index, String::length);
- }
-
- /**
- * Add a {@code String.length()} constraint to the column at specified index of the DSV data. In others words
- * that means each value of this column should always have that length.
- *
- * @param index Index of the column
- * @param length The required length for specified column values
- * @return The {@code DsvBuilder} instance
- * @throws IllegalArgumentException If the length is less than 0
- */
- public DsvBuilder withLengthConstraint(final int index, final int length) {
- if (0 > length) {
- throw new IllegalArgumentException("Invalid length: " + length + " (greater than or equal to 0 expected)");
- }
- return withConstraint(index, value -> length == value.length());
- }
-
- // MIN LENGTH
- /**
- * Add {@code String} minimum length estimations to all columns of the DSV data.
- *
- * @param minLength The estimated minimum length for all column values
- * @return The {@code DsvBuilder} instance
- * @throws IllegalArgumentException If the minimum length is less than or equal to 0
- */
- public DsvBuilder withMinLengthEstimations(final int minLength) {
- if (1 > minLength) {
- throw new IllegalArgumentException("Invalid minimum length: " + minLength + " (greater than 0 expected)");
- }
- return withEstimations(value -> minLength <= value.length());
- }
-
- /**
- * Add a {@code String} minimum length estimation to the column at specified index of the DSV data.
- *
- * @param index Index of the column
- * @param minLength The estimated minimum length for specified column values
- * @return The {@code DsvBuilder} instance
- * @throws IllegalArgumentException If the minimum length is less than or equal to 0
- */
- public DsvBuilder withMinLengthEstimation(final int index, final int minLength) {
- if (1 > minLength) {
- throw new IllegalArgumentException("Invalid minimum length: " + minLength + " (greater than 0 expected)");
- }
- return withEstimation(index, value -> minLength <= value.length());
- }
-
- /**
- * Add a {@code String} minimum length constraint to the column at specified index of the DSV data. In others
- * words that means each value of this column should always have that minimum length.
- *
- * @param index Index of the column
- * @param minLength The required minimum length for specified column values
- * @return The {@code DsvBuilder} instance
- * @throws IllegalArgumentException If the minimum length is less than or equal to 0
- */
- public DsvBuilder withMinLengthConstraint(final int index, final int minLength) {
- if (1 > minLength) {
- throw new IllegalArgumentException("Invalid minimum length: " + minLength + " (greater than 0 expected)");
- }
- return withConstraint(index, value -> minLength <= value.length());
- }
-
- // MAX LENGTH
- /**
- * Add {@code String} maximum length estimations to all columns of the DSV data.
- *
- * @param maxLength The estimated maximum length for all column values
- * @return The {@code DsvBuilder} instance
- * @throws IllegalArgumentException If the maximum length is less than or equal to 0
- */
- public DsvBuilder withMaxLengthEstimations(final int maxLength) {
- if (1 > maxLength) {
- throw new IllegalArgumentException("Invalid maximum length: " + maxLength + " (greater than 0 expected)");
- }
- return withEstimations(value -> maxLength >= value.length());
- }
-
- /**
- * Add a {@code String} maximum length estimation to the column at specified index of the DSV data.
- *
- * @param index Index of the column
- * @param maxLength The estimated maximum length for specified column values
- * @return The {@code DsvBuilder} instance
- * @throws IllegalArgumentException If the maximum length is less than or equal to 0
- */
- public DsvBuilder withMaxLengthEstimation(final int index, final int maxLength) {
- if (1 > maxLength) {
- throw new IllegalArgumentException("Invalid maximum length: " + maxLength + " (greater than 0 expected)");
- }
- return withEstimation(index, value -> maxLength >= value.length());
- }
-
- /**
- * Add a {@code String} maximum length constraint to the column at specified index of the DSV data. In others
- * words that means each value of this column should always have that maximum length.
- *
- * @param index Index of the column
- * @param maxLength The required maximum length for specified column values
- * @return The {@code DsvBuilder} instance
- * @throws IllegalArgumentException If the maximum length is less than or equal to 0
- */
- public DsvBuilder withMaxLengthConstraint(final int index, final int maxLength) {
- if (1 > maxLength) {
- throw new IllegalArgumentException("Invalid maximum length: " + maxLength + " (greater than 0 expected)");
- }
- return withConstraint(index, value -> maxLength >= value.length());
- }
-
- // RANGE LENGTH
- /**
- * Add {@code String} range length estimations to all columns of the DSV data.
- *
- * @param minLength The estimated minimum length for all column values
- * @param maxLength The estimated maximum length for all column values
- * @return The {@code DsvBuilder} instance
- * @throws IllegalArgumentException If the minimum length is less than or equal to 0 or if the maximum length is
- * greater than the minimum length
- */
- public DsvBuilder withRangeLengthEstimations(final int minLength, final int maxLength) {
- if (1 > minLength) {
- throw new IllegalArgumentException("Invalid maximum length: " + minLength + " (greater than 0 expected)");
- }
- if (minLength + 1 > maxLength) {
- throw new IllegalArgumentException("Invalid maximum length: " + maxLength + " (greater than minimum length expected)");
- }
- return withEstimations(value -> minLength <= value.length() && maxLength >= value.length());
- }
-
- /**
- * Add a {@code String} range length estimation to the column at specified index of the DSV data.
- *
- * @param index Index of the column
- * @param minLength The estimated minimum length for specified column values
- * @param maxLength The estimated maximum length for specified column values
- * @return The {@code DsvBuilder} instance
- * @throws IllegalArgumentException If the minimum length is less than or equal to 0 or if the maximum length is
- * greater than the minimum length
- */
- public DsvBuilder withRangeLengthEstimation(final int index, final int minLength, final int maxLength) {
- if (1 > minLength) {
- throw new IllegalArgumentException("Invalid maximum length: " + minLength + " (greater than 0 expected)");
- }
- if (minLength + 1 > maxLength) {
- throw new IllegalArgumentException("Invalid maximum length: " + maxLength + " (greater than minimum length expected)");
- }
- return withEstimation(index, value -> minLength <= value.length() && maxLength >= value.length());
- }
-
- /**
- * Add a {@code String} range length constraint to the column at specified index of the DSV data. In others
- * words that means each value of this column should always have that minimum and maximum lengths.
- *
- * @param index Index of the column
- * @param minLength The required minimum length for specified column values
- * @param maxLength The required maximum length for specified column values
- * @return The {@code DsvBuilder} instance
- * @throws IllegalArgumentException If the minimum length is less than or equal to 0 or if the maximum length is
- * greater than the minimum length
- */
- public DsvBuilder withRangeLengthConstraint(final int index, final int minLength, final int maxLength) {
- if (1 > minLength) {
- throw new IllegalArgumentException("Invalid maximum length: " + minLength + " (greater than 0 expected)");
- }
- if (minLength + 1 > maxLength) {
- throw new IllegalArgumentException("Invalid maximum length: " + maxLength + " (greater than minimum length expected)");
- }
- return withConstraint(index, value -> minLength <= value.length() && maxLength >= value.length());
- }
-
- // PATTERN
- /**
- * Add {@link Pattern} estimations to all columns of the DSV data.
- *
- * @param pattern The estimated matched {@code Pattern} by all column values
- * @return The {@code DsvBuilder} instance
- * @throws NullPointerException If the pattern is null
- */
- public DsvBuilder withPatternEstimations(final Pattern pattern) {
- if (null == pattern) {
- throw new NullPointerException("Invalid pattern (not null expected)");
- }
- return withEstimations(value -> pattern.matcher(value).matches());
- }
-
- /**
- * Add a {@link Pattern} estimation to the column at specified index of the DSV data.
- *
- * @param index Index of the column
- * @param pattern The estimated matched {@code Pattern} by specified column values
- * @return The {@code DsvBuilder} instance
- * @throws NullPointerException If the pattern is {@code null}
- */
- public DsvBuilder withPatternEstimation(final int index, final Pattern pattern) {
- if (null == pattern) {
- throw new NullPointerException("Invalid pattern (not null expected)");
- }
- return withEstimation(index, value -> pattern.matcher(value).matches());
- }
-
- /**
- * Add a {@link Pattern} constraint to the column at specified index of the DSV data. In others
- * words that means each value of this column should always match that {@code Pattern}.
- *
- * @param index Index of the column
- * @param pattern The required matched {@code Pattern} by specified column values
- * @return The {@code DsvBuilder} instance
- * @throws NullPointerException If the pattern is {@code null}
- */
- public DsvBuilder withPatternConstraint(final int index, final Pattern pattern) {
- if (null == pattern) {
- throw new NullPointerException("Invalid pattern (not null expected)");
- }
- return withConstraint(index, value -> pattern.matcher(value).matches());
- }
-
- // CONTAINS
- /**
- * Add {@code String.contains()} estimations to all columns of the DSV data.
- *
- * @param substring The estimated contained substring by all column values
- * @return The {@code DsvBuilder} instance
- * @throws NullPointerException If the substring is {@code null}
- */
- public DsvBuilder withContainsEstimations(final String substring) {
- if (null == substring) {
- throw new NullPointerException("Invalid substring (not null expected)");
- }
- return withEstimations(value -> value.contains(substring));
- }
-
- /**
- * Add a {@code String.contains()} estimation to the column at specified index of the DSV data.
- *
- * @param index Index of the column
- * @param substring The estimated contained substring by specified column values
- * @return The {@code DsvBuilder} instance
- * @throws NullPointerException If the substring is {@code null}
- */
- public DsvBuilder withContainsEstimation(final int index, final String substring) {
- if (null == substring) {
- throw new NullPointerException("Invalid substring (not null expected)");
- }
- return withEstimation(index, value -> value.contains(substring));
- }
-
- /**
- * Add a {@code String.contains()} constraint to the column at specified index of the DSV data. In others words
- * that means each value of this column should always contain the substring.
- *
- * @param index Index of the column
- * @param substring The required contained substring by specified column values
- * @return The {@code DsvBuilder} instance
- * @throws NullPointerException If the substring is {@code null}
- */
- public DsvBuilder withContainsConstraint(final int index, final String substring) {
- if (null == substring) {
- throw new NullPointerException("Invalid substring (not null expected)");
- }
- return withConstraint(index, value -> value.contains(substring));
- }
-
- // CONTAINS NONE
- /**
- * Add a {@code String} contains-none constraint to the column at specified index of the DSV data. In others
- * words that means each value of this column should never contain the substring.
- *
- * @param index Index of the column
- * @param substring The required not contained substring by specified column values
- * @return The {@code DsvBuilder} instance
- * @throws NullPointerException If the substring is {@code null}
- */
- public DsvBuilder withContainsNoneConstraint(final int index, final String substring) {
- if (null == substring) {
- throw new NullPointerException("Invalid substring (not null expected)");
- }
- return withConstraint(index, value -> !value.contains(substring));
- }
-
- // STARTS WITH
- /**
- * Add {@code String.startsWith()} estimations to all columns of the DSV data.
- *
- * @param prefix The estimated prefix of all column values
- * @return The {@code DsvBuilder} instance
- * @throws NullPointerException If the prefix is {@code null}
- */
- public DsvBuilder withStartsWithEstimations(final String prefix) {
- if (null == prefix) {
- throw new NullPointerException("Invalid prefix (not null expected)");
- }
- return withEstimations(value -> value.startsWith(prefix));
- }
-
- /**
- * Add a {@code String.startsWith()} estimation to the column at specified index of the DSV data.
- *
- * @param index Index of the column
- * @param prefix The estimated prefix of specified column values
- * @return The {@code DsvBuilder} instance
- * @throws NullPointerException If the prefix is {@code null}
- */
- public DsvBuilder withStartsWithEstimation(final int index, final String prefix) {
- if (null == prefix) {
- throw new NullPointerException("Invalid prefix (not null expected)");
- }
- return withEstimation(index, value -> value.startsWith(prefix));
- }
-
- /**
- * Add a {@code String.startsWith()} constraint to the column at specified index of the DSV data. In others
- * words that means each value of this column should always start with the prefix.
- *
- * @param index Index of the column
- * @param prefix The required prefix of specified column values
- * @return The {@code DsvBuilder} instance
- * @throws NullPointerException If the prefix is {@code null}
- */
- public DsvBuilder withStartsWithConstraint(final int index, final String prefix) {
- if (null == prefix) {
- throw new NullPointerException("Invalid prefix (not null expected)");
- }
- return withConstraint(index, value -> value.startsWith(prefix));
- }
-
- // ENDS WITH
- /**
- * Add {@code String.endsWith()} estimations to all columns of the DSV data.
- *
- * @param suffix The estimated suffix of all column values
- * @return The {@code DsvBuilder} instance
- * @throws NullPointerException If the suffix is {@code null}
- */
- public DsvBuilder withEndsWithEstimations(final String suffix) {
- if (null == suffix) {
- throw new NullPointerException("Invalid suffix (not null expected)");
- }
- return withEstimations(value -> value.endsWith(suffix));
- }
-
- /**
- * Add a {@code String.endsWith()} estimation to the column at specified index of the DSV data.
- *
- * @param index Index of the column
- * @param suffix The estimated suffix of specified column values
- * @return The {@code DsvBuilder} instance
- * @throws NullPointerException If the suffix is {@code null}
- */
- public DsvBuilder withEndsWithEstimation(final int index, final String suffix) {
- if (null == suffix) {
- throw new NullPointerException("Invalid suffix (not null expected)");
- }
- return withEstimation(index, value -> value.endsWith(suffix));
- }
-
- /**
- * Add a {@code String.endsWith()} constraint to the column at specified index of the DSV data. In others words
- * that means each value of this column should always end with the suffix.
- *
- * @param index Index of the column
- * @param suffix The required suffix of specified column values
- * @return The {@code DsvBuilder} instance
- * @throws NullPointerException If the suffix is {@code null}
- */
- public DsvBuilder withEndsWithConstraint(final int index, final String suffix) {
- if (null == suffix) {
- throw new NullPointerException("Invalid suffix (not null expected)");
- }
- return withConstraint(index, value -> value.endsWith(suffix));
- }
-
- // CUSTOM
- /**
- * Add custom estimation function to all columns of the DSV data.
- *
- * @param function The custom estimation function for all column values
- * @return The {@code DsvBuilder} instance
- */
- public DsvBuilder withEstimations(final Function function) {
- IntStream.range(0, nbColumns).forEach(i -> evaluator.addEstimation(i, Estimation.of(function)));
- return this;
- }
-
- /**
- * Add a custom estimation function to the column at specified index of the DSV data.
- *
- * @param index Index of the column
- * @param function The custom estimation function for specified column values
- * @return The {@code DsvBuilder} instance
- */
- public DsvBuilder withEstimation(final int index, final Function function) {
- return withEstimation(index, Estimation.of(function));
- }
-
- /**
- * Add a custom estimation to the column at specified index of the DSV data.
- *
- * @param index Index of the column
- * @param estimation The custom estimation for specified column values
- * @return The {@code DsvBuilder} instance
- * @throws IndexOutOfBoundsException If the index is not valid
- */
- public DsvBuilder withEstimation(final int index, final Estimation estimation) {
- if (0 > index || nbColumns <= index) {
- throw new IndexOutOfBoundsException("Invalid index: " + index + " (between 0 and " + (nbColumns - 1) + " expected)");
- }
- evaluator.addEstimation(index, estimation);
- return this;
- }
-
- /**
- * Add a custom constraint predicate to all columns of the DSV data.
- *
- * @param predicate The custom constraint predicate for all column values
- * @return The {@code DsvBuilder} instance
- */
- public DsvBuilder withConstraints(final Predicate predicate) {
- IntStream.range(0, nbColumns).forEach(i -> evaluator.addConstraint(i, Constraint.of(predicate)));
- return this;
- }
-
- /**
- * Add a custom constraint predicate to the column at specified index of the DSV data.
- *
- * @param index Index of the column
- * @param predicate The custom constraint predicate for specified column values
- * @return The {@code DsvBuilder} instance
- */
- public DsvBuilder withConstraint(final int index, final Predicate predicate) {
- return withConstraint(index, Constraint.of(predicate));
- }
-
- /**
- * Add a custom constraint to the column at specified index of the DSV data.
- *
- * @param index Index of the column
- * @param constraint The custom constraint for specified column values
- * @return The {@code DsvBuilder} instance
- * @throws IndexOutOfBoundsException If the index is not valid
- */
- public DsvBuilder withConstraint(final int index, final Constraint constraint) {
- if (0 > index || nbColumns <= index) {
- throw new IndexOutOfBoundsException("Invalid index: " + index + " (between 0 and " + (nbColumns - 1) + " expected)");
- }
- evaluator.addConstraint(index, constraint);
- return this;
- }
-
- /**
- * Build and return a {@code DsvMender} instance using configured attributes.
- *
- * @return The {@code DsvMender} instance
- */
- public DsvMender build() {
- return new DsvMender(evaluator, delimiter, nbColumns, maxDepth);
- }
-}
\ No newline at end of file
diff --git a/src/main/java/org/mender/dsv/DsvEvaluator.java b/src/main/java/org/mender/dsv/DsvEvaluator.java
deleted file mode 100644
index f9c80f2..0000000
--- a/src/main/java/org/mender/dsv/DsvEvaluator.java
+++ /dev/null
@@ -1,148 +0,0 @@
-/*
-MIT License
-
-Copyright (c) 2018 Alexis Jehan
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all
-copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-SOFTWARE.
-*/
-package org.mender.dsv;
-
-import java.util.HashMap;
-import java.util.Map;
-
-import org.mender.Evaluator;
-import org.mender.criteria.Constraint;
-import org.mender.criteria.Estimation;
-
-/**
- * A {@link Evaluator} implementation for the DSV format.
- *
- * Score computation: (1 * c[1] * c2] * c[3]...) * (e[1] + e[2] + e[3]...)
- * (with c[n] the n-th constraint score and e[n] the n-th estimation score)
- *
- * @since 1.0
- */
-class DsvEvaluator implements Evaluator {
-
- /**
- * Map of constraints associated to column indexes.
- */
- private final Map, Integer> constraints = new HashMap<>();
-
- /**
- * Map of estimations associated to column indexes.
- */
- private final Map, Integer> estimations = new HashMap<>();
-
- /**
- * Add a new {@code Constraint} at the specified index.
- *
- * @param index The related column index
- * @param constraint The constraint to register
- * @throws NullPointerException If the constraint is {@code null}
- * @throws IndexOutOfBoundsException If the index is negative
- */
- public void addConstraint(final int index, final Constraint constraint) {
- if (null == constraint) {
- throw new NullPointerException("Invalid constraint (not null expected)");
- }
- if (0 > index) {
- throw new IndexOutOfBoundsException("Invalid index: " + index + " (greater than or equal to 0 expected)");
- }
- constraints.put(constraint, index);
- }
-
- /**
- * Add a new {@code Estimation} at the specified index.
- *
- * @param index The related column index
- * @param estimation The estimation to register
- * @throws NullPointerException If the estimation is {@code null}
- * @throws IndexOutOfBoundsException If the index is negative
- */
- public void addEstimation(final int index, final Estimation estimation) {
- if (null == estimation) {
- throw new NullPointerException("Invalid estimation (not null expected)");
- }
- if (0 > index) {
- throw new IndexOutOfBoundsException("Invalid index: " + index + " (greater than or equal to 0 expected)");
- }
- estimations.put(estimation, index);
- }
-
- /**
- * Check constraints using given values.
- *
- * @param values Values to use to check
- * @return {@code true} if values pass all constraints
- * @throws NullPointerException If values are {@code null}
- */
- @Override
- public boolean checkConstraints(final String[] values) {
- if (null == values) {
- throw new NullPointerException("Invalid values (not null expected)");
- }
- for (final Map.Entry, Integer> constraint : constraints.entrySet()) {
- if (!constraint.getKey().check(values[constraint.getValue()])) {
- return false;
- }
- }
- return true;
- }
-
- /**
- * Adjust estimations using given values.
- *
- * @param values Values to use to adjust
- * @throws NullPointerException If values are {@code null}
- */
- @Override
- public void adjustEstimations(final String[] values) {
- if (null == values) {
- throw new NullPointerException("Invalid values (not null expected)");
- }
- for (final Map.Entry, Integer> estimation : estimations.entrySet()) {
- estimation.getKey().adjust(values[estimation.getValue()]);
- }
- }
-
- /**
- * Evaluate given values to get a computed score.
- *
- * @param values Values to evaluate
- * @throws NullPointerException If values are {@code null}
- */
- @Override
- public double evaluate(final String[] values) {
- if (null == values) {
- throw new NullPointerException("Invalid values (not null expected)");
- }
- return
- constraints.entrySet()
- .stream()
- .mapToDouble(constraint -> constraint.getKey().calculate(values[constraint.getValue()]))
- .reduce(1, (a, b) -> a * b) *
- (
- estimations.isEmpty() ? 1.0d : estimations.entrySet()
- .stream()
- .mapToDouble(estimation -> estimation.getKey().calculate(values[estimation.getValue()]))
- .sum()
- );
- }
-}
\ No newline at end of file
diff --git a/src/main/java/org/mender/dsv/DsvMender.java b/src/main/java/org/mender/dsv/DsvMender.java
deleted file mode 100644
index 6644bee..0000000
--- a/src/main/java/org/mender/dsv/DsvMender.java
+++ /dev/null
@@ -1,544 +0,0 @@
-/*
-MIT License
-
-Copyright (c) 2018 Alexis Jehan
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all
-copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-SOFTWARE.
-*/
-package org.mender.dsv;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import org.apache.commons.lang3.StringUtils;
-import org.mender.Mender;
-import org.mender.MenderException;
-
-/**
- * Main component to use to repair malformed DSV data.
- *
- * @since 1.0
- */
-public class DsvMender implements Mender {
-
- /**
- * {@code Nodes} is an object that wraps and optimizes a collection of {@code String} arrays for
- * {@code DsvMender} uses.
- *
- * Optimizations:
- * - Unlike a {@link java.util.HashMap}, the hash of the String array is computed using a custom hash function
- * that serves as the key of the map.
- * - Because of large uses of equivalents {@code String} objects, {@code String.intern()} is called for all of
- * them.
- *
- * @since 1.0
- */
- private static class Nodes {
-
- /**
- * Delegate map with hash codes as keys and {@code String} arrays as values.
- */
- private final Map delegate = new HashMap<>();
-
- /**
- * Add all entries to the {@code Nodes}.
- *
- * @param entries Collection of entries to add
- */
- void addAll(final Collection entries) {
- for (final String[] entry : entries) {
- final int hashCode = customHashCode(entry);
- if (delegate.containsKey(hashCode)) {
- continue;
- }
- for (int i = 0; i < entry.length; ++i) {
- entry[i] = entry[i].intern();
- }
- delegate.put(hashCode, entry);
- }
- }
-
- /**
- * Return a collection of {@code Nodes} values.
- *
- * @return The collection of values
- */
- Collection values() {
- return delegate.values();
- }
-
- /**
- * Empty all nodes.
- */
- void clear() {
- delegate.clear();
- }
-
- /**
- * Custom hash code generation of {@code String} arrays, because of a high risk of collision with standard
- * {@code Arrays.hashCode} function.
- * Example of collision:
- * {@code Arrays.hashCode(new String[] {"o", "ooo"})} gives 114625
- * {@code Arrays.hashCode(new String[] {"oo", "oo"})} gives 114625 too
- *
- * @param array The array to get the hash code from
- * @return Computed hash code
- */
- private static int customHashCode(final String[] array) {
- int arrayHashCode = 1;
- for (int i = 0; i < array.length; ++i) {
- final String string = array[i];
- int stringHashCode = 0;
- if (null != string) {
- final int length = string.length();
- for (int j = 0; j < length; ++j) {
- stringHashCode = 257 * stringHashCode + string.charAt(j);
- }
- }
- arrayHashCode = 31 * arrayHashCode + stringHashCode;
- }
- return arrayHashCode;
- }
- }
-
- /**
- * Default maximum depth of the nodes tree.
- */
- static final int DEFAULT_MAX_DEPTH = 20;
-
- /**
- * {@code DsvEvaluator} object.
- */
- private final DsvEvaluator evaluator;
-
- /**
- * String delimiter of the DSV data.
- */
- private final String delimiter;
-
- /**
- * Number of columns of the DSV data.
- */
- private final int nbColumns;
-
- /**
- * Maximum depth of the nodes tree.
- */
- private final int maxDepth;
-
- /**
- * Map that associates nodes to their scores.
- */
- private Map nodeScores;
-
- /**
- * Best score, of the returned node.
- */
- private double score;
-
- /**
- * Package-private constructor used by the {@link DsvBuilder} class.
- *
- * @param evaluator {@code DsvEvaluator} to use
- * @param delimiter {@code String} delimiter of the DSV data
- * @param nbColumns Number of columns of the DSV data
- * @param maxDepth Maximum depth of the nodes tree
- * @throws NullPointerException If the evaluator or the delimiter are {@code null}
- * @throws IllegalArgumentException If the number of columns if lower than 2
- */
- DsvMender(final DsvEvaluator evaluator, final String delimiter, final int nbColumns, final int maxDepth) {
- if (null == evaluator) {
- throw new NullPointerException("Invalid evaluator (not null expected)");
- }
- if (null == delimiter) {
- throw new NullPointerException("Invalid delimiter (not null expected)");
- }
- if (2 > nbColumns) {
- throw new IllegalArgumentException("Invalid number of columns: " + nbColumns + " (greater than or equal to 2 expected)");
- }
- this.evaluator = evaluator;
- this.delimiter = delimiter;
- this.nbColumns = nbColumns;
- this.maxDepth = maxDepth;
- }
-
- /**
- * Fit the evaluator with a row from the DSV data, only if it is well-formed.
- *
- * @param row Row of separated values
- * @throws NullPointerException If the row is {@code null}
- */
- public void fitIfValid(final String row) {
- if (null == row) {
- throw new NullPointerException("Invalid row (not null expected)");
- }
- fitIfValid(StringUtils.splitByWholeSeparatorPreserveAllTokens(row, delimiter));
- }
-
- /**
- * Fit the evaluator with a row from the DSV data, only if it is well-formed.
- *
- * @param values Array of separated values
- * @throws NullPointerException If values are {@code null}
- */
- public void fitIfValid(final String[] values) {
- if (null == values) {
- throw new NullPointerException("Invalid values (not null expected)");
- }
- if (nbColumns == values.length && evaluator.checkConstraints(values)) {
- fit(values);
- }
- }
-
- /**
- * Fit the evaluator with a well-formed row from the DSV data.
- *
- * @param row Row of well-formed separated values
- * @throws NullPointerException If the row is {@code null}
- */
- public void fit(final String row) {
- if (null == row) {
- throw new NullPointerException("Invalid row (not null expected)");
- }
- fit(StringUtils.splitByWholeSeparatorPreserveAllTokens(row, delimiter));
- }
-
- /**
- * Fit the evaluator with a well-formed row from the DSV data.
- *
- * @param values Array of well-formed separated values
- * @throws NullPointerException If values are {@code null}
- * @throws IllegalArgumentException If the number of values is different from the number of columns or if values
- * don't pass constraints
- */
- @Override
- public void fit(final String[] values) {
- if (null == values) {
- throw new NullPointerException("Invalid values (not null expected)");
- }
- if (nbColumns != values.length) {
- throw new IllegalArgumentException("Invalid number of values: " + values.length + " (" + nbColumns + " expected)");
- }
- if (!evaluator.checkConstraints(values)) {
- throw new IllegalArgumentException("Invalid values: " + Arrays.toString(values) + " (constraints not passed)");
- }
- evaluator.adjustEstimations(values);
- }
-
- /**
- * In some cases you should consider using this method instead of {@code fix()} for better performances and less
- * memory consumption when lot of delimiters are appearing consecutively.
- *
- * @param row Row of malformed separated values
- * @param nbSafeColumns The maximum number of possibly consecutive empty values in a well-formed row
- * @return Fixed extracted values from the row
- * @throws NullPointerException If the row is {@code null}
- * @throws IllegalArgumentException If the number of safe columns is negative
- * @throws MenderException In particular cases the {@code fix()} method might not work because of used criteria
- * @see DsvUtils
- */
- public String[] optimizedFix(final String row, final int nbSafeColumns) throws MenderException {
- if (null == row) {
- throw new NullPointerException("Invalid row (not null expected)");
- }
- return optimizedFix(StringUtils.splitByWholeSeparatorPreserveAllTokens(row, delimiter), nbSafeColumns);
- }
-
- /**
- * In some cases you should consider using this method instead of {@code fix()} for better performances and less
- * memory consumption when lot of delimiters are appearing consecutively.
- *
- * @param values Array of malformed separated values
- * @param nbSafeColumns The maximum number of possibly consecutive empty values in a well-formed row
- * @return Fixed values
- * @throws NullPointerException If values are {@code null}
- * @throws IllegalArgumentException If the number of safe columns is negative
- * @throws MenderException In particular cases the {@code fix()} method might not work because of used criteria
- * @see DsvUtils
- */
- public String[] optimizedFix(final String[] values, final int nbSafeColumns) throws MenderException {
- if (null == values) {
- throw new NullPointerException("Invalid values (not null expected)");
- }
- if (0 > nbSafeColumns) {
- throw new IllegalArgumentException("Invalid number of safe columns (greater than or equal to 0 expected)");
- }
- return fix(DsvUtils.optimize(values, delimiter, nbColumns, nbSafeColumns));
- }
-
- /**
- * Fix a row from the DSV data using previously learned ones, only if it is malformed.
- *
- * @param row Row of separated values
- * @return Fixed extracted values from the row if it was malformed
- * @throws MenderException In particular cases that method might not work because of used criteria
- * @throws NullPointerException If the row is {@code null}
- */
- public String[] fixIfNotValid(final String row) throws MenderException {
- if (null == row) {
- throw new NullPointerException("Invalid row (not null expected)");
- }
- return fixIfNotValid(StringUtils.splitByWholeSeparatorPreserveAllTokens(row, delimiter));
- }
-
- /**
- * Fix a row from the DSV data using previously learned ones, only if it is malformed.
- *
- * @param values Array of separated values
- * @return Fixed values if they were malformed
- * @throws MenderException In particular cases that method might not work because of used criteria
- * @throws NullPointerException If values are {@code null}
- */
- public String[] fixIfNotValid(final String[] values) throws MenderException {
- if (null == values) {
- throw new NullPointerException("Invalid values (not null expected)");
- }
- if (nbColumns != values.length) {
- return fix(values);
- } else {
- return values;
- }
- }
-
- /**
- * Fix a malformed row from the DSV data using previously learned ones.
- *
- * @param row Row of malformed separated values
- * @return Fixed extracted values from the row
- * @throws MenderException In particular cases that method might not work because of used criteria
- * @throws NullPointerException If the row is {@code null}
- */
- public String[] fix(final String row) throws MenderException {
- if (null == row) {
- throw new NullPointerException("Invalid row (not null expected)");
- }
- return fix(StringUtils.splitByWholeSeparatorPreserveAllTokens(row, delimiter));
- }
-
- /**
- * Fix a malformed row from the DSV data using previously learned ones.
- *
- * @param values Array of malformed separated values
- * @return Fixed values
- * @throws MenderException In particular cases that method might not work because of used criteria
- * @throws NullPointerException If values are {@code null}
- * @throws IllegalArgumentException If the number of values is equal to the number of columns
- */
- @Override
- public String[] fix(final String[] values) throws MenderException {
- if (null == values) {
- throw new NullPointerException("Invalid values (not null expected)");
- }
- if (nbColumns == values.length) {
- throw new IllegalArgumentException("Invalid number of values: " + values.length + " (different from " + nbColumns + " expected)");
- }
- if (maxDepth < Math.abs(nbColumns - values.length - 2)) {
- throw new MenderException("Could not fix because the depth should be less than or equal to " + maxDepth + " (" + Math.abs(nbColumns - values.length - 2) + " for given values)");
- }
- final Nodes nodes = new Nodes();
- if (nbColumns < values.length) {
- nodes.addAll(generateJoinChildNodes(values));
- for (int i = nbColumns; i < values.length - 1; ++i) {
- final List subNodes = new ArrayList<>(nodes.values());
- nodes.clear();
- for (final String[] subNode : subNodes) {
- nodes.addAll(generateJoinChildNodes(subNode));
- }
- }
- } else {
- nodes.addAll(generateShiftChildNodes(values));
- for (int i = nbColumns; i > values.length + 1; --i) {
- final List subNodes = new ArrayList<>(nodes.values());
- nodes.clear();
- for (final String[] subNode : subNodes) {
- nodes.addAll(generateShiftChildNodes(subNode));
- }
- }
- }
- nodeScores = new HashMap<>();
- for (final String[] node : nodes.values()) {
- final double nodeScore = evaluator.evaluate(node);
- if (0 < nodeScore) {
- nodeScores.put(node, nodeScore);
- }
- }
- if (nodeScores.isEmpty()) {
- throw new MenderException("No solution has been found for values: \"" + Arrays.toString(values) + "\" (consider using others estimations or constraints)");
- }
- final Map.Entry bestNode = Collections.max(nodeScores.entrySet(), Map.Entry.comparingByValue());
- score = bestNode.getValue();
- return bestNode.getKey();
- }
-
- /**
- * Create child nodes from a parent by computing all possible merging cases of values. Each value is attempted to
- * be merged with the next one using the delimiter as separator.
- *
- * @param parent The parent node
- * @return List of generated child nodes
- */
- private List generateJoinChildNodes(final String[] parent) {
- final List children = new ArrayList<>(parent.length - 1);
- for (int i = 0; i < parent.length - 1; ++i) {
- final String[] child = new String[parent.length - 1];
- for (int j = 0; j < parent.length - 1; ++j) {
- if (j == i) {
- child[j] = parent[j] + delimiter + parent[j + 1];
- } else {
- child[j] = parent[j < i ? j : j + 1];
- }
- }
- children.add(child);
- }
- return children;
- }
-
- /**
- * Create child nodes from a parent by computing all possible shifting cases of values. An empty value is added
- * at any position between all values.
- *
- * @param parent The parent node
- * @return List of generated child nodes
- */
- private List generateShiftChildNodes(final String[] parent) {
- final List children = new ArrayList<>(parent.length + 1);
- for (int i = 0; i < parent.length + 1; ++i) {
- final String[] child = new String[parent.length + 1];
- for (int j = 0; j < parent.length + 1; ++j) {
- if (j == i) {
- child[j] = "";
- } else {
- child[j] = parent[j < i ? j : j - 1];
- }
- }
- children.add(child);
- }
- return children;
- }
-
- /**
- * Return a map that associates scores to each node generated by the last call to {@code fix()}.
- *
- * @return An unmodifiable map of nodes' scores
- */
- public Map getNodeScores() {
- return Collections.unmodifiableMap(nodeScores);
- }
-
- /**
- * Return the score of the more relevant node used by the last call to {@code fix()}.
- *
- * @return The more relevant score
- */
- public double getScore() {
- return score;
- }
-
- /**
- * Create a new {@link DsvBuilder} instance to build a {@code DsvMender} object, using a {@code char} delimiter
- * and the default maximum depth.
- *
- * @param delimiter {@code char} delimiter of the DSV data
- * @param nbColumns Number of columns of the DSV data
- * @return The instantiated {@link DsvBuilder} instance
- */
- public static DsvBuilder builder(final char delimiter, final int nbColumns) {
- return builder(Character.toString(delimiter), nbColumns);
- }
-
- /**
- * Create a new {@link DsvBuilder} instance to build a {@code DsvMender} object, using a {@code String} delimiter
- * and the default maximum depth.
- *
- * @param delimiter {@code String} delimiter of the DSV data
- * @param nbColumns Number of columns of the DSV data
- * @return The instantiated {@link DsvBuilder} instance
- */
- public static DsvBuilder builder(final String delimiter, final int nbColumns) {
- return builder(delimiter, nbColumns, DEFAULT_MAX_DEPTH);
- }
-
- /**
- * Create a new {@link DsvBuilder} instance to build a {@code DsvMender} object, using a {@code char} delimiter
- * and a non-default maximum depth.
- *
- * @param delimiter {@code char} delimiter of the DSV data
- * @param nbColumns Number of columns of the DSV data
- * @param maxDepth Maximum depth of the nodes tree
- * @return The instantiated {@link DsvBuilder} instance
- * @throws NullPointerException If the delimiter is {@code null}
- * @throws IllegalArgumentException If the number of columns is lower than 2
- */
- public static DsvBuilder builder(final char delimiter, final int nbColumns, final int maxDepth) {
- return builder(Character.toString(delimiter), nbColumns, maxDepth);
- }
-
- /**
- * Create a new {@link DsvBuilder} instance to build a {@code DsvMender} object, using a {@code String} delimiter
- * and a non-default maximum depth.
- *
- * @param delimiter {@code String} delimiter of the DSV data
- * @param nbColumns Number of columns of the DSV data
- * @param maxDepth Maximum depth of the nodes tree
- * @return The instantiated {@link DsvBuilder} instance
- * @throws NullPointerException If the delimiter is {@code null}
- * @throws IllegalArgumentException If the number of columns is lower than 2
- */
- public static DsvBuilder builder(final String delimiter, final int nbColumns, final int maxDepth) {
- if (null == delimiter) {
- throw new NullPointerException("Invalid delimiter (not null expected)");
- }
- if (0 > nbColumns) {
- throw new IllegalArgumentException("Invalid number of columns: " + nbColumns + " (greater than or equal to 2 expected)");
- }
- return new DsvBuilder(delimiter, nbColumns, maxDepth);
- }
-
- /**
- * Create a new {@link DsvBuilder} instance automatically, using a {@code char} delimiter and configured with
- * empty-string and length-string estimations.
- *
- * @param delimiter {@code char} delimiter of the DSV data
- * @param nbColumns Number of columns of the DSV data
- * @return The instantiated {@link DsvBuilder} instance
- */
- public static DsvMender auto(final char delimiter, final int nbColumns) {
- return auto(Character.toString(delimiter), nbColumns);
- }
-
- /**
- * Create a new {@link DsvBuilder} instance automatically, using a {@code String} delimiter and configured with
- * empty-string and length-string estimations.
- *
- * @param delimiter {@code String} delimiter of the DSV data
- * @param nbColumns Number of columns of the DSV data
- * @return The instantiated {@link DsvBuilder} instance
- */
- public static DsvMender auto(final String delimiter, final int nbColumns) {
- return builder(delimiter, nbColumns, DEFAULT_MAX_DEPTH)
- .withEmptyEstimations()
- .withLengthEstimations()
- .build();
- }
-}
\ No newline at end of file
diff --git a/src/main/java/org/mender/dsv/DsvReader.java b/src/main/java/org/mender/dsv/DsvReader.java
deleted file mode 100644
index aff8df5..0000000
--- a/src/main/java/org/mender/dsv/DsvReader.java
+++ /dev/null
@@ -1,163 +0,0 @@
-/*
-MIT License
-
-Copyright (c) 2018 Alexis Jehan
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all
-copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-SOFTWARE.
-*/
-package org.mender.dsv;
-
-import java.io.BufferedReader;
-import java.io.Closeable;
-import java.io.IOException;
-import java.io.Reader;
-import java.io.StringReader;
-import java.nio.charset.Charset;
-import java.nio.file.Files;
-import java.nio.file.Path;
-
-import org.mender.MenderException;
-
-/**
- * A reader for DSV data that fit itself progressively while forwarding in the stream using the given
- * {@code DsvMender}. Invalids rows are automatically fixed using at-the-time knowledges.
- *
- * Node: For better results you should instead use a two-pass process that first fit valid rows and then fix
- * invalids ones.
- *
- * @since 1.0
- */
-public class DsvReader implements Closeable {
-
- /**
- * Configured {@code DsvMender} to use.
- */
- private final DsvMender mender;
-
- /**
- * Delegated {@code BufferedReader} to read lines from the input.
- */
- private final BufferedReader bufferedReader;
-
- /**
- * Boolean that indicate if a line has already been read.
- */
- private boolean hasStarted = false;
-
- /**
- * Constructor using a file {@code Path}.
- *
- * @param mender The {@code DSVMender} to use
- * @param file The input file {@code Path}
- * @throws IOException Might occurs with I/O operations
- */
- public DsvReader(final DsvMender mender, final Path file) throws IOException {
- this(mender, Files.newBufferedReader(file));
- }
-
- /**
- * Constructor using a file {@code Path} and a custom {@code Charset}.
- *
- * @param mender The {@code DSVMender} to use
- * @param file The input file {@code Path}
- * @param charset Custom {@code Charset}
- * @throws IOException Might occurs with I/O operations
- */
- public DsvReader(final DsvMender mender, final Path file, final Charset charset) throws IOException {
- this(mender, Files.newBufferedReader(file, charset));
- }
-
- /**
- * Constructor using a {@code String}
- *
- * @param mender The {@code DSVMender} to use
- * @param string The {@code String} to use
- */
- public DsvReader(final DsvMender mender, final String string) {
- this(mender, new StringReader(string));
- }
-
- /**
- * Constructor using a {@code Reader}
- *
- * @param mender The {@code DSVMender} to use
- * @param reader The {@code Reader} to use
- */
- public DsvReader(final DsvMender mender, final Reader reader) {
- this(mender, new BufferedReader(reader));
- }
-
- /**
- * Constructor using a {@code BufferedReader}
- *
- * @param mender The {@code DSVMender} to use
- * @param bufferedReader The {@code BufferedReader} to use
- * @throws NullPointerException If the {@code DsvMender} or the {@code BufferedReader} are {@code null}
- */
- public DsvReader(final DsvMender mender, final BufferedReader bufferedReader) {
- if (null == mender) {
- throw new NullPointerException("Invalid DSV mender (not null expected)");
- }
- if (null == bufferedReader) {
- throw new NullPointerException("Invalid buffered reader (not null expected)");
- }
- this.mender = mender;
- this.bufferedReader = bufferedReader;
- }
-
- /**
- * Read the DSV header, it must be the first read line. Note that no fit operation will be performed.
- *
- * @return Array of DSV header values
- * @throws MenderException If the header has invalid number of values and if the fix has failed
- * @throws IOException Might occurs with I/O operations
- */
- public String[] readHeader() throws MenderException, IOException {
- if (hasStarted) {
- throw new IllegalStateException("Header must be the first read line");
- }
- final String line = bufferedReader.readLine();
- if (null == line) {
- return null;
- }
- return mender.fixIfNotValid(line);
- }
-
- /**
- * Read a DSV row, performing a fit operation if it is valid, or a fix operation else.
- *
- * @return Array of DSV row values
- * @throws MenderException If the row has invalid number of values and if the fix has failed
- * @throws IOException Might occurs with I/O operations
- */
- public String[] readRow() throws MenderException, IOException {
- hasStarted = true;
- final String line = bufferedReader.readLine();
- if (null == line) {
- return null;
- }
- mender.fitIfValid(line);
- return mender.fixIfNotValid(line);
- }
-
- @Override
- public void close() throws IOException {
- bufferedReader.close();
- }
-}
\ No newline at end of file
diff --git a/src/main/java/org/mender/dsv/DsvUtils.java b/src/main/java/org/mender/dsv/DsvUtils.java
deleted file mode 100644
index 6eca79b..0000000
--- a/src/main/java/org/mender/dsv/DsvUtils.java
+++ /dev/null
@@ -1,106 +0,0 @@
-/*
-MIT License
-
-Copyright (c) 2018 Alexis Jehan
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all
-copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-SOFTWARE.
-*/
-package org.mender.dsv;
-
-import java.util.Arrays;
-
-/**
- * Utility class that for now only provides an optimization strategy.
- *
- * @since 1.0
- */
-final class DsvUtils {
-
- /**
- * Constructor not available
- */
- private DsvUtils() {
- throw new AssertionError();
- }
-
- /**
- * This function aims to considerably improve performances of the {@code DsvMender.fix()} method for specials
- * cases, especially when the delimiter occurs several times consecutively and lead to nodes explosion.
- *
- * @param values Array of values to optimize
- * @param delimiter DSV delimiter string
- * @param nbColumns DSV number of columns
- * @param nbSafeColumns An indication of possibly consecutive empty values
- * @return Array of optimized values
- */
- static String[] optimize(final String[] values, final String delimiter, final int nbColumns, final int nbSafeColumns) {
- String[] result = Arrays.copyOf(values, values.length);
- while (nbColumns < result.length) {
- // Interval of longest consecutive empty values
- int dmin = 0;
- int dmax = 0;
- int s = -1;
- for (int i = 0; i < result.length; ++i) {
- if (-1 == s && result[i].isEmpty()) {
- s = i;
- continue;
- }
- if (-1 != s && !result[i].isEmpty()) {
- if (dmax - dmin < i - s) {
- dmin = s;
- dmax = i;
- }
- s = -1;
- }
- }
- if (-1 != s && dmax - dmin < result.length - s) {
- dmin = s;
- dmax = result.length;
- }
- if (dmax - dmin > 2 * nbSafeColumns + 1) {
- // Merging consecutive empty values while keeping a safe amount on each side
- for (int i = dmin + nbSafeColumns + 1; i < dmax - nbSafeColumns; ++i) {
- result = remove(result, dmin + nbSafeColumns + 1);
- result[dmin + nbSafeColumns] = result[dmin + nbSafeColumns] + delimiter;
- }
- } else {
- break;
- }
- }
- return result;
- }
-
- /**
- * Remove an element from an array at a specific index.
- *
- * @param array The array to remove the element from
- * @param index Index of the element to remove in the array
- * @return A new array without the removed element
- */
- private static String[] remove(final String[] array, final int index) {
- final String[] result = new String[array.length - 1];
- if (0 < index) {
- System.arraycopy(array, 0, result, 0, index);
- }
- if (index < array.length - 1) {
- System.arraycopy(array, index + 1, result, index, array.length - index - 1);
- }
- return result;
- }
-}
\ No newline at end of file
diff --git a/src/main/java/org/mender/dsv/package-info.java b/src/main/java/org/mender/dsv/package-info.java
deleted file mode 100644
index 0f23d30..0000000
--- a/src/main/java/org/mender/dsv/package-info.java
+++ /dev/null
@@ -1,38 +0,0 @@
-/*
-MIT License
-
-Copyright (c) 2018 Alexis Jehan
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all
-copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-SOFTWARE.
-*/
-/**
- * {@code Mender} implementation for malformed DSV data.
- *
- * DSV (Delimiter-Separated Values) is a data format that consists of rows of values separated by a delimiting
- * character or a string. CSV (Comma-Separated Values) is a famous and commonly-used format inherited from
- * DSV.
- * While most of that formatted data are escaping the delimiter, that's not always the case. Basically sometimes the
- * non-escaped delimiter is contained by some values and while parsing the computer is not able to retrieve the original
- * information, or malformed lines are simply ignored. Another case is when values are missing from some lines.
- * {@link org.mender.dsv.DsvMender} is a tool that is able to repair malformed DSV data by learning columns features
- * from corrects rows.
- *
- * @since 1.0
- */
-package org.mender.dsv;
\ No newline at end of file
diff --git a/src/main/java/org/mender/package-info.java b/src/main/java/org/mender/package-info.java
deleted file mode 100644
index df19d7f..0000000
--- a/src/main/java/org/mender/package-info.java
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
-MIT License
-
-Copyright (c) 2018 Alexis Jehan
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all
-copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-SOFTWARE.
-*/
-/**
- * Components of the experimental {@link org.mender.Mender} pattern that have to be implemented by concrete
- * classes.
- *
- * @since 1.0
- */
-package org.mender;
\ No newline at end of file
diff --git a/src/main/resources/missing_cells.tsv b/src/main/resources/missing_cells.tsv
deleted file mode 100644
index f05a291..0000000
--- a/src/main/resources/missing_cells.tsv
+++ /dev/null
@@ -1,5 +0,0 @@
-YEAR MAKE MODEL DESCRIPTION PRICE
-1997 Ford E350 ac, abs, moon 3000.00
-1999 Venture "Extended Edition" 4900.00
-1999 Chevy Venture "Extended Edition", Very Large 5000.00
-1996 Jeep Grand Cherokee MUST SELL! air, moon roof, loaded 4799.00
\ No newline at end of file
diff --git a/src/main/resources/not_quoted.csv b/src/main/resources/not_quoted.csv
deleted file mode 100644
index 5ab3bdb..0000000
--- a/src/main/resources/not_quoted.csv
+++ /dev/null
@@ -1,6 +0,0 @@
-ID,NAME,DESCRIPTION,BIRTHDAY,COUNTRY
-1,John,Hey everyone I'm the first user,1984-05-16,United Kingdom
-2,Pierre,Bonjour à tous vous allez bien ?,1992-11-26,France
-3,Pedro,Holà qué tal ?,1962-01-05,Spain
-4,Arnold,My country name contains a , in it,1974-05-30,Macedonia, Rep. of
-5,Peter,I, like, to, use, commas, between, words,1994-12-04,United States
\ No newline at end of file
diff --git a/src/test/java/com/github/alexisjehan/mender/api/MendExceptionTest.java b/src/test/java/com/github/alexisjehan/mender/api/MendExceptionTest.java
new file mode 100644
index 0000000..f7023ed
--- /dev/null
+++ b/src/test/java/com/github/alexisjehan/mender/api/MendExceptionTest.java
@@ -0,0 +1,48 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2017-2019 Alexis Jehan
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+package com.github.alexisjehan.mender.api;
+
+import com.github.alexisjehan.javanilla.io.Serializables;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatNullPointerException;
+
+/**
+ * {@link MendException} unit tests.
+ */
+final class MendExceptionTest {
+
+ @Test
+ void testConstructorInvalid() {
+ assertThatNullPointerException().isThrownBy(() -> new MendException(null));
+ }
+
+ @Test
+ void testSerializable() {
+ final var mendException = new MendException("foo");
+ final var deserializedMendException = Serializables.deserialize(Serializables.serialize(mendException));
+ assertThat(deserializedMendException.getMessage()).isEqualTo(mendException.getMessage());
+ }
+}
\ No newline at end of file
diff --git a/src/test/java/com/github/alexisjehan/mender/api/evaluators/ConstraintEvaluatorTest.java b/src/test/java/com/github/alexisjehan/mender/api/evaluators/ConstraintEvaluatorTest.java
new file mode 100644
index 0000000..87844de
--- /dev/null
+++ b/src/test/java/com/github/alexisjehan/mender/api/evaluators/ConstraintEvaluatorTest.java
@@ -0,0 +1,54 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2017-2019 Alexis Jehan
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+package com.github.alexisjehan.mender.api.evaluators;
+
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatNullPointerException;
+
+/**
+ * {@link ConstraintEvaluator} unit tests.
+ */
+final class ConstraintEvaluatorTest {
+
+ @Test
+ void testConstructorInvalid() {
+ assertThatNullPointerException().isThrownBy(() -> new ConstraintEvaluator<>(null));
+ }
+
+ @Test
+ void testIsValid() {
+ final var evaluator = new ConstraintEvaluator<>("foo"::equals);
+ assertThat(evaluator.isValid("foo")).isTrue();
+ assertThat(evaluator.isValid("bar")).isFalse();
+ }
+
+ @Test
+ void testEvaluate() {
+ final var evaluator = new ConstraintEvaluator<>("foo"::equals);
+ assertThat(evaluator.evaluate("foo")).isEqualTo(1.0d);
+ assertThat(evaluator.evaluate("bar")).isNaN();
+ }
+}
\ No newline at end of file
diff --git a/src/test/java/com/github/alexisjehan/mender/api/evaluators/EstimationEvaluatorTest.java b/src/test/java/com/github/alexisjehan/mender/api/evaluators/EstimationEvaluatorTest.java
new file mode 100644
index 0000000..bd80753
--- /dev/null
+++ b/src/test/java/com/github/alexisjehan/mender/api/evaluators/EstimationEvaluatorTest.java
@@ -0,0 +1,68 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2017-2019 Alexis Jehan
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+package com.github.alexisjehan.mender.api.evaluators;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.function.Function;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatNullPointerException;
+
+/**
+ * {@link EstimationEvaluator} unit tests.
+ */
+final class EstimationEvaluatorTest {
+
+ @Test
+ void testConstructorInvalid() {
+ assertThatNullPointerException().isThrownBy(() -> new EstimationEvaluator<>(null));
+ }
+
+ @Test
+ void testFitEvaluate() {
+ {
+ final var evaluator = new EstimationEvaluator<>(Function.identity());
+ assertThat(evaluator.evaluate("foo")).isNaN();
+ assertThat(evaluator.evaluate("bar")).isNaN();
+ evaluator.fit("foo");
+ assertThat(evaluator.evaluate("foo")).isEqualTo(1.0d);
+ assertThat(evaluator.evaluate("bar")).isEqualTo(0.0d);
+ evaluator.fit("bar");
+ assertThat(evaluator.evaluate("foo")).isEqualTo(0.5d);
+ assertThat(evaluator.evaluate("bar")).isEqualTo(0.5d);
+ }
+ {
+ final var evaluator = new EstimationEvaluator<>(String::length);
+ assertThat(evaluator.evaluate("foo")).isNaN();
+ assertThat(evaluator.evaluate("fooo")).isNaN();
+ evaluator.fit("foo");
+ assertThat(evaluator.evaluate("foo")).isEqualTo(1.0d);
+ assertThat(evaluator.evaluate("fooo")).isEqualTo(0.0d);
+ evaluator.fit("fooo");
+ assertThat(evaluator.evaluate("foo")).isEqualTo(0.5d);
+ assertThat(evaluator.evaluate("fooo")).isEqualTo(0.5d);
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/test/java/com/github/alexisjehan/mender/dsv/DsvMendCandidateTest.java b/src/test/java/com/github/alexisjehan/mender/dsv/DsvMendCandidateTest.java
new file mode 100644
index 0000000..800dbe6
--- /dev/null
+++ b/src/test/java/com/github/alexisjehan/mender/dsv/DsvMendCandidateTest.java
@@ -0,0 +1,93 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2017-2019 Alexis Jehan
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+package com.github.alexisjehan.mender.dsv;
+
+import com.github.alexisjehan.javanilla.lang.array.ObjectArrays;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
+import static org.assertj.core.api.Assertions.assertThatNullPointerException;
+
+/**
+ * {@link DsvMendCandidate} unit tests.
+ */
+final class DsvMendCandidateTest {
+
+ @Test
+ void testConstructorImmutable() {
+ final var values = ObjectArrays.singleton("foo");
+ final var mendCandidate = new DsvMendCandidate(values, 1.0d);
+ assertThat(mendCandidate.getValue()).containsExactly("foo");
+ values[0] = "bar";
+ assertThat(mendCandidate.getValue()).containsExactly("foo");
+ }
+
+ @Test
+ void testConstructorInvalid() {
+ assertThatNullPointerException().isThrownBy(() -> new DsvMendCandidate(null, 1.0d));
+ assertThatIllegalArgumentException().isThrownBy(() -> new DsvMendCandidate(ObjectArrays.empty(String.class), 1.0d));
+ assertThatIllegalArgumentException().isThrownBy(() -> new DsvMendCandidate(ObjectArrays.singleton("foo"), -1.0d));
+ }
+
+ @Test
+ void testEqualsHashCodeToString() {
+ final var mendCandidate = new DsvMendCandidate(ObjectArrays.singleton("foo"), 1.0d);
+ assertThat(mendCandidate).isEqualTo(mendCandidate);
+ assertThat(mendCandidate).isNotEqualTo(1);
+ {
+ final var otherMendCandidate = new DsvMendCandidate(mendCandidate.getValue(), mendCandidate.getScore());
+ assertThat(mendCandidate).isEqualTo(otherMendCandidate);
+ assertThat(mendCandidate).hasSameHashCodeAs(otherMendCandidate);
+ assertThat(mendCandidate).hasToString(otherMendCandidate.toString());
+ }
+ {
+ final var otherMendCandidate = new DsvMendCandidate(ObjectArrays.singleton("bar"), mendCandidate.getScore());
+ assertThat(mendCandidate).isNotEqualTo(otherMendCandidate);
+ assertThat(mendCandidate.hashCode()).isNotEqualTo(otherMendCandidate.hashCode());
+ assertThat(mendCandidate.toString()).isNotEqualTo(otherMendCandidate.toString());
+ }
+ {
+ final var otherMendCandidate = new DsvMendCandidate(mendCandidate.getValue(), 0.5d);
+ assertThat(mendCandidate).isNotEqualTo(otherMendCandidate);
+ assertThat(mendCandidate.hashCode()).isNotEqualTo(otherMendCandidate.hashCode());
+ assertThat(mendCandidate.toString()).isNotEqualTo(otherMendCandidate.toString());
+ }
+ }
+
+ @Test
+ void testGetters() {
+ final var mendCandidate = new DsvMendCandidate(ObjectArrays.singleton("foo"), 1.0d);
+ assertThat(mendCandidate.getValue()).containsExactly("foo");
+ assertThat(mendCandidate.getScore()).isEqualTo(1.0d);
+ }
+
+ @Test
+ void testGettersImmutable() {
+ final var mendCandidate = new DsvMendCandidate(ObjectArrays.singleton("foo"), 1.0d);
+ assertThat(mendCandidate.getValue()).containsExactly("foo");
+ mendCandidate.getValue()[0] = "bar";
+ assertThat(mendCandidate.getValue()).containsExactly("foo");
+ }
+}
\ No newline at end of file
diff --git a/src/test/java/com/github/alexisjehan/mender/dsv/DsvMendResultTest.java b/src/test/java/com/github/alexisjehan/mender/dsv/DsvMendResultTest.java
new file mode 100644
index 0000000..0b9579e
--- /dev/null
+++ b/src/test/java/com/github/alexisjehan/mender/dsv/DsvMendResultTest.java
@@ -0,0 +1,106 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2017-2019 Alexis Jehan
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+package com.github.alexisjehan.mender.dsv;
+
+import com.github.alexisjehan.javanilla.lang.array.ObjectArrays;
+import org.junit.jupiter.api.Test;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
+import static org.assertj.core.api.Assertions.assertThatNullPointerException;
+
+/**
+ * {@link DsvMendResult} unit tests.
+ */
+final class DsvMendResultTest {
+
+ @Test
+ void testConstructorImmutable() {
+ final var values = ObjectArrays.singleton("foo");
+ final var mendResult = new DsvMendResult(values, Set.of(new DsvMendCandidate(ObjectArrays.singleton("bar"), 0.5d)), new DsvMendCandidate(ObjectArrays.singleton("bar"), 0.5d));
+ assertThat(mendResult.getValue()).containsExactly("foo");
+ values[0] = "bar";
+ assertThat(mendResult.getValue()).containsExactly("foo");
+ }
+
+ @Test
+ void testConstructorInvalid() {
+ assertThatNullPointerException().isThrownBy(() -> new DsvMendResult(null, Set.of(new DsvMendCandidate(ObjectArrays.singleton("bar"), 0.5d)), new DsvMendCandidate(ObjectArrays.singleton("bar"), 0.5d)));
+ assertThatIllegalArgumentException().isThrownBy(() -> new DsvMendResult(ObjectArrays.empty(String.class), Set.of(new DsvMendCandidate(ObjectArrays.singleton("bar"), 0.5d)), new DsvMendCandidate(ObjectArrays.singleton("bar"), 0.5d)));
+ assertThatNullPointerException().isThrownBy(() -> new DsvMendResult(ObjectArrays.singleton("foo"), null, new DsvMendCandidate(ObjectArrays.singleton("bar"), 0.5d)));
+ assertThatNullPointerException().isThrownBy(() -> new DsvMendResult(ObjectArrays.singleton("foo"), new HashSet<>(null), new DsvMendCandidate(ObjectArrays.singleton("bar"), 0.5d)));
+ assertThatIllegalArgumentException().isThrownBy(() -> new DsvMendResult(ObjectArrays.singleton("foo"), Set.of(), new DsvMendCandidate(ObjectArrays.singleton("bar"), 0.5d)));
+ assertThatNullPointerException().isThrownBy(() -> new DsvMendResult(ObjectArrays.singleton("foo"), Set.of(new DsvMendCandidate(ObjectArrays.singleton("bar"), 0.5d)), null));
+ }
+
+ @Test
+ void testEqualsHashCodeToString() {
+ final var mendResult = new DsvMendResult(ObjectArrays.singleton("foo"), Set.of(new DsvMendCandidate(ObjectArrays.singleton("bar"), 0.5d)), new DsvMendCandidate(ObjectArrays.singleton("bar"), 0.5d));
+ assertThat(mendResult).isEqualTo(mendResult);
+ assertThat(mendResult).isNotEqualTo(1);
+ {
+ final var otherMendResult = new DsvMendResult(mendResult.getValue(), mendResult.getCandidates(), mendResult.getBestCandidate());
+ assertThat(mendResult).isEqualTo(otherMendResult);
+ assertThat(mendResult).hasSameHashCodeAs(otherMendResult);
+ assertThat(mendResult).hasToString(otherMendResult.toString());
+ }
+ {
+ final var otherMendResult = new DsvMendResult(ObjectArrays.singleton("bar"), mendResult.getCandidates(), mendResult.getBestCandidate());
+ assertThat(mendResult).isNotEqualTo(otherMendResult);
+ assertThat(mendResult.hashCode()).isNotEqualTo(otherMendResult.hashCode());
+ assertThat(mendResult.toString()).isNotEqualTo(otherMendResult.toString());
+ }
+ {
+ final var otherMendResult = new DsvMendResult(mendResult.getValue(), Set.of(new DsvMendCandidate(ObjectArrays.singleton("bar"), 0.5d), new DsvMendCandidate(ObjectArrays.singleton("bar"), 1.0d)), mendResult.getBestCandidate());
+ assertThat(mendResult).isNotEqualTo(otherMendResult);
+ assertThat(mendResult.hashCode()).isNotEqualTo(otherMendResult.hashCode());
+ assertThat(mendResult.toString()).isNotEqualTo(otherMendResult.toString());
+ }
+ {
+ final var otherMendResult = new DsvMendResult(mendResult.getValue(), mendResult.getCandidates(), new DsvMendCandidate(ObjectArrays.singleton("bar"), 1.0d));
+ assertThat(mendResult).isNotEqualTo(otherMendResult);
+ assertThat(mendResult.hashCode()).isNotEqualTo(otherMendResult.hashCode());
+ assertThat(mendResult.toString()).isNotEqualTo(otherMendResult.toString());
+ }
+ }
+
+ @Test
+ void testGetters() {
+ final var mendResult = new DsvMendResult(ObjectArrays.singleton("foo"), Set.of(new DsvMendCandidate(ObjectArrays.singleton("bar"), 0.5d)), new DsvMendCandidate(ObjectArrays.singleton("bar"), 0.5d));
+ assertThat(mendResult.getValue()).containsExactly("foo");
+ assertThat(mendResult.getCandidates()).containsExactly(new DsvMendCandidate(ObjectArrays.singleton("bar"), 0.5d));
+ assertThat(mendResult.getBestCandidate()).isEqualTo(new DsvMendCandidate(ObjectArrays.singleton("bar"), 0.5d));
+ }
+
+ @Test
+ void testGettersImmutable() {
+ final var mendResult = new DsvMendResult(ObjectArrays.singleton("foo"), Set.of(new DsvMendCandidate(ObjectArrays.singleton("bar"), 0.5d)), new DsvMendCandidate(ObjectArrays.singleton("bar"), 0.5d));
+ assertThat(mendResult.getValue()).containsExactly("foo");
+ mendResult.getValue()[0] = "bar";
+ assertThat(mendResult.getValue()).containsExactly("foo");
+ }
+}
\ No newline at end of file
diff --git a/src/test/java/com/github/alexisjehan/mender/dsv/DsvMenderBuilderTest.java b/src/test/java/com/github/alexisjehan/mender/dsv/DsvMenderBuilderTest.java
new file mode 100644
index 0000000..4844fc1
--- /dev/null
+++ b/src/test/java/com/github/alexisjehan/mender/dsv/DsvMenderBuilderTest.java
@@ -0,0 +1,216 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2017-2019 Alexis Jehan
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+package com.github.alexisjehan.mender.dsv;
+
+import com.github.alexisjehan.javanilla.lang.array.IntArrays;
+import com.github.alexisjehan.javanilla.lang.array.ObjectArrays;
+import org.junit.jupiter.api.Test;
+
+import java.util.function.Function;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
+import static org.assertj.core.api.Assertions.assertThatNullPointerException;
+
+/**
+ * {@link DsvMender.Builder} unit tests.
+ */
+final class DsvMenderBuilderTest {
+
+ @Test
+ void testDefaultMaxDepth() {
+ final var dsvMender = DsvMender.builder()
+ .withDelimiter(",")
+ .withLength(2)
+ .build();
+ assertThat(dsvMender.getMaxDepth()).isEqualTo(20);
+ }
+
+ @Test
+ void testWithDelimiter() {
+ {
+ final var delimiterStep = DsvMender.builder();
+ final var lengthStep = delimiterStep.withDelimiter(',');
+ assertThat(lengthStep).isSameAs(delimiterStep);
+ final var buildStep = lengthStep.withLength(2);
+ final var dsvMender = buildStep.build();
+ assertThat(dsvMender.getDelimiter()).isEqualTo(",");
+ }
+ final var delimiterStep = DsvMender.builder();
+ final var lengthStep = delimiterStep.withDelimiter(",");
+ assertThat(lengthStep).isSameAs(delimiterStep);
+ final var buildStep = lengthStep.withLength(2);
+ final var dsvMender = buildStep.build();
+ assertThat(dsvMender.getDelimiter()).isEqualTo(",");
+ }
+
+ @Test
+ void testWithLength() {
+ final var lengthStep = DsvMender.builder()
+ .withDelimiter(",");
+ final var buildStep = lengthStep.withLength(2);
+ assertThat(buildStep).isSameAs(lengthStep);
+ final var dsvMender = buildStep.build();
+ assertThat(dsvMender.getLength()).isEqualTo(2);
+ }
+
+ @Test
+ void testWithMaxDepth() {
+ final var optionalMaxDepthStep = DsvMender.builder()
+ .withDelimiter(",")
+ .withLength(2);
+ final var buildStep = optionalMaxDepthStep.withMaxDepth(1);
+ assertThat(buildStep).isSameAs(optionalMaxDepthStep);
+ final var dsvMender = buildStep.build();
+ assertThat(dsvMender.getMaxDepth()).isEqualTo(1);
+ }
+
+ @Test
+ void testWithConstraint() {
+ final var optionalEvaluatorStep = DsvMender.builder()
+ .withDelimiter(",")
+ .withLength(2);
+ final var buildStep = optionalEvaluatorStep.withConstraint("foo"::equals);
+ assertThat(buildStep).isSameAs(optionalEvaluatorStep);
+ final var dsvMender = buildStep.build();
+ final var constraintEvaluators = dsvMender.getConstraintEvaluators();
+ assertThat(constraintEvaluators).hasSize(dsvMender.getLength());
+ for (final var constraintEvaluator : constraintEvaluators) {
+ assertThat(constraintEvaluator.evaluate(ObjectArrays.of("foo", "foo"))).isEqualTo(1.0d);
+ assertThat(constraintEvaluator.evaluate(ObjectArrays.of("bar", "bar"))).isNaN();
+ }
+ }
+
+ @Test
+ void testWithConstraintIndexes() {
+ final var optionalEvaluatorStep = DsvMender.builder()
+ .withDelimiter(",")
+ .withLength(2);
+ final var buildStep = optionalEvaluatorStep.withConstraint("foo"::equals, 0);
+ assertThat(buildStep).isSameAs(optionalEvaluatorStep);
+ final var dsvMender = buildStep.build();
+ final var constraintEvaluators = dsvMender.getConstraintEvaluators();
+ assertThat(constraintEvaluators).hasSize(1);
+ for (final var constraintEvaluator : constraintEvaluators) {
+ assertThat(constraintEvaluator.evaluate(ObjectArrays.of("foo", "foo"))).isEqualTo(1.0d);
+ assertThat(constraintEvaluator.evaluate(ObjectArrays.of("foo", "bar"))).isEqualTo(1.0d);
+ assertThat(constraintEvaluator.evaluate(ObjectArrays.of("bar", "foo"))).isNaN();
+ assertThat(constraintEvaluator.evaluate(ObjectArrays.of("bar", "bar"))).isNaN();
+ }
+ }
+
+ @Test
+ void testWithConstraintInvalid() {
+ final var delimiterStep = DsvMender.builder();
+ final var lengthStep = delimiterStep.withDelimiter(",");
+ final var optionalEvaluatorStep = lengthStep.withLength(2);
+ assertThatNullPointerException().isThrownBy(() -> optionalEvaluatorStep.withConstraint(null));
+ assertThatNullPointerException().isThrownBy(() -> optionalEvaluatorStep.withConstraint("foo"::equals, (int[]) null));
+ assertThatIllegalArgumentException().isThrownBy(() -> optionalEvaluatorStep.withConstraint("foo"::equals, IntArrays.EMPTY));
+ assertThatIllegalArgumentException().isThrownBy(() -> optionalEvaluatorStep.withConstraint("foo"::equals, -1));
+ assertThatIllegalArgumentException().isThrownBy(() -> optionalEvaluatorStep.withConstraint("foo"::equals, 2));
+ }
+
+ @Test
+ void testWithEstimation() {
+ final var optionalEvaluatorStep = DsvMender.builder()
+ .withDelimiter(",")
+ .withLength(2);
+ final var buildStep = optionalEvaluatorStep.withEstimation(Function.identity());
+ assertThat(buildStep).isSameAs(optionalEvaluatorStep);
+ final var dsvMender = buildStep.build();
+ final var estimationEvaluators = dsvMender.getEstimationEvaluators();
+ assertThat(estimationEvaluators).hasSize(dsvMender.getLength());
+ for (final var estimationEvaluator : estimationEvaluators) {
+ assertThat(estimationEvaluator.evaluate(ObjectArrays.of("foo", "foo"))).isNaN();
+ assertThat(estimationEvaluator.evaluate(ObjectArrays.of("foo", "foo"))).isNaN();
+ estimationEvaluator.fit(ObjectArrays.of("foo", "foo"));
+ assertThat(estimationEvaluator.evaluate(ObjectArrays.of("foo", "foo"))).isEqualTo(1.0d);
+ assertThat(estimationEvaluator.evaluate(ObjectArrays.of("bar", "bar"))).isEqualTo(0.0d);
+ estimationEvaluator.fit(ObjectArrays.of("bar", "bar"));
+ assertThat(estimationEvaluator.evaluate(ObjectArrays.of("foo", "foo"))).isEqualTo(0.5d);
+ assertThat(estimationEvaluator.evaluate(ObjectArrays.of("bar", "bar"))).isEqualTo(0.5d);
+ }
+ }
+
+ @Test
+ void testWithEstimationIndexes() {
+ final var optionalEvaluatorStep = DsvMender.builder()
+ .withDelimiter(",")
+ .withLength(2);
+ final var buildStep = optionalEvaluatorStep.withEstimation(Function.identity(), 0);
+ assertThat(buildStep).isSameAs(optionalEvaluatorStep);
+ final var dsvMender = buildStep.build();
+ final var estimationEvaluators = dsvMender.getEstimationEvaluators();
+ assertThat(estimationEvaluators).hasSize(1);
+ for (final var estimationEvaluator : estimationEvaluators) {
+ assertThat(estimationEvaluator.evaluate(ObjectArrays.of("foo", "foo"))).isNaN();
+ assertThat(estimationEvaluator.evaluate(ObjectArrays.of("foo", "bar"))).isNaN();
+ assertThat(estimationEvaluator.evaluate(ObjectArrays.of("bar", "foo"))).isNaN();
+ assertThat(estimationEvaluator.evaluate(ObjectArrays.of("bar", "bar"))).isNaN();
+ estimationEvaluator.fit(ObjectArrays.of("foo", "foo"));
+ assertThat(estimationEvaluator.evaluate(ObjectArrays.of("foo", "foo"))).isEqualTo(1.0d);
+ assertThat(estimationEvaluator.evaluate(ObjectArrays.of("foo", "bar"))).isEqualTo(1.0d);
+ assertThat(estimationEvaluator.evaluate(ObjectArrays.of("bar", "foo"))).isEqualTo(0.0d);
+ assertThat(estimationEvaluator.evaluate(ObjectArrays.of("bar", "bar"))).isEqualTo(0.0d);
+ estimationEvaluator.fit(ObjectArrays.of("bar", "bar"));
+ assertThat(estimationEvaluator.evaluate(ObjectArrays.of("foo", "foo"))).isEqualTo(0.5d);
+ assertThat(estimationEvaluator.evaluate(ObjectArrays.of("foo", "bar"))).isEqualTo(0.5d);
+ assertThat(estimationEvaluator.evaluate(ObjectArrays.of("bar", "foo"))).isEqualTo(0.5d);
+ assertThat(estimationEvaluator.evaluate(ObjectArrays.of("bar", "bar"))).isEqualTo(0.5d);
+ }
+ }
+
+ @Test
+ void testWithEstimationInvalid() {
+ final var delimiterStep = DsvMender.builder();
+ final var lengthStep = delimiterStep.withDelimiter(",");
+ final var optionalEvaluatorStep = lengthStep.withLength(2);
+ assertThatNullPointerException().isThrownBy(() -> optionalEvaluatorStep.withEstimation(null));
+ assertThatNullPointerException().isThrownBy(() -> optionalEvaluatorStep.withEstimation(Function.identity(), (int[]) null));
+ assertThatIllegalArgumentException().isThrownBy(() -> optionalEvaluatorStep.withEstimation(Function.identity(), IntArrays.EMPTY));
+ assertThatIllegalArgumentException().isThrownBy(() -> optionalEvaluatorStep.withEstimation(Function.identity(), -1));
+ assertThatIllegalArgumentException().isThrownBy(() -> optionalEvaluatorStep.withEstimation(Function.identity(), 2));
+ }
+
+ @Test
+ void testBasic() {
+ {
+ final var dsvMender = DsvMender.basic(',', 2);
+ assertThat(dsvMender.getDelimiter()).isEqualTo(",");
+ }
+ final var dsvMender = DsvMender.basic(",", 2);
+ assertThat(dsvMender.getDelimiter()).isEqualTo(",");
+ assertThat(dsvMender.getLength()).isEqualTo(2);
+ final var estimationEvaluators = dsvMender.getEstimationEvaluators();
+ assertThat(estimationEvaluators).hasSize(2 * dsvMender.getLength());
+ for (final var estimationEvaluator : estimationEvaluators) {
+ assertThat(estimationEvaluator.evaluate(ObjectArrays.of("foo", "foo"))).isNaN();
+ assertThat(estimationEvaluator.evaluate(ObjectArrays.of("", ""))).isNaN();
+ estimationEvaluator.fit(ObjectArrays.of("foo", "foo"));
+ assertThat(estimationEvaluator.evaluate(ObjectArrays.of("foo", "foo"))).isEqualTo(1.0d);
+ assertThat(estimationEvaluator.evaluate(ObjectArrays.of("", ""))).isEqualTo(0.0d);
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/test/java/com/github/alexisjehan/mender/dsv/DsvMenderTest.java b/src/test/java/com/github/alexisjehan/mender/dsv/DsvMenderTest.java
new file mode 100644
index 0000000..d447531
--- /dev/null
+++ b/src/test/java/com/github/alexisjehan/mender/dsv/DsvMenderTest.java
@@ -0,0 +1,153 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2017-2019 Alexis Jehan
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+package com.github.alexisjehan.mender.dsv;
+
+import com.github.alexisjehan.javanilla.lang.Strings;
+import com.github.alexisjehan.javanilla.lang.array.ObjectArrays;
+import com.github.alexisjehan.mender.api.MendException;
+import com.github.alexisjehan.mender.api.evaluators.ConstraintEvaluator;
+import com.github.alexisjehan.mender.api.evaluators.EstimationEvaluator;
+import org.junit.jupiter.api.Test;
+
+import java.util.Set;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
+import static org.assertj.core.api.Assertions.assertThatNullPointerException;
+
+/**
+ * {@link DsvMender} unit tests.
+ */
+final class DsvMenderTest {
+
+ @Test
+ void testConstructorInvalid() {
+ assertThatNullPointerException().isThrownBy(() -> new DsvMender(null, 3, 5, Set.of(new ConstraintEvaluator<>(values -> "foo".equals(values[0]))), Set.of(new EstimationEvaluator<>(values -> values[2]))));
+ assertThatIllegalArgumentException().isThrownBy(() -> new DsvMender(Strings.EMPTY, 3, 5, Set.of(new ConstraintEvaluator<>(values -> "foo".equals(values[0]))), Set.of(new EstimationEvaluator<>(values -> values[2]))));
+ assertThatIllegalArgumentException().isThrownBy(() -> new DsvMender(",", 1, 5, Set.of(new ConstraintEvaluator<>(values -> "foo".equals(values[0]))), Set.of(new EstimationEvaluator<>(values -> values[2]))));
+ assertThatIllegalArgumentException().isThrownBy(() -> new DsvMender(",", 3, 0, Set.of(new ConstraintEvaluator<>(values -> "foo".equals(values[0]))), Set.of(new EstimationEvaluator<>(values -> values[2]))));
+ assertThatNullPointerException().isThrownBy(() -> new DsvMender(",", 3, 5, null, Set.of(new EstimationEvaluator<>(values -> values[2]))));
+ assertThatNullPointerException().isThrownBy(() -> new DsvMender(",", 3, 5, Set.of((ConstraintEvaluator) null), Set.of(new EstimationEvaluator<>(values -> values[2]))));
+ assertThatNullPointerException().isThrownBy(() -> new DsvMender(",", 3, 5, Set.of(new ConstraintEvaluator<>(values -> "foo".equals(values[0]))), null));
+ assertThatNullPointerException().isThrownBy(() -> new DsvMender(",", 3, 5, Set.of(new ConstraintEvaluator<>(values -> "foo".equals(values[0]))), Set.of((EstimationEvaluator) null)));
+ }
+
+ @Test
+ void testOptimize() {
+ final var dsvMender = new DsvMender(",", 3, 5, Set.of(new ConstraintEvaluator<>(values -> "foo".equals(values[0]))), Set.of(new EstimationEvaluator<>(values -> values[2])));
+ assertThat(dsvMender.optimize(0, "foo,")).containsExactly("foo", "");
+ assertThat(dsvMender.optimize(0, "foo", "")).containsExactly("foo", "");
+ assertThat(dsvMender.optimize(0, "foo", "bar", "", "")).containsExactly("foo", "bar", ",");
+ assertThat(dsvMender.optimize(0, "", "foo")).containsExactly("", "foo");
+ assertThat(dsvMender.optimize(0, "", "", "bar", "foo")).containsExactly(",", "bar", "foo");
+ assertThat(dsvMender.optimize(0, "foo", "", "", "bar", "")).containsExactly("foo", ",", "bar", "");
+ assertThat(dsvMender.optimize(0, "foo", "bar")).containsExactly("foo", "bar");
+ assertThat(dsvMender.optimize(0, "foo", "", "bar")).containsExactly("foo", "", "bar");
+ assertThat(dsvMender.optimize(0, "foo", "", "", "", "", "bar")).containsExactly("foo", ",,,", "bar");
+ assertThat(dsvMender.optimize(1, "foo", "", "", "", "", "bar")).containsExactly("foo", "", ",", "", "bar");
+ assertThat(dsvMender.optimize(2, "foo", "", "", "", "", "bar")).containsExactly("foo", "", "", "", "", "bar");
+ }
+
+ @Test
+ void testOptimizeInvalid() {
+ final var dsvMender = new DsvMender(",", 3, 5, Set.of(new ConstraintEvaluator<>(values -> "foo".equals(values[0]))), Set.of(new EstimationEvaluator<>(values -> values[2])));
+ assertThatIllegalArgumentException().isThrownBy(() -> dsvMender.optimize(-1, "foo", "", "", "", "bar"));
+ assertThatNullPointerException().isThrownBy(() -> dsvMender.optimize(0, (String) null));
+ assertThatNullPointerException().isThrownBy(() -> dsvMender.optimize(0, (String[]) null));
+ }
+
+ @Test
+ void testMend() {
+ {
+ final var dsvMender = new DsvMender(",", 3, 5, Set.of(new ConstraintEvaluator<>(values -> "foo".equals(values[0]))), Set.of(new EstimationEvaluator<>(values -> values[2])));
+ assertThat(dsvMender.mend("foo,,bar")).containsExactly("foo", "", "bar");
+ assertThat(dsvMender.mend("foo", "", "bar")).containsExactly("foo", "", "bar");
+ assertThat(dsvMender.mend("foo")).containsExactly("foo", "", "");
+ assertThat(dsvMender.mend("foo", "", "", "", "bar")).containsExactly("foo", ",,", "bar");
+ assertThatExceptionOfType(MendException.class).isThrownBy(() -> dsvMender.mend("bar", "", "foo"));
+ }
+ {
+ final var dsvMender = new DsvMender(",", 3, 5, Set.of(), Set.of());
+ assertThatExceptionOfType(MendException.class).isThrownBy(() -> dsvMender.mend("foo", "bar"));
+ }
+ {
+ final var dsvMender = new DsvMender(",", 3, 5, Set.of(), Set.of(new EstimationEvaluator<>(values -> values[0].length()), new EstimationEvaluator<>(values -> values[1].length()), new EstimationEvaluator<>(values -> values[2].length())));
+ assertThat(dsvMender.mend("foo", "", "bar")).containsExactly("foo", "", "bar");
+ assertThat(dsvMender.mend("f", "o", "", "b", "r")).containsExactly("f,o", "", "b,r");
+ }
+ }
+
+ @Test
+ void testMendInvalid() {
+ final var dsvMender = new DsvMender(",", 3, 5, Set.of(new ConstraintEvaluator<>(values -> "foo".equals(values[0]))), Set.of(new EstimationEvaluator<>(values -> values[2])));
+ assertThatNullPointerException().isThrownBy(() -> dsvMender.mend((String) null));
+ assertThatNullPointerException().isThrownBy(() -> dsvMender.mend((String[]) null));
+ }
+
+ @Test
+ void testGetLastResult() {
+ final var dsvMender = new DsvMender(",", 3, 5, Set.of(new ConstraintEvaluator<>(values -> "foo".equals(values[0]))), Set.of(new EstimationEvaluator<>(values -> values[2])));
+ assertThat(dsvMender.getLastResult()).isEmpty();
+ assertThat(dsvMender.mend("foo", "", "bar")).containsExactly("foo", "", "bar");
+ assertThat(dsvMender.getLastResult()).isEmpty();
+ assertThat(dsvMender.mend("foo", "bar")).containsExactly("foo", "", "bar");
+ final var optionalLastResult = dsvMender.getLastResult();
+ assertThat(optionalLastResult).isPresent();
+ final var lastResult = optionalLastResult.orElseThrow();
+ assertThat(lastResult.getValue()).containsExactly("foo", "bar");
+ assertThat(lastResult.getCandidates()).containsExactlyInAnyOrder(
+ new DsvMendCandidate(ObjectArrays.of("", "foo", "bar"), Double.NaN),
+ new DsvMendCandidate(ObjectArrays.of("foo", "", "bar"), 1.0d),
+ new DsvMendCandidate(ObjectArrays.of("foo", "bar", ""), 0.5d)
+ );
+ assertThat(lastResult.getBestCandidate()).isEqualTo(new DsvMendCandidate(ObjectArrays.of("foo", "", "bar"), 1.0d));
+ }
+
+ @Test
+ void testGetters() {
+ final var dsvMender = new DsvMender(",", 3, 5, Set.of(new ConstraintEvaluator<>(values -> "foo".equals(values[0]))), Set.of(new EstimationEvaluator<>(values -> values[2])));
+ assertThat(dsvMender.getDelimiter()).isEqualTo(",");
+ assertThat(dsvMender.getLength()).isEqualTo(3);
+ assertThat(dsvMender.getMaxDepth()).isEqualTo(5);
+ final var constraintEvaluators = dsvMender.getConstraintEvaluators();
+ assertThat(constraintEvaluators).hasSize(1);
+ for (final var constraintEvaluator : constraintEvaluators) {
+ assertThat(constraintEvaluator.evaluate(ObjectArrays.of("foo", "foo", "foo"))).isEqualTo(1.0d);
+ assertThat(constraintEvaluator.evaluate(ObjectArrays.of("bar", "foo", "foo"))).isNaN();
+ }
+ final var estimationEvaluators = dsvMender.getEstimationEvaluators();
+ assertThat(estimationEvaluators).hasSize(1);
+ for (final var estimationEvaluator : estimationEvaluators) {
+ assertThat(estimationEvaluator.evaluate(ObjectArrays.of("foo", "foo", "foo"))).isNaN();
+ assertThat(estimationEvaluator.evaluate(ObjectArrays.of("foo", "foo", "foo"))).isNaN();
+ estimationEvaluator.fit(ObjectArrays.of("foo", "foo", "foo"));
+ assertThat(estimationEvaluator.evaluate(ObjectArrays.of("foo", "foo", "foo"))).isEqualTo(1.0d);
+ assertThat(estimationEvaluator.evaluate(ObjectArrays.of("foo", "foo", "bar"))).isEqualTo(0.0d);
+ estimationEvaluator.fit(ObjectArrays.of("foo", "foo", "bar"));
+ assertThat(estimationEvaluator.evaluate(ObjectArrays.of("foo", "foo", "foo"))).isEqualTo(0.5d);
+ assertThat(estimationEvaluator.evaluate(ObjectArrays.of("foo", "foo", "bar"))).isEqualTo(0.5d);
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/test/java/org/mender/criteria/ConstraintTest.java b/src/test/java/org/mender/criteria/ConstraintTest.java
deleted file mode 100644
index 43c3955..0000000
--- a/src/test/java/org/mender/criteria/ConstraintTest.java
+++ /dev/null
@@ -1,59 +0,0 @@
-/*
-MIT License
-
-Copyright (c) 2018 Alexis Jehan
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all
-copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-SOFTWARE.
-*/
-package org.mender.criteria;
-
-import org.junit.Assert;
-import org.junit.Test;
-
-/**
- * {@link Constraint} unit tests.
- */
-public class ConstraintTest {
-
- @Test
- public void testSimple() {
- final Constraint constraint = new Constraint<>("foo"::equals);
- Assert.assertEquals(1.0d, constraint.calculate("foo"), 0);
- Assert.assertEquals(0.0d, constraint.calculate("bar"), 0);
- }
-
- @Test
- public void testStatic() {
- final Constraint constraint = Constraint.of("foo"::equals);
- Assert.assertEquals(1.0d, constraint.calculate("foo"), 0);
- Assert.assertEquals(0.0d, constraint.calculate("bar"), 0);
- }
-
- @Test
- public void testCheck() {
- final Constraint constraint = new Constraint<>("foo"::equals);
- Assert.assertTrue(constraint.check("foo"));
- Assert.assertFalse(constraint.check("bar"));
- }
-
- @Test(expected = NullPointerException.class)
- public void testNull() {
- new Constraint<>(null);
- }
-}
\ No newline at end of file
diff --git a/src/test/java/org/mender/criteria/EstimationTest.java b/src/test/java/org/mender/criteria/EstimationTest.java
deleted file mode 100644
index 367d036..0000000
--- a/src/test/java/org/mender/criteria/EstimationTest.java
+++ /dev/null
@@ -1,87 +0,0 @@
-/*
-MIT License
-
-Copyright (c) 2018 Alexis Jehan
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all
-copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-SOFTWARE.
-*/
-package org.mender.criteria;
-
-import org.junit.Assert;
-import org.junit.Test;
-
-/**
- * {@link Estimation} unit tests.
- */
-public class EstimationTest {
-
- @Test
- public void testSimple() {
- final Estimation estimation = new Estimation<>(value -> value);
- estimation.adjust("foo");
- estimation.adjust("foo");
- estimation.adjust("foo");
- estimation.adjust("bar");
- Assert.assertEquals(0.75d, estimation.calculate("foo"), 0);
- Assert.assertEquals(0.25d, estimation.calculate("bar"), 0);
- }
-
- @Test
- public void testStatic() {
- final Estimation estimation = Estimation.of(value -> value);
- estimation.adjust("foo");
- estimation.adjust("foo");
- estimation.adjust("foo");
- estimation.adjust("bar");
- Assert.assertEquals(0.75d, estimation.calculate("foo"), 0);
- Assert.assertEquals(0.25d, estimation.calculate("bar"), 0);
- }
-
- @Test(expected = NullPointerException.class)
- public void testNull() {
- new Estimation<>(null);
- }
-
- @Test
- public void testNotPresent() {
- final Estimation estimation = new Estimation<>(value -> value);
- estimation.adjust("foo");
- estimation.adjust("foo");
- Assert.assertEquals(0.0d, estimation.calculate("bar"), 0);
- }
-
- @Test
- public void testEmpty() {
- final Estimation estimation = new Estimation<>(value -> value);
- Assert.assertEquals(0.0d, estimation.calculate("foo"), 0);
- }
-
- @Test
- public void testTransform() {
- final Estimation estimation = new Estimation<>(String::length);
- estimation.adjust("fooo");
- estimation.adjust("fooo");
- estimation.adjust("foo");
- estimation.adjust("bar");
- Assert.assertEquals(0.0d, estimation.calculate("fo"), 0);
- Assert.assertEquals(0.5d, estimation.calculate("foo"), 0);
- Assert.assertEquals(0.5d, estimation.calculate("fooo"), 0);
- Assert.assertEquals(0.5d, estimation.calculate("bar"), 0);
- }
-}
\ No newline at end of file
diff --git a/src/test/java/org/mender/dsv/DsvBuilderTest.java b/src/test/java/org/mender/dsv/DsvBuilderTest.java
deleted file mode 100644
index 04abaf8..0000000
--- a/src/test/java/org/mender/dsv/DsvBuilderTest.java
+++ /dev/null
@@ -1,938 +0,0 @@
-/*
-MIT License
-
-Copyright (c) 2018 Alexis Jehan
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all
-copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-SOFTWARE.
-*/
-package org.mender.dsv;
-
-import java.util.regex.Pattern;
-
-import org.junit.Assert;
-import org.junit.Test;
-import org.mender.MenderException;
-
-/**
- * {@link DsvBuilder} unit tests.
- */
-public class DsvBuilderTest {
-
- @Test
- public void testEmptyEstimations() {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withEmptyEstimations()
- .build();
- mender.fit("a,b,c");
- mender.fit("a,,c");
- mender.fit("a,,c");
- try {
- final String[] values = mender.fix("a,,,");
- Assert.assertEquals("a", values[0]);
- Assert.assertEquals( "", values[1]); // Has been empty two times
- Assert.assertEquals(",", values[2]); // Has not been empty
- } catch (final MenderException e) {
- Assert.fail(e.getMessage());
- }
- }
-
- @Test
- public void testEmptyEstimation() {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withEmptyEstimation(0)
- .withEmptyEstimation(2)
- .build();
- mender.fit(",,");
- mender.fit(",,");
- try {
- final String[] values = mender.fix(",,,,,,");
- Assert.assertTrue(values[0].isEmpty()); // Has been empty all times
- Assert.assertFalse(values[1].isEmpty());
- Assert.assertTrue(values[2].isEmpty()); // Has been empty all times
- } catch (final MenderException e) {
- Assert.fail(e.getMessage());
- }
- }
-
- @Test
- public void testEmptyConstraint() {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withEmptyConstraint(0)
- .build();
- try {
- final String[] values = mender.fix(",a,b,c");
- Assert.assertTrue(values[0].isEmpty()); // Must be empty
- } catch (final MenderException e) {
- Assert.fail(e.getMessage());
- }
- }
-
- @Test(expected = MenderException.class)
- public void testEmptyConstraintNoSolution() throws MenderException {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withEmptyConstraint(0)
- .build();
- mender.fix("a,b,c,"); // No solution
- }
-
- @Test(expected = MenderException.class)
- public void testEmptyConstraintConflict() throws MenderException {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withEmptyConstraint(0)
- .withNonEmptyConstraint(0)
- .build();
- mender.fix("a,b,c,"); // Conflict
- }
-
- @Test
- public void testNonEmptyConstraints() {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withNonEmptyConstraints()
- .build();
- try {
- final String[] values = mender.fix(",a,,,c,");
- Assert.assertFalse(values[0].isEmpty());
- Assert.assertFalse(values[1].isEmpty());
- Assert.assertFalse(values[2].isEmpty());
- } catch (final MenderException e) {
- Assert.fail(e.getMessage());
- }
- }
-
- @Test(expected = MenderException.class)
- public void testNonEmptyConstraintsNoSolution() throws MenderException {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withNonEmptyConstraints()
- .build();
- mender.fix(",,,"); // No solution
- }
-
- @Test
- public void testNonEmptyConstraint() {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withNonEmptyConstraint(2)
- .build();
- try {
- final String[] values = mender.fix("a,,,c");
- Assert.assertFalse(values[2].isEmpty()); // Must not be empty
- } catch (final MenderException e) {
- Assert.fail(e.getMessage());
- }
- }
-
- @Test(expected = MenderException.class)
- public void testNonEmptyConstraintNoSolution() throws MenderException {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withNonEmptyConstraint(1)
- .withNonEmptyConstraint(2)
- .build();
- mender.fix("a,,,"); // No solution
- }
-
- @Test
- public void testLengthEstimations() {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withLengthEstimations()
- .build();
- mender.fit("a,bb,ccc");
- mender.fit("a,bb,ccc");
- mender.fit("a,b,ccc");
- try {
- final String[] values = mender.fix("a,ccc");
- Assert.assertEquals( "a", values[0]); // Always had a length of 1
- Assert.assertEquals( "", values[1]);
- Assert.assertEquals("ccc", values[2]); // Always had a length of 3
- } catch (final MenderException e) {
- Assert.fail(e.getMessage());
- }
- }
-
- @Test
- public void testLengthEstimation() {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withLengthEstimation(1)
- .build();
- mender.fit("aaa,bb,c");
- mender.fit("aa,bb,cc");
- mender.fit("a,bb,ccc");
- try {
- final String[] values = mender.fix("bb");
- Assert.assertEquals( "", values[0]);
- Assert.assertEquals("bb", values[1]); // Always had a length of 2
- Assert.assertEquals( "", values[2]);
- } catch (final MenderException e) {
- Assert.fail(e.getMessage());
- }
- }
-
- @Test
- public void testLengthConstraint() {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withLengthConstraint(1, 2)
- .build();
- try {
- final String[] values = mender.fix("aa,,bb,cc");
- Assert.assertEquals("aa,", values[0]);
- Assert.assertEquals( "bb", values[1]); // Must have a length of 2
- Assert.assertEquals( "cc", values[2]);
- } catch (final MenderException e) {
- Assert.fail(e.getMessage());
- }
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void testLengthConstraintInvalid() {
- DsvMender.builder(",", 3)
- .withLengthConstraint(1, -1)
- .build();
- }
-
- @Test(expected = MenderException.class)
- public void testLengthConstraintNoSolution() throws MenderException {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withLengthConstraint(1, 2)
- .build();
- mender.fix(",a,bbb,cc"); // No solution
- }
-
- @Test(expected = MenderException.class)
- public void testLengthConstraintConflict() throws MenderException {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withLengthConstraint(1, 2)
- .withLengthConstraint(1, 3)
- .build();
- mender.fix("a,bb,,cc"); // Conflict
- }
-
- @Test
- public void testMinLengthEstimations() {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withMinLengthEstimations(2)
- .build();
- mender.fit("a,bb,ccc");
- mender.fit("a,bb,ccc");
- mender.fit("a,b,ccc");
- try {
- final String[] values = mender.fix("a,,b,,c");
- Assert.assertEquals( "a", values[0]); // Always had a length less than 2
- Assert.assertEquals(",b", values[1]);
- Assert.assertEquals(",c", values[2]);
- } catch (final MenderException e) {
- Assert.fail(e.getMessage());
- }
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void testMinLengthEstimationsInvalid() {
- DsvMender.builder(",", 3)
- .withMinLengthEstimations(0)
- .build();
- }
-
- @Test
- public void testMinLengthEstimation() {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withMinLengthEstimation(1, 2)
- .build();
- mender.fit("a,bb,ccc");
- mender.fit("a,bbb,ccc");
- mender.fit("aa,b,ccc");
- try {
- final String[] values = mender.fix("a,b,,ccc");
- Assert.assertEquals( "a", values[0]);
- Assert.assertEquals( "b,", values[1]); // Had a length greater than or equal to 2 most of times
- Assert.assertEquals("ccc", values[2]);
- } catch (final MenderException e) {
- Assert.fail(e.getMessage());
- }
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void testMinLengthEstimationInvalid() {
- DsvMender.builder(",", 3)
- .withMinLengthEstimation(1, 0)
- .build();
- }
-
- @Test
- public void testMinLengthConstraint() {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withMinLengthConstraint(1, 2)
- .build();
- try {
- final String[] values = mender.fix(",,b,cc");
- Assert.assertEquals( "", values[0]);
- Assert.assertEquals(",b", values[1]); // Must have a length greater than or equal to 2
- Assert.assertEquals("cc", values[2]);
- } catch (final MenderException e) {
- Assert.fail(e.getMessage());
- }
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void testMinLengthConstraintInvalid() {
- DsvMender.builder(",", 3)
- .withMinLengthConstraint(1, 0)
- .build();
- }
-
- @Test(expected = MenderException.class)
- public void testMinLengthConstraintNoSolution() throws MenderException {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withLengthConstraint(1, 2)
- .build();
- mender.fix(",a,bbb,cc"); // No solution
- }
-
- @Test
- public void testMaxLengthEstimations() {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withNonEmptyConstraints()
- .withMaxLengthEstimations(3)
- .build();
- mender.fit("aaa,bb,c");
- mender.fit("aaa,bb,c");
- mender.fit("aaa,b,cc");
- try {
- final String[] values = mender.fix("aaa,,,b,ccc");
- Assert.assertEquals("aaa", values[0]); // Could not have a length greater than 3
- Assert.assertEquals(",,b", values[1]);
- Assert.assertEquals("ccc", values[2]);
- } catch (final MenderException e) {
- Assert.fail(e.getMessage());
- }
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void testMaxLengthEstimationsInvalid() {
- DsvMender.builder(",", 3)
- .withNonEmptyConstraints()
- .withMaxLengthEstimations(0)
- .build();
- }
-
- @Test
- public void testMaxLengthEstimation() {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withNonEmptyConstraints()
- .withMaxLengthEstimation(1, 2)
- .build();
- mender.fit("aaa,bb,c");
- mender.fit("aaa,bb,c");
- mender.fit("aaa,b,cc");
- try {
- final String[] values = mender.fix("aaa,,bb,,ccc");
- Assert.assertEquals("aaa,", values[0]);
- Assert.assertEquals( "bb", values[1]); // Could not have a length greater than 2
- Assert.assertEquals(",ccc", values[2]);
- } catch (final MenderException e) {
- Assert.fail(e.getMessage());
- }
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void testMaxLengthEstimationInvalid() {
- DsvMender.builder(",", 3)
- .withNonEmptyConstraints()
- .withMaxLengthEstimation(1, 0)
- .build();
- }
-
- @Test
- public void testMaxLengthConstraint() {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withNonEmptyConstraints()
- .withMaxLengthConstraint(1, 2)
- .build();
- try {
- final String[] values = mender.fix("aa,,bb,,cc");
- Assert.assertEquals("aa,", values[0]);
- Assert.assertEquals( "bb", values[1]); // Must not have a length greater than 2
- Assert.assertEquals(",cc", values[2]);
- } catch (final MenderException e) {
- Assert.fail(e.getMessage());
- }
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void testMaxLengthConstraintInvalid() {
- DsvMender.builder(",", 3)
- .withNonEmptyConstraints()
- .withMaxLengthConstraint(1, 0)
- .build();
- }
-
- @Test(expected = MenderException.class)
- public void testMaxLengthConstraintNoSolution() throws MenderException {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withNonEmptyConstraints()
- .withMaxLengthConstraint(2, 2)
- .build();
- mender.fix(",a,b,cccccc"); // No solution
- }
-
- @Test(expected = MenderException.class)
- public void testMaxLengthConstraintConflict() throws MenderException {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withMinLengthConstraint(2, 3)
- .withMaxLengthConstraint(2, 2)
- .build();
- mender.fix(",a,b,cc"); // Conflict
- }
-
- @Test
- public void testRangeLengthEstimations() {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withRangeLengthEstimations(2, 3)
- .build();
- mender.fit("aaa,b,ccc");
- mender.fit("aaa,,cc");
- mender.fit("aaa,b,cc");
- try {
- final String[] values = mender.fix("aaa,,,b,,c,c");
- Assert.assertEquals( "aaa", values[0]); // Could not have a length greater than 3
- Assert.assertEquals(",,b,", values[1]);
- Assert.assertEquals( "c,c", values[2]); // Could not have a length greater than 3
- } catch (final MenderException e) {
- Assert.fail(e.getMessage());
- }
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void testRangeLengthEstimationsInvalidMinLength() {
- DsvMender.builder(",", 3)
- .withRangeLengthEstimations(0, 1)
- .build();
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void testRangeLengthEstimationsInvalidMaxLength() {
- DsvMender.builder(",", 3)
- .withRangeLengthEstimations(2, 1)
- .build();
- }
-
- @Test
- public void testRangeLengthEstimation() {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withRangeLengthEstimation(1, 2, 3)
- .build();
- mender.fit("aaa,bb,c");
- mender.fit("aaa,bb,c");
- mender.fit("aaa,bbb,cc");
- try {
- final String[] values = mender.fix("aaa,,bbb,,ccc");
- Assert.assertEquals("aaa,", values[0]);
- Assert.assertEquals( "bbb", values[1]); // Could not have a length greater than 3
- Assert.assertEquals(",ccc", values[2]);
- } catch (final MenderException e) {
- Assert.fail(e.getMessage());
- }
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void testRangeLengthEstimationInvalidMinLength() {
- DsvMender.builder(",", 3)
- .withRangeLengthEstimation(1, 0, 1)
- .build();
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void testRangeLengthEstimationInvalidMaxLength() {
- DsvMender.builder(",", 3)
- .withRangeLengthEstimation(1, 2, 1)
- .build();
- }
-
- @Test
- public void testRangeLengthConstraint() {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withRangeLengthConstraint(1, 2, 3)
- .build();
- try {
- final String[] values = mender.fix("aa,,,,cc");
- Assert.assertEquals("aa", values[0]);
- Assert.assertEquals(",,", values[1]); // Must not have a length lower than 2
- Assert.assertEquals("cc", values[2]);
- } catch (final MenderException e) {
- Assert.fail(e.getMessage());
- }
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void testRangeLengthConstraintInvalidMinLength() {
- DsvMender.builder(",", 3)
- .withRangeLengthConstraint(1, 0, 1)
- .build();
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void testRangeLengthConstraintInvalidMaxLength() {
- DsvMender.builder(",", 3)
- .withRangeLengthConstraint(1, 2, 1)
- .build();
- }
-
- @Test(expected = MenderException.class)
- public void testRangeLengthConstraintNoSolution() throws MenderException {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withNonEmptyConstraints()
- .withRangeLengthConstraint(2, 5, 10)
- .build();
- mender.fix(",a,b,ccc"); // No solution
- }
-
- @Test(expected = MenderException.class)
- public void testRangeLengthConstraintConflict() throws MenderException {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withRangeLengthConstraint(2, 1, 2)
- .withRangeLengthConstraint(2, 2, 3)
- .build();
- mender.fix(",a,b,ccc"); // Conflict
- }
-
- @Test
- public void testPatternEstimations() {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withPatternEstimations(Pattern.compile("[a-z]+"))
- .build();
- mender.fit("aaa,bbb,ccc");
- mender.fit("aaa,bb,");
- mender.fit("aaa,b,ccc");
- try {
- final String[] values = mender.fix("aaa,bb,,cc");
- Assert.assertEquals("aaa", values[0]);
- Assert.assertEquals( "bb", values[1]);
- Assert.assertEquals(",cc", values[2]); // Pattern matched only two times
- } catch (final MenderException e) {
- Assert.fail(e.getMessage());
- }
- }
-
- @Test(expected = NullPointerException.class)
- public void testPatternEstimationsNull() {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withPatternEstimations(null)
- .build();
- mender.fit("aaa,bbb,ccc");
- }
-
- @Test
- public void testPatternEstimation() {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withNonEmptyConstraints()
- .withPatternEstimation(2, Pattern.compile("c+"))
- .build();
- mender.fit("aaa,bbb,ccc");
- mender.fit("aaa,b,cc");
- mender.fit("aaa,bbb,c");
- try {
- final String[] values = mender.fix("aaa,bb,,ccc");
- Assert.assertEquals("aaa", values[0]);
- Assert.assertEquals("bb,", values[1]);
- Assert.assertEquals("ccc", values[2]); // Had always matched
- } catch (final MenderException e) {
- Assert.fail(e.getMessage());
- }
- }
-
- @Test(expected = NullPointerException.class)
- public void testPatternEstimationNull() {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withPatternEstimation(2, null)
- .build();
- mender.fit("aaa,bbb,ccc");
- }
-
- @Test
- public void testPatternConstraint() {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withPatternConstraint(0, Pattern.compile("a+"))
- .withPatternConstraint(2, Pattern.compile("c+"))
- .build();
- try {
- final String[] values = mender.fix("aa,,,,,cc");
- Assert.assertEquals( "aa", values[0]); // Must only contains 'a' chars
- Assert.assertEquals(",,,", values[1]);
- Assert.assertEquals( "cc", values[2]); // Must only contains 'c' chars
- } catch (final MenderException e) {
- Assert.fail(e.getMessage());
- }
- }
-
- @Test(expected = NullPointerException.class)
- public void testPatternConstraintNull() {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withPatternConstraint(2, null)
- .build();
- try {
- mender.fix("a,b,c,");
- } catch (final MenderException e) {
- Assert.fail(e.getMessage());
- }
- }
-
- @Test(expected = MenderException.class)
- public void testPatternConstraintNoSolution() throws MenderException {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withNonEmptyConstraints()
- .withPatternConstraint(2, Pattern.compile("c+"))
- .build();
- mender.fix(",a,b,cc,"); // No solution
- }
-
- @Test(expected = MenderException.class)
- public void testPatternConstraintConflict() throws MenderException {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withPatternConstraint(2, Pattern.compile("[^c]+"))
- .withPatternConstraint(2, Pattern.compile("c+"))
- .build();
- mender.fix(",a,b,cc"); // Conflict
- }
-
- @Test
- public void testContainsEstimations() {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withNonEmptyConstraints()
- .withContainsEstimations("bb")
- .build();
- mender.fit("aa,bbb,ccc");
- mender.fit("aa,bb,c");
- mender.fit("aa,bb,ccc");
- try {
- final String[] values = mender.fix(",aa,bb,cc,");
- Assert.assertEquals(",aa", values[0]);
- Assert.assertEquals( "bb", values[1]); // Always contained "bb" substring
- Assert.assertEquals("cc,", values[2]);
- } catch (final MenderException e) {
- Assert.fail(e.getMessage());
- }
- }
-
- @Test(expected = NullPointerException.class)
- public void testContainsEstimationsNull() {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withContainsEstimations(null)
- .build();
- mender.fit("aa,bbb,ccc");
- }
-
- @Test
- public void testContainsEstimation() {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withNonEmptyConstraints()
- .withContainsEstimation(0, "aa")
- .build();
- mender.fit("aaa,bbb,ccc");
- mender.fit("aaa,b,cc");
- mender.fit("aaa,bbb,c");
- try {
- final String[] values = mender.fix("a,aa,bb,ccc");
- Assert.assertEquals( "a,aa", values[0]); // Had always matched
- Assert.assertEquals( "bb", values[1]);
- Assert.assertEquals( "ccc", values[2]);
- } catch (final MenderException e) {
- Assert.fail(e.getMessage());
- }
- }
-
- @Test(expected = NullPointerException.class)
- public void testContainsEstimationNull() {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withNonEmptyConstraints()
- .withContainsEstimation(0, null)
- .build();
- mender.fit("aaa,bbb,ccc");
- }
-
- @Test
- public void testContainsConstraint() {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withContainsConstraint(0, "aa")
- .withContainsConstraint(2, "cc")
- .build();
- try {
- final String[] values = mender.fix(",aa,,cc,");
- Assert.assertEquals(",aa", values[0]); // Must contains "aa" substring
- Assert.assertEquals( "", values[1]);
- Assert.assertEquals("cc,", values[2]); // Must contains "cc" substring
- } catch (final MenderException e) {
- Assert.fail(e.getMessage());
- }
- }
-
- @Test(expected = NullPointerException.class)
- public void testContainsConstraintNull() {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withContainsConstraint(0, null)
- .build();
- try {
- mender.fix(",aa,,cc,");
- } catch (final MenderException e) {
- Assert.fail(e.getMessage());
- }
- }
-
- @Test(expected = MenderException.class)
- public void testContainsConstraintNoSolution() throws MenderException {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withContainsConstraint(1, "z")
- .build();
- mender.fix(",a,b,cc,"); // No solution
- }
-
- @Test(expected = MenderException.class)
- public void testContainsConstraintConflict() throws MenderException {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withContainsConstraint(1, "z")
- .withContainsNoneConstraint(1, "z")
- .build();
- mender.fix(",a,b,cc"); // Conflict
- }
-
- @Test
- public void testContainsNoneConstraint() {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withContainsNoneConstraint(0, ",")
- .withContainsNoneConstraint(2, ",")
- .build();
- try {
- final String[] values = mender.fix("aa,,,,,cc");
- Assert.assertEquals( "aa", values[0]); // Must contains "," substring
- Assert.assertEquals(",,,", values[1]);
- Assert.assertEquals( "cc", values[2]); // Must contains "," substring
- } catch (final MenderException e) {
- Assert.fail(e.getMessage());
- }
- }
-
- @Test(expected = NullPointerException.class)
- public void testContainsNoneConstraintNull() {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withContainsNoneConstraint(0, null)
- .build();
- try {
- mender.fix("aa,,,,,cc");
- } catch (final MenderException e) {
- Assert.fail(e.getMessage());
- }
- }
-
- @Test
- public void testStartsWithEstimations() {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withNonEmptyConstraints()
- .withStartsWithEstimations(",")
- .build();
- mender.fit("aaa,bb,c");
- mender.fit("aaa,bb,cc");
- mender.fit("aaa,bb,ccc");
- try {
- final String[] values = mender.fix("aaa,bb,,ccc");
- Assert.assertEquals("aaa", values[0]);
- Assert.assertEquals("bb,", values[1]);
- Assert.assertEquals("ccc", values[2]); // Had never started with ','
- } catch (final MenderException e) {
- Assert.fail(e.getMessage());
- }
- }
-
- @Test(expected = NullPointerException.class)
- public void testStartsWithEstimationsNull() {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withStartsWithEstimations(null)
- .build();
- mender.fit("aaa,bbb,ccc");
- }
-
- @Test
- public void testStartsWithEstimation() {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withNonEmptyConstraints()
- .withStartsWithEstimation(1, "b")
- .build();
- mender.fit("aaa,bb,c");
- mender.fit("aaa,bb,cc");
- mender.fit("aaa,bb,ccc");
- try {
- final String[] values = mender.fix("a,,,bb,ccc");
- Assert.assertEquals("a,,", values[0]);
- Assert.assertEquals( "bb", values[1]); // Had always started with 'b'
- Assert.assertEquals("ccc", values[2]);
- } catch (final MenderException e) {
- Assert.fail(e.getMessage());
- }
- }
-
- @Test(expected = NullPointerException.class)
- public void testStartsWithEstimationNull() {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withStartsWithEstimation(1, null)
- .build();
- mender.fit("aaa,bbb,ccc");
- }
-
- @Test
- public void testStartsWithConstraint() {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withNonEmptyConstraints()
- .withStartsWithConstraint(1, "b")
- .build();
- try {
- final String[] values = mender.fix("a,,,bb,ccc");
- Assert.assertEquals("a,,", values[0]);
- Assert.assertEquals( "bb", values[1]); // Must start with 'b'
- Assert.assertEquals("ccc", values[2]);
- } catch (final MenderException e) {
- Assert.fail(e.getMessage());
- }
- }
-
- @Test(expected = NullPointerException.class)
- public void testStartsWithConstraintNull() {
- DsvMender.builder(",", 3)
- .withStartsWithConstraint(1, null)
- .build();
- }
-
- @Test(expected = MenderException.class)
- public void testStartsWithConstraintNoSolution() throws MenderException {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withStartsWithConstraint(1, "z")
- .build();
- mender.fix(",a,b,cc,"); // No solution
- }
-
- @Test(expected = MenderException.class)
- public void testStartsWithConstraintConflict() throws MenderException {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withStartsWithConstraint(1, "b")
- .withStartsWithConstraint(1, "c")
- .build();
- mender.fix(",a,b,cc"); // Conflict
- }
-
- @Test
- public void testEndsWithEstimations() {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withNonEmptyConstraints()
- .withEndsWithEstimations(",")
- .build();
- mender.fit("aaa,bb,c");
- mender.fit("aaa,bb,cc");
- mender.fit("aaa,bb,ccc");
- try {
- final String[] values = mender.fix("aaa,bbb,,cc");
- Assert.assertEquals("aaa", values[0]);
- Assert.assertEquals("bbb", values[1]); // Had never ended with ','
- Assert.assertEquals(",cc", values[2]);
- } catch (final MenderException e) {
- Assert.fail(e.getMessage());
- }
- }
-
- @Test(expected = NullPointerException.class)
- public void testEndsWithEstimationsNull() {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withEndsWithEstimations(null)
- .build();
- mender.fit("aaa,bbb,ccc");
- }
-
- @Test
- public void testEndsWithEstimation() {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withNonEmptyConstraints()
- .withEndsWithEstimation(1, "b")
- .build();
- mender.fit("aaa,bb,c");
- mender.fit("aaa,bb,cc");
- mender.fit("aaa,bb,ccc");
- try {
- final String[] values = mender.fix("aaa,bb,,,c");
- Assert.assertEquals("aaa", values[0]);
- Assert.assertEquals( "bb", values[1]); // Had always ended with 'b'
- Assert.assertEquals(",,c", values[2]);
- } catch (final MenderException e) {
- Assert.fail(e.getMessage());
- }
- }
-
- @Test(expected = NullPointerException.class)
- public void testEndsWithEstimationNull() {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withEndsWithEstimation(1, null)
- .build();
- mender.fit("aaa,bbb,ccc");
- }
-
- @Test
- public void testEndsWithConstraint() {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withNonEmptyConstraints()
- .withEndsWithConstraint(1, "b")
- .build();
- try {
- final String[] values = mender.fix("aaa,bb,,,c");
- Assert.assertEquals("aaa", values[0]);
- Assert.assertEquals( "bb", values[1]); // Must end with 'b'
- Assert.assertEquals(",,c", values[2]);
- } catch (final MenderException e) {
- Assert.fail(e.getMessage());
- }
- }
-
- @Test(expected = NullPointerException.class)
- public void testEndsWithConstraintNull() {
- DsvMender.builder(",", 3)
- .withEndsWithConstraint(1, null)
- .build();
- }
-
- @Test(expected = MenderException.class)
- public void testEndsWithConstraintNoSolution() throws MenderException {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withEndsWithConstraint(1, "z")
- .build();
- mender.fix(",a,b,cc,"); // No solution
- }
-
- @Test(expected = MenderException.class)
- public void testEndsWithConstraintConflict() throws MenderException {
- final DsvMender mender = DsvMender.builder(",", 3)
- .withEndsWithConstraint(1, "b")
- .withEndsWithConstraint(1, "c")
- .build();
- mender.fix(",a,b,cc"); // Conflict
- }
-
- @Test(expected = IndexOutOfBoundsException.class)
- public void testEstimationInvalid() {
- DsvMender.builder(",", 3)
- .withEstimation(3, String::isEmpty)
- .build();
- }
-
- @Test(expected = IndexOutOfBoundsException.class)
- public void testConstraintInvalid() {
- DsvMender.builder(",", 3)
- .withConstraint(3, String::isEmpty)
- .build();
- }
-}
\ No newline at end of file
diff --git a/src/test/java/org/mender/dsv/DsvEvaluatorTest.java b/src/test/java/org/mender/dsv/DsvEvaluatorTest.java
deleted file mode 100644
index aa502b2..0000000
--- a/src/test/java/org/mender/dsv/DsvEvaluatorTest.java
+++ /dev/null
@@ -1,122 +0,0 @@
-package org.mender.dsv;
-
-import org.junit.Assert;
-import org.junit.Test;
-import org.mender.criteria.Constraint;
-import org.mender.criteria.Estimation;
-
-/**
- * {@link DsvEvaluator} unit tests.
- */
-public class DsvEvaluatorTest {
-
- @Test
- public void testConstraint() {
- final DsvEvaluator evaluator = new DsvEvaluator();
- evaluator.addConstraint(1, Constraint.of("+"::equals));
- Assert.assertEquals(0.0d, evaluator.evaluate(new String[] {"-", "-", "-"}), 0); // Second value is not "+"
- Assert.assertEquals(1.0d, evaluator.evaluate(new String[] {"-", "+", "-"}), 0); // Ok
- }
-
- @Test
- public void testConstraints() {
- final DsvEvaluator evaluator = new DsvEvaluator();
- evaluator.addConstraint(0, Constraint.of("-"::equals));
- evaluator.addConstraint(1, Constraint.of("+"::equals));
- evaluator.addConstraint(2, Constraint.of(value -> 2 == value.length()));
- Assert.assertEquals(0.0d, evaluator.evaluate(new String[] {"+", "-", "-" }), 0); // First value is not "-"
- Assert.assertEquals(0.0d, evaluator.evaluate(new String[] {"-", "-", "-" }), 0); // Second value is not "+"
- Assert.assertEquals(0.0d, evaluator.evaluate(new String[] {"-", "+", "-" }), 0); // Third value is not two chars length
- Assert.assertEquals(1.0d, evaluator.evaluate(new String[] {"-", "+", "--"}), 0); // Ok
- }
-
- @Test
- public void testEstimation() {
- final DsvEvaluator evaluator = new DsvEvaluator();
- evaluator.addEstimation(1, Estimation.of("+"::equals));
-
- // Not adjusted yet
- Assert.assertEquals(0.0d, evaluator.evaluate(new String[] {"-", "-", "-"}), 0);
- Assert.assertEquals(0.0d, evaluator.evaluate(new String[] {"-", "+", "-"}), 0);
-
- evaluator.adjustEstimations(new String[] {"-", "+", "-"});
-
- // Adjusted with "+" once
- Assert.assertEquals(0.0d, evaluator.evaluate(new String[] {"-", "-", "-"}), 0);
- Assert.assertEquals(1.0d, evaluator.evaluate(new String[] {"-", "+", "-"}), 0);
-
- evaluator.adjustEstimations(new String[] {"-", "-", "-"});
-
- // Adjusted with "+" once and "-" once
- Assert.assertEquals(0.5d, evaluator.evaluate(new String[] {"-", "-", "-"}), 0);
- Assert.assertEquals(0.5d, evaluator.evaluate(new String[] {"-", "+", "-"}), 0);
- }
-
- @Test
- public void testEstimations() {
- final DsvEvaluator evaluator = new DsvEvaluator();
- evaluator.addEstimation(0, Estimation.of("-"::equals));
- evaluator.addEstimation(1, Estimation.of("+"::equals));
- evaluator.addEstimation(2, Estimation.of(value -> 2 == value.length()));
-
- // Not adjusted yet
- Assert.assertEquals(0.0d, evaluator.evaluate(new String[] {"-", "+", "--"}), 0);
-
- evaluator.adjustEstimations(new String[] {"-", "+", "--"});
-
- Assert.assertEquals(0.0d, evaluator.evaluate(new String[] {"+", "-", "-" }), 0); // First value is not "-", second value is not "+" and third value is not two chars length
- Assert.assertEquals(1.0d, evaluator.evaluate(new String[] {"-", "-", "-" }), 0); // Second value is not "+" and third value is not two chars length
- Assert.assertEquals(2.0d, evaluator.evaluate(new String[] {"-", "+", "-" }), 0); // Third value is not two chars length
- Assert.assertEquals(3.0d, evaluator.evaluate(new String[] {"-", "+", "--"}), 0); // Ok
-
- evaluator.adjustEstimations(new String[] {"+", "-", "-"});
-
- // Conflicts in adjustments
- Assert.assertEquals(1.5d, evaluator.evaluate(new String[] {"+", "-", "-" }), 0);
- Assert.assertEquals(1.5d, evaluator.evaluate(new String[] {"-", "-", "-" }), 0);
- Assert.assertEquals(1.5d, evaluator.evaluate(new String[] {"-", "+", "-" }), 0);
- Assert.assertEquals(1.5d, evaluator.evaluate(new String[] {"-", "+", "--"}), 0);
- }
-
- @Test(expected = NullPointerException.class)
- public void testConstraintNull() {
- final DsvEvaluator evaluator = new DsvEvaluator();
- evaluator.addConstraint(0, null);
- }
-
- @Test(expected = IndexOutOfBoundsException.class)
- public void testConstraintNegativeIndex() {
- final DsvEvaluator evaluator = new DsvEvaluator();
- evaluator.addConstraint(-1, Constraint.of("-"::equals));
- }
-
- @Test(expected = NullPointerException.class)
- public void testEstimationNull() {
- final DsvEvaluator evaluator = new DsvEvaluator();
- evaluator.addEstimation(0, null);
- }
-
- @Test(expected = IndexOutOfBoundsException.class)
- public void testEstimationNegativeIndex() {
- final DsvEvaluator evaluator = new DsvEvaluator();
- evaluator.addEstimation(-1, Estimation.of("-"::equals));
- }
-
- @Test(expected = NullPointerException.class)
- public void testAdjustNull() {
- final DsvEvaluator evaluator = new DsvEvaluator();
- evaluator.adjustEstimations(null);
- }
-
- @Test(expected = NullPointerException.class)
- public void testEvaluateNull() {
- final DsvEvaluator evaluator = new DsvEvaluator();
- evaluator.evaluate(null);
- }
-
- @Test
- public void testEvaluateEmpty() {
- final DsvEvaluator evaluator = new DsvEvaluator();
- Assert.assertEquals(1.0d, evaluator.evaluate(new String[] {"1", "2", "3"}), 0);
- }
-}
\ No newline at end of file
diff --git a/src/test/java/org/mender/dsv/DsvMenderTest.java b/src/test/java/org/mender/dsv/DsvMenderTest.java
deleted file mode 100644
index 3871e22..0000000
--- a/src/test/java/org/mender/dsv/DsvMenderTest.java
+++ /dev/null
@@ -1,169 +0,0 @@
-/*
-MIT License
-
-Copyright (c) 2018 Alexis Jehan
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all
-copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-SOFTWARE.
-*/
-package org.mender.dsv;
-
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.util.Arrays;
-import java.util.function.Function;
-
-import org.junit.Assert;
-import org.junit.Test;
-import org.mender.MenderException;
-
-/**
- * {@link DsvMender} unit tests.
- */
-public class DsvMenderTest {
-
- @Test
- public void testSimple() {
- final DsvMender mender = DsvMender.auto(",", 3);
- mender.fit("aaa,bbb,ccc");
- try {
- final String[] values = mender.fix("aaa,bbb,c,c");
- Assert.assertEquals("aaa", values[0]);
- Assert.assertEquals("bbb", values[1]);
- Assert.assertEquals("c,c", values[2]);
- } catch (final MenderException e) {
- Assert.fail(e.getMessage());
- }
- }
-
- @Test(expected = NullPointerException.class)
- public void testNullEvaluator() {
- new DsvMender(null, ",", 2, DsvMender.DEFAULT_MAX_DEPTH);
- }
-
- @Test(expected = NullPointerException.class)
- public void testNullDelimiter() {
- new DsvMender(new DsvEvaluator(), null, 2, DsvMender.DEFAULT_MAX_DEPTH);
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void testInvalidNbColumns() {
- new DsvMender(new DsvEvaluator(), ",", 1, DsvMender.DEFAULT_MAX_DEPTH);
- }
-
- @Test(expected = NullPointerException.class)
- public void testFitRowNull() {
- final DsvMender mender = new DsvMender(new DsvEvaluator(), ",", 2, DsvMender.DEFAULT_MAX_DEPTH);
- mender.fit((String) null);
- }
-
- @Test(expected = NullPointerException.class)
- public void testFitValuesNull() {
- final DsvMender mender = new DsvMender(new DsvEvaluator(), ",", 2, DsvMender.DEFAULT_MAX_DEPTH);
- mender.fit((String[]) null);
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void testFitValuesInvalidNbColumns() {
- final DsvMender mender = new DsvMender(new DsvEvaluator(), ",", 2, DsvMender.DEFAULT_MAX_DEPTH);
- mender.fit(new String[] {"1", "2", "3"});
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void testFitValuesInvalidConstraint() {
- final DsvMender mender = DsvMender.builder(",", 2)
- .withNonEmptyConstraints()
- .build();
- mender.fit(new String[] {"1", ""});
- }
-
- @Test(expected = NullPointerException.class)
- public void testOptimizedFixRowNull() {
- final DsvMender mender = new DsvMender(new DsvEvaluator(), ",", 2, DsvMender.DEFAULT_MAX_DEPTH);
- try {
- mender.optimizedFix((String) null, 1);
- } catch (final MenderException e) {
- Assert.fail(e.getMessage());
- }
- }
-
- @Test(expected = NullPointerException.class)
- public void testOptimizedFixValuesNull() {
- final DsvMender mender = new DsvMender(new DsvEvaluator(), ",", 2, DsvMender.DEFAULT_MAX_DEPTH);
- try {
- mender.optimizedFix((String[]) null, 1);
- } catch (final MenderException e) {
- Assert.fail(e.getMessage());
- }
- }
-
- @Test(expected = NullPointerException.class)
- public void testFixRowNull() {
- final DsvMender mender = new DsvMender(new DsvEvaluator(), ",", 2, DsvMender.DEFAULT_MAX_DEPTH);
- try {
- mender.fix((String) null);
- } catch (final MenderException e) {
- Assert.fail(e.getMessage());
- }
- }
-
- @Test(expected = NullPointerException.class)
- public void testFixValuesNull() {
- final DsvMender mender = new DsvMender(new DsvEvaluator(), ",", 2, DsvMender.DEFAULT_MAX_DEPTH);
- try {
- mender.fix((String[]) null);
- } catch (final MenderException e) {
- Assert.fail(e.getMessage());
- }
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void testFitInvalidColumns() {
- final DsvMender mender = DsvMender.auto(",", 3);
- mender.fit("aaa,bbb,ccc,ddd");
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void testFixValidColumns() {
- final DsvMender mender = DsvMender.auto(",", 4);
- try {
- mender.fix("aaa,bbb,ccc,ddd");
- } catch (final MenderException e) {
- Assert.fail(e.getMessage());
- }
- }
-
- @Test
- public void testHashCode() {
- final Function standard = Arrays::hashCode;
- final Function custom = array -> {
- try {
- final Method method = DsvMender.class.getDeclaredClasses()[0].getDeclaredMethod("customHashCode", String[].class);
- method.setAccessible(true);
- return (int) method.invoke(null, new Object[] {array});
- } catch (final NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
- Assert.fail(e.getMessage());
- }
- return null;
- };
- Assert.assertEquals(standard.apply(new String[] {"00", "00"}), standard.apply(new String[] {"00", "00"}));
- Assert.assertEquals( custom.apply(new String[] {"00", "00"}) , custom.apply(new String[] {"00", "00"}));
- Assert.assertEquals( standard.apply(new String[] {"0", "000"}), standard.apply(new String[] {"00", "00"})); // Collision
- Assert.assertNotEquals( custom.apply(new String[] {"0", "000"}), custom.apply(new String[] {"00", "00"}));
- }
-}
\ No newline at end of file
diff --git a/src/test/java/org/mender/dsv/DsvReaderTest.java b/src/test/java/org/mender/dsv/DsvReaderTest.java
deleted file mode 100644
index 4858836..0000000
--- a/src/test/java/org/mender/dsv/DsvReaderTest.java
+++ /dev/null
@@ -1,77 +0,0 @@
-/*
-MIT License
-
-Copyright (c) 2018 Alexis Jehan
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all
-copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-SOFTWARE.
-*/
-package org.mender.dsv;
-
-import java.io.BufferedReader;
-import java.io.IOException;
-
-import org.junit.Assert;
-import org.junit.Test;
-import org.mender.MenderException;
-
-public class DsvReaderTest {
-
- @Test
- public void testSimple() {
- final DsvMender mender = DsvMender.auto(",", 3);
- final String dsv = "1,2,3\n"
- + "aaa,bbb,ccc\n"
- + "aaa,bbb,ccc\n"
- + "aa,,bbb,ccc";
- try (final DsvReader reader = new DsvReader(mender, dsv)) {
- Assert.assertArrayEquals(new String[] { "1", "2", "3"}, reader.readHeader());
- Assert.assertArrayEquals(new String[] {"aaa", "bbb", "ccc"}, reader.readRow());
- Assert.assertArrayEquals(new String[] {"aaa", "bbb", "ccc"}, reader.readRow());
- Assert.assertArrayEquals(new String[] {"aa,", "bbb", "ccc"}, reader.readRow());
- Assert.assertArrayEquals(null, reader.readRow());
- } catch (final IOException | MenderException e) {
- Assert.fail(e.getMessage());
- }
- }
-
- @Test(expected = IllegalStateException.class)
- public void testReadHeaderLate() {
- final DsvMender mender = DsvMender.auto(",", 3);
- final String dsv = "1,2,3\n"
- + "aaa,bbb,ccc\n"
- + "aaa,bbb,ccc\n"
- + "aa,,bbb,ccc";
- try (final DsvReader reader = new DsvReader(mender, dsv)) {
- reader.readRow();
- reader.readHeader();
- } catch (final IOException | MenderException e) {
- Assert.fail(e.getMessage());
- }
- }
-
- @Test(expected = NullPointerException.class)
- public void testNullDsvMender() {
- new DsvReader(null, "");
- }
-
- @Test(expected = NullPointerException.class)
- public void testNullBufferedReader() {
- new DsvReader(DsvMender.auto(",", 3), (BufferedReader) null);
- }
-}
\ No newline at end of file
diff --git a/src/test/java/org/mender/dsv/DsvUtilsTest.java b/src/test/java/org/mender/dsv/DsvUtilsTest.java
deleted file mode 100644
index 1c8a9cc..0000000
--- a/src/test/java/org/mender/dsv/DsvUtilsTest.java
+++ /dev/null
@@ -1,81 +0,0 @@
-/*
-MIT License
-
-Copyright (c) 2018 Alexis Jehan
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all
-copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-SOFTWARE.
-*/
-package org.mender.dsv;
-
-import org.junit.Assert;
-import org.junit.Test;
-
-/**
- * {@link DsvUtils} unit tests.
- */
-public class DsvUtilsTest {
-
- @Test
- public void testSimple() {
- Assert.assertArrayEquals(
- new String[] {"a", ":", "b", ":", "c"},
- DsvUtils.optimize(new String[] {"a", "", "", "b", ":", "c"}, ":", 5, 0)
- );
- }
-
- @Test
- public void testNbSafeColumns() {
- Assert.assertArrayEquals(
- new String[] {"a", "", ":", "", "b", "", ":", "", "c"},
- DsvUtils.optimize(new String[] {"a", "", "", "", "", "b", "", "", "", "", "c"}, ":", 5, 1)
- );
- }
-
- @Test
- public void testTooLargeNbSafeColumns() {
- Assert.assertArrayEquals(
- new String[] {"a", "", "", "b", "", "", "c"},
- DsvUtils.optimize(new String[] {"a", "", "", "b", "", "", "c"}, ":", 5, 1)
- );
- }
-
- @Test
- public void testLargeConsecutive() {
- Assert.assertArrayEquals(
- new String[] {"a", "", "", ":::::::::::::::", "", "", "b", "c"},
- DsvUtils.optimize(new String[] {"a", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "b", "c"}, ":", 5, 2)
- );
- }
-
- @Test
- public void testFirstColumn() {
- Assert.assertArrayEquals(
- new String[] {"::", "a", "b", ":", "c"},
- DsvUtils.optimize(new String[] {"", "", "", "a", "b", "", "", "c"}, ":", 5, 0)
- );
- }
-
- @Test
- public void testLastColumn() {
- Assert.assertArrayEquals(
- new String[] {"a", ":", "b", "c", "::"},
- DsvUtils.optimize(new String[] {"a", "", "", "b", "c", "", "", ""}, ":", 5, 0)
- );
- }
-}
\ No newline at end of file