Clover icon

sunshower-base

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

File SerializationAware.java

 

Coverage histogram

../../../../img/srcFileCovDistChart5.png
36% of files have more coverage

Code metrics

2
37
10
2
105
91
18
0.49
3.7
5
1.8

Classes

Class Line # Actions
SerializationAware 12 33 15
0.4761904847.6%
SerializationAware.Format 14 4 3
0.4285714342.9%
 

Contributing tests

This file is covered by 2 tests. .

Source view

1    package io.sunshower.test.common;
2   
3    import java.io.*;
4    import java.util.Properties;
5    import javax.xml.bind.JAXBException;
6    import javax.xml.bind.Marshaller;
7    import javax.xml.bind.Unmarshaller;
8    import org.eclipse.persistence.jaxb.JAXBContextFactory;
9    import org.eclipse.persistence.jaxb.MarshallerProperties;
10    import org.eclipse.persistence.jaxb.UnmarshallerProperties;
11   
 
12    public class SerializationAware {
13   
 
14    public enum Format {
15    XML("eclipselink.media-type", "application/xml"),
16    JSON("eclipselink.media-type", "application/json");
17   
18    private final String propertyName;
19    private final String propertyValue;
20   
 
21  2 toggle Format(String propertyName, String propertyValue) {
22  2 this.propertyName = propertyName;
23  2 this.propertyValue = propertyValue;
24    }
25   
 
26  0 toggle public String getPropertyName() {
27  0 return propertyName;
28    }
29   
 
30  0 toggle public String getPropertyValue() {
31  0 return propertyValue;
32    }
33    }
34   
 
35  2 toggle public static javax.xml.bind.JAXBContext createContext(Class<?>... types) {
36  2 try {
37  2 final Class<?>[] atypes = new Class<?>[types.length];
38  4 for (int i = 0; i < types.length; i++) {
39  2 atypes[i] = types[i];
40    }
41  2 final Properties properties = new Properties();
42  2 return JAXBContextFactory.createContext(atypes, new Properties());
43    } catch (JAXBException e) {
44  0 throw new IllegalStateException(e);
45    }
46    }
47   
 
48  2 toggle public static Marshaller createMarshaller(Format format, Class<?>... types) {
49  2 try {
50  2 Marshaller marshaller = createContext(types).createMarshaller();
51  2 marshaller.setProperty(format.propertyName, format.propertyValue);
52  2 marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
53  2 return marshaller;
54    } catch (JAXBException e) {
55  0 throw new IllegalStateException(e);
56    }
57    }
58   
 
59  0 toggle public static Unmarshaller createUnmarshaller(Format format, Class<?>... types) {
60  0 try {
61  0 Unmarshaller unmarshaller = createContext(types).createUnmarshaller();
62  0 unmarshaller.setProperty(format.propertyName, format.propertyValue);
63  0 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
64  0 return unmarshaller;
65    } catch (JAXBException e) {
66  0 throw new RuntimeException(e);
67    }
68    }
69   
 
70  0 toggle @SuppressWarnings("unchecked")
71    public static <T> T read(String t, Format format, Class<T> type) {
72  0 try {
73  0 return (T) createUnmarshaller(format, type).unmarshal(new StringReader(t));
74    } catch (JAXBException e) {
75  0 throw new RuntimeException(e);
76    }
77    }
78   
 
79  0 toggle @SuppressWarnings("unchecked")
80    public static <T> T read(InputStream inputStream, Format format, Class<T> type) {
81  0 try {
82  0 return (T) createUnmarshaller(format, type).unmarshal(new InputStreamReader(inputStream));
83    } catch (JAXBException e) {
84  0 throw new RuntimeException(e);
85    }
86    }
87   
 
88  0 toggle public static <T> void write(T value, Format format, OutputStream output) {
89  0 try {
90  0 createMarshaller(format, value.getClass()).marshal(value, output);
91    } catch (JAXBException e) {
92  0 throw new RuntimeException(e);
93    }
94    }
95   
 
96  2 toggle public static <T> String write(T value, Format format) {
97  2 try {
98  2 StringWriter writer = new StringWriter();
99  2 createMarshaller(format, value.getClass()).marshal(value, writer);
100  2 return writer.toString();
101    } catch (JAXBException e) {
102  0 throw new RuntimeException(e);
103    }
104    }
105    }