View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe.tools.logging.jdk;
12  
13  import static org.junit.jupiter.api.Assertions.assertEquals;
14  import static org.mockito.Mockito.doReturn;
15  import static org.mockito.Mockito.mockStatic;
16  import static org.mockito.Mockito.spy;
17  
18  import java.io.File;
19  import java.nio.file.Path;
20  
21  import org.junit.jupiter.api.BeforeEach;
22  import org.junit.jupiter.api.Test;
23  import org.mockito.MockedStatic;
24  
25  import psiprobe.tools.Instruments;
26  
27  /**
28   * The Class JuliHandlerAccessorTest.
29   */
30  class JuliHandlerAccessorTest {
31  
32    /** The accessor. */
33    private JuliHandlerAccessor accessor;
34  
35    /**
36     * Sets the up.
37     */
38    @BeforeEach
39    void setUp() {
40      accessor = spy(new JuliHandlerAccessor());
41    }
42  
43  
44    /**
45     * Test get file all fields present.
46     */
47    @Test
48    void testGetFile_AllFieldsPresent() {
49      Object target = new Object();
50      doReturn(target).when(accessor).getTarget();
51  
52      try (MockedStatic<Instruments> instrumentsMock = mockStatic(Instruments.class)) {
53        instrumentsMock.when(() -> Instruments.getField(target, "directory")).thenReturn("logs");
54        instrumentsMock.when(() -> Instruments.getField(target, "prefix")).thenReturn("app-");
55        instrumentsMock.when(() -> Instruments.getField(target, "suffix")).thenReturn(".log");
56        instrumentsMock.when(() -> Instruments.getField(target, "date")).thenReturn("20240607");
57  
58        File expected = Path.of("logs", "app-20240607.log").toFile();
59        File actual = accessor.getFile();
60  
61        assertEquals(expected, actual);
62      }
63    }
64  
65    /**
66     * Test get file missing field returns stdout file.
67     */
68    @Test
69    void testGetFile_MissingField_ReturnsStdoutFile() {
70      Object target = new Object();
71      doReturn(target).when(accessor).getTarget();
72  
73      try (MockedStatic<Instruments> instrumentsMock = mockStatic(Instruments.class)) {
74        instrumentsMock.when(() -> Instruments.getField(target, "directory")).thenReturn(null);
75        instrumentsMock.when(() -> Instruments.getField(target, "prefix")).thenReturn("app-");
76        instrumentsMock.when(() -> Instruments.getField(target, "suffix")).thenReturn(".log");
77        instrumentsMock.when(() -> Instruments.getField(target, "date")).thenReturn("20240607");
78  
79        Path stdoutFile = Path.of("stdout.log");
80        doReturn(stdoutFile.toFile()).when(accessor).getStdoutFile();
81  
82        File actual = accessor.getFile();
83  
84        assertEquals(stdoutFile.toFile(), actual);
85      }
86    }
87  
88  }