1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.job.internal

File JobStatusSerializerTest.java

 

Code metrics

0
137
36
9
479
342
36
0.26
3.81
4
1

Classes

Class Line # Actions
JobStatusSerializerTest 53 123 0% 23 0
1.0100%
JobStatusSerializerTest.SerializableCrossReferenceObject 60 1 0% 1 0
1.0100%
JobStatusSerializerTest.SerializableObjectTest 71 1 0% 1 0
1.0100%
JobStatusSerializerTest.CustomSerializableObject 82 2 0% 2 0
1.0100%
JobStatusSerializerTest.SerializableCustomObject 99 2 0% 2 0
1.0100%
JobStatusSerializerTest.NotSerializableCustomObject 116 3 0% 3 2
0.666666766.7%
JobStatusSerializerTest.SerializableProvider 139 1 0% 1 2
0.00%
JobStatusSerializerTest.SerializableImplementationProvider 148 1 0% 1 2
0.00%
JobStatusSerializerTest.TestException 159 3 0% 2 0
1.0100%
 

Contributing tests

This file is covered by 21 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;
21   
22    import java.io.File;
23    import java.io.IOException;
24    import java.util.Objects;
25   
26    import javax.inject.Provider;
27    import javax.xml.parsers.ParserConfigurationException;
28   
29    import org.junit.jupiter.api.BeforeEach;
30    import org.junit.jupiter.api.Test;
31    import org.slf4j.Logger;
32    import org.xwiki.component.manager.ComponentManager;
33    import org.xwiki.job.DefaultJobStatus;
34    import org.xwiki.job.DefaultRequest;
35    import org.xwiki.job.Request;
36    import org.xwiki.job.annotation.Serializable;
37    import org.xwiki.job.event.status.JobStatus;
38    import org.xwiki.job.test.SerializableStandaloneComponent;
39    import org.xwiki.job.test.StandaloneComponent;
40    import org.xwiki.logging.marker.TranslationMarker;
41   
42    import static org.junit.jupiter.api.Assertions.assertEquals;
43    import static org.junit.jupiter.api.Assertions.assertNotNull;
44    import static org.junit.jupiter.api.Assertions.assertNull;
45    import static org.junit.jupiter.api.Assertions.assertSame;
46    import static org.mockito.Mockito.mock;
47   
48    /**
49    * Validate {@link JobStatusSerializer}.
50    *
51    * @version $Id: 6b549b2ab65566ec0c8f77a067a3bafcec48b5ea $
52    */
 
