parent
675889a993
commit
c8cd6629d9
|
@ -22,9 +22,9 @@ import static org.hamcrest.CoreMatchers.is;
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileReader;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
|
||||||
import java.nio.file.attribute.FileTime;
|
import java.nio.file.attribute.FileTime;
|
||||||
import java.time.ZoneId;
|
import java.time.ZoneId;
|
||||||
import java.time.ZonedDateTime;
|
import java.time.ZonedDateTime;
|
||||||
|
@ -33,6 +33,7 @@ import java.time.temporal.ChronoUnit;
|
||||||
import java.time.temporal.TemporalAccessor;
|
import java.time.temporal.TemporalAccessor;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import org.eclipse.jetty.toolchain.test.FS;
|
import org.eclipse.jetty.toolchain.test.FS;
|
||||||
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
|
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);
|
ZonedDateTime now = toDateTime("2016.04.10-08:30:12.3 AM AEDT", zone);
|
||||||
|
|
||||||
File template = new File(testDir,"test-rofos-yyyy_mm_dd.log");
|
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 =
|
try (RolloverFileOutputStream rofos =
|
||||||
new RolloverFileOutputStream(template.getAbsolutePath(),false,3,TimeZone.getTimeZone(zone),null,null,now))
|
new RolloverFileOutputStream(template.getAbsolutePath(),false,3,TimeZone.getTimeZone(zone),null,null,now))
|
||||||
{
|
{
|
||||||
|
rofos.write("TOCK".getBytes());
|
||||||
|
rofos.flush();
|
||||||
String[] ls = testDir.list();
|
String[] ls = testDir.list();
|
||||||
assertThat(ls.length,is(1));
|
assertThat(ls.length,is(2));
|
||||||
assertThat(ls[0],is("test-rofos-2016_04_10.log"));
|
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()));
|
Files.setLastModifiedTime(testPath.resolve("test-rofos-2016_04_10.log"),FileTime.from(now.toInstant()));
|
||||||
|
|
||||||
ZonedDateTime time = now.minus(1,ChronoUnit.DAYS);
|
ZonedDateTime time = now.minus(1,ChronoUnit.DAYS);
|
||||||
|
@ -183,19 +204,26 @@ public class RolloverFileOutputStreamTest
|
||||||
Path path = testPath.resolve(file);
|
Path path = testPath.resolve(file);
|
||||||
FS.touch(path);
|
FS.touch(path);
|
||||||
Files.setLastModifiedTime(path,FileTime.from(time.toInstant()));
|
Files.setLastModifiedTime(path,FileTime.from(time.toInstant()));
|
||||||
time = time.minus(1,ChronoUnit.DAYS);
|
|
||||||
}
|
if (i%2==0)
|
||||||
for (int i=10;i-->5;)
|
{
|
||||||
{
|
file = "test-rofos-2016_04_0"+i+".log.083512300";
|
||||||
String file = "unrelated-"+i;
|
path = testPath.resolve(file);
|
||||||
Path 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);
|
FS.touch(path);
|
||||||
Files.setLastModifiedTime(path,FileTime.from(time.toInstant()));
|
Files.setLastModifiedTime(path,FileTime.from(time.toInstant()));
|
||||||
|
|
||||||
time = time.minus(1,ChronoUnit.DAYS);
|
time = time.minus(1,ChronoUnit.DAYS);
|
||||||
}
|
}
|
||||||
|
|
||||||
ls = testDir.list();
|
ls = testDir.list();
|
||||||
assertThat(ls.length,is(11));
|
assertThat(ls.length,is(14));
|
||||||
assertThat(Arrays.asList(ls),Matchers.containsInAnyOrder(
|
assertThat(Arrays.asList(ls),Matchers.containsInAnyOrder(
|
||||||
"test-rofos-2016_04_05.log",
|
"test-rofos-2016_04_05.log",
|
||||||
"test-rofos-2016_04_06.log",
|
"test-rofos-2016_04_06.log",
|
||||||
|
@ -203,6 +231,9 @@ public class RolloverFileOutputStreamTest
|
||||||
"test-rofos-2016_04_08.log",
|
"test-rofos-2016_04_08.log",
|
||||||
"test-rofos-2016_04_09.log",
|
"test-rofos-2016_04_09.log",
|
||||||
"test-rofos-2016_04_10.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-9",
|
||||||
"unrelated-8",
|
"unrelated-8",
|
||||||
"unrelated-7",
|
"unrelated-7",
|
||||||
|
@ -212,16 +243,63 @@ public class RolloverFileOutputStreamTest
|
||||||
|
|
||||||
rofos.removeOldFiles(now);
|
rofos.removeOldFiles(now);
|
||||||
ls = testDir.list();
|
ls = testDir.list();
|
||||||
assertThat(ls.length,is(8));
|
assertThat(ls.length,is(10));
|
||||||
assertThat(Arrays.asList(ls),Matchers.containsInAnyOrder(
|
assertThat(Arrays.asList(ls),Matchers.containsInAnyOrder(
|
||||||
"test-rofos-2016_04_08.log",
|
"test-rofos-2016_04_08.log",
|
||||||
"test-rofos-2016_04_09.log",
|
"test-rofos-2016_04_09.log",
|
||||||
"test-rofos-2016_04_10.log",
|
"test-rofos-2016_04_10.log",
|
||||||
|
"test-rofos-2016_04_08.log.083512300",
|
||||||
|
"test-rofos-2016_04_10.log.083512300",
|
||||||
"unrelated-9",
|
"unrelated-9",
|
||||||
"unrelated-8",
|
"unrelated-8",
|
||||||
"unrelated-7",
|
"unrelated-7",
|
||||||
"unrelated-6",
|
"unrelated-6",
|
||||||
"unrelated-5"));
|
"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