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     */
016    package org.joda.beans.test;
017    
018    import 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     */
026    public 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 expected  the expected value, not null
049         * @param actual  the actual value, not null
050         */
051        public static void assertBeanEquals(final String message, final Bean expected, final Bean actual) {
052            if (expected == null) {
053                throw new AssertionError(message + ": Expected bean must not be null");
054            }
055            if (actual == null) {
056                throw new AssertionError(message + ": Actual bean must not be null");
057            }
058            if (expected.equals(actual) == false) {
059                throw new BeanComparisonError(message, 10, expected, actual);
060            }
061        }
062    
063        //-----------------------------------------------------------------------
064        /**
065         * Asserts that two beans are equal, providing a better error message.
066         * 
067         * @param expected  the expected value, not null
068         * @param actual  the actual value, not null
069         */
070        public static void assertBeanEqualsFullDetail(final Bean expected, final Bean actual) {
071            assertBeanEqualsFullDetail(null, expected, actual);
072        }
073    
074        /**
075         * Asserts that two beans are equal, providing a better error message, with
076         * an unlimited number of errors reported.
077         * 
078         * @param expected  the expected value, not null
079         * @param actual  the actual value, not null
080         */
081        public static void assertBeanEqualsFullDetail(final String message, final Bean expected, final Bean actual) {
082            if (expected == null) {
083                throw new AssertionError(message + ": Expected bean must not be null");
084            }
085            if (actual == null) {
086                throw new AssertionError(message + ": Actual bean must not be null");
087            }
088            if (expected.equals(actual) == false) {
089                throw new BeanComparisonError(message, Integer.MAX_VALUE, expected, actual);
090            }
091        }
092    
093    }