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.apache.commons.io.IOUtils;
35  import org.junit.experimental.theories.*;
36  import org.junit.runner.*;
37  import org.mockito.internal.matchers.apachecommons.ReflectionEquals;
38  
39  import javax.annotation.Nonnull;
40  import java.io.ByteArrayInputStream;
41  import java.io.ByteArrayOutputStream;
42  import java.io.IOException;
43  import java.io.InputStream;
44  import java.net.URL;
45  
46  import static org.hamcrest.CoreMatchers.*;
47  import static org.junit.Assert.*;
48  
49  /**
50   * Abstract base class for serializer tests.
51   *
52   * @param <T> the type of domain object
53   */
54  @RunWith( Theories.class )
55  public abstract class AbstractSerializerTest2<T> {
56    @Theory
57    public void testSerializer( @Nonnull Entry<T> entry ) throws Exception {
58      Serializer<T> serializer = getSerializer();
59  
60      //Serialize
61      byte[] serialized = serialize( serializer, entry.getObject() );
62  
63      //Verify
64      verifySerialized( entry, serialized );
65  
66      verifyDeserialized( serializer.deserialize( new ByteArrayInputStream( serialized ) ), entry.getObject() );
67    }
68  
69    @Nonnull
70    protected byte[] serialize( @Nonnull Serializer<T> serializer, @Nonnull T objectToSerialize ) throws IOException {
71      ByteArrayOutputStream out = new ByteArrayOutputStream();
72      serializer.serialize( objectToSerialize, out );
73      return out.toByteArray();
74    }
75  
76    protected abstract void verifySerialized( @Nonnull Entry<T> entry, @Nonnull byte[] serialized ) throws Exception;
77  
78    /**
79     * Returns the serializer
80     *
81     * @return the serializer
82     */
83    @Nonnull
84    protected abstract Serializer<T> getSerializer() throws Exception;
85  
86    /**
87     * Verifies the deserialized object
88     *
89     * @param deserialized the deserialized object
90     * @param original     the original
91     */
92    protected void verifyDeserialized( @Nonnull T deserialized, @Nonnull T original ) {
93      assertEquals( original, deserialized );
94      assertThat( deserialized, is( new ReflectionEquals( original ) ) );
95    }
96  
97    @Nonnull
98    public static <T> Entry<? extends T> create( @Nonnull T object, @Nonnull byte[] expected ) {
99      return new Entry<T>( object, expected );
100   }
101 
102   @Nonnull
103   public static <T> Entry<? extends T> create( @Nonnull T object, @Nonnull URL expected ) {
104     try {
105       return new Entry<T>( object, IOUtils.toByteArray( expected.openStream() ) );
106     } catch ( IOException e ) {
107       throw new RuntimeException( e );
108     }
109   }
110 
111   @Nonnull
112   public static <T> Entry<? extends T> create( @Nonnull T object, @Nonnull InputStream expected ) {
113     try {
114       return new Entry<T>( object, IOUtils.toByteArray( expected ) );
115     } catch ( IOException e ) {
116       throw new RuntimeException( e );
117     }
118   }
119 }