You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PreferencesSettingTest.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright (c) 2006-2011 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.config.prefs;
  23. import com.dmdirc.config.ConfigManager;
  24. import com.dmdirc.config.IdentityManager;
  25. import com.dmdirc.util.validators.NotEmptyValidator;
  26. import com.dmdirc.util.validators.PermissiveValidator;
  27. import com.dmdirc.util.validators.StringLengthValidator;
  28. import java.util.HashMap;
  29. import java.util.Map;
  30. import org.junit.BeforeClass;
  31. import org.junit.Test;
  32. import static org.junit.Assert.*;
  33. import static org.mockito.Mockito.*;
  34. public class PreferencesSettingTest {
  35. @BeforeClass
  36. public static void setUp() throws Exception {
  37. IdentityManager.load();
  38. }
  39. @Test(expected=IllegalArgumentException.class)
  40. public void testIllegalMultichoice1() {
  41. new PreferencesSetting(PreferencesType.MULTICHOICE, "domain",
  42. "option", "title", "helptext", null, null);
  43. }
  44. @Test(expected=IllegalArgumentException.class)
  45. public void testIllegalMultichoice2() {
  46. new PreferencesSetting(PreferencesType.MULTICHOICE,
  47. new PermissiveValidator<String>(), "domain",
  48. "option", "title", "helptext", null, null);
  49. }
  50. @Test
  51. public void testNormalConstructor() {
  52. ConfigManager cm = mock(ConfigManager.class);
  53. when(cm.getOption("domain", "option")).thenReturn("fallback");
  54. final PreferencesSetting ps = new PreferencesSetting(PreferencesType.TEXT, "domain",
  55. "option", "title", "helptext", cm, null);
  56. assertEquals(PreferencesType.TEXT, ps.getType());
  57. assertEquals("fallback", ps.getValue());
  58. assertEquals("title", ps.getTitle());
  59. assertEquals("helptext", ps.getHelptext());
  60. assertFalse(ps.isRestartNeeded());
  61. assertTrue(ps.getValidator() instanceof PermissiveValidator);
  62. }
  63. @Test
  64. public void testValidatorConstructor() {
  65. ConfigManager cm = mock(ConfigManager.class);
  66. when(cm.getOption("domain", "option")).thenReturn("fallback");
  67. final PreferencesSetting ps = new PreferencesSetting(PreferencesType.TEXT,
  68. new NotEmptyValidator(), "domain",
  69. "option", "title", "helptext", cm, null);
  70. assertEquals(PreferencesType.TEXT, ps.getType());
  71. assertEquals("fallback", ps.getValue());
  72. assertEquals("title", ps.getTitle());
  73. assertEquals("helptext", ps.getHelptext());
  74. assertFalse(ps.isRestartNeeded());
  75. assertTrue(ps.getValidator() instanceof NotEmptyValidator);
  76. }
  77. @Test
  78. public void testRestartNeeded() {
  79. ConfigManager cm = mock(ConfigManager.class);
  80. when(cm.getOption("domain", "option")).thenReturn("fallback");
  81. final PreferencesSetting ps = new PreferencesSetting(PreferencesType.TEXT, "domain",
  82. "option", "title", "helptext", cm, null);
  83. assertFalse(ps.isRestartNeeded());
  84. assertSame(ps, ps.setRestartNeeded());
  85. assertTrue(ps.isRestartNeeded());
  86. }
  87. @Test
  88. public void testMultichoiceAdding() {
  89. ConfigManager cm = mock(ConfigManager.class);
  90. when(cm.getOption("domain", "option")).thenReturn("new");
  91. final Map<String, String> map = new HashMap<String, String>();
  92. map.put("a", "b");
  93. map.put("c", "d");
  94. final PreferencesSetting ps = new PreferencesSetting("domain",
  95. "option", "title", "helptext", map, cm, null);
  96. assertEquals(3, ps.getComboOptions().size());
  97. assertNotNull(ps.getComboOptions().get("new"));
  98. assertTrue(ps.getComboOptions().get("new").startsWith("Current"));
  99. }
  100. @Test
  101. public void testSetValue() {
  102. ConfigManager cm = mock(ConfigManager.class);
  103. when(cm.getOption("domain", "option")).thenReturn("fallback");
  104. final PreferencesSetting ps = new PreferencesSetting(PreferencesType.TEXT, "domain",
  105. "option", "title", "helptext", cm, null);
  106. ps.setValue("newvalue");
  107. assertEquals("newvalue", ps.getValue());
  108. }
  109. @Test
  110. public void testDismiss() {
  111. ConfigManager cm = mock(ConfigManager.class);
  112. when(cm.getOption("domain", "option")).thenReturn("fallback");
  113. final PreferencesSetting ps = new PreferencesSetting(PreferencesType.TEXT, "domain",
  114. "option", "title", "helptext", cm, null);
  115. ps.setValue("newvalue");
  116. ps.dismiss();
  117. assertEquals("fallback", ps.getValue());
  118. final PreferencesSetting ps2 = new PreferencesSetting(PreferencesType.TEXT, "domain",
  119. "option", "title", "helptext", cm, null);
  120. ps2.setValue(null);
  121. ps2.dismiss();
  122. assertEquals("fallback", ps2.getValue());
  123. }
  124. @Test
  125. public void testListener() {
  126. ConfigManager cm = mock(ConfigManager.class);
  127. when(cm.getOption("domain", "option")).thenReturn("fallback");
  128. final PreferencesSetting ps = new PreferencesSetting(PreferencesType.TEXT, "domain",
  129. "option", "title", "helptext", cm, null);
  130. final SettingChangeListener tl = mock(SettingChangeListener.class);
  131. ps.registerChangeListener(tl);
  132. ps.setValue("newvalue");
  133. ps.dismiss();
  134. ps.dismiss();
  135. verify(tl, times(2)).settingChanged(ps);
  136. }
  137. @Test
  138. public void testNeedsSaving() {
  139. ConfigManager cm = mock(ConfigManager.class);
  140. when(cm.getOption("domain", "option")).thenReturn("fallback");
  141. when(cm.getOption("domain", "option2")).thenReturn("fallback");
  142. final PreferencesSetting ps = new PreferencesSetting(PreferencesType.TEXT,
  143. new StringLengthValidator(5, 100), "domain",
  144. "option", "title", "helptext", cm, null);
  145. assertFalse(ps.needsSaving());
  146. ps.setValue("abc");
  147. assertFalse(ps.needsSaving());
  148. ps.setValue("abcdefg");
  149. assertTrue(ps.needsSaving());
  150. final PreferencesSetting ps2 = new PreferencesSetting(PreferencesType.TEXT, "domain",
  151. "option2", "title", "helptext", cm, null);
  152. ps2.setValue(null);
  153. assertTrue(ps2.needsSaving());
  154. }
  155. @Test
  156. public void testSaveUnset() {
  157. IdentityManager.getConfigIdentity().setOption("unit-test", "ps", "abc");
  158. final PreferencesSetting ps = new PreferencesSetting(PreferencesType.TEXT,
  159. "unit-test", "ps", "title", "helptext",
  160. IdentityManager.getIdentityManager().getGlobalConfiguration(),
  161. IdentityManager.getConfigIdentity());
  162. assertFalse(ps.save());
  163. ps.setValue(null);
  164. assertTrue(ps.save());
  165. assertFalse(IdentityManager.getConfigIdentity().hasOptionString("unit-test", "ps"));
  166. }
  167. @Test
  168. public void testSaveNormal() {
  169. IdentityManager.getConfigIdentity().setOption("unit-test", "ps", "abc");
  170. final PreferencesSetting ps = new PreferencesSetting(PreferencesType.TEXT,
  171. "unit-test", "ps", "title", "helptext",
  172. IdentityManager.getIdentityManager().getGlobalConfiguration(),
  173. IdentityManager.getConfigIdentity());
  174. assertFalse(ps.save());
  175. ps.setValue("def");
  176. assertTrue(ps.save());
  177. assertEquals("def", IdentityManager.getConfigIdentity().getOption("unit-test", "ps"));
  178. }
  179. }