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.

MultiEventFormatProviderTest.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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.messages;
  23. import com.dmdirc.events.ChannelModesDiscoveredEvent;
  24. import java.util.Optional;
  25. import org.junit.Before;
  26. import org.junit.Test;
  27. import org.junit.runner.RunWith;
  28. import org.mockito.Mock;
  29. import org.mockito.runners.MockitoJUnitRunner;
  30. import static org.junit.Assert.assertFalse;
  31. import static org.junit.Assert.assertSame;
  32. import static org.junit.Assert.assertTrue;
  33. import static org.mockito.Mockito.when;
  34. @RunWith(MockitoJUnitRunner.class)
  35. public class MultiEventFormatProviderTest {
  36. @Mock private EventFormat mockEventFormat1;
  37. @Mock private EventFormat mockEventFormat2;
  38. @Mock private EventFormatProvider mockEventFormatProvider1;
  39. @Mock private EventFormatProvider mockEventFormatProvider2;
  40. @Before
  41. public void setup() {
  42. when(mockEventFormatProvider1.getFormat(ChannelModesDiscoveredEvent.class))
  43. .thenReturn(Optional.of(mockEventFormat1));
  44. when(mockEventFormatProvider2.getFormat(ChannelModesDiscoveredEvent.class))
  45. .thenReturn(Optional.of(mockEventFormat2));
  46. }
  47. @Test
  48. public void testReturnsEmptyWithNoProviders() {
  49. final EventFormatProvider provider = new MultiEventFormatProvider();
  50. assertFalse(provider.getFormat(ChannelModesDiscoveredEvent.class).isPresent());
  51. }
  52. @Test
  53. public void testReturnsFormatFromProvider() {
  54. final EventFormatProvider provider = new MultiEventFormatProvider(mockEventFormatProvider1);
  55. final Optional<EventFormat> res = provider.getFormat(ChannelModesDiscoveredEvent.class);
  56. assertTrue(res.isPresent());
  57. assertSame(mockEventFormat1, res.get());
  58. }
  59. @Test
  60. public void testReturnsFormatFromAlternateProvider() {
  61. final MultiEventFormatProvider provider =
  62. new MultiEventFormatProvider(mockEventFormatProvider1);
  63. provider.addProvider(mockEventFormatProvider2);
  64. final Optional<EventFormat> res = provider.getFormat(ChannelModesDiscoveredEvent.class);
  65. assertTrue(res.isPresent());
  66. assertSame(mockEventFormat1, res.get());
  67. }
  68. @Test
  69. public void testReturnsFirstFormatIfMultipleProvidersHaveFormats() {
  70. final MultiEventFormatProvider provider =
  71. new MultiEventFormatProvider(mockEventFormatProvider1);
  72. provider.addProvider(mockEventFormatProvider2);
  73. final Optional<EventFormat> res = provider.getFormat(ChannelModesDiscoveredEvent.class);
  74. assertTrue(res.isPresent());
  75. assertSame(mockEventFormat1, res.get());
  76. }
  77. @Test
  78. public void testRemovesProvider() {
  79. final MultiEventFormatProvider provider =
  80. new MultiEventFormatProvider(mockEventFormatProvider1);
  81. provider.addProvider(mockEventFormatProvider2);
  82. provider.removeProvider(mockEventFormatProvider1);
  83. final Optional<EventFormat> res = provider.getFormat(ChannelModesDiscoveredEvent.class);
  84. assertTrue(res.isPresent());
  85. assertSame(mockEventFormat2, res.get());
  86. provider.removeProvider(mockEventFormatProvider2);
  87. assertFalse(provider.getFormat(ChannelModesDiscoveredEvent.class).isPresent());
  88. }
  89. }