001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.junit.tools;
017:
018: import com.google.gwt.user.tools.util.ArgHandlerEclipse;
019: import com.google.gwt.user.tools.util.ArgHandlerIgnore;
020: import com.google.gwt.user.tools.util.ArgHandlerOverwrite;
021: import com.google.gwt.util.tools.ArgHandlerExtra;
022: import com.google.gwt.util.tools.ArgHandlerOutDir;
023: import com.google.gwt.util.tools.ArgHandlerString;
024: import com.google.gwt.util.tools.ToolBase;
025: import com.google.gwt.util.tools.Utility;
026:
027: import java.io.File;
028: import java.io.IOException;
029: import java.util.HashMap;
030: import java.util.Map;
031:
032: /**
033: * Tool to create JUnit test case.
034: *
035: */
036: public final class JUnitCreator extends ToolBase {
037:
038: /**
039: * Arg Handler for <code>JUnitCreator</code>.
040: */
041: protected class ArgHandlerTestClass extends ArgHandlerExtra {
042:
043: @Override
044: public boolean addExtraArg(String arg) {
045: if (fullClassName != null) {
046: System.err.println("Too many arguments.");
047: return false;
048: }
049:
050: // Check className for certain properties
051: if (!arg.matches("[\\w\\$]+(\\.[\\w\\$]+)+")) {
052: System.err
053: .println("'"
054: + arg
055: + "' does not appear to be a valid fully-qualified Java class name.");
056: return false;
057: }
058:
059: // Check out the class name.
060: //
061: if (arg.indexOf('$') != -1) {
062: System.err
063: .println("'"
064: + arg
065: + "': This version of the tool does not support nested classes");
066: return false;
067: }
068:
069: String[] parts = arg.split("\\.");
070: if (parts.length < 2) {
071: System.err
072: .println("'"
073: + arg
074: + "': Cannot live in the root package. Please specify a package.");
075: return false;
076: }
077:
078: fullClassName = arg;
079: return true;
080: }
081:
082: @Override
083: public String getPurpose() {
084: return "The fully-qualified name of the test class to create";
085: }
086:
087: @Override
088: public String[] getTagArgs() {
089: return new String[] { "className" };
090: }
091:
092: @Override
093: public boolean isRequired() {
094: return true;
095: }
096: }
097:
098: private static final String PACKAGE_PATH;
099:
100: static {
101: String path = JUnitCreator.class.getName();
102: path = path.substring(0, path.lastIndexOf('.') + 1);
103: PACKAGE_PATH = path.replace('.', '/');
104: }
105:
106: public static void main(String[] args) {
107: JUnitCreator creator = new JUnitCreator();
108: if (creator.processArgs(args)) {
109: if (creator.run()) {
110: return;
111: }
112: }
113:
114: System.exit(1);
115: }
116:
117: /**
118: * @param junitPath the path to the user's junit jar
119: * @param moduleName the name of the module to contain this test
120: * @param fullClassName Name of the fully-qualified Java class to create as an
121: * Application.
122: * @param outDir Where to put the output files
123: * @param eclipse The name of a project to attach a .launch config to
124: * @param overwrite Overwrite an existing files if they exist.
125: * @param ignore Ignore existing files if they exist.
126: * @throws IOException
127: */
128: static void createTest(String junitPath, String moduleName,
129: String fullClassName, File outDir, String eclipse,
130: boolean overwrite, boolean ignore) throws IOException {
131:
132: // Figure out the installation directory
133: String installPath = Utility.getInstallPath();
134: String gwtUserPath = installPath + '/' + "gwt-user.jar";
135: String gwtDevPath = installPath + '/' + Utility.getDevJarName();
136:
137: // Figure out what platform we're on
138: //
139: boolean isWindows = gwtDevPath.substring(
140: gwtDevPath.lastIndexOf('/') + 1).indexOf("windows") >= 0;
141: boolean isMacOsX = gwtDevPath.substring(
142: gwtDevPath.lastIndexOf('/') + 1).indexOf("mac") >= 0;
143:
144: // If the path from here to the install directory is relative, we need to
145: // set specific "base" directory tags; this is for sample generation during
146: // the build.
147: String basePathEnv;
148: if (!new File(installPath).isAbsolute()) {
149: if (isWindows) {
150: basePathEnv = "%~dp0\\";
151: } else {
152: basePathEnv = "$APPDIR/";
153: }
154: } else {
155: basePathEnv = "";
156: }
157:
158: // Check if junit path is absolute, add base if needed
159: if (!new File(junitPath).isAbsolute()
160: && junitPath.charAt(0) != File.separatorChar) {
161: if (isWindows) {
162: junitPath = "%~dp0\\" + junitPath;
163: } else {
164: junitPath = "$APPDIR/" + junitPath;
165: }
166: }
167:
168: // Check out the class and package names.
169: //
170: int pos = fullClassName.lastIndexOf('.');
171: String clientPackageName = fullClassName.substring(0, pos);
172: String className = fullClassName.substring(pos + 1);
173:
174: // Is the requested moduleName in a parent package of the clientPackage?
175: //
176: pos = moduleName.lastIndexOf('.');
177: if (pos >= 0) {
178: String modulePackageName = moduleName.substring(0, pos);
179: if (modulePackageName.length() == clientPackageName
180: .length()
181: || !clientPackageName
182: .startsWith(modulePackageName + '.')) {
183: System.err.println("Warning: '" + modulePackageName
184: + "' is not a parent package of '"
185: + clientPackageName + "'. The source for '"
186: + className + "' may be unavailable.");
187: }
188: }
189:
190: // Compute module name and directories
191: //
192: pos = clientPackageName.lastIndexOf('.');
193: File clientDir = Utility.getDirectory(outDir, "test", true);
194: if (pos >= 0) {
195: String clientPackage = clientPackageName.replace('.', '/');
196: clientDir = Utility.getDirectory(clientDir, clientPackage,
197: true);
198: }
199:
200: // Create a map of replacements
201: //
202: Map<String, String> replacements = new HashMap<String, String>();
203: replacements.put("@className", className);
204: replacements.put("@moduleName", moduleName);
205: replacements.put("@clientPackage", clientPackageName);
206: replacements.put("@junitPath", junitPath);
207: replacements.put("@gwtUserPath", basePathEnv + gwtUserPath);
208: replacements.put("@gwtDevPath", basePathEnv + gwtDevPath);
209: replacements.put("@vmargs", isMacOsX ? "-XstartOnFirstThread"
210: : "");
211:
212: {
213: // Create a skeleton Test class
214: File javaClass = Utility.createNormalFile(clientDir,
215: className + ".java", overwrite, ignore);
216: if (javaClass != null) {
217: String out = Utility.getFileFromClassPath(PACKAGE_PATH
218: + "JUnitClassTemplate.javasrc");
219: Utility.writeTemplateFile(javaClass, out, replacements);
220: }
221: }
222:
223: if (eclipse != null) {
224: // Create an eclipse launch config
225: replacements.put("@projectName", eclipse);
226: File hostedConfig = Utility.createNormalFile(outDir,
227: className + "-hosted.launch", overwrite, ignore);
228: if (hostedConfig != null) {
229: String out = Utility.getFileFromClassPath(PACKAGE_PATH
230: + "JUnit-hosted.launchsrc");
231: Utility.writeTemplateFile(hostedConfig, out,
232: replacements);
233: }
234:
235: File webConfig = Utility.createNormalFile(outDir, className
236: + "-web.launch", overwrite, ignore);
237: if (webConfig != null) {
238: String out = Utility.getFileFromClassPath(PACKAGE_PATH
239: + "JUnit-web.launchsrc");
240: Utility.writeTemplateFile(webConfig, out, replacements);
241: }
242: }
243:
244: // create startup files
245: String extension;
246: if (isWindows) {
247: extension = ".cmd";
248: } else {
249: extension = "";
250: }
251:
252: File junitHosted = Utility.createNormalFile(outDir, className
253: + "-hosted" + extension, overwrite, ignore);
254: if (junitHosted != null) {
255: String out = Utility.getFileFromClassPath(PACKAGE_PATH
256: + "junit-hosted" + extension + "src");
257: Utility.writeTemplateFile(junitHosted, out, replacements);
258: if (extension.length() == 0) {
259: Runtime.getRuntime().exec(
260: "chmod u+x " + junitHosted.getAbsolutePath());
261: }
262: }
263:
264: File junitWeb = Utility.createNormalFile(outDir, className
265: + "-web" + extension, overwrite, ignore);
266: if (junitWeb != null) {
267: String out = Utility.getFileFromClassPath(PACKAGE_PATH
268: + "junit-web" + extension + "src");
269: Utility.writeTemplateFile(junitWeb, out, replacements);
270: if (extension.length() == 0) {
271: Runtime.getRuntime().exec(
272: "chmod u+x " + junitWeb.getAbsolutePath());
273: }
274: }
275: }
276:
277: private String eclipse = null;
278:
279: private String fullClassName = null;
280:
281: private boolean ignore = false;
282: private String junitPath = null;
283: private String moduleName = null;
284: private File outDir;
285: private boolean overwrite = false;
286:
287: protected JUnitCreator() {
288:
289: registerHandler(new ArgHandlerString() {
290:
291: @Override
292: public String[] getDefaultArgs() {
293: return null;
294: }
295:
296: @Override
297: public String getPurpose() {
298: return "Specify the path to your junit.jar (required)";
299: }
300:
301: @Override
302: public String getTag() {
303: return "-junit";
304: }
305:
306: @Override
307: public String[] getTagArgs() {
308: return new String[] { "pathToJUnitJar" };
309: }
310:
311: @Override
312: public boolean isRequired() {
313: return true;
314: }
315:
316: @Override
317: public boolean setString(String str) {
318: File f = new File(str);
319: if (!f.exists() || !f.isFile()) {
320: System.err.println("File not found: " + str);
321: return false;
322: }
323: junitPath = str;
324: return true;
325: }
326: });
327:
328: registerHandler(new ArgHandlerString() {
329:
330: @Override
331: public String[] getDefaultArgs() {
332: return null;
333: }
334:
335: @Override
336: public String getPurpose() {
337: return "Specify the name of the GWT module to use (required)";
338: }
339:
340: @Override
341: public String getTag() {
342: return "-module";
343: }
344:
345: @Override
346: public String[] getTagArgs() {
347: return new String[] { "moduleName" };
348: }
349:
350: @Override
351: public boolean isRequired() {
352: return true;
353: }
354:
355: @Override
356: public boolean setString(String str) {
357: moduleName = str;
358: return true;
359: }
360: });
361:
362: registerHandler(new ArgHandlerEclipse() {
363: @Override
364: public String getPurpose() {
365: return "Creates a debug launch config for the named eclipse project";
366: }
367:
368: @Override
369: public boolean setString(String str) {
370: eclipse = str;
371: return true;
372: }
373: });
374:
375: registerHandler(new ArgHandlerOutDir() {
376:
377: @Override
378: public void setDir(File dir) {
379: outDir = dir;
380: }
381: });
382:
383: registerHandler(new ArgHandlerOverwrite() {
384:
385: @Override
386: public boolean setFlag() {
387: if (ignore) {
388: System.err
389: .println("-overwrite cannot be used with -ignore.");
390: return false;
391: }
392: overwrite = true;
393: return true;
394: }
395: });
396:
397: registerHandler(new ArgHandlerIgnore() {
398:
399: @Override
400: public boolean setFlag() {
401: if (overwrite) {
402: System.err
403: .println("-ignore cannot be used with -overwrite.");
404: return false;
405: }
406: ignore = true;
407: return true;
408: }
409: });
410:
411: registerHandler(new ArgHandlerTestClass());
412: }
413:
414: protected boolean run() {
415: try {
416: createTest(junitPath, moduleName, fullClassName, outDir,
417: eclipse, overwrite, ignore);
418: return true;
419: } catch (IOException e) {
420: System.err.println(e.getClass().getName() + ": "
421: + e.getMessage());
422: return false;
423: }
424: }
425: }
|