Path: blob/master/test/jdk/com/sun/jndi/dns/EnvTests/EnvTestBase.java
41155 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import javax.naming.Context;24import javax.naming.NamingException;25import javax.naming.directory.Attributes;26import java.util.Hashtable;2728/**29* Abstract test base for most of Env Tests, this class extends DNSTestBase.30*31* @see DNSTestBase32* @see TestBase33*/34abstract class EnvTestBase extends DNSTestBase {35private static final String[] MANDATORY_ATTRIBUTES = { "A", "MX", "HINFO",36"TXT", "29" };37private static final String[] OPTIONAL_ATTRIBUTES = {};3839private String key;40private String fqdnUrl;41private String foreignFqdnUrl;4243public EnvTestBase() {44// set default test data45setKey("host1");46}4748/**49* Setup test before real test run, it overrides the method of TestBase.50*/51@Override public void setupTest() {52super.setupTest();53String fqdn = DNSTestUtils.buildFqdn(key, env(), true);5455String foreignLeaf = (String) env().get("FOREIGN_LEAF");56String foreignFqdn = DNSTestUtils.buildFqdn(foreignLeaf, env(), false);5758fqdnUrl = DNSTestUtils.getRootUrl(env()) + "/" + fqdn;59foreignFqdnUrl = DNSTestUtils.getRootUrl(env()) + "/" + foreignFqdn;60}6162/**63* Overload method of addToEnvAndVerifyOldValIsNull, use context() as64* context.65*66* @param propName given property name67* @param propVal given property value68* @throws NamingException if a naming exception is encountered69*/70public void addToEnvAndVerifyOldValIsNull(String propName, Object propVal)71throws NamingException {72addToEnvAndVerifyOldValIsNull(context(), propName, propVal);73}7475/**76* Add given property name/value to the environment of given context and77* verify the previous old property value is null which means the property78* was not in the environment before.79*80* @param context given context81* @param propName given property name82* @param propVal given property value83* @throws NamingException if a naming exception is encountered84*/85public void addToEnvAndVerifyOldValIsNull(Context context, String propName,86Object propVal) throws NamingException {87Object oldValue = context.addToEnvironment(propName, propVal);88DNSTestUtils.debug("Old Value for " + propName + " : " + oldValue);89if (oldValue != null) {90throw new RuntimeException(91"Failed: old value expected to be null for " + propName92+ " but actual is : " + oldValue);93}94}9596/**97* Verify the value of specified property in given environment matched with98* given expected value. If property not exist and given expected value is99* null, we think verify passed. RuntimeException will be thrown if verify100* failed.101*102* @param env given environment103* @param propName given property name to verify104* @param expectedVal expected property value105*/106public void verifyEnvProperty(Hashtable<?, ?> env, String propName,107Object expectedVal) {108boolean equals = true;109Object actualVal = env.get(propName);110if (actualVal != null && expectedVal != null) {111if (!expectedVal.equals(actualVal)) {112equals = false;113}114} else {115if (actualVal != null || expectedVal != null) {116equals = false;117}118}119120if (!equals) {121throw new RuntimeException(122"Failed: value not match for " + propName + " expected: "123+ expectedVal + " actual: " + actualVal);124}125}126127/**128* Retrieve attributes by given name and attributes ids and verify129* attributes contains the mandatory attributes and the right130* objectclass attribute, will throw RuntimeException if verify failed.131*132* @param name given name133* @param attrIds given attribute ids134* @throws NamingException if a naming exception is encountered135*/136public void retrieveAndVerifyData(String name, String[] attrIds)137throws NamingException {138Attributes retAttrs = context().getAttributes(name, attrIds);139DNSTestUtils.verifySchema(retAttrs, MANDATORY_ATTRIBUTES,140OPTIONAL_ATTRIBUTES);141}142143public String getKey() {144return key;145}146147public void setKey(String key) {148this.key = key;149}150151public String getFqdnUrl() {152return fqdnUrl;153}154155public String getForeignFqdnUrl() {156return foreignFqdnUrl;157}158}159160161