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

File SafeXStreamTest.java

 

Code metrics

2
49
26
8
277
199
27
0.55
1.88
3.25
1.04

Classes

Class Line # Actions
SafeXStreamTest 43 38 0% 13 1
0.980769298.1%
SafeXStreamTest.RecursiveObject 45 2 0% 2 2
0.550%
SafeXStreamTest.RecursiveObjectThroughArray 61 2 0% 2 2
0.550%
SafeXStreamTest.FailToSerializeObject 77 1 0% 3 1
0.7575%
SafeXStreamTest.FailToSerializeField 93 0 - 0 0
-1.0 -
SafeXStreamTest.NotSerializableObject 99 3 0% 3 2
0.666666766.7%
SafeXStreamTest.NotSerializableField 121 2 0% 2 2
0.550%
SafeXStreamTest.NotSerializableObjectWithFailingToString 138 1 0% 2 0
1.0100%
 

Contributing tests

This file is covered by 9 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.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    * Validate {@link SafeXStream}.
40    *
41    * @version $Id: 316171b3ee8d8c23458361a6867a04ceecb39804 $
42    */
 
43    public class SafeXStreamTest
44    {
 
45    static class RecursiveObject
46    {
47    RecursiveObject recurse;
48   
 
49  1 toggle public RecursiveObject()
50    {
51  1 this.recurse = this;
52    }
53   
 
54  0 toggle @Override
55    public String toString()
56    {
57  0 return "recursive object";
58    }
59    }
60   
 
61    static class RecursiveObjectThroughArray
62    {
63    RecursiveObjectThroughArray[] recurse;
64   
 
65  1 toggle public RecursiveObjectThroughArray()
66    {
67  1 this.recurse = new RecursiveObjectThroughArray[] { this };
68    }
69   
 
70  0 toggle @Override
71    public String toString()
72    {
73  0 return "recursive object through array";
74    }
75    }
76   
 
77    static class FailToSerializeObject implements Serializable
78    {
 
79  1 toggle private void writeObject(java.io.ObjectOutputStream out) throws IOException
80    {
81  1 throw new IOException();
82    }
83   
 
84  1 toggle private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException
85    {
86    }
87   
 
88  0 toggle private void readObjectNoData() throws ObjectStreamException
89    {
90    }
91    }
92   
 
93    static class FailToSerializeField implements Serializable
94    {
95    FailToSerializeObject failingObject;
96    }
97   
98    @org.xwiki.job.annotation.Serializable(false)
 
99    private static class NotSerializableObject
100    {
101    public String field;
102   
 
103  3 toggle public NotSerializableObject(String field)
104    {
105  3 this.field = field;
106    }
107   
 
108  0 toggle @Override
109    public boolean equals(Object obj)
110    {
111  0 return Objects.equals(((NotSerializableObject) obj).field, this.field);
112    }
113   
 
114  1 toggle @Override
115    public String toString()
116    {
117  1 return this.field;
118    }
119    }
120   
 
121    private static class NotSerializableField
122    {
123    public NotSerializableObject field;
124   
 
125  1 toggle public NotSerializableField(String field)
126    {
127  1 this.field = new NotSerializableObject(field);
128    }
129   
 
130  0 toggle @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)
 
138    static class NotSerializableObjectWithFailingToString
139    {
 
140  1 toggle public NotSerializableObjectWithFailingToString()
141    {
142   
143    }
144   
 
145  1 toggle @Override
146    public String toString()
147    {
148  1 throw new RuntimeException();
149    }
150    }
151   
152    private XStream xstream;
153   
 
154  9 toggle @Before
155    public void before()
156    {
157  9 this.xstream = new SafeXStream();
158    }
159   
 
160  8 toggle 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   
 
173  1 toggle 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    // Tests
187   
 
188  1 toggle @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   
 
198  1 toggle @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   
 
211  1 toggle @Test
212    public void testArrayWithReference() throws IOException
213    {
214  1 assertReadwrite("/xstream/ArrayWithReference.xml");
215    }
216   
 
217  1 toggle @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   
 
226  1 toggle @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   
 
236  1 toggle @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   
 
246  1 toggle @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   
 
257  1 toggle @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   
 
267  1 toggle @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    }