001 /*
002 * Created on May 2, 2008
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with 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
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 *
014 * Copyright @2008-2010 the original author or authors.
015 */
016 package org.fest.swing.fixture;
017
018 import static org.fest.assertions.Assertions.assertThat;
019 import static org.fest.swing.util.Colors.colorFromHexString;
020
021 import java.awt.Color;
022
023 import org.fest.assertions.BasicDescription;
024 import org.fest.assertions.Description;
025
026 /**
027 * Understands state verification of <code>{@link Color}</code>s.
028 *
029 * @author Alex Ruiz
030 */
031 public class ColorFixture {
032
033 private final Color target;
034 private final Description description;
035
036 /**
037 * Creates a new </code>{@link ColorFixture}</code>.
038 * @param target the color to manage.
039 * @throws NullPointerException if <code>target</code> is <code>null</code>.
040 */
041 public ColorFixture(Color target) {
042 this(target, (Description)null);
043 }
044
045 /**
046 * Creates a new </code>{@link ColorFixture}</code>.
047 * @param target the color to manage.
048 * @param description this fixture's description.
049 * @throws NullPointerException if <code>target</code> is <code>null</code>.
050 */
051 public ColorFixture(Color target, String description) {
052 this(target, new BasicDescription(description));
053 }
054
055 /**
056 * Creates a new </code>{@link ColorFixture}</code>.
057 * @param target the color to manage.
058 * @param description this fixture's description.
059 * @throws NullPointerException if <code>target</code> is <code>null</code>.
060 */
061 public ColorFixture(Color target, Description description) {
062 if (target == null) throw new NullPointerException("The given color should not be null");
063 this.target = target;
064 this.description = description;
065 }
066
067 /**
068 * Verifies that this fixture's <code>Color</code> is equal to the given color represented by the given hexadecimal
069 * value (e.g. "82A9FF".)
070 * @param hexValue the value representing the color to compare to.
071 * @return this fixture.
072 * @throws NullPointerException if the hexadecimal code is <code>null</code>.
073 * @throws IllegalArgumentException if the hexadecimal code is empty.
074 * @throws NumberFormatException if the hexadecimal code is empty.
075 * @throws AssertionError if this fixture's <code>Color</code> is not equal to the given one.
076 */
077 public ColorFixture requireEqualTo(String hexValue) {
078 return requireEqualTo(colorFromHexString(hexValue));
079 }
080
081 /**
082 * Verifies that this fixture's <code>Color</code> is equal to the given one.
083 * @param color the given <code>Color</code> to compare to.
084 * @return this fixture.
085 * @throws AssertionError if this fixture's <code>Color</code> is not equal to the given one.
086 */
087 public ColorFixture requireEqualTo(Color color) {
088 assertThat(target).as(description).isEqualTo(color);
089 return this;
090 }
091
092 /**
093 * Verifies that this fixture's <code>Color</code> is not equal to the given color represented by the given
094 * hexadecimal value (e.g. "82A9FF".)
095 * @param hexValue the value representing the color to compare to.
096 * @return this fixture.
097 * @throws NullPointerException if the hexadecimal code is <code>null</code>.
098 * @throws IllegalArgumentException if the hexadecimal code is empty.
099 * @throws NumberFormatException if the hexadecimal code is empty.
100 * @throws AssertionError if this fixture's <code>Color</code> is equal to the given one.
101 */
102 public ColorFixture requireNotEqualTo(String hexValue) {
103 return requireNotEqualTo(colorFromHexString(hexValue));
104 }
105
106 /**
107 * Verifies that this fixture's <code>Color</code> is not equal to the given one.
108 * @param color the given <code>Color</code> to compare to.
109 * @return this fixture.
110 * @throws AssertionError if this fixture's <code>Color</code> is equal to the given one.
111 */
112 public ColorFixture requireNotEqualTo(Color color) {
113 assertThat(target).as(description).isNotEqualTo(color);
114 return this;
115 }
116
117 /**
118 * Returns this fixture's color.
119 * @return this fixture's color.
120 */
121 public Color target() { return target; }
122
123 /**
124 * Returns this fixture's description.
125 * @return this fixture's description.
126 */
127 public final String description() { return description != null ? description.value() : null; }
128 }