001    /*
002     * Created on Dec 21, 2010
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
005     * the License. You may obtain a copy of the License at
006     *
007     * http://www.apache.org/licenses/LICENSE-2.0
008     *
009     * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
010     * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
011     * specific language governing permissions and limitations under the License.
012     *
013     * Copyright @2010-2011 the original author or authors.
014     */
015    package org.fest.assertions.api;
016    
017    import java.util.Comparator;
018    import java.util.Map;
019    
020    import org.fest.assertions.core.EnumerableAssert;
021    import org.fest.assertions.data.MapEntry;
022    import org.fest.assertions.internal.Maps;
023    import org.fest.util.VisibleForTesting;
024    
025    /**
026     * Assertions for <code>{@link Map}</code>s.
027     * <p>
028     * To create a new instance of this class, invoke <code>{@link Assertions#assertThat(Map)}</code>.
029     * </p>
030     *
031     * @author David DIDIER
032     * @author Yvonne Wang
033     * @author Alex Ruiz
034     * @author Mikhail Mazursky
035     */
036    public class MapAssert extends AbstractAssert<MapAssert, Map<?, ?>> implements EnumerableAssert<MapAssert, MapEntry> {
037    
038      @VisibleForTesting Maps maps = Maps.instance();
039    
040      protected MapAssert(Map<?, ?> actual) {
041        super(actual, MapAssert.class);
042      }
043    
044      /** {@inheritDoc} */
045      public void isNullOrEmpty() {
046        maps.assertNullOrEmpty(info, actual);
047      }
048    
049      /** {@inheritDoc} */
050      public void isEmpty() {
051        maps.assertEmpty(info, actual);
052      }
053    
054      /** {@inheritDoc} */
055      public MapAssert isNotEmpty() {
056        maps.assertNotEmpty(info, actual);
057        return this;
058      }
059    
060      /** {@inheritDoc} */
061      public MapAssert hasSize(int expected) {
062        maps.assertHasSize(info, actual, expected);
063        return this;
064      }
065    
066      /**
067       * Verifies that the actual map contains the given entries, in any order.
068       * @param entries the given entries.
069       * @return {@code this} assertion object.
070       * @throws NullPointerException if the given argument is {@code null}.
071       * @throws IllegalArgumentException if the given argument is an empty array.
072       * @throws NullPointerException if any of the entries in the given array is {@code null}.
073       * @throws AssertionError if the actual map is {@code null}.
074       * @throws AssertionError if the actual map does not contain the given entries.
075       */
076      public MapAssert contains(MapEntry...entries) {
077        maps.assertContains(info, actual, entries);
078        return this;
079      }
080    
081      /**
082       * Verifies that the actual map does not contain the given entries.
083       * @param entries the given entries.
084       * @return {@code this} assertion object.
085       * @throws NullPointerException if the given argument is {@code null}.
086       * @throws IllegalArgumentException if the given argument is an empty array.
087       * @throws AssertionError if the actual map is {@code null}.
088       * @throws AssertionError if the actual map contains any of the given entries.
089       */
090      public MapAssert doesNotContain(MapEntry...entries) {
091        maps.assertDoesNotContain(info, actual, entries);
092        return this;
093      }
094    
095      /** {@inheritDoc} */
096      public MapAssert usingElementComparator(Comparator<? super MapEntry> customComparator) {
097        throw new UnsupportedOperationException("custom element Comparator is not supported for MapEntry comparison");
098      }
099    
100      /** {@inheritDoc} */
101      public MapAssert usingDefaultElementComparator() {
102        throw new UnsupportedOperationException("custom element Comparator is not supported for MapEntry comparison");
103      }
104    }