1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    *  Copyright (C) 1999-2025, 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  
15  package ch.qos.logback.core.rolling.testUtil;
16  
17  import ch.qos.logback.core.Context;
18  import ch.qos.logback.core.ContextBase;
19  import ch.qos.logback.core.encoder.EchoEncoder;
20  import ch.qos.logback.core.rolling.helper.FileFilterUtil;
21  import ch.qos.logback.core.rolling.helper.FileNamePattern;
22  import ch.qos.logback.core.testUtil.CoreTestConstants;
23  import ch.qos.logback.core.testUtil.RandomUtil;
24  
25  import java.io.File;
26  import java.io.IOException;
27  import java.sql.Date;
28  import java.util.ArrayList;
29  import java.util.Calendar;
30  import java.util.Enumeration;
31  import java.util.List;
32  import java.util.concurrent.Future;
33  import java.util.concurrent.TimeUnit;
34  import java.util.zip.ZipEntry;
35  import java.util.zip.ZipFile;
36  
37  import static org.junit.jupiter.api.Assertions.assertTrue;
38  
39  public class ParentScaffoldingForRollingTests {
40  
41      protected EchoEncoder<Object> encoder = new EchoEncoder<Object>();
42      protected int diff = RandomUtil.getPositiveInt();
43      protected String randomOutputDir = CoreTestConstants.OUTPUT_DIR_PREFIX + diff + "/";
44      protected List<String> expectedFilenameList = new ArrayList<String>();
45  
46      Calendar calendar = Calendar.getInstance();
47      protected Context context = new ContextBase();
48  
49      protected long currentTime; // initialized in setUp()
50      protected List<Future<?>> futureList = new ArrayList<Future<?>>();
51  
52      public static void existenceCheck(List<String> filenameList) {
53          for (String filename : filenameList) {
54              assertTrue(new File(filename).exists(), "File " + filename + " does not exist");
55          }
56      }
57  
58      public static void reverseSortedContentCheck(String outputDirStr, int runLength, String prefix) throws IOException {
59          File[] fileArray = ScaffoldingForRollingTests.getFilesInDirectory(outputDirStr);
60          FileFilterUtil.reverseSortFileArrayByName(fileArray);
61          ScaffoldingForRollingTests.fileContentCheck(fileArray, runLength, prefix);
62      }
63  
64      static protected void checkZipEntryName(String filepath, String pattern) throws IOException {
65          ZipFile zf = new ZipFile(filepath);
66  
67          try {
68              Enumeration<? extends ZipEntry> entries = zf.entries();
69              assert ((entries.hasMoreElements()));
70              ZipEntry firstZipEntry = entries.nextElement();
71              assert ((!entries.hasMoreElements()));
72              assertTrue(firstZipEntry.getName().matches(pattern));
73          } finally {
74              if (zf != null)
75                  zf.close();
76          }
77      }
78  
79      static protected void zipEntryNameCheck(List<String> expectedFilenameList, String pattern) throws IOException {
80          for (String filepath : expectedFilenameList) {
81              checkZipEntryName(filepath, pattern);
82          }
83      }
84  
85      public void setUp() {
86          context.setName("test");
87          calendar.set(Calendar.MILLISECOND, 333);
88          currentTime = 1760822446333L; //calendar.getTimeInMillis();
89  
90      }
91  
92      protected void add(Future<?> future) {
93          if (future == null)
94              return;
95          if (!futureList.contains(future)) {
96              futureList.add(future);
97          }
98      }
99  
100     protected void waitForJobsToComplete() {
101         for (Future<?> future : futureList) {
102             try {
103                 future.get(10, TimeUnit.SECONDS);
104             } catch (Exception e) {
105                 new RuntimeException("unexpected exception while testing", e);
106             }
107         }
108         futureList.clear();
109     }
110 
111     protected List<String> filterElementsInListBySuffix(String suffix) {
112         List<String> zipFiles = new ArrayList<String>();
113         for (String filename : expectedFilenameList) {
114             if (filename.endsWith(suffix))
115                 zipFiles.add(filename);
116         }
117         return zipFiles;
118     }
119 
120     protected void addExpectedFileName_ByDate(String patternStr, long millis) {
121         FileNamePattern fileNamePattern = new FileNamePattern(patternStr, context);
122         String fn = fileNamePattern.convert(new Date(millis));
123         expectedFilenameList.add(fn);
124     }
125 
126     protected String testId2FileName(String testId) {
127         return randomOutputDir + testId + ".log";
128     }
129 
130     protected void incCurrentTime(long increment) {
131         currentTime += increment;
132     }
133 }