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

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