/* * 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 org.junit.Assert; import org.junit.Test;
publicclass TestArrayELResolver {
/** * Tests that a null context results in an NPE as per EL Javadoc.
*/
@Test(expected = NullPointerException.class) publicvoid testGetType01() {
ArrayELResolver resolver = new ArrayELResolver();
resolver.getType(null, new Object(), new Object());
}
/** * Tests that a valid property is not resolved if base is not an array.
*/
@Test publicvoid testGetType02() {
doNegativeTest(new Object(), new Object(), MethodUnderTest.GET_TYPE, true);
}
/** * Tests that a valid property is resolved.
*/
@Test publicvoid testGetType03() {
ArrayELResolver resolver = new ArrayELResolver();
ELContext context = new StandardELContext(ELManager.getExpressionFactory());
String[] base = new String[] { "element" }; Class<?> result = resolver.getType(context, base, Integer.valueOf(0));
/** * Tests that the key is out of bounds and exception will be thrown.
*/
@Test(expected = PropertyNotFoundException.class) publicvoid testGetType04() {
ArrayELResolver resolver = new ArrayELResolver();
ELContext context = new StandardELContext(ELManager.getExpressionFactory());
String[] base = new String[] { "element" };
resolver.getType(context, base, Integer.valueOf(1));
}
/** * Tests that a result is returned even when a coercion cannot be performed.
*/
@Test publicvoid testGetType05() {
ArrayELResolver resolver = new ArrayELResolver();
ELContext context = new StandardELContext(ELManager.getExpressionFactory());
String[] base = new String[] { "element" }; Class<?> result = resolver.getType(context, base, "index");
/** * Tests that a null result is returned when the base is null.
*/
@Test publicvoid testGetType06() {
ArrayELResolver resolver = new ArrayELResolver();
ELContext context = new StandardELContext(ELManager.getExpressionFactory());
Class<?> result = resolver.getType(context, null, "index");
/** * Tests that a null context results in an NPE as per EL Javadoc.
*/
@Test(expected = NullPointerException.class) publicvoid testGetValue01() {
ArrayELResolver resolver = new ArrayELResolver();
resolver.getValue(null, new Object(), new Object());
}
/** * Tests that a valid property is not resolved if base is not an array.
*/
@Test publicvoid testGetValue02() {
doNegativeTest(new Object(), new Object(), MethodUnderTest.GET_VALUE, true);
}
/** * Tests that a valid property is resolved.
*/
@Test publicvoid testGetValue03() {
ArrayELResolver resolver = new ArrayELResolver();
ELContext context = new StandardELContext(ELManager.getExpressionFactory());
String[] base = new String[] { "element" };
Object result = resolver.getValue(context, base, Integer.valueOf(0));
@Test(expected = IllegalArgumentException.class) publicvoid testGetValueCoercion04() {
ArrayELResolver resolver = new ArrayELResolver();
ELContext context = new StandardELContext(ELManager.getExpressionFactory());
String[] base = new String[] { "element" };
resolver.getValue(context, base, new Object());
}
@Test(expected = IllegalArgumentException.class) publicvoid testGetValueCoercion05() {
ArrayELResolver resolver = new ArrayELResolver();
ELContext context = new StandardELContext(ELManager.getExpressionFactory());
String[] base = new String[] { "element" };
resolver.getValue(context, base, null);
}
/** * Tests a coercion cannot be performed as the key is not integer.
*/
@Test(expected = IllegalArgumentException.class) publicvoid testGetValueCoercion06() {
ArrayELResolver resolver = new ArrayELResolver();
ELContext context = new StandardELContext(ELManager.getExpressionFactory());
String[] base = new String[] { "element" };
resolver.getValue(context, base, "key");
}
/** * Tests that if the key is out of bounds null will be returned.
*/
@Test publicvoid testGetValue05() {
ArrayELResolver resolver = new ArrayELResolver();
ELContext context = new StandardELContext(ELManager.getExpressionFactory());
String[] base = new String[] { "element" };
Object result = resolver.getValue(context, base, Integer.valueOf(1));
/** * Tests that a null context results in an NPE as per EL Javadoc.
*/
@Test(expected = NullPointerException.class) publicvoid testSetValue01() {
ArrayELResolver resolver = new ArrayELResolver();
resolver.setValue(null, new Object(), new Object(), new Object());
}
/** * Tests that a valid property is not set if base is not an array.
*/
@Test publicvoid testSetValue02() {
doNegativeTest(new Object(), new Object(), MethodUnderTest.SET_VALUE, false);
}
/** * Tests that an exception is thrown when readOnly is true.
*/
@Test(expected = PropertyNotWritableException.class) publicvoid testSetValue03() {
ArrayELResolver resolver = new ArrayELResolver(true);
ELContext context = new StandardELContext(ELManager.getExpressionFactory());
resolver.setValue(context, new String[] {}, new Object(), new Object());
}
/** * Tests that a valid property is set.
*/
@Test publicvoid testSetValue04() {
ArrayELResolver resolver = new ArrayELResolver();
ELContext context = new StandardELContext(ELManager.getExpressionFactory());
String[] base = new String[] { "element" };
resolver.setValue(context, base, Integer.valueOf(0), "new-element");
/** * Tests a coercion cannot be performed as the key is not integer.
*/
@Test(expected = IllegalArgumentException.class) publicvoid testSetValue05() {
ArrayELResolver resolver = new ArrayELResolver();
ELContext context = new StandardELContext(ELManager.getExpressionFactory());
String[] base = new String[] { "element" };
resolver.setValue(context, base, "key", "new-element");
}
/** * Tests that the key is out of bounds and exception will be thrown.
*/
@Test(expected = PropertyNotFoundException.class) publicvoid testSetValue06() {
ArrayELResolver resolver = new ArrayELResolver();
ELContext context = new StandardELContext(ELManager.getExpressionFactory());
String[] base = new String[] { "element" };
resolver.setValue(context, base, Integer.valueOf(1), "new-element");
}
/** * Tests that an exception will be thrown if the value is not from the corresponding type.
*/
@Test(expected = ClassCastException.class) publicvoid testSetValue07() {
ArrayELResolver resolver = new ArrayELResolver();
ELContext context = new StandardELContext(ELManager.getExpressionFactory());
String[] base = new String[] { "element" };
resolver.setValue(context, base, Integer.valueOf(0), Integer.valueOf(1));
}
/** * Tests setting arrays of primitives. https://bz.apache.org/bugzilla/show_bug.cgi?id=55691
*/
@Test publicvoid testSetValue08() {
ArrayELResolver resolver = new ArrayELResolver();
ELContext context = new StandardELContext(ELManager.getExpressionFactory());
/* * Null base should be a NO-OP rather than an exception
*/
@Test publicvoid testSetValue09() {
ArrayELResolver resolver = new ArrayELResolver();
ELContext context = new StandardELContext(ELManager.getExpressionFactory());
/** * Tests that a null context results in an NPE as per EL Javadoc.
*/
@Test(expected = NullPointerException.class) publicvoid testIsReadOnly01() {
ArrayELResolver resolver = new ArrayELResolver();
resolver.isReadOnly(null, new Object(), new Object());
}
/** * Tests that the propertyResolved is false if base is not an array.
*/
@Test publicvoid testIsReadOnly02() {
ArrayELResolver resolver = new ArrayELResolver();
ELContext context = new StandardELContext(ELManager.getExpressionFactory());
boolean result = resolver.isReadOnly(context, new Object(), new Object());
/** * Tests that if the ArrayELResolver is constructed with readOnly the method will return always true, otherwise * false.
*/
@Test publicvoid testIsReadOnly03() {
ArrayELResolver resolver = new ArrayELResolver();
ELContext context = new StandardELContext(ELManager.getExpressionFactory());
String[] base = new String[] { "element" }; boolean result = resolver.isReadOnly(context, base, Integer.valueOf(0));
/** * Tests that the key is out of bounds and exception will be thrown.
*/
@Test(expected = PropertyNotFoundException.class) publicvoid testIsReadOnly04() {
doTestIsReadOutOfBounds(1);
}
privatevoid doTestIsReadOutOfBounds(int index) {
ArrayELResolver resolver = new ArrayELResolver();
ELContext context = new StandardELContext(ELManager.getExpressionFactory());
String[] base = new String[] { "element" };
resolver.isReadOnly(context, base, Integer.valueOf(index)); Assert.assertTrue(context.isPropertyResolved());
}
/** * Tests that a result is returned even when a coercion cannot be performed.
*/
@Test publicvoid testIsReadOnly06() {
ArrayELResolver resolver = new ArrayELResolver();
ELContext context = new StandardELContext(ELManager.getExpressionFactory());
String[] base = new String[] { "element" }; boolean result = resolver.isReadOnly(context, base, "key");
privatevoid doNegativeTest(Object base, Object trigger, MethodUnderTest method, booleancheckResult) {
ArrayELResolver resolver = new ArrayELResolver();
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 ist noch experimentell.