org.assertj.guava.api.RangeMapAssert Maven / Gradle / Ivy
/**
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2016 the original author or authors.
*/
package org.assertj.guava.api;
import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Sets.newLinkedHashSet;
import static org.assertj.core.error.ShouldBeEmpty.shouldBeEmpty;
import static org.assertj.core.error.ShouldContain.shouldContain;
import static org.assertj.core.error.ShouldNotBeEmpty.shouldNotBeEmpty;
import static org.assertj.guava.error.ShouldContainKeys.shouldContainKeys;
import static org.assertj.guava.error.ShouldContainValues.shouldContainValues;
import static org.assertj.guava.util.ExceptionUtils.throwIllegalArgumentExceptionIfTrue;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.assertj.core.api.AbstractAssert;
import org.assertj.core.internal.Failures;
import org.assertj.core.internal.Objects;
import org.assertj.core.util.VisibleForTesting;
import org.assertj.guava.data.MapEntry;
import com.google.common.collect.Range;
import com.google.common.collect.RangeMap;
/**
* Assertions for guava {@link com.google.common.collect.RangeMap}.
*
* To create an instance of this class, invoke {@link
* org.assertj.guava.api.Assertions#assertThat(com.google.common.collect.RangeMap)}
*
*
* @param the type of keys of the tested RangeMap value
* @param the type of values of the tested RangeMap value
* @author Marcin Kwaczyński
*/
public class RangeMapAssert, V> extends AbstractAssert, RangeMap> {
@VisibleForTesting
Failures failures = Failures.instance();
protected RangeMapAssert(final RangeMap actual) {
super(actual, RangeMapAssert.class);
}
// visible for test
protected RangeMap getActual() {
return actual;
}
/**
* Verifies that the actual {@link com.google.common.collect.RangeMap} contains the given keys.
*
* Example :
*
*
RangeMap<Integer, String> spectralColors = TreeRangeMap.create();
*
* spectralColors.put(Range.closedOpen(380, 450), "violet");
* spectralColors.put(Range.closedOpen(450, 495), "blue");
* spectralColors.put(Range.closedOpen(495, 570), "green");
* spectralColors.put(Range.closedOpen(570, 590), "yellow");
* spectralColors.put(Range.closedOpen(590, 620), "orange");
* spectralColors.put(Range.closedOpen(620, 750), "red");
*
* assertThat(spectralColors).containsKeys(380, 600, 700);
*
* If the keys
argument is null or empty, an {@link IllegalArgumentException} is thrown.
*
*
* @param keys the keys to look for in actual {@link com.google.common.collect.RangeMap}.
* @return this {@link RangeMapAssert} for assertions chaining.
* @throws IllegalArgumentException if no param keys have been set.
* @throws AssertionError if the actual {@link com.google.common.collect.RangeMap} is {@code null}.
* @throws AssertionError if the actual {@link com.google.common.collect.RangeMap} does not contain the given keys.
*/
public RangeMapAssert containsKeys(@SuppressWarnings("unchecked") K... keys) {
Objects.instance().assertNotNull(info, actual);
throwIllegalArgumentExceptionIfTrue(keys == null, "The keys to look for should not be null");
throwIllegalArgumentExceptionIfTrue(keys.length == 0, "The keys to look for should not be empty");
Set keysNotFound = newLinkedHashSet();
for (K key : keys) {
if (actual.get(key) == null) {
keysNotFound.add(key);
}
}
if (!keysNotFound.isEmpty()) {
throw failures.failure(info, shouldContainKeys(actual, keys, keysNotFound));
}
return myself;
}
/**
* Verifies that the actual {@link com.google.common.collect.RangeMap} contains the given entries.
*
* Example :
*
*
RangeMap<Integer, String> spectralColors = TreeRangeMap.create();
*
* spectralColors.put(Range.closedOpen(380, 450), "violet");
* spectralColors.put(Range.closedOpen(450, 495), "blue");
* spectralColors.put(Range.closedOpen(495, 570), "green");
* spectralColors.put(Range.closedOpen(570, 590), "yellow");
* spectralColors.put(Range.closedOpen(590, 620), "orange");
* spectralColors.put(Range.closedOpen(620, 750), "red");
*
* // entry can be statically imported from {@link org.assertj.guava.data.MapEntry}
* assertThat(spectralColors).contains(entry("400", "violet"), entry("650", "red"));
*
* If the entries
argument is null or empty, an {@link IllegalArgumentException} is thrown.
*
*
* @param entries the entries to look for in actual {@link com.google.common.collect.RangeMap}.
* @return this {@link RangeMapAssert} for assertions chaining.
* @throws IllegalArgumentException if no param entries have been set.
* @throws AssertionError if the actual {@link com.google.common.collect.RangeMap} is {@code null}.
* @throws AssertionError if the actual {@link com.google.common.collect.RangeMap} does not contain the given entries.
*/
@SafeVarargs
public final RangeMapAssert contains(MapEntry... entries) {
Objects.instance().assertNotNull(info, actual);
throwIllegalArgumentExceptionIfTrue(entries == null, "The entries to look for should not be null");
throwIllegalArgumentExceptionIfTrue(entries.length == 0, "The entries to look for should not be empty");
List> entriesNotFound = newArrayList();
for (MapEntry entry : entries) {
final V value = actual.get(entry.key);
if (value == null || !value.equals(entry.value)) {
entriesNotFound.add(entry);
}
}
if (!entriesNotFound.isEmpty()) {
throw failures.failure(info, shouldContain(actual, entries, entriesNotFound));
}
return myself;
}
/**
* Verifies that the actual {@link com.google.common.collect.RangeMap} contains the given values.
*
* Example :
*
*
RangeMap<Integer, String> spectralColors = TreeRangeMap.create();
*
* spectralColors.put(Range.closedOpen(380, 450), "violet");
* spectralColors.put(Range.closedOpen(450, 495), "blue");
* spectralColors.put(Range.closedOpen(495, 570), "green");
* spectralColors.put(Range.closedOpen(570, 590), "yellow");
* spectralColors.put(Range.closedOpen(590, 620), "orange");
* spectralColors.put(Range.closedOpen(620, 750), "red");
*
* assertThat(actual).containsValues("violet", "orange");
*
* If the values
argument is null or empty, an {@link IllegalArgumentException} is thrown.
*
*
* @param values the values to look for in actual {@link com.google.common.collect.RangeMap}.
* @return this {@link RangeMapAssert} for assertions chaining.
* @throws IllegalArgumentException if no param values have been set.
* @throws AssertionError if the actual {@link com.google.common.collect.RangeMap} is {@code null}.
* @throws AssertionError if the actual {@link com.google.common.collect.RangeMap} does not contain the given values.
*/
public RangeMapAssert containsValues(@SuppressWarnings("unchecked") V... values) {
Objects.instance().assertNotNull(info, actual);
throwIllegalArgumentExceptionIfTrue(values == null, "The values to look for should not be null");
throwIllegalArgumentExceptionIfTrue(values.length == 0, "The values to look for should not be empty");
final Map, V> mapOfRanges = actual.asMapOfRanges();
Set valuesNotFound = newLinkedHashSet();
for (V value : values) {
if (!mapOfRanges.containsValue(value)) {
valuesNotFound.add(value);
}
}
if (!valuesNotFound.isEmpty()) {
throw failures.failure(info, shouldContainValues(actual, values, valuesNotFound));
}
return myself;
}
/**
* Verifies that the actual {@link com.google.common.collect.RangeMap} is empty.
*
*
* Example :
*
*
RangeMap<Integer, String> spectralColors = TreeRangeMap.create();
*
* assertThat(actual).isEmpty();
*
* @throws AssertionError if the actual {@link com.google.common.collect.RangeMap} is {@code null}.
* @throws AssertionError if the actual {@link com.google.common.collect.RangeMap} is not empty.
*/
public RangeMapAssert isEmpty() {
Objects.instance().assertNotNull(info, actual);
if (!actual.asMapOfRanges().isEmpty()) {
throw failures.failure(info, shouldBeEmpty(actual));
}
return myself;
}
/**
* Verifies that the actual {@link com.google.common.collect.RangeMap} is not empty.
*
*
* Example :
*
*
RangeMap<Integer, String> spectralColors = TreeRangeMap.create();
* spectralColors.put(Range.closedOpen(380, 450), "violet");
* spectralColors.put(Range.closedOpen(450, 495), "blue");
* spectralColors.put(Range.closedOpen(495, 570), "green");
* spectralColors.put(Range.closedOpen(570, 590), "yellow");
* spectralColors.put(Range.closedOpen(590, 620), "orange");
* spectralColors.put(Range.closedOpen(620, 750), "red");
*
* assertThat(spectralColors).isNotEmpty();
*
* @throws AssertionError if the actual {@link com.google.common.collect.RangeMap} is {@code null}.
* @throws AssertionError if the actual {@link com.google.common.collect.RangeMap} is empty.
*/
public RangeMapAssert isNotEmpty() {
Objects.instance().assertNotNull(info, actual);
if (actual.asMapOfRanges().isEmpty()) {
throw failures.failure(info, shouldNotBeEmpty());
}
return myself;
}
}