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.

DiskLoggingErrorManagerTest.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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.config.ConfigBinder;
  24. import com.dmdirc.events.ProgramErrorEvent;
  25. import com.dmdirc.interfaces.EventBus;
  26. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  27. import com.dmdirc.tests.JimFsRule;
  28. import java.nio.file.Files;
  29. import java.nio.file.Path;
  30. import java.time.LocalDateTime;
  31. import java.util.Optional;
  32. import org.junit.Before;
  33. import org.junit.Rule;
  34. import org.junit.Test;
  35. import org.junit.runner.RunWith;
  36. import org.mockito.Mock;
  37. import org.mockito.runners.MockitoJUnitRunner;
  38. import static org.junit.Assert.assertFalse;
  39. import static org.junit.Assert.assertTrue;
  40. import static org.mockito.Mockito.verify;
  41. import static org.mockito.Mockito.when;
  42. @RunWith(MockitoJUnitRunner.class)
  43. public class DiskLoggingErrorManagerTest {
  44. @Rule public final JimFsRule jimFsRule = new JimFsRule();
  45. @Mock private EventBus eventBus;
  46. @Mock private AggregateConfigProvider config;
  47. @Mock private ConfigBinder configBinder;
  48. @Mock private ProgramErrorEvent error;
  49. @Mock private ProgramError programError;
  50. private DiskLoggingErrorManager instance;
  51. @Before
  52. public void setUp() throws Exception {
  53. when(error.getTimestamp()).thenReturn(LocalDateTime.now());
  54. when(error.getError()).thenReturn(programError);
  55. when(programError.getThrowable()).thenReturn(Optional.of(new IllegalArgumentException()));
  56. when(programError.getThrowableAsString()).thenReturn(Optional.of("test"));
  57. when(programError.getLevel()).thenReturn(ErrorLevel.MEDIUM);
  58. when(config.getBinder()).thenReturn(configBinder);
  59. instance = new DiskLoggingErrorManager(jimFsRule.getPath("/errors"),
  60. eventBus);
  61. }
  62. @Test
  63. public void testInitialise() throws Exception {
  64. assertFalse(Files.exists(jimFsRule.getPath("/errors")));
  65. instance.initialise(config);
  66. verify(configBinder).bind(instance, DiskLoggingErrorManager.class);
  67. assertTrue(Files.exists(jimFsRule.getPath("/errors")));
  68. assertFalse(instance.isDirectoryError());
  69. }
  70. @Test
  71. public void testInitialiseFailed() throws Exception {
  72. // TODO: Test error condition creating directory
  73. }
  74. @Test
  75. public void testHandleErrorEvent() throws Exception {
  76. instance.initialise(config);
  77. instance.handleLoggingSetting(true);
  78. final String logName = error.getTimestamp() + "-" + error.getError().getLevel() + ".log";;
  79. assertFalse(Files.exists(jimFsRule.getPath("/errors", logName)));
  80. instance.handleErrorEvent(error);
  81. final Path errorPath = jimFsRule.getPath("/errors", logName);
  82. assertTrue(Files.exists(errorPath));
  83. assertTrue(Files.readAllLines(errorPath).contains("Level: Medium"));
  84. }
  85. @Test
  86. public void testHandleErrorEventNotLogging() throws Exception {
  87. instance.initialise(config);
  88. instance.handleLoggingSetting(false);
  89. final String logName = error.getTimestamp() + "-" + error.getError().getLevel() + ".log";;
  90. assertFalse(Files.exists(jimFsRule.getPath("/errors", logName)));
  91. instance.handleErrorEvent(error);
  92. assertFalse(Files.exists(jimFsRule.getPath("/errors", logName)));
  93. }
  94. @Test
  95. public void testHandledErrorWriting() throws Exception {
  96. // TODO: Test error condition on write
  97. }
  98. }