PropertyFileSettingsSaver implements SettingsSaver { @Override public void saveSettings() { final Properties properties = new Properties(); final User me = settings.getMe(); properties.put(NICK_NAME.getKey(), Tools.emptyIfNull(me.getNick())); properties.put(OWN_COLOR.getKey(), String.valueOf(settings.getOwnColor())); properties.put(SYS_COLOR.getKey(), String.valueOf(settings.getSysColor())); properties.put(LOGGING.getKey(), String.valueOf(settings.isLogging())); properties.put(SOUND.getKey(), String.valueOf(settings.isSound())); properties.put(BROWSER.getKey(), Tools.emptyIfNull(settings.getBrowser())); properties.put(SMILEYS.getKey(), String.valueOf(settings.isSmileys())); properties.put(LOOK_AND_FEEL.getKey(), Tools.emptyIfNull(settings.getLookAndFeel())); properties.put(BALLOONS.getKey(), String.valueOf(settings.isBalloons())); properties.put(SYSTEM_TRAY.getKey(), String.valueOf(settings.isSystemTray())); properties.put(NETWORK_INTERFACE.getKey(), Tools.emptyIfNull(settings.getNetworkInterface())); try { ioTools.createFolder(Constants.APP_FOLDER); propertyTools.saveProperties(PropertyFileSettingsLoader.SETTINGS_FILE, properties, coreMessages.getMessage("core.settings.file.comment", Constants.APP_NAME)); } catch (final IOException e) { LOG.log(Level.SEVERE, "Failed to save settings", e); errorHandler.showError(coreMessages.getMessage("core.settings.errorPopup.saveFailed", e)); } } PropertyFileSettingsSaver(final Settings settings, final CoreMessages coreMessages,
                                     final ErrorHandler errorHandler); @Override void saveSettings();  }
@Test public void saveSettingsShouldCreateKouChatFolderBeforeSaving() throws IOException { settingsSaver.saveSettings(); final InOrder inOrder = inOrder(ioTools, propertyTools); inOrder.verify(ioTools).createFolder(Constants.APP_FOLDER); inOrder.verify(propertyTools).saveProperties(eq(Constants.APP_FOLDER + "kouchat.ini"), any(Properties.class), eq("KouChat Settings")); }
@Test public void saveSettingsShouldShowErrorOnException() throws IOException { doThrow(new IOException("Don't save")).when(propertyTools).saveProperties( anyString(), any(Properties.class), anyString()); settingsSaver.saveSettings(); verify(errorHandler).showError("Settings could not be saved:\njava.io.IOException: Don't save"); }
@Test public void saveSettingsShouldNotShowErrorWhenOK() throws IOException { settingsSaver.saveSettings(); verify(errorHandler, never()).showError(anyString()); }
@Test public void saveSettingsShouldConvertAllValuesToStringsToAvoidClassCastException() throws IOException { settings.getMe().setNick("Linda"); settings.setOwnColor(100); settings.setSysColor(-200); settings.setSound(false); settings.setLogging(true); settings.setSmileys(false); settings.setBalloons(true); settings.setSystemTray(false); settings.setBrowser("firefox"); settings.setLookAndFeel("starwars"); settings.setNetworkInterface("wlan2"); settingsSaver.saveSettings(); final ArgumentCaptor<Properties> propertiesCaptor = ArgumentCaptor.forClass(Properties.class); verify(propertyTools).saveProperties(anyString(), propertiesCaptor.capture(), anyString()); final Properties properties = propertiesCaptor.getValue(); assertEquals(11, properties.size()); assertEquals("Linda", properties.get(NICK_NAME.getKey())); assertEquals("100", properties.get(OWN_COLOR.getKey())); assertEquals("-200", properties.get(SYS_COLOR.getKey())); assertEquals("false", properties.get(SOUND.getKey())); assertEquals("true", properties.get(LOGGING.getKey())); assertEquals("false", properties.get(SMILEYS.getKey())); assertEquals("true", properties.get(BALLOONS.getKey())); assertEquals("false", properties.get(SYSTEM_TRAY.getKey())); assertEquals("firefox", properties.get(BROWSER.getKey())); assertEquals("starwars", properties.get(LOOK_AND_FEEL.getKey())); assertEquals("wlan2", properties.get(NETWORK_INTERFACE.getKey())); }
@Test public void saveSettingsShouldHandleNullStringsToAvoidNullPointerException() throws IOException { settings.getMe().setNick(null); settings.setBrowser(null); settings.setLookAndFeel(null); settings.setNetworkInterface(null); settingsSaver.saveSettings(); final ArgumentCaptor<Properties> propertiesCaptor = ArgumentCaptor.forClass(Properties.class); verify(propertyTools).saveProperties(anyString(), propertiesCaptor.capture(), anyString()); final Properties properties = propertiesCaptor.getValue(); assertEquals(11, properties.size()); assertEquals("", properties.get(NICK_NAME.getKey())); assertEquals("", properties.get(BROWSER.getKey())); assertEquals("", properties.get(LOOK_AND_FEEL.getKey())); assertEquals("", properties.get(NETWORK_INTERFACE.getKey())); }