1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.velocity.introspection; |
21 |
|
|
22 |
|
import java.io.StringReader; |
23 |
|
import java.io.StringWriter; |
24 |
|
import java.util.Date; |
25 |
|
import java.util.Properties; |
26 |
|
|
27 |
|
import org.apache.velocity.runtime.RuntimeConstants; |
28 |
|
import org.apache.velocity.util.introspection.UberspectImpl; |
29 |
|
import org.junit.Assert; |
30 |
|
import org.junit.Test; |
31 |
|
import org.xwiki.test.jmock.AbstractComponentTestCase; |
32 |
|
import org.xwiki.velocity.VelocityEngine; |
33 |
|
|
34 |
|
|
35 |
|
@link |
36 |
|
|
37 |
|
@version |
38 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (60) |
Complexity: 7 |
Complexity Density: 0.13 |
|
39 |
|
public class LinkingUberspectorTest extends AbstractComponentTestCase |
40 |
|
{ |
41 |
|
private VelocityEngine engine; |
42 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
43 |
6 |
@Override... |
44 |
|
protected void registerComponents() throws Exception |
45 |
|
{ |
46 |
6 |
this.engine = getComponentManager().getInstance(VelocityEngine.class); |
47 |
|
} |
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
52 |
1 |
@Test... |
53 |
|
public void testEmptyArray() throws Exception |
54 |
|
{ |
55 |
1 |
Properties prop = new Properties(); |
56 |
1 |
prop.setProperty(RuntimeConstants.UBERSPECT_CLASSNAME, LinkingUberspector.class.getCanonicalName()); |
57 |
1 |
prop.setProperty(LinkingUberspector.UBERSPECT_ARRAY_CLASSNAMES, TestingUberspector.class.getCanonicalName()); |
58 |
1 |
TestingUberspector.methodCalls = 0; |
59 |
1 |
this.engine.initialize(prop); |
60 |
1 |
StringWriter writer = new StringWriter(); |
61 |
1 |
this.engine.evaluate(new org.apache.velocity.VelocityContext(), writer, "mytemplate", new StringReader( |
62 |
|
"#set($foo = 'hello')#set($bar = $foo.toString())$bar")); |
63 |
1 |
Assert.assertEquals("$bar", writer.toString()); |
64 |
1 |
Assert.assertEquals(1, TestingUberspector.methodCalls); |
65 |
|
} |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
71 |
1 |
@Test... |
72 |
|
public void testBasicArray() throws Exception |
73 |
|
{ |
74 |
1 |
Properties prop = new Properties(); |
75 |
1 |
prop.setProperty(RuntimeConstants.UBERSPECT_CLASSNAME, LinkingUberspector.class.getCanonicalName()); |
76 |
1 |
prop.setProperty(LinkingUberspector.UBERSPECT_ARRAY_CLASSNAMES, TestingUberspector.class.getCanonicalName() |
77 |
|
+ "," + TestingUberspector.class.getCanonicalName() + "," + UberspectImpl.class.getCanonicalName() + "," |
78 |
|
+ TestingUberspector.class.getCanonicalName()); |
79 |
1 |
TestingUberspector.methodCalls = 0; |
80 |
1 |
TestingUberspector.getterCalls = 0; |
81 |
1 |
this.engine.initialize(prop); |
82 |
1 |
StringWriter writer = new StringWriter(); |
83 |
1 |
this.engine.evaluate(new org.apache.velocity.VelocityContext(), writer, "mytemplate", new StringReader( |
84 |
|
"#set($foo = 'hello')#set($bar = $foo.toString())$bar")); |
85 |
1 |
Assert.assertEquals("hello", writer.toString()); |
86 |
1 |
Assert.assertEquals(2, TestingUberspector.methodCalls); |
87 |
1 |
Assert.assertEquals(0, TestingUberspector.getterCalls); |
88 |
|
} |
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
93 |
1 |
@Test... |
94 |
|
public void testInvalidUberspectorsAreIgnored() throws Exception |
95 |
|
{ |
96 |
1 |
Properties prop = new Properties(); |
97 |
1 |
prop.setProperty(RuntimeConstants.UBERSPECT_CLASSNAME, LinkingUberspector.class.getCanonicalName()); |
98 |
1 |
prop.setProperty(LinkingUberspector.UBERSPECT_ARRAY_CLASSNAMES, Date.class.getCanonicalName() + "," |
99 |
|
+ AbstractChainableUberspector.class.getCanonicalName() + "," + InvalidUberspector.class.getCanonicalName() |
100 |
|
+ "," + TestingUberspector.class.getCanonicalName() + "," + UberspectImpl.class.getCanonicalName()); |
101 |
1 |
TestingUberspector.methodCalls = 0; |
102 |
1 |
InvalidUberspector.methodCalls = 0; |
103 |
1 |
this.engine.initialize(prop); |
104 |
1 |
StringWriter writer = new StringWriter(); |
105 |
1 |
this.engine.evaluate(new org.apache.velocity.VelocityContext(), writer, "mytemplate", new StringReader( |
106 |
|
"#set($foo = 'hello')#set($bar = $foo.toString())$bar")); |
107 |
1 |
Assert.assertEquals("hello", writer.toString()); |
108 |
1 |
Assert.assertEquals(1, TestingUberspector.methodCalls); |
109 |
1 |
Assert.assertEquals(0, InvalidUberspector.methodCalls); |
110 |
|
} |
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
115 |
1 |
@Test... |
116 |
|
public void testDefaultUberspectorWorks() throws Exception |
117 |
|
{ |
118 |
1 |
Properties prop = new Properties(); |
119 |
1 |
prop.setProperty(RuntimeConstants.UBERSPECT_CLASSNAME, LinkingUberspector.class.getCanonicalName()); |
120 |
1 |
prop.setProperty(LinkingUberspector.UBERSPECT_ARRAY_CLASSNAMES, UberspectImpl.class.getCanonicalName()); |
121 |
1 |
this.engine.initialize(prop); |
122 |
1 |
StringWriter writer = new StringWriter(); |
123 |
1 |
this.engine.evaluate(new org.apache.velocity.VelocityContext(), writer, "mytemplate", new StringReader( |
124 |
|
"#set($foo = 'hello')" + "#set($bar = $foo.getClass().getConstructors())$bar")); |
125 |
1 |
Assert.assertTrue(writer.toString().startsWith("[Ljava.lang.reflect.Constructor")); |
126 |
|
} |
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
131 |
1 |
@Test... |
132 |
|
public void testSecureUberspectorWorks() throws Exception |
133 |
|
{ |
134 |
1 |
Properties prop = new Properties(); |
135 |
1 |
prop.setProperty(RuntimeConstants.UBERSPECT_CLASSNAME, LinkingUberspector.class.getCanonicalName()); |
136 |
1 |
prop.setProperty(LinkingUberspector.UBERSPECT_ARRAY_CLASSNAMES, SecureUberspector.class.getCanonicalName()); |
137 |
1 |
this.engine.initialize(prop); |
138 |
1 |
StringWriter writer = new StringWriter(); |
139 |
1 |
this.engine.evaluate(new org.apache.velocity.VelocityContext(), writer, "mytemplate", new StringReader( |
140 |
|
"#set($foo = 'hello')" + "#set($bar = $foo.getClass().getConstructors())$foo$bar")); |
141 |
1 |
Assert.assertEquals("hello$bar", writer.toString()); |
142 |
|
} |
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
147 |
1 |
@Test... |
148 |
|
public void testSecureUberspectorEnabledByDefault() throws Exception |
149 |
|
{ |
150 |
1 |
Properties prop = new Properties(); |
151 |
1 |
prop.setProperty(RuntimeConstants.UBERSPECT_CLASSNAME, LinkingUberspector.class.getCanonicalName()); |
152 |
1 |
prop.setProperty(LinkingUberspector.UBERSPECT_ARRAY_CLASSNAMES, ""); |
153 |
1 |
this.engine.initialize(prop); |
154 |
1 |
StringWriter writer = new StringWriter(); |
155 |
1 |
this.engine.evaluate(new org.apache.velocity.VelocityContext(), writer, "mytemplate", new StringReader( |
156 |
|
"#set($foo = 'hello')" + "#set($bar = $foo.getClass().getConstructors())$foo$bar")); |
157 |
1 |
Assert.assertEquals("hello$bar", writer.toString()); |
158 |
|
} |
159 |
|
} |