001 /*
002 * Created on Oct 22, 2010
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 @2010-2011 the original author or authors.
014 */
015 package org.fest.assertions.internal;
016
017 import static org.fest.assertions.error.ShouldNotBeEqual.shouldNotBeEqual;
018 import static org.fest.assertions.error.ShouldBeEqual.shouldBeEqual;
019
020 import org.fest.assertions.core.AssertionInfo;
021 import org.fest.util.VisibleForTesting;
022
023 /**
024 * Reusable assertions for <code>{@link Boolean}</code>s.
025 *
026 * @author Alex Ruiz
027 */
028 public class Booleans {
029
030 private static final Booleans INSTANCE = new Booleans();
031
032 /**
033 * Returns the singleton instance of this class.
034 * @return the singleton instance of this class.
035 */
036 public static Booleans instance() {
037 return INSTANCE;
038 }
039
040 @VisibleForTesting Failures failures = Failures.instance();
041
042 @VisibleForTesting Booleans() {}
043
044 /**
045 * Asserts that two booleans are equal.
046 * @param info contains information about the assertion.
047 * @param actual the actual value.
048 * @param expected the expected value.
049 * @throws AssertionError if the actual value is {@code null}.
050 * @throws AssertionError if the actual value is not equal to the expected one. This method will throw a
051 * {@code org.junit.ComparisonFailure} instead if JUnit is in the classpath and the expected and actual values are not
052 * equal.
053 */
054 public void assertEqual(AssertionInfo info, Boolean actual, boolean expected) {
055 assertNotNull(info, actual);
056 if (actual.booleanValue() == expected) return;
057 throw failures.failure(info, shouldBeEqual(actual, expected));
058 }
059
060 /**
061 * Asserts that two longs are not equal.
062 * @param info contains information about the assertion.
063 * @param actual the actual value.
064 * @param other the value to compare the actual value to.
065 * @throws AssertionError if the actual value is {@code null}.
066 * @throws AssertionError if the actual value is equal to the other one.
067 */
068 public void assertNotEqual(AssertionInfo info, Boolean actual, boolean other) {
069 assertNotNull(info, actual);
070 if (actual.booleanValue() != other) return;
071 throw failures.failure(info, shouldNotBeEqual(actual, other));
072 }
073
074 private static void assertNotNull(AssertionInfo info, Boolean actual) {
075 Objects.instance().assertNotNull(info, actual);
076 }
077 }