1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.job.internal.xstream; |
21 |
|
|
22 |
|
import java.io.IOException; |
23 |
|
import java.io.ObjectStreamException; |
24 |
|
import java.io.Serializable; |
25 |
|
import java.util.Objects; |
26 |
|
|
27 |
|
import org.apache.commons.io.IOUtils; |
28 |
|
import org.junit.Before; |
29 |
|
import org.junit.Test; |
30 |
|
|
31 |
|
import com.thoughtworks.xstream.XStream; |
32 |
|
|
33 |
|
import static org.junit.Assert.assertEquals; |
34 |
|
import static org.junit.Assert.assertNotNull; |
35 |
|
import static org.junit.Assert.assertNull; |
36 |
|
import static org.junit.Assert.assertSame; |
37 |
|
|
38 |
|
|
39 |
|
@link |
40 |
|
|
41 |
|
@version |
42 |
|
|
|
|
| 98.1% |
Uncovered Elements: 1 (52) |
Complexity: 13 |
Complexity Density: 0.34 |
|
43 |
|
public class SafeXStreamTest |
44 |
|
{ |
|
|
| 50% |
Uncovered Elements: 2 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
45 |
|
static class RecursiveObject |
46 |
|
{ |
47 |
|
RecursiveObject recurse; |
48 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
49 |
1 |
public RecursiveObject()... |
50 |
|
{ |
51 |
1 |
this.recurse = this; |
52 |
|
} |
53 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
54 |
0 |
@Override... |
55 |
|
public String toString() |
56 |
|
{ |
57 |
0 |
return "recursive object"; |
58 |
|
} |
59 |
|
} |
60 |
|
|
|
|
| 50% |
Uncovered Elements: 2 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
61 |
|
static class RecursiveObjectThroughArray |
62 |
|
{ |
63 |
|
RecursiveObjectThroughArray[] recurse; |
64 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
65 |
1 |
public RecursiveObjectThroughArray()... |
66 |
|
{ |
67 |
1 |
this.recurse = new RecursiveObjectThroughArray[] { this }; |
68 |
|
} |
69 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
70 |
0 |
@Override... |
71 |
|
public String toString() |
72 |
|
{ |
73 |
0 |
return "recursive object through array"; |
74 |
|
} |
75 |
|
} |
76 |
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 3 |
Complexity Density: 3 |
|
77 |
|
static class FailToSerializeObject implements Serializable |
78 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
79 |
1 |
private void writeObject(java.io.ObjectOutputStream out) throws IOException... |
80 |
|
{ |
81 |
1 |
throw new IOException(); |
82 |
|
} |
83 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
84 |
1 |
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException... |
85 |
|
{ |
86 |
|
} |
87 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
88 |
0 |
private void readObjectNoData() throws ObjectStreamException... |
89 |
|
{ |
90 |
|
} |
91 |
|
} |
92 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
93 |
|
static class FailToSerializeField implements Serializable |
94 |
|
{ |
95 |
|
FailToSerializeObject failingObject; |
96 |
|
} |
97 |
|
|
98 |
|
@org.xwiki.job.annotation.Serializable(false) |
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 3 |
Complexity Density: 1 |
|
99 |
|
private static class NotSerializableObject |
100 |
|
{ |
101 |
|
public String field; |
102 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
103 |
3 |
public NotSerializableObject(String field)... |
104 |
|
{ |
105 |
3 |
this.field = field; |
106 |
|
} |
107 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
108 |
0 |
@Override... |
109 |
|
public boolean equals(Object obj) |
110 |
|
{ |
111 |
0 |
return Objects.equals(((NotSerializableObject) obj).field, this.field); |
112 |
|
} |
113 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
114 |
1 |
@Override... |
115 |
|
public String toString() |
116 |
|
{ |
117 |
1 |
return this.field; |
118 |
|
} |
119 |
|
} |
120 |
|
|
|
|
| 50% |
Uncovered Elements: 2 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
121 |
|
private static class NotSerializableField |
122 |
|
{ |
123 |
|
public NotSerializableObject field; |
124 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
125 |
1 |
public NotSerializableField(String field)... |
126 |
|
{ |
127 |
1 |
this.field = new NotSerializableObject(field); |
128 |
|
} |
129 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
130 |
0 |
@Override... |
131 |
|
public boolean equals(Object obj) |
132 |
|
{ |
133 |
0 |
return Objects.equals(((NotSerializableObject) obj).field, this.field); |
134 |
|
} |
135 |
|
} |
136 |
|
|
137 |
|
@org.xwiki.job.annotation.Serializable(false) |
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
138 |
|
static class NotSerializableObjectWithFailingToString |
139 |
|
{ |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
140 |
1 |
public NotSerializableObjectWithFailingToString()... |
141 |
|
{ |
142 |
|
|
143 |
|
} |
144 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
145 |
1 |
@Override... |
146 |
|
public String toString() |
147 |
|
{ |
148 |
1 |
throw new RuntimeException(); |
149 |
|
} |
150 |
|
} |
151 |
|
|
152 |
|
private XStream xstream; |
153 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
154 |
9 |
@Before... |
155 |
|
public void before() |
156 |
|
{ |
157 |
9 |
this.xstream = new SafeXStream(); |
158 |
|
} |
159 |
|
|
|
|
| 85.7% |
Uncovered Elements: 1 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
160 |
8 |
private Object writeread(Object obj, String resource) throws IOException... |
161 |
|
{ |
162 |
8 |
String str = this.xstream.toXML(obj); |
163 |
|
|
164 |
8 |
if (resource != null) { |
165 |
8 |
String content = IOUtils.toString(getClass().getResourceAsStream(resource)); |
166 |
|
|
167 |
8 |
assertEquals(content, str); |
168 |
|
} |
169 |
|
|
170 |
8 |
return this.xstream.fromXML(str); |
171 |
|
} |
172 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
173 |
1 |
private Object assertReadwrite(String resource) throws IOException... |
174 |
|
{ |
175 |
1 |
String content = IOUtils.toString(getClass().getResourceAsStream(resource)); |
176 |
|
|
177 |
1 |
Object obj = this.xstream.fromXML(content); |
178 |
|
|
179 |
1 |
String str = this.xstream.toXML(obj); |
180 |
|
|
181 |
1 |
assertEquals(content, str); |
182 |
|
|
183 |
1 |
return obj; |
184 |
|
} |
185 |
|
|
186 |
|
|
187 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
188 |
1 |
@Test... |
189 |
|
public void testRecursiveObject() throws IOException |
190 |
|
{ |
191 |
1 |
RecursiveObject obj = (RecursiveObject) writeread(new RecursiveObject(), "/xstream/RecursiveObject.xml"); |
192 |
|
|
193 |
1 |
assertNotNull(obj); |
194 |
1 |
assertNotNull(obj.recurse); |
195 |
1 |
assertSame(obj, obj.recurse); |
196 |
|
} |
197 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
198 |
1 |
@Test... |
199 |
|
public void testRecursiveObjectThroughArray() throws IOException |
200 |
|
{ |
201 |
1 |
RecursiveObjectThroughArray obj = |
202 |
|
(RecursiveObjectThroughArray) writeread(new RecursiveObjectThroughArray(), |
203 |
|
"/xstream/RecursiveObjectThroughArray.xml"); |
204 |
|
|
205 |
1 |
assertNotNull(obj); |
206 |
1 |
assertNotNull(obj.recurse); |
207 |
1 |
assertEquals(1, obj.recurse.length); |
208 |
1 |
assertSame(obj, obj.recurse[0]); |
209 |
|
} |
210 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
211 |
1 |
@Test... |
212 |
|
public void testArrayWithReference() throws IOException |
213 |
|
{ |
214 |
1 |
assertReadwrite("/xstream/ArrayWithReference.xml"); |
215 |
|
} |
216 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
217 |
1 |
@Test... |
218 |
|
public void testFailToSerializeObject() throws IOException |
219 |
|
{ |
220 |
1 |
FailToSerializeObject obj = |
221 |
|
(FailToSerializeObject) writeread(new FailToSerializeObject(), "/xstream/FailToSerializeObject.xml"); |
222 |
|
|
223 |
1 |
assertNotNull(obj); |
224 |
|
} |
225 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
226 |
1 |
@Test... |
227 |
|
public void testFailToSerializeField() throws IOException |
228 |
|
{ |
229 |
1 |
FailToSerializeField obj = |
230 |
|
(FailToSerializeField) writeread(new FailToSerializeField(), "/xstream/FailToSerializeField.xml"); |
231 |
|
|
232 |
1 |
assertNotNull(obj); |
233 |
1 |
assertNull(obj.failingObject); |
234 |
|
} |
235 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
236 |
1 |
@Test... |
237 |
|
public void testNotSerializableObject() throws IOException |
238 |
|
{ |
239 |
1 |
NotSerializableObject obj = |
240 |
|
(NotSerializableObject) writeread(new NotSerializableObject("value"), "/xstream/NotSerializableObject.xml"); |
241 |
|
|
242 |
1 |
assertNotNull(obj); |
243 |
1 |
assertNull(obj.field); |
244 |
|
} |
245 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
246 |
1 |
@Test... |
247 |
|
public void testNotSerializableObjectInArray() throws IOException |
248 |
|
{ |
249 |
1 |
Object[] obj = |
250 |
|
(Object[]) writeread(new Object[] { new NotSerializableObject("value") }, |
251 |
|
"/xstream/NotSerializableObjectInArray.xml"); |
252 |
|
|
253 |
1 |
assertNotNull(obj); |
254 |
1 |
assertEquals("value", obj[0]); |
255 |
|
} |
256 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
257 |
1 |
@Test... |
258 |
|
public void testNotSerializableField() throws IOException |
259 |
|
{ |
260 |
1 |
NotSerializableField obj = |
261 |
|
(NotSerializableField) writeread(new NotSerializableField("value"), "/xstream/NotSerializableField.xml"); |
262 |
|
|
263 |
1 |
assertNotNull(obj); |
264 |
1 |
assertNull(obj.field); |
265 |
|
} |
266 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
267 |
1 |
@Test... |
268 |
|
public void testNotSerializableObjectWithFailingToStringInArray() throws IOException |
269 |
|
{ |
270 |
1 |
Object[] obj = |
271 |
|
(Object[]) writeread(new Object[] { new NotSerializableObjectWithFailingToString() }, |
272 |
|
"/xstream/NotSerializableObjectWithFailingToStringInArray.xml"); |
273 |
|
|
274 |
1 |
assertNotNull(obj); |
275 |
1 |
assertEquals(null, obj[0]); |
276 |
|
} |
277 |
|
} |