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  * Abstract test base for Factory related Tests, this class extends DNSTestBase.
  26  *
  27  * This test base will also been referenced outside of current open test folder,
  28  * please double check all usages when modify this.
  29  *
  30  * @see DNSTestBase
  31  * @see TestBase
  32  */
  33 public abstract class LookupFactoryBase extends DNSTestBase {
  34     // pre defined attribute value for 'A'
  35     public static final String ATTRIBUTE_VALUE = "1.2.4.1";
  36 
  37     public static final String DNS_LOOKUP_ATTR = "com.sun.jndi.dns.lookup.attr";
  38 
  39     private String key;
  40 
  41     public LookupFactoryBase() {
  42         // set default test data
  43         setKey("host1");
  44     }
  45 
  46     /**
  47      * Overload method of verifyLookupObject, specify class type TestDnsObject.
  48      *
  49      * @param obj given lookup returned object
  50      */
  51     public void verifyLookupObject(Object obj) {
  52         verifyLookupObject(obj, TestDnsObject.class);
  53     }
  54 
  55     /**
  56      * Verify given lookup returned object whether match class type, will throw
  57      * RuntimeException if verify failed.
  58      *
  59      * @param obj       given lookup returned object
  60      * @param classType given class type
  61      * @param <T>       given type
  62      */
  63     public <T> void verifyLookupObject(Object obj, Class<T> classType) {
  64         DNSTestUtils.debug("Object is: " + obj);
  65         if (!classType.isInstance(obj)) {
  66             throw new RuntimeException(
  67                     "Failed: expecting a " + classType + ", but actual is "
  68                             + obj.getClass());
  69         }
  70     }
  71 
  72     /**
  73      * Verify given lookup returned object match class type TestDnsObject, then
  74      * verify the object toString() value match with given value, will throw
  75      * RuntimeException if any verification step failed.
  76      *
  77      * @param obj   given lookup returned object
  78      * @param value given expected value
  79      */
  80     public void verifyLookupObjectAndValue(Object obj, String value) {
  81         verifyLookupObject(obj);
  82         if (!obj.toString().equals(value)) {
  83             throw new RuntimeException(
  84                     "Unexpected object value: " + obj.toString() + " expected: "
  85                             + value);
  86         }
  87     }
  88 
  89     public String getKey() {
  90         return key;
  91     }
  92 
  93     public void setKey(String key) {
  94         this.key = key;
  95     }
  96 }