View Javadoc

1   /**
2    * Copyright (C) cedarsoft GmbH.
3    *
4    * Licensed under the GNU General Public License version 3 (the "License")
5    * with Classpath Exception; you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at
7    *
8    *         http://www.cedarsoft.org/gpl3ce
9    *         (GPL 3 with Classpath Exception)
10   *
11   * This code is free software; you can redistribute it and/or modify it
12   * under the terms of the GNU General Public License version 3 only, as
13   * published by the Free Software Foundation. cedarsoft GmbH designates this
14   * particular file as subject to the "Classpath" exception as provided
15   * by cedarsoft GmbH in the LICENSE file that accompanied this code.
16   *
17   * This code is distributed in the hope that it will be useful, but WITHOUT
18   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20   * version 3 for more details (a copy is included in the LICENSE file that
21   * accompanied this code).
22   *
23   * You should have received a copy of the GNU General Public License version
24   * 3 along with this work; if not, write to the Free Software Foundation,
25   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26   *
27   * Please contact cedarsoft GmbH, 72810 Gomaringen, Germany,
28   * or visit www.cedarsoft.com if you need additional information or
29   * have any questions.
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   * It is necessary to define at least one DataPoint
44   * <pre>&#064;DataPoint<br/>public static final VersionEntry ENTRY1 = create(<br/> Version.valueOf( 1, 0, 0 ),<br/> &quot;&lt;xml/&gt;&quot; );</pre>
45   *
46   * @param <T> the type
47   */
48  public abstract class AbstractXmlVersionTest2<T> extends AbstractVersionTest2<T> {
49    /**
50     * Converts the xml string to a byte array used to deserialize.
51     * This method automatically adds the namespace containing the version.
52     *
53     * @param xml        the xml
54     * @param version    the version
55     * @param serializer the serializer
56     * @return the byte array using the xml string
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 }