|
15 | 15 |
|
16 | 16 | import java.io.File;
|
17 | 17 | import java.io.IOException;
|
| 18 | +import java.nio.file.Files; |
| 19 | +import java.nio.file.Paths; |
| 20 | +import java.text.SimpleDateFormat; |
18 | 21 | import java.util.Date;
|
19 | 22 | import java.util.List;
|
20 | 23 | import java.util.concurrent.ExecutionException;
|
21 | 24 | import java.util.function.UnaryOperator;
|
22 | 25 |
|
| 26 | +import ch.qos.logback.core.CoreConstants; |
| 27 | +import ch.qos.logback.core.util.CachingDateFormatter; |
23 | 28 | import ch.qos.logback.core.util.Duration;
|
| 29 | +import ch.qos.logback.core.util.StatusPrinter; |
24 | 30 | import org.junit.jupiter.api.BeforeEach;
|
25 | 31 | import org.junit.jupiter.api.Test;
|
26 | 32 |
|
|
31 | 37 | import ch.qos.logback.core.status.testUtil.StatusChecker;
|
32 | 38 | import ch.qos.logback.core.util.FileSize;
|
33 | 39 |
|
| 40 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
34 | 41 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
35 | 42 |
|
36 | 43 | public class SizeAndTimeBasedFNATP_Test extends ScaffoldingForRollingTests {
|
@@ -61,6 +68,7 @@ private void initRollingFileAppender(RollingFileAppender<Object> rfa, String fil
|
61 | 68 | private void initPolicies(RollingFileAppender<Object> rfa, TimeBasedRollingPolicy<Object> tbrp,
|
62 | 69 | String filenamePattern, int sizeThreshold, long givenTime, long lastCheck) {
|
63 | 70 | sizeAndTimeBasedFNATP = new SizeAndTimeBasedFileNamingAndTriggeringPolicy<Object>();
|
| 71 | + sizeAndTimeBasedFNATP.setContext(context); |
64 | 72 | sizeAndTimeBasedFNATP.setCheckIncrement(Duration.buildByMilliseconds(10));
|
65 | 73 | tbrp.setContext(context);
|
66 | 74 | sizeAndTimeBasedFNATP.setMaxFileSize(new FileSize(sizeThreshold));
|
@@ -243,6 +251,53 @@ public void checkDateCollision() {
|
243 | 251 | checker.assertContainsMatch("The date format in FileNamePattern");
|
244 | 252 | }
|
245 | 253 |
|
| 254 | + @Test |
| 255 | + public void checkInitialFileSize_withFile() throws IOException { |
| 256 | + String stem = "foo.log"; |
| 257 | + String testId = "checkDateCollision"; |
| 258 | + String fixedContent = "Hello world"; |
| 259 | + byte[] fixedContentBytes = fixedContent.getBytes(); |
| 260 | + |
| 261 | + String fileProperty = randomOutputDir + stem; |
| 262 | + Files.createDirectories(Paths.get(randomOutputDir)); |
| 263 | + Files.write(Paths.get(fileProperty), fixedContentBytes); |
| 264 | + |
| 265 | + initRollingFileAppender(rfa1, fileProperty); |
| 266 | + sizeThreshold = 300; |
| 267 | + initPolicies(rfa1, tbrp1, randomOutputDir + testId + "-%d-%i.txt", sizeThreshold, |
| 268 | + currentTime, 0); |
| 269 | + |
| 270 | + //StatusPrinter.print(context); |
| 271 | + |
| 272 | + assertEquals(fixedContentBytes.length, tbrp1.getLengthCounter().getLength()); |
| 273 | + } |
| 274 | + |
| 275 | + |
| 276 | + @Test |
| 277 | + public void checkInitialFileSize_withoutFile() throws IOException { |
| 278 | + String testId = "checkInitialFileSize_withoutFile"; |
| 279 | + String fixedContent = "Hello world"; |
| 280 | + byte[] fixedContentBytes = fixedContent.getBytes(); |
| 281 | + |
| 282 | + |
| 283 | + CachingDateFormatter cdf = new CachingDateFormatter(CoreConstants.DAILY_DATE_PATTERN); |
| 284 | + String nowString = cdf.format(currentTime); |
| 285 | + String pathToFirstFile = randomOutputDir + testId + "-"+nowString+"-0.txt"; |
| 286 | + |
| 287 | + Files.createDirectories(Paths.get(randomOutputDir)); |
| 288 | + Files.write(Paths.get(pathToFirstFile), fixedContentBytes); |
| 289 | + |
| 290 | + |
| 291 | + initRollingFileAppender(rfa1, null); |
| 292 | + sizeThreshold = 300; |
| 293 | + initPolicies(rfa1, tbrp1, randomOutputDir + testId + "-%d-%i.txt", sizeThreshold, |
| 294 | + currentTime, 0); |
| 295 | + |
| 296 | + StatusPrinter.print(context); |
| 297 | + |
| 298 | + assertEquals(fixedContentBytes.length, tbrp1.getLengthCounter().getLength()); |
| 299 | + } |
| 300 | + |
246 | 301 | // @Test
|
247 | 302 | // public void testHistoryAsFileCount() throws IOException {
|
248 | 303 | // String testId = "testHistoryAsFileCount";
|
|
0 commit comments