ttomcat-1778514358873.zip-extract/apache-tomcat-11.0.18-src/test/jakarta/el/TestResourceBundleELResolver.java

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

      
    
Rootfs path

      
    
Size
9431 (9.2 KB)
MD5
f0177f4a173ad50cee1e663942ee16b9
SHA1
ac648f4b63287b95659f63b2bfc8a865f552c5e1
SHA256
2ac7a50e100bddf5553c9fb36c9dd153195739860fb6245a0cc509684c29c22b
SHA512

      
    
SHA1_git
157614593644a12359880e76e1bf26117335c5e2
Is binary

      
    
Is text
True
Is archive

      
    
Is media

      
    
Is legal

      
    
Is manifest

      
    
Is readme

      
    
Is top level

      
    
Is key file

      
    
TestResourceBundleELResolver.java | 9.2 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 jakarta.el; import java.util.Enumeration; import java.util.ListResourceBundle; import java.util.ResourceBundle; import org.junit.Assert; import org.junit.Test; import org.apache.jasper.el.ELContextImpl; public class TestResourceBundleELResolver { @Test public void bug53001() { ExpressionFactory factory = ExpressionFactory.newInstance(); ELContext context = new ELContextImpl(); ResourceBundle rb = new TesterResourceBundle(); ValueExpression var = factory.createValueExpression(rb, ResourceBundle.class); context.getVariableMapper().setVariable("rb", var); ValueExpression ve = factory.createValueExpression(context, "${rb.keys}", String.class); MethodExpression me = factory.createMethodExpression(context, "${rb.getKeys()}", Enumeration.class, null); // Ensure we are specification compliant String result1 = (String) ve.getValue(context); Assert.assertEquals("???keys???", result1); // Check that the method expression does return the keys Object result2 = me.invoke(context, null); @SuppressWarnings("unchecked") Enumeration<String> e = (Enumeration<String>) result2; Assert.assertTrue(e.hasMoreElements()); String element = e.nextElement(); if ("key1".equals(element)) { Assert.assertEquals("key1", element); Assert.assertTrue(e.hasMoreElements()); Assert.assertEquals("key2", e.nextElement()); Assert.assertFalse(e.hasMoreElements()); } else { Assert.assertEquals("key2", element); Assert.assertTrue(e.hasMoreElements()); Assert.assertEquals("key1", e.nextElement()); Assert.assertFalse(e.hasMoreElements()); } } /** * Tests that a null context results in an NPE as per EL Javadoc. */ @Test(expected = NullPointerException.class) public void testGetValue01() { ResourceBundleELResolver resolver = new ResourceBundleELResolver(); resolver.getValue(null, new Object(), new Object()); } /** * Tests that a valid property is not resolved if base is not ResourceBundle. */ @Test public void testGetValue02() { doNegativeTest(new Object(), new Object(), MethodUnderTest.GET_VALUE, true); } /** * Tests that a valid property is resolved. */ @Test public void testGetValue03() { ResourceBundleELResolver resolver = new ResourceBundleELResolver(); ELContext context = new StandardELContext(ELManager.getExpressionFactory()); ResourceBundle resourceBundle = new TesterResourceBundle(); Object result = resolver.getValue(context, resourceBundle, "key1"); Assert.assertEquals("value1", result); Assert.assertTrue(context.isPropertyResolved()); result = resolver.getValue(context, resourceBundle, "unknown-key"); Assert.assertEquals("???unknown-key???", result); Assert.assertTrue(context.isPropertyResolved()); result = resolver.getValue(context, resourceBundle, null); Assert.assertNull(result); Assert.assertTrue(context.isPropertyResolved()); } /** * Tests that a null context results in an NPE as per EL Javadoc. */ @Test(expected = NullPointerException.class) public void testGetType01() { ResourceBundleELResolver resolver = new ResourceBundleELResolver(); resolver.getType(null, new Object(), new Object()); } /** * Tests that a valid property is not resolved if base is not ResourceBundle. */ @Test public void testGetType02() { doNegativeTest(new Object(), new Object(), MethodUnderTest.GET_TYPE, true); } /** * Tests that null will be returned when base is ResourceBundle. Checks that the propertyResolved is true. */ @Test public void testGetType03() { ResourceBundleELResolver resolver = new ResourceBundleELResolver(); ELContext context = new StandardELContext(ELManager.getExpressionFactory()); ResourceBundle resourceBundle = new TesterResourceBundle(); Class<?> result = resolver.getType(context, resourceBundle, "key1"); Assert.assertNull(result); Assert.assertTrue(context.isPropertyResolved()); } /** * Tests that a null context results in an NPE as per EL Javadoc. */ @Test(expected = NullPointerException.class) public void testSetValue01() { ResourceBundleELResolver resolver = new ResourceBundleELResolver(); resolver.setValue(null, new Object(), new Object(), new Object()); } /** * Tests that a valid property is not set if base is not ResourceBundle. */ @Test public void testSetValue02() { doNegativeTest(new Object(), new Object(), MethodUnderTest.SET_VALUE, false); } /** * Tests that an exception is thrown because the resolver is read only. */ @Test(expected = PropertyNotWritableException.class) public void testSetValue03() { ResourceBundleELResolver resolver = new ResourceBundleELResolver(); ELContext context = new StandardELContext(ELManager.getExpressionFactory()); ResourceBundle resourceBundle = new TesterResourceBundle(); resolver.setValue(context, resourceBundle, new Object(), new Object()); } /** * Tests that a null context results in an NPE as per EL Javadoc. */ @Test(expected = NullPointerException.class) public void testIsReadOnly01() { ResourceBundleELResolver resolver = new ResourceBundleELResolver(); resolver.isReadOnly(null, new Object(), new Object()); } /** * Tests that the propertyResolved is false and readOnly is false if base is not ResourceBundle. */ @Test public void testIsReadOnly02() { ResourceBundleELResolver resolver = new ResourceBundleELResolver(); ELContext context = new StandardELContext(ELManager.getExpressionFactory()); boolean result = resolver.isReadOnly(context, new Object(), new Object()); Assert.assertFalse(result); Assert.assertFalse(context.isPropertyResolved()); } /** * Tests that the readOnly is true always when the base is ResourceBundle. */ @Test public void testIsReadOnly03() { ResourceBundleELResolver resolver = new ResourceBundleELResolver(); ELContext context = new StandardELContext(ELManager.getExpressionFactory()); ResourceBundle resourceBundle = new TesterResourceBundle(); boolean result = resolver.isReadOnly(context, resourceBundle, new Object()); Assert.assertTrue(result); Assert.assertTrue(context.isPropertyResolved()); } private static class TesterResourceBundle extends ListResourceBundle { TesterResourceBundle() { this(new Object[][] { { "key1", "value1" }, { "key2", "value2" } }); } TesterResourceBundle(Object[][] contents) { this.contents = contents; } @Override protected Object[][] getContents() { return contents; } private Object[][] contents; } private void doNegativeTest(Object base, Object trigger, MethodUnderTest method, boolean checkResult) { ResourceBundleELResolver resolver = new ResourceBundleELResolver(); ELContext context = new StandardELContext(ELManager.getExpressionFactory()); Object result = null; switch (method) { case GET_VALUE: { result = resolver.getValue(context, base, trigger); break; } case SET_VALUE: { resolver.setValue(context, base, trigger, new Object()); break; } case GET_TYPE: { result = resolver.getType(context, base, trigger); break; } default: { // Should never happen Assert.fail("Missing case for method"); } } if (checkResult) { Assert.assertNull(result); } Assert.assertFalse(context.isPropertyResolved()); } private enum MethodUnderTest { GET_VALUE, SET_VALUE, GET_TYPE } }
Detected license expression
apache-2.0
Detected license expression (SPDX)
Apache-2.0
Percentage of license text
14.34
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