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 org.junit.*;
35  import org.xml.sax.SAXException;
36  
37  import javax.annotation.Nonnull;
38  import java.io.ByteArrayInputStream;
39  import java.io.ByteArrayOutputStream;
40  import java.io.IOException;
41  
42  import static org.junit.Assert.*;
43  
44  /**
45   * Abstract class for serializer tests
46   *
47   * @param <T> the type of the serialized object
48   * @deprecated use {@link AbstractSerializerTest2} instead
49   */
50  @Deprecated
51  public abstract class AbstractSerializerTest<T> {
52    /**
53     * Default test method that checks the serialization and deserialization using the latest format
54     *
55     * @throws IOException
56     * @throws SAXException
57     */
58    @Test
59    public void testSerializer() throws Exception {
60      Serializer<T> serializer = getSerializer();
61  
62      T objectToSerialize = createObjectToSerialize();
63  
64      ByteArrayOutputStream out = new ByteArrayOutputStream();
65      serializer.serialize( objectToSerialize, out );
66  
67      byte[] serialized = out.toByteArray();
68      verifySerialized( serialized );
69  
70      T deserialized = serializer.deserialize( new ByteArrayInputStream( serialized ) );
71  
72      verifyDeserialized( deserialized );
73    }
74  
75    /**
76     * Returns the serializer
77     *
78     * @return the serializer
79     */
80    @Nonnull
81    protected abstract Serializer<T> getSerializer() throws Exception;
82  
83    /**
84     * Verifies the serialized object
85     *
86     * @param serialized the serialized object
87     * @throws SAXException
88     * @throws IOException
89     */
90    protected abstract void verifySerialized( @Nonnull byte[] serialized ) throws Exception;
91  
92    /**
93     * Creates the object to serialize
94     *
95     * @return the object to serialize
96     */
97    @Nonnull
98    protected abstract T createObjectToSerialize() throws Exception;
99  
100   /**
101    * Verifies the deserialized object.
102    * The default implementation simply calls equals
103    *
104    * @param deserialized the deserialized object
105    */
106   protected void verifyDeserialized( @Nonnull T deserialized ) throws Exception {
107     assertEquals( deserialized, createObjectToSerialize() );
108   }
109 }