test/jdk/com/sun/jndi/dns/EnvTests/EnvTestBase.java
changeset 51331 7939b3c4e408
equal deleted inserted replaced
51330:edd33b9946ca 51331:7939b3c4e408
       
     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 import javax.naming.Context;
       
    25 import javax.naming.NamingException;
       
    26 import javax.naming.directory.Attributes;
       
    27 import java.util.Hashtable;
       
    28 
       
    29 /**
       
    30  * Abstract test base for most of Env Tests, this class extends DNSTestBase.
       
    31  *
       
    32  * @see DNSTestBase
       
    33  * @see TestBase
       
    34  */
       
    35 abstract class EnvTestBase extends DNSTestBase {
       
    36     private static final String[] MANDATORY_ATTRIBUTES = { "A", "MX", "HINFO",
       
    37             "TXT", "29" };
       
    38     private static final String[] OPTIONAL_ATTRIBUTES = {};
       
    39 
       
    40     private String key;
       
    41     private String fqdnUrl;
       
    42     private String foreignFqdnUrl;
       
    43 
       
    44     public EnvTestBase() {
       
    45         // set default test data
       
    46         setKey("host1");
       
    47     }
       
    48 
       
    49     /**
       
    50      * Setup test before real test run, it overrides the method of TestBase.
       
    51      */
       
    52     @Override public void setupTest() {
       
    53         super.setupTest();
       
    54         String fqdn = DNSTestUtils.buildFqdn(key, env(), true);
       
    55 
       
    56         String foreignLeaf = (String) env().get("FOREIGN_LEAF");
       
    57         String foreignFqdn = DNSTestUtils.buildFqdn(foreignLeaf, env(), false);
       
    58 
       
    59         fqdnUrl = DNSTestUtils.getRootUrl(env()) + "/" + fqdn;
       
    60         foreignFqdnUrl = DNSTestUtils.getRootUrl(env()) + "/" + foreignFqdn;
       
    61     }
       
    62 
       
    63     /**
       
    64      * Overload method of addToEnvAndVerifyOldValIsNull, use context() as
       
    65      * context.
       
    66      *
       
    67      * @param propName given property name
       
    68      * @param propVal  given property value
       
    69      * @throws NamingException if a naming exception is encountered
       
    70      */
       
    71     public void addToEnvAndVerifyOldValIsNull(String propName, Object propVal)
       
    72             throws NamingException {
       
    73         addToEnvAndVerifyOldValIsNull(context(), propName, propVal);
       
    74     }
       
    75 
       
    76     /**
       
    77      * Add given property name/value to the environment of given context and
       
    78      * verify the previous old property value is null which means the property
       
    79      * was not in the environment before.
       
    80      *
       
    81      * @param context  given context
       
    82      * @param propName given property name
       
    83      * @param propVal  given property value
       
    84      * @throws NamingException if a naming exception is encountered
       
    85      */
       
    86     public void addToEnvAndVerifyOldValIsNull(Context context, String propName,
       
    87             Object propVal) throws NamingException {
       
    88         Object oldValue = context.addToEnvironment(propName, propVal);
       
    89         DNSTestUtils.debug("Old Value for " + propName + " : " + oldValue);
       
    90         if (oldValue != null) {
       
    91             throw new RuntimeException(
       
    92                     "Failed: old value expected to be null for " + propName
       
    93                             + " but actual is : " + oldValue);
       
    94         }
       
    95     }
       
    96 
       
    97     /**
       
    98      * Verify the value of specified property in given environment matched with
       
    99      * given expected value. If property not exist and given expected value is
       
   100      * null, we think verify passed. RuntimeException will be thrown if verify
       
   101      * failed.
       
   102      *
       
   103      * @param env         given environment
       
   104      * @param propName    given property name to verify
       
   105      * @param expectedVal expected property value
       
   106      */
       
   107     public void verifyEnvProperty(Hashtable<?, ?> env, String propName,
       
   108             Object expectedVal) {
       
   109         boolean equals = true;
       
   110         Object actualVal = env.get(propName);
       
   111         if (actualVal != null && expectedVal != null) {
       
   112             if (!expectedVal.equals(actualVal)) {
       
   113                 equals = false;
       
   114             }
       
   115         } else {
       
   116             if (actualVal != null || expectedVal != null) {
       
   117                 equals = false;
       
   118             }
       
   119         }
       
   120 
       
   121         if (!equals) {
       
   122             throw new RuntimeException(
       
   123                     "Failed: value not match for " + propName + " expected: "
       
   124                             + expectedVal + " actual: " + actualVal);
       
   125         }
       
   126     }
       
   127 
       
   128     /**
       
   129      * Retrieve attributes by given name and attributes ids and verify
       
   130      * attributes contains the mandatory attributes and the right
       
   131      * objectclass attribute, will throw RuntimeException if verify failed.
       
   132      *
       
   133      * @param name    given name
       
   134      * @param attrIds given attribute ids
       
   135      * @throws NamingException if a naming exception is encountered
       
   136      */
       
   137     public void retrieveAndVerifyData(String name, String[] attrIds)
       
   138             throws NamingException {
       
   139         Attributes retAttrs = context().getAttributes(name, attrIds);
       
   140         DNSTestUtils.verifySchema(retAttrs, MANDATORY_ATTRIBUTES,
       
   141                 OPTIONAL_ATTRIBUTES);
       
   142     }
       
   143 
       
   144     public String getKey() {
       
   145         return key;
       
   146     }
       
   147 
       
   148     public void setKey(String key) {
       
   149         this.key = key;
       
   150     }
       
   151 
       
   152     public String getFqdnUrl() {
       
   153         return fqdnUrl;
       
   154     }
       
   155 
       
   156     public String getForeignFqdnUrl() {
       
   157         return foreignFqdnUrl;
       
   158     }
       
   159 }