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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright (c) 2006-2008 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.Test;
  26. import static org.junit.Assert.*;
  27. public class ProgramErrorTest extends junit.framework.TestCase {
  28. @Test
  29. public void testConstructorNegativeID() {
  30. boolean exception = false;
  31. try {
  32. new ProgramError(-1, ErrorLevel.HIGH, "moo", new String[0], new Date());
  33. } catch (Exception ex) {
  34. exception = true;
  35. }
  36. assertTrue(exception);
  37. }
  38. @Test
  39. public void testConstructorNullErrorLevel() {
  40. boolean exception = false;
  41. try {
  42. new ProgramError(1, null, "moo", new String[0], new Date());
  43. } catch (Exception ex) {
  44. exception = true;
  45. }
  46. assertTrue(exception);
  47. }
  48. @Test
  49. public void testConstructorNullMessage() {
  50. boolean exception = false;
  51. try {
  52. new ProgramError(1, ErrorLevel.HIGH, null, new String[0], new Date());
  53. } catch (Exception ex) {
  54. exception = true;
  55. }
  56. assertTrue(exception);
  57. }
  58. @Test
  59. public void testConstructorEmptyMessage() {
  60. boolean exception = false;
  61. try {
  62. new ProgramError(1, ErrorLevel.HIGH, "", new String[0], new Date());
  63. } catch (Exception ex) {
  64. exception = true;
  65. }
  66. assertTrue(exception);
  67. }
  68. @Test
  69. public void testConstructorNullTrace() {
  70. boolean exception = false;
  71. try {
  72. new ProgramError(1, ErrorLevel.HIGH, "moo", null, new Date());
  73. } catch (Exception ex) {
  74. exception = true;
  75. }
  76. assertTrue(exception);
  77. }
  78. @Test
  79. public void testConstructorNullDate() {
  80. boolean exception = false;
  81. try {
  82. new ProgramError(1, ErrorLevel.HIGH, "moo", new String[0], null);
  83. } catch (Exception ex) {
  84. exception = true;
  85. }
  86. assertTrue(exception);
  87. }
  88. @Test
  89. public void testConstructorGood() {
  90. boolean exception = false;
  91. try {
  92. new ProgramError(1, ErrorLevel.HIGH, "moo", new String[0], new Date());
  93. } catch (IllegalArgumentException ex) {
  94. exception = true;
  95. }
  96. assertFalse(exception);
  97. }
  98. @Test
  99. public void testGetLevel() {
  100. final ProgramError pe = new ProgramError(1, ErrorLevel.HIGH, "moo",
  101. new String[0], new Date());
  102. assertEquals(ErrorLevel.HIGH, pe.getLevel());
  103. }
  104. @Test
  105. public void testGetMessage() {
  106. final ProgramError pe = new ProgramError(1, ErrorLevel.HIGH, "moo",
  107. new String[0], new Date());
  108. assertEquals("moo", pe.getMessage());
  109. }
  110. @Test
  111. public void testGetTrace() {
  112. final String[] trace = new String[]{"abc", "def", "ghi", ""};
  113. final ProgramError pe = new ProgramError(1, ErrorLevel.HIGH, "moo",
  114. trace, new Date());
  115. assertTrue(Arrays.equals(trace, pe.getTrace()));
  116. }
  117. @Test
  118. public void testGetDate() {
  119. final Date date = new Date();
  120. final ProgramError pe = new ProgramError(1, ErrorLevel.HIGH, "moo",
  121. new String[0], date);
  122. assertEquals(date, pe.getDate());
  123. }
  124. @Test
  125. public void testReportStatus() {
  126. final ProgramError pe = new ProgramError(1, ErrorLevel.HIGH, "moo",
  127. new String[0], new Date());
  128. assertEquals(ErrorReportStatus.WAITING, pe.getReportStatus());
  129. pe.setReportStatus(null);
  130. assertEquals(ErrorReportStatus.WAITING, pe.getReportStatus());
  131. pe.setReportStatus(ErrorReportStatus.WAITING);
  132. assertEquals(ErrorReportStatus.WAITING, pe.getReportStatus());
  133. pe.setReportStatus(ErrorReportStatus.ERROR);
  134. assertEquals(ErrorReportStatus.ERROR, pe.getReportStatus());
  135. }
  136. @Test
  137. public void testFixedStatus() {
  138. final ProgramError pe = new ProgramError(1, ErrorLevel.HIGH, "moo",
  139. new String[0], new Date());
  140. assertEquals(ErrorFixedStatus.UNKNOWN, pe.getFixedStatus());
  141. pe.setFixedStatus(null);
  142. assertEquals(ErrorFixedStatus.UNKNOWN, pe.getFixedStatus());
  143. pe.setFixedStatus(ErrorFixedStatus.UNKNOWN);
  144. assertEquals(ErrorFixedStatus.UNKNOWN, pe.getFixedStatus());
  145. pe.setFixedStatus(ErrorFixedStatus.INVALID);
  146. assertEquals(ErrorFixedStatus.INVALID, pe.getFixedStatus());
  147. }
  148. @Test
  149. public void testToString() {
  150. final ProgramError pe = new ProgramError(1, ErrorLevel.HIGH, "moo",
  151. new String[0], new Date());
  152. assertTrue(pe.toString().indexOf("moo") > -1);
  153. }
  154. @Test
  155. public void testEquals() {
  156. final ProgramError pe1 = new ProgramError(10, ErrorLevel.LOW, "moo",
  157. new String[0], new Date());
  158. final ProgramError pe2 = new ProgramError(11, ErrorLevel.LOW, "moo",
  159. new String[0], new Date());
  160. final ProgramError pe3 = new ProgramError(10, ErrorLevel.MEDIUM, "moo",
  161. new String[0], new Date());
  162. final ProgramError pe4 = new ProgramError(10, ErrorLevel.LOW, "bar",
  163. new String[0], new Date());
  164. final ProgramError pe5 = new ProgramError(10, ErrorLevel.LOW, "moo",
  165. new String[]{"Hello"}, new Date());
  166. assertFalse(pe1.equals(null));
  167. assertFalse(pe1.equals("moo"));
  168. assertTrue(pe1.equals(pe2));
  169. assertTrue(pe1.equals(pe1));
  170. assertTrue(pe2.equals(pe1));
  171. assertFalse(pe1.equals(pe3));
  172. assertFalse(pe1.equals(pe4));
  173. assertFalse(pe1.equals(pe5));
  174. assertFalse(pe4.equals(pe5));
  175. assertFalse(pe4.equals(pe3));
  176. assertEquals(pe1.hashCode(), pe2.hashCode());
  177. assertEquals(pe1.hashCode(), pe1.hashCode());
  178. }
  179. }