1 /* 2 * $Id$ 3 * 4 * Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved. 5 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 6 * 7 * This code is free software; you can redistribute it and/or modify it 8 * under the terms of the GNU General Public License version 2 only, as 9 * published by the Free Software Foundation. Oracle designates this 10 * particular file as subject to the "Classpath" exception as provided 11 * by Oracle in the LICENSE file that accompanied this code. 12 * 13 * This code is distributed in the hope that it will be useful, but WITHOUT 14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 16 * version 2 for more details (a copy is included in the LICENSE file that 17 * accompanied this code). 18 * 19 * You should have received a copy of the GNU General Public License version 20 * 2 along with this work; if not, write to the Free Software Foundation, 21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 22 * 23 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 24 * or visit www.oracle.com if you need additional information or have any 25 * questions. 26 */ 27 28 package com.sun.javatest.junit; 29 30 import com.sun.javatest.TestFinder; 31 import com.sun.javatest.finder.CommentStream; 32 import com.sun.javatest.finder.JavaCommentStream; 33 import java.io.File; 34 import java.util.ArrayList; 35 import java.util.HashMap; 36 import java.util.List; 37 import java.util.Map; 38 39 /** 40 * 41 */ 42 public abstract class JUnitTestFinder extends TestFinder { 43 44 /** Creates a new instance of JUnitTestFinder */ 45 public JUnitTestFinder() { 46 } 47 48 /** 49 * Decode the arg at a specified position in the arg array. If overridden 50 * by a subtype, the subtype should try and decode any arg it recognizes, 51 * and then call super.decodeArg to give the superclass(es) a chance to 52 * recognize any arguments. 53 * 54 * @param args The array of arguments 55 * @param i The next argument to be decoded. 56 * @return The number of elements consumed in the array; for example, 57 * for a simple option like "-v" the result should be 1; for an 58 * option with an argument like "-f file" the result should be 59 * 2, etc. 60 * @throws TestFinder.Fault If there is a problem with the value of the current arg, 61 * such as a bad value to an option, the Fault exception can be 62 * thrown. The exception should NOT be thrown if the current 63 * arg is unrecognized: in that case, an implementation should 64 * delegate the call to the supertype. 65 */ 66 protected void decodeAllArgs(String[] args) throws Fault { 67 // supports selection of two modes - 68 // 1 - scan for .java files, use to locate .class file 69 // 2 - just scan for .class files 70 for (int i = 0; i < args.length; i++) { 71 if ("-scanClasses".equalsIgnoreCase(args[i])) { 72 scanClasses = true; 73 addExtension(".class", null); 74 } else if ("-verbose".equalsIgnoreCase(args[i])) { 75 verbose = true; 76 } else 77 super.decodeArg(args, i); 78 } // for 79 80 // this establishes the default if the user did not select 81 // class scanning 82 if (!scanClasses) 83 addExtension(".java", JavaCommentStream.class); 84 } 85 86 /** 87 * Get the name of the file currently being scanned. 88 * @return the name of the file currently being scanned. 89 */ 90 // Ideally, we should be able to get the current line number as well, 91 // (for error messages) 92 protected File getCurrentFile() { 93 return currFile; 94 } 95 96 /** 97 * Exclude all files with a particular name from being scanned. 98 * This will typically be for directories like SCCS, Codemgr_wsdata, etc 99 * @param name The name of files to be excluded. 100 */ 101 public void exclude(String name) { 102 excludeList.put(name, name); 103 } 104 105 /** 106 * Exclude all files with particular names from being scanned. 107 * This will typically be for directories like SCCS, Codemgr_wsdata, etc 108 * @param names The names of files to be excluded. 109 */ 110 public void exclude(String[] names) { 111 for (int i = 0; i < names.length; i++) { 112 String name = names[i]; 113 excludeList.put(name, name); 114 } 115 } 116 117 /** 118 * Nominate a class to read files that have a particular extension. 119 * @param extn The extension for which this class is to be used 120 * @param commentStreamClass 121 * A class to read files of a particular extension. 122 * The class must be a subtype of CommentStream 123 */ 124 public void addExtension(String extn, Class commentStreamClass) { 125 if (!extn.startsWith(".")) 126 throw new IllegalArgumentException("extension must begin with `.'"); 127 if (commentStreamClass != null && !CommentStream.class.isAssignableFrom(commentStreamClass)) 128 throw new IllegalArgumentException("class must be a subtype of " + CommentStream.class.getName()); 129 130 extensionTable.put(extn, commentStreamClass); 131 } 132 133 /** 134 * Get the class used to handle an extension. 135 * @param extn The extension in question 136 * @return the class previously registered with addExtension 137 */ 138 public Class getClassForExtension(String extn) { 139 return extensionTable.get(extn); 140 } 141 142 /** 143 * Call to register the methods which are test methods. 144 */ 145 public void foundTestMethod(String name) { 146 testMethods.add(name); 147 } 148 149 protected boolean verbose = false; 150 protected Map<String, String> tdValues = new HashMap<>(); 151 protected boolean scanClasses = false; 152 protected File currFile; 153 protected Map<String, String> excludeList = new HashMap<>(); 154 protected Map<String, Class> extensionTable = new HashMap<>(); 155 protected List<String> testMethods; 156 protected static final String[] excludeNames = { 157 "SCCS", "deleted_files", ".svn" 158 }; 159 }