Quelle TestResourceBundleELResolver.java
Sprache: JAVA
/* * 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;
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;
/** * Tests that a null context results in an NPE as per EL Javadoc.
*/
@Test(expected = NullPointerException.class) publicvoid 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 publicvoid testGetValue02() {
doNegativeTest(new Object(), new Object(), MethodUnderTest.GET_VALUE, true);
}
/** * Tests that a valid property is resolved.
*/
@Test publicvoid testGetValue03() {
ResourceBundleELResolver resolver = new ResourceBundleELResolver();
ELContext context = new StandardELContext(ELManager.getExpressionFactory());
ResourceBundle resourceBundle = new TesterResourceBundle();
Object result = resolver.getValue(context, resourceBundle, "key1");
/** * Tests that a null context results in an NPE as per EL Javadoc.
*/
@Test(expected = NullPointerException.class) publicvoid 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 publicvoid 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 publicvoid testGetType03() {
ResourceBundleELResolver resolver = new ResourceBundleELResolver();
ELContext context = new StandardELContext(ELManager.getExpressionFactory());
ResourceBundle resourceBundle = new TesterResourceBundle(); Class<?> result = resolver.getType(context, resourceBundle, "key1");
/** * Tests that a null context results in an NPE as per EL Javadoc.
*/
@Test(expected = NullPointerException.class) publicvoid 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 publicvoid 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) publicvoid 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) publicvoid 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 publicvoid testIsReadOnly02() {
ResourceBundleELResolver resolver = new ResourceBundleELResolver();
ELContext context = new StandardELContext(ELManager.getExpressionFactory());
boolean result = resolver.isReadOnly(context, new Object(), new Object());
/** * Tests that the readOnly is true always when the base is ResourceBundle.
*/
@Test publicvoid testIsReadOnly03() {
ResourceBundleELResolver resolver = new ResourceBundleELResolver();
ELContext context = new StandardELContext(ELManager.getExpressionFactory());
ResourceBundle resourceBundle = new TesterResourceBundle(); boolean result = resolver.isReadOnly(context, resourceBundle, new Object());
/** * Tests that a valid FeatureDescriptors are not returned if base is not ResourceBundle.
*/
@Deprecated(forRemoval = true, since = "Tomcat 10.1.0")
@Test publicvoid testGetFeatureDescriptors01() {
ResourceBundleELResolver resolver = new ResourceBundleELResolver();
ELContext context = new StandardELContext(ELManager.getExpressionFactory());
Iterator<FeatureDescriptor> result = resolver.getFeatureDescriptors(context, new Object());
Assert.assertNull(result);
}
/** * Tests that a valid FeatureDescriptors are returned.
*/
@Deprecated(forRemoval = true, since = "Tomcat 10.1.0")
@Test publicvoid testGetFeatureDescriptors02() {
ResourceBundleELResolver resolver = new ResourceBundleELResolver();
ELContext context = new StandardELContext(ELManager.getExpressionFactory());
ResourceBundle resourceBundle = new TesterResourceBundle(new Object[][] { { "key", "value" } });
Iterator<FeatureDescriptor> result = resolver.getFeatureDescriptors(context, resourceBundle);
privatevoid doNegativeTest(Object base, Object trigger, MethodUnderTest method, booleancheckResult) {
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());
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.