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.model;
12  
13  import static org.junit.jupiter.api.Assertions.assertEquals;
14  import static org.junit.jupiter.api.Assertions.assertNotNull;
15  import static org.junit.jupiter.api.Assertions.assertNull;
16  
17  import com.codebox.bean.JavaBeanTester;
18  
19  import java.io.File;
20  import java.nio.file.Path;
21  import java.sql.Timestamp;
22  
23  import org.junit.jupiter.api.Test;
24  import org.mockito.Mockito;
25  
26  import psiprobe.tools.logging.LogDestination;
27  
28  /**
29   * The Class DisconnectedLogDestinationTest.
30   */
31  class DisconnectedLogDestinationTest {
32  
33    /**
34     * Javabean tester.
35     */
36    @Test
37    void javabeanTester() {
38      JavaBeanTester.builder(DisconnectedLogDestination.class).loadData().skipStrictSerializable()
39          .test();
40    }
41  
42    @Test
43    void testBuilderFromDestination() {
44      LogDestination dest = Mockito.mock(LogDestination.class);
45      Application app = new Application();
46      Mockito.when(dest.getApplication()).thenReturn(app);
47      Mockito.when(dest.isRoot()).thenReturn(true);
48      Mockito.when(dest.isContext()).thenReturn(false);
49      Mockito.when(dest.getName()).thenReturn("testLogger");
50      Mockito.when(dest.getIndex()).thenReturn("0");
51      Mockito.when(dest.getTargetClass()).thenReturn("com.example.Logger");
52      Mockito.when(dest.getConversionPattern()).thenReturn("%d %m%n");
53      File logFile = Path.of(System.getProperty("java.io.tmpdir"), "test.log").toFile();
54      Mockito.when(dest.getFile()).thenReturn(logFile);
55      Mockito.when(dest.getLogType()).thenReturn("logback");
56      Mockito.when(dest.getSize()).thenReturn(1024L);
57      Mockito.when(dest.getLastModified()).thenReturn(new Timestamp(1000L));
58      Mockito.when(dest.getLevel()).thenReturn("INFO");
59      Mockito.when(dest.getValidLevels()).thenReturn(new String[] {"DEBUG", "INFO", "WARN"});
60      Mockito.when(dest.getEncoding()).thenReturn("UTF-8");
61  
62      DisconnectedLogDestination dld = new DisconnectedLogDestination().builder(dest);
63  
64      assertEquals(app, dld.getApplication());
65      assertEquals(true, dld.isRoot());
66      assertEquals(false, dld.isContext());
67      assertEquals("testLogger", dld.getName());
68      assertEquals("0", dld.getIndex());
69      assertEquals("com.example.Logger", dld.getTargetClass());
70      assertEquals("%d %m%n", dld.getConversionPattern());
71      assertEquals(logFile.getPath(), dld.getFile().getPath());
72      assertEquals("logback", dld.getLogType());
73      assertEquals(1024L, dld.getSize());
74      assertNotNull(dld.getLastModified());
75      assertEquals("INFO", dld.getLevel());
76      assertEquals(3, dld.getValidLevels().length);
77      assertEquals("UTF-8", dld.getEncoding());
78    }
79  
80    @Test
81    void testGetLastModifiedWhenNull() {
82      DisconnectedLogDestination dld = new DisconnectedLogDestination();
83      assertNull(dld.getLastModified());
84    }
85  
86    @Test
87    void testGetValidLevelsWhenNull() {
88      DisconnectedLogDestination dld = new DisconnectedLogDestination();
89      assertEquals(0, dld.getValidLevels().length);
90    }
91  
92    @Test
93    void testGetLastModifiedReturnsDefensiveCopy() {
94      LogDestination dest = Mockito.mock(LogDestination.class);
95      Timestamp ts = new Timestamp(5000L);
96      Mockito.when(dest.getLastModified()).thenReturn(ts);
97      Mockito.when(dest.getValidLevels()).thenReturn(new String[0]);
98  
99      DisconnectedLogDestination dld = new DisconnectedLogDestination().builder(dest);
100     Timestamp retrieved = dld.getLastModified();
101     // Should be a defensive copy, not the same instance
102     assertEquals(ts.getTime(), retrieved.getTime());
103     assertEquals(5000L, retrieved.getTime());
104   }
105 }