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

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