Permalink
Browse files

Fixed issue 152

User should be able to configure the mock to be serializable and have extra interfaces

--HG--
extra : convert_revision : svn%3Aaa2aecf3-ea3e-0410-9d70-716747e7c967/trunk%401768
  • Loading branch information...
mockitoguy committed Nov 21, 2009
1 parent b6790ee commit 77cb2037314dd024eb53ffe2e9e06304088a2d53
@@ -9,9 +9,6 @@
import org.mockito.internal.util.MockName;
import org.mockito.stubbing.Answer;
import java.util.Arrays;
import java.io.Serializable;
public class MockSettingsImpl implements MockSettings {
private static final long serialVersionUID = 4475297236197939568L;
@@ -20,9 +17,11 @@
private Object spiedInstance;
private Answer<Object> defaultAnswer;
private MockName mockName;
private boolean serializable;
public MockSettings serializable() {
return this.extraInterfaces(Serializable.class);
this.serializable = true;
return this;
}
public MockSettings extraInterfaces(Class<?>... extraInterfaces) {
@@ -74,9 +73,7 @@ public MockSettings defaultAnswer(Answer defaultAnswer) {
}
public boolean isSerializable() {
return
extraInterfaces != null
&& Arrays.asList(extraInterfaces).contains(Serializable.class);
return serializable;
}
public void initiateMockName(Class classToMock) {
@@ -0,0 +1,11 @@
package org.mockito.internal.util;
public class ArrayUtils {
public Class<?>[] concat(Class<?>[] interfaces, Class<?> clazz) {
int length = interfaces.length;
Class[] out = new Class[length +1];
System.arraycopy(interfaces, 0, out, 0, length);
out[length] = clazz;
return out;
}
}
@@ -16,6 +16,8 @@
import org.mockito.internal.creation.jmock.ClassImposterizer;
import org.mockito.internal.util.reflection.LenientCopyTool;
import java.io.Serializable;
@SuppressWarnings("unchecked")
public class MockUtil {
@@ -39,7 +41,14 @@ public MockUtil() {
MockHandler<T> mockHandler = new MockHandler<T>(settings);
MethodInterceptorFilter filter = new MethodInterceptorFilter(mockHandler, settings);
Class<?>[] interfaces = settings.getExtraInterfaces();
Class<?>[] ancillaryTypes = interfaces == null ? new Class<?>[0] : interfaces;
Class<?>[] ancillaryTypes;
if (settings.isSerializable()) {
ancillaryTypes = interfaces == null ? new Class<?>[] {Serializable.class} : new ArrayUtils().concat(interfaces, Serializable.class);
} else {
ancillaryTypes = interfaces == null ? new Class<?>[0] : interfaces;
}
Object spiedInstance = settings.getSpiedInstance();
T mock = ClassImposterizer.INSTANCE.imposterise(filter, classToMock, ancillaryTypes);
@@ -4,15 +4,13 @@
*/
package org.mockito.internal.creation;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.io.Serializable;
import org.junit.Test;
import org.mockito.exceptions.base.MockitoException;
import org.mockitoutil.TestBase;
import org.fest.assertions.Assertions;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
public class MockSettingsImplTest extends TestBase {
@@ -59,7 +57,7 @@ public void shouldSetMockToBeSerializable() throws Exception {
mockSettingsImpl.serializable();
//then
Assertions.assertThat(mockSettingsImpl.getExtraInterfaces()).contains(Serializable.class);
assertTrue(mockSettingsImpl.isSerializable());
}
@Test
@@ -0,0 +1,32 @@
package org.mockito.internal.util;
import org.fest.assertions.Assertions;
import org.junit.Test;
import org.mockitoutil.TestBase;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
public class ArrayUtilsTest extends TestBase {
ArrayUtils utils = new ArrayUtils();
@Test
public void shouldConcatenateItemToAnEmptyArray() throws Exception {
//when
Class<?>[] items = utils.concat(new Class[0], List.class);
//then
Assertions.assertThat(items).containsOnly(List.class);
}
@Test
public void shouldConcatenateItemToFullArray() throws Exception {
//when
Class<?>[] items = utils.concat(new Class[] {Serializable.class, Map.class}, List.class);
//then
Assertions.assertThat(items).containsOnly(Serializable.class, Map.class, List.class);
}
}
@@ -295,4 +295,15 @@ public void shouldSerializeAlreadySerializableClass() throws Exception {
// then
assertEquals("foo", mock.toString());
}
@Test
public void shouldBeSerializeAndHaveExtraInterfaces() throws Exception {
//when
IMethods mock = mock(IMethods.class, withSettings().serializable().extraInterfaces(List.class));
IMethods mockTwo = mock(IMethods.class, withSettings().extraInterfaces(List.class).serializable());
//then
serializeAndBack((List) mock);
serializeAndBack((List) mockTwo);
}
}

0 comments on commit 77cb203

Please sign in to comment.