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.Map; 018 019 import org.fest.assertions.core.EnumerableAssert; 020 import org.fest.assertions.data.MapEntry; 021 import org.fest.assertions.internal.Maps; 022 import org.fest.util.VisibleForTesting; 023 024 /** 025 * Assertions for <code>{@link Map}</code>s. 026 * <p> 027 * To create a new instance of this class, invoke <code>{@link Assertions#assertThat(Map)}</code>. 028 * </p> 029 * 030 * @author David DIDIER 031 * @author Yvonne Wang 032 * @author Alex Ruiz 033 */ 034 public class MapAssert extends AbstractAssert<MapAssert, Map<?, ?>> implements EnumerableAssert<MapAssert> { 035 036 @VisibleForTesting Maps maps = Maps.instance(); 037 038 protected MapAssert(Map<?, ?> actual) { 039 super(actual, MapAssert.class); 040 } 041 042 /** {@inheritDoc} */ 043 public void isNullOrEmpty() { 044 maps.assertNullOrEmpty(info, actual); 045 } 046 047 /** {@inheritDoc} */ 048 public void isEmpty() { 049 maps.assertEmpty(info, actual); 050 } 051 052 /** {@inheritDoc} */ 053 public MapAssert isNotEmpty() { 054 maps.assertNotEmpty(info, actual); 055 return this; 056 } 057 058 /** {@inheritDoc} */ 059 public MapAssert hasSize(int expected) { 060 maps.assertHasSize(info, actual, expected); 061 return this; 062 } 063 064 /** 065 * Verifies that the actual map contains the given entries, in any order. 066 * @param entries the given entries. 067 * @return {@code this} assertion object. 068 * @throws NullPointerException if the given argument is {@code null}. 069 * @throws IllegalArgumentException if the given argument is an empty array. 070 * @throws NullPointerException if any of the entries in the given array is {@code null}. 071 * @throws AssertionError if the actual map is {@code null}. 072 * @throws AssertionError if the actual map does not contain the given entries. 073 */ 074 public MapAssert contains(MapEntry...entries) { 075 maps.assertContains(info, actual, entries); 076 return this; 077 } 078 079 /** 080 * Verifies that the actual map does not contain the given entries. 081 * @param entries the given entries. 082 * @return {@code this} assertion object. 083 * @throws NullPointerException if the given argument is {@code null}. 084 * @throws IllegalArgumentException if the given argument is an empty array. 085 * @throws AssertionError if the actual map is {@code null}. 086 * @throws AssertionError if the actual map contains any of the given entries. 087 */ 088 public MapAssert doesNotContain(MapEntry...entries) { 089 maps.assertDoesNotContain(info, actual, entries); 090 return this; 091 } 092 }