File: XmlFormatterNanos.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (274 lines) | stat: -rw-r--r-- 11,353 bytes parent folder | download | duplicates (16)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;
import java.util.logging.XMLFormatter;

/**
 * @test
 * @bug 8072645
 * @summary tests that XmlFormatter will print out dates with the new
 *                nanosecond precision.
 * @run main/othervm XmlFormatterNanos
 * @author danielfuchs
 */
public class XmlFormatterNanos {

    static final int MILLIS_IN_SECOND = 1000;
    static final int NANOS_IN_MILLI = 1000_000;
    static final int NANOS_IN_MICRO = 1000;
    static final int NANOS_IN_SECOND = 1000_000_000;

    static final boolean verbose = true;

    static final class TestAssertException extends RuntimeException {
        TestAssertException(String msg) { super(msg); }
    }

    private static void assertEquals(long expected, long received, String msg) {
        if (expected != received) {
            throw new TestAssertException("Unexpected result for " + msg
                    + ".\n\texpected: " + expected
                    +  "\n\tactual:   " + received);
        } else if (verbose) {
            System.out.println("Got expected " + msg + ": " + received);
        }
    }

    private static void assertEquals(Object expected, Object received, String msg) {
        if (!Objects.equals(expected, received)) {
            throw new TestAssertException("Unexpected result for " + msg
                    + ".\n\texpected: " + expected
                    +  "\n\tactual:   " + received);
        } else if (verbose) {
            System.out.println("Got expected " + msg + ": " + received);
        }
    }

    static class CustomXMLFormatter extends XMLFormatter {}

    static class Configuration {
        final Properties propertyFile;
        private Configuration(Properties properties) {
            propertyFile = properties;
        }
        public Configuration apply() {
            try {
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                propertyFile.store(bytes, testName());
                ByteArrayInputStream bais = new ByteArrayInputStream(bytes.toByteArray());
                LogManager.getLogManager().readConfiguration(bais);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            return this;
        }

        public static String useInstantProperty(Class<?> type) {
            return type.getName()+".useInstant";
        }

        public boolean useInstant(XMLFormatter formatter) {
            return Boolean.parseBoolean(propertyFile.getProperty(
                    formatter.getClass().getName()+".useInstant", "true"));
        }

        public String testName() {
            return propertyFile.getProperty("test.name", "????");
        }

        public static Configuration of(Properties props) {
            return new Configuration(props);
        }

        public static Configuration apply(Properties props) {
            return of(props).apply();
        }
    }

    static final List<Properties> properties;
    static {
        Properties props1 = new Properties();
        props1.setProperty("test.name", "with XML nano element (default)");
        Properties props2 = new Properties();
        props2.setProperty("test.name", "with XML nano element (standard=true, custom=false)");
        props2.setProperty(Configuration.useInstantProperty(XMLFormatter.class), "true");
        props2.setProperty(Configuration.useInstantProperty(CustomXMLFormatter.class), "false");
        Properties props3 = new Properties();
        props3.setProperty("test.name", "with XML nano element (standard=false, custom=true)");
        props3.setProperty(Configuration.useInstantProperty(XMLFormatter.class), "false");
        props3.setProperty(Configuration.useInstantProperty(CustomXMLFormatter.class), "true");

        properties = Collections.unmodifiableList(Arrays.asList(
                    props1,
                    props2));
    }

    public static void main(String[] args) throws Exception {
        Locale.setDefault(Locale.ENGLISH);
        properties.stream().forEach(XmlFormatterNanos::test);
    }

    static int getNanoAdjustment(LogRecord record) {
        return record.getInstant().getNano() % NANOS_IN_MILLI;
    }
    static void setNanoAdjustment(LogRecord record, int nanos) {
        record.setInstant(Instant.ofEpochSecond(record.getInstant().getEpochSecond(),
                (record.getInstant().getNano() / NANOS_IN_MILLI) * NANOS_IN_MILLI + nanos));
    }

