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  import java.util.ArrayList;
42  import java.util.List;
43  
44  import static org.junit.Assert.*;
45  
46  /**
47   * Abstract class for serializer tests
48   *
49   * @param <T> the type of the serialized object
50   * @deprecated use {@link AbstractSerializerTest2} instead
51   */
52  @Deprecated
53  public abstract class AbstractSerializerMultiTest<T> {
54    @Test
55    public void testSerializer() throws Exception {
56      Serializer<T> serializer = getSerializer();
57  
58      Iterable<? extends T> objectsToSerialize = createObjectsToSerialize();
59  
60      //Serialize
61      List<? extends byte[]> serialized = serialize( serializer, objectsToSerialize );
62  
63      //Verify
64      verifySerialized( serialized );
65  
66  
67      List<T> deserialized = new ArrayList<T>();
68      for ( byte[] currentSerialized : serialized ) {
69        deserialized.add( serializer.deserialize( new ByteArrayInputStream( currentSerialized ) ) );
70      }
71  
72      verifyDeserialized( deserialized );
73    }
74  
75    @Nonnull
76    private List<? extends byte[]> serialize( @Nonnull Serializer<T> serializer, @Nonnull Iterable<? extends T> objectsToSerialize ) throws IOException {
77      List<byte[]> serialized = new ArrayList<byte[]>();
78  
79      int index = 0;
80      for ( T objectToSerialize : objectsToSerialize ) {
81        try {
82          serialized.add( serialize( serializer, objectToSerialize ) );
83          index++;
84        } catch ( IOException e ) {
85          throw new IOException( "Serialization failed for (" + index + ") <" + objectsToSerialize + ">", e );
86        }
87      }
88      return serialized;
89    }
90  
91    @Nonnull
92    protected byte[] serialize( @Nonnull Serializer<T> serializer, @Nonnull T objectToSerialize ) throws IOException {
93      ByteArrayOutputStream out = new ByteArrayOutputStream();
94      serializer.serialize( objectToSerialize, out );
95      return out.toByteArray();
96    }
97  
98    /**
99     * Returns the serializer
100    *
101    * @return the serializer
102    */
103   @Nonnull
104   protected abstract Serializer<T> getSerializer() throws Exception;
105 
106   /**
107    * Verifies the serialized object
108    *
109    * @param serialized the serialized objects (within the same order)
110    * @throws SAXException
111    * @throws IOException
112    */
113   protected abstract void verifySerialized( @Nonnull List<? extends byte[]> serialized ) throws Exception;
114 
115   /**
116    * Creates the object to serialize
117    *
118    * @return the object to serialize
119    */
120   @Nonnull
121   protected abstract Iterable<? extends T> createObjectsToSerialize() throws Exception;
122 
123   /**
124    * Verifies the deserialized object.
125    * The default implementation simply calls equals
126    *
127    * @param deserialized the deserialized object
128    */
129   protected void verifyDeserialized( @Nonnull List<? extends T> deserialized ) throws Exception {
130     int index = 0;
131     for ( T currentExpected : createObjectsToSerialize() ) {
132       assertEquals( deserialized.get( index ), currentExpected );
133       index++;
134     }
135   }
136 }