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 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.IdentityManager;
  24. import com.dmdirc.util.validators.NotEmptyValidator;
  25. import com.dmdirc.util.validators.PermissiveValidator;
  26. import com.dmdirc.util.validators.StringLengthValidator;
  27. import java.util.HashMap;
  28. import java.util.Map;
  29. import org.junit.BeforeClass;
  30. import org.junit.Test;
  31. import static org.junit.Assert.*;
  32. import static org.mockito.Mockito.*;
  33. public class PreferencesSettingTest {
  34. @BeforeClass
  35. public static void setUp() throws Exception {
  36. IdentityManager.load();
  37. }
  38. @Test(expected=IllegalArgumentException.class)
  39. public void testIllegalMultichoice1() {
  40. new PreferencesSetting(PreferencesType.MULTICHOICE, "domain",
  41. "option", "title", "helptext");
  42. }
  43. @Test(expected=IllegalArgumentException.class)
  44. public void testIllegalMultichoice2() {
  45. new PreferencesSetting(PreferencesType.MULTICHOICE,
  46. new PermissiveValidator<String>(), "domain",
  47. "option", "title", "helptext");
  48. }
  49. @Test
  50. public void testNormalConstructor() {
  51. IdentityManager.getConfigIdentity().setOption("domain", "option", "fallback");
  52. final PreferencesSetting ps = new PreferencesSetting(PreferencesType.TEXT, "domain",
  53. "option", "title", "helptext");
  54. assertEquals(PreferencesType.TEXT, ps.getType());
  55. assertEquals("fallback", ps.getValue());
  56. assertEquals("title", ps.getTitle());
  57. assertEquals("helptext", ps.getHelptext());
  58. assertFalse(ps.isRestartNeeded());
  59. assertTrue(ps.getValidator() instanceof PermissiveValidator);
  60. }
  61. @Test
  62. public void testValidatorConstructor() {
  63. IdentityManager.getConfigIdentity().setOption("domain", "option", "fallback");
  64. final PreferencesSetting ps = new PreferencesSetting(PreferencesType.TEXT,
  65. new NotEmptyValidator(), "domain",
  66. "option", "title", "helptext");
  67. assertEquals(PreferencesType.TEXT, ps.getType());
  68. assertEquals("fallback", ps.getValue());
  69. assertEquals("title", ps.getTitle());
  70. assertEquals("helptext", ps.getHelptext());
  71. assertFalse(ps.isRestartNeeded());
  72. assertTrue(ps.getValidator() instanceof NotEmptyValidator);
  73. }
  74. @Test
  75. public void testRestartNeeded() {
  76. IdentityManager.getConfigIdentity().setOption("domain", "option", "fallback");
  77. final PreferencesSetting ps = new PreferencesSetting(PreferencesType.TEXT, "domain",
  78. "option", "title", "helptext");
  79. assertFalse(ps.isRestartNeeded());
  80. assertSame(ps, ps.setRestartNeeded());
  81. assertTrue(ps.isRestartNeeded());
  82. }
  83. @Test
  84. public void testMultichoiceAdding() {
  85. IdentityManager.getConfigIdentity().setOption("domain", "option", "new");
  86. final Map<String, String> map = new HashMap<String, String>();
  87. map.put("a", "b");
  88. map.put("c", "d");
  89. final PreferencesSetting ps = new PreferencesSetting("domain",
  90. "option", "title", "helptext", map);
  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. IdentityManager.getConfigIdentity().setOption("domain", "option", "fallback");
  98. final PreferencesSetting ps = new PreferencesSetting(PreferencesType.TEXT, "domain",
  99. "option", "title", "helptext");
  100. ps.setValue("newvalue");
  101. assertEquals("newvalue", ps.getValue());
  102. }
  103. @Test
  104. public void testDismiss() {
  105. IdentityManager.getConfigIdentity().setOption("domain", "option", "fallback");
  106. final PreferencesSetting ps = new PreferencesSetting(PreferencesType.TEXT, "domain",
  107. "option", "title", "helptext");
  108. ps.setValue("newvalue");
  109. ps.dismiss();
  110. assertEquals("fallback", ps.getValue());
  111. final PreferencesSetting ps2 = new PreferencesSetting(PreferencesType.TEXT, "domain",
  112. "option", "title", "helptext");
  113. ps2.setValue(null);
  114. ps2.dismiss();
  115. assertEquals("fallback", ps2.getValue());
  116. }
  117. @Test
  118. public void testListener() {
  119. IdentityManager.getConfigIdentity().setOption("domain", "option", "fallback");
  120. final PreferencesSetting ps = new PreferencesSetting(PreferencesType.TEXT, "domain",
  121. "option", "title", "helptext");
  122. final SettingChangeListener tl = mock(SettingChangeListener.class);
  123. ps.registerChangeListener(tl);
  124. ps.setValue("newvalue");
  125. ps.dismiss();
  126. ps.dismiss();
  127. verify(tl, times(2)).settingChanged(ps);
  128. }
  129. @Test
  130. public void testNeedsSaving() {
  131. IdentityManager.getConfigIdentity().setOption("domain", "option", "fallback");
  132. final PreferencesSetting ps = new PreferencesSetting(PreferencesType.TEXT,
  133. new StringLengthValidator(5, 100), "domain",
  134. "option", "title", "helptext");
  135. assertFalse(ps.needsSaving());
  136. ps.setValue("abc");
  137. assertFalse(ps.needsSaving());
  138. ps.setValue("abcdefg");
  139. assertTrue(ps.needsSaving());
  140. IdentityManager.getConfigIdentity().setOption("domain", "option2", "fallback");
  141. final PreferencesSetting ps2 = new PreferencesSetting(PreferencesType.TEXT, "domain",
  142. "option2", "title", "helptext");
  143. ps2.setValue(null);
  144. assertTrue(ps2.needsSaving());
  145. }
  146. @Test
  147. public void testSaveUnset() {
  148. IdentityManager.getConfigIdentity().setOption("unit-test", "ps", "abc");
  149. final PreferencesSetting ps = new PreferencesSetting(PreferencesType.TEXT,
  150. "unit-test", "ps", "title", "helptext");
  151. assertFalse(ps.save());
  152. ps.setValue(null);
  153. assertTrue(ps.save());
  154. assertFalse(IdentityManager.getConfigIdentity().hasOptionString("unit-test", "ps"));
  155. }
  156. @Test
  157. public void testSaveNormal() {
  158. IdentityManager.getConfigIdentity().setOption("unit-test", "ps", "abc");
  159. final PreferencesSetting ps = new PreferencesSetting(PreferencesType.TEXT,
  160. "unit-test", "ps", "title", "helptext");
  161. assertFalse(ps.save());
  162. ps.setValue("def");
  163. assertTrue(ps.save());
  164. assertEquals("def", IdentityManager.getConfigIdentity().getOption("unit-test", "ps"));
  165. }
  166. }