    public static void test(Properties props) {
        Configuration conf = Configuration.apply(props);

        LogRecord record = new LogRecord(Level.INFO, "Test Name: {0}");
        record.setLoggerName("test");
        record.setParameters(new Object[] {conf.testName()});
        int nanos = record.getInstant().getNano() % NANOS_IN_MILLI;
        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 some nanos - and make sure we don't have
        // trailing zeros
        if (nanos % 10 == 0) nanos = nanos + 7;

        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());
        }
        long expectedNanos = (millis % MILLIS_IN_SECOND) * NANOS_IN_MILLI + nanos;
        assertEquals(expectedNanos, instant.getNano(), "Instant.getNano()");

        XMLFormatter formatter = new XMLFormatter();
        testMatching(formatter, record, instant, expectedNanos, conf.useInstant(formatter));

        XMLFormatter custom = new CustomXMLFormatter();
        testMatching(custom, record, instant, expectedNanos, conf.useInstant(custom));
    }

    private static void testMatching(XMLFormatter formatter,
            LogRecord record,  Instant instant, long expectedNanos,
            boolean useInstant) {

        ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault());
        int zdtNanos = zdt.getNano();
        assertEquals(expectedNanos, zdtNanos, "ZonedDateTime.getNano()");

        String str = formatter.format(record);

        String match = "."+expectedNanos;
        if (str.contains(match) != useInstant) {
            throw new RuntimeException(formatter.getClass().getSimpleName()
                    + ".format()"
                    + " string does not contain expected nanos: "
                    + "\n\texpected match for: '" + match + "'"
                    + "\n\tin: \n" + str);
        }
        System.out.println("Found expected match for '"+match+"' in \n"+str);

        match = "<millis>"+instant.toEpochMilli()+"</millis>";
        if (!str.contains(match)) {
            throw new RuntimeException(formatter.getClass().getSimpleName()
                    + ".format()"
                    + " string does not contain expected millis: "
                    + "\n\texpected match for: '" + match + "'"
                    + "\n\tin: \n" + str);
        }
        System.out.println("Found expected match for '"+match+"' in \n"+str);

        match = "<nanos>";
        if (str.contains(match) != useInstant) {
            throw new RuntimeException(formatter.getClass().getSimpleName()
                    + ".format()"
                    + " string "
                    + (useInstant
                            ? "does not contain expected nanos: "
                            : "contains unexpected nanos: ")
                    + "\n\t" + (useInstant ? "expected" : "unexpected")
                    + " match for: '" + match + "'"
                    + "\n\tin: \n" + str);
        }
        match = "<nanos>"+getNanoAdjustment(record)+"</nanos>";
        if (str.contains(match) != useInstant) {
            throw new RuntimeException(formatter.getClass().getSimpleName()
                    + ".format()"
                    + " string "
                    + (useInstant
                            ? "does not contain expected nanos: "
                            : "contains unexpected nanos: ")
                    + "\n\t" + (useInstant ? "expected" : "unexpected")
                    + " match for: '" + match + "'"
                    + "\n\tin: \n" + str);
        }
        if (useInstant) {
            System.out.println("Found expected match for '"+match+"' in \n"+str);
        } else {
            System.out.println("As expected '"+match+"' is not present in \n"+str);
        }

        match = useInstant ? DateTimeFormatter.ISO_INSTANT.format(instant)
                : zdt.truncatedTo(ChronoUnit.SECONDS)
                        .format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
        match = "<date>"+match+"</date>";
        if (!str.contains(match)) {
            throw new RuntimeException(formatter.getClass().getSimpleName()
                    + ".format()"
                    + " string does not contain expected date: "
                    + "\n\texpected match for: '" + match + "'"
                    + "\n\tin: \n" + str);
        }
        System.out.println("Found expected match for '"+match+"' in \n"+str);

    }

}