1   /**
2    * This program is free software: you can redistribute it and/or modify
3    * it under the terms of the GNU General Public License as published by
4    * the Free Software Foundation, version 3.
5    *
6    * This program is distributed in the hope that it will be useful,
7    * but WITHOUT ANY WARRANTY; without even the implied warranty of
8    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9    * GNU General Public License for more details.
10   *
11   * You should have received a copy of the GNU General Public License
12   * along with this program. If not, see <http://www.gnu.org/licenses/>.
13   */
14  
15  package net.curre.prefcount.service;
16  
17  import java.io.File;
18  import java.util.logging.Logger;
19  
20  import net.curre.prefcount.PrefCountRegistry;
21  import net.curre.prefcount.bean.Settings;
22  import net.curre.prefcount.gui.theme.skin.PrefSkin;
23  import net.curre.prefcount.test.BaseTestCase;
24  
25  /**
26   * This is a junit test for testing settings service.
27   * <p/>
28   * Created date: Jul 1, 2007
29   *
30   * @author Yevgeny Nyden
31   */
32  public class SettingsServiceTest extends BaseTestCase {
33  
34    /** Private class logger. */
35    private static Logger log = Logger.getLogger(SettingsServiceTest.class.toString());
36  
37    /** Value for the main window frame width. */
38    private static final int SETTINGS_MAIN_FRAME_WIDTH = 615;
39  
40    /** Value for the main window frame height. */
41    private static final int SETTINGS_MAIN_FRAME_HEIGHT = 655;
42  
43    /** Value for the dialog window frame width. */
44    private static final int SETTINGS_DIALOG_FRAME_WIDTH = 324;
45  
46    /** Value for the dialog window frame height. */
47    private static final int SETTINGS_DIALOG_FRAME_HEIGHT = 333;
48  
49    /** Value for the Look and Feel theme/skin. */
50    private static final PrefSkin SETTINGS_LAF_SKIN = LafThemeService.AVAILABLE_SKINS[1];
51  
52    /** Value for the locale ID (case insensitive language name). */
53    private static final String SETTINGS_LOCALE_ID = "us";
54  
55    /**
56     * {@inheritDoc}
57     * <p/>
58     * Sets the settings file path in the PrefCountRegistry
59     * to the test value and deletes this test settings file
60     * if it exists.
61     *
62     * @throws Exception on error.
63     */
64    protected void setUp() throws Exception {
65      super.setUp();
66      PrefCountRegistry.getInstance().setSettingsFilePath(SETTINGS_FILE);
67      deleteTestSettingsFile();
68      PrefCountRegistry.getInstance().setMainWindow(null);
69    }
70  
71    /**
72     * {@inheritDoc}
73     * <p/>
74     * Deletes test settings file.
75     *
76     * @throws Exception on error.
77     */
78    protected void tearDown() throws Exception {
79      super.tearDown();
80      PrefCountRegistry.getInstance().setSettingsFilePath(SETTINGS_FILE);
81      deleteTestSettingsFile();
82    }
83  
84    /**
85     * Test main settings service functionality.
86     *
87     * @throws Exception on error.
88     */
89    public void testAll() throws Exception {
90  
91      log.info("Running testAll()...");
92  
93      // testing default settings
94      Settings settings = SettingsService.getSettings();
95      checkSettings(settings, Settings.DEFAULT_MAIN_FRAME_WIDTH, Settings.DEFAULT_MAIN_FRAME_HEIGHT,
96                    Settings.DEFAULT_DIALOG_FRAME_WIDTH, Settings.DEFAULT_DIALOG_FRAME_HEIGHT,
97                    Settings.DEFAULT_LAF_SKIN_ID, PrefCountRegistry.DEFAULT_LOCALE_ID);
98      File file = new File(SETTINGS_FILE);
99      assertFalse("Settings file must not have been created", file.exists());
100 
101     // saving and testing new settings
102     settings.setMainFrameWidth(SETTINGS_MAIN_FRAME_WIDTH);
103     settings.setMainFrameHeight(SETTINGS_MAIN_FRAME_HEIGHT);
104     settings.setDialogFrameWidth(SETTINGS_DIALOG_FRAME_WIDTH);
105     settings.setDialogFrameHeight(SETTINGS_DIALOG_FRAME_HEIGHT);
106     SettingsService.updateSkin(SETTINGS_LAF_SKIN);
107     PrefCountRegistry.getInstance().setCurrentLocale(SETTINGS_LOCALE_ID);
108     SettingsService.saveSettings();
109     assertTrue("Settings file hasn't been created", file.exists());
110     settings = SettingsService.loadSettings();
111     checkSettings(settings, SETTINGS_MAIN_FRAME_WIDTH, SETTINGS_MAIN_FRAME_HEIGHT,
112                   SETTINGS_DIALOG_FRAME_WIDTH, SETTINGS_DIALOG_FRAME_HEIGHT,
113                   SETTINGS_LAF_SKIN.getNameResourceKey(), SETTINGS_LOCALE_ID);
114 
115     // testing settings reset functionality
116     SettingsService.resetSettings();
117     assertTrue("Settings file is not present", file.exists());
118     settings = SettingsService.loadSettings();
119     checkSettings(settings, Settings.DEFAULT_MAIN_FRAME_WIDTH, Settings.DEFAULT_MAIN_FRAME_HEIGHT,
120                   Settings.DEFAULT_DIALOG_FRAME_WIDTH, Settings.DEFAULT_DIALOG_FRAME_HEIGHT,
121                   Settings.DEFAULT_LAF_SKIN_ID, PrefCountRegistry.DEFAULT_LOCALE_ID);
122 
123     // testing errors
124     assertTrue("Internal error - unable to set file read only", file.setReadOnly());
125     try {
126       SettingsService.saveSettings();
127       fail("saveSettings() should have thrown a ServiceException!");
128     } catch (ServiceException e) {
129       // expected
130     }
131   }
132 
133   /**
134    * Test settings service functionality using real frames.
135    *
136    * @throws Exception on error.
137    */
138 /*
139   public void testSettingsWithFrames() throws Exception {
140 
141     log.info("Running testSettingsWithFrames()...");
142 
143     MainWindow main = new MainWindow(false);
144     main.setSize(SETTINGS_MAIN_FRAME_WIDTH, SETTINGS_MAIN_FRAME_HEIGHT);
145     PrefCountRegistry.getInstance().setMainWindow(main);
146 
147     // saving and testing new settings
148     SettingsService.updateSkin(SETTINGS_LAF_SKIN);
149     PrefCountRegistry.setCurrentLocale(SETTINGS_LOCALE_ID);
150     SettingsService.saveSettings();
151     File file = new File(SETTINGS_FILE);
152     assertTrue("Settings file can not be located", file.exists());
153     Settings settings = SettingsService.loadSettings();
154     checkSettings(settings, SETTINGS_MAIN_FRAME_WIDTH, SETTINGS_MAIN_FRAME_HEIGHT,
155                   Settings.DEFAULT_DIALOG_FRAME_WIDTH, Settings.DEFAULT_DIALOG_FRAME_HEIGHT,
156                   SETTINGS_LAF_SKIN.getNameResourceKey(), SETTINGS_LOCALE_ID);
157 
158     main.playerDialogFrame = new PlayerDialogBasePanel(3, main);
159     main.playerDialogFrame.setSize(SETTINGS_DIALOG_FRAME_WIDTH, SETTINGS_DIALOG_FRAME_HEIGHT);
160     SettingsService.saveSettings();
161     settings = SettingsService.loadSettings();
162     checkSettings(settings, SETTINGS_MAIN_FRAME_WIDTH, SETTINGS_MAIN_FRAME_HEIGHT,
163                   SETTINGS_DIALOG_FRAME_WIDTH, SETTINGS_DIALOG_FRAME_HEIGHT,
164                   SETTINGS_LAF_SKIN.getNameResourceKey(), SETTINGS_LOCALE_ID);
165   }
166 */
167 
168   /**
169    * Test settings service functionality in regards to
170    * dealing with old and stale settings files.
171    *
172    * @throws Exception on error.
173    */
174 /*
175   public void testOldSettings() throws Exception {
176 
177     log.info("Running testOldSettings()...");
178 
179     // test loading old settings (without localeId field)
180     PrefCountRegistry.getInstance().setSettingsFilePath(OLD_SETTINGS_FILE);
181     File file = new File(OLD_SETTINGS_FILE);
182     assertTrue("Old settings file can not be located", file.exists());
183     Settings settings = SettingsService.loadSettings();
184     checkSettings(settings, Settings.DEFAULT_MAIN_FRAME_WIDTH, Settings.DEFAULT_MAIN_FRAME_HEIGHT,
185                   Settings.DEFAULT_DIALOG_FRAME_WIDTH, Settings.DEFAULT_DIALOG_FRAME_HEIGHT,
186                   Settings.DEFAULT_LAF_SKIN_ID, PrefCountRegistry.DEFAULT_LOCALE_ID);
187 
188     // test loading stale settings (with a different serialVersionUID)
189     PrefCountRegistry.getInstance().setSettingsFilePath(STALE_SETTINGS_FILE);
190     file = new File(STALE_SETTINGS_FILE);
191     assertTrue("Stale settings file can not be located", file.exists());
192     settings = SettingsService.loadSettings();
193     checkSettings(settings, Settings.DEFAULT_MAIN_FRAME_WIDTH, Settings.DEFAULT_MAIN_FRAME_HEIGHT,
194                   Settings.DEFAULT_DIALOG_FRAME_WIDTH, Settings.DEFAULT_DIALOG_FRAME_HEIGHT,
195                   Settings.DEFAULT_LAF_SKIN_ID, PrefCountRegistry.DEFAULT_LOCALE_ID);
196   }
197 */
198 
199   /** Private methods ***********************/
200 
201   /**
202    * Tests the passed settings object.
203    *
204    * @param settings          Settings object to test.
205    * @param mainFrameWidth    Expected main frame width.
206    * @param mainFrameHeight   Expected main frame height.
207    * @param dialogFrameWidth  Expected dialog frame width.
208    * @param dialogFrameHeight Expected dialog frame height.
209    * @param lafSkinId         Expected LAF skin ID.
210    * @param localeId          Expected locale ID.
211    * @throws Exception On error.
212    */
213   private void checkSettings(Settings settings, int mainFrameWidth, int mainFrameHeight,
214                              int dialogFrameWidth, int dialogFrameHeight, String lafSkinId,
215                              String localeId) throws Exception {
216     assertNotNull("Settings must not be null", settings);
217     assertEquals("Settings has a wrong Main frame width", mainFrameWidth, settings.getMainFrameWidth());
218     assertEquals("Settings has a wrong Main frame height", mainFrameHeight, settings.getMainFrameHeight());
219     assertEquals("Settings has a wrong Dialog frame width", dialogFrameWidth, settings.getDialogFrameWidth());
220     assertEquals("Settings has a wrong Dialog frame height", dialogFrameHeight, settings.getDialogFrameHeight());
221     assertEquals("Settings has a wrong LAF skin ID", lafSkinId, settings.getLafSkinId());
222     assertEquals("Settings has a wrong Locale ID", localeId, settings.getLocaleId());
223   }
224 
225 }