EMMA Coverage Report (generated Sun Apr 20 22:38:01 CEST 2008)
[all classes][net.sf.jomic.tools]

COVERAGE SUMMARY FOR SOURCE FILE [BasicSettingsTest.java]

nameclass, %method, %block, %line, %
BasicSettingsTest.java100% (1/1)100% (21/21)84%  (716/852)87%  (138.1/158)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class BasicSettingsTest100% (1/1)100% (21/21)84%  (716/852)87%  (138.1/158)
testNoGetPropertyWithDefault (): void 100% (1/1)23%  (7/31)50%  (3/6)
testChoiceProperty (): void 100% (1/1)45%  (29/64)45%  (5/11)
testLimitedIntProperty (): void 100% (1/1)58%  (57/98)50%  (9/18)
setAndApply (String, Component): void 100% (1/1)70%  (19/27)80%  (4/5)
testColor (): void 100% (1/1)79%  (60/76)96%  (7.7/8)
<static initializer> 100% (1/1)83%  (15/18)92%  (1.8/2)
testChangeListener (): void 100% (1/1)90%  (36/40)94%  (7.6/8)
testComponentArea (): void 100% (1/1)96%  (127/132)100% (25/25)
BasicSettingsTest (): void 100% (1/1)100% (3/3)100% (1/1)
assertWithinVisibleArea (Component): void 100% (1/1)100% (52/52)100% (10/10)
propertyChange (PropertyChangeEvent): void 100% (1/1)100% (7/7)100% (2/2)
setUp (): void 100% (1/1)100% (74/74)100% (14/14)
tearDown (): void 100% (1/1)100% (6/6)100% (3/3)
testBoolean (): void 100% (1/1)100% (35/35)100% (7/7)
testBooleanFallback (): void 100% (1/1)100% (13/13)100% (3/3)
testColorFallback (): void 100% (1/1)100% (13/13)100% (3/3)
testFile (): void 100% (1/1)100% (31/31)100% (6/6)
testInt (): void 100% (1/1)100% (31/31)100% (7/7)
testIntFallback (): void 100% (1/1)100% (13/13)100% (3/3)
testRemove (): void 100% (1/1)100% (51/51)100% (10/10)
testSystemPropertyFallback (): void 100% (1/1)100% (37/37)100% (7/7)

