2121import ch .qos .logback .core .testUtil .CoreTestConstants ;
2222import ch .qos .logback .core .testUtil .RandomUtil ;
2323import ch .qos .logback .core .status .testUtil .StatusChecker ;
24+ import ch .qos .logback .core .util .EnvUtil ;
2425import ch .qos .logback .core .util .StatusPrinter ;
2526
27+ import ch .qos .logback .core .util .StatusPrinter2 ;
2628import org .junit .jupiter .api .BeforeEach ;
2729import org .junit .jupiter .api .Disabled ;
2830import org .junit .jupiter .api .Test ;
3234import java .io .FileNotFoundException ;
3335import java .io .FileOutputStream ;
3436import java .io .IOException ;
37+ import java .nio .channels .FileLock ;
3538
3639import static org .junit .jupiter .api .Assertions .assertEquals ;
3740import static org .junit .jupiter .api .Assertions .assertTrue ;
@@ -41,6 +44,7 @@ public class RenameUtilTest {
4144 Encoder <Object > encoder ;
4245 Context context = new ContextBase ();
4346 StatusChecker statusChecker = new StatusChecker (context );
47+ StatusPrinter2 statusPrinter2 = new StatusPrinter2 ();
4448
4549 long currentTime = System .currentTimeMillis ();
4650 int diff = RandomUtil .getPositiveInt ();
@@ -66,12 +70,18 @@ public void renameToNonExistingDirectory() throws IOException, RolloverFailure {
6670 String randomTARGETDir = CoreTestConstants .OUTPUT_DIR_PREFIX + diff2 ;
6771
6872 renameUtil .rename (fromFile .toString (), new File (randomTARGETDir + "/to.test" ).toString ());
69- StatusPrinter .printInCaseOfErrorsOrWarnings (context );
73+ statusPrinter2 .printInCaseOfErrorsOrWarnings (context );
7074 assertTrue (statusChecker .isErrorFree (0 ));
7175 }
7276
7377 @ Test // LOGBACK-1054
7478 public void renameLockedAbstractFile_LOGBACK_1054 () throws IOException , RolloverFailure {
79+
80+ // this tests only works on windows
81+ if (!EnvUtil .isWindows ()) {
82+ return ;
83+ }
84+
7585 RenameUtil renameUtil = new RenameUtil ();
7686 renameUtil .setContext (context );
7787
@@ -82,12 +92,15 @@ public void renameLockedAbstractFile_LOGBACK_1054() throws IOException, Rollover
8292
8393 makeFile (src );
8494
85- FileInputStream fisLock = new FileInputStream (src );
95+ // open file in a way preventing simple rename in order to force call to
96+ // areOnDifferentVolumes() method. This only works on Windows
97+
98+ FileInputStream fis = new FileInputStream (src );
8699 renameUtil .rename (src , target );
87100 // release the lock
88- fisLock .close ();
101+ fis .close ();
89102
90- StatusPrinter .print (context );
103+ statusPrinter2 .print (context );
91104 assertEquals (0 , statusChecker .matchCount ("Parent of target file ." + target + ". is null" ));
92105 }
93106
@@ -101,7 +114,7 @@ public void MANUAL_renamingOnDifferentVolumesOnLinux() throws IOException, Rollo
101114 makeFile (src );
102115
103116 renameUtil .rename (src , "/tmp/foo" + diff + ".txt" );
104- StatusPrinter .print (context );
117+ statusPrinter2 .print (context );
105118 }
106119
107120 @ Test
@@ -114,7 +127,7 @@ public void MANUAL_renamingOnDifferentVolumesOnWindows() throws IOException, Rol
114127 makeFile (src );
115128
116129 renameUtil .rename (src , "d:/tmp/foo" + diff + ".txt" );
117- StatusPrinter .print (context );
130+ statusPrinter2 .print (context );
118131 assertTrue (statusChecker .isErrorFree (0 ));
119132 }
120133
0 commit comments