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.

ProfileTest.java 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Copyright (c) 2006-2014 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
  20. * THE SOFTWARE.
  21. */
  22. package com.dmdirc.addons.ui_swing.dialogs.profiles;
  23. import com.dmdirc.interfaces.config.ConfigProvider;
  24. import com.dmdirc.interfaces.config.IdentityFactory;
  25. import java.util.ArrayList;
  26. import java.util.Arrays;
  27. import java.util.List;
  28. import org.junit.Test;
  29. import org.junit.runner.RunWith;
  30. import org.mockito.Mock;
  31. import org.mockito.runners.MockitoJUnitRunner;
  32. import static org.junit.Assert.assertEquals;
  33. import static org.junit.Assert.assertTrue;
  34. import static org.mockito.Mockito.mock;
  35. import static org.mockito.Mockito.verify;
  36. import static org.mockito.Mockito.when;
  37. /**
  38. * Test profile class.
  39. */
  40. @RunWith(MockitoJUnitRunner.class)
  41. public class ProfileTest {
  42. @Mock private IdentityFactory identityFactory;
  43. private Profile createProfile() {
  44. final List<String> nicknames = new ArrayList<>();
  45. nicknames.add("nickname1");
  46. nicknames.add("nickname2");
  47. final ConfigProvider configProvider = mock(ConfigProvider.class);
  48. when(configProvider.getName()).thenReturn("profile");
  49. when(configProvider.getOption("identity", "name")).thenReturn("profile");
  50. when(configProvider.getOptionList("profile", "nicknames")).thenReturn(nicknames);
  51. when(configProvider.getOption("profile", "realname")).thenReturn("realname");
  52. when(configProvider.getOption("profile", "ident")).thenReturn("ident");
  53. return new Profile(identityFactory, configProvider);
  54. }
  55. /**
  56. * Test null profile constructor.
  57. */
  58. @Test
  59. public void testEmptyConstructor() {
  60. Profile instance = new Profile("New Profile", identityFactory);
  61. assertEquals("", instance.getIdent());
  62. assertEquals("New Profile", instance.getName());
  63. assertEquals(new ArrayList<>(Arrays.asList("NewProfile")), instance.getNicknames());
  64. assertEquals("New Profile", instance.getRealname());
  65. }
  66. /**
  67. * Test null profile constructor.
  68. */
  69. @Test
  70. public void testIdentityConstructor() {
  71. Profile instance = createProfile();
  72. assertEquals("ident", instance.getIdent());
  73. assertEquals("profile", instance.getName());
  74. assertTrue(Arrays.asList(new String[]{"nickname1", "nickname2", })
  75. .equals(instance.getNicknames()));
  76. assertEquals("realname", instance.getRealname());
  77. }
  78. /**
  79. * Test of addNickname method, of class Profile.
  80. */
  81. @Test
  82. public void testAddNickname_String() {
  83. Profile instance = createProfile();
  84. instance.addNickname("nickname3");
  85. assertTrue(Arrays.asList(new String[]{"nickname1", "nickname2",
  86. "nickname3"}).equals(instance.getNicknames()));
  87. }
  88. /**
  89. * Test of addNickname method, of class Profile.
  90. */
  91. @Test
  92. public void testAddNickname_String_Contains() {
  93. Profile instance = createProfile();
  94. instance.addNickname("nickname2");
  95. assertTrue(Arrays.asList(new String[]{"nickname1", "nickname2"})
  96. .equals(instance.getNicknames()));
  97. }
  98. /**
  99. * Test of addNickname method, of class Profile.
  100. */
  101. @Test
  102. public void testAddNickname_String_int() {
  103. Profile instance = createProfile();
  104. instance.addNickname("nickname3", 0);
  105. assertTrue(Arrays.asList(new String[]{"nickname3", "nickname1",
  106. "nickname2"}).equals(instance.getNicknames()));
  107. }
  108. /**
  109. * Test of addNickname method, of class Profile.
  110. */
  111. @Test
  112. public void testAddNickname_String_int_Contains() {
  113. Profile instance = createProfile();
  114. instance.addNickname("nickname2", 0);
  115. assertTrue(Arrays.asList(new String[]{"nickname1","nickname2"})
  116. .equals(instance.getNicknames()));
  117. }
  118. /**
  119. * Test of delNickname method, of class Profile.
  120. */
  121. @Test
  122. public void testDelNickname() {
  123. Profile instance = createProfile();
  124. instance.delNickname("nickname2");
  125. assertTrue(Arrays.asList(new String[]{"nickname1"})
  126. .equals(instance.getNicknames()));
  127. }
  128. /**
  129. * Test of save method, of class Profile.
  130. */
  131. @Test
  132. public void testSave() {
  133. final List<String> nicknames = new ArrayList<>();
  134. nicknames.add("nickname1");
  135. nicknames.add("nickname2");
  136. final ConfigProvider configProvider = mock(ConfigProvider.class);
  137. when(configProvider.getName()).thenReturn("profile");
  138. when(configProvider.getOption("identity", "name")).thenReturn("profile");
  139. when(configProvider.getOptionList("profile", "nicknames")).thenReturn(nicknames);
  140. when(configProvider.getOption("profile", "realname")).thenReturn("realname");
  141. when(configProvider.getOption("profile", "ident")).thenReturn("ident");
  142. Profile instance = new Profile(identityFactory, configProvider);
  143. instance.save();
  144. verify(configProvider).setOption("identity", "name", "profile");
  145. verify(configProvider).setOption("profile", "nicknames", nicknames);
  146. verify(configProvider).setOption("profile", "realname", "realname");
  147. verify(configProvider).setOption("profile", "ident", "ident");
  148. }
  149. @Test
  150. public void testSaveNoConfig() {
  151. final ConfigProvider configProvider = mock(ConfigProvider.class);
  152. when(identityFactory.createProfileConfig("New Profile")).thenReturn(configProvider);
  153. Profile instance = new Profile("New Profile", identityFactory);
  154. instance.save();
  155. verify(identityFactory).createProfileConfig("New Profile");
  156. }
  157. /**
  158. * Test of editNickname method, of class Profile.
  159. */
  160. @Test
  161. public void testEditNicknameOldEmpty() {
  162. Profile instance = createProfile();
  163. instance.editNickname("", "nickname3");
  164. assertTrue(Arrays.asList(new String[]{"nickname1", "nickname2"})
  165. .equals(instance.getNicknames()));
  166. }
  167. /**
  168. * Test of editNickname method, of class Profile.
  169. */
  170. @Test
  171. public void testEditNicknameNewEmpty() {
  172. Profile instance = createProfile();
  173. instance.editNickname("nickname2", "");
  174. assertTrue(Arrays.asList(new String[]{"nickname1", "nickname2"})
  175. .equals(instance.getNicknames()));
  176. }
  177. /**
  178. * Test of editNickname method, of class Profile.
  179. */
  180. @Test
  181. public void testEditNicknameSame() {
  182. Profile instance = createProfile();
  183. instance.editNickname("nickname2", "nickname2");
  184. assertTrue(Arrays.asList(new String[]{"nickname1", "nickname2"})
  185. .equals(instance.getNicknames()));
  186. }
  187. /**
  188. * Test of editNickname method, of class Profile.
  189. */
  190. @Test
  191. public void testEditNickname() {
  192. Profile instance = createProfile();
  193. instance.editNickname("nickname2", "nickname3");
  194. assertTrue(Arrays.asList(new String[]{"nickname1", "nickname3"})
  195. .equals(instance.getNicknames()));
  196. }
  197. /**
  198. * Test of delete method, of class Profile.
  199. */
  200. @Test
  201. public void testDelete() {
  202. final List<String> nicknames = new ArrayList<>();
  203. nicknames.add("nickname1");
  204. nicknames.add("nickname2");
  205. final ConfigProvider configProvider = mock(ConfigProvider.class);
  206. when(configProvider.getName()).thenReturn("profile");
  207. when(configProvider.getOption("identity", "name")).thenReturn("profile");
  208. when(configProvider.getOptionList("profile", "nicknames")).thenReturn(nicknames);
  209. when(configProvider.getOption("profile", "realname")).thenReturn("realname");
  210. when(configProvider.getOption("profile", "ident")).thenReturn("ident");
  211. Profile instance = new Profile(identityFactory, configProvider);
  212. instance.delete();
  213. verify(configProvider).delete();
  214. }
  215. /**
  216. * Test of delete method, of class Profile.
  217. */
  218. @Test
  219. public void testDeleteNullIdentity() {
  220. Profile instance = new Profile("New Profile", null);
  221. instance.delete();
  222. }
  223. }