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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.binding.ConfigBinder;
  24. import com.dmdirc.events.ProgramErrorEvent;
  25. import com.dmdirc.events.eventbus.EventBus;
  26. import com.dmdirc.config.provider.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.getThrowableAsString()).thenReturn(Optional.of("test"));
  56. when(programError.getLevel()).thenReturn(ErrorLevel.MEDIUM);
  57. when(config.getBinder()).thenReturn(configBinder);
  58. instance = new DiskLoggingErrorManager(jimFsRule.getPath("/errors"),
  59. eventBus);
  60. }
  61. @Test
  62. public void testInitialise() throws Exception {
  63. assertFalse(Files.exists(jimFsRule.getPath("/errors")));
  64. instance.initialise(config);
  65. verify(configBinder).bind(instance, DiskLoggingErrorManager.class);
  66. assertTrue(Files.exists(jimFsRule.getPath("/errors")));
  67. assertFalse(instance.isDirectoryError());
  68. }
  69. @Test
  70. public void testInitialiseFailed() throws Exception {
  71. // TODO: Test error condition creating directory
  72. }
  73. @Test
  74. public void testHandleErrorEvent() throws Exception {
  75. instance.initialise(config);
  76. instance.handleLoggingSetting(true);
  77. final String logName = error.getTimestamp() + "-" + error.getError().getLevel() + ".log";;
  78. assertFalse(Files.exists(jimFsRule.getPath("/errors", logName)));
  79. instance.handleErrorEvent(error);
  80. final Path errorPath = jimFsRule.getPath("/errors", logName);
  81. assertTrue(Files.exists(errorPath));
  82. assertTrue(Files.readAllLines(errorPath).contains("Level: Medium"));
  83. }
  84. @Test
  85. public void testHandleErrorEventNotLogging() throws Exception {
  86. instance.initialise(config);
  87. instance.handleLoggingSetting(false);
  88. final String logName = error.getTimestamp() + "-" + error.getError().getLevel() + ".log";;
  89. assertFalse(Files.exists(jimFsRule.getPath("/errors", logName)));
  90. instance.handleErrorEvent(error);
  91. assertFalse(Files.exists(jimFsRule.getPath("/errors", logName)));
  92. }
  93. @Test
  94. public void testHandledErrorWriting() throws Exception {
  95. // TODO: Test error condition on write
  96. }
  97. }