Author: violetagg Date: Mon Jul 8 19:10:52 2013 New Revision: 1500887 URL: http://svn.apache.org/r1500887 Log: Unit tests for ELContext
Modified: tomcat/trunk/java/javax/el/ELContext.java tomcat/trunk/test/javax/el/TestELContext.java tomcat/trunk/test/javax/el/TesterEvaluationListener.java Modified: tomcat/trunk/java/javax/el/ELContext.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/javax/el/ELContext.java?rev=1500887&r1=1500886&r2=1500887&view=diff ============================================================================== --- tomcat/trunk/java/javax/el/ELContext.java (original) +++ tomcat/trunk/java/javax/el/ELContext.java Mon Jul 8 19:10:52 2013 @@ -61,7 +61,7 @@ public abstract class ELContext { // Can't use Class<?> because API needs to match specification /** * @throws NullPointerException - * If the supplied context is <code>null</code> + * If the supplied key or context is <code>null</code> */ public void putContext(@SuppressWarnings("rawtypes") Class key, Object contextObject) { @@ -79,7 +79,7 @@ public abstract class ELContext { // Can't use Class<?> because API needs to match specification /** * @throws NullPointerException - * If the key is <code>null</code> + * If the supplied key is <code>null</code> */ public Object getContext(@SuppressWarnings("rawtypes") Class key) { if (key == null) { Modified: tomcat/trunk/test/javax/el/TestELContext.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/javax/el/TestELContext.java?rev=1500887&r1=1500886&r2=1500887&view=diff ============================================================================== --- tomcat/trunk/test/javax/el/TestELContext.java (original) +++ tomcat/trunk/test/javax/el/TestELContext.java Mon Jul 8 19:10:52 2013 @@ -16,6 +16,11 @@ */ package javax.el; +import java.util.List; + +import javax.el.TesterEvaluationListener.Pair; + +import org.junit.Assert; import org.junit.Test; public class TestELContext { @@ -29,4 +34,140 @@ public class TestELContext { elContext.getContext(null); } + /** + * Tests that a null key results in an NPE as per EL Javadoc. + */ + @Test(expected = NullPointerException.class) + public void testPutContext01() { + ELContext elContext = new TesterELContext(); + elContext.putContext(null, new Object()); + } + + /** + * Tests that a null context results in an NPE as per EL Javadoc. + */ + @Test(expected = NullPointerException.class) + public void testPutContext02() { + ELContext elContext = new TesterELContext(); + elContext.putContext(Object.class, null); + } + + /** + * Tests that the context object will be added to the map with context + * objects. The key is used as unique identifier of the context object in + * the map. + */ + @Test + public void testPutContext03() { + ELContext elContext = new TesterELContext(); + Assert.assertNull(elContext.getContext(String.class)); + elContext.putContext(String.class, "test"); + Assert.assertEquals("test", elContext.getContext(String.class)); + elContext.putContext(String.class, "test1"); + Assert.assertEquals("test1", elContext.getContext(String.class)); + } + + /** + * Tests that propertyResolved will be set to true and the corresponding + * listeners will be notified. + */ + @Test + public void testSetPropertyResolved() { + ELContext elContext = new TesterELContext(); + + TesterEvaluationListener listener = new TesterEvaluationListener(); + elContext.addEvaluationListener(listener); + + TesterBean bean = new TesterBean("test"); + + elContext.setPropertyResolved(bean, "name"); + + Assert.assertTrue(elContext.isPropertyResolved()); + + List<Pair> events = listener.getResolvedProperties(); + Assert.assertEquals(1, events.size()); + Pair p = events.get(0); + Assert.assertEquals(bean, p.getBase()); + Assert.assertEquals("name", p.getProperty()); + } + + /** + * Tests that the corresponding listeners will be notified. + */ + @Test + public void testNotifyBeforeEvaluation() { + ELContext elContext = new TesterELContext(); + + TesterEvaluationListener listener = new TesterEvaluationListener(); + elContext.addEvaluationListener(listener); + + elContext.notifyBeforeEvaluation("before"); + + List<String> events = listener.getBeforeEvaluationExpressions(); + Assert.assertEquals(1, events.size()); + Assert.assertEquals("before", events.get(0)); + } + + /** + * Tests that the corresponding listeners will be notified. + */ + @Test + public void testNotifyAfterEvaluation() { + ELContext elContext = new TesterELContext(); + + TesterEvaluationListener listener = new TesterEvaluationListener(); + elContext.addEvaluationListener(listener); + + elContext.notifyAfterEvaluation("after"); + + List<String> events = listener.getAfterEvaluationExpressions(); + Assert.assertEquals(1, events.size()); + Assert.assertEquals("after", events.get(0)); + } + + /** + * Tests not compatible object and type. + */ + @Test(expected = ELException.class) + public void testConvertToType01() { + ELContext elContext = new TesterELContext(); + elContext.convertToType("test", Integer.class); + } + + /** + * Tests that if there is no ELResolver a standard coercions will be + * invoked. + */ + @Test + public void testConvertToType02() { + ELContext elContext = new TesterELContext(); + boolean originalPropertyResolved = elContext.isPropertyResolved(); + + Object result = elContext.convertToType("test", String.class); + Assert.assertEquals("test", result); + + Assert.assertTrue(originalPropertyResolved == elContext + .isPropertyResolved()); + } + + /** + * Tests that if there is ELResolver it will handle the conversion. If this + * resolver cannot return a result the standard coercions will be invoked. + */ + @Test + public void testConvertToType03() { + ELContext elContext = new TesterELContext(new TesterELResolverOne()); + + boolean originalPropertyResolved = elContext.isPropertyResolved(); + Object result = elContext.convertToType("1", String.class); + Assert.assertEquals("ONE", result); + Assert.assertTrue(originalPropertyResolved == elContext + .isPropertyResolved()); + + originalPropertyResolved = elContext.isPropertyResolved(); + result = elContext.convertToType("test", String.class); + Assert.assertEquals("test", result); + Assert.assertTrue(originalPropertyResolved == elContext + .isPropertyResolved()); + } } Modified: tomcat/trunk/test/javax/el/TesterEvaluationListener.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/javax/el/TesterEvaluationListener.java?rev=1500887&r1=1500886&r2=1500887&view=diff ============================================================================== --- tomcat/trunk/test/javax/el/TesterEvaluationListener.java (original) +++ tomcat/trunk/test/javax/el/TesterEvaluationListener.java Mon Jul 8 19:10:52 2013 @@ -22,6 +22,8 @@ import java.util.List; public class TesterEvaluationListener extends EvaluationListener { private final List<Pair> resolvedProperties = new ArrayList<>(); + private final List<String> beforeEvaluationExpressions = new ArrayList<>(); + private final List<String> afterEvaluationExpressions = new ArrayList<>(); @Override @@ -31,10 +33,33 @@ public class TesterEvaluationListener ex } + @Override + public void beforeEvaluation(ELContext context, String expression) { + beforeEvaluationExpressions.add(expression); + } + + + @Override + public void afterEvaluation(ELContext context, String expression) { + afterEvaluationExpressions.add(expression); + } + + public List<Pair> getResolvedProperties() { return resolvedProperties; } + + public List<String> getBeforeEvaluationExpressions() { + return beforeEvaluationExpressions; + } + + + public List<String> getAfterEvaluationExpressions() { + return afterEvaluationExpressions; + } + + public static class Pair { private final Object base; private final Object property; --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org