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

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