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

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