NamingContext implements Context, Serializable { @Override public void destroySubcontext(Name contextToDestroy) throws NamingException { Name name = stripEmptyPrefix(contextToDestroy); if (name.isEmpty()) throw new NamingException("Invalid name: " + name.toString()); NamingEntry namingEntry = bindings.get(name.get(0)); if (namingEntry == null) throw new NameNotFoundException("Name not found: " + name.get(0).toString()); if (name.size() > 1) { if (namingEntry.getType() == NamingEntryType.CONTEXT) ((Context) namingEntry.getObject()).destroySubcontext(name.getSuffix(1)); else throw new NamingException("Expected a context object: " + name.toString()); } else { if (namingEntry.getType() == NamingEntryType.CONTEXT) { ((Context) namingEntry.getObject()).close(); bindings.remove(name.get(0)); } else throw new NotContextException("Expected a context object"); } } NamingContext();  NamingContext(Hashtable<String, Object> environment);  NamingContext(Hashtable<String, Object> environment, Map<String, Object> objects);  NamingContext(Hashtable<String, Object> environment, String nameInNamespace); @Override void bind(Name name, Object object); void bind(Name bindName, Object object, boolean rebind); @Override Name composeName(Name name, Name prefix); @Override String composeName(String name, String prefix); @Override Context createSubcontext(Name name); @Override void destroySubcontext(Name contextToDestroy); @Override String getNameInNamespace(); @Override NameParser getNameParser(Name name); @Override NameParser getNameParser(String name); @Override NamingEnumeration list(Name name); @Override NamingEnumeration list(String name); @Override NamingEnumeration<Binding> listBindings(Name name); @Override NamingEnumeration<Binding> listBindings(String name); @Override Object lookup(Name lookupName); @Override Object lookupLink(Name name); @Override void rebind(Name name, Object object); @Override void rename(Name oldName, Name newName); @Override void unbind(Name toUnbind); @Override Object lookup(String name); @Override Object lookupLink(String name); @Override void unbind(String name); @Override void rebind(String name, Object object); @Override Object removeFromEnvironment(String propertyName); @Override Object addToEnvironment(String propertyName, Object propertyVal); @Override Hashtable<?, ?> getEnvironment(); @Override void bind(String name, Object object); @Override void close(); @Override Context createSubcontext(String name); @Override void destroySubcontext(String name); @Override void rename(String oldName, String newName);  }
@Test public void testDestroySubcontext() throws Exception { try (CloseableNamingContext context = closeableContext()) { context.bind("goodbye", new Object()); context.bind("a/b/c", new Object()); context.createSubcontext(createName("hello")); assertNotNull(context.lookup("hello")); context.destroySubcontext(createName("hello")); try { context.destroySubcontext("hello"); fail(); } catch (NameNotFoundException exoected) { } try { context.destroySubcontext("a/b/c"); fail(); } catch (NamingException exoected) { } context.destroySubcontext("a/b"); try { context.destroySubcontext("goodbye"); fail(); } catch (NotContextException expected) { } try { context.destroySubcontext(new CompositeName()); fail(); } catch (NamingException expected) { } } }