下面列出了java.util.logging.SimpleFormatter#format ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Deserializes the Base64 encoded string returned by {@link
* #getBase64()}, format the obtained LogRecord using a
* SimpleFormatter, and checks that the string representation obtained
* matches the original string representation returned by {@link
* #getString()}.
*/
protected void dotest() {
try {
final String base64 = getBase64();
final ByteArrayInputStream bais =
new ByteArrayInputStream(Base64.getDecoder().decode(base64));
final ObjectInputStream ois = new ObjectInputStream(bais);
final LogRecord record = (LogRecord)ois.readObject();
final SimpleFormatter formatter = new SimpleFormatter();
String expected = getString();
String str2 = formatter.format(record);
check(expected, str2);
System.out.println(str2);
System.out.println("PASSED: "+this.getClass().getName()+"\n");
} catch (IOException | ClassNotFoundException x) {
throw new RuntimeException(x);
}
}
/**
* Deserializes the Base64 encoded string returned by {@link
* #getBase64()}, format the obtained LogRecord using a
* SimpleFormatter, and checks that the string representation obtained
* matches the original string representation returned by {@link
* #getString()}.
*/
protected void dotest() {
try {
final String base64 = getBase64();
final ByteArrayInputStream bais =
new ByteArrayInputStream(Base64.getDecoder().decode(base64));
final ObjectInputStream ois = new ObjectInputStream(bais);
final LogRecord record = (LogRecord)ois.readObject();
final SimpleFormatter formatter = new SimpleFormatter();
String expected = getString();
String str2 = formatter.format(record);
check(expected, str2);
System.out.println(str2);
System.out.println("PASSED: "+this.getClass().getName()+"\n");
} catch (IOException | ClassNotFoundException x) {
throw new RuntimeException(x);
}
}
/**
* Deserializes the Base64 encoded string returned by {@link
* #getBase64()}, format the obtained LogRecord using a
* SimpleFormatter, and checks that the string representation obtained
* matches the original string representation returned by {@link
* #getString()}.
*/
protected void dotest() {
try {
final String base64 = getBase64();
final ByteArrayInputStream bais =
new ByteArrayInputStream(Base64.getDecoder().decode(base64));
final ObjectInputStream ois = new ObjectInputStream(bais);
final LogRecord record = (LogRecord)ois.readObject();
final SimpleFormatter formatter = new SimpleFormatter();
String expected = getString();
String str2 = formatter.format(record);
check(expected, str2);
System.out.println(str2);
System.out.println("PASSED: "+this.getClass().getName()+"\n");
} catch (IOException | ClassNotFoundException x) {
throw new RuntimeException(x);
}
}
/**
* Deserializes the Base64 encoded string returned by {@link
* #getBase64()}, format the obtained LogRecord using a
* SimpleFormatter, and checks that the string representation obtained
* matches the original string representation returned by {@link
* #getString()}.
*/
protected void dotest() {
try {
final String base64 = getBase64();
final ByteArrayInputStream bais =
new ByteArrayInputStream(Base64.getDecoder().decode(base64));
final ObjectInputStream ois = new ObjectInputStream(bais);
final LogRecord record = (LogRecord)ois.readObject();
final SimpleFormatter formatter = new SimpleFormatter();
String expected = getString();
String str2 = formatter.format(record);
check(expected, str2);
System.out.println(str2);
System.out.println("PASSED: "+this.getClass().getName()+"\n");
} catch (IOException | ClassNotFoundException x) {
throw new RuntimeException(x);
}
}
/**
* Deserializes the Base64 encoded string returned by {@link
* #getBase64()}, format the obtained LogRecord using a
* SimpleFormatter, and checks that the string representation obtained
* matches the original string representation returned by {@link
* #getString()}.
*/
protected void dotest() {
try {
final String base64 = getBase64();
final ByteArrayInputStream bais =
new ByteArrayInputStream(Base64.getDecoder().decode(base64));
final ObjectInputStream ois = new ObjectInputStream(bais);
final LogRecord record = (LogRecord)ois.readObject();
final SimpleFormatter formatter = new SimpleFormatter();
String expected = getString();
String str2 = formatter.format(record);
check(expected, str2);
System.out.println(str2);
System.out.println("PASSED: "+this.getClass().getName()+"\n");
} catch (IOException | ClassNotFoundException x) {
throw new RuntimeException(x);
}
}
/**
* Deserializes the Base64 encoded string returned by {@link
* #getBase64()}, format the obtained LogRecord using a
* SimpleFormatter, and checks that the string representation obtained
* matches the original string representation returned by {@link
* #getString()}.
*/
protected void dotest() {
try {
final String base64 = getBase64();
final ByteArrayInputStream bais =
new ByteArrayInputStream(Base64.getDecoder().decode(base64));
final ObjectInputStream ois = new ObjectInputStream(bais);
final LogRecord record = (LogRecord)ois.readObject();
final SimpleFormatter formatter = new SimpleFormatter();
String expected = getString();
String str2 = formatter.format(record);
check(expected, str2);
System.out.println(str2);
System.out.println("PASSED: "+this.getClass().getName()+"\n");
} catch (IOException | ClassNotFoundException x) {
throw new RuntimeException(x);
}
}
/**
* Deserializes the Base64 encoded string returned by {@link
* #getBase64()}, format the obtained LogRecord using a
* SimpleFormatter, and checks that the string representation obtained
* matches the original string representation returned by {@link
* #getString()}.
*/
protected void dotest() {
try {
final String base64 = getBase64();
final ByteArrayInputStream bais =
new ByteArrayInputStream(Base64.getDecoder().decode(base64));
final ObjectInputStream ois = new ObjectInputStream(bais);
final LogRecord record = (LogRecord)ois.readObject();
final SimpleFormatter formatter = new SimpleFormatter();
String expected = getString();
String str2 = formatter.format(record);
check(expected, str2);
System.out.println(str2);
System.out.println("PASSED: "+this.getClass().getName()+"\n");
} catch (IOException | ClassNotFoundException x) {
throw new RuntimeException(x);
}
}
/**
* Deserializes the Base64 encoded string returned by {@link
* #getBase64()}, format the obtained LogRecord using a
* SimpleFormatter, and checks that the string representation obtained
* matches the original string representation returned by {@link
* #getString()}.
*/
protected void dotest() {
try {
final String base64 = getBase64();
final ByteArrayInputStream bais =
new ByteArrayInputStream(Base64.getDecoder().decode(base64));
final ObjectInputStream ois = new ObjectInputStream(bais);
final LogRecord record = (LogRecord)ois.readObject();
final SimpleFormatter formatter = new SimpleFormatter();
String expected = getString();
String str2 = formatter.format(record);
check(expected, str2);
System.out.println(str2);
System.out.println("PASSED: "+this.getClass().getName()+"\n");
} catch (IOException | ClassNotFoundException x) {
throw new RuntimeException(x);
}
}
/**
* Serializes a log record, encode the serialized bytes in base 64, and
* prints pseudo java code that can be cut and pasted into this test.
* @param record the log record to serialize, encode in base 64, and for
* which test data will be generated.
* @return A string containing the generated pseudo java code.
* @throws IOException Unexpected.
* @throws ClassNotFoundException Unexpected.
*/
public static String generate(LogRecord record) throws IOException, ClassNotFoundException {
// Format the given logRecord using the SimpleFormatter
SimpleFormatter formatter = new SimpleFormatter();
String str = formatter.format(record);
// Serialize the given LogRecord
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(record);
oos.flush();
oos.close();
// Now we're going to perform a number of smoke tests before
// generating the Java pseudo code.
//
// First checks that the log record can be deserialized
final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
final ObjectInputStream ois = new ObjectInputStream(bais);
final LogRecord record2 = (LogRecord)ois.readObject();
// Format the deserialized LogRecord using the SimpleFormatter, and
// check that the string representation obtained matches the string
// representation of the original LogRecord
String str2 = formatter.format(record2);
if (!str.equals(str2)) throw new RuntimeException("Unexpected values in deserialized object:"
+ "\n\tExpected: " + str
+ "\n\tRetrieved: "+str);
// Now get a Base64 string representation of the serialized bytes.
final String base64 = Base64.getEncoder().encodeToString(baos.toByteArray());
// Check that we can deserialize a log record from the Base64 string
// representation we just computed.
final ByteArrayInputStream bais2 = new ByteArrayInputStream(Base64.getDecoder().decode(base64));
final ObjectInputStream ois2 = new ObjectInputStream(bais2);
final LogRecord record3 = (LogRecord)ois2.readObject();
// Format the new deserialized LogRecord using the SimpleFormatter, and
// check that the string representation obtained matches the string
// representation of the original LogRecord
String str3 = formatter.format(record3);
if (!str.equals(str3)) throw new RuntimeException("Unexpected values in deserialized object:"
+ "\n\tExpected: " + str
+ "\n\tRetrieved: "+str);
//System.out.println(base64);
//System.out.println();
// Generates the Java Pseudo code that can be cut & pasted into
// this test (see Jdk8SerializedLog and Jdk9SerializedLog below)
final StringBuilder sb = new StringBuilder();
sb.append(" /**").append('\n');
sb.append(" * Base64 encoded string for LogRecord object.").append('\n');
sb.append(" * Java version: ").append(System.getProperty("java.version")).append('\n');
sb.append(" **/").append('\n');
sb.append(" final String base64 = ").append("\n ");
final int last = base64.length() - 1;
for (int i=0; i<base64.length();i++) {
if (i%64 == 0) sb.append("\"");
sb.append(base64.charAt(i));
if (i%64 == 63 || i == last) {
sb.append("\"");
if (i == last) sb.append(";\n");
else sb.append("\n + ");
}
}
sb.append('\n');
sb.append(" /**").append('\n');
sb.append(" * SimpleFormatter output for LogRecord object.").append('\n');
sb.append(" * Java version: ").append(System.getProperty("java.version")).append('\n');
sb.append(" **/").append('\n');
sb.append(" final String str = ").append("\n ");
sb.append("\"").append(str.replace("\n", "\\n")).append("\";\n");
return sb.toString();
}
/**
* Serializes a log record, encode the serialized bytes in base 64, and
* prints pseudo java code that can be cut and pasted into this test.
* @param record the log record to serialize, encode in base 64, and for
* which test data will be generated.
* @return A string containing the generated pseudo java code.
* @throws IOException Unexpected.
* @throws ClassNotFoundException Unexpected.
*/
public static String generate(LogRecord record) throws IOException, ClassNotFoundException {
// Format the given logRecord using the SimpleFormatter
SimpleFormatter formatter = new SimpleFormatter();
String str = formatter.format(record);
// Serialize the given LogRecord
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(record);
oos.flush();
oos.close();
// Now we're going to perform a number of smoke tests before
// generating the Java pseudo code.
//
// First checks that the log record can be deserialized
final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
final ObjectInputStream ois = new ObjectInputStream(bais);
final LogRecord record2 = (LogRecord)ois.readObject();
// Format the deserialized LogRecord using the SimpleFormatter, and
// check that the string representation obtained matches the string
// representation of the original LogRecord
String str2 = formatter.format(record2);
if (!str.equals(str2)) throw new RuntimeException("Unexpected values in deserialized object:"
+ "\n\tExpected: " + str
+ "\n\tRetrieved: "+str);
// Now get a Base64 string representation of the serialized bytes.
final String base64 = Base64.getEncoder().encodeToString(baos.toByteArray());
// Check that we can deserialize a log record from the Base64 string
// representation we just computed.
final ByteArrayInputStream bais2 = new ByteArrayInputStream(Base64.getDecoder().decode(base64));
final ObjectInputStream ois2 = new ObjectInputStream(bais2);
final LogRecord record3 = (LogRecord)ois2.readObject();
// Format the new deserialized LogRecord using the SimpleFormatter, and
// check that the string representation obtained matches the string
// representation of the original LogRecord
String str3 = formatter.format(record3);
if (!str.equals(str3)) throw new RuntimeException("Unexpected values in deserialized object:"
+ "\n\tExpected: " + str
+ "\n\tRetrieved: "+str);
//System.out.println(base64);
//System.out.println();
// Generates the Java Pseudo code that can be cut & pasted into
// this test (see Jdk8SerializedLog and Jdk9SerializedLog below)
final StringBuilder sb = new StringBuilder();
sb.append(" /**").append('\n');
sb.append(" * Base64 encoded string for LogRecord object.").append('\n');
sb.append(" * Java version: ").append(System.getProperty("java.version")).append('\n');
sb.append(" **/").append('\n');
sb.append(" final String base64 = ").append("\n ");
final int last = base64.length() - 1;
for (int i=0; i<base64.length();i++) {
if (i%64 == 0) sb.append("\"");
sb.append(base64.charAt(i));
if (i%64 == 63 || i == last) {
sb.append("\"");
if (i == last) sb.append(";\n");
else sb.append("\n + ");
}
}
sb.append('\n');
sb.append(" /**").append('\n');
sb.append(" * SimpleFormatter output for LogRecord object.").append('\n');
sb.append(" * Java version: ").append(System.getProperty("java.version")).append('\n');
sb.append(" **/").append('\n');
sb.append(" final String str = ").append("\n ");
sb.append("\"").append(str.replace("\n", "\\n")).append("\";\n");
return sb.toString();
}
/**
* Serializes a log record, encode the serialized bytes in base 64, and
* prints pseudo java code that can be cut and pasted into this test.
* @param record the log record to serialize, encode in base 64, and for
* which test data will be generated.
* @return A string containing the generated pseudo java code.
* @throws IOException Unexpected.
* @throws ClassNotFoundException Unexpected.
*/
public static String generate(LogRecord record) throws IOException, ClassNotFoundException {
// Format the given logRecord using the SimpleFormatter
SimpleFormatter formatter = new SimpleFormatter();
String str = formatter.format(record);
// Serialize the given LogRecord
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(record);
oos.flush();
oos.close();
// Now we're going to perform a number of smoke tests before
// generating the Java pseudo code.
//
// First checks that the log record can be deserialized
final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
final ObjectInputStream ois = new ObjectInputStream(bais);
final LogRecord record2 = (LogRecord)ois.readObject();
// Format the deserialized LogRecord using the SimpleFormatter, and
// check that the string representation obtained matches the string
// representation of the original LogRecord
String str2 = formatter.format(record2);
if (!str.equals(str2)) throw new RuntimeException("Unexpected values in deserialized object:"
+ "\n\tExpected: " + str
+ "\n\tRetrieved: "+str);
// Now get a Base64 string representation of the serialized bytes.
final String base64 = Base64.getEncoder().encodeToString(baos.toByteArray());
// Check that we can deserialize a log record from the Base64 string
// representation we just computed.
final ByteArrayInputStream bais2 = new ByteArrayInputStream(Base64.getDecoder().decode(base64));
final ObjectInputStream ois2 = new ObjectInputStream(bais2);
final LogRecord record3 = (LogRecord)ois2.readObject();
// Format the new deserialized LogRecord using the SimpleFormatter, and
// check that the string representation obtained matches the string
// representation of the original LogRecord
String str3 = formatter.format(record3);
if (!str.equals(str3)) throw new RuntimeException("Unexpected values in deserialized object:"
+ "\n\tExpected: " + str
+ "\n\tRetrieved: "+str);
//System.out.println(base64);
//System.out.println();
// Generates the Java Pseudo code that can be cut & pasted into
// this test (see Jdk8SerializedLog and Jdk9SerializedLog below)
final StringBuilder sb = new StringBuilder();
sb.append(" /**").append('\n');
sb.append(" * Base64 encoded string for LogRecord object.").append('\n');
sb.append(" * Java version: ").append(System.getProperty("java.version")).append('\n');
sb.append(" **/").append('\n');
sb.append(" final String base64 = ").append("\n ");
final int last = base64.length() - 1;
for (int i=0; i<base64.length();i++) {
if (i%64 == 0) sb.append("\"");
sb.append(base64.charAt(i));
if (i%64 == 63 || i == last) {
sb.append("\"");
if (i == last) sb.append(";\n");
else sb.append("\n + ");
}
}
sb.append('\n');
sb.append(" /**").append('\n');
sb.append(" * SimpleFormatter output for LogRecord object.").append('\n');
sb.append(" * Java version: ").append(System.getProperty("java.version")).append('\n');
sb.append(" **/").append('\n');
sb.append(" final String str = ").append("\n ");
sb.append("\"").append(str.replace("\n", "\\n")).append("\";\n");
return sb.toString();
}
/**
* Serializes a log record, encode the serialized bytes in base 64, and
* prints pseudo java code that can be cut and pasted into this test.
* @param record the log record to serialize, encode in base 64, and for
* which test data will be generated.
* @return A string containing the generated pseudo java code.
* @throws IOException Unexpected.
* @throws ClassNotFoundException Unexpected.
*/
public static String generate(LogRecord record) throws IOException, ClassNotFoundException {
// Format the given logRecord using the SimpleFormatter
SimpleFormatter formatter = new SimpleFormatter();
String str = formatter.format(record);
// Serialize the given LogRecord
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(record);
oos.flush();
oos.close();
// Now we're going to perform a number of smoke tests before
// generating the Java pseudo code.
//
// First checks that the log record can be deserialized
final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
final ObjectInputStream ois = new ObjectInputStream(bais);
final LogRecord record2 = (LogRecord)ois.readObject();
// Format the deserialized LogRecord using the SimpleFormatter, and
// check that the string representation obtained matches the string
// representation of the original LogRecord
String str2 = formatter.format(record2);
if (!str.equals(str2)) throw new RuntimeException("Unexpected values in deserialized object:"
+ "\n\tExpected: " + str
+ "\n\tRetrieved: "+str);
// Now get a Base64 string representation of the serialized bytes.
final String base64 = Base64.getEncoder().encodeToString(baos.toByteArray());
// Check that we can deserialize a log record from the Base64 string
// representation we just computed.
final ByteArrayInputStream bais2 = new ByteArrayInputStream(Base64.getDecoder().decode(base64));
final ObjectInputStream ois2 = new ObjectInputStream(bais2);
final LogRecord record3 = (LogRecord)ois2.readObject();
// Format the new deserialized LogRecord using the SimpleFormatter, and
// check that the string representation obtained matches the string
// representation of the original LogRecord
String str3 = formatter.format(record3);
if (!str.equals(str3)) throw new RuntimeException("Unexpected values in deserialized object:"
+ "\n\tExpected: " + str
+ "\n\tRetrieved: "+str);
//System.out.println(base64);
//System.out.println();
// Generates the Java Pseudo code that can be cut & pasted into
// this test (see Jdk8SerializedLog and Jdk9SerializedLog below)
final StringBuilder sb = new StringBuilder();
sb.append(" /**").append('\n');
sb.append(" * Base64 encoded string for LogRecord object.").append('\n');
sb.append(" * Java version: ").append(System.getProperty("java.version")).append('\n');
sb.append(" **/").append('\n');
sb.append(" final String base64 = ").append("\n ");
final int last = base64.length() - 1;
for (int i=0; i<base64.length();i++) {
if (i%64 == 0) sb.append("\"");
sb.append(base64.charAt(i));
if (i%64 == 63 || i == last) {
sb.append("\"");
if (i == last) sb.append(";\n");
else sb.append("\n + ");
}
}
sb.append('\n');
sb.append(" /**").append('\n');
sb.append(" * SimpleFormatter output for LogRecord object.").append('\n');
sb.append(" * Java version: ").append(System.getProperty("java.version")).append('\n');
sb.append(" **/").append('\n');
sb.append(" final String str = ").append("\n ");
sb.append("\"").append(str.replace("\n", "\\n")).append("\";\n");
return sb.toString();
}
/**
* Serializes a log record, encode the serialized bytes in base 64, and
* prints pseudo java code that can be cut and pasted into this test.
* @param record the log record to serialize, encode in base 64, and for
* which test data will be generated.
* @return A string containing the generated pseudo java code.
* @throws IOException Unexpected.
* @throws ClassNotFoundException Unexpected.
*/
public static String generate(LogRecord record) throws IOException, ClassNotFoundException {
// Format the given logRecord using the SimpleFormatter
SimpleFormatter formatter = new SimpleFormatter();
String str = formatter.format(record);
// Serialize the given LogRecord
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(record);
oos.flush();
oos.close();
// Now we're going to perform a number of smoke tests before
// generating the Java pseudo code.
//
// First checks that the log record can be deserialized
final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
final ObjectInputStream ois = new ObjectInputStream(bais);
final LogRecord record2 = (LogRecord)ois.readObject();
// Format the deserialized LogRecord using the SimpleFormatter, and
// check that the string representation obtained matches the string
// representation of the original LogRecord
String str2 = formatter.format(record2);
if (!str.equals(str2)) throw new RuntimeException("Unexpected values in deserialized object:"
+ "\n\tExpected: " + str
+ "\n\tRetrieved: "+str);
// Now get a Base64 string representation of the serialized bytes.
final String base64 = Base64.getEncoder().encodeToString(baos.toByteArray());
// Check that we can deserialize a log record from the Base64 string
// representation we just computed.
final ByteArrayInputStream bais2 = new ByteArrayInputStream(Base64.getDecoder().decode(base64));
final ObjectInputStream ois2 = new ObjectInputStream(bais2);
final LogRecord record3 = (LogRecord)ois2.readObject();
// Format the new deserialized LogRecord using the SimpleFormatter, and
// check that the string representation obtained matches the string
// representation of the original LogRecord
String str3 = formatter.format(record3);
if (!str.equals(str3)) throw new RuntimeException("Unexpected values in deserialized object:"
+ "\n\tExpected: " + str
+ "\n\tRetrieved: "+str);
//System.out.println(base64);
//System.out.println();
// Generates the Java Pseudo code that can be cut & pasted into
// this test (see Jdk8SerializedLog and Jdk9SerializedLog below)
final StringBuilder sb = new StringBuilder();
sb.append(" /**").append('\n');
sb.append(" * Base64 encoded string for LogRecord object.").append('\n');
sb.append(" * Java version: ").append(System.getProperty("java.version")).append('\n');
sb.append(" **/").append('\n');
sb.append(" final String base64 = ").append("\n ");
final int last = base64.length() - 1;
for (int i=0; i<base64.length();i++) {
if (i%64 == 0) sb.append("\"");
sb.append(base64.charAt(i));
if (i%64 == 63 || i == last) {
sb.append("\"");
if (i == last) sb.append(";\n");
else sb.append("\n + ");
}
}
sb.append('\n');
sb.append(" /**").append('\n');
sb.append(" * SimpleFormatter output for LogRecord object.").append('\n');
sb.append(" * Java version: ").append(System.getProperty("java.version")).append('\n');
sb.append(" **/").append('\n');
sb.append(" final String str = ").append("\n ");
sb.append("\"").append(str.replace("\n", "\\n")).append("\";\n");
return sb.toString();
}
/**
* Serializes a log record, then deserializes it and check that both
* records match.
* @param record the log record to serialize & deserialize.
* @param hasExceedingNanos whether the record has a nano adjustment whose
* value exceeds 1ms.
* @throws IOException Unexpected.
* @throws ClassNotFoundException Unexpected.
*/
public static void test(LogRecord record, boolean hasExceedingNanos)
throws IOException, ClassNotFoundException {
// Format the given logRecord using the SimpleFormatter
SimpleFormatter formatter = new SimpleFormatter();
String str = formatter.format(record);
// Serialize the given LogRecord
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(record);
oos.flush();
oos.close();
// First checks that the log record can be deserialized
final ByteArrayInputStream bais =
new ByteArrayInputStream(baos.toByteArray());
final ObjectInputStream ois = new ObjectInputStream(bais);
final LogRecord record2 = (LogRecord)ois.readObject();
assertEquals(record.getMillis(), record2.getMillis(), "getMillis()");
assertEquals(record.getInstant().getEpochSecond(),
record2.getInstant().getEpochSecond(),
"getInstant().getEpochSecond()");
assertEquals(record.getInstant().getNano(),
record2.getInstant().getNano(),
"getInstant().getNano()");
assertEquals(record.getInstant().toEpochMilli(),
record2.getInstant().toEpochMilli(),
"getInstant().toEpochMilli()");
long millis = record.getMillis();
millis = hasExceedingNanos
? Instant.ofEpochSecond(millis/MILLIS_IN_SECOND,
(millis%MILLIS_IN_SECOND)*NANOS_IN_MILLI
+ record.getInstant().getNano() % NANOS_IN_MILLI).toEpochMilli()
: millis;
assertEquals(millis, record.getInstant().toEpochMilli(),
"getMillis()/getInstant().toEpochMilli()");
millis = record2.getMillis();
millis = hasExceedingNanos
? Instant.ofEpochSecond(millis/MILLIS_IN_SECOND,
(millis%MILLIS_IN_SECOND)*NANOS_IN_MILLI
+ record2.getInstant().getNano() % NANOS_IN_MILLI).toEpochMilli()
: millis;
assertEquals(millis, record2.getInstant().toEpochMilli(),
"getMillis()/getInstant().toEpochMilli()");
long nanos = nanoInSecondFromEpochMilli(record.getMillis())
+ record.getInstant().getNano() % NANOS_IN_MILLI;
assertEquals(nanos, record.getInstant().getNano(),
"nanoInSecondFromEpochMilli(record.getMillis())"
+ " + record.getInstant().getNano() % NANOS_IN_MILLI /getInstant().getNano()");
nanos = nanoInSecondFromEpochMilli(record2.getMillis())
+ record2.getInstant().getNano() % NANOS_IN_MILLI;
assertEquals(nanos, record2.getInstant().getNano(),
"nanoInSecondFromEpochMilli(record2.getMillis())"
+ " + record2.getInstant().getNano() % NANOS_IN_MILLI /getInstant().getNano()");
// Format the deserialized LogRecord using the SimpleFormatter, and
// check that the string representation obtained matches the string
// representation of the original LogRecord
String str2 = formatter.format(record2);
if (!str.equals(str2))
throw new RuntimeException("Unexpected values in deserialized object:"
+ "\n\tExpected: " + str
+ "\n\tRetrieved: "+str);
}
/**
* Serializes a log record, then deserializes it and check that both
* records match.
* @param record the log record to serialize & deserialize.
* @throws IOException Unexpected.
* @throws ClassNotFoundException Unexpected.
*/
public static void test(LogRecord record)
throws IOException, ClassNotFoundException {
// Format the given logRecord using the SimpleFormatter
SimpleFormatter formatter = new SimpleFormatter();
String str = formatter.format(record);
// Serialize the given LogRecord
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(record);
oos.flush();
oos.close();
// First checks that the log record can be deserialized
final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
final ObjectInputStream ois = new ObjectInputStream(bais);
final LogRecord record2 = (LogRecord)ois.readObject();
assertEquals(record.getMillis(), record2.getMillis(), "getMillis()");
assertEquals(record.getInstant().getEpochSecond(),
record2.getInstant().getEpochSecond(),
"getInstant().getEpochSecond()");
assertEquals(record.getInstant().getNano(),
record2.getInstant().getNano(),
"getInstant().getNano()");
assertEquals(record.getInstant().toEpochMilli(),
record2.getInstant().toEpochMilli(),
"getInstant().toEpochMilli()");
assertEquals(record.getMillis(),
record.getInstant().toEpochMilli(),
"getMillis()/getInstant().toEpochMilli()");
assertEquals(record2.getMillis(),
record2.getInstant().toEpochMilli(),
"getMillis()/getInstant().toEpochMilli()");
assertEquals((record.getMillis()%MILLIS_IN_SECOND)*NANOS_IN_MILLI
+ (record.getInstant().getNano() % NANOS_IN_MILLI),
record.getInstant().getNano(),
"record.getMillis()%1000)*1000_000"
+ " + record.getInstant().getNano()%1000_000 / getInstant().getNano()");
assertEquals((record2.getMillis()%MILLIS_IN_SECOND)*NANOS_IN_MILLI
+ (record2.getInstant().getNano() % NANOS_IN_MILLI),
record2.getInstant().getNano(),
"record2.getMillis()%1000)*1000_000"
+ " + record2.getInstant().getNano()%1000_000 / getInstant().getNano()");
// Format the deserialized LogRecord using the SimpleFormatter, and
// check that the string representation obtained matches the string
// representation of the original LogRecord
String str2 = formatter.format(record2);
if (!str.equals(str2))
throw new RuntimeException("Unexpected values in deserialized object:"
+ "\n\tExpected: " + str
+ "\n\tRetrieved: "+str);
}
public static void main(String[] args) throws Exception {
Locale.setDefault(Locale.ENGLISH);
LogRecord record = new LogRecord(Level.INFO, "Java Version: {0}");
record.setLoggerName("test");
record.setParameters(new Object[] {System.getProperty("java.version")});
int nanos = getNanoAdjustment(record);
long millis = record.getMillis();
// make sure we don't have leading zeros when printing below
// the second precision
if (millis % MILLIS_IN_SECOND < 100) millis = millis + 100;
// make sure we have some nanos
if (nanos % NANOS_IN_MICRO == 0) nanos = nanos + 999;
record.setMillis(millis);
setNanoAdjustment(record, nanos);
final Instant instant = record.getInstant();
if (nanos < 0) {
throw new RuntimeException("Unexpected negative nano adjustment: "
+ getNanoAdjustment(record));
}
if (nanos >= NANOS_IN_MILLI) {
throw new RuntimeException("Nano adjustment exceeds 1ms: "
+ getNanoAdjustment(record));
}
if (millis != record.getMillis()) {
throw new RuntimeException("Unexpected millis: " + millis + " != "
+ record.getMillis());
}
if (millis != record.getInstant().toEpochMilli()) {
throw new RuntimeException("Unexpected millis: "
+ record.getInstant().toEpochMilli());
}
ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault());
int zdtNanos = zdt.getNano();
long expectedNanos = (millis % MILLIS_IN_SECOND) * NANOS_IN_MILLI + nanos;
assertEquals(expectedNanos, instant.getNano(), "Instant.getNano()");
assertEquals(expectedNanos, zdtNanos, "ZonedDateTime.getNano()");
String match = "."+expectedNanos+" ";
System.out.println("Testing with default format...");
SimpleFormatter formatter = new SimpleFormatter();
String str = formatter.format(record);
if (str.contains(match)) {
throw new RuntimeException("SimpleFormatter.format()"
+ " string contains unexpected nanos: "
+ "\n\tdid not expect match for: '" + match + "'"
+ "\n\tin: " + str);
}
System.out.println("Nanos omitted as expected: no '"+match+"' in "+str);
System.out.println("Changing format to print nanos...");
System.setProperty("java.util.logging.SimpleFormatter.format",
"%1$tb %1$td, %1$tY %1$tl:%1$tM:%1$tS.%1$tN %1$Tp %2$s%n%4$s: %5$s%6$s%n");
SimpleFormatter formatter2 = new SimpleFormatter();
str = formatter2.format(record);
if (!str.contains(match)) {
throw new RuntimeException("SimpleFormatter.format()"
+ " string does not contain expected nanos: "
+ "\n\texpected match for: '" + match + "'"
+ "\n\tin: " + str);
}
System.out.println("Found expected match for '"+match+"' in "+str);
System.out.println("Changing format to omit nanos...");
System.setProperty("java.util.logging.SimpleFormatter.format",
"%1$tb %1$td, %1$tY %1$tl:%1$tM:%1$tS %1$Tp %2$s%n%4$s: %5$s%6$s%n");
SimpleFormatter formatter3 = new SimpleFormatter();
str = formatter3.format(record);
String notMatch = match;
if (str.contains(notMatch)) {
throw new RuntimeException("SimpleFormatter.format()"
+ " string contains unexpected nanos: "
+ "\n\tdid not expect match for: '" + notMatch + "'"
+ "\n\tin: " + str);
}
System.out.println("Nanos omitted as expected: no '"+notMatch+"' in "+str);
}
/**
* Serializes a log record, encode the serialized bytes in base 64, and
* prints pseudo java code that can be cut and pasted into this test.
* @param record the log record to serialize, encode in base 64, and for
* which test data will be generated.
* @return A string containing the generated pseudo java code.
* @throws IOException Unexpected.
* @throws ClassNotFoundException Unexpected.
*/
public static String generate(LogRecord record) throws IOException, ClassNotFoundException {
// Format the given logRecord using the SimpleFormatter
SimpleFormatter formatter = new SimpleFormatter();
String str = formatter.format(record);
// Serialize the given LogRecord
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(record);
oos.flush();
oos.close();
// Now we're going to perform a number of smoke tests before
// generating the Java pseudo code.
//
// First checks that the log record can be deserialized
final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
final ObjectInputStream ois = new ObjectInputStream(bais);
final LogRecord record2 = (LogRecord)ois.readObject();
// Format the deserialized LogRecord using the SimpleFormatter, and
// check that the string representation obtained matches the string
// representation of the original LogRecord
String str2 = formatter.format(record2);
if (!str.equals(str2)) throw new RuntimeException("Unexpected values in deserialized object:"
+ "\n\tExpected: " + str
+ "\n\tRetrieved: "+str);
// Now get a Base64 string representation of the serialized bytes.
final String base64 = Base64.getEncoder().encodeToString(baos.toByteArray());
// Check that we can deserialize a log record from the Base64 string
// representation we just computed.
final ByteArrayInputStream bais2 = new ByteArrayInputStream(Base64.getDecoder().decode(base64));
final ObjectInputStream ois2 = new ObjectInputStream(bais2);
final LogRecord record3 = (LogRecord)ois2.readObject();
// Format the new deserialized LogRecord using the SimpleFormatter, and
// check that the string representation obtained matches the string
// representation of the original LogRecord
String str3 = formatter.format(record3);
if (!str.equals(str3)) throw new RuntimeException("Unexpected values in deserialized object:"
+ "\n\tExpected: " + str
+ "\n\tRetrieved: "+str);
//System.out.println(base64);
//System.out.println();
// Generates the Java Pseudo code that can be cut & pasted into
// this test (see Jdk8SerializedLog and Jdk9SerializedLog below)
final StringBuilder sb = new StringBuilder();
sb.append(" /**").append('\n');
sb.append(" * Base64 encoded string for LogRecord object.").append('\n');
sb.append(" * Java version: ").append(System.getProperty("java.version")).append('\n');
sb.append(" **/").append('\n');
sb.append(" final String base64 = ").append("\n ");
final int last = base64.length() - 1;
for (int i=0; i<base64.length();i++) {
if (i%64 == 0) sb.append("\"");
sb.append(base64.charAt(i));
if (i%64 == 63 || i == last) {
sb.append("\"");
if (i == last) sb.append(";\n");
else sb.append("\n + ");
}
}
sb.append('\n');
sb.append(" /**").append('\n');
sb.append(" * SimpleFormatter output for LogRecord object.").append('\n');
sb.append(" * Java version: ").append(System.getProperty("java.version")).append('\n');
sb.append(" **/").append('\n');
sb.append(" final String str = ").append("\n ");
sb.append("\"").append(str.replace("\n", "\\n")).append("\";\n");
return sb.toString();
}
/**
* Serializes a log record, encode the serialized bytes in base 64, and
* prints pseudo java code that can be cut and pasted into this test.
* @param record the log record to serialize, encode in base 64, and for
* which test data will be generated.
* @return A string containing the generated pseudo java code.
* @throws IOException Unexpected.
* @throws ClassNotFoundException Unexpected.
*/
public static String generate(LogRecord record) throws IOException, ClassNotFoundException {
// Format the given logRecord using the SimpleFormatter
SimpleFormatter formatter = new SimpleFormatter();
String str = formatter.format(record);
// Serialize the given LogRecord
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(record);
oos.flush();
oos.close();
// Now we're going to perform a number of smoke tests before
// generating the Java pseudo code.
//
// First checks that the log record can be deserialized
final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
final ObjectInputStream ois = new ObjectInputStream(bais);
final LogRecord record2 = (LogRecord)ois.readObject();
// Format the deserialized LogRecord using the SimpleFormatter, and
// check that the string representation obtained matches the string
// representation of the original LogRecord
String str2 = formatter.format(record2);
if (!str.equals(str2)) throw new RuntimeException("Unexpected values in deserialized object:"
+ "\n\tExpected: " + str
+ "\n\tRetrieved: "+str);
// Now get a Base64 string representation of the serialized bytes.
final String base64 = Base64.getEncoder().encodeToString(baos.toByteArray());
// Check that we can deserialize a log record from the Base64 string
// representation we just computed.
final ByteArrayInputStream bais2 = new ByteArrayInputStream(Base64.getDecoder().decode(base64));
final ObjectInputStream ois2 = new ObjectInputStream(bais2);
final LogRecord record3 = (LogRecord)ois2.readObject();
// Format the new deserialized LogRecord using the SimpleFormatter, and
// check that the string representation obtained matches the string
// representation of the original LogRecord
String str3 = formatter.format(record3);
if (!str.equals(str3)) throw new RuntimeException("Unexpected values in deserialized object:"
+ "\n\tExpected: " + str
+ "\n\tRetrieved: "+str);
//System.out.println(base64);
//System.out.println();
// Generates the Java Pseudo code that can be cut & pasted into
// this test (see Jdk8SerializedLog and Jdk9SerializedLog below)
final StringBuilder sb = new StringBuilder();
sb.append(" /**").append('\n');
sb.append(" * Base64 encoded string for LogRecord object.").append('\n');
sb.append(" * Java version: ").append(System.getProperty("java.version")).append('\n');
sb.append(" **/").append('\n');
sb.append(" final String base64 = ").append("\n ");
final int last = base64.length() - 1;
for (int i=0; i<base64.length();i++) {
if (i%64 == 0) sb.append("\"");
sb.append(base64.charAt(i));
if (i%64 == 63 || i == last) {
sb.append("\"");
if (i == last) sb.append(";\n");
else sb.append("\n + ");
}
}
sb.append('\n');
sb.append(" /**").append('\n');
sb.append(" * SimpleFormatter output for LogRecord object.").append('\n');
sb.append(" * Java version: ").append(System.getProperty("java.version")).append('\n');
sb.append(" **/").append('\n');
sb.append(" final String str = ").append("\n ");
sb.append("\"").append(str.replace("\n", "\\n")).append("\";\n");
return sb.toString();
}
/**
* Serializes a log record, encode the serialized bytes in base 64, and
* prints pseudo java code that can be cut and pasted into this test.
* @param record the log record to serialize, encode in base 64, and for
* which test data will be generated.
* @return A string containing the generated pseudo java code.
* @throws IOException Unexpected.
* @throws ClassNotFoundException Unexpected.
*/
public static String generate(LogRecord record) throws IOException, ClassNotFoundException {
// Format the given logRecord using the SimpleFormatter
SimpleFormatter formatter = new SimpleFormatter();
String str = formatter.format(record);
// Serialize the given LogRecord
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(record);
oos.flush();
oos.close();
// Now we're going to perform a number of smoke tests before
// generating the Java pseudo code.
//
// First checks that the log record can be deserialized
final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
final ObjectInputStream ois = new ObjectInputStream(bais);
final LogRecord record2 = (LogRecord)ois.readObject();
// Format the deserialized LogRecord using the SimpleFormatter, and
// check that the string representation obtained matches the string
// representation of the original LogRecord
String str2 = formatter.format(record2);
if (!str.equals(str2)) throw new RuntimeException("Unexpected values in deserialized object:"
+ "\n\tExpected: " + str
+ "\n\tRetrieved: "+str);
// Now get a Base64 string representation of the serialized bytes.
final String base64 = Base64.getEncoder().encodeToString(baos.toByteArray());
// Check that we can deserialize a log record from the Base64 string
// representation we just computed.
final ByteArrayInputStream bais2 = new ByteArrayInputStream(Base64.getDecoder().decode(base64));
final ObjectInputStream ois2 = new ObjectInputStream(bais2);
final LogRecord record3 = (LogRecord)ois2.readObject();
// Format the new deserialized LogRecord using the SimpleFormatter, and
// check that the string representation obtained matches the string
// representation of the original LogRecord
String str3 = formatter.format(record3);
if (!str.equals(str3)) throw new RuntimeException("Unexpected values in deserialized object:"
+ "\n\tExpected: " + str
+ "\n\tRetrieved: "+str);
//System.out.println(base64);
//System.out.println();
// Generates the Java Pseudo code that can be cut & pasted into
// this test (see Jdk8SerializedLog and Jdk9SerializedLog below)
final StringBuilder sb = new StringBuilder();
sb.append(" /**").append('\n');
sb.append(" * Base64 encoded string for LogRecord object.").append('\n');
sb.append(" * Java version: ").append(System.getProperty("java.version")).append('\n');
sb.append(" **/").append('\n');
sb.append(" final String base64 = ").append("\n ");
final int last = base64.length() - 1;
for (int i=0; i<base64.length();i++) {
if (i%64 == 0) sb.append("\"");
sb.append(base64.charAt(i));
if (i%64 == 63 || i == last) {
sb.append("\"");
if (i == last) sb.append(";\n");
else sb.append("\n + ");
}
}
sb.append('\n');
sb.append(" /**").append('\n');
sb.append(" * SimpleFormatter output for LogRecord object.").append('\n');
sb.append(" * Java version: ").append(System.getProperty("java.version")).append('\n');
sb.append(" **/").append('\n');
sb.append(" final String str = ").append("\n ");
sb.append("\"").append(str.replace("\n", "\\n")).append("\";\n");
return sb.toString();
}
/**
* Serializes a log record, encode the serialized bytes in base 64, and
* prints pseudo java code that can be cut and pasted into this test.
* @param record the log record to serialize, encode in base 64, and for
* which test data will be generated.
* @return A string containing the generated pseudo java code.
* @throws IOException Unexpected.
* @throws ClassNotFoundException Unexpected.
*/
public static String generate(LogRecord record) throws IOException, ClassNotFoundException {
// Format the given logRecord using the SimpleFormatter
SimpleFormatter formatter = new SimpleFormatter();
String str = formatter.format(record);
// Serialize the given LogRecord
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(record);
oos.flush();
oos.close();
// Now we're going to perform a number of smoke tests before
// generating the Java pseudo code.
//
// First checks that the log record can be deserialized
final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
final ObjectInputStream ois = new ObjectInputStream(bais);
final LogRecord record2 = (LogRecord)ois.readObject();
// Format the deserialized LogRecord using the SimpleFormatter, and
// check that the string representation obtained matches the string
// representation of the original LogRecord
String str2 = formatter.format(record2);
if (!str.equals(str2)) throw new RuntimeException("Unexpected values in deserialized object:"
+ "\n\tExpected: " + str
+ "\n\tRetrieved: "+str);
// Now get a Base64 string representation of the serialized bytes.
final String base64 = Base64.getEncoder().encodeToString(baos.toByteArray());
// Check that we can deserialize a log record from the Base64 string
// representation we just computed.
final ByteArrayInputStream bais2 = new ByteArrayInputStream(Base64.getDecoder().decode(base64));
final ObjectInputStream ois2 = new ObjectInputStream(bais2);
final LogRecord record3 = (LogRecord)ois2.readObject();
// Format the new deserialized LogRecord using the SimpleFormatter, and
// check that the string representation obtained matches the string
// representation of the original LogRecord
String str3 = formatter.format(record3);
if (!str.equals(str3)) throw new RuntimeException("Unexpected values in deserialized object:"
+ "\n\tExpected: " + str
+ "\n\tRetrieved: "+str);
//System.out.println(base64);
//System.out.println();
// Generates the Java Pseudo code that can be cut & pasted into
// this test (see Jdk8SerializedLog and Jdk9SerializedLog below)
final StringBuilder sb = new StringBuilder();
sb.append(" /**").append('\n');
sb.append(" * Base64 encoded string for LogRecord object.").append('\n');
sb.append(" * Java version: ").append(System.getProperty("java.version")).append('\n');
sb.append(" **/").append('\n');
sb.append(" final String base64 = ").append("\n ");
final int last = base64.length() - 1;
for (int i=0; i<base64.length();i++) {
if (i%64 == 0) sb.append("\"");
sb.append(base64.charAt(i));
if (i%64 == 63 || i == last) {
sb.append("\"");
if (i == last) sb.append(";\n");
else sb.append("\n + ");
}
}
sb.append('\n');
sb.append(" /**").append('\n');
sb.append(" * SimpleFormatter output for LogRecord object.").append('\n');
sb.append(" * Java version: ").append(System.getProperty("java.version")).append('\n');
sb.append(" **/").append('\n');
sb.append(" final String str = ").append("\n ");
sb.append("\"").append(str.replace("\n", "\\n")).append("\";\n");
return sb.toString();
}