| 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/>. |
| 16 | package net.sf.jomic.tools; |
| 17 | |
| 18 | import java.awt.Color; |
| 19 | import java.awt.Component; |
| 20 | import java.awt.Dimension; |
| 21 | import java.awt.Point; |
| 22 | import java.awt.Toolkit; |
| 23 | import java.beans.PropertyChangeEvent; |
| 24 | import java.beans.PropertyChangeListener; |
| 25 | import java.io.File; |
| 26 | |
| 27 | import javax.swing.JFrame; |
| 28 | import javax.swing.JLabel; |
| 29 | |
| 30 | import net.sf.wraplog.Logger; |
| 31 | |
| 32 | import junit.framework.TestCase; |
| 33 | |
| 34 | /** |
| 35 | * TestCase for BasicSettings. |
| 36 | * |
| 37 | * @author Thomas Aglassinger |
| 38 | */ |
| 39 | public 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 | } |