001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.hamcrest;
018    
019    import static org.hamcrest.MatcherAssert.assertThat;
020    import static org.hamcrest.Matchers.instanceOf;
021    
022    /**
023     * A set of useful assertions you can use when testing
024     *
025     * @version $Revision: 656656 $
026     */
027    public final class Assertions {
028        private Assertions() {
029            // Helper class
030        }
031    
032        /**
033         * Performs the assertion that the given value is an instance of the specified type
034         *
035         * @param value the value to be compared
036         * @param type  the type to assert
037         * @return the value cast as the type
038         * @throws AssertionError if the instance is not of the correct type
039         */
040        public static <T> T assertInstanceOf(Object value, Class<T> type) {
041            assertThat(value, instanceOf(type));
042            return type.cast(value);
043        }
044    
045        /**
046         * Performs the assertion that the given value is an instance of the specified type
047         *
048         * @param message the description of the value
049         * @param value   the value to be compared
050         * @param type    the type to assert
051         * @return the value cast as the type
052         * @throws AssertionError if the instance is not of the correct type
053         */
054        public static <T> T assertInstanceOf(String message, Object value, Class<T> type) {
055            assertThat(message, value, instanceOf(type));
056            return type.cast(value);
057        }
058    }