parent
1f8b48cb83
commit
f2721d3407
|
@ -267,7 +267,7 @@ public class RolloverFileOutputStream extends FilterOutputStream
|
|||
// Yep
|
||||
_file=file;
|
||||
if (!_append && file.exists())
|
||||
file.renameTo(new File(file.toString()+"."+_fileBackupFormat.format(now)));
|
||||
file.renameTo(new File(file.toString()+"."+_fileBackupFormat.format(new Date(now.toInstant().toEpochMilli()))));
|
||||
OutputStream oldOut=out;
|
||||
out=new FileOutputStream(file.toString(),_append);
|
||||
if (oldOut!=null)
|
||||
|
|
|
@ -22,9 +22,9 @@ import static org.hamcrest.CoreMatchers.is;
|
|||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.attribute.FileTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
|
@ -33,6 +33,7 @@ import java.time.temporal.ChronoUnit;
|
|||
import java.time.temporal.TemporalAccessor;
|
||||
import java.util.Arrays;
|
||||
import java.util.TimeZone;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.eclipse.jetty.toolchain.test.FS;
|
||||
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
|
||||
|
@ -167,13 +168,33 @@ public class RolloverFileOutputStreamTest
|
|||
ZonedDateTime now = toDateTime("2016.04.10-08:30:12.3 AM AEDT", zone);
|
||||
|
||||
File template = new File(testDir,"test-rofos-yyyy_mm_dd.log");
|
||||
|
||||
try (RolloverFileOutputStream rofos =
|
||||
new RolloverFileOutputStream(template.getAbsolutePath(),false,3,TimeZone.getTimeZone(zone),null,null,now))
|
||||
{
|
||||
rofos.write("TICK".getBytes());
|
||||
rofos.flush();
|
||||
}
|
||||
|
||||
now = now.plus(5,ChronoUnit.MINUTES);
|
||||
|
||||
try (RolloverFileOutputStream rofos =
|
||||
new RolloverFileOutputStream(template.getAbsolutePath(),false,3,TimeZone.getTimeZone(zone),null,null,now))
|
||||
{
|
||||
rofos.write("TOCK".getBytes());
|
||||
rofos.flush();
|
||||
String[] ls = testDir.list();
|
||||
assertThat(ls.length,is(1));
|
||||
assertThat(ls[0],is("test-rofos-2016_04_10.log"));
|
||||
assertThat(ls.length,is(2));
|
||||
String backup = null;
|
||||
for (String n: ls)
|
||||
{
|
||||
if (!"test-rofos-2016_04_10.log".equals(n))
|
||||
backup = n;
|
||||
}
|
||||
|
||||
assertThat(Arrays.asList(ls),Matchers.containsInAnyOrder(backup,"test-rofos-2016_04_10.log"));
|
||||
|
||||
Files.setLastModifiedTime(testPath.resolve(backup),FileTime.from(now.toInstant()));
|
||||
Files.setLastModifiedTime(testPath.resolve("test-rofos-2016_04_10.log"),FileTime.from(now.toInstant()));
|
||||
|
||||
ZonedDateTime time = now.minus(1,ChronoUnit.DAYS);
|
||||
|
@ -183,19 +204,26 @@ public class RolloverFileOutputStreamTest
|
|||
Path path = testPath.resolve(file);
|
||||
FS.touch(path);
|
||||
Files.setLastModifiedTime(path,FileTime.from(time.toInstant()));
|
||||
time = time.minus(1,ChronoUnit.DAYS);
|
||||
}
|
||||
for (int i=10;i-->5;)
|
||||
{
|
||||
String file = "unrelated-"+i;
|
||||
Path path = testPath.resolve(file);
|
||||
|
||||
if (i%2==0)
|
||||
{
|
||||
file = "test-rofos-2016_04_0"+i+".log.083512300";
|
||||
path = testPath.resolve(file);
|
||||
FS.touch(path);
|
||||
Files.setLastModifiedTime(path,FileTime.from(time.toInstant()));
|
||||
time = time.minus(1,ChronoUnit.DAYS);
|
||||
}
|
||||
|
||||
file = "unrelated-"+i;
|
||||
path = testPath.resolve(file);
|
||||
FS.touch(path);
|
||||
Files.setLastModifiedTime(path,FileTime.from(time.toInstant()));
|
||||
|
||||
time = time.minus(1,ChronoUnit.DAYS);
|
||||
}
|
||||
|
||||
ls = testDir.list();
|
||||
assertThat(ls.length,is(11));
|
||||
assertThat(ls.length,is(14));
|
||||
assertThat(Arrays.asList(ls),Matchers.containsInAnyOrder(
|
||||
"test-rofos-2016_04_05.log",
|
||||
"test-rofos-2016_04_06.log",
|
||||
|
@ -203,6 +231,9 @@ public class RolloverFileOutputStreamTest
|
|||
"test-rofos-2016_04_08.log",
|
||||
"test-rofos-2016_04_09.log",
|
||||
"test-rofos-2016_04_10.log",
|
||||
"test-rofos-2016_04_06.log.083512300",
|
||||
"test-rofos-2016_04_08.log.083512300",
|
||||
"test-rofos-2016_04_10.log.083512300",
|
||||
"unrelated-9",
|
||||
"unrelated-8",
|
||||
"unrelated-7",
|
||||
|
@ -212,16 +243,63 @@ public class RolloverFileOutputStreamTest
|
|||
|
||||
rofos.removeOldFiles(now);
|
||||
ls = testDir.list();
|
||||
assertThat(ls.length,is(8));
|
||||
assertThat(ls.length,is(10));
|
||||
assertThat(Arrays.asList(ls),Matchers.containsInAnyOrder(
|
||||
"test-rofos-2016_04_08.log",
|
||||
"test-rofos-2016_04_09.log",
|
||||
"test-rofos-2016_04_10.log",
|
||||
"test-rofos-2016_04_08.log.083512300",
|
||||
"test-rofos-2016_04_10.log.083512300",
|
||||
"unrelated-9",
|
||||
"unrelated-8",
|
||||
"unrelated-7",
|
||||
"unrelated-6",
|
||||
"unrelated-5"));
|
||||
|
||||
|
||||
assertThat(IO.toString(new FileReader(new File(testDir,backup))),is("TICK"));
|
||||
assertThat(IO.toString(new FileReader(new File(testDir,"test-rofos-2016_04_10.log"))),is("TOCK"));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRollover() throws Exception
|
||||
{
|
||||
File testDir = MavenTestingUtils.getTargetTestingDir(ResourceTest.class.getName());
|
||||
FS.ensureEmpty(testDir);
|
||||
|
||||
ZoneId zone = toZoneId("Australia/Sydney");
|
||||
ZonedDateTime now = toDateTime("2016.04.10-11:59:55.0 PM AEDT", zone);
|
||||
|
||||
File template = new File(testDir,"test-rofos-yyyy_mm_dd.log");
|
||||
|
||||
try (RolloverFileOutputStream rofos =
|
||||
new RolloverFileOutputStream(template.getAbsolutePath(),false,0,TimeZone.getTimeZone(zone),null,null,now))
|
||||
{
|
||||
rofos.write("BEFORE".getBytes());
|
||||
rofos.flush();
|
||||
String[] ls = testDir.list();
|
||||
assertThat(ls.length,is(1));
|
||||
assertThat(ls[0],is("test-rofos-2016_04_10.log"));
|
||||
|
||||
TimeUnit.SECONDS.sleep(10);
|
||||
rofos.write("AFTER".getBytes());
|
||||
ls = testDir.list();
|
||||
assertThat(ls.length,is(2));
|
||||
|
||||
for (String n : ls)
|
||||
{
|
||||
String content = IO.toString(new FileReader(new File(testDir,n)));
|
||||
if ("test-rofos-2016_04_10.log".equals(n))
|
||||
{
|
||||
assertThat(content,is("BEFORE"));
|
||||
}
|
||||
else
|
||||
{
|
||||
assertThat(content,is("AFTER"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue