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.

ProfileManagerModelTest.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. * Copyright (c) 2006-2012 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 THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.ui_swing.dialogs.profiles;
  23. import com.dmdirc.config.Identity;
  24. import com.dmdirc.config.IdentityManager;
  25. import java.util.ArrayList;
  26. import java.util.Arrays;
  27. import java.util.Collections;
  28. import java.util.List;
  29. import org.junit.BeforeClass;
  30. import org.junit.Test;
  31. import static org.junit.Assert.*;
  32. import static org.mockito.Mockito.*;
  33. /**
  34. * Test for ProfileManagerModel
  35. */
  36. public class ProfileManagerModelTest {
  37. private static IdentityManager manager;
  38. private static Profile defaultProfile;
  39. private Profile createProfile(final String prefix) {
  40. final List<String> nicknames = new ArrayList<String>();
  41. nicknames.add(prefix + "nickname");
  42. final Identity identity = mock(Identity.class);
  43. when(identity.getName()).thenReturn(prefix + "profile");
  44. when(identity.getOption("identity", "name")).thenReturn(prefix + "profile");
  45. when(identity.getOptionList("profile", "nicknames")).thenReturn(nicknames);
  46. when(identity.getOption("profile", "realname")).thenReturn(prefix + "realname");
  47. when(identity.getOption("profile", "ident")).thenReturn(prefix + "ident");
  48. final Profile profile = new Profile(identity);
  49. return profile;
  50. }
  51. private ProfileManagerModel createModel() {
  52. defaultProfile = createProfile("1");
  53. final ProfileManagerModel model = new ProfileManagerModel(manager);
  54. model.addProfile(defaultProfile);
  55. return model;
  56. }
  57. @BeforeClass
  58. @SuppressWarnings("unchecked")
  59. public static void setUpClass() throws Exception {
  60. final List<Identity> identities = Collections.emptyList();
  61. manager = mock(IdentityManager.class);
  62. when(manager.getIdentitiesByType("profile")).thenReturn(identities);
  63. }
  64. /**
  65. * Test of setProfiles method, of class ProfileManagerModel.
  66. */
  67. @Test
  68. public void testGetAndSetProfiles() {
  69. final List<Profile> newProfiles = Arrays.asList(
  70. new Profile[]{createProfile("2"),});
  71. ProfileManagerModel instance = new ProfileManagerModel(manager);
  72. assertEquals(Collections.emptyList(), instance.getProfiles());
  73. instance.setProfiles(newProfiles);
  74. assertEquals(newProfiles, instance.getProfiles());
  75. }
  76. /**
  77. * Test of addProfiles method, of class ProfileManagerModel.
  78. */
  79. @Test
  80. public void testAddProfiles() {
  81. final Profile newProfile = createProfile("2");
  82. ProfileManagerModel instance = createModel();
  83. assertEquals(Arrays.asList(new Profile[]{defaultProfile,}), instance.getProfiles());
  84. final List<Profile> newProfiles = new ArrayList<Profile>();
  85. newProfiles.add(defaultProfile);
  86. newProfiles.add(newProfile);
  87. instance.addProfile(newProfile);
  88. assertEquals(newProfiles, instance.getProfiles());
  89. }
  90. /**
  91. * Test of deleteProfile method, of class ProfileManagerModel.
  92. */
  93. @Test
  94. public void testDeleteProfile() {
  95. ProfileManagerModel instance = createModel();
  96. final List<Profile> newProfiles = new ArrayList<Profile>(instance.getProfiles());
  97. newProfiles.remove(defaultProfile);
  98. instance.deleteProfile(defaultProfile);
  99. assertEquals(newProfiles, instance.getProfiles());
  100. }
  101. /**
  102. * Test of setSelectedProfile method, of class ProfileManagerModel.
  103. */
  104. @Test
  105. public void testGetAndSetSelectedProfile() {
  106. Profile profile2 = createProfile("2");
  107. Profile profile3 = createProfile("3");
  108. Profile profile4 = createProfile("4");
  109. ProfileManagerModel instance = createModel();
  110. assertEquals(defaultProfile, instance.getSelectedProfile());
  111. instance.addProfile(profile2);
  112. assertEquals(profile2, instance.getSelectedProfile());
  113. instance.addProfile(profile3);
  114. instance.setSelectedProfile(profile2);
  115. assertEquals(profile2, instance.getSelectedProfile());
  116. instance.setSelectedProfile(profile4);
  117. assertNotSame(profile4, instance.getSelectedProfile());
  118. }
  119. /**
  120. * Test of getNicknames method, of class ProfileManagerModel.
  121. */
  122. @Test
  123. public void testGetAndSetNicknames() {
  124. ProfileManagerModel instance = createModel();
  125. final List<String> expResult = defaultProfile.getNicknames();
  126. List<String> result = instance.getNicknames();
  127. assertEquals(expResult, result);
  128. result = Arrays.asList(new String[]{"foo", "bar",});
  129. instance.setNicknames(result);
  130. assertEquals(result, instance.getNicknames());
  131. }
  132. /**
  133. * Test of addNickname method, of class ProfileManagerModel.
  134. */
  135. @Test
  136. public void testAddNickname() {
  137. String nickname = "foo";
  138. ProfileManagerModel instance = createModel();
  139. assertEquals(defaultProfile.getNicknames(), instance.getNicknames());
  140. final List<String> nicknames = new ArrayList<String>(defaultProfile.getNicknames());
  141. instance.addNickname(nickname);
  142. nicknames.add(nickname);
  143. assertEquals(nicknames, instance.getNicknames());
  144. }
  145. /**
  146. * Test of deleteNickname method, of class ProfileManagerModel.
  147. */
  148. @Test
  149. public void testDeleteNicknameObject() {
  150. ProfileManagerModel instance = createModel();
  151. final List<String> nicknames = new ArrayList<String>(instance.getNicknames());
  152. final Object nickname = new Object();
  153. assertEquals(nicknames, instance.getNicknames());
  154. instance.deleteNickname(nickname);
  155. assertEquals(nicknames, instance.getNicknames());
  156. }
  157. /**
  158. * Test of deleteNickname method, of class ProfileManagerModel.
  159. */
  160. @Test
  161. public void testDeleteNicknameString() {
  162. ProfileManagerModel instance = createModel();
  163. final List<String> nicknames = new ArrayList<String>(instance.getNicknames());
  164. final String nickname = "1nickname1";
  165. assertEquals(nicknames, instance.getNicknames());
  166. instance.deleteNickname(nickname);
  167. nicknames.remove(nickname);
  168. assertEquals(nicknames, instance.getNicknames());
  169. }
  170. /**
  171. * Test of setSelectedNickname method, of class ProfileManagerModel.
  172. */
  173. @Test
  174. public void testGetAndSetSelectedNickname() {
  175. final String selectedNickname = "1nickname";
  176. ProfileManagerModel instance = createModel();
  177. assertNull(instance.getSelectedNickname());
  178. instance.setSelectedNickname(selectedNickname);
  179. assertEquals(selectedNickname, instance.getSelectedNickname());
  180. instance.setSelectedNickname("foo");
  181. assertNotSame("foo", instance.getSelectedNickname());
  182. }
  183. /**
  184. * Test of editNickname method, of class ProfileManagerModel.
  185. */
  186. @Test
  187. public void testEditNickname() {
  188. ProfileManagerModel instance = createModel();
  189. final List<String> nicknames = new ArrayList<String>(instance.getNicknames());
  190. final String nickname = nicknames.get(0);
  191. assertEquals(nicknames, instance.getNicknames());
  192. instance.editNickname(nickname, "foo");
  193. final int index = nicknames.indexOf(nickname);
  194. nicknames.remove(nickname);
  195. nicknames.add(index, "foo");
  196. assertEquals(nicknames, instance.getNicknames());
  197. }
  198. /**
  199. * Test of getName method, of class ProfileManagerModel.
  200. */
  201. @Test
  202. public void testGetAndSetName() {
  203. ProfileManagerModel instance = createModel();
  204. final String name = defaultProfile.getName();
  205. final String newName = "foo";
  206. assertEquals(name, instance.getName());
  207. instance.setName(newName);
  208. assertEquals(newName, ((Profile) instance.getSelectedProfile()).getName());
  209. }
  210. /**
  211. * Test of getRealname method, of class ProfileManagerModel.
  212. */
  213. @Test
  214. public void testGetAndSetRealname() {
  215. ProfileManagerModel instance = createModel();
  216. final String name = "1realname";
  217. final String newName = "foo";
  218. assertEquals(name, instance.getRealname());
  219. instance.setRealname(newName);
  220. assertEquals(newName, ((Profile) instance.getSelectedProfile()).getRealname());
  221. }
  222. /**
  223. * Test of getIdent method, of class ProfileManagerModel.
  224. */
  225. @Test
  226. public void testGetAndSetIdent() {
  227. ProfileManagerModel instance = createModel();
  228. final String name = "1ident";
  229. final String newName = "foo";
  230. assertEquals(name, instance.getIdent());
  231. instance.setIdent(newName);
  232. assertEquals(newName, ((Profile) instance.getSelectedProfile()).getIdent());
  233. }
  234. /**
  235. * Test of isManipulateProfileAllowed method, of class ProfileManagerModel.
  236. */
  237. @Test
  238. public void testIsManipulateProfileAllowed() {
  239. ProfileManagerModel instance = createModel();
  240. assertTrue(instance.isManipulateProfileAllowed());
  241. instance.setProfiles(Arrays.asList(new Profile[]{}));
  242. assertFalse(instance.isManipulateProfileAllowed());
  243. }
  244. /**
  245. * Test of isManipulateNicknameAllowed method, of class ProfileManagerModel.
  246. */
  247. @Test
  248. public void testIsManipulateNicknameAllowed() {
  249. ProfileManagerModel instance = createModel();
  250. assertFalse(instance.isManipulateNicknameAllowed());
  251. instance.setSelectedNickname("1nickname");
  252. assertTrue(instance.isManipulateNicknameAllowed());
  253. }
  254. /**
  255. * Test of isOKAllowed method, of class ProfileManagerModel.
  256. */
  257. @Test
  258. public void testIsOKAllowed() {
  259. final Profile profile = new Profile(null);
  260. profile.setName("*");
  261. profile.setRealname("");
  262. profile.setIdent("*");
  263. ProfileManagerModel instance = createModel();
  264. assertTrue(instance.isOKAllowed());
  265. instance.setProfiles(Arrays.asList(new Profile[]{}));
  266. assertFalse(instance.isOKAllowed());
  267. instance.setProfiles(Arrays.asList(new Profile[]{profile,}));
  268. assertFalse(instance.isOKAllowed());
  269. profile.setName("profile");
  270. assertFalse(instance.isOKAllowed());
  271. profile.addNickname("nickname");
  272. assertFalse(instance.isOKAllowed());
  273. profile.setRealname("realname");
  274. assertFalse(instance.isOKAllowed());
  275. profile.setIdent("ident");
  276. assertTrue(instance.isOKAllowed());
  277. }
  278. /**
  279. * Test of isNameValid method, of class ProfileManagerModel.
  280. */
  281. @Test
  282. public void testIsNameValid() {
  283. ProfileManagerModel instance = createModel();
  284. instance.setProfiles(Arrays.asList(defaultProfile));
  285. instance.setName("\\");
  286. assertTrue(instance.isNameValid().isFailure());
  287. instance.setName("profile");
  288. assertFalse(instance.isNameValid().isFailure());
  289. }
  290. /**
  291. * Test of isNicknamesValid method, of class ProfileManagerModel.
  292. */
  293. @Test
  294. public void testIsNicknamesValid() {
  295. ProfileManagerModel instance = createModel();
  296. instance.setProfiles(Arrays.asList(defaultProfile));
  297. instance.setNicknames(Arrays.asList(new String[]{}));
  298. assertTrue(instance.isNicknamesValid().isFailure());
  299. instance.setNicknames(Arrays.asList(new String[]{"nickname"}));
  300. assertFalse(instance.isNicknamesValid().isFailure());
  301. }
  302. /**
  303. * Test of isRealnameValid method, of class ProfileManagerModel.
  304. */
  305. @Test
  306. public void testIsRealnameValid() {
  307. ProfileManagerModel instance = createModel();
  308. instance.setProfiles(Arrays.asList(defaultProfile));
  309. instance.setRealname("");
  310. assertTrue(instance.isRealnameValid().isFailure());
  311. instance.setRealname("realname");
  312. assertFalse(instance.isRealnameValid().isFailure());
  313. }
  314. /**
  315. * Test of isIdentValid method, of class ProfileManagerModel.
  316. */
  317. @Test
  318. public void testIsIdentValid() {
  319. ProfileManagerModel instance = createModel();
  320. instance.setProfiles(Arrays.asList(defaultProfile));
  321. instance.setIdent("*");
  322. assertTrue(instance.isIdentValid().isFailure());
  323. instance.setIdent("ident");
  324. assertFalse(instance.isIdentValid().isFailure());
  325. }
  326. }