All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.assertj.guava.api.MultimapAssert Maven / Gradle / Ivy

There is a newer version: 3.26.3
Show newest version
/**
 * 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.difference;
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.ShouldContainOnly.shouldContainOnly;
import static org.assertj.core.error.ShouldHaveSize.shouldHaveSize;
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.Set;

import org.assertj.core.api.AbstractAssert;
import org.assertj.core.data.MapEntry;
import org.assertj.core.internal.Failures;
import org.assertj.core.internal.Objects;
import org.assertj.core.util.VisibleForTesting;

import com.google.common.collect.ListMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.SetMultimap;

/**
 * Assertions for guava {@link Multimap}.
 *
 * @author @marcelfalliere
 * @author @miralak
 * @author Joel Costigliola
 */
public class MultimapAssert extends AbstractAssert, Multimap> {

  @VisibleForTesting
  Failures failures = Failures.instance();

  protected MultimapAssert(Multimap actual) {
    super(actual, MultimapAssert.class);
  }

  // visible for test
  protected Multimap getActual() {
    return actual;
  }

  /**
   * Verifies that the actual {@link Multimap} contains the given keys.
*

* Example : * *

 Multimap<String, String> actual = ArrayListMultimap.create();
   * 
   * actual.putAll("Lakers", newArrayList("Kobe Bryant", "Magic Johnson", "Kareem Abdul Jabbar"));
   * actual.putAll("Spurs", newArrayList("Tony Parker", "Tim Duncan", "Manu Ginobili"));
   * actual.putAll("Bulls", newArrayList("Michael Jordan", "Scottie Pippen", "Derrick Rose"));
   *
   * assertThat(actual).containsKeys("Lakers", "Bulls");
* * If the keys argument is null or empty, an {@link IllegalArgumentException} is thrown. *

* * @param keys the keys to look for in actual {@link Multimap}. * @return this {@link MultimapAssert} for assertions chaining. * @throws IllegalArgumentException if no param keys have been set. * @throws AssertionError if the actual {@link Multimap} is {@code null}. * @throws AssertionError if the actual {@link Multimap} does not contain the given keys. */ public MultimapAssert 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.containsKey(key)) { keysNotFound.add(key); } } if (!keysNotFound.isEmpty()) { throw failures.failure(info, shouldContainKeys(actual, keys, keysNotFound)); } return myself; } /** * Verifies that the actual {@link Multimap} contains the given entries.
*

* Example : * *

 Multimap<String, String> actual = ArrayListMultimap.create();
   *
   * actual.putAll("Lakers", newArrayList("Kobe Bryant", "Magic Johnson", "Kareem Abdul Jabbar"));
   * actual.putAll("Spurs", newArrayList("Tony Parker", "Tim Duncan", "Manu Ginobili"));
   * actual.putAll("Bulls", newArrayList("Michael Jordan", "Scottie Pippen", "Derrick Rose"));
   *
   * // entry can be statically imported from org.assertj.guava.api.Assertions or org.assertj.guava.data.MapEntry
   * assertThat(actual).contains(entry("Lakers", "Kobe Bryant"), entry("Spurs", "Tim Duncan"));
* * If the entries argument is null or empty, an {@link IllegalArgumentException} is thrown. *

* * @param entries the entries to look for in actual {@link Multimap}. * @return this {@link MultimapAssert} for assertions chaining. * @throws IllegalArgumentException if no param entries have been set. * @throws AssertionError if the actual {@link Multimap} is {@code null}. * @throws AssertionError if the actual {@link Multimap} does not contain the given entries. */ @SafeVarargs public final MultimapAssert 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) { if (!actual.containsEntry(entry.key, entry.value)) { entriesNotFound.add(entry); } } if (!entriesNotFound.isEmpty()) { throw failures.failure(info, shouldContain(actual, entries, entriesNotFound)); } return myself; } /** * Verifies that the actual {@link Multimap} contains the given values for any key.
*

* Example : * *

 Multimap<String, String> actual = ArrayListMultimap.create();
   *
   * actual.putAll("Lakers", newArrayList("Kobe Bryant", "Magic Johnson", "Kareem Abdul Jabbar"));
   * actual.putAll("Spurs", newArrayList("Tony Parker", "Tim Duncan", "Manu Ginobili"));
   * actual.putAll("Bulls", newArrayList("Michael Jordan", "Scottie Pippen", "Derrick Rose"));
   *
   * // note that given values are not linked to same key
   * assertThat(actual).containsValues("Kobe Bryant", "Michael Jordan");
* * If the values argument is null or empty, an {@link IllegalArgumentException} is thrown. *

* * @param values the values to look for in actual {@link Multimap}. * @return this {@link MultimapAssert} for assertions chaining. * @throws IllegalArgumentException if no param values have been set. * @throws AssertionError if the actual {@link Multimap} is {@code null}. * @throws AssertionError if the actual {@link Multimap} does not contain the given values. */ public MultimapAssert 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"); Set valuesNotFound = newLinkedHashSet(); for (V value : values) { if (!actual.containsValue(value)) { valuesNotFound.add(value); } } if (!valuesNotFound.isEmpty()) { throw failures.failure(info, shouldContainValues(actual, values, valuesNotFound)); } return myself; } /** * Verifies that the actual {@link Multimap} is empty. * *

