View Javadoc

1   /*
2    * Copyright 2008-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.util.test.theories;
17  
18  import static org.hamcrest.CoreMatchers.equalTo;
19  import static org.hamcrest.CoreMatchers.is;
20  import static org.hamcrest.CoreMatchers.not;
21  import static org.junit.Assert.assertThat;
22  import static org.junit.Assume.assumeThat;
23  
24  import java.io.IOException;
25  
26  import org.junit.experimental.theories.Theories;
27  import org.junit.experimental.theories.Theory;
28  import org.junit.runner.RunWith;
29  
30  import de.smartics.util.test.io.SerialTestUtils;
31  
32  /* CHECKSTYLE:OFF */
33  /**
34   * Test theory on serializing serializable instances. If instances are defined
35   * as data points that are not serializable, the test leads to a test error.
36   *
37   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
38   * @version $Revision:591 $
39   */
40  @RunWith(Theories.class)
41  public abstract class SerializationTheory // NOPMD
42  /* CHECKSTYLE:ON */
43  {
44    // ********************************* Fields *********************************
45  
46    // --- constants ------------------------------------------------------------
47  
48    // --- members --------------------------------------------------------------
49  
50    // ****************************** Inner Classes *****************************
51  
52    // ********************************* Methods ********************************
53  
54    // --- prepare --------------------------------------------------------------
55  
56    // --- helper ---------------------------------------------------------------
57  
58    // CHECKSTYLE:OFF
59    /**
60     * Determines whether or not the theory should check the serialized and
61     * deserialized instance on equality with the original instance. If the unit
62     * under test (UUT) does not provide an implementation of
63     * {@link Object#equals(Object)}, this method must return <code>false</code>.
64     * <p>
65     * Override this method by your subclass.
66     * </p>
67     *
68     * @return <code>false</code> per default.
69     */
70    protected boolean checkThatResultEquals()
71    {
72      return false;
73    }
74  
75    // CHECKSTYLE:ON
76  
77    // --- tests ----------------------------------------------------------------
78  
79    /**
80     * Checks that the unit under test (UUT) is serializable.
81     *
82     * @param uut the unit under test. It is required for the instance to
83     *          implement the equals method, if {@link #checkThatResultEquals()}
84     *          returns <code>true</code> If that method returns
85     *          <code>false</code> this test only checks that the serialization
86     *          process throws no exception.
87     * @throws ClassNotFoundException if the class cannot be found during
88     *           deserialization.
89     * @throws IOException on any problem writing the instance to or reading it
90     *           from the stream.
91     * @see java.io.Serializable
92     * @see java.io.Externizable
93     */
94    @Theory
95    public final void isSerializable(final Object uut) throws IOException,
96      ClassNotFoundException
97    {
98      assumeThat(uut, is(not(equalTo(null))));
99  
100     final Object serializedAndDerserializedInstance =
101         SerialTestUtils.runSerialization(uut);
102     if (checkThatResultEquals())
103     {
104       assertThat(uut.equals(serializedAndDerserializedInstance), is(true));
105     }
106   }
107 }