Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ProfileTest.java 9.1KB

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