View Javadoc

1   /*
2    * Copyright 2012-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 test.de.smartics.properties.spi.core.util;
17  
18  import static org.hamcrest.MatcherAssert.assertThat;
19  import static org.hamcrest.Matchers.is;
20  import static org.powermock.api.mockito.PowerMockito.spy;
21  import static org.powermock.api.mockito.PowerMockito.when;
22  
23  import java.io.ByteArrayInputStream;
24  import java.io.ByteArrayOutputStream;
25  import java.io.IOException;
26  import java.io.ObjectInputStream;
27  import java.io.ObjectOutputStream;
28  import java.lang.reflect.Method;
29  
30  import org.junit.Test;
31  import org.junit.experimental.categories.Category;
32  import org.junit.runner.RunWith;
33  import org.powermock.core.classloader.annotations.PrepareForTest;
34  import org.powermock.modules.junit4.PowerMockRunner;
35  
36  import de.smartics.properties.spi.core.util.SerializableMethod;
37  import de.smartics.testdoc.annotations.Uut;
38  import de.smartics.testdoc.categories.Technical;
39  
40  /**
41   * Tests {@link SerializableMethod}.
42   */
43  @Uut(type = SerializableMethod.class)
44  @RunWith(PowerMockRunner.class)
45  @PrepareForTest(SerializableMethod.class)
46  public class SerializableMethodDeserializeTest
47  {
48    // ********************************* Fields *********************************
49  
50    // --- constants ------------------------------------------------------------
51  
52    private static final Class<?>[] NO_PARAMETER = new Class<?>[0];
53  
54    private static final Class<?>[] ONE_PARAMETER =
55        new Class<?>[] { String.class };
56  
57    private static final Class<?>[] TWO_PARAMETERS =
58        new Class<?>[] { String.class, String.class };
59  
60    private static final Method METHOD;
61  
62    static
63    {
64      try
65      {
66        METHOD = String.class.getMethod("toString", NO_PARAMETER);
67      }
68      catch (final Exception e)
69      {
70        throw new IllegalStateException("Cannot find method for test.", e);
71      }
72    }
73  
74    // --- members --------------------------------------------------------------
75  
76    // ****************************** Inner Classes *****************************
77  
78    // ********************************* Methods ********************************
79  
80    // --- prepare --------------------------------------------------------------
81  
82    // --- helper ---------------------------------------------------------------
83  
84    private void runDeserialization(final Class<?>[] parameterTypes)
85      throws Exception
86    {
87      final SerializableMethod uut = createUut(parameterTypes);
88      final ObjectInputStream input = createInvalidOis(uut);
89  
90      input.readObject();
91    }
92  
93    private static SerializableMethod createUut(final Class<?>[] parameterTypes)
94    {
95      try
96      {
97        final Method spy = spy(METHOD);
98        when(spy.getName()).thenReturn("unknown");
99        when(spy.getParameterTypes()).thenReturn(parameterTypes);
100       assertThat(spy.getName(), is("unknown"));
101       final SerializableMethod uut = new SerializableMethod(spy);
102       return uut;
103     }
104     catch (final Throwable e) // NOPMD
105     {
106       throw new IllegalStateException("Cannot create test spy.", e);
107     }
108   }
109 
110   private ObjectInputStream createInvalidOis(final SerializableMethod uut)
111   {
112     try
113     {
114       final ByteArrayOutputStream output = new ByteArrayOutputStream(1024);
115       final ObjectOutputStream oos = new ObjectOutputStream(output);
116 
117       oos.writeObject(uut);
118 
119       final ObjectInputStream ois =
120           new ObjectInputStream(new ByteArrayInputStream(output.toByteArray()));
121       return ois;
122     }
123     catch (final Throwable e) // NOPMD
124     {
125       throw new IllegalStateException(
126           "Cannot create invalid object input stream.", e);
127     }
128   }
129 
130   // --- tests ----------------------------------------------------------------
131 
132   @Test(expected = IOException.class)
133   public void signalsProblemsIfSerializedFormCannotBeDeserialized()
134     throws Exception
135   {
136     try
137     {
138       runDeserialization(NO_PARAMETER);
139     }
140     catch (final IOException e)
141     {
142       assertThat(e.getMessage(),
143           is("Cannot recreate method java.lang.String.unknown()."));
144       throw e;
145     }
146   }
147 
148   @Test(expected = IOException.class)
149   @Category(Technical.class)
150   public void signalsProblemsIfSerializedFormCannotBeDeserializedWithOneParameter()
151     throws Exception
152   {
153     try
154     {
155       runDeserialization(ONE_PARAMETER);
156     }
157     catch (final IOException e)
158     {
159       assertThat(
160           e.getMessage(),
161           is("Cannot recreate method java.lang.String.unknown(java.lang.String)."));
162       throw e;
163     }
164   }
165 
166   @Test(expected = IOException.class)
167   @Category(Technical.class)
168   public void signalsProblemsIfSerializedFormCannotBeDeserializedWithTwoParameters()
169     throws Exception
170   {
171     try
172     {
173       runDeserialization(TWO_PARAMETERS);
174     }
175     catch (final IOException e)
176     {
177       assertThat(
178           e.getMessage(),
179           is("Cannot recreate method java.lang.String.unknown(java.lang.String, java.lang.String)."));
180       throw e;
181     }
182   }
183 }