JAVA-8279 Split or move core java module
This commit is contained in:
@@ -3,14 +3,7 @@
|
||||
### Relevant Articles:
|
||||
- [Getting Started with Java Properties](http://www.baeldung.com/java-properties)
|
||||
- [Java Money and the Currency API](http://www.baeldung.com/java-money-and-currency)
|
||||
- [Introduction to Java Serialization](http://www.baeldung.com/java-serialization)
|
||||
- [Guide to UUID in Java](http://www.baeldung.com/java-uuid)
|
||||
- [Compiling Java *.class Files with javac](http://www.baeldung.com/javac)
|
||||
- [Introduction to Javadoc](http://www.baeldung.com/javadoc)
|
||||
- [Guide to the Externalizable Interface in Java](http://www.baeldung.com/java-externalizable)
|
||||
- [What is the serialVersionUID?](http://www.baeldung.com/java-serial-version-uid)
|
||||
- [A Guide to the ResourceBundle](http://www.baeldung.com/java-resourcebundle)
|
||||
- [Merging java.util.Properties Objects](https://www.baeldung.com/java-merging-properties)
|
||||
- [Deserialization Vulnerabilities in Java](https://www.baeldung.com/java-deserialization-vulnerabilities)
|
||||
- [Generating Alphanumeric UUID String in Java](https://www.baeldung.com/java-generate-alphanumeric-uuid)
|
||||
- [Serialization Validation in Java](https://www.baeldung.com/java-validate-serializable)
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
package com.baeldung.deserialization;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class AppleProduct implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1234567L; // user-defined (i.e. not default or generated)
|
||||
// private static final long serialVersionUID = 7654321L; // user-defined (i.e. not default or generated)
|
||||
|
||||
public String headphonePort;
|
||||
public String thunderboltPort;
|
||||
public String lightningPort;
|
||||
|
||||
public String getHeadphonePort() {
|
||||
return headphonePort;
|
||||
}
|
||||
|
||||
public String getThunderboltPort() {
|
||||
return thunderboltPort;
|
||||
}
|
||||
|
||||
public static long getSerialVersionUID() {
|
||||
return serialVersionUID;
|
||||
}
|
||||
|
||||
public String getLightningPort() {
|
||||
return lightningPort;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package com.baeldung.deserialization;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Serializable;
|
||||
|
||||
public class DefaultSerial implements Serializable {
|
||||
|
||||
private String name;
|
||||
|
||||
public static void main(String[] args) throws IOException, ClassNotFoundException {
|
||||
String digest = "rO0ABXNyACpjb20uYmFlbGR1bmcuZGVzZXJpY"
|
||||
+ "WxpemF0aW9uLkRlZmF1bHRTZXJpYWx9iVz3Lz/mdAIAAHhw";
|
||||
DefaultSerial instance = (DefaultSerial) DeserializationUtility.deSerializeObjectFromString(digest);
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
package com.baeldung.deserialization;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.util.Base64;
|
||||
|
||||
public class DeserializationUtility {
|
||||
|
||||
public static void main(String[] args) throws ClassNotFoundException, IOException {
|
||||
|
||||
String serializedObj = "rO0ABXNyACljb20uYmFlbGR1bmcuZGVzZXJpYWxpemF0aW9uLkFwcGxlUHJvZHVjdAAAAAAAEtaHAgADTAANaGVhZHBob25lUG9ydHQAEkxqYXZhL2xhbmcvU3RyaW5nO0wADWxpZ2h0bmluZ1BvcnRxAH4AAUwAD3RodW5kZXJib2x0UG9ydHEAfgABeHB0ABFoZWFkcGhvbmVQb3J0MjAyMHQAEWxpZ2h0bmluZ1BvcnQyMDIwdAATdGh1bmRlcmJvbHRQb3J0MjAyMA==";
|
||||
System.out.println("Deserializing AppleProduct...");
|
||||
AppleProduct deserializedObj = (AppleProduct) deSerializeObjectFromString(serializedObj);
|
||||
System.out.println("Headphone port of AppleProduct:" + deserializedObj.getHeadphonePort());
|
||||
System.out.println("Thunderbolt port of AppleProduct:" + deserializedObj.getThunderboltPort());
|
||||
System.out.println("LightningPort port of AppleProduct:" + deserializedObj.getLightningPort());
|
||||
}
|
||||
|
||||
public static Object deSerializeObjectFromString(String s) throws IOException, ClassNotFoundException {
|
||||
byte[] data = Base64.getDecoder().decode(s);
|
||||
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data));
|
||||
Object o = ois.readObject();
|
||||
ois.close();
|
||||
return o;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
package com.baeldung.deserialization;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.Serializable;
|
||||
import java.util.Base64;
|
||||
|
||||
public class SerializationUtility {
|
||||
|
||||
public static void main(String[] args) throws ClassNotFoundException, IOException {
|
||||
|
||||
AppleProduct macBook = new AppleProduct();
|
||||
macBook.headphonePort = "headphonePort2020";
|
||||
macBook.thunderboltPort = "thunderboltPort2020";
|
||||
macBook.lightningPort = "lightningPort2020";
|
||||
|
||||
String serializedObj = serializeObjectToString(macBook);
|
||||
System.out.println("Serialized AppleProduct object to string:");
|
||||
System.out.println(serializedObj);
|
||||
}
|
||||
|
||||
public static String serializeObjectToString(Serializable o) throws IOException {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
ObjectOutputStream oos = new ObjectOutputStream(baos);
|
||||
oos.writeObject(o);
|
||||
oos.close();
|
||||
return Base64.getEncoder().encodeToString(baos.toByteArray());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package com.baeldung.externalizable;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class Community implements Serializable {
|
||||
|
||||
private int id;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Community{" +
|
||||
"id=" + id +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
package com.baeldung.externalizable;
|
||||
|
||||
import java.io.Externalizable;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInput;
|
||||
import java.io.ObjectOutput;
|
||||
|
||||
public class Country implements Externalizable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private String name;
|
||||
private String capital;
|
||||
private int code;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getCapital() {
|
||||
return capital;
|
||||
}
|
||||
|
||||
public void setCapital(String capital) {
|
||||
this.capital = capital;
|
||||
}
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(int code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeExternal(ObjectOutput out) throws IOException {
|
||||
out.writeUTF(name);
|
||||
out.writeUTF(capital);
|
||||
out.writeInt(code);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
|
||||
this.name = in.readUTF();
|
||||
this.capital = in.readUTF();
|
||||
this.code = in.readInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Country{" +
|
||||
"name='" + name + '\'' +
|
||||
", capital='" + capital + '\'' +
|
||||
", code=" + code +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
package com.baeldung.externalizable;
|
||||
|
||||
import java.io.Externalizable;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInput;
|
||||
import java.io.ObjectOutput;
|
||||
|
||||
public class Region extends Country implements Externalizable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private String climate;
|
||||
private Double population;
|
||||
private Community community;
|
||||
|
||||
public String getClimate() {
|
||||
return climate;
|
||||
}
|
||||
|
||||
public void setClimate(String climate) {
|
||||
this.climate = climate;
|
||||
}
|
||||
|
||||
public Double getPopulation() {
|
||||
return population;
|
||||
}
|
||||
|
||||
public void setPopulation(Double population) {
|
||||
this.population = population;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeExternal(ObjectOutput out) throws IOException {
|
||||
super.writeExternal(out);
|
||||
out.writeUTF(climate);
|
||||
community = new Community();
|
||||
community.setId(5);
|
||||
out.writeObject(community);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
|
||||
super.readExternal(in);
|
||||
this.climate = in.readUTF();
|
||||
community = (Community) in.readObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Region = {" +
|
||||
"country='" + super.toString() + '\'' +
|
||||
"community='" + community.toString() + '\'' +
|
||||
"climate='" + climate + '\'' +
|
||||
", population=" + population +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package com.baeldung.serialization;
|
||||
|
||||
public class Address {
|
||||
|
||||
private int houseNumber;
|
||||
|
||||
public int getHouseNumber() {
|
||||
return houseNumber;
|
||||
}
|
||||
|
||||
public void setHouseNumber(int houseNumber) {
|
||||
this.houseNumber = houseNumber;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
package com.baeldung.serialization;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Employee implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private transient Address address; // not an serializable object
|
||||
private Person person;
|
||||
|
||||
public Address getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(Address address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public Person getPerson() {
|
||||
return person;
|
||||
}
|
||||
|
||||
public void setPerson(Person person) {
|
||||
this.person = person;
|
||||
}
|
||||
|
||||
private void writeObject(ObjectOutputStream oos) throws IOException {
|
||||
oos.defaultWriteObject();
|
||||
oos.writeObject(address.getHouseNumber());
|
||||
}
|
||||
|
||||
private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
|
||||
ois.defaultReadObject();
|
||||
Integer houseNumber = (Integer) ois.readObject();
|
||||
Address a = new Address();
|
||||
a.setHouseNumber(houseNumber);
|
||||
this.setAddress(a);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package com.baeldung.serialization;
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Person implements Serializable {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private int age;
|
||||
private String name;
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
package com.baeldung.util;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
|
||||
public class MySerializationUtils {
|
||||
|
||||
public static <T extends Serializable> byte[] serialize(T obj) throws IOException {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
ObjectOutputStream oos = new ObjectOutputStream(baos);
|
||||
oos.writeObject(obj);
|
||||
oos.close();
|
||||
return baos.toByteArray();
|
||||
}
|
||||
|
||||
public static <T extends Serializable> T deserialize(byte[] b, Class<T> cl) throws IOException, ClassNotFoundException {
|
||||
ByteArrayInputStream bais = new ByteArrayInputStream(b);
|
||||
ObjectInputStream ois = new ObjectInputStream(bais);
|
||||
Object o = ois.readObject();
|
||||
return cl.cast(o);
|
||||
}
|
||||
|
||||
public static boolean isSerializable(Class<?> it) {
|
||||
boolean serializable = it.isPrimitive() || it.isInterface() || Serializable.class.isAssignableFrom(it);
|
||||
if (!serializable) {
|
||||
return serializable;
|
||||
}
|
||||
Field[] declaredFields = it.getDeclaredFields();
|
||||
for (Field field : declaredFields) {
|
||||
if (Modifier.isVolatile(field.getModifiers()) || Modifier.isTransient(field.getModifiers()) || Modifier.isStatic(field.getModifiers())) {
|
||||
continue;
|
||||
}
|
||||
Class<?> fieldType = field.getType();
|
||||
return isSerializable(fieldType);
|
||||
}
|
||||
return serializable;
|
||||
}
|
||||
}
|
||||
@@ -1,202 +0,0 @@
|
||||
package com.baeldung.uuid;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.time.Duration;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Arrays;
|
||||
import java.util.Random;
|
||||
import java.util.UUID;
|
||||
|
||||
public class UUIDGenerator {
|
||||
|
||||
private static final char[] hexArray = "0123456789ABCDEF".toCharArray();
|
||||
|
||||
/**
|
||||
* Type 1 UUID Generation
|
||||
*/
|
||||
public static UUID generateType1UUID() {
|
||||
|
||||
long most64SigBits = get64MostSignificantBitsForVersion1();
|
||||
long least64SigBits = get64LeastSignificantBitsForVersion1();
|
||||
|
||||
return new UUID(most64SigBits, least64SigBits);
|
||||
}
|
||||
|
||||
private static long get64LeastSignificantBitsForVersion1() {
|
||||
Random random = new Random();
|
||||
long random63BitLong = random.nextLong() & 0x3FFFFFFFFFFFFFFFL;
|
||||
long variant3BitFlag = 0x8000000000000000L;
|
||||
return random63BitLong + variant3BitFlag;
|
||||
}
|
||||
|
||||
private static long get64MostSignificantBitsForVersion1() {
|
||||
LocalDateTime start = LocalDateTime.of(1582, 10, 15, 0, 0, 0);
|
||||
Duration duration = Duration.between(start, LocalDateTime.now());
|
||||
long seconds = duration.getSeconds();
|
||||
long nanos = duration.getNano();
|
||||
long timeForUuidIn100Nanos = seconds * 10000000 + nanos * 100;
|
||||
long least12SignificatBitOfTime = (timeForUuidIn100Nanos & 0x000000000000FFFFL) >> 4;
|
||||
long version = 1 << 12;
|
||||
return (timeForUuidIn100Nanos & 0xFFFFFFFFFFFF0000L) + version + least12SignificatBitOfTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Type 3 UUID Generation
|
||||
*
|
||||
* @throws UnsupportedEncodingException
|
||||
*/
|
||||
public static UUID generateType3UUID(String namespace, String name) throws UnsupportedEncodingException {
|
||||
|
||||
byte[] nameSpaceBytes = bytesFromUUID(namespace);
|
||||
byte[] nameBytes = name.getBytes("UTF-8");
|
||||
byte[] result = joinBytes(nameSpaceBytes, nameBytes);
|
||||
|
||||
return UUID.nameUUIDFromBytes(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Type 4 UUID Generation
|
||||
*/
|
||||
public static UUID generateType4UUID() {
|
||||
UUID uuid = UUID.randomUUID();
|
||||
return uuid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Type 5 UUID Generation
|
||||
*
|
||||
* @throws UnsupportedEncodingException
|
||||
*/
|
||||
public static UUID generateType5UUID(String namespace, String name) throws UnsupportedEncodingException {
|
||||
|
||||
byte[] nameSpaceBytes = bytesFromUUID(namespace);
|
||||
byte[] nameBytes = name.getBytes("UTF-8");
|
||||
byte[] result = joinBytes(nameSpaceBytes, nameBytes);
|
||||
|
||||
return type5UUIDFromBytes(result);
|
||||
}
|
||||
|
||||
public static UUID type5UUIDFromBytes(byte[] name) {
|
||||
MessageDigest md;
|
||||
try {
|
||||
md = MessageDigest.getInstance("SHA-1");
|
||||
} catch (NoSuchAlgorithmException nsae) {
|
||||
throw new InternalError("SHA-1 not supported", nsae);
|
||||
}
|
||||
byte[] bytes = Arrays.copyOfRange(md.digest(name), 0, 16);
|
||||
bytes[6] &= 0x0f; /* clear version */
|
||||
bytes[6] |= 0x50; /* set to version 5 */
|
||||
bytes[8] &= 0x3f; /* clear variant */
|
||||
bytes[8] |= 0x80; /* set to IETF variant */
|
||||
return constructType5UUID(bytes);
|
||||
}
|
||||
|
||||
private static UUID constructType5UUID(byte[] data) {
|
||||
long msb = 0;
|
||||
long lsb = 0;
|
||||
assert data.length == 16 : "data must be 16 bytes in length";
|
||||
|
||||
for (int i = 0; i < 8; i++)
|
||||
msb = (msb << 8) | (data[i] & 0xff);
|
||||
|
||||
for (int i = 8; i < 16; i++)
|
||||
lsb = (lsb << 8) | (data[i] & 0xff);
|
||||
return new UUID(msb, lsb);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unique Keys Generation Using Message Digest and Type 4 UUID
|
||||
*
|
||||
* @throws NoSuchAlgorithmException
|
||||
* @throws UnsupportedEncodingException
|
||||
*/
|
||||
public static String generateUniqueKeysWithUUIDAndMessageDigest() throws NoSuchAlgorithmException, UnsupportedEncodingException {
|
||||
MessageDigest salt = MessageDigest.getInstance("SHA-256");
|
||||
salt.update(UUID.randomUUID()
|
||||
.toString()
|
||||
.getBytes("UTF-8"));
|
||||
String digest = bytesToHex(salt.digest());
|
||||
return digest;
|
||||
}
|
||||
|
||||
private static String bytesToHex(byte[] bytes) {
|
||||
char[] hexChars = new char[bytes.length * 2];
|
||||
for (int j = 0; j < bytes.length; j++) {
|
||||
int v = bytes[j] & 0xFF;
|
||||
hexChars[j * 2] = hexArray[v >>> 4];
|
||||
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
|
||||
}
|
||||
return new String(hexChars);
|
||||
}
|
||||
|
||||
private static byte[] bytesFromUUID(String uuidHexString) {
|
||||
String normalizedUUIDHexString = uuidHexString.replace("-", "");
|
||||
|
||||
assert normalizedUUIDHexString.length() == 32;
|
||||
|
||||
byte[] bytes = new byte[16];
|
||||
for (int i = 0; i < 16; i++) {
|
||||
byte b = hexToByte(normalizedUUIDHexString.substring(i * 2, i * 2 + 2));
|
||||
bytes[i] = b;
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
public static byte hexToByte(String hexString) {
|
||||
int firstDigit = Character.digit(hexString.charAt(0), 16);
|
||||
int secondDigit = Character.digit(hexString.charAt(1), 16);
|
||||
return (byte) ((firstDigit << 4) + secondDigit);
|
||||
}
|
||||
|
||||
public static byte[] joinBytes(byte[] byteArray1, byte[] byteArray2) {
|
||||
int finalLength = byteArray1.length + byteArray2.length;
|
||||
byte[] result = new byte[finalLength];
|
||||
|
||||
for (int i = 0; i < byteArray1.length; i++) {
|
||||
result[i] = byteArray1[i];
|
||||
}
|
||||
|
||||
for (int i = 0; i < byteArray2.length; i++) {
|
||||
result[byteArray1.length + i] = byteArray2[i];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static UUID generateType5UUID(String name) {
|
||||
|
||||
try {
|
||||
|
||||
byte[] bytes = name.getBytes(StandardCharsets.UTF_8);
|
||||
MessageDigest md = MessageDigest.getInstance("SHA-1");
|
||||
|
||||
byte[] hash = md.digest(bytes);
|
||||
|
||||
long msb = getLeastAndMostSignificantBitsVersion5(hash, 0);
|
||||
long lsb = getLeastAndMostSignificantBitsVersion5(hash, 8);
|
||||
// Set the version field
|
||||
msb &= ~(0xfL << 12);
|
||||
msb |= ((long) 5) << 12;
|
||||
// Set the variant field to 2
|
||||
lsb &= ~(0x3L << 62);
|
||||
lsb |= 2L << 62;
|
||||
return new UUID(msb, lsb);
|
||||
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static long getLeastAndMostSignificantBitsVersion5(final byte[] src, final int offset) {
|
||||
long ans = 0;
|
||||
for (int i = offset + 7; i >= offset; i -= 1) {
|
||||
ans <<= 8;
|
||||
ans |= src[i] & 0xffL;
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
package com.baeldung.deserialization;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InvalidClassException;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
public class DeserializationUnitTest {
|
||||
|
||||
private static final String serializedObj = "rO0ABXNyACljb20uYmFlbGR1bmcuZGVzZXJpYWxpemF0aW9uLkFwcGxlUHJvZHVjdAAAAAAAdMuxAgADTAANaGVhZHBob25lUG9ydHQAEkxqYXZhL2xhbmcvU3RyaW5nO0wADWxpZ2h0bmluZ1BvcnRxAH4AAUwAD3RodW5kZXJib2x0UG9ydHEAfgABeHB0ABFoZWFkcGhvbmVQb3J0MjAyMHQAEWxpZ2h0bmluZ1BvcnQyMDIwdAATdGh1bmRlcmJvbHRQb3J0MjAyMA";
|
||||
|
||||
private static long userDefinedSerialVersionUID = 1234567L;
|
||||
|
||||
/**
|
||||
* Tests the deserialization of the original "AppleProduct" (no exceptions are thrown)
|
||||
* @throws ClassNotFoundException
|
||||
* @throws IOException
|
||||
*/
|
||||
@Test
|
||||
public void testDeserializeObj_compatible() throws IOException, ClassNotFoundException {
|
||||
|
||||
assertEquals(userDefinedSerialVersionUID, AppleProduct.getSerialVersionUID());
|
||||
|
||||
AppleProduct macBook = new AppleProduct();
|
||||
macBook.headphonePort = "headphonePort2020";
|
||||
macBook.thunderboltPort = "thunderboltPort2020";
|
||||
macBook.lightningPort = "lightningPort2020";
|
||||
|
||||
// serializes the "AppleProduct" object
|
||||
String serializedProduct = SerializationUtility.serializeObjectToString(macBook);
|
||||
|
||||
// deserializes the "AppleProduct" object
|
||||
AppleProduct deserializedProduct = (AppleProduct) DeserializationUtility.deSerializeObjectFromString(serializedProduct);
|
||||
|
||||
assertTrue(deserializedProduct.headphonePort.equalsIgnoreCase(macBook.headphonePort));
|
||||
assertTrue(deserializedProduct.thunderboltPort.equalsIgnoreCase(macBook.thunderboltPort));
|
||||
assertTrue(deserializedProduct.lightningPort.equalsIgnoreCase(macBook.lightningPort));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the deserialization of the modified (non-compatible) "AppleProduct".
|
||||
* The test should result in an InvalidClassException being thrown.
|
||||
*
|
||||
* Note: to run this test:
|
||||
* 1. Modify the value of the serialVersionUID identifier in AppleProduct.java
|
||||
* 2. Remove the @Ignore annotation
|
||||
* 3. Run the test individually (do not run the entire set of tests)
|
||||
* 4. Revert the changes made in 1 & 2 (so that you're able to re-run the tests successfully)
|
||||
*
|
||||
* @throws ClassNotFoundException
|
||||
* @throws IOException
|
||||
*/
|
||||
@Ignore
|
||||
@Test(expected = InvalidClassException.class)
|
||||
public void testDeserializeObj_incompatible() throws ClassNotFoundException, IOException {
|
||||
|
||||
assertNotEquals(userDefinedSerialVersionUID, AppleProduct.getSerialVersionUID());
|
||||
// attempts to deserialize the "AppleProduct" object
|
||||
DeserializationUtility.deSerializeObjectFromString(serializedObj);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
package com.baeldung.externalizable;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class ExternalizableUnitTest {
|
||||
|
||||
private final static String OUTPUT_FILE = "externalizable.txt";
|
||||
|
||||
@Test
|
||||
public void whenSerializing_thenUseExternalizable() throws IOException, ClassNotFoundException {
|
||||
|
||||
Country c = new Country();
|
||||
c.setCapital("Yerevan");
|
||||
c.setCode(374);
|
||||
c.setName("Armenia");
|
||||
|
||||
FileOutputStream fileOutputStream = new FileOutputStream(OUTPUT_FILE);
|
||||
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
|
||||
c.writeExternal(objectOutputStream);
|
||||
|
||||
objectOutputStream.flush();
|
||||
objectOutputStream.close();
|
||||
fileOutputStream.close();
|
||||
|
||||
FileInputStream fileInputStream = new FileInputStream(OUTPUT_FILE);
|
||||
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
|
||||
|
||||
Country c2 = new Country();
|
||||
c2.readExternal(objectInputStream);
|
||||
|
||||
objectInputStream.close();
|
||||
fileInputStream.close();
|
||||
|
||||
assertTrue(c2.getCode() == c.getCode());
|
||||
assertTrue(c2.getName().equals(c.getName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInheritanceSerialization_then_UseExternalizable() throws IOException, ClassNotFoundException {
|
||||
|
||||
Region r = new Region();
|
||||
r.setCapital("Yerevan");
|
||||
r.setCode(374);
|
||||
r.setName("Armenia");
|
||||
r.setClimate("Mediterranean");
|
||||
r.setPopulation(120.000);
|
||||
|
||||
FileOutputStream fileOutputStream = new FileOutputStream(OUTPUT_FILE);
|
||||
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
|
||||
r.writeExternal(objectOutputStream);
|
||||
|
||||
objectOutputStream.flush();
|
||||
objectOutputStream.close();
|
||||
fileOutputStream.close();
|
||||
|
||||
FileInputStream fileInputStream = new FileInputStream(OUTPUT_FILE);
|
||||
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
|
||||
|
||||
Region r2 = new Region();
|
||||
r2.readExternal(objectInputStream);
|
||||
|
||||
objectInputStream.close();
|
||||
fileInputStream.close();
|
||||
|
||||
assertTrue(r2.getPopulation() == null);
|
||||
}
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
package com.baeldung.serialization;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class PersonUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenSerializingAndDeserializing_ThenObjectIsTheSame() throws IOException, ClassNotFoundException {
|
||||
Person p = new Person();
|
||||
p.setAge(20);
|
||||
p.setName("Joe");
|
||||
|
||||
FileOutputStream fileOutputStream = new FileOutputStream("yofile.txt");
|
||||
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
|
||||
objectOutputStream.writeObject(p);
|
||||
objectOutputStream.flush();
|
||||
objectOutputStream.close();
|
||||
|
||||
FileInputStream fileInputStream = new FileInputStream("yofile.txt");
|
||||
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
|
||||
Person p2 = (Person) objectInputStream.readObject();
|
||||
objectInputStream.close();
|
||||
|
||||
assertTrue(p2.getAge() == p.getAge());
|
||||
assertTrue(p2.getName().equals(p.getName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCustomSerializingAndDeserializing_ThenObjectIsTheSame() throws IOException, ClassNotFoundException {
|
||||
Person p = new Person();
|
||||
p.setAge(20);
|
||||
p.setName("Joe");
|
||||
|
||||
Address a = new Address();
|
||||
a.setHouseNumber(1);
|
||||
|
||||
Employee e = new Employee();
|
||||
e.setPerson(p);
|
||||
e.setAddress(a);
|
||||
|
||||
FileOutputStream fileOutputStream = new FileOutputStream("yofile2.txt");
|
||||
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
|
||||
objectOutputStream.writeObject(e);
|
||||
objectOutputStream.flush();
|
||||
objectOutputStream.close();
|
||||
|
||||
FileInputStream fileInputStream = new FileInputStream("yofile2.txt");
|
||||
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
|
||||
Employee e2 = (Employee) objectInputStream.readObject();
|
||||
objectInputStream.close();
|
||||
|
||||
assertTrue(e2.getPerson().getAge() == e.getPerson().getAge());
|
||||
assertTrue(e2.getAddress().getHouseNumber() == (e.getAddress().getHouseNumber()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,111 +0,0 @@
|
||||
package com.baeldung.serialization;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.NotSerializableException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.apache.commons.lang3.SerializationUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.util.MySerializationUtils;
|
||||
|
||||
public class SerializationUnitTest {
|
||||
|
||||
@Test(expected = NotSerializableException.class)
|
||||
public void whenSerializing_ThenThrowsError() throws IOException {
|
||||
Address address = new Address();
|
||||
address.setHouseNumber(10);
|
||||
FileOutputStream fileOutputStream = new FileOutputStream("yofile.txt");
|
||||
try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream)) {
|
||||
objectOutputStream.writeObject(address);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSerializingAndDeserializing_ThenObjectIsTheSame() throws IOException, ClassNotFoundException {
|
||||
Person p = new Person();
|
||||
p.setAge(20);
|
||||
p.setName("Joe");
|
||||
|
||||
FileOutputStream fileOutputStream = new FileOutputStream("yofile.txt");
|
||||
try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream)) {
|
||||
objectOutputStream.writeObject(p);
|
||||
}
|
||||
|
||||
FileInputStream fileInputStream = new FileInputStream("yofile.txt");
|
||||
try (ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream)) {
|
||||
Person p2 = (Person) objectInputStream.readObject();
|
||||
assertEquals(p2.getAge(), p.getAge());
|
||||
assertEquals(p2.getName(), p.getName());
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = ClassCastException.class)
|
||||
public void whenSerializingUsingApacheCommons_ThenThrowsError() {
|
||||
Address address = new Address();
|
||||
address.setHouseNumber(10);
|
||||
SerializationUtils.serialize((Serializable) address);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSerializingAndDeserializingUsingApacheCommons_ThenObjectIsTheSame() {
|
||||
Person p = new Person();
|
||||
p.setAge(20);
|
||||
p.setName("Joe");
|
||||
byte[] serialize = SerializationUtils.serialize(p);
|
||||
Person p2 = (Person) SerializationUtils.deserialize(serialize);
|
||||
assertEquals(p2.getAge(), p.getAge());
|
||||
assertEquals(p2.getName(), p.getName());
|
||||
}
|
||||
|
||||
@Test(expected = ClassCastException.class)
|
||||
public void whenSerializingUsingSpringSerializationUtils_ThenThrowsError() {
|
||||
Address address = new Address();
|
||||
address.setHouseNumber(10);
|
||||
org.springframework.util.SerializationUtils.serialize((Serializable) address);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSerializingAndDeserializingUsingSpringSerializationUtils_ThenObjectIsTheSame() {
|
||||
Person p = new Person();
|
||||
p.setAge(20);
|
||||
p.setName("Joe");
|
||||
byte[] serialize = org.springframework.util.SerializationUtils.serialize(p);
|
||||
Person p2 = (Person) org.springframework.util.SerializationUtils.deserialize(serialize);
|
||||
assertEquals(p2.getAge(), p.getAge());
|
||||
assertEquals(p2.getName(), p.getName());
|
||||
}
|
||||
|
||||
@Test(expected = ClassCastException.class)
|
||||
public void whenSerializingUsingCustomSerializationUtils_ThenThrowsError() throws IOException {
|
||||
Address address = new Address();
|
||||
address.setHouseNumber(10);
|
||||
MySerializationUtils.serialize((Serializable) address);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSerializingAndDeserializingUsingCustomSerializationUtils_ThenObjectIsTheSame() throws IOException, ClassNotFoundException {
|
||||
Person p = new Person();
|
||||
p.setAge(20);
|
||||
p.setName("Joe");
|
||||
byte[] serialize = MySerializationUtils.serialize(p);
|
||||
Person p2 = MySerializationUtils.deserialize(serialize, Person.class);
|
||||
assertEquals(p2.getAge(), p.getAge());
|
||||
assertEquals(p2.getName(), p.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingCustomSerializationUtils_ThanOk() {
|
||||
assertFalse(MySerializationUtils.isSerializable(Address.class));
|
||||
assertTrue(MySerializationUtils.isSerializable(Person.class));
|
||||
assertTrue(MySerializationUtils.isSerializable(Integer.class));
|
||||
}
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
package com.baeldung.uuid;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class UUIDGeneratorUnitTest {
|
||||
|
||||
private static final String NAMESPACE_URL = "6ba7b811-9dad-11d1-80b4-00c04fd430c8";
|
||||
private static final String NAMESPACE_DNS = "6ba7b810-9dad-11d1-80b4-00c04fd430c8";
|
||||
|
||||
@Test
|
||||
public void version_1_UUID_is_generated_with_correct_length_version_and_variant() {
|
||||
|
||||
UUID uuid = UUIDGenerator.generateType1UUID();
|
||||
|
||||
assertEquals(36, uuid.toString().length());
|
||||
assertEquals(1, uuid.version());
|
||||
assertEquals(2, uuid.variant());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void version_3_UUID_is_correctly_generated_for_domain_baeldung_com() throws UnsupportedEncodingException {
|
||||
|
||||
UUID uuid = UUIDGenerator.generateType3UUID(NAMESPACE_DNS, "baeldung.com");
|
||||
|
||||
assertEquals("23785b78-0132-3ac6-aff6-cfd5be162139", uuid.toString());
|
||||
assertEquals(3, uuid.version());
|
||||
assertEquals(2, uuid.variant());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void version_3_UUID_is_correctly_generated_for_domain_d() throws UnsupportedEncodingException {
|
||||
|
||||
UUID uuid = UUIDGenerator.generateType3UUID(NAMESPACE_DNS, "d");
|
||||
|
||||
assertEquals("dbd41ecb-f466-33de-b309-1468addfc63b", uuid.toString());
|
||||
assertEquals(3, uuid.version());
|
||||
assertEquals(2, uuid.variant());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void version_4_UUID_is_generated_with_correct_length_version_and_variant() {
|
||||
|
||||
UUID uuid = UUIDGenerator.generateType4UUID();
|
||||
|
||||
assertEquals(36, uuid.toString().length());
|
||||
assertEquals(4, uuid.version());
|
||||
assertEquals(2, uuid.variant());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void version_5_UUID_is_correctly_generated_for_domain_baeldung_com() throws UnsupportedEncodingException {
|
||||
|
||||
UUID uuid = UUIDGenerator.generateType5UUID(NAMESPACE_URL, "baeldung.com");
|
||||
|
||||
assertEquals("aeff44a5-8a61-52b6-bcbe-c8e5bd7d0300", uuid.toString());
|
||||
assertEquals(5, uuid.version());
|
||||
assertEquals(2, uuid.variant());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void version_5_UUID_is_correctly_generated_for_domain_baeldung_name() {
|
||||
|
||||
UUID uuid = UUIDGenerator.generateType5UUID("baeldung.com");
|
||||
|
||||
assertEquals("efd5462b-b07a-52a3-94ea-bf575c0e0e75", uuid.toString());
|
||||
assertEquals(5, uuid.version());
|
||||
assertEquals(2, uuid.variant());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user