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