* Example : * *

 Multimap<String, String> actual = ArrayListMultimap.create();
   *
   * assertThat(actual).isEmpty();
* * @throws AssertionError if the actual {@link Multimap} is {@code null}. * @throws AssertionError if the actual {@link Multimap} is not empty. */ public void isEmpty() { Objects.instance().assertNotNull(info, actual); if (!actual.isEmpty()) { throw failures.failure(info, shouldBeEmpty(actual)); } } /** * Verifies that the actual {@link Multimap} is not empty. * *

* Example : * *

 Multimap<String, String> actual = ArrayListMultimap.create();
   * nba.put("Bulls", "Derrick Rose");
   * nba.put("Bulls", "Joachim Noah");
   * 
   * assertThat(nba).isNotEmpty();
* * @throws AssertionError if the actual {@link Multimap} is {@code null}. * @throws AssertionError if the actual {@link Multimap} is empty. */ public void isNotEmpty() { Objects.instance().assertNotNull(info, actual); if (actual.isEmpty()) { throw failures.failure(info, shouldNotBeEmpty()); } } /** * Verifies that the number of values in the actual {@link Multimap} is equal to the given one. * *

* Example : * *

 Multimap<String, String> actual = ArrayListMultimap.create();
   *
   * actual.putAll("Lakers", newArrayList("Kobe Bryant", "Magic Johnson", "Kareem Abdul Jabbar"));
   * actual.putAll("Spurs", newArrayList("Tony Parker", "Tim Duncan", "Manu Ginobili"));
   * actual.putAll("Bulls", newArrayList("Michael Jordan", "Scottie Pippen", "Derrick Rose"));
   *
   * assertThat(actual).hasSize(9);
* * @param expectedSize the expected size of actual {@link Multimap}. * @return this {@link MultimapAssert} for assertions chaining. * @throws AssertionError if the actual {@link Multimap} is {@code null}. * @throws AssertionError if the number of values of the actual {@link Multimap} is not equal to the given one. */ public MultimapAssert hasSize(int expectedSize) { Objects.instance().assertNotNull(info, actual); int sizeOfActual = actual.size(); if (sizeOfActual == expectedSize) { return this; } throw failures.failure(info, shouldHaveSize(actual, sizeOfActual, expectedSize)); } /** * Verifies that the actual {@link Multimap} has the same entries as the given one.
* It allows to compare two multimaps having the same content but who are not equal because being of different types * like {@link SetMultimap} and {@link ListMultimap}. *

* Example : * *

 Multimap<String, String> actual = ArrayListMultimap.create();
   * listMultimap.putAll("Spurs", newArrayList("Tony Parker", "Tim Duncan", "Manu Ginobili"));
   * listMultimap.putAll("Bulls", newArrayList("Michael Jordan", "Scottie Pippen", "Derrick Rose"));
   *
   * Multimap<String, String> setMultimap = TreeMultimap.create();
   * setMultimap.putAll("Spurs", newHashSet("Tony Parker", "Tim Duncan", "Manu Ginobili"));
   * setMultimap.putAll("Bulls", newHashSet("Michael Jordan", "Scottie Pippen", "Derrick Rose"));
   *
   * // assertion will pass as listMultimap and setMultimap have the same content
   * assertThat(listMultimap).hasSameEntriesAs(setMultimap);
   * 
   * // this assertion FAILS even though both multimaps have the same content
   * assertThat(listMultimap).isEqualTo(setMultimap);
* * @param other {@link Multimap} to compare actual's entries with. * @return this {@link MultimapAssert} for assertions chaining. * @throws AssertionError if the actual {@link Multimap} is {@code null}. * @throws IllegalArgumentException if the other {@link Multimap} is {@code null}. * @throws AssertionError if actual {@link Multimap} does not have the same entries as the other {@link Multimap}. */ public final MultimapAssert hasSameEntriesAs(Multimap other) { Objects.instance().assertNotNull(info, actual); throwIllegalArgumentExceptionIfTrue(other == null, "The multimap to compare actual with should not be null"); Set entriesNotExpectedInActual = difference(newLinkedHashSet(actual.entries()), newLinkedHashSet(other.entries())); Set entriesNotFoundInActual = difference(newLinkedHashSet(other.entries()), newLinkedHashSet(actual.entries())); if (entriesNotFoundInActual.isEmpty() && entriesNotExpectedInActual.isEmpty()) return myself; throw failures.failure(info, shouldContainOnly(actual, other, entriesNotFoundInActual, entriesNotExpectedInActual)); } /** * Verifies that the actual {@link Multimap} contains all entries of the given one (it might contain more entries). *

* Example : * *

 Multimap<String, String> actual = ArrayListMultimap.create();
   * actual.putAll("Spurs", newArrayList("Tony Parker", "Tim Duncan", "Manu Ginobili"));
   * actual.putAll("Bulls", newArrayList("Michael Jordan", "Scottie Pippen", "Derrick Rose"));
   *
   * Multimap<String, String> other = TreeMultimap.create();
   * other.putAll("Spurs", newHashSet("Tony Parker", "Tim Duncan"));
   * other.putAll("Bulls", newHashSet("Michael Jordan", "Scottie Pippen"));
   *
   * // assertion will pass as other is a subset of actual.
   * assertThat(actual).containsAllEntriesOf(other);
   * 
   * // this assertion FAILS as other does not contain "Spurs -> "Manu Ginobili" and "Bulls" -> "Derrick Rose"
   * assertThat(other).containsAllEntriesOf(actual);
* * @param other {@link Multimap} to compare actual's entries with. * @return this {@link MultimapAssert} for assertions chaining. * @throws AssertionError if the actual {@link Multimap} is {@code null}. * @throws IllegalArgumentException if the other {@link Multimap} is {@code null}. * @throws AssertionError if actual {@link Multimap} does not have contain all the given {@link Multimap} entries. */ public final MultimapAssert containsAllEntriesOf(Multimap other) { Objects.instance().assertNotNull(info, actual); throwIllegalArgumentExceptionIfTrue(other == null, "The multimap to compare actual with should not be null"); Set entriesNotFoundInActual = difference(newLinkedHashSet(other.entries()), newLinkedHashSet(actual.entries())); if (entriesNotFoundInActual.isEmpty()) return myself; throw failures.failure(info, shouldContain(actual, other, entriesNotFoundInActual)); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy