16
16
*/
17
17
package org .apache .logging .log4j .core .appender .rolling ;
18
18
19
- import java .io .*;
19
+ import static org .junit .Assert .assertEquals ;
20
+ import static org .junit .Assert .assertNotEquals ;
21
+ import static org .junit .Assert .assertNotNull ;
22
+ import static org .junit .Assert .assertNull ;
23
+ import static org .junit .Assert .fail ;
24
+
25
+ import java .io .File ;
26
+ import java .io .FileInputStream ;
27
+ import java .io .IOException ;
28
+ import java .io .InputStreamReader ;
29
+ import java .io .Reader ;
20
30
import java .nio .charset .StandardCharsets ;
21
31
import org .apache .logging .log4j .core .LoggerContext ;
22
32
import org .apache .logging .log4j .core .appender .RollingFileAppender ;
26
36
import org .apache .logging .log4j .core .layout .PatternLayout ;
27
37
import org .apache .logging .log4j .core .lookup .StrSubstitutor ;
28
38
import org .apache .logging .log4j .core .util .IOUtils ;
29
- import org .junit .Assert ;
30
39
import org .junit .Test ;
40
+ import org .junitpioneer .jupiter .Issue ;
31
41
32
42
public class RollingFileManagerTest {
33
43
@@ -75,14 +85,14 @@ public RolloverDescription rollover(final RollingFileManager manager) throws Sec
75
85
.withPolicy (new SizeBasedTriggeringPolicy (100 ))
76
86
.build ();
77
87
78
- Assert . assertNotNull (appender );
88
+ assertNotNull (appender );
79
89
final String testContent = "Test" ;
80
90
try (final RollingFileManager manager = appender .getManager ()) {
81
- Assert . assertEquals (file .getAbsolutePath (), manager .getFileName ());
91
+ assertEquals (file .getAbsolutePath (), manager .getFileName ());
82
92
manager .writeToDestination (testContent .getBytes (StandardCharsets .US_ASCII ), 0 , testContent .length ());
83
93
}
84
94
try (final Reader reader = new InputStreamReader (new FileInputStream (file ), StandardCharsets .US_ASCII )) {
85
- Assert . assertEquals (testContent , IOUtils .toString (reader ));
95
+ assertEquals (testContent , IOUtils .toString (reader ));
86
96
}
87
97
}
88
98
}
@@ -125,7 +135,7 @@ public RolloverDescription rollover(final RollingFileManager manager) throws Sec
125
135
null ,
126
136
null ,
127
137
configuration );
128
- Assert . assertNotNull (manager );
138
+ assertNotNull (manager );
129
139
manager .initialize ();
130
140
131
141
// Get the initialTime of this original log file
@@ -139,9 +149,43 @@ public RolloverDescription rollover(final RollingFileManager manager) throws Sec
139
149
manager .rollover ();
140
150
141
151
// If the rollover fails, then the size should not be reset
142
- Assert . assertNotEquals (0 , manager .getFileSize ());
152
+ assertNotEquals (0 , manager .getFileSize ());
143
153
144
154
// The initialTime should not have changed
145
- Assert .assertEquals (initialTime , manager .getFileTime ());
155
+ assertEquals (initialTime , manager .getFileTime ());
156
+ }
157
+
158
+ @ Test
159
+ @ Issue ("https://github.com/apache/logging-log4j2/issues/1645" )
160
+ public void testCreateParentDir () {
161
+ final Configuration configuration = new NullConfiguration ();
162
+ final RollingFileManager manager = RollingFileManager .getFileManager (
163
+ null ,
164
+ "testCreateParentDir.log.%d{yyyy-MM-dd}" ,
165
+ true ,
166
+ false ,
167
+ NoOpTriggeringPolicy .INSTANCE ,
168
+ DirectWriteRolloverStrategy .newBuilder ()
169
+ .withConfig (configuration )
170
+ .build (),
171
+ null ,
172
+ PatternLayout .createDefaultLayout (configuration ),
173
+ 0 ,
174
+ true ,
175
+ true ,
176
+ null ,
177
+ null ,
178
+ null ,
179
+ configuration );
180
+ assertNotNull (manager );
181
+ try {
182
+ final File file = new File ("file_in_current_dir.log" );
183
+ assertNull (file .getParentFile ());
184
+ manager .createParentDir (file );
185
+ } catch (final Throwable t ) {
186
+ fail ("createParentDir failed: " + t .getMessage ());
187
+ } finally {
188
+ manager .close ();
189
+ }
146
190
}
147
191
}
0 commit comments