1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
31 |
|
@since |
32 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (52) |
Complexity: 7 |
Complexity Density: 0.16 |
|
33 |
|
public class ExecutionContextPropertyTest |
34 |
|
{ |
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
@param |
39 |
|
@param |
40 |
|
@return |
41 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
42 |
11 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
55 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
75 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
101 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
115 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
125 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
138 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.33 |
|
150 |
|
public static class TestCloneable implements Cloneable |
151 |
|
{ |
152 |
|
|
153 |
|
public String value = "original"; |
154 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
155 |
2 |
@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 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 1 |
|
164 |
|
public static class TestNonpublicClone implements Cloneable |
165 |
|
{ |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
166 |
0 |
@Override... |
167 |
|
protected Object clone() throws CloneNotSupportedException |
168 |
|
{ |
169 |
0 |
return super.clone(); |
170 |
|
} |
171 |
|
} |
172 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
173 |
|
public static class SomeClass |
174 |
|
{ |
175 |
|
} |
176 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
177 |
|
public static class SomeSubClass extends SomeClass |
178 |
|
{ |
179 |
|
} |
180 |
|
} |