Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

CoreNewServerDialogModelTest.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * Copyright (c) 2006-2015 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.ui.core.newserver;
  23. import com.dmdirc.config.profiles.Profile;
  24. import com.dmdirc.config.profiles.ProfileManager;
  25. import com.dmdirc.events.ProfileAddedEvent;
  26. import com.dmdirc.events.ProfileDeletedEvent;
  27. import com.dmdirc.interfaces.ConnectionManager;
  28. import com.dmdirc.events.eventbus.EventBus;
  29. import com.dmdirc.config.provider.AggregateConfigProvider;
  30. import com.dmdirc.config.provider.ConfigProvider;
  31. import com.dmdirc.interfaces.ui.NewServerDialogModelListener;
  32. import com.google.common.collect.Lists;
  33. import java.net.URI;
  34. import java.net.URISyntaxException;
  35. import java.util.Optional;
  36. import org.junit.Before;
  37. import org.junit.Test;
  38. import org.junit.runner.RunWith;
  39. import org.mockito.Mock;
  40. import org.mockito.runners.MockitoJUnitRunner;
  41. import static org.junit.Assert.assertEquals;
  42. import static org.junit.Assert.assertFalse;
  43. import static org.junit.Assert.assertTrue;
  44. import static org.mockito.ArgumentMatchers.any;
  45. import static org.mockito.Mockito.never;
  46. import static org.mockito.Mockito.verify;
  47. import static org.mockito.Mockito.when;
  48. @RunWith(MockitoJUnitRunner.class)
  49. public class CoreNewServerDialogModelTest {
  50. @Mock private AggregateConfigProvider globalConfig;
  51. @Mock private ConfigProvider userConfig;
  52. @Mock private ProfileManager profileManager;
  53. @Mock private ConnectionManager connectionManager;
  54. @Mock private EventBus eventBus;
  55. @Mock private Profile profile1;
  56. @Mock private Profile profile2;
  57. @Mock private NewServerDialogModelListener listener;
  58. private CoreNewServerDialogModel instance;
  59. @Before
  60. public void setupMocks() {
  61. when(profileManager.getProfiles()).thenReturn(
  62. Lists.newArrayList(profile1, profile2));
  63. when(globalConfig.getOption("newserver", "hostname")).thenReturn("hostname");
  64. when(globalConfig.getOptionInt("newserver", "port")).thenReturn(1111);
  65. when(globalConfig.getOption("newserver", "password")).thenReturn("password");
  66. when(globalConfig.getOptionBool("newserver", "ssl")).thenReturn(true);
  67. instance = new CoreNewServerDialogModel(globalConfig, userConfig, profileManager,
  68. connectionManager, eventBus);
  69. }
  70. @Test
  71. public void testGetProfiles() {
  72. instance.loadModel();
  73. assertEquals("loadModel: ", Lists.newArrayList(profile1, profile2), instance.
  74. getProfileList());
  75. }
  76. @Test
  77. public void testSelectedProfile() {
  78. instance.loadModel();
  79. assertEquals("testSelectedProfile: ",
  80. Optional.<Profile>empty(), instance.getSelectedProfile());
  81. instance.setSelectedProfile(Optional.ofNullable(profile1));
  82. assertEquals("testSelectedProfile", Optional.ofNullable(profile1),
  83. instance.getSelectedProfile());
  84. }
  85. @Test
  86. public void testProfileValidatorEmpty() {
  87. when(profileManager.getProfiles()).thenReturn(Lists.<Profile>newArrayList());
  88. instance.loadModel();
  89. assertFalse("testProfileValidatorEmpty: ", instance.isProfileListValid());
  90. }
  91. @Test
  92. public void testProfileValidatorNotEmpty() {
  93. when(profileManager.getProfiles()).thenReturn(Lists.newArrayList(profile1, profile2));
  94. instance.loadModel();
  95. assertTrue("testProfileValidatorNotEmpty: ", instance.isProfileListValid());
  96. }
  97. @Test
  98. public void testHostname() {
  99. instance.loadModel();
  100. assertEquals("testHostname: ", Optional.ofNullable("hostname"), instance.getHostname());
  101. instance.setHostname(Optional.ofNullable("test"));
  102. assertEquals("testHostname: ", Optional.ofNullable("test"), instance.getHostname());
  103. }
  104. @Test
  105. public void testHostnameValidatorValid() {
  106. when(globalConfig.getOption("newserver", "hostname")).thenReturn("hostname");
  107. instance.loadModel();
  108. assertTrue("testHostnameValidatorValid: ", instance.isHostnameValid());
  109. }
  110. @Test
  111. public void testHostnameValidatorInvalid() {
  112. when(globalConfig.getOption("newserver", "hostname")).thenReturn("~!£$^£$%^&");
  113. instance.loadModel();
  114. assertFalse("testHostnameValidatorInvalid: ", instance.isHostnameValid());
  115. }
  116. @Test
  117. public void testPort() {
  118. instance.loadModel();
  119. assertEquals("testPort: ", Optional.ofNullable(1111), instance.getPort());
  120. instance.setPort(Optional.ofNullable(5678));
  121. assertEquals("testPort: ", Optional.ofNullable(5678), instance.getPort());
  122. }
  123. @Test
  124. public void testPortValidatorValid() {
  125. when(globalConfig.getOptionInt("newserver", "port")).thenReturn(1111);
  126. instance.loadModel();
  127. assertTrue("testPortValidatorValid: ", instance.isPortValid());
  128. }
  129. @Test
  130. public void testPortValidatorTooLow() {
  131. when(globalConfig.getOptionInt("newserver", "port")).thenReturn(-1);
  132. instance.loadModel();
  133. assertFalse("testPortValidatorTooLow: ", instance.isPortValid());
  134. }
  135. @Test
  136. public void testPortValidatorTooHigh() {
  137. when(globalConfig.getOptionInt("newserver", "port")).thenReturn(65536);
  138. instance.loadModel();
  139. assertFalse("testPortValidatorTooHigh: ", instance.isPortValid());
  140. }
  141. @Test
  142. public void testPassword() {
  143. instance.loadModel();
  144. assertEquals("testPassword: ", Optional.ofNullable("password"), instance.getPassword());
  145. instance.setPassword(Optional.ofNullable("test"));
  146. assertEquals("testPassword: ", Optional.ofNullable("test"), instance.getPassword());
  147. }
  148. @Test
  149. public void testPasswordValidatorValid() {
  150. when(globalConfig.getOption("newserver", "password")).thenReturn("password");
  151. instance.loadModel();
  152. assertTrue("testPasswordValidatorValid: ", instance.isPortValid());
  153. }
  154. @Test
  155. public void testSSL() {
  156. instance.loadModel();
  157. assertTrue("testSSL: ", instance.getSSL());
  158. instance.setSSL(false);
  159. assertFalse("testSSL: ", instance.getSSL());
  160. }
  161. @Test
  162. public void testSaveAsDefault() {
  163. instance.loadModel();
  164. assertFalse("testSaveAsDefault:", instance.getSaveAsDefault());
  165. instance.setSaveAsDefault(true);
  166. assertTrue("testSaveAsDefault:", instance.getSaveAsDefault());
  167. }
  168. @Test
  169. public void testIsSaveAllowedValid() {
  170. when(globalConfig.getOption("newserver", "hostname")).thenReturn("hostname");
  171. instance.loadModel();
  172. assertTrue("testIsSaveAllowedValid: ", instance.isSaveAllowed());
  173. }
  174. @Test
  175. public void testIsSaveAllowedInvalidHostname() {
  176. when(globalConfig.getOption("newserver", "hostname")).thenReturn("~!£$^£$%^&");
  177. instance.loadModel();
  178. assertFalse("testIsSaveAllowedInvalidHostname: ", instance.isSaveAllowed());
  179. }
  180. @Test
  181. public void testIsSaveAllowedInvalidPort() {
  182. when(globalConfig.getOption("newserver", "hostname")).thenReturn("-1");
  183. instance.loadModel();
  184. assertFalse("testIsSaveAllowedInvalidPort: ", instance.isSaveAllowed());
  185. }
  186. @Test
  187. public void testSaveSaveDefaults() {
  188. instance.loadModel();
  189. instance.setSaveAsDefault(true);
  190. instance.save();
  191. verify(userConfig).setOption("newserver", "hostname", "hostname");
  192. verify(userConfig).setOption("newserver", "port", 1111);
  193. verify(userConfig).setOption("newserver", "password", "password");
  194. verify(userConfig).setOption("newserver", "ssl", true);
  195. }
  196. @Test
  197. public void testSaveNotDefaults() {
  198. instance.loadModel();
  199. instance.setSaveAsDefault(false);
  200. instance.save();
  201. verify(userConfig, never()).setOption("newserver", "hostname", "hostname");
  202. verify(userConfig, never()).setOption("newserver", "port", 1111);
  203. verify(userConfig, never()).setOption("newserver", "password", "password");
  204. verify(userConfig, never()).setOption("newserver", "ssl", true);
  205. }
  206. @Test
  207. public void testSaveConnectWithoutProfile() throws URISyntaxException {
  208. instance.loadModel();
  209. instance.save();
  210. verify(connectionManager).connectToAddress(new URI("ircs://password@hostname:1111"));
  211. verify(connectionManager, never()).connectToAddress(any(URI.class), any(Profile.class));
  212. }
  213. @Test
  214. public void testSaveConnectWithProfile() throws URISyntaxException {
  215. instance.loadModel();
  216. instance.setSelectedProfile(Optional.ofNullable(profile1));
  217. instance.save();
  218. verify(connectionManager, never()).connectToAddress(any(URI.class));
  219. verify(connectionManager).connectToAddress(new URI("ircs://password@hostname:1111"),
  220. profile1);
  221. }
  222. @Test
  223. public void testAddConfigProvider() {
  224. when(profileManager.getProfiles()).thenReturn(Lists.newArrayList(profile1));
  225. instance.loadModel();
  226. assertEquals("testAddConfigProvider:", Lists.newArrayList(profile1),
  227. instance.getProfileList());
  228. instance.profileAdded(new ProfileAddedEvent(profile2));
  229. assertEquals("testAddConfigProvider:", Lists.newArrayList(profile1, profile2),
  230. instance.getProfileList());
  231. }
  232. @Test
  233. public void testRemoveConfigProvider() {
  234. when(profileManager.getProfiles()).thenReturn(Lists.newArrayList(profile1, profile2));
  235. instance.loadModel();
  236. assertEquals("testRemoveConfigProvider:", Lists.newArrayList(profile1, profile2),
  237. instance.getProfileList());
  238. instance.profileDeleted(new ProfileDeletedEvent(profile2));
  239. assertEquals("testRemoveConfigProvider:", Lists.newArrayList(profile1),
  240. instance.getProfileList());
  241. }
  242. @Test
  243. public void testRemoveConfigProviderSelectedProfile() {
  244. when(profileManager.getProfiles()).thenReturn(Lists.newArrayList(profile1, profile2));
  245. instance.loadModel();
  246. instance.setSelectedProfile(Optional.ofNullable(profile2));
  247. assertEquals("testRemoveConfigProviderSelectedProfile:",
  248. Lists.newArrayList(profile1, profile2),
  249. instance.getProfileList());
  250. assertEquals("testRemoveConfigProviderSelectedProfile:", Optional.ofNullable(profile2),
  251. instance.getSelectedProfile());
  252. instance.profileDeleted(new ProfileDeletedEvent(profile2));
  253. assertEquals("testRemoveConfigProviderSelectedProfile:", Lists.newArrayList(profile1),
  254. instance.getProfileList());
  255. assertEquals("testRemoveConfigProviderSelectedProfile:", Optional.<Profile>empty(),
  256. instance.getSelectedProfile());
  257. }
  258. @Test
  259. public void testListenerSelectedProfileChanged() {
  260. instance.loadModel();
  261. assertEquals("testListenerSelectedProfileChanged: ", Optional.<Profile>empty(),
  262. instance.getSelectedProfile());
  263. instance.addListener(listener);
  264. instance.setSelectedProfile(Optional.ofNullable(profile1));
  265. assertEquals("testListenerSelectedProfileChanged: ", Optional.ofNullable(profile1),
  266. instance.getSelectedProfile());
  267. verify(listener).selectedProfileChanged(Optional.<Profile>empty(),
  268. Optional.ofNullable(profile1));
  269. }
  270. @Test
  271. public void testListenerProfileListChangedRemoved() {
  272. instance.loadModel();
  273. instance.addListener(listener);
  274. assertEquals("testListenerProfileListChangedRemoved: ",
  275. Lists.newArrayList(profile1, profile2), instance.getProfileList());
  276. instance.profileDeleted(new ProfileDeletedEvent(profile2));
  277. assertEquals("testListenerProfileListChangedRemoved:", Lists.newArrayList(profile1),
  278. instance.getProfileList());
  279. verify(listener).profileListChanged(Lists.newArrayList(profile1));
  280. }
  281. @Test
  282. public void testListenerProfileListChangedAdded() {
  283. when(profileManager.getProfiles()).thenReturn(Lists.newArrayList(profile1));
  284. instance.loadModel();
  285. instance.addListener(listener);
  286. assertEquals("testListenerProfileListChangedAdded: ", Lists.newArrayList(profile1),
  287. instance.getProfileList());
  288. instance.profileAdded(new ProfileAddedEvent(profile2));
  289. assertEquals("testListenerProfileListChangedAdded:", Lists.newArrayList(profile1, profile2),
  290. instance.getProfileList());
  291. verify(listener).profileListChanged(Lists.newArrayList(profile1, profile2));
  292. }
  293. @Test
  294. public void testListenerServerDetailsChangedPort() {
  295. instance.loadModel();
  296. instance.addListener(listener);
  297. instance.setPort(Optional.ofNullable(9999));
  298. verify(listener).serverDetailsChanged(Optional.ofNullable("hostname"),
  299. Optional.ofNullable(9999), Optional.ofNullable("password"), true, false);
  300. }
  301. @Test
  302. public void testListenerServerDetailsChangedPassword() {
  303. instance.loadModel();
  304. instance.addListener(listener);
  305. instance.setPassword(Optional.ofNullable("password-test"));
  306. verify(listener).serverDetailsChanged(Optional.ofNullable("hostname"),
  307. Optional.ofNullable(1111), Optional.ofNullable("password-test"), true, false);
  308. }
  309. @Test
  310. public void testListenerServerDetailsChangedSSL() {
  311. instance.loadModel();
  312. instance.addListener(listener);
  313. instance.setSSL(false);
  314. verify(listener).serverDetailsChanged(Optional.ofNullable("hostname"),
  315. Optional.ofNullable(1111), Optional.ofNullable("password"), false, false);
  316. }
  317. @Test
  318. public void testListenerServerDetailsChangedSaveAsDefault() {
  319. instance.loadModel();
  320. instance.addListener(listener);
  321. instance.setSaveAsDefault(true);
  322. verify(listener).serverDetailsChanged(Optional.ofNullable("hostname"),
  323. Optional.ofNullable(1111), Optional.ofNullable("password"), true, true);
  324. }
  325. @Test
  326. public void testRemoveListener() {
  327. instance.loadModel();
  328. instance.addListener(listener);
  329. instance.setSaveAsDefault(true);
  330. verify(listener).serverDetailsChanged(Optional.ofNullable("hostname"),
  331. Optional.ofNullable(1111), Optional.ofNullable("password"), true, true);
  332. instance.removeListener(listener);
  333. instance.setHostname(Optional.ofNullable("test"));
  334. verify(listener, never()).serverDetailsChanged(Optional.ofNullable("test"),
  335. Optional.ofNullable(1111), Optional.ofNullable("password"), true, false);
  336. }
  337. @Test
  338. public void testSaveDefaults() throws URISyntaxException {
  339. instance.loadModel();
  340. instance.setSelectedProfile(Optional.ofNullable(profile1));
  341. instance.setSaveAsDefault(true);
  342. instance.save();
  343. verify(userConfig).setOption("newserver", "hostname", "hostname");
  344. verify(userConfig).setOption("newserver", "port", 1111);
  345. verify(userConfig).setOption("newserver", "password", "password");
  346. verify(userConfig).setOption("newserver", "ssl", true);
  347. }
  348. @Test
  349. public void testNoSaveDefaults() throws URISyntaxException {
  350. instance.loadModel();
  351. instance.setSelectedProfile(Optional.ofNullable(profile1));
  352. instance.setSaveAsDefault(false);
  353. instance.save();
  354. verify(userConfig, never()).setOption("newserver", "hostname", "hostname");
  355. verify(userConfig, never()).setOption("newserver", "port", 1111);
  356. verify(userConfig, never()).setOption("newserver", "password", "password");
  357. verify(userConfig, never()).setOption("newserver", "ssl", true);
  358. }
  359. }