1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.velocity.introspection

File LinkingUberspectorTest.java

 

Code metrics

0
53
7
1
159
106
7
0.13
7.57
7
1

Classes

Class Line # Actions
LinkingUberspectorTest 39 53 0% 7 0
1.0100%
 

Contributing tests

This file is covered by 6 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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    * Unit tests for {@link LinkingUberspector}.
36    *
37    * @version $Id: a82fc9360bd6cb54f0e2c3239a1012af13d602af $
38    */
 
39    public class LinkingUberspectorTest extends AbstractComponentTestCase
40    {
41    private VelocityEngine engine;
42   
 
43  6 toggle @Override
44    protected void registerComponents() throws Exception
45    {
46  6 this.engine = getComponentManager().getInstance(VelocityEngine.class);
47    }
48   
49    /*
50    * Tests that the uberspectors in the list are called, and without a real uberspector no methods are found.
51    */
 
52  1 toggle @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    * Tests that using several uberspectors in the array works, methods are correctly found by a valid uberspector in
69    * the chain, and after a method is found no further calls are performed.
70    */
 
71  1 toggle @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    * Tests that invalid uberspectors classnames are ignored.
92    */
 
93  1 toggle @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    * Checks that the default (non-secure) uberspector works and allows calling restricted methods.
114    */
 
115  1 toggle @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    * Checks that the secure uberspector works and does not allow calling restricted methods.
130    */
 
131  1 toggle @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    * Checks that when the array property is not configured, by default the secure ubespector is used.
146    */
 
147  1 toggle @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    }