PropertyFileSettingsLoader { public void loadSettings(final Settings settings) { Validate.notNull(settings, "Settings can not be null"); try { final Properties fileContents = propertyTools.loadProperties(SETTINGS_FILE); setNickName(settings, fileContents); setOwnColor(settings, fileContents); setSysColor(settings, fileContents); setLogging(settings, fileContents); setBalloons(settings, fileContents); setSystemTray(settings, fileContents); setBrowser(settings, fileContents); setLookAndFeel(settings, fileContents); setNetworkInterface(settings, fileContents); setSound(settings, fileContents); setSmileys(settings, fileContents); } catch (final FileNotFoundException e) { LOG.log(Level.WARNING, "Could not find " + SETTINGS_FILE + ", using default settings."); } catch (final IOException e) { LOG.log(Level.SEVERE, e.toString(), e); } }  void loadSettings(final Settings settings); static final String SETTINGS_FILE; }
@Test public void loadSettingsShouldThrowExceptionIfSettingsIsNull() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Settings can not be null"); loader.loadSettings(null); }
@Test public void loadSettingsShouldHandleMissingPropertiesAndKeepDefaultValues() throws IOException { when(propertyTools.loadProperties(anyString())).thenReturn(new Properties()); loader.loadSettings(settings); SettingsTest.verifyDefaultValues(settings); }
@Test public void loadSettingsShouldLoadAllSettingsFromProperties() throws IOException { final Properties properties = new Properties(); properties.setProperty(NICK_NAME.getKey(), "Kenny"); properties.setProperty(OWN_COLOR.getKey(), "-1234"); properties.setProperty(SYS_COLOR.getKey(), "5678"); properties.setProperty(SOUND.getKey(), "false"); properties.setProperty(LOGGING.getKey(), "true"); properties.setProperty(SMILEYS.getKey(), "false"); properties.setProperty(BALLOONS.getKey(), "true"); properties.setProperty(SYSTEM_TRAY.getKey(), "false"); properties.setProperty(BROWSER.getKey(), "opera"); properties.setProperty(LOOK_AND_FEEL.getKey(), "sega"); properties.setProperty(NETWORK_INTERFACE.getKey(), "eth5"); assertEquals(11, properties.size()); when(propertyTools.loadProperties(anyString())).thenReturn(properties); loader.loadSettings(settings); assertEquals("Kenny", settings.getMe().getNick()); assertEquals(-1234, settings.getOwnColor()); assertEquals(5678, settings.getSysColor()); assertFalse(settings.isSound()); assertTrue(settings.isLogging()); assertFalse(settings.isSmileys()); assertTrue(settings.isBalloons()); assertFalse(settings.isSystemTray()); assertEquals("opera", settings.getBrowser()); assertEquals("sega", settings.getLookAndFeel()); assertEquals("eth5", settings.getNetworkInterface()); }
@Test public void loadSettingsShouldHandleBooleansWithStrangeValues() throws IOException { final Properties properties = new Properties(); properties.setProperty(SOUND.getKey(), "yeah"); properties.setProperty(LOGGING.getKey(), "nah"); properties.setProperty(SMILEYS.getKey(), "nope"); properties.setProperty(BALLOONS.getKey(), "yey"); properties.setProperty(SYSTEM_TRAY.getKey(), "wow"); when(propertyTools.loadProperties(anyString())).thenReturn(properties); loader.loadSettings(settings); assertFalse(settings.isSound()); assertFalse(settings.isLogging()); assertFalse(settings.isSmileys()); assertFalse(settings.isBalloons()); assertFalse(settings.isSystemTray()); }
@Test public void loadSettingsShouldIgnoreMissingNickName() throws IOException { when(propertyTools.loadProperties(anyString())).thenReturn(new Properties()); final String defaultNickName = settings.getMe().getNick(); assertTrue(Tools.isValidNick(defaultNickName)); loader.loadSettings(settings); assertEquals(defaultNickName, settings.getMe().getNick()); }
@Test public void loadSettingsShouldIgnoreInvalidNickName() throws IOException { final Properties properties = new Properties(); properties.setProperty(NICK_NAME.getKey(), "@Boss"); when(propertyTools.loadProperties(anyString())).thenReturn(properties); final String defaultNickName = settings.getMe().getNick(); assertTrue(Tools.isValidNick(defaultNickName)); loader.loadSettings(settings); assertEquals(defaultNickName, settings.getMe().getNick()); }
@Test public void loadSettingsShouldHandleFileNotFound() throws IOException { doThrow(new FileNotFoundException("No file")).when(propertyTools).loadProperties(anyString()); loader.loadSettings(settings); verify(propertyTools).loadProperties(Constants.APP_FOLDER + "kouchat.ini"); verify(log).log(Level.WARNING, "Could not find " + Constants.APP_FOLDER + "kouchat.ini, using default settings."); SettingsTest.verifyDefaultValues(settings); }
@Test public void loadSettingsShouldHandleIOException() throws IOException { final IOException ioException = new IOException("Unknown error"); doThrow(ioException).when(propertyTools).loadProperties(anyString()); loader.loadSettings(settings); verify(propertyTools).loadProperties(Constants.APP_FOLDER + "kouchat.ini"); verify(log).log(Level.SEVERE, "java.io.IOException: Unknown error", ioException); SettingsTest.verifyDefaultValues(settings); }