< prev index next >

src/com/sun/javatest/lib/TestCases.java

Print this page
rev 145 : 7902237: Fixing raw use of parameterized class
Reviewed-by: jjg


 121      * Explicitly exclude a set of test cases by name. Subsequent calls
 122      * are cumulative; by default, no test cases are excluded.
 123      * @param testCaseNames an array of test cases names.
 124      * Each name must identify a method in the test object, that takes
 125      * no arguments and returns a {@link com.sun.javatest.Status status}.
 126      * @throws TestCases.Fault if any of the test case names are invalid.
 127      */
 128     public void exclude(String[] testCaseNames) throws Fault  {
 129         for (int i = 0; i < testCaseNames.length; i++) {
 130             String t = testCaseNames[i];
 131             excludedCases.put(t, getTestCase(t));
 132         }
 133     }
 134 
 135 
 136     /**
 137      * Return an enumeration of the selected test cases, based on the
 138      * select and exclude calls that have been made, if any.
 139      * @return An enumeration of the test cases.
 140      */
 141     public Enumeration enumerate() {
 142         Vector<Method> v = new Vector<>();
 143         if (selectedCases.isEmpty()) {
 144             Method[] methods = testClass.getMethods();
 145             for (int i = 0; i < methods.length; i++) {
 146                 Method m = methods[i];
 147                 if (excludedCases.get(m.getName()) == null) {
 148                     Class[] paramTypes = m.getParameterTypes();
 149                     Class returnType = m.getReturnType();
 150                     if ((paramTypes.length == 0) && Status.class.isAssignableFrom(returnType))
 151                         v.addElement(m);
 152                 }
 153             }
 154         }
 155         else {
 156             for (Method m : selectedCases.values()) {
 157                 if (excludedCases.get(m.getName()) == null)
 158                     v.addElement(m);
 159                 }
 160         }
 161         return v.elements();
 162     }
 163 
 164 
 165     /**
 166      * Invoke each of the selected test cases, based upon the select and exclude
 167      * calls that have been made, if any.
 168      * If the test object provides a public method
 169      * {@link com.sun.javatest.Status}invokeTestCase({@link java.lang.reflect.Method})
 170      * that method will be called to invoke the test cases; otherwise, the test
 171      * cases will be invoked directly.
 172      * It is an error if no test cases are selected, (or if they have all been excluded.)
 173      * @return the combined result of executing all the test cases.
 174      */
 175     public Status invokeTestCases() {
 176         // see if test object provides  Status invokeTestCase(Method m)
 177         Method invoker;
 178         try {
 179             invoker = testClass.getMethod("invokeTestCase", new Class[] {Method.class});
 180             if (!Status.class.isAssignableFrom(invoker.getReturnType()))
 181                 invoker = null;
 182         }
 183         catch (NoSuchMethodException e) {
 184             invoker = null;
 185         }
 186 
 187         MultiStatus ms = new MultiStatus(log);
 188         for (Enumeration e = enumerate(); e.hasMoreElements(); ) {
 189             Method m = (Method)(e.nextElement());
 190             Status s;
 191             try {
 192                 if (invoker != null)
 193                     s = (Status)invoker.invoke(test, new Object[] {m});
 194                 else
 195                     s = (Status)m.invoke(test, noArgs);
 196             }
 197             catch (IllegalAccessException ex) {
 198                 s = Status.failed("Could not access test case: " + m.getName());
 199             }
 200             catch (InvocationTargetException ex) {
 201                 printStackTrace(ex.getTargetException());
 202                 s = Status.failed("Exception from test case: " +
 203                                        ex.getTargetException().toString());
 204             }
 205             catch (ThreadDeath t) {
 206                 printStackTrace(t);
 207                 throw t;
 208             }
 209             catch (Throwable t) {


 254         Vector<String> v = new Vector<>();
 255         int start = 0;
 256         for (int i = s.indexOf(','); i != -1; i = s.indexOf(',', start)) {
 257             v.addElement(s.substring(start, i));
 258             start = i + 1;
 259         }
 260         if (start != s.length())
 261             v.addElement(s.substring(start));
 262         String[] ss = new String[v.size()];
 263         v.copyInto(ss);
 264         return ss;
 265     }
 266 
 267     private Object test;
 268     private Class<?> testClass;
 269     private Map<String, Method> selectedCases = new Hashtable<>();
 270     private Map<String, Method> excludedCases = new Hashtable<>();
 271     private PrintWriter log;
 272 
 273     private static final Object[] noArgs = { };
 274     private static final Class[] noArgTypes = { };
 275 }


 121      * Explicitly exclude a set of test cases by name. Subsequent calls
 122      * are cumulative; by default, no test cases are excluded.
 123      * @param testCaseNames an array of test cases names.
 124      * Each name must identify a method in the test object, that takes
 125      * no arguments and returns a {@link com.sun.javatest.Status status}.
 126      * @throws TestCases.Fault if any of the test case names are invalid.
 127      */
 128     public void exclude(String[] testCaseNames) throws Fault  {
 129         for (int i = 0; i < testCaseNames.length; i++) {
 130             String t = testCaseNames[i];
 131             excludedCases.put(t, getTestCase(t));
 132         }
 133     }
 134 
 135 
 136     /**
 137      * Return an enumeration of the selected test cases, based on the
 138      * select and exclude calls that have been made, if any.
 139      * @return An enumeration of the test cases.
 140      */
 141     public Enumeration<Method> enumerate() {
 142         Vector<Method> v = new Vector<>();
 143         if (selectedCases.isEmpty()) {
 144             Method[] methods = testClass.getMethods();
 145             for (int i = 0; i < methods.length; i++) {
 146                 Method m = methods[i];
 147                 if (excludedCases.get(m.getName()) == null) {
 148                     Class<?>[] paramTypes = m.getParameterTypes();
 149                     Class<?> returnType = m.getReturnType();
 150                     if ((paramTypes.length == 0) && Status.class.isAssignableFrom(returnType))
 151                         v.addElement(m);
 152                 }
 153             }
 154         }
 155         else {
 156             for (Method m : selectedCases.values()) {
 157                 if (excludedCases.get(m.getName()) == null)
 158                     v.addElement(m);
 159                 }
 160         }
 161         return v.elements();
 162     }
 163 
 164 
 165     /**
 166      * Invoke each of the selected test cases, based upon the select and exclude
 167      * calls that have been made, if any.
 168      * If the test object provides a public method
 169      * {@link com.sun.javatest.Status}invokeTestCase({@link java.lang.reflect.Method})
 170      * that method will be called to invoke the test cases; otherwise, the test
 171      * cases will be invoked directly.
 172      * It is an error if no test cases are selected, (or if they have all been excluded.)
 173      * @return the combined result of executing all the test cases.
 174      */
 175     public Status invokeTestCases() {
 176         // see if test object provides  Status invokeTestCase(Method m)
 177         Method invoker;
 178         try {
 179             invoker = testClass.getMethod("invokeTestCase", new Class<?>[] {Method.class});
 180             if (!Status.class.isAssignableFrom(invoker.getReturnType()))
 181                 invoker = null;
 182         }
 183         catch (NoSuchMethodException e) {
 184             invoker = null;
 185         }
 186 
 187         MultiStatus ms = new MultiStatus(log);
 188         for (Enumeration<Method> e = enumerate(); e.hasMoreElements(); ) {
 189             Method m = (e.nextElement());
 190             Status s;
 191             try {
 192                 if (invoker != null)
 193                     s = (Status)invoker.invoke(test, new Object[] {m});
 194                 else
 195                     s = (Status)m.invoke(test, noArgs);
 196             }
 197             catch (IllegalAccessException ex) {
 198                 s = Status.failed("Could not access test case: " + m.getName());
 199             }
 200             catch (InvocationTargetException ex) {
 201                 printStackTrace(ex.getTargetException());
 202                 s = Status.failed("Exception from test case: " +
 203                                        ex.getTargetException().toString());
 204             }
 205             catch (ThreadDeath t) {
 206                 printStackTrace(t);
 207                 throw t;
 208             }
 209             catch (Throwable t) {


 254         Vector<String> v = new Vector<>();
 255         int start = 0;
 256         for (int i = s.indexOf(','); i != -1; i = s.indexOf(',', start)) {
 257             v.addElement(s.substring(start, i));
 258             start = i + 1;
 259         }
 260         if (start != s.length())
 261             v.addElement(s.substring(start));
 262         String[] ss = new String[v.size()];
 263         v.copyInto(ss);
 264         return ss;
 265     }
 266 
 267     private Object test;
 268     private Class<?> testClass;
 269     private Map<String, Method> selectedCases = new Hashtable<>();
 270     private Map<String, Method> excludedCases = new Hashtable<>();
 271     private PrintWriter log;
 272 
 273     private static final Object[] noArgs = { };
 274     private static final Class<?>[] noArgTypes = { };
 275 }
< prev index next >