53    public class JobStatusSerializerTest
54    {
55    private JobStatusSerializer serializer;
56   
57    private File testFile = new File("target/test/status.xml");
58   
59    @Serializable
 
60    private static class SerializableCrossReferenceObject
61    {
62    public SerializableCrossReferenceObject field;
63   
 
64  1 toggle public SerializableCrossReferenceObject()
65    {
66  1 this.field = this;
67    }
68    }
69   
70    @Serializable
 
71    private static class SerializableObjectTest
72    {
73    public Object field;
74   
 
75  7 toggle public SerializableObjectTest(Object field)
76    {
77  7 this.field = field;
78    }
79    }
80   
81    @Serializable
 
82    private static class CustomSerializableObject
83    {
84    public String field;
85   
 
86  2 toggle public CustomSerializableObject(String field)
87    {
88  2 this.field = field;
89    }
90   
 
91  1 toggle @Override
92    public boolean equals(Object obj)
93    {
94  1 return Objects.equals(((CustomSerializableObject) obj).field, this.field);
95    }
96    }
97   
98    @Serializable
 
99    private static class SerializableCustomObject
100    {
101    public String field;
102   
 
103  2 toggle public SerializableCustomObject(String field)
104    {
105  2 this.field = field;
106    }
107   
 
108  1 toggle @Override
109    public boolean equals(Object obj)
110    {
111  1 return Objects.equals(((SerializableCustomObject) obj).field, this.field);
112    }
113    }
114   
115    @Serializable(false)
 
116    private static class NotSerializableCustomObject
117    {
118    public String field;
119   
 
120  1 toggle public NotSerializableCustomObject(String field)
121    {
122  1 this.field = field;
123    }
124   
 
125  0 toggle @Override
126    public boolean equals(Object obj)
127    {
128  0 return Objects.equals(((NotSerializableCustomObject) obj).field, this.field);
129    }
130   
 
131  1 toggle @Override
132    public String toString()
133    {
134  1 return this.field;
135    }
136    }
137   
138    @Serializable
 
139    private static class SerializableProvider implements Provider<String>
140    {
 
141  0 toggle @Override
142    public String get()
143    {
144  0 return null;
145    }
146    }
147   
 
148    private static class SerializableImplementationProvider implements Provider<String>, java.io.Serializable
149    {
150    private static final long serialVersionUID = 1L;
151   
 
152  0 toggle @Override
153    public String get()
154    {
155  0 return null;
156    }
157    }
158   
 
159    private static class TestException extends Exception
160    {
161    private Object custom;
162   
 
163  1 toggle public TestException(String message, Throwable cause, Object custom)
164    {
165  1 super(message, cause);
166   
167  1 this.custom = custom;
168    }
169   
 
170  1 toggle public Object getCustom()
171    {
172  1 return this.custom;
173    }
174    }
175   
 
176  21 toggle @BeforeEach
177    public void before() throws ParserConfigurationException
178    {
179  21 this.serializer = new JobStatusSerializer();
180    }
181   
 
182  21 toggle private JobStatus writeread(JobStatus status) throws IOException
183    {
184  21 this.serializer.write(status, this.testFile);
185   
186  21 return this.serializer.read(this.testFile);
187    }
188   
189    // Tests
190   
 
191  1 toggle @Test
192    public void test() throws IOException
193    {
194  1 JobStatus status = new DefaultJobStatus<Request>(new DefaultRequest(), null, null, null);
195   
196  1 writeread(status);
197    }
198   
 
199  1 toggle @Test
200    public void testLogMessage() throws IOException
201    {
202  1 JobStatus status = new DefaultJobStatus<Request>(new DefaultRequest(), null, null, null);
203   
204  1 status.getLog().error("error message");
205   
206  1 status = writeread(status);
207   
208  1 assertNotNull(status.getLog());
209  1 assertEquals("error message", status.getLog().peek().getMessage());
210    }
211   
 
212  1 toggle @Test
213    public void testLogMarker() throws IOException
214    {
215  1 JobStatus status = new DefaultJobStatus<Request>(new DefaultRequest(), null, null, null);
216   
217  1 status.getLog().error(new TranslationMarker("translation.key"), "error message");
218   
219  1 status = writeread(status);
220   
221  1 assertNotNull(status.getLog());
222  1 assertEquals("error message", status.getLog().peek().getMessage());
223  1 assertEquals(new TranslationMarker("translation.key"), status.getLog().peek().getMarker());
224    }
225   
 
226  1 toggle @Test
227    public void testLogWithException() throws IOException
228    {
229  1 JobStatus status = new DefaultJobStatus<Request>(new DefaultRequest(), null, null, null);
230   
231  1 status.getLog().error("error message",
232    new TestException("exception message", new Exception("cause"), "custom"));
233   
234  1 status = writeread(status);
235   
236  1 assertNotNull(status.getLog());
237  1 assertEquals("error message", status.getLog().peek().getMessage());
238  1 assertEquals("exception message", status.getLog().peek().getThrowable().getMessage());
239  1 assertEquals("cause", status.getLog().peek().getThrowable().getCause().getMessage());
240  1 assertNull(((TestException) status.getLog().peek().getThrowable()).getCustom(), "exception message");
241    }
242   
 
243  1 toggle @Test
244    public void testLogWithArguments() throws IOException
245    {
246  1 JobStatus status = new DefaultJobStatus<Request>(new DefaultRequest(), null, null, null);
247   
248  1 status.getLog().error("error message", "arg1", "arg2");
249   
250  1 status = writeread(status);
251   
252  1 assertNotNull(status.getLog());
253  1 assertEquals("error message", status.getLog().peek().getMessage());
254  1 assertEquals("arg1", status.getLog().peek().getArgumentArray()[0]);
255  1 assertEquals("arg2", status.getLog().peek().getArgumentArray()[1]);
256    }
257   
 
258  1 toggle @Test
259    public void testLogWithNullArguments() throws IOException
260    {
261  1 JobStatus status = new DefaultJobStatus<Request>(new DefaultRequest(), null, null, null);
262   
263  1 status.getLog().error("error message", "arg1", null, "arg3");
264   
265  1 status = writeread(status);
266   
267  1 assertNotNull(status.getLog());
268  1 assertEquals("error message", status.getLog().peek().getMessage());
269  1 assertEquals("arg1", status.getLog().peek().getArgumentArray()[0]);
270  1 assertNull(status.getLog().peek().getArgumentArray()[1]);
271  1 assertEquals("arg3", status.getLog().peek().getArgumentArray()[2]);
272    }
273   
 
274  1 toggle @Test
275    public void testLogWithComponentArgument() throws IOException
276    {
277  1 JobStatus status = new DefaultJobStatus<Request>(new DefaultRequest(), null, null, null);
278   
279  1 status.getLog().error("error message", new DefaultJobStatusStore());
280   
281  1 status = writeread(status);
282   
283  1 assertNotNull(status.getLog());
284  1 assertEquals("error message", status.getLog().peek().getMessage());
285  1 assertEquals(String.class, status.getLog().peek().getArgumentArray()[0].getClass());
286    }
287   
 
288  1 toggle @Test
289    public void testLogWithStandaloneComponentArgument() throws IOException
290    {
291  1 JobStatus status = new DefaultJobStatus<Request>(new DefaultRequest(), null, null, null);
292   
293  1 status.getLog().error("error message", new StandaloneComponent());
294   
295  1 status = writeread(status);
296   
297  1 assertNotNull(status.getLog());
298  1 assertEquals("error message", status.getLog().peek().getMessage());
299  1 assertEquals(String.class, status.getLog().peek().getArgumentArray()[0].getClass());
300    }
301   
 
302  1 toggle @Test
303    public void testLogWithSerializableStandaloneComponentArgument() throws IOException
304    {
305  1 JobStatus status = new DefaultJobStatus<Request>(new DefaultRequest(), null, null, null);
306   
307  1 status.getLog().error("error message", new SerializableStandaloneComponent());
308   
309  1 status = writeread(status);
310   
311  1 assertNotNull(status.getLog());
312  1 assertEquals("error message", status.getLog().peek().getMessage());
313  1 assertEquals(SerializableStandaloneComponent.class, status.getLog().peek().getArgumentArray()[0].getClass());
314    }
315   
 
316  1 toggle @Test
317    public void testLogWithCrossReference() throws IOException
318    {
319  1 JobStatus status = new DefaultJobStatus<Request>(new DefaultRequest(), null, null, null);
320   
321  1 status.getLog().error("message", new SerializableCrossReferenceObject());
322   
323  1 status = writeread(status);
324   
325  1 assertNotNull(status.getLog());
326  1 SerializableCrossReferenceObject obj =
327    (SerializableCrossReferenceObject) status.getLog().peek().getArgumentArray()[0];
328  1 assertSame(obj, obj.field);
329    }
330   
 
331  1 toggle @Test
332    public void testLogWithComponentField() throws IOException
333    {
334  1 JobStatus status = new DefaultJobStatus<Request>(new DefaultRequest(), null, null, null);
335   
336  1 status.getLog().error("error message", new SerializableObjectTest(new DefaultJobStatusStore()));
337   
338  1 status = writeread(status);
339   
340  1 assertNotNull(status.getLog());
341  1 assertNull(((SerializableObjectTest) status.getLog().peek().getArgumentArray()[0]).field);
342    }
343   
 
344  1 toggle @Test
345    public void testLogWithStandaloneComponentField() throws IOException
346    {
347  1 JobStatus status = new DefaultJobStatus<Request>(new DefaultRequest(), null, null, null);
348   
349  1 status.getLog().error("error message", new SerializableObjectTest(new StandaloneComponent()));
350   
351  1 status = writeread(status);
352   
353  1 assertNotNull(status.getLog());
354  1 assertNull(((SerializableObjectTest) status.getLog().peek().getArgumentArray()[0]).field);
355    }
356   
 
357  1 toggle @Test
358    public void testLogWithLoggerField() throws IOException
359    {
360  1 JobStatus status = new DefaultJobStatus<Request>(new DefaultRequest(), null, null, null);
361   
362  1 status.getLog().error("error message", new SerializableObjectTest(mock(Logger.class)));
363   
364  1 status = writeread(status);
365   
366  1 assertNotNull(status.getLog());
367  1 assertNull(((SerializableObjectTest) status.getLog().peek().getArgumentArray()[0]).field);
368    }
369   
 
370  1 toggle @Test
371    public void testLogWithProviderField() throws IOException
372    {
373  1 JobStatus status = new DefaultJobStatus<Request>(new DefaultRequest(), null, null, null);
374   
375  1 status.getLog().error("error message", new SerializableObjectTest(mock(Provider.class)));
376   
377  1 status = writeread(status);
378   
379  1 assertNotNull(status.getLog());
380  1 assertNull(((SerializableObjectTest) status.getLog().peek().getArgumentArray()[0]).field);
381    }
382   
 
383  1 toggle @Test
384    public void testLogWithComponentManagerField() throws IOException
385    {
386  1 JobStatus status = new DefaultJobStatus<Request>(new DefaultRequest(), null, null, null);
387   
388  1 status.getLog().error("error message", new SerializableObjectTest(mock(ComponentManager.class)));
389   
390  1 status = writeread(status);
391   
392  1 assertNotNull(status.getLog());
393  1 assertNull(((SerializableObjectTest) status.getLog().peek().getArgumentArray()[0]).field);
394    }
395   
 
396  1 toggle @Test
397    public void testLogWithSerializableProviderField() throws IOException
398    {
399  1 JobStatus status = new DefaultJobStatus<Request>(new DefaultRequest(), null, null, null);
400   
401  1 status.getLog().error("error message", new SerializableObjectTest(new SerializableProvider()));
402   
403  1 status = writeread(status);
404   
405  1 assertNotNull(status.getLog());
406  1 assertEquals("error message", status.getLog().peek().getMessage());
407  1 assertEquals(SerializableProvider.class,
408    ((SerializableObjectTest) status.getLog().peek().getArgumentArray()[0]).field.getClass());
409    }
410   
 
411  1 toggle @Test
412    public void testLogWithSerializableImplementationProviderField() throws IOException
413    {
414  1 JobStatus status = new DefaultJobStatus<Request>(new DefaultRequest(), null, null, null);
415   
416  1 status.getLog().error("error message", new SerializableObjectTest(new SerializableImplementationProvider()));
417   
418  1 status = writeread(status);
419   
420  1 assertNotNull(status.getLog());
421  1 assertEquals("error message", status.getLog().peek().getMessage());
422  1 assertEquals(SerializableImplementationProvider.class,
423    ((SerializableObjectTest) status.getLog().peek().getArgumentArray()[0]).field.getClass());
424    }
425   
 
426  1 toggle @Test
427    public void testLogWithCustomObjectArgument() throws IOException
428    {
429  1 JobStatus status = new DefaultJobStatus<Request>(new DefaultRequest(), null, null, null);
430   
431  1 status.getLog().error("error message", new CustomSerializableObject("value"));
432   
433  1 status = writeread(status);
434   
435  1 assertNotNull(status.getLog());
436  1 assertEquals("error message", status.getLog().peek().getMessage());
437  1 assertEquals(new CustomSerializableObject("value"), status.getLog().peek().getArgumentArray()[0]);
438    }
439   
 
440  1 toggle @Test
441    public void testLogWithSerializableCustomObjectArgument() throws IOException
442    {
443  1 JobStatus status = new DefaultJobStatus<Request>(new DefaultRequest(), null, null, null);
444   
445  1 status.getLog().error("error message", new SerializableCustomObject("value"));
446   
447  1 status = writeread(status);
448   
449  1 assertNotNull(status.getLog());
450  1 assertEquals("error message", status.getLog().peek().getMessage());
451  1 assertEquals(new SerializableCustomObject("value"), status.getLog().peek().getArgumentArray()[0]);
452    }
453   
 
454  1 toggle @Test
455    public void testLogWithNotSerializableCustomObjectArgument() throws IOException
456    {
457  1 JobStatus status = new DefaultJobStatus<Request>(new DefaultRequest(), null, null, null);
458   
459  1 status.getLog().error("error message", new NotSerializableCustomObject("value"));
460   
461  1 status = writeread(status);
462   
463  1 assertNotNull(status.getLog());
464  1 assertEquals("error message", status.getLog().peek().getMessage());
465  1 assertEquals("value", status.getLog().peek().getArgumentArray()[0]);
466    }
467   
 
468  1 toggle @Test
469    public void testProgress() throws IOException
470    {
471  1 JobStatus status = new DefaultJobStatus<Request>(new DefaultRequest(), null, null, null);
472   
473  1 status = writeread(status);
474   
475  1 assertNotNull(status.getProgress());
476  1 assertEquals(0.0d, status.getProgress().getOffset(), 0.1d);
477  1 assertEquals(0.0d, status.getProgress().getCurrentLevelOffset(), 0.1d);
478    }
479    }