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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (c) 2006-2014 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 java.util.Arrays;
  24. import java.util.Date;
  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.*;
  31. @RunWith(MockitoJUnitRunner.class)
  32. public class ProgramErrorTest {
  33. @Mock private ErrorManager errorManager;
  34. @Before
  35. public void setup() {
  36. ErrorManager.setErrorManager(errorManager);
  37. }
  38. @Test(expected = IllegalArgumentException.class)
  39. public void testConstructorNegativeID() {
  40. new ProgramError(-1, ErrorLevel.HIGH, "moo", null, null, new Date());
  41. }
  42. @Test(expected = IllegalArgumentException.class)
  43. public void testConstructorNullErrorLevel() {
  44. new ProgramError(1, null, "moo", null, null, new Date());
  45. }
  46. @Test(expected = IllegalArgumentException.class)
  47. public void testConstructorNullMessage() {
  48. new ProgramError(1, ErrorLevel.HIGH, null, null, null, new Date());
  49. }
  50. @Test(expected = IllegalArgumentException.class)
  51. public void testConstructorEmptyMessage() {
  52. new ProgramError(1, ErrorLevel.HIGH, "", null, null, new Date());
  53. }
  54. @Test(expected = IllegalArgumentException.class)
  55. public void testConstructorNullDate() {
  56. new ProgramError(1, ErrorLevel.HIGH, "moo", null, null, null);
  57. }
  58. @Test
  59. public void testConstructorGood() {
  60. new ProgramError(1, ErrorLevel.HIGH, "moo", new Exception(), null, new Date());
  61. }
  62. @Test
  63. public void testGetLevel() {
  64. final ProgramError pe = new ProgramError(1, ErrorLevel.HIGH, "moo",
  65. new Exception(), null, new Date());
  66. assertEquals(ErrorLevel.HIGH, pe.getLevel());
  67. }
  68. @Test
  69. public void testGetMessage() {
  70. final ProgramError pe = new ProgramError(1, ErrorLevel.HIGH, "moo",
  71. new Exception(), null, new Date());
  72. assertEquals("moo", pe.getMessage());
  73. }
  74. @Test
  75. public void testGetDate() {
  76. final Date date = new Date();
  77. final ProgramError pe = new ProgramError(1, ErrorLevel.HIGH, "moo",
  78. new Exception(), null, date);
  79. assertEquals(date, pe.getDate());
  80. }
  81. @Test
  82. public void testReportStatus() {
  83. final ProgramError pe = new ProgramError(1, ErrorLevel.HIGH, "moo",
  84. new Exception(), null, new Date());
  85. assertEquals(ErrorReportStatus.WAITING, pe.getReportStatus());
  86. pe.setReportStatus(null);
  87. assertEquals(ErrorReportStatus.WAITING, pe.getReportStatus());
  88. pe.setReportStatus(ErrorReportStatus.WAITING);
  89. assertEquals(ErrorReportStatus.WAITING, pe.getReportStatus());
  90. pe.setReportStatus(ErrorReportStatus.ERROR);
  91. assertEquals(ErrorReportStatus.ERROR, pe.getReportStatus());
  92. }
  93. @Test
  94. public void testFixedStatus() {
  95. final ProgramError pe = new ProgramError(1, ErrorLevel.HIGH, "moo",
  96. new Exception(), null, new Date());
  97. assertEquals(ErrorFixedStatus.UNKNOWN, pe.getFixedStatus());
  98. pe.setFixedStatus(null);
  99. assertEquals(ErrorFixedStatus.UNKNOWN, pe.getFixedStatus());
  100. pe.setFixedStatus(ErrorFixedStatus.UNKNOWN);
  101. assertEquals(ErrorFixedStatus.UNKNOWN, pe.getFixedStatus());
  102. pe.setFixedStatus(ErrorFixedStatus.INVALID);
  103. assertEquals(ErrorFixedStatus.INVALID, pe.getFixedStatus());
  104. }
  105. @Test
  106. public void testToString() {
  107. final ProgramError pe = new ProgramError(1, ErrorLevel.HIGH, "moo",
  108. new Exception(), null, new Date());
  109. assertTrue(pe.toString().indexOf("moo") > -1);
  110. }
  111. @Test
  112. public void testEquals() {
  113. final Exception ex = new Exception();
  114. final ProgramError pe1 = new ProgramError(10, ErrorLevel.LOW, "moo",
  115. ex, null, new Date());
  116. final ProgramError pe2 = new ProgramError(11, ErrorLevel.LOW, "moo",
  117. ex, null, new Date());
  118. final ProgramError pe3 = new ProgramError(10, ErrorLevel.MEDIUM, "moo",
  119. ex, null, new Date());
  120. final ProgramError pe4 = new ProgramError(10, ErrorLevel.LOW, "bar",
  121. ex, null, new Date());
  122. final ProgramError pe5 = new ProgramError(10, ErrorLevel.LOW, "moo",
  123. null, "Hello", new Date());
  124. assertFalse(pe1.equals(null));
  125. assertFalse(pe1.equals("moo"));
  126. assertTrue(pe1.equals(pe2));
  127. assertTrue(pe1.equals(pe1));
  128. assertTrue(pe2.equals(pe1));
  129. assertFalse(pe1.equals(pe3));
  130. assertFalse(pe1.equals(pe4));
  131. assertFalse(pe1.equals(pe5));
  132. assertFalse(pe4.equals(pe5));
  133. assertFalse(pe4.equals(pe3));
  134. assertEquals(pe1.hashCode(), pe2.hashCode());
  135. assertEquals(pe1.hashCode(), pe1.hashCode());
  136. }
  137. }