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

File ExecutionContextPropertyTest.java

 

Code metrics

0
49
9
5
180
108
9
0.18
5.44
1.8
1

Classes

Class Line # Actions
ExecutionContextPropertyTest 33 45 0% 7 0
1.0100%
ExecutionContextPropertyTest.TestCloneable 150 3 0% 1 0
1.0100%
ExecutionContextPropertyTest.TestNonpublicClone 164 1 0% 1 2
0.00%
ExecutionContextPropertyTest.SomeClass 173 0 - 0 0
-1.0 -
ExecutionContextPropertyTest.SomeSubClass 177 0 - 0 0
-1.0 -
 

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.context;
21   
22    import java.lang.reflect.Field;
23    import java.util.Map;
24   
25    import org.junit.Assert;
26    import org.junit.Test;
27    import org.xwiki.context.internal.ExecutionContextProperty;
28   
29    /**
30    * @version $Id: d064361e47a029aab34d3642c760fd3b2e24fbc4 $
31    * @since 4.3M1
32    */
 
33    public class ExecutionContextPropertyTest
34    {
35    /**
36    * Access the properties via reflection. This method requires ReflectPermission suppressAccessChecks.
37    *
38    * @param context The execution context
39    * @param key The property key
40    * @return the execution context property corresponding to the given key.
41    */
 
42  11 toggle @SuppressWarnings("unchecked")
43    private ExecutionContextProperty fetch(ExecutionContext context, String key) throws Exception
44    {
45  11 Field propertiesField = ExecutionContext.class.getDeclaredField("properties");
46   
47  11 propertiesField.setAccessible(true);
48   
49  11 Map<String, ExecutionContextProperty> properties =
50    (Map<String, ExecutionContextProperty>) propertiesField.get(context);
51   
52  11 return properties.get(key);
53    }
54   
 
55  1 toggle @Test
56    public void defaultValues() throws Exception
57    {
58  1 final String key = "test";
59   
60  1 ExecutionContext context = new ExecutionContext();
61  1 context.newProperty(key).declare();
62   
63  1 Assert.assertFalse(fetch(context, key).isFinal());
64  1 Assert.assertTrue(key.equals(fetch(context, key).getKey()));
65  1 Assert.assertTrue(null == fetch(context, key).getValue());
66  1 Assert.assertFalse(fetch(context, key).isInherited());
67   
68  1 Object o = new Object();
69  1 context.setProperty(key, o);
70  1 Assert.assertTrue(fetch(context, key).getValue() == o);
71  1 context.setProperty(key, null);
72  1 Assert.assertTrue(fetch(context, key).getValue() == null);
73    }
74   
 
75  1 toggle @Test
76    public void cloning() throws Exception
77    {
78  1 final String k1 = "test1";
79  1 final String k2 = "test2";
80  1 final String k3 = "test3";
81   
82  1 ExecutionContext context = new ExecutionContext();
83   
84  1 TestCloneable value = new TestCloneable();
85   
86  1 context.newProperty(k1).initial(value).declare();
87   
88  1 Assert.assertTrue(value == fetch(context, k1).clone().getValue());
89   
90  1 context.newProperty(k2).initial(value).cloneValue().declare();
91   
92  1 TestCloneable clonedValue = (TestCloneable) fetch(context, k2).clone().getValue();
93   
94  1 Assert.assertTrue(value != clonedValue && clonedValue.value.equals("clone"));
95   
96  1 context.newProperty(k3).initial(value).cloneValue().makeFinal().inherited().declare();
97   
98  1 Assert.assertTrue(fetch(context, k3).clone().isClonedFrom(fetch(context, k3)));
99    }
100   
 
101  1 toggle @Test(expected = IllegalStateException.class)
102    public void cloningNonPublicCloneMethod() throws Exception
103    {
104  1 ExecutionContext context = new ExecutionContext();
105   
106  1 final String key = "test";
107   
108  1 TestNonpublicClone value = new TestNonpublicClone();
109   
110  1 context.newProperty(key).cloneValue().initial(value).declare();
111   
112  1 fetch(context, key).clone();
113    }
114   
 
115  1 toggle @Test(expected = IllegalArgumentException.class)
116    public void nonNullCheck()
117    {
118  1 ExecutionContext context = new ExecutionContext();
119   
120  1 final String key = "test";
121   
122  1 context.newProperty(key).nonNull().initial(null).declare();
123    }
124   
 
125  1 toggle @Test
126    public void typeChecking()
127    {
128  1 ExecutionContext context = new ExecutionContext();
129   
130  1 final String key = "test";
131   
132  1 context.newProperty(key).type(SomeClass.class).declare();
133   
134  1 context.setProperty(key, new SomeClass());
135  1 context.setProperty(key, new SomeSubClass());
136    }
137   
 
138  1 toggle @Test(expected = IllegalArgumentException.class)
139    public void typeCheckingMismatch()
140    {
141  1 ExecutionContext context = new ExecutionContext();
142   
143  1 final String key = "test";
144   
145  1 context.newProperty(key).type(SomeSubClass.class).declare();
146   
147  1 context.setProperty(key, new SomeClass());
148    }
149   
 
150    public static class TestCloneable implements Cloneable
151    {
152   
153    public String value = "original";
154   
 
155  2 toggle @Override
156    public Object clone() throws CloneNotSupportedException
157    {
158  2 TestCloneable clone = (TestCloneable) super.clone();
159  2 clone.value = "clone";
160  2 return clone;
161    }
162    }
163   
 
164    public static class TestNonpublicClone implements Cloneable
165    {
 
166  0 toggle @Override
167    protected Object clone() throws CloneNotSupportedException
168    {
169  0 return super.clone();
170    }
171    }
172   
 
173    public static class SomeClass
174    {
175    }
176   
 
177    public static class SomeSubClass extends SomeClass
178    {
179    }
180    }