Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ProgramErrorTest.java 5.5KB

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