001    /*
002     * Created on Apr 16, 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.util.Strings.concat;
020    import static org.fest.util.Strings.isEmpty;
021    
022    import java.awt.Font;
023    
024    import org.fest.assertions.BasicDescription;
025    import org.fest.assertions.Description;
026    
027    /**
028     * Understands state verification of <code>{@link Font}</code>s.
029     *
030     * @author Yvonne Wang
031     * @author Alex Ruiz
032     */
033    public class FontFixture {
034    
035      private static final String PROPERTY_SEPARATOR = " - ";
036    
037      private static final String BOLD_PROPERTY = "bold";
038      private static final String FAMILY_PROPERTY = "family";
039      private static final String ITALIC_PROPERTY = "italic";
040      private static final String NAME_PROPERTY = "name";
041      private static final String PLAIN_PROPERTY = "plain";
042      private static final String SIZE_PROPERTY = "size";
043    
044      private final Font target;
045      private final Description description;
046    
047      /**
048       * Creates a new </code>{@link FontFixture}</code>.
049       * @param target the font to manage.
050       * @throws NullPointerException if <code>target</code> is <code>null</code>.
051       */
052      public FontFixture(Font target) {
053        this(target, (Description)null);
054      }
055    
056      /**
057       * Creates a new </code>{@link FontFixture}</code>.
058       * @param target the font to manage.
059       * @param description this fixture's description.
060       * @throws NullPointerException if <code>target</code> is <code>null</code>.
061       */
062      public FontFixture(Font target, String description) {
063        this(target, new BasicDescription(description));
064      }
065    
066      /**
067       * Creates a new </code>{@link FontFixture}</code>.
068       * @param target the font to manage.
069       * @param description this fixture's description.
070       * @throws NullPointerException if <code>target</code> is <code>null</code>.
071       */
072      public FontFixture(Font target, Description description) {
073        if (target == null) throw new NullPointerException("The given font should not be null");
074        this.target = target;
075        this.description = description;
076      }
077    
078      /**
079       * Verifies that the family name of this fixture's font is equal to the given one.
080       * @param family the expected family name.
081       * @return this assertion object.
082       * @throws AssertionError if the family name of this fixture's font is not equal to the given one.
083       * @see Font#getFamily()
084       */
085      public FontFixture requireFamily(String family) {
086        assertThat(target.getFamily()).as(property(FAMILY_PROPERTY)).isEqualTo(family);
087        return this;
088      }
089    
090      /**
091       * Verifies that the logical name of this fixture's font is equal to the given one.
092       * @param name the expected logical name.
093       * @return this assertion object.
094       * @throws AssertionError if the logical name of this fixture's font is not equal to the given one.
095       * @see Font#getName()
096       */
097      public FontFixture requireName(String name) {
098        assertThat(target.getName()).as(property(NAME_PROPERTY)).isEqualTo(name);
099        return this;
100      }
101    
102      /**
103       * Verifies that the point size of this fixture's font is equal to the given one.
104       * @param size the expected point size.
105       * @return this assertion object.
106       * @throws AssertionError if the point size of this fixture's font is not equal to the given one.
107       * @see Font#getSize()
108       */
109      public FontFixture requireSize(int size) {
110        assertThat(target.getSize()).as(property(SIZE_PROPERTY)).isEqualTo(size);
111        return this;
112      }
113    
114      /**
115       * Verifies that this fixture's font is bold.
116       * @return this assertion object.
117       * @throws AssertionError if this fixture's font is not bold.
118       * @see Font#isBold()
119       */
120      public FontFixture requireBold() {
121        return requireBold(true);
122      }
123    
124      /**
125       * Verifies that this fixture's font is not bold.
126       * @return this assertion object.
127       * @throws AssertionError if this fixture's font is bold.
128       * @see Font#isBold()
129       */
130      public FontFixture requireNotBold() {
131        return requireBold(false);
132      }
133    
134      private FontFixture requireBold(boolean bold) {
135        assertThat(target.isBold()).as(property(BOLD_PROPERTY)).isEqualTo(bold);
136        return this;
137      }
138    
139      /**
140       * Verifies that this fixture's font is italic.
141       * @return this assertion object.
142       * @throws AssertionError if this fixture's font is not italic.
143       * @see Font#isItalic()
144       */
145      public FontFixture requireItalic() {
146        return requireItalic(true);
147      }
148    
149      /**
150       * Verifies that this fixture's font is not italic.
151       * @return this assertion object.
152       * @throws AssertionError if this fixture's font is italic.
153       * @see Font#isItalic()
154       */
155      public FontFixture requireNotItalic() {
156        return requireItalic(false);
157      }
158    
159      private FontFixture requireItalic(boolean italic) {
160        assertThat(target.isItalic()).as(property(ITALIC_PROPERTY)).isEqualTo(italic);
161        return this;
162      }
163    
164      /**
165       * Verifies that this fixture's font is plain.
166       * @return this assertion object.
167       * @throws AssertionError if this fixture's font is not plain.
168       * @see Font#isPlain()
169       */
170      public FontFixture requirePlain() {
171        return requirePlain(true);
172      }
173    
174      /**
175       * Verifies that this fixture's font is not plain.
176       * @return this assertion object.
177       * @throws AssertionError if this fixture's font is plain.
178       * @see Font#isPlain()
179       */
180      public FontFixture requireNotPlain() {
181        return requirePlain(false);
182      }
183    
184      private FontFixture requirePlain(boolean plain) {
185        assertThat(target.isBold()).as(property(PLAIN_PROPERTY)).isEqualTo(plain);
186        return this;
187      }
188    
189      private String property(String s) {
190        if (!isEmpty(description())) return concat(description.value(), PROPERTY_SEPARATOR, s);
191        return s;
192      }
193    
194      /**
195       * Returns this fixture's font.
196       * @return this fixture's font.
197       */
198      public Font target() { return target; }
199    
200      /**
201       * Returns this fixture's description.
202       * @return this fixture's description.
203       */
204      public final String description() { return description != null ? description.value() : null; }
205    }