Clover icon

sunshower-base

  1. Project Clover database Fri Apr 6 2018 03:41:27 UTC
  2. Package io.sunshower.test.common

File SerializationTestCase.java

 

Coverage histogram

../../../../img/srcFileCovDistChart0.png
50% of files have more coverage

Code metrics

0
34
10
1
105
89
18
0.53
3.4
10
1.8

Classes

Class Line # Actions
SerializationTestCase 11 34 18
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    package io.sunshower.test.common;
2   
3    import java.io.*;
4    import javax.xml.bind.JAXBContext;
5    import javax.xml.bind.JAXBException;
6    import javax.xml.bind.Marshaller;
7    import javax.xml.bind.Unmarshaller;
8    import javax.xml.transform.stream.StreamSource;
9   
10    @SuppressWarnings("unchecked")
 
11    public class SerializationTestCase {
12   
13    private final JAXBContext context;
14    private final Marshaller marshaller;
15    private final Unmarshaller unmarshaller;
16   
 
17  0 toggle public SerializationTestCase(SerializationAware.Format format, Class... bound) {
18  0 this(format, false, bound);
19    }
20   
 
21  0 toggle public SerializationTestCase(
22    SerializationAware.Format format, boolean includeRoot, Class... bound) {
23  0 this.context = SerializationAware.createContext(bound);
24   
25  0 try {
26  0 this.marshaller = this.context.createMarshaller();
27  0 this.marshaller.setProperty("eclipselink.json.include-root", includeRoot);
28    } catch (JAXBException var5) {
29  0 throw new RuntimeException(var5);
30    }
31   
32  0 try {
33  0 this.unmarshaller = this.context.createUnmarshaller();
34  0 this.unmarshaller.setProperty("eclipselink.json.include-root", includeRoot);
35    } catch (JAXBException var4) {
36  0 throw new RuntimeException(var4);
37    }
38   
39  0 this.setFormat(format);
40    }
41   
 
42  0 toggle protected void setFormat(SerializationAware.Format format) {
43  0 try {
44  0 this.unmarshaller.setProperty(format.getPropertyName(), format.getPropertyValue());
45  0 this.marshaller.setProperty(format.getPropertyName(), format.getPropertyValue());
46    } catch (Exception var3) {
47  0 throw new RuntimeException(var3);
48    }
49    }
50   
 
51  0 toggle @SuppressWarnings("unchecked")
52    public <T> T copy(T input) {
53  0 return copy(input, (Class<T>) input.getClass());
54    }
55   
 
56  0 toggle public <T> T copy(T input, Class<T> type) {
57  0 return this.read(this.write(input), type);
58    }
59   
 
60  0 toggle public <T> T read(String t) {
61  0 try {
62  0 return (T) this.unmarshaller.unmarshal(new StringReader(t));
63    } catch (JAXBException var3) {
64  0 throw new RuntimeException(var3);
65    }
66    }
67   
 
68  0 toggle public <T> T read(String t, Class<T> type) {
69  0 try {
70  0 return (T)
71    this.unmarshaller.unmarshal(new StreamSource(new StringReader(t)), type).getValue();
72    } catch (JAXBException var4) {
73  0 throw new RuntimeException(var4);
74    }
75    }
76   
 
77  0 toggle public <T> T read(InputStream inputStream, Class<T> type) {
78  0 try {
79  0 return (T)
80    this.unmarshaller
81    .unmarshal(new StreamSource(new InputStreamReader(inputStream)), type)
82    .getValue();
83    } catch (JAXBException var4) {
84  0 throw new RuntimeException(var4);
85    }
86    }
87   
 
88  0 toggle public <T> void write(T value, OutputStream output) {
89  0 try {
90  0 this.marshaller.marshal(value, output);
91    } catch (JAXBException var4) {
92  0 throw new RuntimeException(var4);
93    }
94    }
95   
 
96  0 toggle public <T> String write(T value) {
97  0 try {
98  0 StringWriter e = new StringWriter();
99  0 this.marshaller.marshal(value, e);
100  0 return e.toString();
101    } catch (JAXBException var3) {
102  0 throw new RuntimeException(var3);
103    }
104    }
105    }