Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ProgramErrorTest.java 5.9KB

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