Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

PreferencesSettingTest.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Copyright (c) 2006-2015 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.provider.AggregateConfigProvider;
  24. import com.dmdirc.config.provider.ConfigProvider;
  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.Test;
  31. import static org.junit.Assert.*;
  32. import static org.mockito.Mockito.*;
  33. public class PreferencesSettingTest {
  34. @Test(expected=IllegalArgumentException.class)
  35. public void testIllegalMultichoice1() {
  36. new PreferencesSetting(PreferencesType.MULTICHOICE, "domain",
  37. "option", "title", "helptext", null, null);
  38. }
  39. @Test(expected=IllegalArgumentException.class)
  40. public void testIllegalMultichoice2() {
  41. new PreferencesSetting(PreferencesType.MULTICHOICE,
  42. new PermissiveValidator<String>(), "domain",
  43. "option", "title", "helptext", null, null);
  44. }
  45. @Test
  46. public void testNormalConstructor() {
  47. final AggregateConfigProvider cm = mock(AggregateConfigProvider.class);
  48. when(cm.getOption("domain", "option")).thenReturn("fallback");
  49. final PreferencesSetting ps = new PreferencesSetting(PreferencesType.TEXT, "domain",
  50. "option", "title", "helptext", cm, null);
  51. assertEquals(PreferencesType.TEXT, ps.getType());
  52. assertEquals("fallback", ps.getValue());
  53. assertEquals("title", ps.getTitle());
  54. assertEquals("helptext", ps.getHelptext());
  55. assertFalse(ps.isRestartNeeded());
  56. assertTrue(ps.getValidator() instanceof PermissiveValidator);
  57. }
  58. @Test
  59. public void testValidatorConstructor() {
  60. final AggregateConfigProvider cm = mock(AggregateConfigProvider.class);
  61. when(cm.getOption("domain", "option")).thenReturn("fallback");
  62. final PreferencesSetting ps = new PreferencesSetting(PreferencesType.TEXT,
  63. new NotEmptyValidator(), "domain",
  64. "option", "title", "helptext", cm, null);
  65. assertEquals(PreferencesType.TEXT, ps.getType());
  66. assertEquals("fallback", ps.getValue());
  67. assertEquals("title", ps.getTitle());
  68. assertEquals("helptext", ps.getHelptext());
  69. assertFalse(ps.isRestartNeeded());
  70. assertTrue(ps.getValidator() instanceof NotEmptyValidator);
  71. }
  72. @Test
  73. public void testRestartNeeded() {
  74. final AggregateConfigProvider cm = mock(AggregateConfigProvider.class);
  75. when(cm.getOption("domain", "option")).thenReturn("fallback");
  76. final PreferencesSetting ps = new PreferencesSetting(PreferencesType.TEXT, "domain",
  77. "option", "title", "helptext", cm, null);
  78. assertFalse(ps.isRestartNeeded());
  79. assertSame(ps, ps.setRestartNeeded());
  80. assertTrue(ps.isRestartNeeded());
  81. }
  82. @Test
  83. public void testMultichoiceAdding() {
  84. final AggregateConfigProvider cm = mock(AggregateConfigProvider.class);
  85. when(cm.getOption("domain", "option")).thenReturn("new");
  86. final Map<String, String> map = new HashMap<>();
  87. map.put("a", "b");
  88. map.put("c", "d");
  89. final PreferencesSetting ps = new PreferencesSetting("domain",
  90. "option", "title", "helptext", map, cm, null);
  91. assertEquals(3, ps.getComboOptions().size());
  92. assertNotNull(ps.getComboOptions().get("new"));
  93. assertTrue(ps.getComboOptions().get("new").startsWith("Current"));
  94. }
  95. @Test
  96. public void testSetValue() {
  97. final AggregateConfigProvider cm = mock(AggregateConfigProvider.class);
  98. when(cm.getOption("domain", "option")).thenReturn("fallback");
  99. final PreferencesSetting ps = new PreferencesSetting(PreferencesType.TEXT, "domain",
  100. "option", "title", "helptext", cm, null);
  101. ps.setValue("newvalue");
  102. assertEquals("newvalue", ps.getValue());
  103. }
  104. @Test
  105. public void testDismiss() {
  106. final AggregateConfigProvider cm = mock(AggregateConfigProvider.class);
  107. when(cm.getOption("domain", "option")).thenReturn("fallback");
  108. final PreferencesSetting ps = new PreferencesSetting(PreferencesType.TEXT, "domain",
  109. "option", "title", "helptext", cm, null);
  110. ps.setValue("newvalue");
  111. ps.dismiss();
  112. assertEquals("fallback", ps.getValue());
  113. final PreferencesSetting ps2 = new PreferencesSetting(PreferencesType.TEXT, "domain",
  114. "option", "title", "helptext", cm, null);
  115. ps2.setValue(null);
  116. ps2.dismiss();
  117. assertEquals("fallback", ps2.getValue());
  118. }
  119. @Test
  120. public void testListener() {
  121. final AggregateConfigProvider cm = mock(AggregateConfigProvider.class);
  122. when(cm.getOption("domain", "option")).thenReturn("fallback");
  123. final PreferencesSetting ps = new PreferencesSetting(PreferencesType.TEXT, "domain",
  124. "option", "title", "helptext", cm, null);
  125. final SettingChangeListener tl = mock(SettingChangeListener.class);
  126. ps.registerChangeListener(tl);
  127. ps.setValue("newvalue");
  128. ps.dismiss();
  129. ps.dismiss();
  130. verify(tl, times(2)).settingChanged(ps);
  131. }
  132. @Test
  133. public void testNeedsSaving() {
  134. final AggregateConfigProvider cm = mock(AggregateConfigProvider.class);
  135. when(cm.getOption("domain", "option")).thenReturn("fallback");
  136. when(cm.getOption("domain", "option2")).thenReturn("fallback");
  137. final PreferencesSetting ps = new PreferencesSetting(PreferencesType.TEXT,
  138. new StringLengthValidator(5, 100), "domain",
  139. "option", "title", "helptext", cm, null);
  140. assertFalse(ps.needsSaving());
  141. ps.setValue("abc");
  142. assertFalse(ps.needsSaving());
  143. ps.setValue("abcdefg");
  144. assertTrue(ps.needsSaving());
  145. final PreferencesSetting ps2 = new PreferencesSetting(PreferencesType.TEXT, "domain",
  146. "option2", "title", "helptext", cm, null);
  147. ps2.setValue(null);
  148. assertTrue(ps2.needsSaving());
  149. }
  150. @Test
  151. public void testSaveUnset() {
  152. final AggregateConfigProvider cm = mock(AggregateConfigProvider.class);
  153. final ConfigProvider configProvider = mock(ConfigProvider.class);
  154. when(cm.getOption("unit-test", "ps")).thenReturn("abc");
  155. when(configProvider.getOption("unit-test", "ps")).thenReturn("abc");
  156. final PreferencesSetting ps = new PreferencesSetting(PreferencesType.TEXT,
  157. "unit-test", "ps", "title", "helptext", cm, configProvider);
  158. assertFalse(ps.save());
  159. ps.setValue(null);
  160. assertTrue(ps.save());
  161. verify(configProvider).unsetOption("unit-test", "ps");
  162. }
  163. @Test
  164. public void testSaveNormal() {
  165. final AggregateConfigProvider cm = mock(AggregateConfigProvider.class);
  166. final ConfigProvider configProvider = mock(ConfigProvider.class);
  167. when(cm.getOption("unit-test", "ps")).thenReturn("abc");
  168. when(configProvider.getOption("unit-test", "ps")).thenReturn("abc");
  169. final PreferencesSetting ps = new PreferencesSetting(PreferencesType.TEXT,
  170. "unit-test", "ps", "title", "helptext", cm, configProvider);
  171. assertFalse(ps.save());
  172. ps.setValue("def");
  173. assertTrue(ps.save());
  174. verify(configProvider).setOption("unit-test", "ps", "def");
  175. }
  176. @Test
  177. public void testIsSet() {
  178. final AggregateConfigProvider cm = mock(AggregateConfigProvider.class);
  179. final ConfigProvider configProvider = mock(ConfigProvider.class);
  180. when(cm.getOption("unit-test", "ps")).thenReturn("abc");
  181. when(configProvider.getOption("unit-test", "ps")).thenReturn("abc");
  182. final PreferencesSetting ps = new PreferencesSetting(PreferencesType.TEXT,
  183. "unit-test", "ps", "title", "helptext", cm, configProvider);
  184. assertFalse(ps.isSet());
  185. }
  186. @Test
  187. public void testIsNotSet() {
  188. final AggregateConfigProvider cm = mock(AggregateConfigProvider.class);
  189. final ConfigProvider identity = mock(ConfigProvider.class);
  190. when(cm.getOption("unit-test", "ps")).thenReturn(null);
  191. when(identity.getOption("unit-test", "ps")).thenReturn(null);
  192. final PreferencesSetting ps = new PreferencesSetting(PreferencesType.TEXT,
  193. "unit-test", "ps", "title", "helptext", cm, identity);
  194. assertFalse(ps.isSet());
  195. }
  196. @Test
  197. public void testUnknownComboOption() {
  198. final AggregateConfigProvider cm = mock(AggregateConfigProvider.class);
  199. final ConfigProvider configProvider = mock(ConfigProvider.class);
  200. when(cm.getOption("unit-test", "ps")).thenReturn("abc");
  201. when(configProvider.getOption("unit-test", "ps")).thenReturn("abc");
  202. final Map<String, String> options = new HashMap<>();
  203. final PreferencesSetting ps = new PreferencesSetting("unit-test",
  204. "ps", "title", "helptext", options, cm, configProvider);
  205. assertEquals("Current (abc)", ps.getComboOptions().get("abc"));
  206. }
  207. @Test
  208. public void testKnownComboOption() {
  209. final AggregateConfigProvider cm = mock(AggregateConfigProvider.class);
  210. final ConfigProvider configProvider = mock(ConfigProvider.class);
  211. when(cm.getOption("unit-test", "ps")).thenReturn("abc");
  212. when(configProvider.getOption("unit-test", "ps")).thenReturn("abc");
  213. final Map<String, String> options = new HashMap<>();
  214. options.put("abc", "123");
  215. final PreferencesSetting ps = new PreferencesSetting("unit-test",
  216. "ps", "title", "helptext", options, cm, configProvider);
  217. assertEquals("123", ps.getComboOptions().get("abc"));
  218. }
  219. }