Path: blob/master/test/jdk/java/util/Calendar/CalendarTestScripts/CalendarTestEngine.java
41152 views
/*1* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/222324import java.io.BufferedReader;25import java.io.File;26import java.io.FileInputStream;27import java.io.InputStreamReader;28import java.util.*;2930public class CalendarTestEngine {31private static File file;32private static BufferedReader in;33private static int testCount;34private static int lineno;35private static Locale locale;36private static TimeZone timezone;37private static boolean leniency = true;3839public static void main(String[] args) throws Exception {40Locale loc = Locale.getDefault();41TimeZone tz = TimeZone.getDefault();42locale = loc;43timezone = tz;44try {45for (String arg : args) {46file = new File(arg);47FileInputStream fis = new FileInputStream(file);48System.out.println("Starting " + file.getName() + "...");49in = new BufferedReader(new InputStreamReader(fis));50testCount = lineno = 0;51run();52System.out.println("Completed " + file.getName());53}54} finally {55Locale.setDefault(loc);56TimeZone.setDefault(tz);57}58}5960private static void run() throws Exception {61String line;62int section = 0;63Map<String, CalendarAdapter> cals = new HashMap<String, CalendarAdapter>();64CalendarAdapter calendar = null;65Result result = new Result();6667while ((line = in.readLine()) != null) {68lineno++;69line = line.trim();70// Skip blank and comment lines71if (line.length() == 0 || line.charAt(0) == '#') {72continue;73}74int comment = line.indexOf('#');75if (comment != -1) {76line = line.substring(0, comment).trim();77}78Scanner sc = new Scanner(line);79String token = sc.next();80Symbol operation = symbol(token);81if (operation == null) {82throw new RuntimeException(lineno() + "wrong op? " + token);83}848586if (operation.type == Symbol.Type.EXCEPTION) {87String className = sc.next();88Class clazz = Exceptions.get(className);89Exception e = result.getException();90if (!clazz.isInstance(e)) {91throw new RuntimeException(lineno() + "unexpected exception: got: " + e92+ ", expected=" + clazz);93}94}9596Exception x = result.getException();97if (x != null) {98throw new RuntimeException(lineno(result.getLineno()) + "Unexpected exception", x);99}100101try {102switch (operation.type) {103case LOCALE:104{105String lang = sc.next();106String country = "", var = "";107if (sc.hasNext()) {108country = sc.next();109if (sc.hasNext()) {110var = sc.next();111}112}113locale = new Locale(lang, country, var);114}115break;116117case TIMEZONE:118{119if (sc.hasNext()) {120String id = sc.next();121timezone = TimeZone.getTimeZone(id);122if (!timezone.getID().equals(id)) {123System.err.printf("Warning: line %d: may get wrong time zone? "124+"(specified: %s vs. actual: %s)%n",125lineno, id, timezone.getID());126}127}128}129break;130131case NEW:132{133Symbol op = symbol(sc.next());134Calendar cal = null;135switch (op.type) {136case INSTANCE:137cal = Calendar.getInstance(timezone, locale);138break;139case GREGORIAN:140cal = new GregorianAdapter(timezone, locale);141break;142default:143symbolError(op);144break;145}146cal.setLenient(leniency);147calendar = new CalendarAdapter(cal);148if (sc.hasNext()) {149String name = sc.next();150cals.put(name.toLowerCase(Locale.ROOT), calendar);151if (!leniency) {152System.out.printf("%s%s is non-lenient%n", lineno(), name);153}154} else {155throw new RuntimeException(lineno() + "Missing associated name");156}157}158break;159160case TEST:161testCount++;162if (sc.hasNext()) {163System.out.printf("Test#%d:%s%n", testCount, sc.findInLine(".+"));164} else {165System.out.printf("Test#%d:%n", testCount);166}167break;168169case USE:170{171String name = sc.next().toLowerCase(Locale.ROOT);172CalendarAdapter c = cals.get(name);173if (c == null) {174throw new CalendarTestException(lineno() + "calendar " + name175+ " not found.");176}177calendar = c;178}179break;180181case ASSIGN:182{183long v = getLong(sc);184String to = sc.next().toLowerCase(Locale.ROOT);185boolean assign = true;186if (sc.hasNext()) {187Symbol condition = symbol(sc.next());188if (condition.type == Symbol.Type.IF) {189long v1 = getLong(sc);190Symbol op = symbol(sc.next());191long v2 = getLong(sc);192assign = relation(v1, op, v2);193} else {194symbolError(condition);195}196}197if (assign)198Variable.newVar(to, v);199}200break;201202case EVAL:203{204long v1 = getLong(sc);205String op = sc.next();206Symbol operator = symbol(op);207if (operator == null) {208throw new RuntimeException("op " + op + " invalid");209}210long v2 = getLong(sc);211if (operator.isArithmetic()) {212long value = 0;213switch (operator.type) {214case PLUS:215value = v1 + v2;216break;217218case MINUS:219value = v1 - v2;220break;221222case MULTIPLY:223value = v1 * v2;224break;225226case DIVIDE:227value = v1 / v2;228break;229230case MOD:231value = v1 % v2;232break;233234default:235symbolError(operator);236break;237}238result.setValue(value);239} else {240if (!relation(v1, operator, v2)) {241throw new RuntimeException("not " + v1 + " " + op + " " + v2);242}243}244}245break;246247case CLEAR:248{249Symbol sym = symbol(sc.next());250if (sym.type == Symbol.Type.ALL) {251calendar.clearAll();252} else if (sym.type == Symbol.Type.FIELD) {253int f = sym.value();254calendar.clearField(f);255} else {256symbolError(sym);257}258}259break;260261case GET:262{263Symbol sym = symbol(sc.next());264switch (sym.type) {265case FIELD:266{267int f = sym.value();268int v = calendar.get(f);269result.setValue(v);270}271break;272273case MILLIS:274{275long v = calendar.getTimeInMillis();276result.setValue(v);277}278break;279280case MINIMUM:281{282int f = getInt(sc);283int v = calendar.getMinimum(f);284result.setValue(v);285}286break;287288case GREATESTMINIMUM:289{290int f = getInt(sc);291int v = calendar.getGreatestMinimum(f);292result.setValue(v);293}294break;295296case ACTUALMINIMUM:297{298int f = getInt(sc);299int v = calendar.getActualMinimum(f);300result.setValue(v);301}302break;303304case MAXIMUM:305{306int f = getInt(sc);307int v = calendar.getMaximum(f);308result.setValue(v);309}310break;311312case LEASTMAXIMUM:313{314int f = getInt(sc);315int v = calendar.getLeastMaximum(f);316result.setValue(v);317}318break;319320case ACTUALMAXIMUM:321{322int f = getInt(sc);323int v = calendar.getActualMaximum(f);324result.setValue(v);325}326break;327328case FIRSTDAYOFWEEK:329{330result.setValue(calendar.getFirstDayOfWeek());331}332break;333334case MINIMALDAYSINFIRSTWEEK:335{336int v = calendar.getMinimalDaysInFirstWeek();337result.setValue(v);338}339break;340341default:342symbolError(sym);343break;344}345}346break;347348case ADD:349case ROLL:350{351Symbol sym = symbol(sc.next());352if (sym.type == Symbol.Type.FIELD) {353int f = sym.value();354int v = sc.nextInt();355switch (operation.type) {356case ADD:357calendar.add(f, v);358break;359360case ROLL:361calendar.roll(f, v);362break;363}364} else {365symbolError(sym);366}367}368break;369370case SET:371{372Symbol sym = symbol(sc.next());373switch (sym.type) {374case FIELD:375{376int f = sym.value();377int v = getInt(sc);378calendar.set(f, v);379}380break;381382case MILLIS:383{384long v = getLong(sc);385calendar.setTimeInMillis(v);386}387break;388389case DATE:390{391int a = getInt(sc);392int b = getInt(sc);393int c = getInt(sc);394if (sc.hasNext()) {395int d = getInt(sc);396// era, year, month, dayOfMonth397calendar.setDate(a, b, c, d);398} else {399// year, month, dayOfMonth400calendar.setDate(a, b, c);401}402}403break;404405case DATETIME:406{407int y = getInt(sc);408int m = getInt(sc);409int d = getInt(sc);410int hh = getInt(sc);411int mm = getInt(sc);412int ss = getInt(sc);413calendar.setDateTime(y, m, d, hh, mm, ss);414}415break;416417case TIMEOFDAY:418{419int hh = getInt(sc);420int mm = getInt(sc);421int ss = getInt(sc);422int ms = getInt(sc);423calendar.setTimeOfDay(hh, mm, ss, ms);424}425break;426427case FIRSTDAYOFWEEK:428{429int v = getInt(sc);430calendar.setFirstDayOfWeek(v);431}432break;433434case MINIMALDAYSINFIRSTWEEK:435{436int v = getInt(sc);437calendar.setMinimalDaysInFirstWeek(v);438}439break;440441case LENIENT:442if (calendar != null) {443calendar.setLenient(true);444}445leniency = true;446break;447448case NONLENIENT:449if (calendar != null) {450calendar.setLenient(false);451}452leniency = false;453break;454455default:456symbolError(sym);457}458if (sc.hasNext()) {459throw new RuntimeException(lineno() + "extra param(s) "460+ sc.findInLine(".+"));461}462}463break;464465case CHECK:466{467Symbol sym = symbol(sc.next());468boolean stat = false;469switch (sym.type) {470case MILLIS:471{472long millis = getLong(sc);473stat = calendar.checkMillis(millis);474}475break;476477case FIELD:478{479int f = sym.value();480int v = getInt(sc);481stat = calendar.checkField(f, v);482}483break;484485case DATE:486{487int a = getInt(sc);488int b = getInt(sc);489int c = getInt(sc);490if (sc.hasNext()) {491int d = getInt(sc);492// era year month dayOfMonth493stat = calendar.checkDate(a, b, c, d);494} else {495// year month dayOfMonth496stat = calendar.checkDate(a, b, c);497}498}499break;500501case DATETIME:502{503int y = getInt(sc);504int m = getInt(sc);505int d = getInt(sc);506int hh = getInt(sc);507int mm = getInt(sc);508int ss = getInt(sc);509if (sc.hasNext()) {510int ms = getInt(sc);511stat = calendar.checkDateTime(y, m, d, hh, mm, ss, ms);512} else {513stat = calendar.checkDateTime(y, m, d, hh, mm, ss);514}515}516break;517518case TIMEOFDAY:519{520int hh = sc.nextInt();521int mm = sc.nextInt();522int ss = sc.nextInt();523int millis = sc.nextInt();524stat = calendar.checkTimeOfDay(hh, mm, ss, millis);525}526break;527528case MINIMUM:529{530int f = getInt(sc);531int v = getInt(sc);532stat = calendar.checkMinimum(f, v);533}534break;535536case GREATESTMINIMUM:537{538int f = getInt(sc);539int v = getInt(sc);540stat = calendar.checkGreatestMinimum(f, v);541}542break;543544case ACTUALMINIMUM:545{546int f = getInt(sc);547int v = getInt(sc);548stat = calendar.checkActualMinimum(f, v);549}550break;551552case MAXIMUM:553{554int f = getInt(sc);555int v = getInt(sc);556stat = calendar.checkMaximum(f, v);557}558break;559560case LEASTMAXIMUM:561{562int f = getInt(sc);563int v = getInt(sc);564stat = calendar.checkLeastMaximum(f, v);565}566break;567568case ACTUALMAXIMUM:569{570int f = getInt(sc);571int v = getInt(sc);572stat = calendar.checkActualMaximum(f, v);573}574break;575576default:577throw new RuntimeException(lineno() + "Unknown operand");578}579if (!stat) {580throw new RuntimeException(lineno() + calendar.getMessage());581}582}583break;584585case PRINT:586{587String s = sc.next();588if (s.charAt(0) == '$') {589Variable var = variable(s);590if (var == null)591throw new RuntimeException(lineno() + "Unknown token: " + s);592System.out.printf("%s%s=%d%n", lineno(), s, var.longValue());593break;594}595596Symbol sym = symbol(s);597switch (sym.type) {598case INSTANCE:599{600Calendar cal = calendar;601String name = "current";602if (sc.hasNext()) {603name = sc.next();604cal = cals.get(name.toLowerCase(Locale.ROOT));605}606System.out.printf("%s%s=%s%n", lineno(), name, cal);607}608break;609610case FIELD:611{612int f = sym.value();613String remark = "";614if (sc.hasNext()) {615remark = sc.findInLine(".+");616}617System.out.printf("%s%s=%d %s%n", lineno(), calendar.fieldName(f),618calendar.get(f), remark);619}620break;621622case MILLIS:623{624String remark = "";625if (sc.hasNext()) {626remark = sc.findInLine(".+");627}628System.out.printf("%sMillis=%d %s%n", lineno(),629calendar.getTimeInMillis(), remark);630}631break;632633case MINIMUM:634System.out.printf("%s%s=%d%n", lineno(),635s, calendar.getMinimum(getInt(sc)));636break;637638case GREATESTMINIMUM:639System.out.printf("%s%s=%d%n", lineno(),640s, calendar.getGreatestMinimum(getInt(sc)));641break;642643case ACTUALMINIMUM:644System.out.printf("%s%s=%d%n", lineno(),645s, calendar.getActualMinimum(getInt(sc)));646break;647648case MAXIMUM:649System.out.printf("%s%s=%d%n", lineno(),650s, calendar.getMaximum(getInt(sc)));651break;652653case LEASTMAXIMUM:654System.out.printf("%s%s=%d%n", lineno(),655s, calendar.getLeastMaximum(getInt(sc)));656break;657658case ACTUALMAXIMUM:659System.out.printf("%s%s=%d%n", lineno(),660s, calendar.getActualMaximum(getInt(sc)));661break;662663case DATE:664System.out.println(lineno() + calendar.toDateString());665break;666667case DATETIME:668System.out.println(lineno() + calendar.toDateTimeString());669break;670671case TIMEZONE:672System.out.println(lineno() + "timezone=" + timezone);673break;674675case LOCALE:676System.out.println(lineno() + "locale=" + locale);677break;678}679}680}681} catch (CalendarTestException cte) {682throw cte;683} catch (NoSuchElementException nsee) {684throw new NoSuchElementException(lineno() + "syntax error");685} catch (Exception e) {686result.setException(e);687result.setLineno(lineno);688}689}690Exception x = result.getException();691if (x != null) {692throw new RuntimeException(lineno(result.getLineno()) + "Unexpected exception", x);693}694}695696private static Symbol symbol(String s) {697return Symbol.get(s.toLowerCase(Locale.ROOT));698}699700private static Variable variable(String s) {701return Variable.get(s.toLowerCase(Locale.ROOT));702}703704private static int getInt(Scanner sc) {705if (sc.hasNextInt()) {706return sc.nextInt();707}708709String s = sc.next();710if (s.charAt(0) == '$') {711Variable var = variable(s);712if (var == null)713throw new RuntimeException(lineno() + "Unknown token: " + s);714return var.intValue();715}716Symbol sym = symbol(s);717if (sym == null)718throw new RuntimeException(lineno() + "Unknown token: " + s);719return sym.value();720}721722private static long getLong(Scanner sc) {723if (sc.hasNextLong()) {724return sc.nextLong();725}726727String s = sc.next();728if (s.charAt(0) == '$') {729Variable var = variable(s);730if (var == null)731throw new RuntimeException(lineno() + "Unknown token: " + s);732return var.longValue();733}734Symbol sym = symbol(s);735if (sym == null)736throw new RuntimeException(lineno() + "Unknown token: " + s);737return sym.value();738}739740private static boolean relation(long v1, Symbol relop, long v2) {741boolean result = false;742switch (relop.type) {743case GT:744result = v1 > v2;745break;746747case GE:748result = v1 >= v2;749break;750751case EQ:752result = v1 == v2;753break;754755case NEQ:756result = v1 != v2;757break;758759case LE:760result = v1 <= v2;761break;762763case LT:764result = v1 < v2;765break;766}767return result;768}769770private static String lineno() {771return lineno(lineno);772}773774private static String lineno(int ln) {775return file.getName() + ":" + ln + ": ";776}777778private static void symbolError(Symbol sym) {779throw new RuntimeException(lineno + ": unexpected symbol: " + sym);780}781}782783784