|
1 |
| -package ch.qos.logback.core.rolling |
2 |
| - |
3 |
| -/** |
4 |
| - * Logback: the reliable, generic, fast and flexible logging framework. |
5 |
| - * Copyright (C) 1999-2011, QOS.ch. All rights reserved. |
6 |
| - * |
7 |
| - * This program and the accompanying materials are dual-licensed under |
8 |
| - * either the terms of the Eclipse Public License v1.0 as published by |
9 |
| - * the Eclipse Foundation |
10 |
| - * |
11 |
| - * or (per the licensee's choosing) |
12 |
| - * |
13 |
| - * under the terms of the GNU Lesser General Public License version 2.1 |
14 |
| - * as published by the Free Software Foundation. |
15 |
| - */ |
16 |
| - |
17 |
| -import ch.qos.logback.core.encoder.EchoEncoder |
18 |
| -import ch.qos.logback.core.status.InfoStatus |
19 |
| -import java.util.Date |
20 |
| -import org.junit.{Ignore, Test} |
21 |
| -import org.junit.Before |
22 |
| -import java.io.File |
23 |
| -import ch.qos.logback.core.util.StatusPrinter |
24 |
| - |
25 |
| -/** |
26 |
| - * @author Ceki Gücü |
27 |
| - */ |
28 |
| -class SizeAndTimeBasedFNATP_STest extends RollingScaffolding { |
29 |
| - private var sizeAndTimeBasedFNATP: SizeAndTimeBasedFNATP[AnyRef] = null |
30 |
| - private val rfa1: RollingFileAppender[AnyRef] = new RollingFileAppender[AnyRef] |
31 |
| - private val tbrp1: TimeBasedRollingPolicy[AnyRef] = new TimeBasedRollingPolicy[AnyRef] |
32 |
| - private val rfa2: RollingFileAppender[AnyRef] = new RollingFileAppender[AnyRef] |
33 |
| - private val tbrp2: TimeBasedRollingPolicy[AnyRef] = new TimeBasedRollingPolicy[AnyRef] |
34 |
| - |
35 |
| - private val encoder: EchoEncoder[AnyRef] = new EchoEncoder[AnyRef] |
36 |
| - var fileSize = 0 |
37 |
| - var fileIndexCounter = 0 |
38 |
| - var sizeThreshold = 0 |
39 |
| - |
40 |
| - @Before |
41 |
| - def setUp() { |
42 |
| - setUpScaffolding |
43 |
| - } |
44 |
| - |
45 |
| - private def initRollingFileAppender(rfa: RollingFileAppender[AnyRef], filename: String): Unit = { |
46 |
| - rfa.setContext(context) |
47 |
| - rfa.setEncoder(encoder) |
48 |
| - if (filename != null) { |
49 |
| - rfa.setFile(filename) |
50 |
| - } |
51 |
| - } |
52 |
| - |
53 |
| - private def initPolicies(rfa: RollingFileAppender[AnyRef], tbrp: TimeBasedRollingPolicy[AnyRef], filenamePattern: String, sizeThreshold: Int, givenTime: Long, lastCheck: Long): Unit = { |
54 |
| - sizeAndTimeBasedFNATP = new SizeAndTimeBasedFNATP[AnyRef] |
55 |
| - tbrp.setContext(context) |
56 |
| - sizeAndTimeBasedFNATP.setMaxFileSize("" + sizeThreshold) |
57 |
| - tbrp.setTimeBasedFileNamingAndTriggeringPolicy(sizeAndTimeBasedFNATP) |
58 |
| - tbrp.setFileNamePattern(filenamePattern) |
59 |
| - tbrp.setParent(rfa) |
60 |
| - tbrp.timeBasedFileNamingAndTriggeringPolicy.setCurrentTime(givenTime) |
61 |
| - rfa.setRollingPolicy(tbrp) |
62 |
| - tbrp.start |
63 |
| - rfa.start |
64 |
| - } |
65 |
| - |
66 |
| - private def addExpectedFileNamedIfItsTime(randomOutputDir: String, testId: String, msg: String, compressionSuffix: String) { |
67 |
| - fileSize = fileSize + msg.getBytes.length |
68 |
| - if (passThresholdTime(nextRolloverThreshold)) { |
69 |
| - fileIndexCounter = 0 |
70 |
| - fileSize = 0 |
71 |
| - addExpectedFileName_ByFileIndexCounter(randomOutputDir, testId, getMillisOfCurrentPeriodsStart, fileIndexCounter, compressionSuffix) |
72 |
| - recomputeRolloverThreshold(currentTime) |
73 |
| - return |
74 |
| - } |
75 |
| - |
76 |
| - // windows can delay file size changes, so we only allow for |
77 |
| - // fileIndexCounter 0 |
78 |
| - if ((fileIndexCounter < 1) && fileSize > sizeThreshold) { |
79 |
| - addExpectedFileName_ByFileIndexCounter(randomOutputDir, testId, getMillisOfCurrentPeriodsStart, fileIndexCounter, compressionSuffix) |
80 |
| - fileIndexCounter = fileIndexCounter + 1 |
81 |
| - fileSize = 0 |
82 |
| - } |
83 |
| - } |
84 |
| - |
85 |
| - def generic(testId: String, stem: String, withSecondPhase: Boolean, compressionSuffix: String = "") { |
86 |
| - var file = if (stem != null) randomOutputDir + stem else null |
87 |
| - initRollingFileAppender(rfa1, file) |
88 |
| - sizeThreshold = 300 |
89 |
| - initPolicies(rfa1, tbrp1, randomOutputDir + testId + "-%d{" + DATE_PATTERN_WITH_SECONDS + "}-%i.txt"+compressionSuffix, sizeThreshold, currentTime, 0) |
90 |
| - addExpectedFileName_ByFileIndexCounter(randomOutputDir, testId, getMillisOfCurrentPeriodsStart, fileIndexCounter, compressionSuffix) |
91 |
| - incCurrentTime(100) |
92 |
| - tbrp1.timeBasedFileNamingAndTriggeringPolicy.setCurrentTime(currentTime) |
93 |
| - var runLength: Int = 100 |
94 |
| - var prefix: String = "Hello -----------------" |
95 |
| - |
96 |
| - for (i <- 0 until runLength) { |
97 |
| - var msg: String = prefix + i |
98 |
| - rfa1.doAppend(msg) |
99 |
| - addExpectedFileNamedIfItsTime(randomOutputDir, testId, msg, compressionSuffix) |
100 |
| - incCurrentTime(20) |
101 |
| - tbrp1.timeBasedFileNamingAndTriggeringPolicy.setCurrentTime(currentTime) |
102 |
| - } |
103 |
| - |
104 |
| - |
105 |
| - if(withSecondPhase) { |
106 |
| - secondPhase |
107 |
| - runLength = runLength*2 |
108 |
| - } |
109 |
| - |
110 |
| - if (stem != null) |
111 |
| - massageExpectedFilesToCorresponToCurrentTarget(file, true) |
112 |
| - |
113 |
| - Thread.sleep(20) |
114 |
| - // wait for compression to finish |
115 |
| - if(tbrp1.future != null) { |
116 |
| - tbrp1.future.get() |
117 |
| - } |
118 |
| - |
119 |
| - StatusPrinter.print(context) |
120 |
| - expectedFilenameList.foreach(println(_)) |
121 |
| - existenceCheck(expectedFilenameList) |
122 |
| - sortedContentCheck(randomOutputDir, runLength, prefix) |
123 |
| - |
124 |
| - def secondPhase() { |
125 |
| - rfa1.stop(); |
126 |
| - |
127 |
| - if(stem != null) { |
128 |
| - val f = new File(file); |
129 |
| - f.setLastModified(currentTime); |
130 |
| - } |
131 |
| - |
132 |
| - sm.add(new InfoStatus("Time when rfa1 is stopped: " + new Date(currentTime), this)); |
133 |
| - sm.add(new InfoStatus("currentTime%1000=" + (currentTime % 1000), this)); |
134 |
| - |
135 |
| - initRollingFileAppender(rfa2, file); |
136 |
| - initPolicies(rfa2, tbrp2, randomOutputDir + testId + "-%d{" |
137 |
| - + DATE_PATTERN_WITH_SECONDS + "}-%i.txt"+compressionSuffix, sizeThreshold, currentTime, 0); |
138 |
| - |
139 |
| - for (i <- runLength until runLength * 2) { |
140 |
| - incCurrentTime(100); |
141 |
| - tbrp2.timeBasedFileNamingAndTriggeringPolicy.setCurrentTime(currentTime); |
142 |
| - var msg = prefix + i |
143 |
| - rfa2.doAppend(msg); |
144 |
| - addExpectedFileNamedIfItsTime(randomOutputDir, testId, msg, compressionSuffix); |
145 |
| - } |
146 |
| - } |
147 |
| - } |
148 |
| - |
149 |
| - val FIRST_PHASE_ONLY = false |
150 |
| - val WITH_SECOND_PHASE = true |
151 |
| - |
152 |
| - @Test |
153 |
| - def noCompression_FileSet_NoRestart_1 { |
154 |
| - generic("test1", "toto.log", FIRST_PHASE_ONLY) |
155 |
| - } |
156 |
| - |
157 |
| - @Test |
158 |
| - def noCompression_FileBlank_NoRestart_2 { |
159 |
| - generic("test2", null, FIRST_PHASE_ONLY) |
160 |
| - } |
161 |
| - |
162 |
| - @Test |
163 |
| - def noCompression_FileBlank_WithStopStart_3 { |
164 |
| - generic("test3", null, WITH_SECOND_PHASE) |
165 |
| - } |
166 |
| - |
167 |
| - @Test |
168 |
| - def noCompression_FileSet_WithStopStart_4 { |
169 |
| - generic("test4", "test4.log", WITH_SECOND_PHASE) |
170 |
| - } |
171 |
| - |
172 |
| - @Test |
173 |
| - def withGZCompression_FileSet_NoRestart_5 { |
174 |
| - generic("test5", "toto.log", FIRST_PHASE_ONLY, ".gz") |
175 |
| - } |
176 |
| - |
177 |
| - @Test |
178 |
| - def withGZCompression_FileBlank_NoRestart_6 { |
179 |
| - generic("test6", null, FIRST_PHASE_ONLY, ".gz") |
180 |
| - } |
181 |
| - |
182 |
| - @Test |
183 |
| - def withZipCompression_FileSet_NoRestart_7 { |
184 |
| - generic("test7", "toto.log", FIRST_PHASE_ONLY, ".zip") |
185 |
| - checkZipEntryMatchesZipFilename(expectedFilenameList.filter(_.endsWith(".zip")), "test7-20\\d{2}-\\d{2}-\\d{2}_\\d{2}_\\d{2}_\\d{2}-\\d") |
186 |
| - |
187 |
| - } |
188 |
| -} |
| 1 | +package ch.qos.logback.core.rolling |
| 2 | + |
| 3 | +/** |
| 4 | + * Logback: the reliable, generic, fast and flexible logging framework. |
| 5 | + * Copyright (C) 1999-2011, QOS.ch. All rights reserved. |
| 6 | + * |
| 7 | + * This program and the accompanying materials are dual-licensed under |
| 8 | + * either the terms of the Eclipse Public License v1.0 as published by |
| 9 | + * the Eclipse Foundation |
| 10 | + * |
| 11 | + * or (per the licensee's choosing) |
| 12 | + * |
| 13 | + * under the terms of the GNU Lesser General Public License version 2.1 |
| 14 | + * as published by the Free Software Foundation. |
| 15 | + */ |
| 16 | + |
| 17 | +import ch.qos.logback.core.encoder.EchoEncoder |
| 18 | +import ch.qos.logback.core.status.InfoStatus |
| 19 | +import java.util.Date |
| 20 | +import org.junit.{Ignore, Test} |
| 21 | +import org.junit.Before |
| 22 | +import java.io.File |
| 23 | +import ch.qos.logback.core.util.StatusPrinter |
| 24 | + |
| 25 | +/** |
| 26 | + * @author Ceki Gücü |
| 27 | + */ |
| 28 | +class SizeAndTimeBasedFNATP_STest extends RollingScaffolding { |
| 29 | + private var sizeAndTimeBasedFNATP: SizeAndTimeBasedFNATP[AnyRef] = null |
| 30 | + private val rfa1: RollingFileAppender[AnyRef] = new RollingFileAppender[AnyRef] |
| 31 | + private val tbrp1: TimeBasedRollingPolicy[AnyRef] = new TimeBasedRollingPolicy[AnyRef] |
| 32 | + private val rfa2: RollingFileAppender[AnyRef] = new RollingFileAppender[AnyRef] |
| 33 | + private val tbrp2: TimeBasedRollingPolicy[AnyRef] = new TimeBasedRollingPolicy[AnyRef] |
| 34 | + |
| 35 | + private val encoder: EchoEncoder[AnyRef] = new EchoEncoder[AnyRef] |
| 36 | + var fileSize = 0 |
| 37 | + var fileIndexCounter = 0 |
| 38 | + var sizeThreshold = 0 |
| 39 | + |
| 40 | + @Before |
| 41 | + def setUp() { |
| 42 | + setUpScaffolding |
| 43 | + } |
| 44 | + |
| 45 | + private def initRollingFileAppender(rfa: RollingFileAppender[AnyRef], filename: String): Unit = { |
| 46 | + rfa.setContext(context) |
| 47 | + rfa.setEncoder(encoder) |
| 48 | + if (filename != null) { |
| 49 | + rfa.setFile(filename) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + private def initPolicies(rfa: RollingFileAppender[AnyRef], tbrp: TimeBasedRollingPolicy[AnyRef], filenamePattern: String, sizeThreshold: Int, givenTime: Long, lastCheck: Long): Unit = { |
| 54 | + sizeAndTimeBasedFNATP = new SizeAndTimeBasedFNATP[AnyRef] |
| 55 | + tbrp.setContext(context) |
| 56 | + sizeAndTimeBasedFNATP.setMaxFileSize("" + sizeThreshold) |
| 57 | + tbrp.setTimeBasedFileNamingAndTriggeringPolicy(sizeAndTimeBasedFNATP) |
| 58 | + tbrp.setFileNamePattern(filenamePattern) |
| 59 | + tbrp.setParent(rfa) |
| 60 | + tbrp.timeBasedFileNamingAndTriggeringPolicy.setCurrentTime(givenTime) |
| 61 | + rfa.setRollingPolicy(tbrp) |
| 62 | + tbrp.start |
| 63 | + rfa.start |
| 64 | + } |
| 65 | + |
| 66 | + private def addExpectedFileNamedIfItsTime(randomOutputDir: String, testId: String, msg: String, compressionSuffix: String) { |
| 67 | + fileSize = fileSize + msg.getBytes.length |
| 68 | + if (passThresholdTime(nextRolloverThreshold)) { |
| 69 | + fileIndexCounter = 0 |
| 70 | + fileSize = 0 |
| 71 | + addExpectedFileName_ByFileIndexCounter(randomOutputDir, testId, getMillisOfCurrentPeriodsStart, fileIndexCounter, compressionSuffix) |
| 72 | + recomputeRolloverThreshold(currentTime) |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + // windows can delay file size changes, so we only allow for |
| 77 | + // fileIndexCounter 0 |
| 78 | + if ((fileIndexCounter < 1) && fileSize > sizeThreshold) { |
| 79 | + addExpectedFileName_ByFileIndexCounter(randomOutputDir, testId, getMillisOfCurrentPeriodsStart, fileIndexCounter, compressionSuffix) |
| 80 | + fileIndexCounter = fileIndexCounter + 1 |
| 81 | + fileSize = 0 |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + def generic(testId: String, stem: String, withSecondPhase: Boolean, compressionSuffix: String = "") { |
| 86 | + var file = if (stem != null) randomOutputDir + stem else null |
| 87 | + initRollingFileAppender(rfa1, file) |
| 88 | + sizeThreshold = 300 |
| 89 | + initPolicies(rfa1, tbrp1, randomOutputDir + testId + "-%d{" + DATE_PATTERN_WITH_SECONDS + "}-%i.txt"+compressionSuffix, sizeThreshold, currentTime, 0) |
| 90 | + addExpectedFileName_ByFileIndexCounter(randomOutputDir, testId, getMillisOfCurrentPeriodsStart, fileIndexCounter, compressionSuffix) |
| 91 | + incCurrentTime(100) |
| 92 | + tbrp1.timeBasedFileNamingAndTriggeringPolicy.setCurrentTime(currentTime) |
| 93 | + var runLength: Int = 100 |
| 94 | + var prefix: String = "Hello -----------------" |
| 95 | + |
| 96 | + for (i <- 0 until runLength) { |
| 97 | + var msg: String = prefix + i |
| 98 | + rfa1.doAppend(msg) |
| 99 | + addExpectedFileNamedIfItsTime(randomOutputDir, testId, msg, compressionSuffix) |
| 100 | + incCurrentTime(20) |
| 101 | + tbrp1.timeBasedFileNamingAndTriggeringPolicy.setCurrentTime(currentTime) |
| 102 | + } |
| 103 | + |
| 104 | + |
| 105 | + if(withSecondPhase) { |
| 106 | + secondPhase |
| 107 | + runLength = runLength*2 |
| 108 | + } |
| 109 | + |
| 110 | + if (stem != null) |
| 111 | + massageExpectedFilesToCorresponToCurrentTarget(file, true) |
| 112 | + |
| 113 | + Thread.sleep(20) |
| 114 | + // wait for compression to finish |
| 115 | + if(tbrp1.future != null) { |
| 116 | + tbrp1.future.get() |
| 117 | + } |
| 118 | + |
| 119 | + StatusPrinter.print(context) |
| 120 | + expectedFilenameList.foreach(println(_)) |
| 121 | + existenceCheck(expectedFilenameList) |
| 122 | + sortedContentCheck(randomOutputDir, runLength, prefix) |
| 123 | + |
| 124 | + def secondPhase() { |
| 125 | + rfa1.stop(); |
| 126 | + |
| 127 | + if(stem != null) { |
| 128 | + val f = new File(file); |
| 129 | + f.setLastModified(currentTime); |
| 130 | + } |
| 131 | + |
| 132 | + sm.add(new InfoStatus("Time when rfa1 is stopped: " + new Date(currentTime), this)); |
| 133 | + sm.add(new InfoStatus("currentTime%1000=" + (currentTime % 1000), this)); |
| 134 | + |
| 135 | + initRollingFileAppender(rfa2, file); |
| 136 | + initPolicies(rfa2, tbrp2, randomOutputDir + testId + "-%d{" |
| 137 | + + DATE_PATTERN_WITH_SECONDS + "}-%i.txt"+compressionSuffix, sizeThreshold, currentTime, 0); |
| 138 | + |
| 139 | + for (i <- runLength until runLength * 2) { |
| 140 | + incCurrentTime(100); |
| 141 | + tbrp2.timeBasedFileNamingAndTriggeringPolicy.setCurrentTime(currentTime); |
| 142 | + var msg = prefix + i |
| 143 | + rfa2.doAppend(msg); |
| 144 | + addExpectedFileNamedIfItsTime(randomOutputDir, testId, msg, compressionSuffix); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + val FIRST_PHASE_ONLY = false |
| 150 | + val WITH_SECOND_PHASE = true |
| 151 | + |
| 152 | + @Test |
| 153 | + def noCompression_FileSet_NoRestart_1 { |
| 154 | + generic("test1", "toto.log", FIRST_PHASE_ONLY) |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + def noCompression_FileBlank_NoRestart_2 { |
| 159 | + generic("test2", null, FIRST_PHASE_ONLY) |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + def noCompression_FileBlank_WithStopStart_3 { |
| 164 | + generic("test3", null, WITH_SECOND_PHASE) |
| 165 | + } |
| 166 | + |
| 167 | + @Test |
| 168 | + def noCompression_FileSet_WithStopStart_4 { |
| 169 | + generic("test4", "test4.log", WITH_SECOND_PHASE) |
| 170 | + } |
| 171 | + |
| 172 | + @Test |
| 173 | + def withGZCompression_FileSet_NoRestart_5 { |
| 174 | + generic("test5", "toto.log", FIRST_PHASE_ONLY, ".gz") |
| 175 | + } |
| 176 | + |
| 177 | + @Test |
| 178 | + def withGZCompression_FileBlank_NoRestart_6 { |
| 179 | + generic("test6", null, FIRST_PHASE_ONLY, ".gz") |
| 180 | + } |
| 181 | + |
| 182 | + @Test |
| 183 | + def withZipCompression_FileSet_NoRestart_7 { |
| 184 | + generic("test7", "toto.log", FIRST_PHASE_ONLY, ".zip") |
| 185 | + checkZipEntryMatchesZipFilename(expectedFilenameList.filter(_.endsWith(".zip")), "test7-20\\d{2}-\\d{2}-\\d{2}_\\d{2}_\\d{2}_\\d{2}-\\d") |
| 186 | + |
| 187 | + } |
| 188 | +} |
0 commit comments