1// Jomic - a viewer for comic book archives.
2// Copyright (C) 2004-2008 Thomas Aglassinger
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program.  If not, see <http://www.gnu.org/licenses/>.
16package net.sf.jomic.tools;
17 
18import java.awt.Color;
19import java.awt.Component;
20import java.awt.Dimension;
21import java.awt.Point;
22import java.awt.Toolkit;
23import java.beans.PropertyChangeEvent;
24import java.beans.PropertyChangeListener;
25import java.io.File;
26 
27import javax.swing.JFrame;
28import javax.swing.JLabel;
29 
30import net.sf.wraplog.Logger;
31 
32import junit.framework.TestCase;
33 
34/**
35 *  TestCase for BasicSettings.
36 *
37 * @author    Thomas Aglassinger
38 */
39public class BasicSettingsTest extends TestCase implements PropertyChangeListener
40{
41    private static final String AREA_KEY = "area";
42    private static final String BOOLEAN_KEY = "boolean";
43    private static final String BOOLEAN_WITH_NO_DEFAULT_KEY = "booleanWithNoDefault";
44    private static final String BROKEN_CHOICE = "mustard";
45    private static final String CHOICE_APPLE = "apple";
46    private static final String CHOICE_KEY = "choice";
47    private static final String CHOICE_ORANGE = "orange";
48    private static final String COLOR_KEY = "color";
49    private static final String COLOR_WITH_NO_DEFAULT_KEY = "colorWithNoDefault";
50    private static final boolean DEFAULT_BOOLEAN = true;
51    private static final String DEFAULT_CHOICE = CHOICE_APPLE;
52    private static final String DEFAULT_COLOR_TEXT = "#123456";
53    private static final Color DEFAULT_COLOR = Color.decode(DEFAULT_COLOR_TEXT);
54    private static final int DEFAULT_INT = 17;
55    private static final String DEFAULT_STRING = "some text";
56    private static final String FILE_KEY = "file";
57    private static final String FILE_WITH_NO_DEFAULT_KEY = "fileWithNoDefault";
58    private static final String INT_KEY = "int";
59    private static final String INT_WITH_NO_DEFAULT_KEY = "intWithNoDefault";
60    private static final int MAX_INT = 23;
61    private static final int MIN_INT = -3;
62    private static final int SOME_OTHER_INT_VALUE = 23;
63    private static final String STRING_KEY = "string";
64    private static final String SYSTEM_KEY = "system";
65    private static final String SYSTEM_VALUE = "system.test";
66    private BasicSettings basics;
67    private int changeCount;
68    private File defaultFile;
69    private Logger logger;
70    private String[] possibleChoices;
71    private StringTools stringTools;
72    private TestTools testTools;
73 
74    /*
75     *  @see TestCase#setUp()
76     */
77    protected void setUp()
78        throws Exception {
79        super.setUp();
80        testTools = TestTools.instance();
81        stringTools = StringTools.instance();
82        defaultFile = new File(System.getProperty("user.home"));
83        logger = Logger.getLogger(BasicSettings.class);
84        basics = new BasicSettings();
85        basics.setDefault(COLOR_KEY, DEFAULT_COLOR_TEXT);
86        basics.setDefault(INT_KEY, DEFAULT_INT);
87        basics.setDefault(BOOLEAN_KEY, DEFAULT_BOOLEAN);
88        basics.setDefault(CHOICE_KEY, DEFAULT_CHOICE);
89        basics.setDefault(FILE_KEY, defaultFile.getAbsolutePath());
90        possibleChoices = new String[]{CHOICE_APPLE, CHOICE_ORANGE};
91        changeCount = 0;
92    }
93 
94    private void setAndApply(String propertyName, Component component) {
95        assert propertyName != null;
96        assert component != null;
97 
98        basics.setComponentAreaProperty(propertyName, component);
99        basics.applyComponentAreaProperty(propertyName, component);
100    }
101 
102    public void propertyChange(PropertyChangeEvent event) {
103        changeCount += 1;
104    }
105 
106    public void testBoolean() {
107        assertEquals(DEFAULT_BOOLEAN, basics.getBooleanProperty(BOOLEAN_KEY));
108        basics.setBooleanProperty(BOOLEAN_KEY, false);
109        assertEquals(false, basics.getBooleanProperty(BOOLEAN_KEY));
110        basics.setBooleanProperty(BOOLEAN_KEY, true);
111        assertEquals(true, basics.getBooleanProperty(BOOLEAN_KEY));
112 
113        assertEquals(false, basics.getBooleanProperty(BOOLEAN_WITH_NO_DEFAULT_KEY));
114    }
115 
116    public void testBooleanFallback() {
117        basics.setProperty(BOOLEAN_KEY, DEFAULT_STRING);
118        assertEquals(false, basics.getBooleanProperty(BOOLEAN_KEY));
119    }
120 
121    public void testChangeListener() {
122        assert changeCount == 0;
123        basics.addPropertyChangeListener(this);
124        basics.setProperty(STRING_KEY, DEFAULT_STRING);
125        assertEquals("changeCount", 1, changeCount);
126        basics.removePropertyChangeListener(this);
127        basics.setProperty(STRING_KEY, "don't listen");
128        assertEquals("changeCount", 1, changeCount);
129    }
130 
131    public void testChoiceProperty() {
132        assertEquals(CHOICE_KEY, DEFAULT_CHOICE, basics.getChoiceProperty(CHOICE_KEY, possibleChoices));
133        basics.setChoiceProperty(CHOICE_KEY, CHOICE_ORANGE, possibleChoices);
134        assertEquals(CHOICE_KEY, CHOICE_ORANGE, basics.getChoiceProperty(CHOICE_KEY, possibleChoices));
135        try {
136            basics.setChoiceProperty(CHOICE_KEY, BROKEN_CHOICE, possibleChoices);
137            fail("attempt to set invalid choice must cause an error");
138        } catch (IllegalArgumentException expectedError) {
139            logger.info("caught expected error: " + expectedError.getMessage());
140        }
141 
142        // Set broken choice by-passing setChoiceProperty().
143        basics.setProperty(CHOICE_KEY, BROKEN_CHOICE);
144        assertEquals(CHOICE_KEY, DEFAULT_CHOICE, basics.getChoiceProperty(CHOICE_KEY, possibleChoices));
145    }
146 
147    public void testColor() {
148        assertEquals(DEFAULT_COLOR, basics.getColorProperty(COLOR_KEY));
149 
150        Color oldValue = basics.getColorProperty(COLOR_KEY);
151        Color newValue = Color.decode("#" + DEFAULT_COLOR_TEXT.substring(3, 5)
152                + DEFAULT_COLOR_TEXT.substring(5, 7) + DEFAULT_COLOR_TEXT.substring(1, 3));
153 
154        assert !oldValue.equals(newValue) : "oldValue=" + oldValue + " must differ from newValue=" + newValue;
155        basics.setColorProperty(COLOR_KEY, newValue);
156        assertEquals(newValue, basics.getColorProperty(COLOR_KEY));
157 
158        assertEquals("default color",
159                Color.decode(BasicSettings.DEFAULT_COLOR),
160                basics.getColorProperty(COLOR_WITH_NO_DEFAULT_KEY));
161    }
162 
163    public void testColorFallback() {
164        basics.setProperty(COLOR_KEY, "hugo");
165        assertEquals(DEFAULT_COLOR, basics.getColorProperty(COLOR_KEY));
166    }
167 
168    public void testComponentArea() {
169        JFrame component = new JFrame();
170 
171        component.getContentPane().add(new JLabel(BasicSettingsTest.class.getName()));
172        component.pack();
173        basics.setComponentAreaProperty(AREA_KEY, component);
174 
175        Point location = component.getLocation();
176        Dimension size = component.getSize();
177        int[] area = new int[]{location.x, location.y, size.width, size.height};
178 
179        assertEquals(stringTools.arrayToString(area), basics.getProperty(AREA_KEY));
180        assertWithinVisibleArea(component);
181 
182        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
183        int screenWidth = screenSize.width;
184        int screenHeight = screenSize.height;
185 
186        component.setLocation(screenWidth, component.getLocation().y);
187        setAndApply(AREA_KEY, component);
188        assertWithinVisibleArea(component);
189 
190        component.setLocation(component.getLocation().x, screenHeight);
191        setAndApply(AREA_KEY, component);
192        assertWithinVisibleArea(component);
193 
194        component.setLocation(-screenWidth, component.getLocation().y);
195        setAndApply(AREA_KEY, component);
196        assertWithinVisibleArea(component);
197 
198        component.setLocation(component.getLocation().x, -screenHeight);
199        setAndApply(AREA_KEY, component);
200        assertWithinVisibleArea(component);
201    }
202 
203    public void testFile() {
204        assertNull(basics.getFileProperty(FILE_WITH_NO_DEFAULT_KEY));
205        assertEquals(defaultFile, basics.getFileProperty(FILE_KEY));
206 
207        File newFile = new File(defaultFile, "hugo.txt");
208 
209        basics.setFileProperty(FILE_KEY, newFile);
210        assertEquals(newFile, basics.getFileProperty(FILE_KEY));
211    }
212 
213    public void testInt() {
214        int oldValue = basics.getIntProperty(INT_KEY);
215 
216        assertEquals(DEFAULT_INT, oldValue);
217 
218        int newValue = oldValue + 1;
219 
220        basics.setIntProperty(INT_KEY, newValue);
221        assertEquals(newValue, basics.getIntProperty(INT_KEY));
222        assertEquals("int default", 0, basics.getIntProperty(INT_WITH_NO_DEFAULT_KEY));
223    }
224 
225    public void testIntFallback() {
226        basics.setProperty(INT_KEY, DEFAULT_STRING);
227        assertEquals(DEFAULT_INT, basics.getIntProperty(INT_KEY));
228    }
229 
230    public void testLimitedIntProperty() {
231        basics.setLimitedIntProperty(INT_KEY, DEFAULT_INT, MIN_INT, MAX_INT);
232        basics.setLimitedIntProperty(INT_KEY, MIN_INT, MIN_INT, MAX_INT);
233        basics.setLimitedIntProperty(INT_KEY, MAX_INT, MIN_INT, MAX_INT);
234        try {
235            basics.setLimitedIntProperty(INT_KEY, MIN_INT - 1, MIN_INT, MAX_INT);
236            fail("attempt to set too small value must cause an error");
237        } catch (IllegalArgumentException expectedError) {
238            logger.info("caught expected error: " + expectedError.getMessage());
239        }
240        try {
241            basics.setLimitedIntProperty(INT_KEY, MAX_INT + 1, MIN_INT, MAX_INT);
242            fail("attempt to set too big value must cause an error");
243        } catch (IllegalArgumentException expectedError) {
244            logger.info("caught expected error: " + expectedError.getMessage());
245        }
246 
247        basics.setLimitedIntProperty(INT_KEY, DEFAULT_INT, MIN_INT, MAX_INT);
248        assertEquals(INT_KEY, DEFAULT_INT, basics.getLimitedIntProperty(INT_KEY, MIN_INT, MAX_INT));
249 
250        // Set value out of range by-passing setLimitIntProperty().
251        basics.setIntProperty(INT_KEY, MIN_INT - 1);
252        assertEquals(INT_KEY, DEFAULT_INT, basics.getLimitedIntProperty(INT_KEY, MIN_INT, MAX_INT));
253    }
254 
255    public void testNoGetPropertyWithDefault() {
256        try {
257            basics.getProperty(STRING_KEY, DEFAULT_STRING);
258            fail("getProperty() must throw " + IllegalStateException.class.getName());
259        } catch (IllegalStateException expectedError) {
260            logger.debug("as expected, cannot get property with default", expectedError);
261        }
262    }
263 
264    public void testRemove() {
265        basics.setIntProperty(INT_KEY, SOME_OTHER_INT_VALUE);
266        assertEquals(SOME_OTHER_INT_VALUE, basics.getIntProperty(INT_KEY));
267        basics.remove(INT_KEY);
268        assertEquals("value of property " + INT_KEY + " after remove", DEFAULT_INT, basics.getIntProperty(INT_KEY));
269        basics.setIntProperty(INT_WITH_NO_DEFAULT_KEY, DEFAULT_INT);
270        assertEquals(DEFAULT_INT, basics.getIntProperty(INT_WITH_NO_DEFAULT_KEY));
271        basics.remove(INT_WITH_NO_DEFAULT_KEY);
272        assertNull("value of property " + INT_WITH_NO_DEFAULT_KEY + " after remove",
273                basics.getProperty(INT_WITH_NO_DEFAULT_KEY));
274        assertEquals("value of property " + INT_WITH_NO_DEFAULT_KEY + " after remove",
275                0, basics.getIntProperty(INT_WITH_NO_DEFAULT_KEY));
276    }
277 
278    public void testSystemPropertyFallback() {
279        assertNull(basics.getProperty(SYSTEM_KEY));
280        basics.setSystemPropertyPrefix("blah.");
281        basics.setProperty(SYSTEM_KEY, DEFAULT_STRING);
282        assertEquals("value from local property", DEFAULT_STRING, basics.getProperty(SYSTEM_KEY));
283        System.setProperty(basics.getSystemPropertyName(SYSTEM_KEY), SYSTEM_VALUE);
284        assertEquals("value from system property", SYSTEM_VALUE, basics.getProperty(SYSTEM_KEY));
285    }
286 
287    /*
288     *  @see TestCase#tearDown()
289     */
290    protected void tearDown()
291        throws Exception {
292        basics = null;
293        super.tearDown();
294    }
295 
296    private void assertWithinVisibleArea(Component component) {
297        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
298        int screenWidth = screenSize.width;
299        int screenHeight = screenSize.height;
300        Point location = component.getLocation();
301        Dimension size = component.getSize();
302 
303        testTools.assertGreaterOrEqual(location.x, 0);
304        testTools.assertGreaterOrEqual(location.y, 0);
305        testTools.assertLessOrEqual(location.x + size.width, screenWidth);
306        testTools.assertLessOrEqual(location.y + size.height, screenHeight);
307    }
308}

[all classes][net.sf.jomic.tools]
EMMA 2.0.4217 (C) Vladimir Roubtsov