001/*
002 *  Copyright 2001-2013 Stephen Colebourne
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.joda.beans.test;
017
018import org.joda.beans.Bean;
019
020/**
021 * Assertion class to compare beans.
022 * <p>
023 * This class fulfils a similar role to other assertion libraries in testing code.
024 * It should generally be statically imported.
025 */
026public final class BeanAssert {
027
028    /**
029     * Restricted constructor.
030     */
031    private BeanAssert() {
032    }
033
034    //-----------------------------------------------------------------------
035    /**
036     * Asserts that two beans are equal, providing a better error message.
037     * 
038     * @param expected  the expected value, not null
039     * @param actual  the actual value, not null
040     */
041    public static void assertBeanEquals(final Bean expected, final Bean actual) {
042        assertBeanEquals(null, expected, actual);
043    }
044
045    /**
046     * Asserts that two beans are equal, providing a better error message.
047     * 
048     * @param message  the message to use in any error, null uses default message
049     * @param expected  the expected value, not null
050     * @param actual  the actual value, not null
051     */
052    public static void assertBeanEquals(final String message, final Bean expected, final Bean actual) {
053        if (expected == null) {
054            throw new AssertionError(message + ": Expected bean must not be null");
055        }
056        if (actual == null) {
057            throw new AssertionError(message + ": Actual bean must not be null");
058        }
059        if (expected.equals(actual) == false) {
060            throw new BeanComparisonError(message, 10, expected, actual);
061        }
062    }
063
064    //-----------------------------------------------------------------------
065    /**
066     * Asserts that two beans are equal, providing a better error message.
067     * 
068     * @param expected  the expected value, not null
069     * @param actual  the actual value, not null
070     */
071    public static void assertBeanEqualsFullDetail(final Bean expected, final Bean actual) {
072        assertBeanEqualsFullDetail(null, expected, actual);
073    }
074
075    /**
076     * Asserts that two beans are equal, providing a better error message, with
077     * an unlimited number of errors reported.
078     * 
079     * @param message  the message to use in any error, null uses default message
080     * @param expected  the expected value, not null
081     * @param actual  the actual value, not null
082     */
083    public static void assertBeanEqualsFullDetail(final String message, final Bean expected, final Bean actual) {
084        if (expected == null) {
085            throw new AssertionError(message + ": Expected bean must not be null");
086        }
087        if (actual == null) {
088            throw new AssertionError(message + ": Actual bean must not be null");
089        }
090        if (expected.equals(actual) == false) {
091            throw new BeanComparisonError(message, Integer.MAX_VALUE, expected, actual);
092        }
093    }
094
095}