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 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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(),
  49. eventBus, true);
  50. }
  51. @Test(expected = NullPointerException.class)
  52. public void testConstructorNullDate() {
  53. new ProgramError(ErrorLevel.HIGH, "moo", null, Lists.newArrayList(), null, null,
  54. eventBus, true);
  55. }
  56. @Test
  57. public void testConstructorGood() {
  58. new ProgramError(ErrorLevel.HIGH, "moo", new UnsupportedOperationException(),
  59. Lists.newArrayList(), null, new Date(), eventBus, true);
  60. }
  61. @Test
  62. public void testGetLevel() {
  63. final ProgramError pe = new ProgramError(ErrorLevel.HIGH, "moo",
  64. new UnsupportedOperationException(), Lists.newArrayList(), null, new Date(),
  65. eventBus, true);
  66. assertEquals(ErrorLevel.HIGH, pe.getLevel());
  67. }
  68. @Test
  69. public void testGetMessage() {
  70. final ProgramError pe = new ProgramError(ErrorLevel.HIGH, "moo",
  71. new UnsupportedOperationException(), Lists.newArrayList(), null, new Date(),
  72. eventBus, true);
  73. assertEquals("moo", pe.getMessage());
  74. }
  75. @Test
  76. public void testGetDate() {
  77. final Date date = new Date();
  78. final ProgramError pe = new ProgramError(ErrorLevel.HIGH, "moo",
  79. new UnsupportedOperationException(), Lists.newArrayList(), null, date,
  80. eventBus, true);
  81. assertEquals(date, pe.getDate());
  82. }
  83. @Test
  84. public void testIsAppError() {
  85. final ProgramError pe = new ProgramError(ErrorLevel.HIGH, "moo",
  86. new UnsupportedOperationException(), Lists.newArrayList(), null, new Date(),
  87. eventBus, true);
  88. assertTrue(pe.isAppError());
  89. }
  90. @Test
  91. public void testReportStatus() {
  92. final ProgramError pe = new ProgramError(ErrorLevel.HIGH, "moo",
  93. new UnsupportedOperationException(), Lists.newArrayList(), null, new Date(),
  94. eventBus, true);
  95. assertEquals(ErrorReportStatus.WAITING, pe.getReportStatus());
  96. pe.setReportStatus(null);
  97. assertEquals(ErrorReportStatus.WAITING, pe.getReportStatus());
  98. verify(eventBus, never()).publishAsync(event.capture());
  99. pe.setReportStatus(ErrorReportStatus.WAITING);
  100. assertEquals(ErrorReportStatus.WAITING, pe.getReportStatus());
  101. verify(eventBus, never()).publishAsync(event.capture());
  102. pe.setReportStatus(ErrorReportStatus.ERROR);
  103. assertEquals(ErrorReportStatus.ERROR, pe.getReportStatus());
  104. verify(eventBus).publishAsync(event.capture());
  105. assertEquals(pe, event.getValue().getError());
  106. }
  107. @Test
  108. public void testToString() {
  109. final ProgramError pe = new ProgramError(ErrorLevel.HIGH, "moo",
  110. new UnsupportedOperationException(), Lists.newArrayList(), null, new Date(),
  111. eventBus, true);
  112. assertTrue(pe.toString().contains("moo"));
  113. }
  114. @Test
  115. public void testGetTrace() {
  116. final List<String> trace = Lists.newArrayList("test", "test1", "test2");
  117. final ProgramError pe = new ProgramError(ErrorLevel.HIGH, "moo",
  118. new UnsupportedOperationException(), trace, null, new Date(),
  119. eventBus, true);
  120. assertEquals(trace, pe.getTrace());
  121. }
  122. @Test
  123. public void testEquals() {
  124. final Exception ex = new UnsupportedOperationException();
  125. final ProgramError pe1 = new ProgramError(ErrorLevel.LOW, "moo",
  126. ex, Lists.newArrayList(), null, new Date(), eventBus, true);
  127. final ProgramError pe2 = new ProgramError(ErrorLevel.LOW, "moo",
  128. ex, Lists.newArrayList(), null, new Date(), eventBus, true);
  129. final ProgramError pe3 = new ProgramError(ErrorLevel.MEDIUM, "moo",
  130. ex, Lists.newArrayList(), null, new Date(), eventBus, true);
  131. final ProgramError pe4 = new ProgramError(ErrorLevel.LOW, "bar",
  132. ex, Lists.newArrayList(), null, new Date(), eventBus, true);
  133. final ProgramError pe5 = new ProgramError(ErrorLevel.LOW, "moo",
  134. null, Lists.newArrayList(), "Hello", new Date(), eventBus, true);
  135. assertFalse(pe1.equals(null)); // NOPMD
  136. assertFalse(pe1.equals("moo"));
  137. assertTrue(pe1.equals(pe2));
  138. assertTrue(pe1.equals(pe1));
  139. assertTrue(pe2.equals(pe1));
  140. assertFalse(pe1.equals(pe3));
  141. assertFalse(pe1.equals(pe4));
  142. assertFalse(pe1.equals(pe5));
  143. assertFalse(pe4.equals(pe5));
  144. assertFalse(pe4.equals(pe3));
  145. assertEquals(pe1.hashCode(), pe2.hashCode());
  146. assertEquals(pe1.hashCode(), pe1.hashCode());
  147. }
  148. }