Permalink
Browse files

Fix null pointer exception in unit test

  • Loading branch information...
1 parent e8427e5 commit 7200a35468a6ef87176ae4ba79095bd92b38af06 @pukkaone committed Mar 8, 2011
View
6 README.md
@@ -26,8 +26,8 @@ In the `web.xml` file, add a listener:
Use a custom tag to surround JSP code in which you do not want EL expression
values to be escaped:
- <%@ taglib prefix="wae" uri="http://pukkaone.github.com/jsp" %>
+ <%@ taglib prefix="enhance" uri="http://pukkaone.github.com/jsp" %>
- <wae:out escapeXml="false">
+ <enhance:out escapeXml="false">
I hope this expression returns safe HTML: ${user.name}
- </wae:out>
+ </enhance:out>
View
2 src/com/github/pukkaone/jsp/EscapeXmlELResolver.java
@@ -40,7 +40,7 @@
/** pageContext attribute name for flag to enable XML escaping */
static final String ESCAPE_XML_ATTRIBUTE =
- EscapeXmlELResolver.class.getPackage() + ".escapeXml";
+ EscapeXmlELResolver.class.getName() + ".escapeXml";
private ThreadLocal<Boolean> excludeMe = new ThreadLocal<Boolean>() {
@Override
View
62 test/com/github/pukkaone/jsp/ConstantELResolver.java
@@ -0,0 +1,62 @@
+package com.github.pukkaone.jsp;
+
+import java.beans.FeatureDescriptor;
+import java.util.Iterator;
+import javax.el.ELContext;
+import javax.el.ELException;
+import javax.el.ELResolver;
+import javax.el.PropertyNotFoundException;
+import javax.el.PropertyNotWritableException;
+
+/**
+ * ELResolver that always resolves a value regardless of the arguments passed to
+ * the getValue method.
+ */
+class ConstantELResolver extends ELResolver {
+
+ private String value;
+
+ ConstantELResolver(String value) {
+ this.value = value;
+ }
+
+ @Override
+ public Class<?> getCommonPropertyType(ELContext context, Object arg1) {
+ return null;
+ }
+
+ @Override
+ public Iterator<FeatureDescriptor> getFeatureDescriptors(
+ ELContext context, Object arg1)
+ {
+ return null;
+ }
+
+ @Override
+ public Class<?> getType(ELContext context, Object arg1, Object arg2)
+ throws NullPointerException, PropertyNotFoundException, ELException
+ {
+ return null;
+ }
+
+ @Override
+ public Object getValue(ELContext context, Object arg1, Object arg2)
+ throws NullPointerException, PropertyNotFoundException, ELException
+ {
+ context.setPropertyResolved(true);
+ return value;
+ }
+
+ @Override
+ public boolean isReadOnly(ELContext context, Object arg1, Object arg2)
+ throws NullPointerException, PropertyNotFoundException, ELException
+ {
+ return false;
+ }
+
+ @Override
+ public void setValue(ELContext context, Object arg1, Object arg2, Object arg3)
+ throws NullPointerException, PropertyNotFoundException, PropertyNotWritableException, ELException
+ {
+ }
+}
View
39 test/com/github/pukkaone/jsp/EscapeXMLELResolverTest.java
@@ -24,6 +24,7 @@
*/
package com.github.pukkaone.jsp;
+import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
@@ -32,18 +33,19 @@
import javax.el.ELResolver;
import javax.el.FunctionMapper;
import javax.el.VariableMapper;
+import javax.servlet.jsp.JspContext;
import org.junit.Before;
import org.junit.Test;
public class EscapeXMLELResolverTest {
private static final String BASE = "base";
private static final String PROPERTY = "property";
- private static final String VALUE = "value";
+ private static final String VALUE = "<h1>&'\"";
private CompositeELResolver compositeResolver;
- private ELContext context = new ELContext() {
+ private ELContext elContext = new ELContext() {
@Override
public VariableMapper getVariableMapper() {
return null;
@@ -60,39 +62,54 @@ public ELResolver getELResolver() {
}
};
+ private JspContext pageContext = new FakeJspContext();
+
@Before
public void beforeTest() {
compositeResolver = new CompositeELResolver();
compositeResolver.add(new EscapeXmlELResolver());
+ compositeResolver.add(new ConstantELResolver(VALUE));
+
+ elContext.putContext(JspContext.class, pageContext);
}
@Test
public void getCommonPropertyType_should_return_null() {
- assertNull(compositeResolver.getCommonPropertyType(context, BASE));
+ assertNull(compositeResolver.getCommonPropertyType(elContext, BASE));
}
@Test
public void getFeatureDescriptors_should_return_empty() {
- assertFalse(compositeResolver.getFeatureDescriptors(context, BASE).hasNext());
+ assertFalse(compositeResolver.getFeatureDescriptors(elContext, BASE).hasNext());
+ }
+
+ @Test
+ public void getType_should_return_null() {
+ assertNull(compositeResolver.getType(elContext, BASE, PROPERTY));
}
@Test
- public void getType_should_not_infinitely_recurse() {
- compositeResolver.getType(context, BASE, PROPERTY);
+ public void getValue_should_escape_value_when_enabled() {
+ String value = (String) compositeResolver.getValue(elContext, BASE, PROPERTY);
+ assertEquals("&lt;h1&gt;&amp;&#039;&#034;", value);
}
@Test
- public void getValue_should_not_infinitely_recurse() {
- compositeResolver.getValue(context, BASE, PROPERTY);
+ public void getValue_should_not_escape_value_when_disabled() {
+ JspContext pageContext = (JspContext) elContext.getContext(JspContext.class);
+ pageContext.setAttribute(EscapeXmlELResolver.ESCAPE_XML_ATTRIBUTE, Boolean.FALSE);
+
+ String value = (String) compositeResolver.getValue(elContext, BASE, PROPERTY);
+ assertEquals(VALUE, value);
}
@Test
- public void isReadOnly_should_not_infinitely_recurse() {
- compositeResolver.isReadOnly(context, BASE, PROPERTY);
+ public void isReadOnly_should_return_false() {
+ assertFalse(compositeResolver.isReadOnly(elContext, BASE, PROPERTY));
}
@Test
public void setValue_should_not_infinitely_recurse() {
- compositeResolver.setValue(context, BASE, PROPERTY, VALUE);
+ compositeResolver.setValue(elContext, BASE, PROPERTY, VALUE);
}
}
View
77 test/com/github/pukkaone/jsp/FakeJspContext.java
@@ -0,0 +1,77 @@
+package com.github.pukkaone.jsp;
+
+import java.util.Enumeration;
+import java.util.HashMap;
+import javax.el.ELContext;
+import javax.servlet.jsp.JspContext;
+import javax.servlet.jsp.JspWriter;
+import javax.servlet.jsp.el.ExpressionEvaluator;
+import javax.servlet.jsp.el.VariableResolver;
+
+@SuppressWarnings("deprecation")
+class FakeJspContext extends JspContext {
+
+ HashMap<String, Object> attributes = new HashMap<String, Object>();
+
+ @Override
+ public Object findAttribute(String arg0) {
+ return null;
+ }
+
+ @Override
+ public Object getAttribute(String name) {
+ return attributes.get(name);
+ }
+
+ @Override
+ public Object getAttribute(String arg0, int arg1) {
+ return null;
+ }
+
+ @Override
+ public Enumeration<String> getAttributeNamesInScope(int arg0) {
+ return null;
+ }
+
+ @Override
+ public int getAttributesScope(String arg0) {
+ return 0;
+ }
+
+ @Override
+ public ELContext getELContext() {
+ return null;
+ }
+
+ @Override
+ public ExpressionEvaluator getExpressionEvaluator() {
+ return null;
+ }
+
+ @Override
+ public JspWriter getOut() {
+ return null;
+ }
+
+ @Override
+ public VariableResolver getVariableResolver() {
+ return null;
+ }
+
+ @Override
+ public void removeAttribute(String arg0) {
+ }
+
+ @Override
+ public void removeAttribute(String arg0, int arg1) {
+ }
+
+ @Override
+ public void setAttribute(String name, Object value) {
+ attributes.put(name, value);
+ }
+
+ @Override
+ public void setAttribute(String arg0, Object arg1, int arg2) {
+ }
+}

0 comments on commit 7200a35

Please sign in to comment.