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.

ProfileTest.java 7.9KB

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