1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.pattern;
15  
16  import ch.qos.logback.classic.spi.LoggingEvent;
17  import org.junit.jupiter.api.Test;
18  
19  import java.time.Instant;
20  import java.util.Collections;
21  
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  
24  public class EpochConverterTest {
25      EpochConverter ec = new EpochConverter();
26  
27      @Test
28      public void withDefaultConfiguration() {
29          ec.setOptionList(null);
30          ec.start();
31          LoggingEvent le = new LoggingEvent();
32          Instant instant = Instant.parse("2026-01-15T10:15:30Z");
33          instant = instant.plusMillis(321);
34          le.setInstant(instant);
35  
36          String result = ec.convert(le);
37          assertEquals("1768472130321", result); // includes millis
38      }
39  
40      @Test
41      public void withSecondsConfiguration() {
42          ec.setOptionList(Collections.singletonList("seconds"));
43          ec.start();
44          LoggingEvent le = new LoggingEvent();
45          Instant instant = Instant.parse("2026-01-15T10:15:30Z");
46          instant = instant.plusMillis(321); // millis should be ignored
47          le.setInstant(instant);
48  
49          String result = ec.convert(le);
50          assertEquals("1768472130", result);
51      }
52  
53      @Test
54      public void withUnknownNonsenseConfiguration() {
55          ec.setOptionList(Collections.singletonList("nonsense"));
56          ec.start();
57          LoggingEvent le = new LoggingEvent();
58          Instant instant = Instant.parse("2026-01-15T10:15:30Z");
59          instant = instant.plusMillis(321);
60          le.setInstant(instant);
61  
62          String result = ec.convert(le);
63          assertEquals("1768472130321", result); // includes millis, default behaviour
64      }
65  }