001    /*
002     * Created on Dec 27, 2006
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 @2006-2011 the original author or authors.
014     */
015    package org.fest.assertions;
016    
017    import static java.util.Collections.emptyList;
018    
019    import java.util.*;
020    
021    import org.fest.util.IntrospectionError;
022    
023    /**
024     * Assertions for <code>{@link Collection}</code>s.
025     * <p>
026     * To create a new instance of this class invoke <code>{@link Assertions#assertThat(Collection)}</code>.
027     * </p>
028     *
029     * @author Yvonne Wang
030     * @author Alex Ruiz
031     */
032    public class CollectionAssert extends ObjectGroupAssert<CollectionAssert, Collection<?>> {
033    
034      /**
035       * Creates a new </code>{@link CollectionAssert}</code>.
036       * @param actual the target to verify.
037       */
038      protected CollectionAssert(Collection<?> actual) {
039        super(CollectionAssert.class, actual);
040      }
041    
042      /**
043       * Returns the number of elements in the actual collection.
044       * @return the number of elements in the actual collection.
045       * @throws AssertionError if the actual collection is {@code null}.
046       */
047      @Override protected int actualGroupSize() {
048        isNotNull();
049        return actual.size();
050      }
051    
052      /**
053       * Creates a new instance of <code>{@link CollectionAssert}</code> whose target collection contains the values of the
054       * given property name from the elements of this {@code CollectionAssert}'s collection. Property access works with
055       * both simple properties like {@code Person.age} and nested properties {@code Person.father.age}.
056       * <p>
057       * For example, let's say we have a collection of {@code Person} objects and you want to verify their age:
058       * <pre>
059       * assertThat(persons).onProperty("age").containsOnly(25, 16, 44, 37); // simple property
060       * assertThat(persons).onProperty("father.age").containsOnly(55, 46, 74, 62); // nested property
061       * </p>
062       * @param propertyName the name of the property to extract values from the actual collection to build a new
063       * {@code CollectionAssert}.
064       * @return a new {@code CollectionAssert} containing the values of the given property name from the elements of this
065       * {@code CollectionAssert}'s collection.
066       * @throws AssertionError if the actual collection is {@code null}.
067       * @throws IntrospectionError if an element in the given collection does not have a matching property.
068       * @since 1.3
069       */
070      @Override public CollectionAssert onProperty(String propertyName) {
071        isNotNull();
072        if (actual.isEmpty()) return new CollectionAssert(emptyList());
073        return new CollectionAssert(PropertySupport.instance().propertyValues(propertyName, actual));
074      }
075    
076      /** {@inheritDoc} */
077      @Override protected Set<Object> actualAsSet() {
078        return new LinkedHashSet<Object>(actual);
079      }
080    
081      /** {@inheritDoc} */
082      @Override protected List<Object> actualAsList() {
083        return new ArrayList<Object>(actual);
084      }
085    }