1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.rolling.testUtil;
15
16 import ch.qos.logback.core.rolling.helper.FileFilterUtil;
17 import ch.qos.logback.core.testUtil.FileToBufferUtil;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.sql.Date;
22 import java.text.SimpleDateFormat;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.function.UnaryOperator;
26
27 import static org.junit.jupiter.api.Assertions.assertEquals;
28 import static org.junit.jupiter.api.Assertions.assertTrue;
29
30
31
32
33
34
35
36 public class ScaffoldingForRollingTests extends ParentScaffoldingForRollingTests {
37
38 static public final String DATE_PATTERN_WITH_SECONDS = "yyyy-MM-dd_HH_mm_ss";
39 static public final String DATE_PATTERN_BY_DAY = "yyyy-MM-dd";
40 static public final SimpleDateFormat SDF = new SimpleDateFormat(DATE_PATTERN_WITH_SECONDS);
41 public static final int MILLIS_IN_ONE_SECOND = 1000;
42
43
44 protected long nextRolloverThreshold;
45
46 public void setUp() {
47 super.setUp();
48 recomputeRolloverThreshold(currentTime);
49 }
50
51 public static void existenceCheck(String filename) {
52 assertTrue(new File(filename).exists(), "File " + filename + " does not exist");
53 }
54
55 public static File[] getFilesInDirectory(String outputDirStr) {
56 File outputDir = new File(outputDirStr);
57 return outputDir.listFiles();
58 }
59
60 public static void fileContentCheck(File[] fileArray, int runLength, String prefix) throws IOException {
61 fileContentCheck(fileArray, runLength, prefix, 0);
62 }
63
64 public static void fileContentCheck(File[] fileArray, int runLength, String prefix, int runStart)
65 throws IOException {
66 List<String> stringList = new ArrayList<String>();
67 for (File file : fileArray) {
68 FileToBufferUtil.readIntoList(file, stringList);
69 }
70
71 List<String> witnessList = new ArrayList<String>();
72
73 for (int i = runStart; i < runLength; i++) {
74 witnessList.add(prefix + i);
75 }
76 assertEquals(witnessList, stringList);
77 }
78
79 public static void sortedContentCheck(String outputDirStr, int runLength, String prefix) throws IOException {
80 sortedContentCheck(outputDirStr, runLength, prefix, 0);
81 }
82
83 public static void sortedContentCheck(String outputDirStr, int runLength, String prefix, int runStart)
84 throws IOException {
85 File[] fileArray = getFilesInDirectory(outputDirStr);
86 FileFilterUtil.sortFileArrayByName(fileArray);
87 fileContentCheck(fileArray, runLength, prefix, runStart);
88 }
89
90 public static int existenceCount(List<String> filenameList) {
91 int existenceCounter = 0;
92 for (String filename : filenameList) {
93 if (new File(filename).exists()) {
94 existenceCounter++;
95 }
96 }
97 return existenceCounter;
98 }
99
100 protected String nullFileName(String testId) {
101 return null;
102 }
103
104 protected String impossibleFileName(String testId) {
105 throw new RuntimeException("implement");
106 }
107
108
109 protected void recomputeRolloverThreshold(long ct) {
110 long delta = ct % MILLIS_IN_ONE_SECOND;
111 nextRolloverThreshold = (ct - delta) + MILLIS_IN_ONE_SECOND;
112 }
113
114 protected boolean passThresholdTime(long nextRolloverThreshold) {
115 return currentTime >= nextRolloverThreshold;
116 }
117
118 protected Date getDateOfCurrentPeriodsStart() {
119 long delta = currentTime % MILLIS_IN_ONE_SECOND;
120 return new Date(currentTime - delta);
121 }
122
123 protected Date getDateOfPreviousPeriodsStart() {
124 long delta = currentTime % MILLIS_IN_ONE_SECOND;
125 return new Date(currentTime - delta - MILLIS_IN_ONE_SECOND);
126 }
127
128 protected long getMillisOfCurrentPeriodsStart() {
129 long delta = currentTime % MILLIS_IN_ONE_SECOND;
130 return (currentTime - delta);
131 }
132
133 protected void addExpectedFileNamedIfItsTime_ByDate(String fileNamePatternStr) {
134 if (passThresholdTime(nextRolloverThreshold)) {
135 addExpectedFileName_ByDate(fileNamePatternStr, getMillisOfCurrentPeriodsStart());
136 recomputeRolloverThreshold(currentTime);
137 }
138 }
139
140 protected void addExpectedFileName_ByDate(String outputDir, String testId, Date date, boolean gzExtension) {
141
142 String fn = outputDir + testId + "-" + SDF.format(date);
143 if (gzExtension) {
144 fn += ".gz";
145 }
146 expectedFilenameList.add(fn);
147 }
148
149 protected void addExpectedFileName_ByFileIndexCounter(String randomOutputDir, String testId, long millis,
150 int fileIndexCounter, String compressionSuffix) {
151 String fn = randomOutputDir + testId + "-" + SDF.format(millis) + "-" + fileIndexCounter + ".txt"
152 + compressionSuffix;
153 expectedFilenameList.add(fn);
154 }
155
156 protected void addExpectedFileNamedIfItsTime_ByDate(String outputDir, String testId, boolean gzExtension) {
157 if (passThresholdTime(nextRolloverThreshold)) {
158 addExpectedFileName_ByDate(outputDir, testId, getDateOfCurrentPeriodsStart(), gzExtension);
159 recomputeRolloverThreshold(currentTime);
160 }
161 }
162
163 protected void massageExpectedFilesToCorresponToCurrentTarget(String testId,
164 UnaryOperator<String> filenameFunction) {
165 int lastIndex = expectedFilenameList.size() - 1;
166 String last = expectedFilenameList.remove(lastIndex);
167
168 String filename = filenameFunction.apply(testId);
169 if (filename != null) {
170 expectedFilenameList.add(filename);
171 } else if (last.endsWith(".gz")) {
172 int lastLen = last.length();
173 String stem = last.substring(0, lastLen - 3);
174 expectedFilenameList.add(stem);
175 }
176 }
177
178 String addGZIfNotLast(int i) {
179 int lastIndex = expectedFilenameList.size() - 1;
180 if (i != lastIndex) {
181 return ".gz";
182 } else {
183 return "";
184 }
185 }
186
187 protected void checkZipEntryMatchesZipFilename(List<String> expectedFilenameList) throws IOException {
188 for (String filepath : expectedFilenameList) {
189 String stripped = stripStemFromZipFilename(filepath);
190 checkZipEntryName(filepath, stripped);
191 }
192 }
193
194 String stripStemFromZipFilename(String filepath) {
195 File filepathAsFile = new File(filepath);
196 String stem = filepathAsFile.getName();
197 int stemLen = stem.length();
198 return stem.substring(0, stemLen - ".zip".length());
199
200 }
201
202
203 }