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.

ProfileManagerTest.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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.config.profiles;
  23. import com.dmdirc.events.ProfileAddedEvent;
  24. import com.dmdirc.events.ProfileDeletedEvent;
  25. import com.dmdirc.interfaces.EventBus;
  26. import com.dmdirc.util.SystemInfo;
  27. import com.google.common.collect.Lists;
  28. import java.util.Optional;
  29. import org.junit.Before;
  30. import org.junit.Test;
  31. import org.junit.runner.RunWith;
  32. import org.mockito.Mock;
  33. import org.mockito.runners.MockitoJUnitRunner;
  34. import static junit.framework.TestCase.assertEquals;
  35. import static junit.framework.TestCase.assertFalse;
  36. import static junit.framework.TestCase.assertTrue;
  37. import static org.mockito.Matchers.any;
  38. import static org.mockito.Mockito.reset;
  39. import static org.mockito.Mockito.verify;
  40. import static org.mockito.Mockito.when;
  41. @RunWith(MockitoJUnitRunner.class)
  42. public class ProfileManagerTest {
  43. @Mock private EventBus eventBus;
  44. @Mock private Profile profile1;
  45. @Mock private Profile profile2;
  46. @Mock private SystemInfo systemInfo;
  47. private ProfileManager instance;
  48. @Before
  49. public void setUp() throws Exception {
  50. when(systemInfo.getProperty("user.name")).thenReturn("UserName");
  51. instance = new ProfileManager(eventBus, systemInfo);
  52. }
  53. @Test
  54. public void testAddProfile() {
  55. assertTrue(instance.getProfiles().isEmpty());
  56. instance.addProfile(profile1);
  57. assertEquals(1, instance.getProfiles().size());
  58. assertTrue(instance.getProfiles().contains(profile1));
  59. }
  60. @Test
  61. public void testAddProfile_Event() {
  62. instance.addProfile(profile1);
  63. verify(eventBus).publishAsync(any(ProfileAddedEvent.class));
  64. }
  65. @Test
  66. public void testDeleteProfile() {
  67. instance.addProfile(profile1);
  68. instance.addProfile(profile2);
  69. assertEquals(2, instance.getProfiles().size());
  70. assertTrue(instance.getProfiles().contains(profile1));
  71. assertTrue(instance.getProfiles().contains(profile2));
  72. instance.deleteProfile(profile2);
  73. assertEquals(1, instance.getProfiles().size());
  74. assertTrue(instance.getProfiles().contains(profile1));
  75. assertFalse(instance.getProfiles().contains(profile2));
  76. }
  77. @Test
  78. public void testDeleteProfile_Event() {
  79. instance.addProfile(profile1);
  80. //Fairly certain this shouldn't be needed, but the any matcher below seems to match the add
  81. reset(eventBus);
  82. instance.deleteProfile(profile1);
  83. verify(eventBus).publishAsync(any(ProfileDeletedEvent.class));
  84. }
  85. @Test
  86. public void testGetDefaultProfile_EmptyList() {
  87. final String nick = "UserName";
  88. final Profile profile = Profile.create(nick, nick, Optional.empty(), Lists.newArrayList(nick));
  89. assertEquals(profile, instance.getDefault());
  90. }
  91. @Test
  92. public void testGetDefaultProfile_1() {
  93. instance.addProfile(profile1);
  94. instance.addProfile(profile2);
  95. assertEquals(profile1, instance.getDefault());
  96. }
  97. @Test
  98. public void testGetDefaultProfile_2() {
  99. instance.addProfile(profile2);
  100. instance.addProfile(profile1);
  101. assertEquals(profile2, instance.getDefault());
  102. }
  103. }