001 /* 002 * Created on Jul 15, 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.ShouldBe.shouldBe; 018 import static org.fest.assertions.error.ShouldHave.shouldHave; 019 import static org.fest.assertions.error.ShouldNotBe.shouldNotBe; 020 import static org.fest.assertions.error.ShouldNotHave.shouldNotHave; 021 022 import org.fest.assertions.core.*; 023 import org.fest.util.VisibleForTesting; 024 025 /** 026 * Verifies that a value satisfies a <code>{@link Condition}</code>. 027 * 028 * @author Alex Ruiz 029 */ 030 public class Conditions { 031 032 private static final Conditions INSTANCE = new Conditions(); 033 034 /** 035 * Returns the singleton instance of this class. 036 * @return the singleton instance of this class. 037 */ 038 public static Conditions instance() { 039 return INSTANCE; 040 } 041 042 @VisibleForTesting Failures failures = Failures.instance(); 043 044 @VisibleForTesting Conditions() {} 045 046 /** 047 * Asserts that the actual value satisfies the given <code>{@link Condition}</code>. 048 * @param <T> the type of the actual value and the type of values that given {@code Condition} takes. 049 * @param info contains information about the assertion. 050 * @param actual the actual value. 051 * @param condition the given {@code Condition}. 052 * @throws NullPointerException if the given {@code Condition} is {@code null}. 053 * @throws AssertionError if the actual value does not satisfy the given {@code Condition}. 054 */ 055 public <T> void assertIs(AssertionInfo info, T actual, Condition<T> condition) { 056 assertIsNotNull(condition); 057 if (condition.matches(actual)) return; 058 throw failures.failure(info, shouldBe(actual, condition)); 059 } 060 061 /** 062 * Asserts that the actual value does not satisfy the given <code>{@link Condition}</code>. 063 * @param <T> the type of the actual value and the type of values that given {@code Condition} takes. 064 * @param info contains information about the assertion. 065 * @param actual the actual value. 066 * @param condition the given {@code Condition}. 067 * @throws NullPointerException if the given {@code Condition} is {@code null}. 068 * @throws AssertionError if the actual value satisfies the given {@code Condition}. 069 */ 070 public <T> void assertIsNot(AssertionInfo info, T actual, Condition<T> condition) { 071 assertIsNotNull(condition); 072 if (!condition.matches(actual)) return; 073 throw failures.failure(info, shouldNotBe(actual, condition)); 074 } 075 076 /** 077 * Asserts that the actual value satisfies the given <code>{@link Condition}</code>. 078 * @param <T> the type of the actual value and the type of values that given {@code Condition} takes. 079 * @param info contains information about the assertion. 080 * @param actual the actual value. 081 * @param condition the given {@code Condition}. 082 * @throws NullPointerException if the given {@code Condition} is {@code null}. 083 * @throws AssertionError if the actual value does not satisfy the given {@code Condition}. 084 */ 085 public <T> void assertHas(AssertionInfo info, T actual, Condition<T> condition) { 086 assertIsNotNull(condition); 087 if (condition.matches(actual)) return; 088 throw failures.failure(info, shouldHave(actual, condition)); 089 } 090 091 /** 092 * Asserts that the actual value does not satisfy the given <code>{@link Condition}</code>. 093 * @param <T> the type of the actual value and the type of values that given {@code Condition} takes. 094 * @param info contains information about the assertion. 095 * @param actual the actual value. 096 * @param condition the given {@code Condition}. 097 * @throws NullPointerException if the given {@code Condition} is {@code null}. 098 * @throws AssertionError if the actual value satisfies the given {@code Condition}. 099 */ 100 public <T> void assertDoesNotHave(AssertionInfo info, T actual, Condition<T> condition) { 101 assertIsNotNull(condition); 102 if (!condition.matches(actual)) return; 103 throw failures.failure(info, shouldNotHave(actual, condition)); 104 } 105 106 /** 107 * Asserts the the given <code>{@link Condition}</code> is not null. 108 * @param condition the given {@code Condition}. 109 * @throws NullPointerException if the given {@code Condition} is {@code null}. 110 */ 111 public void assertIsNotNull(Condition<?> condition) { 112 if (condition == null) throw new NullPointerException("The condition to evaluate should not be null"); 113 } 114 }