Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ProgramErrorTest.java 5.8KB

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