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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright (c) 2006-2009 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.harness.TestSettingChangeListener;
  24. import com.dmdirc.config.IdentityManager;
  25. import com.dmdirc.config.prefs.validator.NotEmptyValidator;
  26. import com.dmdirc.config.prefs.validator.PermissiveValidator;
  27. import com.dmdirc.config.prefs.validator.StringLengthValidator;
  28. import java.util.HashMap;
  29. import java.util.Map;
  30. import org.junit.Before;
  31. import org.junit.BeforeClass;
  32. import org.junit.Test;
  33. import static org.junit.Assert.*;
  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");
  43. }
  44. @Test(expected=IllegalArgumentException.class)
  45. public void testIllegalMultichoice2() {
  46. new PreferencesSetting(PreferencesType.MULTICHOICE,
  47. new PermissiveValidator<String>(), "domain",
  48. "option", "title", "helptext");
  49. }
  50. @Test
  51. public void testNormalConstructor() {
  52. IdentityManager.getConfigIdentity().setOption("domain", "option", "fallback");
  53. final PreferencesSetting ps = new PreferencesSetting(PreferencesType.TEXT, "domain",
  54. "option", "title", "helptext");
  55. assertEquals(PreferencesType.TEXT, ps.getType());
  56. assertEquals("fallback", ps.getValue());
  57. assertEquals("title", ps.getTitle());
  58. assertEquals("helptext", ps.getHelptext());
  59. assertFalse(ps.isRestartNeeded());
  60. assertTrue(ps.getValidator() instanceof PermissiveValidator);
  61. }
  62. @Test
  63. public void testValidatorConstructor() {
  64. IdentityManager.getConfigIdentity().setOption("domain", "option", "fallback");
  65. final PreferencesSetting ps = new PreferencesSetting(PreferencesType.TEXT,
  66. new NotEmptyValidator(), "domain",
  67. "option", "title", "helptext");
  68. assertEquals(PreferencesType.TEXT, ps.getType());
  69. assertEquals("fallback", ps.getValue());
  70. assertEquals("title", ps.getTitle());
  71. assertEquals("helptext", ps.getHelptext());
  72. assertFalse(ps.isRestartNeeded());
  73. assertTrue(ps.getValidator() instanceof NotEmptyValidator);
  74. }
  75. @Test
  76. public void testRestartNeeded() {
  77. IdentityManager.getConfigIdentity().setOption("domain", "option", "fallback");
  78. final PreferencesSetting ps = new PreferencesSetting(PreferencesType.TEXT, "domain",
  79. "option", "title", "helptext");
  80. assertFalse(ps.isRestartNeeded());
  81. assertSame(ps, ps.setRestartNeeded());
  82. assertTrue(ps.isRestartNeeded());
  83. }
  84. @Test
  85. public void testMultichoiceAdding() {
  86. IdentityManager.getConfigIdentity().setOption("domain", "option", "new");
  87. final Map<String, String> map = new HashMap<String, String>();
  88. map.put("a", "b");
  89. map.put("c", "d");
  90. final PreferencesSetting ps = new PreferencesSetting("domain",
  91. "option", "title", "helptext", map);
  92. assertEquals(3, ps.getComboOptions().size());
  93. assertNotNull(ps.getComboOptions().get("new"));
  94. assertTrue(ps.getComboOptions().get("new").startsWith("Current"));
  95. }
  96. @Test
  97. public void testSetValue() {
  98. IdentityManager.getConfigIdentity().setOption("domain", "option", "fallback");
  99. final PreferencesSetting ps = new PreferencesSetting(PreferencesType.TEXT, "domain",
  100. "option", "title", "helptext");
  101. ps.setValue("newvalue");
  102. assertEquals("newvalue", ps.getValue());
  103. }
  104. @Test
  105. public void testDismiss() {
  106. IdentityManager.getConfigIdentity().setOption("domain", "option", "fallback");
  107. final PreferencesSetting ps = new PreferencesSetting(PreferencesType.TEXT, "domain",
  108. "option", "title", "helptext");
  109. ps.setValue("newvalue");
  110. ps.dismiss();
  111. assertEquals("fallback", ps.getValue());
  112. final PreferencesSetting ps2 = new PreferencesSetting(PreferencesType.TEXT, "domain",
  113. "option", "title", "helptext");
  114. ps2.setValue(null);
  115. ps2.dismiss();
  116. assertEquals("fallback", ps2.getValue());
  117. }
  118. @Test
  119. public void testListener() {
  120. IdentityManager.getConfigIdentity().setOption("domain", "option", "fallback");
  121. final PreferencesSetting ps = new PreferencesSetting(PreferencesType.TEXT, "domain",
  122. "option", "title", "helptext");
  123. final TestSettingChangeListener tl = new TestSettingChangeListener();
  124. ps.registerChangeListener(tl);
  125. ps.setValue("newvalue");
  126. ps.dismiss();
  127. ps.dismiss();
  128. assertEquals(2, tl.count);
  129. assertSame(ps, tl.setting);
  130. }
  131. @Test
  132. public void testNeedsSaving() {
  133. IdentityManager.getConfigIdentity().setOption("domain", "option", "fallback");
  134. final PreferencesSetting ps = new PreferencesSetting(PreferencesType.TEXT,
  135. new StringLengthValidator(5, 100), "domain",
  136. "option", "title", "helptext");
  137. assertFalse(ps.needsSaving());
  138. ps.setValue("abc");
  139. assertFalse(ps.needsSaving());
  140. ps.setValue("abcdefg");
  141. assertTrue(ps.needsSaving());
  142. IdentityManager.getConfigIdentity().setOption("domain", "option2", "fallback");
  143. final PreferencesSetting ps2 = new PreferencesSetting(PreferencesType.TEXT, "domain",
  144. "option2", "title", "helptext");
  145. ps2.setValue(null);
  146. assertTrue(ps2.needsSaving());
  147. }
  148. @Test
  149. public void testSaveUnset() {
  150. IdentityManager.getConfigIdentity().setOption("unit-test", "ps", "abc");
  151. final PreferencesSetting ps = new PreferencesSetting(PreferencesType.TEXT,
  152. "unit-test", "ps", "title", "helptext");
  153. assertFalse(ps.save());
  154. ps.setValue(null);
  155. assertTrue(ps.save());
  156. assertFalse(IdentityManager.getConfigIdentity().hasOptionString("unit-test", "ps"));
  157. }
  158. @Test
  159. public void testSaveNormal() {
  160. IdentityManager.getConfigIdentity().setOption("unit-test", "ps", "abc");
  161. final PreferencesSetting ps = new PreferencesSetting(PreferencesType.TEXT,
  162. "unit-test", "ps", "title", "helptext");
  163. assertFalse(ps.save());
  164. ps.setValue("def");
  165. assertTrue(ps.save());
  166. assertEquals("def", IdentityManager.getConfigIdentity().getOption("unit-test", "ps"));
  167. }
  168. }