BAEL-5915 Added different ways to create an object (#13539)
This commit is contained in:
parent
8a02df3f58
commit
8fd4ad46ea
@ -0,0 +1,18 @@
|
||||
package com.baeldung.objectcreation.objects;
|
||||
|
||||
public class ClonableRabbit implements Cloneable {
|
||||
|
||||
String name = "";
|
||||
|
||||
public ClonableRabbit(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Object clone() throws CloneNotSupportedException {
|
||||
return super.clone();
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package com.baeldung.objectcreation.objects;
|
||||
|
||||
public class Rabbit {
|
||||
|
||||
String name = "";
|
||||
|
||||
public Rabbit() {
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.baeldung.objectcreation.objects;
|
||||
|
||||
public enum RabbitType {
|
||||
PET,
|
||||
TAME,
|
||||
WILD
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.baeldung.objectcreation.objects;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class SerializableRabbit implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 2589844379773087465L;
|
||||
|
||||
String name = "";
|
||||
|
||||
public SerializableRabbit() {
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
package com.baeldung.objectcreation.utils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import com.baeldung.objectcreation.objects.ClonableRabbit;
|
||||
import com.baeldung.objectcreation.objects.Rabbit;
|
||||
import com.baeldung.objectcreation.objects.RabbitType;
|
||||
import com.baeldung.objectcreation.objects.SerializableRabbit;
|
||||
|
||||
public final class CreateRabbits {
|
||||
|
||||
public static Rabbit createRabbitUsingNewOperator() {
|
||||
Rabbit rabbit = new Rabbit();
|
||||
|
||||
return rabbit;
|
||||
}
|
||||
|
||||
public static Rabbit createRabbitUsingClassForName() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
|
||||
Rabbit rabbit = (Rabbit) Class.forName("com.baeldung.objectcreation.objects.Rabbit").newInstance();
|
||||
|
||||
return rabbit;
|
||||
}
|
||||
|
||||
public static Rabbit createRabbitUsingClassNewInstance() throws InstantiationException, IllegalAccessException {
|
||||
Rabbit rabbit = Rabbit.class.newInstance();
|
||||
|
||||
return rabbit;
|
||||
}
|
||||
|
||||
public static Rabbit createRabbitUsingConstructorNewInstance() throws InstantiationException, IllegalAccessException, IllegalArgumentException,
|
||||
InvocationTargetException, NoSuchMethodException, SecurityException {
|
||||
Rabbit rabbit = Rabbit.class.getConstructor().newInstance();
|
||||
|
||||
return rabbit;
|
||||
}
|
||||
|
||||
public static ClonableRabbit createRabbitUsingClone(ClonableRabbit originalRabbit) throws CloneNotSupportedException {
|
||||
ClonableRabbit clonedRabbit = (ClonableRabbit) originalRabbit.clone();
|
||||
|
||||
return clonedRabbit;
|
||||
}
|
||||
|
||||
public static SerializableRabbit createRabbitUsingDeserialization(File file) throws IOException, ClassNotFoundException {
|
||||
try (FileInputStream fis = new FileInputStream(file);
|
||||
ObjectInputStream ois = new ObjectInputStream(fis);) {
|
||||
return (SerializableRabbit) ois.readObject();
|
||||
}
|
||||
}
|
||||
|
||||
public static Rabbit createRabbitUsingSupplier() {
|
||||
Supplier<Rabbit> rabbitSupplier = Rabbit::new;
|
||||
Rabbit rabbit = rabbitSupplier.get();
|
||||
|
||||
return rabbit;
|
||||
}
|
||||
|
||||
public static Rabbit[] createRabbitArray() {
|
||||
Rabbit[] rabbitArray = new Rabbit[10];
|
||||
|
||||
return rabbitArray;
|
||||
}
|
||||
|
||||
public static RabbitType createRabbitTypeEnum() {
|
||||
return RabbitType.PET; //any RabbitType could be returned here, PET is just an example.
|
||||
}
|
||||
}
|
@ -0,0 +1,100 @@
|
||||
package com.baeldung.objectcreation;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.objectcreation.objects.ClonableRabbit;
|
||||
import com.baeldung.objectcreation.objects.Rabbit;
|
||||
import com.baeldung.objectcreation.objects.RabbitType;
|
||||
import com.baeldung.objectcreation.objects.SerializableRabbit;
|
||||
import com.baeldung.objectcreation.utils.CreateRabbits;
|
||||
|
||||
public class CreateRabbitsUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenUsingNewOperator_thenRabbitIsReturned() {
|
||||
assertTrue(CreateRabbits.createRabbitUsingNewOperator() instanceof Rabbit);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingClassForName_thenRabbitIsReturned() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
|
||||
assertTrue(CreateRabbits.createRabbitUsingClassForName() instanceof Rabbit);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingClassNewInstance_thenRabbitIsReturned() throws InstantiationException, IllegalAccessException {
|
||||
assertTrue(CreateRabbits.createRabbitUsingClassNewInstance() instanceof Rabbit);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingConstructorNewInstance_thenRabbitIsReturned() throws InstantiationException, IllegalAccessException, IllegalArgumentException,
|
||||
InvocationTargetException, NoSuchMethodException, SecurityException {
|
||||
assertTrue(CreateRabbits.createRabbitUsingConstructorNewInstance() instanceof Rabbit);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClonableRabbit_whenUsingCloneMethod_thenClonedRabbitIsReturned() throws CloneNotSupportedException {
|
||||
//given
|
||||
ClonableRabbit originalRabbit = new ClonableRabbit("Peter");
|
||||
|
||||
//when
|
||||
ClonableRabbit clonedRabbit = CreateRabbits.createRabbitUsingClone(originalRabbit);
|
||||
|
||||
//then
|
||||
assertTrue(clonedRabbit instanceof ClonableRabbit);
|
||||
assertNotEquals(originalRabbit, clonedRabbit);
|
||||
assertEquals("Peter", clonedRabbit.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSerializedRabbit_whenDeserializing_thenNewRabbitIsReturned() throws IOException, ClassNotFoundException {
|
||||
//given
|
||||
SerializableRabbit originalRabbit = new SerializableRabbit();
|
||||
originalRabbit.setName("Peter");
|
||||
|
||||
File resourcesFolder = new File("src/test/resources");
|
||||
resourcesFolder.mkdirs(); //creates the directory in case it doesn't exist
|
||||
|
||||
File file = new File(resourcesFolder, "rabbit.ser");
|
||||
|
||||
try (FileOutputStream fileOutputStream = new FileOutputStream(file);
|
||||
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);) {
|
||||
objectOutputStream.writeObject(originalRabbit);
|
||||
}
|
||||
|
||||
//when
|
||||
SerializableRabbit newRabbit = CreateRabbits.createRabbitUsingDeserialization(file);
|
||||
|
||||
//then
|
||||
assertTrue(newRabbit instanceof SerializableRabbit);
|
||||
assertNotEquals(originalRabbit, newRabbit);
|
||||
assertEquals("Peter", newRabbit.getName());
|
||||
|
||||
//clean up
|
||||
file.delete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingSupplier_thenRabbitIsReturned() {
|
||||
assertTrue(CreateRabbits.createRabbitUsingSupplier() instanceof Rabbit);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingArrays_thenRabbitArrayIsReturned() {
|
||||
assertTrue(CreateRabbits.createRabbitArray() instanceof Rabbit[]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingEnums_thenRabbitTypeIsReturned() {
|
||||
assertTrue(CreateRabbits.createRabbitTypeEnum() instanceof RabbitType);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user