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

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