001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (license2)
004: */
005: /*
006: * Licensed to the Apache Software Foundation (ASF) under one or more
007: * contributor license agreements. See the NOTICE file distributed with
008: * this work for additional information regarding copyright ownership.
009: * The ASF licenses this file to You under the Apache License, Version 2.0
010: * (the "License"); you may not use this file except in compliance with
011: * the License. You may obtain a copy of the License at
012: *
013: * http://www.apache.org/licenses/LICENSE-2.0
014: *
015: * Unless required by applicable law or agreed to in writing, software
016: * distributed under the License is distributed on an "AS IS" BASIS,
017: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018: * See the License for the specific language governing permissions and
019: * limitations under the License.
020: */
021: package org.h2.test.trace;
022:
023: import java.io.BufferedReader;
024: import java.io.FileReader;
025: import java.io.IOException;
026: import java.io.LineNumberReader;
027: import java.util.HashMap;
028:
029: /**
030: * This tool can re-run Java style log files. There is no size limit.
031: */
032: public class Player {
033:
034: // TODO support InputStream;
035: // TODO support Reader;
036: // TODO support int[];
037: // TODO support Blob and Clob;
038: // TODO support Calendar
039: // TODO support Object
040: // TODO support Object[]
041: // TODO support URL
042: // TODO support Array
043: // TODO support Ref
044: // TODO support SQLInput, SQLOutput
045: // TODO support Properties
046: // TODO support Map
047: // TODO support SQLXML
048:
049: private boolean log;
050: private static final String[] IMPORTED_PACKAGES = { "",
051: "java.lang.", "java.sql.", "javax.sql." };
052: private HashMap objects = new HashMap();
053:
054: /**
055: * Execute a trace file using the command line. The log file name to execute
056: * (replayed) must be specified as the last parameter. The following
057: * optional command line parameters are supported:
058: * <ul>
059: * <li><code>-log</code> to enable logging the executed statement to
060: * System.out
061: * </ul>
062: *
063: * @param args the arguments of the application
064: */
065: public static void main(String[] args) throws Exception {
066: new Player().run(args);
067: }
068:
069: /**
070: * Execute a trace file.
071: *
072: * @param fileName
073: * @param log print debug information
074: * @param checkResult if the result of each method should be compared
075: * against the result in the file
076: */
077: public static void execute(String fileName, boolean log,
078: boolean checkResult) throws IOException {
079: new Player().runFile(fileName, log);
080: }
081:
082: private void run(String[] args) throws IOException {
083: String fileName = "test.log.db";
084: try {
085: fileName = args[args.length - 1];
086: for (int i = 0; i < args.length - 1; i++) {
087: if ("-log".equals(args[i])) {
088: log = true;
089: } else {
090: throw new Error("Unknown setting: " + args[i]);
091: }
092: }
093: } catch (Exception e) {
094: e.printStackTrace();
095: System.out.println("Usage: java " + getClass().getName()
096: + " [-log] <fileName>");
097: return;
098: }
099: runFile(fileName, log);
100: }
101:
102: private void runFile(String fileName, boolean log)
103: throws IOException {
104: this .log = log;
105: LineNumberReader reader = new LineNumberReader(
106: new BufferedReader(new FileReader(fileName)));
107: while (true) {
108: String line = reader.readLine();
109: if (line == null) {
110: break;
111: }
112: runLine(line.trim());
113: }
114: }
115:
116: void log(String s) {
117: if (log) {
118: System.out.println(s);
119: }
120: }
121:
122: private void runLine(String line) {
123: if (!line.startsWith("/**/")) {
124: return;
125: }
126: line = line.substring("/**/".length()) + ";";
127: Statement s = Parser.parseStatement(this , line);
128: log("> " + s.toString());
129: try {
130: s.execute();
131: } catch (Exception e) {
132: e.printStackTrace();
133: log("error: " + e.toString());
134: }
135: }
136:
137: static Class getClass(String className) {
138: for (int i = 0; i < IMPORTED_PACKAGES.length; i++) {
139: try {
140: return Class.forName(IMPORTED_PACKAGES[i] + className);
141: } catch (ClassNotFoundException e) {
142: }
143: }
144: throw new Error("Class not found: " + className);
145: }
146:
147: void assign(String objectName, Object obj) {
148: objects.put(objectName, obj);
149: }
150:
151: Object getObject(String name) {
152: return objects.get(name);
153: }
154:
155: }
|