1 /*
   2  * $Id$
   3  *
   4  * Copyright (c) 1996, 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 package com.sun.javatest.finder;
  28 
  29 import java.io.File;
  30 import com.sun.javatest.TestDescription;
  31 import com.sun.javatest.TestEnvironment;
  32 import com.sun.javatest.TestFinder;
  33 import com.sun.javatest.util.I18NResourceBundle;
  34 
  35 /**
  36  * A test finder that reads tests from a delegate, and returns the
  37  * results in the reverse order. This is primarily for debugging
  38  * and testing purposes.
  39  */
  40 public class ReverseTestFinder extends TestFinder
  41 {
  42 
  43     /**
  44      * Default constructor
  45      */
  46     public ReverseTestFinder() {
  47     }
  48 
  49     /**
  50      * Constructor to create ReverseTestFinder object when the
  51      * original TestFinder instance is already created and initialized.
  52      * Finders created with this constructor do not require the init() method
  53      * to be invoked.
  54      *
  55      * @param delegate - the real test finder object.
  56      */
  57     public ReverseTestFinder(TestFinder delegate) {
  58         this();
  59         this.delegate = delegate;
  60     }
  61 
  62     /**
  63      * Initialize the test finder.
  64      * @param args The first entry in the array should be the name
  65      *          of the test finder to be used to actually read the tests;
  66      *          subsequent entries in the array will be passed through to
  67      *          the init method for that class.
  68      * @param testSuiteRoot The root file of the test suite to be read.
  69      * @param env An environment for the test finder to use if required.
  70      *          The ReverseTestFinder does not use this value directly;
  71      *          it just passes it on to the test finder to which it
  72      *          delegates the reading.
  73      * @throws TestFinder.Fault if any problems occur during initialization.
  74      */
  75     public synchronized void init(String[] args, File testSuiteRoot,
  76                      TestEnvironment env) throws Fault {
  77          String delegateClassName = args[0];
  78          try {
  79              Class<?>  delegateClass = Class.forName(delegateClassName, true, ClassLoader.getSystemClassLoader());
  80              delegate = (TestFinder)(delegateClass.newInstance());
  81              args = shift(args, 1);
  82              delegate.init(args, testSuiteRoot, env);
  83          }
  84          catch (Throwable t) {
  85              throw new Fault(i18n, "reverse.cantInitDelegate", t);
  86          }
  87     }
  88 
  89     public File getRoot() {
  90         return delegate.getRoot();
  91     }
  92 
  93     public File getRootDir() {
  94         return delegate.getRootDir();
  95     }
  96 
  97     public void read(File file) {
  98         delegate.read(file);
  99     }
 100 
 101     public TestDescription[] getTests() {
 102         TestDescription[] tds = delegate.getTests();
 103         if (tds != null) {
 104             int n = tds.length;
 105             for (int i = 0; i < n/2; i++) {
 106                 TestDescription temp = tds[i];
 107                 tds[i] = tds[n - 1 - i];
 108                 tds[n - 1 - i] = temp;
 109             }
 110         }
 111         return tds;
 112     }
 113 
 114     public File[] getFiles() {
 115         File[] fs = delegate.getFiles();
 116         if (fs != null) {
 117             int n = fs.length;
 118             for (int i = 0; i < n/2; i++) {
 119                 File temp = fs[i];
 120                 fs[i] = fs[n - 1 - i];
 121                 fs[n - 1 - i] = temp;
 122             }
 123         }
 124         return fs;
 125     }
 126 
 127     protected void scan(File file) {
 128         throw new Error("should not be called!");
 129     }
 130 
 131     private String[] shift(String[] args, int n) {
 132         String[] result = new String[args.length - n];
 133         System.arraycopy(args, n, result, 0, result.length);
 134         return result;
 135     }
 136 
 137     private TestFinder delegate;
 138 
 139     private static I18NResourceBundle i18n = I18NResourceBundle.getBundleForClass(TagTestFinder.class);
 140 
 141 }