ttomcat-1778514358873.zip-extract/apache-tomcat-11.0.18-src/test/org/apache/naming/TestNamingContext.java

Path
ttomcat-1778514358873.zip-extract/apache-tomcat-11.0.18-src/test/org/apache/naming/TestNamingContext.java
Status
scanned
Type
file
Name
TestNamingContext.java
Extension
.java
Programming language
Java
Mime type
text/plain
File type
ASCII text, with CRLF line terminators
Tag

      
    
Rootfs path

      
    
Size
6033 (5.9 KB)
MD5
de98aae4ff89d7dd69b230de0aae1f15
SHA1
733c29def2c3fc689226d27dda5bdf84c76cd71a
SHA256
4085ea70bb30625026a174df1376d383bb04652572dd4f1c65cc046d17aa8a69
SHA512

      
    
SHA1_git
de91baa5f56d5e4676967b16ea0fcc9cca7360ca
Is binary

      
    
Is text
True
Is archive

      
    
Is media

      
    
Is legal

      
    
Is manifest

      
    
Is readme

      
    
Is top level

      
    
Is key file

      
    
TestNamingContext.java | 5.9 KB |

/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.naming; import javax.naming.Context; import javax.naming.NamingException; import org.junit.Assert; import org.junit.Test; import org.apache.catalina.LifecycleListener; import org.apache.catalina.core.NamingContextListener; import org.apache.catalina.startup.Tomcat; import org.apache.catalina.startup.TomcatBaseTest; import org.apache.naming.factory.ResourceLinkFactory; import org.apache.tomcat.util.descriptor.web.ContextEnvironment; import org.apache.tomcat.util.descriptor.web.ContextResourceLink; public class TestNamingContext extends TomcatBaseTest { private static final String COMP_ENV = "comp/env"; private static final String MODULE_ENV = "module/env"; private static final String GLOBAL_NAME = "global"; private static final String LOCAL_NAME = "local"; private static final String DATA = "Cabbage"; @Test public void testGlobalNaming() throws Exception { Tomcat tomcat = getTomcatInstance(); tomcat.enableNaming(); org.apache.catalina.Context ctx = getProgrammaticRootContext(); tomcat.start(); Context webappInitial = ContextBindings.getContext(ctx); // Nothing added at the moment so should be null Object obj = doLookup(webappInitial, COMP_ENV + "/" + LOCAL_NAME); Assert.assertNull(obj); ContextEnvironment ce = new ContextEnvironment(); ce.setName(GLOBAL_NAME); ce.setValue(DATA); ce.setType(DATA.getClass().getName()); tomcat.getServer().getGlobalNamingResources().addEnvironment(ce); // No link so still should be null obj = doLookup(webappInitial, COMP_ENV + "/" + LOCAL_NAME); Assert.assertNull(obj); // Now add a resource link to the context ContextResourceLink crl = new ContextResourceLink(); crl.setGlobal(GLOBAL_NAME); crl.setName(LOCAL_NAME); crl.setType(DATA.getClass().getName()); ctx.getNamingResources().addResourceLink(crl); // Link exists so should be OK now obj = doLookup(webappInitial, COMP_ENV + "/" + LOCAL_NAME); Assert.assertEquals(DATA, obj); // Try shortcut ResourceLinkFactory factory = new ResourceLinkFactory(); ResourceLinkRef rlr = new ResourceLinkRef(DATA.getClass().getName(), GLOBAL_NAME, null, null); obj = factory.getObjectInstance(rlr, null, null, null); Assert.assertEquals(DATA, obj); // Remove the link ctx.getNamingResources().removeResourceLink(LOCAL_NAME); // No link so should be null obj = doLookup(webappInitial, COMP_ENV + "/" + LOCAL_NAME); Assert.assertNull(obj); // Shortcut should fail too obj = factory.getObjectInstance(rlr, null, null, null); Assert.assertNull(obj); } @Test public void testModuleEquivalentToComp() throws Exception { Tomcat tomcat = getTomcatInstance(); tomcat.enableNaming(); org.apache.catalina.Context ctx = getProgrammaticRootContext(); tomcat.start(); // Equivalent to: Context initContext = new InitialContext(); Context webappInitial = ContextBindings.getContext(ctx); // Make it writable (it is normally read-only) String namingContextName = null; LifecycleListener[] listeners = ctx.findLifecycleListeners(); for (LifecycleListener listener : listeners) { if (listener instanceof NamingContextListener namingListener) { namingContextName = namingListener.getName(); break; } } ContextAccessController.setWritable(namingContextName, ctx.getNamingToken()); // Nothing created so should be null Object obj = doLookup(webappInitial, COMP_ENV + "/" + LOCAL_NAME); Assert.assertNull(obj); obj = doLookup(webappInitial, MODULE_ENV + "/" + LOCAL_NAME); Assert.assertNull(obj); // Create in java:comp/env webappInitial.bind(COMP_ENV + "/" + LOCAL_NAME, DATA); // Check it was created in java:comp/env and java:module/env obj = doLookup(webappInitial, COMP_ENV + "/" + LOCAL_NAME); Assert.assertEquals(DATA, obj); obj = doLookup(webappInitial, MODULE_ENV + "/" + LOCAL_NAME); Assert.assertEquals(DATA, obj); // Remove it webappInitial.unbind(COMP_ENV + "/" + LOCAL_NAME); // Create in java:module/env webappInitial.bind(MODULE_ENV + "/" + LOCAL_NAME, DATA); // Check it was created in java:comp/env and java:module/env obj = doLookup(webappInitial, COMP_ENV + "/" + LOCAL_NAME); Assert.assertEquals(DATA, obj); obj = doLookup(webappInitial, MODULE_ENV + "/" + LOCAL_NAME); Assert.assertEquals(DATA, obj); } private Object doLookup(Context context, String name) { Object result = null; try { result = context.lookup(name); } catch (NamingException nnfe) { // Ignore } return result; } }
Detected license expression
apache-2.0
Detected license expression (SPDX)
Apache-2.0
Percentage of license text
18.92
Copyrights

      
    
Holders

      
    
Authors

      
    
License detections License expression License expression SPDX
apache_2_0-4bde3f57-78aa-4201-96bf-531cba09e7de apache-2.0 Apache-2.0
URL Start line End line
http://www.apache.org/licenses/LICENSE-2.0 9 9