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.

ProgramErrorTest.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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.logger;
  23. import com.dmdirc.DMDircMBassador;
  24. import com.dmdirc.events.ProgramErrorStatusEvent;
  25. import com.dmdirc.util.ClientInfo;
  26. import com.google.common.collect.Lists;
  27. import java.util.Date;
  28. import java.util.List;
  29. import org.junit.Test;
  30. import org.junit.runner.RunWith;
  31. import org.mockito.ArgumentCaptor;
  32. import org.mockito.Captor;
  33. import org.mockito.Mock;
  34. import org.mockito.runners.MockitoJUnitRunner;
  35. import static org.junit.Assert.assertEquals;
  36. import static org.junit.Assert.assertFalse;
  37. import static org.junit.Assert.assertTrue;
  38. import static org.mockito.Mockito.never;
  39. import static org.mockito.Mockito.verify;
  40. @RunWith(MockitoJUnitRunner.class)
  41. public class ProgramErrorTest {
  42. @Mock private ErrorManager errorManager;
  43. @Mock private DMDircMBassador eventBus;
  44. @Mock private ClientInfo clientInfo;
  45. @Captor private ArgumentCaptor<ProgramErrorStatusEvent> event;
  46. @Test(expected = NullPointerException.class)
  47. public void testConstructorNullErrorLevel() {
  48. new ProgramError(null, "moo", null, Lists.newArrayList(), null, new Date(), errorManager,
  49. eventBus);
  50. }
  51. @Test(expected = NullPointerException.class)
  52. public void testConstructorNullMessage() {
  53. new ProgramError(ErrorLevel.HIGH, null, null, Lists.newArrayList(), null, new Date(),
  54. errorManager, eventBus);
  55. }
  56. @Test(expected = IllegalArgumentException.class)
  57. public void testConstructorEmptyMessage() {
  58. new ProgramError(ErrorLevel.HIGH, "", null, Lists.newArrayList(), null, new Date(),
  59. errorManager, eventBus);
  60. }
  61. @Test(expected = NullPointerException.class)
  62. public void testConstructorNullDate() {
  63. new ProgramError(ErrorLevel.HIGH, "moo", null, Lists.newArrayList(), null, null,
  64. errorManager, eventBus);
  65. }
  66. @Test
  67. public void testConstructorGood() {
  68. new ProgramError(ErrorLevel.HIGH, "moo", new UnsupportedOperationException(),
  69. Lists.newArrayList(), null, new Date(), errorManager, eventBus);
  70. }
  71. @Test
  72. public void testGetLevel() {
  73. final ProgramError pe = new ProgramError(ErrorLevel.HIGH, "moo",
  74. new UnsupportedOperationException(), Lists.newArrayList(), null, new Date(),
  75. errorManager, eventBus);
  76. assertEquals(ErrorLevel.HIGH, pe.getLevel());
  77. }
  78. @Test
  79. public void testGetMessage() {
  80. final ProgramError pe = new ProgramError(ErrorLevel.HIGH, "moo",
  81. new UnsupportedOperationException(), Lists.newArrayList(), null, new Date(),
  82. errorManager, eventBus);
  83. assertEquals("moo", pe.getMessage());
  84. }
  85. @Test
  86. public void testGetDate() {
  87. final Date date = new Date();
  88. final ProgramError pe = new ProgramError(ErrorLevel.HIGH, "moo",
  89. new UnsupportedOperationException(), Lists.newArrayList(), null, date,
  90. errorManager, eventBus);
  91. assertEquals(date, pe.getDate());
  92. }
  93. @Test
  94. public void testReportStatus() {
  95. final ProgramError pe = new ProgramError(ErrorLevel.HIGH, "moo",
  96. new UnsupportedOperationException(), Lists.newArrayList(), null, new Date(),
  97. errorManager, eventBus);
  98. assertEquals(ErrorReportStatus.WAITING, pe.getReportStatus());
  99. pe.setReportStatus(null);
  100. assertEquals(ErrorReportStatus.WAITING, pe.getReportStatus());
  101. verify(eventBus, never()).publishAsync(event.capture());
  102. pe.setReportStatus(ErrorReportStatus.WAITING);
  103. assertEquals(ErrorReportStatus.WAITING, pe.getReportStatus());
  104. verify(eventBus, never()).publishAsync(event.capture());
  105. pe.setReportStatus(ErrorReportStatus.ERROR);
  106. assertEquals(ErrorReportStatus.ERROR, pe.getReportStatus());
  107. verify(eventBus).publishAsync(event.capture());
  108. assertEquals(pe, event.getValue().getError());
  109. }
  110. @Test
  111. public void testToString() {
  112. final ProgramError pe = new ProgramError(ErrorLevel.HIGH, "moo",
  113. new UnsupportedOperationException(), Lists.newArrayList(), null, new Date(),
  114. errorManager, eventBus);
  115. assertTrue(pe.toString().contains("moo"));
  116. }
  117. @Test
  118. public void testGetTrace() {
  119. final List<String> trace = Lists.newArrayList("test", "test1", "test2");
  120. final ProgramError pe = new ProgramError(ErrorLevel.HIGH, "moo",
  121. new UnsupportedOperationException(), trace, null, new Date(), errorManager,
  122. eventBus);
  123. assertEquals(trace, pe.getTrace());
  124. }
  125. @Test
  126. public void testEquals() {
  127. final Exception ex = new UnsupportedOperationException();
  128. final ProgramError pe1 = new ProgramError(ErrorLevel.LOW, "moo",
  129. ex, Lists.newArrayList(), null, new Date(), errorManager, eventBus);
  130. final ProgramError pe2 = new ProgramError(ErrorLevel.LOW, "moo",
  131. ex, Lists.newArrayList(), null, new Date(), errorManager, eventBus);
  132. final ProgramError pe3 = new ProgramError(ErrorLevel.MEDIUM, "moo",
  133. ex, Lists.newArrayList(), null, new Date(), errorManager, eventBus);
  134. final ProgramError pe4 = new ProgramError(ErrorLevel.LOW, "bar",
  135. ex, Lists.newArrayList(), null, new Date(), errorManager, eventBus);
  136. final ProgramError pe5 = new ProgramError(ErrorLevel.LOW, "moo",
  137. null, Lists.newArrayList(), "Hello", new Date(), errorManager, eventBus);
  138. assertFalse(pe1.equals(null)); // NOPMD
  139. assertFalse(pe1.equals("moo"));
  140. assertTrue(pe1.equals(pe2));
  141. assertTrue(pe1.equals(pe1));
  142. assertTrue(pe2.equals(pe1));
  143. assertFalse(pe1.equals(pe3));
  144. assertFalse(pe1.equals(pe4));
  145. assertFalse(pe1.equals(pe5));
  146. assertFalse(pe4.equals(pe5));
  147. assertFalse(pe4.equals(pe3));
  148. assertEquals(pe1.hashCode(), pe2.hashCode());
  149. assertEquals(pe1.hashCode(), pe1.hashCode());
  150. }
  151. }