1616 */
1717package org .apache .logging .log4j .core .appender .rolling ;
1818
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 ;
2030import java .nio .charset .StandardCharsets ;
2131import org .apache .logging .log4j .core .LoggerContext ;
2232import org .apache .logging .log4j .core .appender .RollingFileAppender ;
2636import org .apache .logging .log4j .core .layout .PatternLayout ;
2737import org .apache .logging .log4j .core .lookup .StrSubstitutor ;
2838import org .apache .logging .log4j .core .util .IOUtils ;
29- import org .junit .Assert ;
3039import org .junit .Test ;
40+ import org .junitpioneer .jupiter .Issue ;
3141
3242public class RollingFileManagerTest {
3343
@@ -75,14 +85,14 @@ public RolloverDescription rollover(final RollingFileManager manager) throws Sec
7585 .withPolicy (new SizeBasedTriggeringPolicy (100 ))
7686 .build ();
7787
78- Assert . assertNotNull (appender );
88+ assertNotNull (appender );
7989 final String testContent = "Test" ;
8090 try (final RollingFileManager manager = appender .getManager ()) {
81- Assert . assertEquals (file .getAbsolutePath (), manager .getFileName ());
91+ assertEquals (file .getAbsolutePath (), manager .getFileName ());
8292 manager .writeToDestination (testContent .getBytes (StandardCharsets .US_ASCII ), 0 , testContent .length ());
8393 }
8494 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 ));
8696 }
8797 }
8898 }
@@ -125,7 +135,7 @@ public RolloverDescription rollover(final RollingFileManager manager) throws Sec
125135 null ,
126136 null ,
127137 configuration );
128- Assert . assertNotNull (manager );
138+ assertNotNull (manager );
129139 manager .initialize ();
130140
131141 // Get the initialTime of this original log file
@@ -139,9 +149,43 @@ public RolloverDescription rollover(final RollingFileManager manager) throws Sec
139149 manager .rollover ();
140150
141151 // If the rollover fails, then the size should not be reset
142- Assert . assertNotEquals (0 , manager .getFileSize ());
152+ assertNotEquals (0 , manager .getFileSize ());
143153
144154 // 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+ }
146190 }
147191}
0 commit comments