/* * Copyright (c) 2012, 2016, 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.
*/ package test.java.util;
@Test(dataProvider="calendarsByLocale") publicvoid test (String calendarLocale) {
failure = 0; int N = 12; //locales = Locale.getAvailableLocales();
Locale[] locales = new Locale[] {
Locale.ENGLISH, Locale.FRENCH, Locale.JAPANESE, Locale.CHINESE};
Random r = new Random();
private String getClassName(Object o) { Class<?> c = o.getClass();
String clname = c.getName().substring(c.getPackage().getName().length() + 1); if (o instanceof TemporalAccessor) {
Chronology chrono = ((TemporalAccessor)o).query(TemporalQueries.chronology()); if (chrono != null) {
clname = clname + "(" + chrono.getId() + ")";
}
} if (o instanceof Calendar) {
String type = ((Calendar)o).getCalendarType();
clname = clname + "(" + type + ")";
} return clname;
}
private String test(String fmtStr, Locale locale,
String expected, Object dt) {
String out = new Formatter( new StringBuilder(), locale).format(fmtStr, dt).out().toString(); if (verbose) {
System.out.printf("%-24s : %s%n", getClassName(dt), out);
}
// expected usually comes from Calendar which only has milliseconds // precision. So we're going to replace it's N:[nanos] stamp with // the correct value for nanos. if ((dt instanceof TemporalAccessor) && expected != null) { try { // Get millis & nanos from the dt final TemporalAccessor ta = (TemporalAccessor) dt; finalint nanos = ta.get(ChronoField.NANO_OF_SECOND); finalint millis = ta.get(ChronoField.MILLI_OF_SECOND); final String nanstr = String.valueOf(nanos); final String mistr = String.valueOf(millis);
// Compute the value of the N:[nanos] field that we expect // to find in 'out' final StringBuilder sb = new StringBuilder();
sb.append("N:["); for (int i=nanstr.length(); i<9; i++) {
sb.append('0');
}
sb.append(nanos).append("]");
// Compute the truncated value of N:[nanos] field that might // be in 'expected' when expected was built from Calendar. final StringBuilder sbm = new StringBuilder();
sbm.append("N:["); for (int i=mistr.length(); i<3; i++) {
sbm.append('0');
}
sbm.append(mistr).append("000000]");
// if expected contains the truncated value, replace it with // the complete value.
expected = expected.replace(sbm.toString(), sb.toString());
} catch (UnsupportedTemporalTypeException e) { // nano seconds unsupported - nothing to do...
}
} if (expected != null && !out.equals(expected)) {
System.out.printf("%-24s actual: %s%n FAILED; expected: %s%n",
getClassName(dt), out, expected); new RuntimeException().printStackTrace(System.out);
failure++;
}
total++; return out;
}
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.