1 /*
   2  * Copyright (c) 1998, 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.Attribute;
  27 import javax.naming.directory.Attributes;
  28 import javax.naming.directory.DirContext;
  29 import javax.naming.directory.InitialDirContext;
  30 
  31 /*
  32  * @test
  33  * @bug 8210339
  34  * @summary Look up of a Interior using a composite name with DNS name as
  35  *          first component
  36  * @library ../lib/ ../FactoryTests/
  37  * @modules java.naming/com.sun.jndi.toolkit.dir
  38  *          java.base/sun.security.util
  39  * @build FedSubordinateNs FedObjectFactory
  40  * @run main/othervm LookupSubInterior
  41  */
  42 
  43 public class LookupSubInterior extends LookupFactoryBase {
  44 
  45     // pre defined attribute value for '/a/b'
  46     public static final String ATTRIBUTE_VALUE = "b";
  47 
  48     public static void main(String[] args) throws Exception {
  49         new LookupSubInterior().run(args);
  50     }
  51 
  52     /*
  53      * Look up of a Interior using a composite name with DNS name as
  54      * first component
  55      */
  56     @Override
  57     public void runTest() throws Exception {
  58         // initial context with object factory
  59         env().put(Context.OBJECT_FACTORIES, "FedObjectFactory");
  60         setContext(new InitialDirContext(env()));
  61 
  62         Object obj = context().lookup(getKey() + "/a/b");
  63 
  64         verifyLookupObject(obj, DirContext.class);
  65 
  66         Attributes attrs = ((DirContext) obj).getAttributes("");
  67         Attribute attr = attrs.get("name");
  68 
  69         DNSTestUtils.debug(getKey() + "/a/b is: " + attrs);
  70         verifyAttribute(attr);
  71     }
  72 
  73     private void verifyAttribute(Attribute attr) throws NamingException {
  74         if (attr == null || !ATTRIBUTE_VALUE.equals(attr.get())) {
  75             throw new RuntimeException(
  76                     "Expecting attribute value: " + ATTRIBUTE_VALUE
  77                             + ", but actual: " + (attr != null ?
  78                             attr.get() :
  79                             attr));
  80         }
  81     }
  82 }