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     * @author Nicolas François
036     */
037    public class MapAssert<K, V> extends AbstractAssert<MapAssert<K, V>, Map<K,V>> implements EnumerableAssert<MapAssert<K, V>, MapEntry> {
038    
039      @VisibleForTesting Maps maps = Maps.instance();
040    
041      protected MapAssert(Map<K, V> actual) {
042        super(actual, MapAssert.class);
043      }
044    
045      /** {@inheritDoc} */
046      public void isNullOrEmpty() {
047        maps.assertNullOrEmpty(info, actual);
048      }
049    
050      /** {@inheritDoc} */
051      public void isEmpty() {
052        maps.assertEmpty(info, actual);
053      }
054    
055      /** {@inheritDoc} */
056      public MapAssert<K, V> isNotEmpty() {
057        maps.assertNotEmpty(info, actual);
058        return this;
059      }
060    
061      /** {@inheritDoc} */
062      public MapAssert<K, V> hasSize(int expected) {
063        maps.assertHasSize(info, actual, expected);
064        return this;
065      }
066    
067      /**
068       * Verifies that the actual map contains the given entries, in any order.
069       * @param entries the given entries.
070       * @return {@code this} assertion object.
071       * @throws NullPointerException if the given argument is {@code null}.
072       * @throws IllegalArgumentException if the given argument is an empty array.
073       * @throws NullPointerException if any of the entries in the given array is {@code null}.
074       * @throws AssertionError if the actual map is {@code null}.
075       * @throws AssertionError if the actual map does not contain the given entries.
076       */
077      public MapAssert<K, V> contains(MapEntry...entries) {
078        maps.assertContains(info, actual, entries);
079        return this;
080      }
081    
082      /**
083       * Verifies that the actual map does not contain the given entries.
084       * @param entries the given entries.
085       * @return {@code this} assertion object.
086       * @throws NullPointerException if the given argument is {@code null}.
087       * @throws IllegalArgumentException if the given argument is an empty array.
088       * @throws AssertionError if the actual map is {@code null}.
089       * @throws AssertionError if the actual map contains any of the given entries.
090       */
091      public MapAssert<K, V> doesNotContain(MapEntry...entries) {
092        maps.assertDoesNotContain(info, actual, entries);
093        return this;
094      }
095    
096      /** {@inheritDoc} */
097      public MapAssert<K, V> usingElementComparator(Comparator<? super MapEntry> customComparator) {
098        throw new UnsupportedOperationException("custom element Comparator is not supported for MapEntry comparison");
099      }
100    
101      /** {@inheritDoc} */
102      public MapAssert<K, V> usingDefaultElementComparator() {
103        throw new UnsupportedOperationException("custom element Comparator is not supported for MapEntry comparison");
104      }
105    }