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

org.fest.assertions.api.MultimapAssert 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.
 */
package org.fest.assertions.api;

import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Sets.newLinkedHashSet;

import static org.fest.assertions.error.ShouldBeEmpty.shouldBeEmpty;
import static org.fest.assertions.error.ShouldContain.shouldContain;
import static org.fest.assertions.error.ShouldContainKeys.shouldContainKeys;
import static org.fest.assertions.error.ShouldContainValues.shouldContainValues;
import static org.fest.assertions.error.ShouldHaveSize.shouldHaveSize;
import static org.fest.assertions.util.ExceptionUtils.throwIllegalArgumentExceptionIfTrue;

import java.util.List;
import java.util.Set;

import com.google.common.collect.Multimap;

import org.fest.assertions.data.MapEntry;
import org.fest.assertions.internal.Failures;
import org.fest.assertions.internal.Objects;
import org.fest.util.VisibleForTesting;

/**
 * 
 * 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);
  }

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

* Example : * *

   * Multimap<String, String> actual = ArrayListMultimap.create();
   * // add several values for each key.
   * 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(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();
   * // add several values for each entrie.
   * 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.fest.assertions.api.GUAVA or org.fest.assertions.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. */ public 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();
   * // add several values for each value.
   * 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(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. * * @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 number of values in the actual {@link Multimap} is equal to the given one. * * @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)); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy