publicstatic FileHandler testFileHandlerLimit(FileHandlerSupplier supplier, long limit) throws Exception {
Configure.doPrivileged(() -> { try {
Files.deleteIfExists(Paths.get(PREFIX));
} catch (IOException x) { thrownew RuntimeException(x);
}
}); final FileHandler fh = supplier.test(); try { // verify we have the expected limit
assertEquals(limit, getLimit(fh), "limit");
// get the metered output stream
OutputStream metered = getMeteredOutput(fh);
// we don't want to actually write to the file, so let's // redirect the metered to our own TestOutputStream.
setTestOutputStream(metered);
// check that fh.meter.written is 0
assertEquals(0, getWritten(metered), "written");
// now we're going to publish a series of log records // we're using the same log record over and over to make // sure we get the same amount of bytes.
String msg = "this is at least 10 chars long";
LogRecord record = new LogRecord(Level.SEVERE, msg);
fh.publish(record);
fh.flush(); long w = getWritten(metered); long offset = getWritten(metered);
System.out.println("first offset is: " + offset);
fh.publish(record);
fh.flush();
offset = getWritten(metered) - w;
w = getWritten(metered);
System.out.println("second offset is: " + offset);
fh.publish(record);
fh.flush();
offset = getWritten(metered) - w;
w = getWritten(metered);
System.out.println("third offset is: " + offset);
// Now set fh.meter.written to something close to the limit, // so that we can trigger log file rotation.
assertEquals(limit-2*offset+10, setWritten(metered, limit-2*offset+10), "written");
w = getWritten(metered);
// publish one more log record. we should still be just beneath // the limit
fh.publish(record);
fh.flush();
assertEquals(w+offset, getWritten(metered), "written");
// check that fh still has the same MeteredStream - indicating // that the file hasn't rotated. if (getMeteredOutput(fh) != metered) { thrownew RuntimeException("Log should not have rotated");
}
// Now publish two log record. The spec is a bit vague about when // exactly the log will be rotated - it could happen just after // writing the first log record or just before writing the next // one. We publich two - so we're sure that the log must have // rotated.
fh.publish(record);
fh.flush();
fh.publish(record);
fh.flush();
// Check that fh.meter is a different instance of MeteredStream. if (getMeteredOutput(fh) == metered) { thrownew RuntimeException("Log should have rotated");
} // success! return fh;
} catch (Error | Exception x) { // if we get an exception we need to close fh. // if we don't get an exception, fh will be closed by the caller. // (and that's why we dont use try-with-resources/finally here). try { fh.close(); } catch(Throwable t) {t.printStackTrace();} throw x;
}
}
if (userDirWritable || expectedException != null) { // These calls will create files in user.dir. // The file name contain a random UUID (PREFIX) which identifies them // and allow us to remove them cleanly at the end (see finally block // in main()).
checkException(expectedException, () -> new FileHandler());
checkException(expectedException, () -> { final FileHandler fh = new FileHandler();
assertEquals(limit, getLimit(fh), "limit"); return fh;
});
checkException(expectedException, () -> testFileHandlerLimit(
() -> new FileHandler(),
limit));
checkException(expectedException, () -> testFileHandlerLimit(
() -> new FileHandler(PREFIX, Long.MAX_VALUE, 1, true), Long.MAX_VALUE));
}
}
staticfinalclass PermissionsBuilder { final Permissions perms; public PermissionsBuilder() { this(new Permissions());
} public PermissionsBuilder(Permissions perms) { this.perms = perms;
} public PermissionsBuilder add(Permission p) {
perms.add(p); returnthis;
} public PermissionsBuilder addAll(PermissionCollection col) { if (col != null) { for (Enumeration<Permission> e = col.elements(); e.hasMoreElements(); ) {
perms.add(e.nextElement());
}
} returnthis;
} public Permissions toPermissions() { final PermissionsBuilder builder = new PermissionsBuilder();
builder.addAll(perms); return builder.perms;
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.