001    /*
002     * Created on May 21, 2007
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005     * in compliance with 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
010     * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011     * or implied. See the License for the specific language governing permissions and limitations under
012     * the License.
013     *
014     * Copyright @2007-2011 the original author or authors.
015     */
016    package org.fest.assertions;
017    
018    import static org.fest.assertions.Formatting.format;
019    
020    /**
021     * Template for assertions for classes representing groups of values.
022     * @param <S> used to simulate "self types." For more information please read &quot;<a
023     * href="http://passion.forco.de/content/emulating-self-types-using-java-generics-simplify-fluent-api-implementation"
024     * target="_blank">Emulating 'self types' using Java Generics to simplify fluent API implementation</a>.&quot;
025     * @param <A> the type the "actual" value.
026     *
027     * @author Yvonne Wang
028     * @author Alex Ruiz
029     */
030    public abstract class GroupAssert<S, A> extends GenericAssert<S, A> {
031    
032      /**
033       * Creates a new <code>{@link GroupAssert}</code>.
034       * @param selfType the "self type."
035       * @param actual the target to verify.
036       */
037      protected GroupAssert(Class<S> selfType, A actual) {
038        super(selfType, actual);
039      }
040    
041      /**
042       * Verifies that the actual group of values is {@code null} or empty.
043       * @throws AssertionError if the actual group of values is not {@code null} or not empty.
044       */
045      public final void isNullOrEmpty() {
046        if (actual == null || !hasElements()) return;
047        failIfCustomMessageIsSet();
048        fail(format("expecting null or empty, but was:<%s>", actual));
049      }
050    
051      /**
052       * Verifies that the actual group of values is empty.
053       * @throws AssertionError if the actual group of values is {@code null} or not empty.
054       */
055      public final void isEmpty() {
056        isNotNull();
057        if (!hasElements()) return;
058        failIfCustomMessageIsSet();
059        fail(format("expecting empty, but was:<%s>", actual));
060      }
061    
062      private boolean hasElements() {
063        return actualGroupSize() > 0;
064      }
065    
066      /**
067       * Verifies that the actual group contains at least on value.
068       * @return this assertion object.
069       * @throws AssertionError if the actual group is {@code null} or empty.
070       */
071      public final S isNotEmpty() {
072        isNotNull();
073        if (hasElements()) return myself;
074        failIfCustomMessageIsSet();
075        throw failure("expecting non-empty, but it was empty");
076      }
077    
078      /**
079       * Verifies that the number of values in the actual group is equal to the given one.
080       * @param expected the expected number of values in the actual group.
081       * @return this assertion object.
082       * @throws AssertionError if the number of values of the actual group is not equal to the given one.
083       */
084      public final S hasSize(int expected) {
085        isNotNull();
086        int size = actualGroupSize();
087        if (size == expected) return myself;
088        failIfCustomMessageIsSet();
089        throw failure(format("expected size:<%s> but was:<%s> for <%s>", expected, size, actual));
090      }
091    
092      /**
093       * Returns the size of the actual group of values (array, collection, etc.)
094       * @return the size of the actual group of values.
095       */
096      protected abstract int actualGroupSize();
097    }