1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * The test base class for JNDI related tests
  26  *
  27  * run() will be the entry to launch whole tests, base test sequence is
  28  * initRes(), initTest(), setupTest() and launch()
  29  *
  30  * launch() will call real test logic runTest() which required to be implemented
  31  * in test class
  32  *
  33  * exception handling logic should be placed in handleException()
  34  *
  35  * cleanup related should be placed in cleanupTest which been called in finally
  36  * block
  37  */
  38 public abstract class TestBase {
  39 
  40     /**
  41      * The entry to the test
  42      *
  43      * @param args given input arguments
  44      * @throws Exception if any exception
  45      */
  46     public void run(String[] args) throws Exception {
  47         initRes();
  48         initTest(args);
  49         setupTest();
  50         launch();
  51     }
  52 
  53     /**
  54      * Initial local resources, such as socket
  55      *
  56      * @throws Exception if any exception
  57      */
  58     public void initRes() throws Exception {
  59     }
  60 
  61     /**
  62      * Initial test with given arguments
  63      *
  64      * @param args given arguments
  65      */
  66     public void initTest(String[] args) {
  67     }
  68 
  69     /**
  70      * Setup test
  71      */
  72     public void setupTest() {
  73     }
  74 
  75     /**
  76      * Launch test
  77      *
  78      * @throws Exception if any exception
  79      */
  80     public void launch() throws Exception {
  81         try {
  82             runTest();
  83         } catch (Exception e) {
  84             if (!handleException(e)) {
  85                 throw e;
  86             }
  87         } finally {
  88             cleanupTest();
  89         }
  90     }
  91 
  92     /**
  93      * The real test logic to run, this required to be implemented in test class
  94      *
  95      * @throws Exception if any exception
  96      */
  97     public abstract void runTest() throws Exception;
  98 
  99     /**
 100      * Handle test exception
 101      *
 102      * @param e exception which been thrown during test runTest()
 103      * @return <tt>true</tt> if given exception is expected
 104      */
 105     public boolean handleException(Exception e) {
 106         return false;
 107     }
 108 
 109     /**
 110      * Cleanup test
 111      */
 112     public abstract void cleanupTest();
 113 }