Merge branch 'master' into master
This commit is contained in:
@@ -7,3 +7,5 @@ This module contains articles about the Java List collection
|
||||
- [Finding All Duplicates in a List in Java](https://www.baeldung.com/java-list-find-duplicates)
|
||||
- [Moving Items Around in an Arraylist](https://www.baeldung.com/java-arraylist-move-items)
|
||||
- [Check if a List Contains an Element From Another List in Java](https://www.baeldung.com/java-check-elements-between-lists)
|
||||
- [Array vs. List Performance in Java](https://www.baeldung.com/java-array-vs-list-performance)
|
||||
- [Set Default Value for Elements in List](https://www.baeldung.com/java-list-set-default-values)
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
## Relevant Articles
|
||||
- [Copying All Keys and Values From One Hashmap Onto Another Without Replacing Existing Keys and Values](https://www.baeldung.com/java-copy-hashmap-no-changes)
|
||||
- [Convert Hashmap to JSON Object in Java](https://www.baeldung.com/java-convert-hashmap-to-json-object)
|
||||
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
package java.com.baeldung.objecttomap;
|
||||
import com.google.gson.Gson;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import wiremock.com.fasterxml.jackson.core.type.TypeReference;
|
||||
import wiremock.com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import wiremock.com.google.common.reflect.TypeToken;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class ObjectToMapUnitTest {
|
||||
Employee employee = new Employee("John", 3000.0);
|
||||
|
||||
@Test
|
||||
public void givenJavaObject_whenUsingReflection_thenConvertToMap() throws IllegalAccessException {
|
||||
Map<String, Object> map = convertUsingReflection(employee);
|
||||
Assert.assertEquals(employee.getName(), map.get("name"));
|
||||
Assert.assertEquals(employee.getSalary(), map.get("salary"));
|
||||
}
|
||||
|
||||
private Map<String, Object> convertUsingReflection(Object object) throws IllegalAccessException {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
Field[] fields = object.getClass().getDeclaredFields();
|
||||
|
||||
for (Field field : fields) {
|
||||
field.setAccessible(true);
|
||||
map.put(field.getName(), field.get(object));
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJavaObject_whenUsingJackson_thenConvertToMap() {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
Map<String, Object> map = objectMapper.convertValue(employee, new TypeReference<Map<String, Object>>() {});
|
||||
Assert.assertEquals(employee.getName(), map.get("name"));
|
||||
Assert.assertEquals(employee.getSalary(), map.get("salary"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJavaObject_whenUsingGson_thenConvertToMap() {
|
||||
Gson gson = new Gson();
|
||||
String json = gson.toJson(employee);
|
||||
Map<String, Object> map = gson.fromJson(json, new TypeToken<Map<String, Object>>() {}.getType());
|
||||
Assert.assertEquals(employee.getName(), map.get("name"));
|
||||
Assert.assertEquals(employee.getSalary(), map.get("salary"));
|
||||
}
|
||||
|
||||
private static class Employee {
|
||||
private String name;
|
||||
private Double salary;
|
||||
|
||||
public Employee(String name, Double salary) {
|
||||
this.name = name;
|
||||
this.salary = salary;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Double getSalary() {
|
||||
return salary;
|
||||
}
|
||||
|
||||
public void setSalary(Double age) {
|
||||
this.salary = salary;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,3 +12,5 @@ This module contains articles about core Java input/output(IO) APIs.
|
||||
- [Storing Java Scanner Input in an Array](https://www.baeldung.com/java-store-scanner-input-in-array)
|
||||
- [How to Take Input as String With Spaces in Java Using Scanner?](https://www.baeldung.com/java-scanner-input-with-spaces)
|
||||
- [Write Console Output to Text File in Java](https://www.baeldung.com/java-write-console-output-file)
|
||||
- [What’s the difference between Scanner next() and nextLine() methods?](https://www.baeldung.com/java-scanner-next-vs-nextline)
|
||||
- [Handle NoSuchElementException When Reading a File Through Scanner](https://www.baeldung.com/java-scanner-nosuchelementexception-reading-file)
|
||||
|
||||
+55
-7
@@ -3,33 +3,81 @@ package com.baeldung.regex.z_regexp;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
public class ZRegularExpressionUnitTest {
|
||||
@Test
|
||||
public void givenCreditCardNumber_thenReturnIfMatched() {
|
||||
String creditCardNumber = "1234567890123456";
|
||||
String creditCardNumber2 = "1234567890123456\n";
|
||||
String pattern = "\\d{16}\\z";
|
||||
Assertions.assertTrue(creditCardNumber.matches(pattern));
|
||||
Assertions.assertTrue(Pattern.compile(pattern).matcher(creditCardNumber).find());
|
||||
Assertions.assertFalse(Pattern.compile(pattern).matcher(creditCardNumber2).find());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCreditCardNumber_thenReturnIfNotMatched() {
|
||||
String creditCardNumber = "1234567890123456\n";
|
||||
String pattern = "\\d{16}\\z";
|
||||
Assertions.assertFalse(Pattern.compile(pattern).matcher(creditCardNumber).find());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLogOutput_thenReturnIfMatched() {
|
||||
String logLine = "2022-05-01 14:30:00,123 INFO Some log message";
|
||||
String pattern = ".*message\\z";
|
||||
Assertions.assertTrue(logLine.matches(pattern));
|
||||
Assertions.assertTrue(Pattern.compile(pattern).matcher(logLine).find());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLogOutput_thenReturnIfNotMatched() {
|
||||
String logLine = "2022-05-01 14:30:00,123 INFO Some log message\n";
|
||||
String pattern = ".*message\\z";
|
||||
Assertions.assertFalse(Pattern.compile(pattern).matcher(logLine).find());
|
||||
}
|
||||
@Test
|
||||
public void givenEmailMessage_thenReturnIfMatched() {
|
||||
String myMessage = "Hello HR, I hope i can write to Baeldung\n";
|
||||
String pattern = ".*Baeldung\\s*\\Z";
|
||||
Assertions.assertTrue(myMessage.matches(pattern));
|
||||
String myMessage2 = "Hello HR, I hope\n i can write to Baeldung";
|
||||
String pattern = ".*Baeldung\\Z";
|
||||
String pattern2 = ".*hope\\Z";
|
||||
Assertions.assertTrue(Pattern.compile(pattern).matcher(myMessage).find());
|
||||
Assertions.assertFalse(Pattern.compile(pattern2).matcher(myMessage2).find());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmailMessage_thenReturnIfNotMatched() {
|
||||
String myMessage = "Hello HR, I hope\n i can write to Baeldung";
|
||||
String pattern = ".*hope\\Z";
|
||||
Assertions.assertFalse(Pattern.compile(pattern).matcher(myMessage).find());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFileExtension_thenReturnIfMatched() {
|
||||
String fileName = "image.jpeg";
|
||||
String fileName = "image.jpeg\n";
|
||||
String fileName2 = "image2.jpeg\n.png";
|
||||
String pattern = ".*\\.jpeg\\Z";
|
||||
Assertions.assertTrue(fileName.matches(pattern));
|
||||
Assertions.assertTrue(Pattern.compile(pattern).matcher(fileName).find());
|
||||
Assertions.assertFalse(Pattern.compile(pattern).matcher(fileName2).find());
|
||||
}
|
||||
|
||||
}
|
||||
@Test
|
||||
public void givenFileExtension_thenReturnIfNotMatched() {
|
||||
String fileName = "image2.jpeg\n.png";
|
||||
String pattern = ".*\\.jpeg\\Z";
|
||||
Assertions.assertFalse(Pattern.compile(pattern).matcher(fileName).find());
|
||||
}
|
||||
@Test
|
||||
public void givenURL_thenReturnIfMatched() {
|
||||
String url = "https://www.example.com/api/endpoint\n";
|
||||
String pattern = ".*/endpoint$";
|
||||
Assertions.assertTrue(Pattern.compile(pattern).matcher(url).find());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSentence_thenReturnIfMatched() {
|
||||
String sentence = "Hello, how are you?";
|
||||
String pattern = ".*[.?!]$";
|
||||
Assertions.assertTrue(Pattern.compile(pattern).matcher(sentence).find());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -7,3 +7,4 @@
|
||||
- [Deserialization Vulnerabilities in Java](https://www.baeldung.com/java-deserialization-vulnerabilities)
|
||||
- [Serialization Validation in Java](https://www.baeldung.com/java-validate-serializable)
|
||||
- [What Is the serialVersionUID?](https://www.baeldung.com/java-serial-version-uid)
|
||||
- [Java Serialization: readObject() vs. readResolve()](https://www.baeldung.com/java-serialization-readobject-vs-readresolve)
|
||||
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.readresolvevsreadobject;
|
||||
|
||||
import java.io.ObjectStreamException;
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Singleton implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private static Singleton INSTANCE = new Singleton();
|
||||
|
||||
private Singleton() {
|
||||
}
|
||||
|
||||
public static Singleton getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
private Object readResolve() throws ObjectStreamException {
|
||||
return INSTANCE;
|
||||
}
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
package com.baeldung.readresolvevsreadobject;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.Serializable;
|
||||
|
||||
public class User implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 3659932210257138726L;
|
||||
private String userName;
|
||||
private String password;
|
||||
|
||||
public String getUserName() {
|
||||
return userName;
|
||||
}
|
||||
|
||||
public void setUserName(String userName) {
|
||||
this.userName = userName;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User [userName=" + userName + ", password=" + password + "]";
|
||||
}
|
||||
|
||||
public User() {
|
||||
}
|
||||
|
||||
public User(String userName, String password) {
|
||||
super();
|
||||
this.userName = userName;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
private void writeObject(ObjectOutputStream oos) throws IOException {
|
||||
this.password = "xyz" + password;
|
||||
oos.defaultWriteObject();
|
||||
}
|
||||
|
||||
private void readObject(ObjectInputStream aInputStream)
|
||||
throws ClassNotFoundException, IOException {
|
||||
aInputStream.defaultReadObject();
|
||||
this.password = password.substring(3);
|
||||
}
|
||||
|
||||
private Object readResolve() {
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.readresolvevsreadobject;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
|
||||
public class SingletonUnitTest {
|
||||
|
||||
@Test
|
||||
public void testSingletonObj_withNoReadResolve() throws ClassNotFoundException, IOException {
|
||||
// Serialization
|
||||
FileOutputStream fos = new FileOutputStream("singleton.ser");
|
||||
ObjectOutputStream oos = new ObjectOutputStream(fos);
|
||||
Singleton actualSingletonObject = Singleton.getInstance();
|
||||
oos.writeObject(actualSingletonObject);
|
||||
|
||||
// Deserialization
|
||||
Singleton deserializedSingletonObject = null;
|
||||
FileInputStream fis = new FileInputStream("singleton.ser");
|
||||
ObjectInputStream ois = new ObjectInputStream(fis);
|
||||
deserializedSingletonObject = (Singleton) ois.readObject();
|
||||
// remove readResolve() from Singleton class and uncomment this to test.
|
||||
//assertNotEquals(actualSingletonObject.hashCode(), deserializedSingletonObject.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSingletonObj_withCustomReadResolve()
|
||||
throws ClassNotFoundException, IOException {
|
||||
// Serialization
|
||||
FileOutputStream fos = new FileOutputStream("singleton.ser");
|
||||
ObjectOutputStream oos = new ObjectOutputStream(fos);
|
||||
Singleton actualSingletonObject = Singleton.getInstance();
|
||||
oos.writeObject(actualSingletonObject);
|
||||
|
||||
// Deserialization
|
||||
Singleton deserializedSingletonObject = null;
|
||||
FileInputStream fis = new FileInputStream("singleton.ser");
|
||||
ObjectInputStream ois = new ObjectInputStream(fis);
|
||||
deserializedSingletonObject = (Singleton) ois.readObject();
|
||||
assertEquals(actualSingletonObject.hashCode(), deserializedSingletonObject.hashCode());
|
||||
}
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package com.baeldung.readresolvevsreadobject;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
|
||||
public class UserUnitTest {
|
||||
|
||||
@Test
|
||||
public void testDeserializeObj_withOverriddenReadObject()
|
||||
throws ClassNotFoundException, IOException {
|
||||
// Serialization
|
||||
FileOutputStream fos = new FileOutputStream("user.ser");
|
||||
ObjectOutputStream oos = new ObjectOutputStream(fos);
|
||||
User acutalObject = new User("Sachin", "Kumar");
|
||||
oos.writeObject(acutalObject);
|
||||
|
||||
// Deserialization
|
||||
User deserializedUser = null;
|
||||
FileInputStream fis = new FileInputStream("user.ser");
|
||||
ObjectInputStream ois = new ObjectInputStream(fis);
|
||||
deserializedUser = (User) ois.readObject();
|
||||
assertNotEquals(deserializedUser.hashCode(), acutalObject.hashCode());
|
||||
assertEquals(deserializedUser.getUserName(), "Sachin");
|
||||
assertEquals(deserializedUser.getPassword(), "Kumar");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializeObj_withDefaultReadObject()
|
||||
throws ClassNotFoundException, IOException {
|
||||
// Serialization
|
||||
FileOutputStream fos = new FileOutputStream("user.ser");
|
||||
ObjectOutputStream oos = new ObjectOutputStream(fos);
|
||||
User acutalObject = new User("Sachin", "Kumar");
|
||||
oos.writeObject(acutalObject);
|
||||
|
||||
// Deserialization
|
||||
User deserializedUser = null;
|
||||
FileInputStream fis = new FileInputStream("user.ser");
|
||||
ObjectInputStream ois = new ObjectInputStream(fis);
|
||||
deserializedUser = (User) ois.readObject();
|
||||
assertNotEquals(deserializedUser.hashCode(), acutalObject.hashCode());
|
||||
assertEquals(deserializedUser.getUserName(), "Sachin");
|
||||
// remove readObject() from User class and uncomment this to test.
|
||||
//assertEquals(deserializedUser.getPassword(), "xyzKumar");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user