How to use readObject method of org.easymock.internal.DefaultClassInstantiator class

Best Easymock code snippet using org.easymock.internal.DefaultClassInstantiator.readObject

Source:DefaultClassInstantiator.java Github

copy

Full Screen

...38 public Object newInstance(final Class<?> c) throws InstantiationException {3940 if (isSerializable(c)) {41 try {42 return readObject(getSerializedBytes(c));43 // ///CLOVER:OFF44 } catch (final IOException e) {45 throw new RuntimeException("Failed to instantiate " + c.getName() + "'s mock: ", e);46 } catch (final ClassNotFoundException e) {47 throw new RuntimeException("Failed to instantiate " + c.getName() + "'s mock: ", e);48 }49 // ///CLOVER:ON50 }5152 final Constructor<?> constructor = getConstructorToUse(c);53 final Object[] params = getArgsForTypes(constructor.getParameterTypes());54 try {55 return constructor.newInstance(params);56 // ///CLOVER:OFF57 } catch (final IllegalArgumentException e) {58 throw new RuntimeException("Failed to instantiate " + c.getName() + "'s mock: ", e);59 } catch (final IllegalAccessException e) {60 throw new RuntimeException("Failed to instantiate " + c.getName() + "'s mock: ", e);61 // ///CLOVER:ON62 } catch (final InvocationTargetException e) {63 throw new RuntimeException("Failed to instantiate " + c.getName() + "'s mock: ", e);64 }65 }6667 /**68 * Tells if the provided class is serializable69 * 70 * @param clazz71 * Class to check72 * @return If the class is serializable73 */74 private boolean isSerializable(final Class<?> clazz) {75 return Serializable.class.isAssignableFrom(clazz);76 }7778 /**79 * Return the constructor considered the best to use with this class.80 * Algorithm is: No args constructor and then first constructor defined in81 * the class82 * 83 * @param clazz84 * Class in which constructor is searched85 * @return Constructor to use86 */87 public Constructor<?> getConstructorToUse(final Class<?> clazz) {88 // First try to use the empty constructor89 try {90 return clazz.getConstructor(new Class[0]);91 } catch (final NoSuchMethodException e) {92 // If it fails just use the first one93 if (clazz.getConstructors().length == 0) {94 throw new IllegalArgumentException("No visible constructors in class " + clazz.getName());95 }96 return clazz.getConstructors()[0];97 }98 }99100 /**101 * Get some default instances of provided classes102 * 103 * @param methodTypes104 * Classes to instantiate105 * @return Instances of methodTypes in order106 * @throws InstantiationException Thrown if the class instantiation fails107 */108 public Object[] getArgsForTypes(final Class<?>[] methodTypes) throws InstantiationException {109 final Object[] methodArgs = new Object[methodTypes.length];110111 for (int i = 0; i < methodTypes.length; i++) {112113 if (methodTypes[i].isPrimitive()) {114 // Return a nice wrapped primitive type115 methodArgs[i] = RecordState.emptyReturnValueFor(methodTypes[i]);116 // ///CLOVER:OFF TODO: Remove when we manage to fix the ignored tests117 } else if (Modifier.isFinal(methodTypes[i].getModifiers())) {118 // Instantiate the class using the best constructor we can find119 // (because it's not120 // possible to mock a final class)121 methodArgs[i] = newInstance(methodTypes[i]);122 // ///CLOVER:ON123 } else {124 // For all classes and interfaces, just return a nice mock125 final Object mock = EasyMock.createNiceMock(methodTypes[i]);126 EasyMock.replay(mock);127 methodArgs[i] = mock;128 }129 }130 return methodArgs;131 }132133 private static byte[] getSerializedBytes(final Class<?> clazz) throws IOException {134 final ByteArrayOutputStream baos = new ByteArrayOutputStream();135 final DataOutputStream data = new DataOutputStream(baos);136 data.writeShort(ObjectStreamConstants.STREAM_MAGIC);137 data.writeShort(ObjectStreamConstants.STREAM_VERSION);138 data.writeByte(ObjectStreamConstants.TC_OBJECT);139 data.writeByte(ObjectStreamConstants.TC_CLASSDESC);140 data.writeUTF(clazz.getName());141142 final Long suid = getSerializableUID(clazz);143144 data.writeLong(suid.longValue());145146 data.writeByte(2); // classDescFlags (2 = Serializable)147 data.writeShort(0); // field count148 data.writeByte(ObjectStreamConstants.TC_ENDBLOCKDATA);149 data.writeByte(ObjectStreamConstants.TC_NULL);150 return baos.toByteArray();151 }152153 private static Long getSerializableUID(final Class<?> clazz) {154155 try {156 final Field f = clazz.getDeclaredField("serialVersionUID");157 final int mask = Modifier.STATIC | Modifier.FINAL;158 if ((f.getModifiers() & mask) == mask) {159 f.setAccessible(true);160 return Long.valueOf(f.getLong(null));161 }162 } catch (final NoSuchFieldException e) {163 // It's not there, compute it then164 } catch (final IllegalAccessException e) {165 // ///CLOVER:OFF166 throw new RuntimeException("Should have been able to get serialVersionUID since it's there");167 // ///CLOVER:ON168 }169 // ///CLOVER:OFF170 return callLongMethod(clazz,171 ClassInstantiatorFactory.is1_3Specifications() ? "computeSerialVersionUID"172 : "computeDefaultSUID");173 // ///CLOVER:ON174 }175176 private static Long callLongMethod(final Class<?> clazz, final String methodName) {177178 Method method;179 // ///CLOVER:OFF180 try {181 method = ObjectStreamClass.class.getDeclaredMethod(methodName, new Class[] { Class.class });182 } catch (final NoSuchMethodException e) {183 throw new InternalError("ObjectStreamClass." + methodName + " seems to have vanished");184 }185 final boolean accessible = method.isAccessible();186 method.setAccessible(true);187 Long suid;188 try {189 suid = (Long) method.invoke(null, new Object[] { clazz });190 } catch (final IllegalAccessException e) {191 throw new InternalError("ObjectStreamClass." + methodName + " should have been accessible");192 } catch (final InvocationTargetException e) {193 throw new InternalError("ObjectStreamClass." + methodName + " failled to be called: "194 + e.getMessage());195 }196 method.setAccessible(accessible);197 // ///CLOVER:ON198 return suid;199 }200201 private static Object readObject(final byte[] bytes) throws IOException, ClassNotFoundException {202 final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes));203 return in.readObject();204 }205206} ...

Full Screen

Full Screen

readObject

Using AI Code Generation

copy

Full Screen

1Object obj = new DefaultClassInstantiator().readObject(inputStream);2new DefaultClassInstantiator().writeObject(obj, outputStream);3Object obj = new DefaultClassInstantiator().readObject(inputStream);4new DefaultClassInstantiator().writeObject(obj, outputStream);5Object obj = new DefaultClassInstantiator().readObject(inputStream);6new DefaultClassInstantiator().writeObject(obj, outputStream);7Object obj = new DefaultClassInstantiator().readObject(inputStream);8new DefaultClassInstantiator().writeObject(obj, outputStream);9Object obj = new DefaultClassInstantiator().readObject(inputStream);10new DefaultClassInstantiator().writeObject(obj, outputStream);11Object obj = new DefaultClassInstantiator().readObject(inputStream);12new DefaultClassInstantiator().writeObject(obj, outputStream);13Object obj = new DefaultClassInstantiator().readObject(inputStream);14new DefaultClassInstantiator().writeObject(obj, outputStream);15Object obj = new DefaultClassInstantiator().readObject(inputStream);16new DefaultClassInstantiator().writeObject(obj,

Full Screen

Full Screen

readObject

Using AI Code Generation

copy

Full Screen

1public class EasyMockDemo {2 public static void main(String[] args) {3 IBook book = EasyMock.createMock(IBook.class);4 EasyMock.expect(book.getName()).andReturn("Effective Java");5 EasyMock.replay(book);6 System.out.println(book.getName());7 }8}

Full Screen

Full Screen

readObject

Using AI Code Generation

copy

Full Screen

1import org.easymock.internal.matchers.And;2import org.easymock.internal.matchers.Equals;3import org.easymock.internal.matchers.InstanceOf;4import org.easymock.internal.matchers.Or;5import org.easymock.internal.matchers.StartsWith;6import org.easymock.internal.matchers.EndsWith;7import org.easymock.internal.matchers.Not;8import java.io.ByteArrayInputStream;9import java.io.IOException;10import java.io.ObjectInputStream;11import java.io.ObjectStreamClass;12import java.io.Serializable;13import java.util.ArrayList;14import java.util.Arrays;15import java.util.List;16import java.util.Random;17public class EasyMockTest {18 public static void main(String[] args) throws Exception {19 And and = new And(Arrays.asList((Serializable) new Equals("a"), new StartsWith("b"), new EndsWith("c")));20 Or or = new Or(Arrays.asList((Serializable) new EndsWith("a"), new EndsWith("c")));21 Not not = new Not(new EndsWith("a"));22 InstanceOf instanceOf = new InstanceOf(String.class);23 List<String> list = new ArrayList<>();24 list.add("a");25 list.add("b");26 list.add("c");27 Random random = new Random(10);28 Equals equals = new Equals("a");29 And and2 = new And(Arrays.asList((Serializable) new Equals("a"), new StartsWith("b"), new EndsWith("c")));30 Or or2 = new Or(Arrays.asList((Serializable) new EndsWith("a"), new EndsWith("c")));

Full Screen

Full Screen

readObject

Using AI Code Generation

copy

Full Screen

1import org.easymock.internal.DefaultClassInstantiator;2import org.easymock.internal.ClassInstantiator;3import org.easymock.internal.ClassInstantiatorFactory;4import org.easymock.internal.MocksControl;5import org.easymock.internal.ObjectMethodsFilter;6import java.io.IOException;7import java.io.ObjectInputStream;8import java.io.Serializable;9import java.util.ArrayList;10import java.util.List;11public class TestClassInstantiator {12 public static void main(String[] args) {13 ClassInstantiatorFactory factory = new ClassInstantiatorFactory();14 ClassInstantiator instantiator = factory.newClassInstantiator();15 Object obj = instantiator.newInstance(ArrayList.class);16 System.out.println(obj.getClass().getName());17 }18}

Full Screen

Full Screen

readObject

Using AI Code Generation

copy

Full Screen

1public class MockObject {2 public static Object createMockObject(Class<?> clazz) {3 return new DefaultClassInstantiator().readObject(clazz);4 }5}6public class MockObjectTest {7 public void testCreateMockObject() {8 Object mockObject = MockObject.createMockObject(String.class);9 Assert.assertTrue(mockObject instanceof String);10 }11}12public class MockObjectTest2 {13 public void testCreateMockObject() {14 String mockObject = (String) MockObject.createMockObject(String.class);15 Assert.assertTrue(mockObject instanceof String);16 }17}18public class MockObjectTest3 {19 public void testCreateMockObject() {20 String mockObject = (String) MockObject.createMockObject(String.class);21 Assert.assertTrue(mockObject instanceof String);22 }23}24public class MockObjectTest4 {25 public void testCreateMockObject() {26 String mockObject = (String) MockObject.createMockObject(String.class);27 Assert.assertTrue(mockObject instanceof String);28 }29}30public class MockObjectTest5 {31 public void testCreateMockObject() {32 String mockObject = (String) MockObject.createMockObject(String.class);33 Assert.assertTrue(mockObject instanceof String);34 }35}36public class MockObjectTest6 {37 public void testCreateMockObject() {38 String mockObject = (String) MockObject.createMockObject(String.class);39 Assert.assertTrue(mockObject

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Easymock automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful