1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package com.cedarsoft.serialization;
33
34 import com.cedarsoft.Version;
35 import org.apache.commons.io.IOUtils;
36 import org.jdom.JDOMException;
37
38 import javax.annotation.Nonnull;
39 import java.io.IOException;
40 import java.net.URL;
41
42
43
44
45
46
47
48 public abstract class AbstractXmlVersionTest2<T> extends AbstractVersionTest2<T> {
49
50
51
52
53
54
55
56
57
58 @Nonnull
59 protected static byte[] processXml( @Nonnull String xml, @Nonnull Version version, @Nonnull AbstractXmlSerializer<?, ?, ?, ?> serializer ) throws Exception {
60 return processXml( xml, serializer.createNameSpace( version ) );
61 }
62
63 @Nonnull
64 protected static byte[] processXml( @Nonnull byte[] xml, @Nonnull Version version, @Nonnull AbstractXmlSerializer<?, ?, ?, ?> serializer ) throws Exception {
65 return processXml( xml, serializer.createNameSpace( version ) );
66 }
67
68 @Nonnull
69 protected static byte[] processXml( @Nonnull String xml, @Nonnull String nameSpace ) throws JDOMException, IOException {
70 return AbstractXmlSerializerTest2.addNameSpace( nameSpace, xml.getBytes() ).getBytes();
71 }
72
73 @Nonnull
74 protected static byte[] processXml( @Nonnull byte[] xml, @Nonnull String nameSpace ) throws JDOMException, IOException {
75 return AbstractXmlSerializerTest2.addNameSpace( nameSpace, xml ).getBytes();
76 }
77
78 @Nonnull
79 protected static VersionEntry create( @Nonnull Version version, @Nonnull String xml ) {
80 return new XmlVersionEntry( version, xml );
81 }
82
83 @Nonnull
84 protected static VersionEntry create( @Nonnull Version version, @Nonnull URL expected ) {
85 try {
86 return new XmlVersionEntry( version, IOUtils.toByteArray( expected.openStream() ) );
87 } catch ( IOException e ) {
88 throw new RuntimeException( e );
89 }
90 }
91
92 public static class XmlVersionEntry implements VersionEntry {
93 @Nonnull
94 private final Version version;
95 @Nonnull
96
97 private final byte[] xml;
98
99 public XmlVersionEntry( @Nonnull Version version, @Nonnull byte[] xml ) {
100 this.version = version;
101 this.xml = xml;
102 }
103
104 public XmlVersionEntry( @Nonnull Version version, @Nonnull String xml ) {
105 this( version, xml.getBytes() );
106 }
107
108 @Nonnull
109 @Override
110 public Version getVersion() {
111 return version;
112 }
113
114 @Nonnull
115 @Override
116 public byte[] getSerialized( @Nonnull Serializer<?> serializer ) throws Exception {
117 return processXml( xml, version, ( AbstractXmlSerializer<?, ?, ?, ?> ) serializer );
118 }
119 }
120 }