1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, 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 v2.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  
15  package ch.qos.logback.core.rolling.helper;
16  
17  
18  import java.io.File;
19  import java.time.Instant;
20  import java.util.Date;
21  
22  import org.junit.jupiter.api.Test;
23  
24  import ch.qos.logback.core.Context;
25  import ch.qos.logback.core.ContextBase;
26  
27  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
28  
29  public class SizeAndTimeBasedArchiveRemoverTest {
30  
31      Context context = new ContextBase();
32  
33      @Test
34      public void smoke() {
35          FileNamePattern fileNamePattern = new FileNamePattern("smoke-%d-%i.gz", context);
36          SizeAndTimeBasedArchiveRemover remover = new SizeAndTimeBasedArchiveRemover(fileNamePattern, null);
37          File[] fileArray = new File[2];
38          File[] expected = new File[2];
39  
40          fileArray[0] = expected[1] = new File("/tmp/smoke-1970-01-01-0.gz");
41          fileArray[1] = expected[0] = new File("/tmp/smoke-1970-01-01-1.gz");
42  
43          remover.descendingSort(fileArray, Instant.ofEpochMilli(0));
44  
45          assertArrayEquals(expected, fileArray);
46      }
47  
48      @Test
49      public void badFilenames() {
50          FileNamePattern fileNamePattern = new FileNamePattern("smoke-%d-%i.gz", context);
51          SizeAndTimeBasedArchiveRemover remover = new SizeAndTimeBasedArchiveRemover(fileNamePattern, null);
52          File[] fileArray = new File[2];
53          File[] expected = new File[2];
54  
55          fileArray[0] = expected[0] = new File("/tmp/smoke-1970-01-01-b.gz");
56          fileArray[1] = expected[1] = new File("/tmp/smoke-1970-01-01-c.gz");
57  
58          remover.descendingSort(fileArray, Instant.ofEpochMilli(0));
59  
60          assertArrayEquals(expected, fileArray);
61      }
62  }