|
| 1 | +package com.peterphi.std.guice.common.ognl; |
| 2 | + |
| 3 | +import ognl.AbstractMemberAccess; |
| 4 | +import ognl.Node; |
| 5 | +import ognl.Ognl; |
| 6 | +import ognl.OgnlContext; |
| 7 | +import org.junit.ComparisonFailure; |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +import java.io.ByteArrayOutputStream; |
| 11 | +import java.io.PrintStream; |
| 12 | +import java.lang.reflect.Member; |
| 13 | +import java.lang.reflect.Modifier; |
| 14 | +import java.util.Map; |
| 15 | + |
| 16 | +import static org.junit.Assert.assertEquals; |
| 17 | + |
| 18 | +/** |
| 19 | + * Unit test set up to track <a href="https://github.com/jkuhnert/ognl/issues/125">https://github.com/jkuhnert/ognl/issues/125</a>.<br |
| 20 | + * /> This unit test will pass <strong>until this bug is fixed</strong> |
| 21 | + */ |
| 22 | +public class OgnlIssue125Test |
| 23 | +{ |
| 24 | + public interface TestInterface |
| 25 | + { |
| 26 | + String request(); |
| 27 | + |
| 28 | + String request(String... args); |
| 29 | + |
| 30 | + String request(int... args); |
| 31 | + } |
| 32 | + |
| 33 | + public static class TestInterfaceImpl implements TestInterface |
| 34 | + { |
| 35 | + @Override |
| 36 | + public String request() |
| 37 | + { |
| 38 | + return "noargs"; |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | + @Override |
| 43 | + public String request(final String... args) |
| 44 | + { |
| 45 | + return "varargs string"; |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + @Override |
| 50 | + public String request(final int... args) |
| 51 | + { |
| 52 | + return "varargs int"; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + public static class OgnlRoot |
| 57 | + { |
| 58 | + public TestInterfaceImpl get() |
| 59 | + { |
| 60 | + return new TestInterfaceImpl(); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private static class OGNLPublicMemberAccess extends AbstractMemberAccess |
| 65 | + { |
| 66 | + @Override |
| 67 | + public boolean isAccessible(final Map context, final Object target, final Member member, final String propertyName) |
| 68 | + { |
| 69 | + return Modifier.isPublic(member.getModifiers()); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + /** |
| 75 | + * @throws Exception on setup failure |
| 76 | + * @throws ComparisonFailure if the bug is still present; if this test starts faiing it is a sign that the bug may be fixed - |
| 77 | + * remove the expected=ComparisonFailure! |
| 78 | + */ |
| 79 | + @Test(expected = ComparisonFailure.class) |
| 80 | + public void testOgnl() throws Exception |
| 81 | + { |
| 82 | + final PrintStream stderr = System.err; |
| 83 | + try |
| 84 | + { |
| 85 | + final OgnlRoot obj = new OgnlRoot(); |
| 86 | + final Node ognl = (Node) Ognl.parseExpression("get().request()"); |
| 87 | + |
| 88 | + final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); |
| 89 | + System.setErr(new PrintStream(buffer)); |
| 90 | + { |
| 91 | + assertEquals("noargs", ognl.getValue(new OgnlContext(null, null, new OGNLPublicMemberAccess()), obj)); |
| 92 | + } |
| 93 | + System.err.flush(); |
| 94 | + |
| 95 | + // This fails currently due to OGNL issue #125 |
| 96 | + assertEquals("Expecting no stderr output from OGNL call", "", buffer.toString("UTF-8")); |
| 97 | + } |
| 98 | + finally |
| 99 | + { |
| 100 | + // Restore stderr |
| 101 | + System.setErr(stderr); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments