Merge remote-tracking branch 'upstream/master'
This commit is contained in:
commit
6fc2261a39
|
@ -15,21 +15,24 @@ import org.junit.Test;
|
|||
public class MapFirstPairUnitTest {
|
||||
|
||||
private Map.Entry<Integer, String> getFirstPairUsingIterator(Map<Integer, String> map) {
|
||||
if (map == null || map.size() == 0)
|
||||
if (map == null || map.size() == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Iterator<Map.Entry<Integer, String>> iterator = map.entrySet()
|
||||
.iterator();
|
||||
|
||||
if (iterator.hasNext())
|
||||
if (iterator.hasNext()) {
|
||||
return iterator.next();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private Map.Entry<Integer, String> getFirstPairUsingStream(Map<Integer, String> map) {
|
||||
if (map == null || map.size() == 0)
|
||||
if (map == null || map.size() == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Set<Map.Entry<Integer, String>> entrySet = map.entrySet();
|
||||
|
||||
|
|
|
@ -0,0 +1,72 @@
|
|||
package com.baeldung.signature;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class OverloadingErrors<T extends Serializable> {
|
||||
|
||||
public void print() {
|
||||
System.out.println("Signature is: print()");
|
||||
}
|
||||
|
||||
/*
|
||||
// Uncommenting this method will lead to a compilation error: java: method print() is already defined in class
|
||||
// The method signature is independent from return type
|
||||
public int print() {
|
||||
System.out.println("Signature is: print()");
|
||||
return 0;
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
// Uncommenting this method will lead to a compilation error: java: method print() is already defined in class
|
||||
// The method signature is independent from modifiers
|
||||
private final void print() {
|
||||
System.out.println("Signature is: print()");
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
// Uncommenting this method will lead to a compilation error: java: method print() is already defined in class
|
||||
// The method signature is independent from thrown exception declaration
|
||||
public void print() throws IllegalStateException {
|
||||
System.out.println("Signature is: print()");
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
*/
|
||||
|
||||
public void print(int parameter) {
|
||||
System.out.println("Signature is: print(int)");
|
||||
}
|
||||
|
||||
/*
|
||||
// Uncommenting this method will lead to a compilation error: java: method print(int) is already defined in class
|
||||
// The method signature is independent from parameter names
|
||||
public void print(int anotherParameter) {
|
||||
System.out.println("Signature is: print(int)");
|
||||
}
|
||||
*/
|
||||
|
||||
public void printElement(T t) {
|
||||
System.out.println("Signature is: printElement(T)");
|
||||
}
|
||||
|
||||
/*
|
||||
// Uncommenting this method will lead to a compilation error: java: name clash: printElement(java.io.Serializable) and printElement(T) have the same erasure
|
||||
// Even though the signatures appear different, the compiler cannot statically bind the correct method after type erasure
|
||||
public void printElement(Serializable o) {
|
||||
System.out.println("Signature is: printElement(Serializable)");
|
||||
}
|
||||
*/
|
||||
|
||||
public void print(Object... parameter) {
|
||||
System.out.println("Signature is: print(Object...)");
|
||||
}
|
||||
|
||||
/*
|
||||
// Uncommenting this method will lead to a compilation error: java cannot declare both sum(Object...) and sum(Object[])
|
||||
// Even though the signatures appear different, after compilation they both resolve to sum(Object[])
|
||||
public void print(Object[] parameter) {
|
||||
System.out.println("Signature is: print(Object[])");
|
||||
}
|
||||
*/
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package com.baeldung.signature;
|
||||
|
||||
public class StaticBinding {
|
||||
|
||||
public Number sum(Integer term1, Integer term2) {
|
||||
System.out.println("Adding integers");
|
||||
return term1 + term2;
|
||||
}
|
||||
|
||||
public Number sum(Number term1, Number term2) {
|
||||
System.out.println("Adding numbers");
|
||||
return term1.doubleValue() + term2.doubleValue();
|
||||
}
|
||||
|
||||
public Number sum(Object term1, Object term2) {
|
||||
System.out.println("Adding objects");
|
||||
return term1.hashCode() + term2.hashCode();
|
||||
}
|
||||
|
||||
public Number sum(Object term1, Object... term2) {
|
||||
System.out.println("Adding variable arguments: " + term2.length);
|
||||
int result = term1.hashCode();
|
||||
for (Object o : term2) {
|
||||
result += o.hashCode();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
StaticBinding obj = new StaticBinding();
|
||||
|
||||
obj.sum(2, 3); // "Adding integers" due to auto-boxing from int to Integer
|
||||
obj.sum(Integer.valueOf(2), Integer.valueOf(3)); // "Adding integers" due to exact parameter types
|
||||
obj.sum(2, 0x1); // "Adding integers" due to type promotion from byte to int
|
||||
|
||||
obj.sum((Number) 2, (Number) 3); // "Adding numbers" due to explicit cast to Number
|
||||
obj.sum(2.0d, 3.0d); // "Adding numbers" due to auto-boxing from double to Double
|
||||
obj.sum(Float.valueOf(2), Float.valueOf(3)); // "Adding numbers" due to polimorphism
|
||||
|
||||
obj.sum((Object) 2, (Object) 3); // "Adding objects" due to explicit cast to Object
|
||||
obj.sum(2, "John"); // "Adding objects" due to polimorphism
|
||||
|
||||
obj.sum(new Object(), new Object(), new Object()); // "Adding variable arguments 2"
|
||||
obj.sum(new Object(), new Object[]{new Object()}); // "Adding variable arguments 1"
|
||||
}
|
||||
}
|
|
@ -25,10 +25,10 @@ public class AvailableCiphersUnitTest {
|
|||
@Test
|
||||
public void whenGetServicesWithFilter_thenGetAllCompatibleCipherAlgorithms() {
|
||||
List<String> algorithms = Arrays.stream(Security.getProviders())
|
||||
.flatMap(provider -> provider.getServices().stream())
|
||||
.filter(service -> "Cipher".equals(service.getType()))
|
||||
.map(Provider.Service::getAlgorithm)
|
||||
.collect(Collectors.toList());
|
||||
.flatMap(provider -> provider.getServices().stream())
|
||||
.filter(service -> "Cipher".equals(service.getType()))
|
||||
.map(Provider.Service::getAlgorithm)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
algorithms.forEach(logger::info);
|
||||
}
|
||||
|
|
|
@ -116,4 +116,41 @@
|
|||
<moshi.version>1.9.2</moshi.version>
|
||||
<commons-lang3.version>3.9</commons-lang3.version>
|
||||
</properties>
|
||||
<build>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
|
||||
<plugin>
|
||||
<groupId>org.eclipse.m2e</groupId>
|
||||
<artifactId>lifecycle-mapping</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<configuration>
|
||||
<lifecycleMappingMetadata>
|
||||
<pluginExecutions>
|
||||
<pluginExecution>
|
||||
<pluginExecutionFilter>
|
||||
<groupId>
|
||||
org.apache.maven.plugins
|
||||
</groupId>
|
||||
<artifactId>
|
||||
maven-pmd-plugin
|
||||
</artifactId>
|
||||
<versionRange>
|
||||
[3.13.0,)
|
||||
</versionRange>
|
||||
<goals>
|
||||
<goal>check</goal>
|
||||
</goals>
|
||||
</pluginExecutionFilter>
|
||||
<action>
|
||||
<ignore></ignore>
|
||||
</action>
|
||||
</pluginExecution>
|
||||
</pluginExecutions>
|
||||
</lifecycleMappingMetadata>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
</build>
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,123 @@
|
|||
package com.baeldung.jsonoptimization;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Objects;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
public class Customer {
|
||||
|
||||
private long id;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private String street;
|
||||
private String postalCode;
|
||||
private String city;
|
||||
private String state;
|
||||
private String phoneNumber;
|
||||
private String email;
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getStreet() {
|
||||
return street;
|
||||
}
|
||||
|
||||
public void setStreet(String street) {
|
||||
this.street = street;
|
||||
}
|
||||
|
||||
public String getPostalCode() {
|
||||
return postalCode;
|
||||
}
|
||||
|
||||
public void setPostalCode(String postalCode) {
|
||||
this.postalCode = postalCode;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public String getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
public void setState(String state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public String getPhoneNumber() {
|
||||
return phoneNumber;
|
||||
}
|
||||
|
||||
public void setPhoneNumber(String phoneNumber) {
|
||||
this.phoneNumber = phoneNumber;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(city, email, firstName, id, lastName, phoneNumber, postalCode, state, street);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (!(obj instanceof Customer)) {
|
||||
return false;
|
||||
}
|
||||
Customer other = (Customer) obj;
|
||||
return Objects.equals(city, other.city) && Objects.equals(email, other.email) && Objects.equals(firstName, other.firstName) && id == other.id && Objects.equals(lastName, other.lastName) && Objects.equals(phoneNumber, other.phoneNumber)
|
||||
&& Objects.equals(postalCode, other.postalCode) && Objects.equals(state, other.state) && Objects.equals(street, other.street);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Customer [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", street=" + street + ", postalCode=" + postalCode + ", city=" + city + ", state=" + state + ", phoneNumber=" + phoneNumber + ", email=" + email + "]";
|
||||
}
|
||||
|
||||
public static Customer[] fromMockFile() throws IOException {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
InputStream jsonFile = new FileInputStream("src/test/resources/json_optimization_mock_data.json");
|
||||
Customer[] feedback = objectMapper.readValue(jsonFile, Customer[].class);
|
||||
return feedback;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
package com.baeldung.jsonoptimization;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.ObjectCodec;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
|
||||
|
||||
public class CustomerDeserializer extends StdDeserializer<Customer> {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public CustomerDeserializer() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public CustomerDeserializer(Class<Customer> t) {
|
||||
super(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Customer deserialize(JsonParser parser, DeserializationContext deserializer) throws IOException {
|
||||
Customer feedback = new Customer();
|
||||
ObjectCodec codec = parser.getCodec();
|
||||
JsonNode node = codec.readTree(parser);
|
||||
|
||||
feedback.setId(node.get(0)
|
||||
.asLong());
|
||||
feedback.setFirstName(node.get(1)
|
||||
.asText());
|
||||
feedback.setLastName(node.get(2)
|
||||
.asText());
|
||||
feedback.setStreet(node.get(3)
|
||||
.asText());
|
||||
feedback.setPostalCode(node.get(4)
|
||||
.asText());
|
||||
feedback.setCity(node.get(5)
|
||||
.asText());
|
||||
feedback.setState(node.get(6)
|
||||
.asText());
|
||||
JsonNode phoneNumber = node.get(7);
|
||||
feedback.setPhoneNumber(phoneNumber.isNull() ? null : phoneNumber.asText());
|
||||
JsonNode email = node.get(8);
|
||||
feedback.setEmail(email.isNull() ? null : email.asText());
|
||||
|
||||
return feedback;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package com.baeldung.jsonoptimization;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
||||
|
||||
public class CustomerSerializer extends StdSerializer<Customer> {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public CustomerSerializer() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public CustomerSerializer(Class<Customer> t) {
|
||||
super(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(Customer customer, JsonGenerator jsonGenerator, SerializerProvider serializer) throws IOException {
|
||||
jsonGenerator.writeStartArray();
|
||||
jsonGenerator.writeNumber(customer.getId());
|
||||
jsonGenerator.writeString(customer.getFirstName());
|
||||
jsonGenerator.writeString(customer.getLastName());
|
||||
jsonGenerator.writeString(customer.getStreet());
|
||||
jsonGenerator.writeString(customer.getPostalCode());
|
||||
jsonGenerator.writeString(customer.getCity());
|
||||
jsonGenerator.writeString(customer.getState());
|
||||
jsonGenerator.writeString(customer.getPhoneNumber());
|
||||
jsonGenerator.writeString(customer.getEmail());
|
||||
jsonGenerator.writeEndArray();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,155 @@
|
|||
package com.baeldung.jsonoptimization;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class CustomerShortNames {
|
||||
|
||||
@JsonProperty("i")
|
||||
private long id;
|
||||
|
||||
@JsonProperty("f")
|
||||
private String firstName;
|
||||
|
||||
@JsonProperty("l")
|
||||
private String lastName;
|
||||
|
||||
@JsonProperty("s")
|
||||
private String street;
|
||||
|
||||
@JsonProperty("p")
|
||||
private String postalCode;
|
||||
|
||||
@JsonProperty("c")
|
||||
private String city;
|
||||
|
||||
@JsonProperty("a")
|
||||
private String state;
|
||||
|
||||
@JsonProperty("o")
|
||||
private String phoneNumber;
|
||||
|
||||
@JsonProperty("e")
|
||||
private String email;
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getStreet() {
|
||||
return street;
|
||||
}
|
||||
|
||||
public void setStreet(String street) {
|
||||
this.street = street;
|
||||
}
|
||||
|
||||
public String getPostalCode() {
|
||||
return postalCode;
|
||||
}
|
||||
|
||||
public void setPostalCode(String postalCode) {
|
||||
this.postalCode = postalCode;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public String getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
public void setState(String state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public String getPhoneNumber() {
|
||||
return phoneNumber;
|
||||
}
|
||||
|
||||
public void setPhoneNumber(String phoneNumber) {
|
||||
this.phoneNumber = phoneNumber;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(city, email, firstName, id, lastName, phoneNumber, postalCode, state, street);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (!(obj instanceof CustomerShortNames)) {
|
||||
return false;
|
||||
}
|
||||
CustomerShortNames other = (CustomerShortNames) obj;
|
||||
return Objects.equals(city, other.city) && Objects.equals(email, other.email) && Objects.equals(firstName, other.firstName) && id == other.id && Objects.equals(lastName, other.lastName) && Objects.equals(phoneNumber, other.phoneNumber)
|
||||
&& Objects.equals(postalCode, other.postalCode) && Objects.equals(state, other.state) && Objects.equals(street, other.street);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CustomerWithShorterAttributes [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", street=" + street + ", postalCode=" + postalCode + ", city=" + city + ", state=" + state + ", phoneNumber=" + phoneNumber + ", email=" + email
|
||||
+ "]";
|
||||
}
|
||||
|
||||
public static CustomerShortNames[] fromCustomers(Customer[] customers) {
|
||||
CustomerShortNames[] feedback = new CustomerShortNames[customers.length];
|
||||
|
||||
for (int i = 0; i < customers.length; i++) {
|
||||
Customer aCustomer = customers[i];
|
||||
CustomerShortNames newOne = new CustomerShortNames();
|
||||
|
||||
newOne.setId(aCustomer.getId());
|
||||
newOne.setFirstName(aCustomer.getFirstName());
|
||||
newOne.setLastName(aCustomer.getLastName());
|
||||
newOne.setStreet(aCustomer.getStreet());
|
||||
newOne.setCity(aCustomer.getCity());
|
||||
newOne.setPostalCode(aCustomer.getPostalCode());
|
||||
newOne.setState(aCustomer.getState());
|
||||
newOne.setPhoneNumber(aCustomer.getPhoneNumber());
|
||||
newOne.setEmail(aCustomer.getEmail());
|
||||
|
||||
feedback[i] = newOne;
|
||||
}
|
||||
|
||||
return feedback;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
package com.baeldung.jsonoptimization;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class CustomerSlim {
|
||||
private long id;
|
||||
private String name;
|
||||
private String address;
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(address, id, name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (!(obj instanceof CustomerSlim)) {
|
||||
return false;
|
||||
}
|
||||
CustomerSlim other = (CustomerSlim) obj;
|
||||
return Objects.equals(address, other.address) && id == other.id && Objects.equals(name, other.name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CustomerSlim [id=" + id + ", name=" + name + ", address=" + address + "]";
|
||||
}
|
||||
|
||||
public static CustomerSlim[] fromCustomers(Customer[] customers) {
|
||||
CustomerSlim[] feedback = new CustomerSlim[customers.length];
|
||||
|
||||
for (int i = 0; i < customers.length; i++) {
|
||||
Customer aCustomer = customers[i];
|
||||
CustomerSlim newOne = new CustomerSlim();
|
||||
|
||||
newOne.setId(aCustomer.getId());
|
||||
newOne.setName(aCustomer.getFirstName() + " " + aCustomer.getLastName());
|
||||
newOne.setAddress(aCustomer.getStreet() + ", " + aCustomer.getCity() + " " + aCustomer.getState() + " " + aCustomer.getPostalCode());
|
||||
|
||||
feedback[i] = newOne;
|
||||
}
|
||||
|
||||
return feedback;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package com.baeldung.jsonoptimization;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.ObjectCodec;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
|
||||
|
||||
public class CustomerSlimDeserializer extends StdDeserializer<CustomerSlim> {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public CustomerSlimDeserializer() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public CustomerSlimDeserializer(Class<CustomerSlim> t) {
|
||||
super(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CustomerSlim deserialize(JsonParser parser, DeserializationContext deserializer) throws IOException {
|
||||
CustomerSlim feedback = new CustomerSlim();
|
||||
ObjectCodec codec = parser.getCodec();
|
||||
JsonNode node = codec.readTree(parser);
|
||||
|
||||
feedback.setId(node.get(0)
|
||||
.asLong());
|
||||
feedback.setName(node.get(1)
|
||||
.asText());
|
||||
feedback.setAddress(node.get(2)
|
||||
.asText());
|
||||
|
||||
return feedback;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package com.baeldung.jsonoptimization;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
||||
|
||||
public class CustomerSlimSerializer extends StdSerializer<CustomerSlim> {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public CustomerSlimSerializer() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public CustomerSlimSerializer(Class<CustomerSlim> t) {
|
||||
super(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(CustomerSlim customer, JsonGenerator jsonGenerator, SerializerProvider serializer) throws IOException {
|
||||
jsonGenerator.writeStartArray();
|
||||
jsonGenerator.writeNumber(customer.getId());
|
||||
jsonGenerator.writeString(customer.getName());
|
||||
jsonGenerator.writeString(customer.getAddress());
|
||||
jsonGenerator.writeEndArray();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,81 @@
|
|||
package com.baeldung.jsonoptimization;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class CustomerSlimShortNames {
|
||||
|
||||
@JsonProperty("i")
|
||||
private long id;
|
||||
|
||||
@JsonProperty("n")
|
||||
private String name;
|
||||
|
||||
@JsonProperty("a")
|
||||
private String address;
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(address, id, name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (!(obj instanceof CustomerSlimShortNames)) {
|
||||
return false;
|
||||
}
|
||||
CustomerSlimShortNames other = (CustomerSlimShortNames) obj;
|
||||
return Objects.equals(address, other.address) && id == other.id && Objects.equals(name, other.name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CustomerSlim [id=" + id + ", name=" + name + ", address=" + address + "]";
|
||||
}
|
||||
|
||||
public static CustomerSlimShortNames[] fromCustomers(Customer[] customers) {
|
||||
CustomerSlimShortNames[] feedback = new CustomerSlimShortNames[customers.length];
|
||||
|
||||
for (int i = 0; i < customers.length; i++) {
|
||||
Customer aCustomer = customers[i];
|
||||
CustomerSlimShortNames newOne = new CustomerSlimShortNames();
|
||||
|
||||
newOne.setId(aCustomer.getId());
|
||||
newOne.setName(aCustomer.getFirstName() + " " + aCustomer.getLastName());
|
||||
newOne.setAddress(aCustomer.getStreet() + ", " + aCustomer.getCity() + " " + aCustomer.getState() + " " + aCustomer.getPostalCode());
|
||||
|
||||
feedback[i] = newOne;
|
||||
}
|
||||
|
||||
return feedback;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,180 @@
|
|||
package com.baeldung.jsonoptimization;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
||||
import com.fasterxml.jackson.core.Version;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.ObjectWriter;
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
|
||||
class JsonOptimizationUnitTest {
|
||||
private static final String TEST_LABEL_JACKSON_DEFAULT_OPTIONS = "Default JSON";
|
||||
private static final String TEST_LABEL_DEFAULT_JSON_NO_NULL = "Default JSON without null";
|
||||
private static final String TEST_LABEL_SHORTER_FIELD_NAMES = "Shorter field names";
|
||||
private static final String TEST_LABEL_SHORTER_FIELD_NAMES_AND_NO_NULL = "Shorter field names without null";
|
||||
private static final String TEST_LABEL_SERIALIZING_TO_ARRAY = "Custom serializer";
|
||||
private static final String TEST_LABEL_SLIM_CUSTOM_SERIALIZER = "Slim custom serializer";
|
||||
private static final String TEST_LABEL_SLIM_CUSTOMER = "Slim customer";
|
||||
private static final String TEST_LABEL_SLIM_CUSTOMER_SHORT_NAMES = "Slim customer with shorter field names";
|
||||
private static DecimalFormat LENGTH_FORMATTER = new DecimalFormat("###,###.0");
|
||||
private static DecimalFormat PERCENT_FORMATTER = new DecimalFormat("###.0");
|
||||
private static Customer[] customers;
|
||||
private ObjectMapper mapper;
|
||||
private static int defaultJsonLength;
|
||||
|
||||
@BeforeAll
|
||||
static void setUpOnce() throws Exception {
|
||||
customers = Customer.fromMockFile();
|
||||
ObjectMapper oneTimeMapper = new ObjectMapper();
|
||||
byte[] feedback = oneTimeMapper.writeValueAsBytes(customers);
|
||||
defaultJsonLength = feedback.length;
|
||||
System.out.println();
|
||||
System.out.println("Default JSON length: " + defaultJsonLength);
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
mapper = new ObjectMapper();
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenSetUp_ThenOneThousandCustomers() {
|
||||
assertEquals(1000, customers.length, "There should be a 1000 customers");
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenJacksonDefaultOptions_thenValid() throws IOException {
|
||||
printBanner(TEST_LABEL_JACKSON_DEFAULT_OPTIONS);
|
||||
byte[] plainJson = createJsonAndVerify(TEST_LABEL_JACKSON_DEFAULT_OPTIONS, customers);
|
||||
compressJson(TEST_LABEL_JACKSON_DEFAULT_OPTIONS, plainJson);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenExcludingNull_thenValid() throws IOException {
|
||||
printBanner(TEST_LABEL_DEFAULT_JSON_NO_NULL);
|
||||
mapper.setSerializationInclusion(Include.NON_NULL);
|
||||
byte[] plainJson = createJsonAndVerify(TEST_LABEL_DEFAULT_JSON_NO_NULL, customers);
|
||||
compressJson(TEST_LABEL_DEFAULT_JSON_NO_NULL, plainJson);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenShorterFieldNames_thenValid() throws IOException {
|
||||
printBanner(TEST_LABEL_SHORTER_FIELD_NAMES);
|
||||
CustomerShortNames[] shorterOnes = CustomerShortNames.fromCustomers(customers);
|
||||
byte[] shorterJson = createJsonAndVerify(TEST_LABEL_SHORTER_FIELD_NAMES, shorterOnes);
|
||||
compressJson(TEST_LABEL_SHORTER_FIELD_NAMES, shorterJson);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenShorterFieldNamesAndExcludingNull_thenValid() throws IOException {
|
||||
printBanner(TEST_LABEL_SHORTER_FIELD_NAMES_AND_NO_NULL);
|
||||
CustomerShortNames[] shorterOnes = CustomerShortNames.fromCustomers(customers);
|
||||
mapper.setSerializationInclusion(Include.NON_NULL);
|
||||
byte[] shorterJson = createJsonAndVerify(TEST_LABEL_SHORTER_FIELD_NAMES_AND_NO_NULL, shorterOnes);
|
||||
compressJson(TEST_LABEL_SHORTER_FIELD_NAMES_AND_NO_NULL, shorterJson);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenSlimCustomer_thenValid() throws IOException {
|
||||
printBanner(TEST_LABEL_SLIM_CUSTOMER);
|
||||
CustomerSlim[] slimOnes = CustomerSlim.fromCustomers(customers);
|
||||
byte[] slimJson = createJsonAndVerify(TEST_LABEL_SLIM_CUSTOMER, slimOnes);
|
||||
compressJson(TEST_LABEL_SLIM_CUSTOMER, slimJson);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenSlimCustomerAndShorterFieldNames_thenValid() throws IOException {
|
||||
printBanner(TEST_LABEL_SLIM_CUSTOMER_SHORT_NAMES);
|
||||
CustomerSlimShortNames[] slimOnes = CustomerSlimShortNames.fromCustomers(customers);
|
||||
byte[] slimJson = createJsonAndVerify(TEST_LABEL_SLIM_CUSTOMER_SHORT_NAMES, slimOnes);
|
||||
compressJson(TEST_LABEL_SLIM_CUSTOMER_SHORT_NAMES, slimJson);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenSerializingToArray_thenValid() throws IOException {
|
||||
printBanner(TEST_LABEL_SERIALIZING_TO_ARRAY);
|
||||
SimpleModule serializer = new SimpleModule("CustomDeSerializer", new Version(1, 0, 0, null, null, null));
|
||||
serializer.addSerializer(Customer.class, new CustomerSerializer());
|
||||
serializer.addDeserializer(Customer.class, new CustomerDeserializer());
|
||||
mapper.registerModule(serializer);
|
||||
|
||||
byte[] plainJson = createJsonAndVerify(TEST_LABEL_SERIALIZING_TO_ARRAY, customers);
|
||||
compressJson(TEST_LABEL_SERIALIZING_TO_ARRAY, plainJson);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenSerializingToArrayAndSlimCustomer_thenValid() throws IOException {
|
||||
printBanner(TEST_LABEL_SLIM_CUSTOM_SERIALIZER);
|
||||
SimpleModule serializer = new SimpleModule("SlimCustomDeSerializer", new Version(1, 0, 0, null, null, null));
|
||||
serializer.addSerializer(CustomerSlim.class, new CustomerSlimSerializer());
|
||||
serializer.addDeserializer(CustomerSlim.class, new CustomerSlimDeserializer());
|
||||
mapper.registerModule(serializer);
|
||||
|
||||
CustomerSlim[] slimOnes = CustomerSlim.fromCustomers(customers);
|
||||
byte[] plainJson = createJsonAndVerify(TEST_LABEL_SLIM_CUSTOM_SERIALIZER, slimOnes);
|
||||
compressJson(TEST_LABEL_SLIM_CUSTOM_SERIALIZER, plainJson);
|
||||
}
|
||||
|
||||
private void printBanner(String name) {
|
||||
System.out.println();
|
||||
System.out.println("************************************************");
|
||||
System.out.println("Testing " + name);
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
void compressJson(String label, byte[] plainJson) throws IOException {
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
GZIPOutputStream gzipStream = new GZIPOutputStream(outputStream);
|
||||
gzipStream.write(plainJson);
|
||||
gzipStream.close();
|
||||
outputStream.close();
|
||||
byte[] gzippedJson = outputStream.toByteArray();
|
||||
double length = gzippedJson.length / 1024d;
|
||||
double percent = gzippedJson.length * 100d / defaultJsonLength;
|
||||
System.out.println(label + " GZIPped length: " + LENGTH_FORMATTER.format(length)
|
||||
+ "kB (" + PERCENT_FORMATTER.format(percent) + "%)");
|
||||
assertTrue(plainJson.length > gzippedJson.length, label + " should be longer than GZIPped data");
|
||||
}
|
||||
|
||||
private byte[] createJsonAndVerify(String label, Object[] customers) throws IOException {
|
||||
System.out.println(label + " sample: ");
|
||||
ObjectWriter prettyWritter = mapper.writerWithDefaultPrettyPrinter();
|
||||
System.out.println(prettyWritter.writeValueAsString(customers[0]));
|
||||
|
||||
byte[] feedback = mapper.writeValueAsBytes(customers);
|
||||
double length = feedback.length / 1024d;
|
||||
double percent = feedback.length * 100d / defaultJsonLength;
|
||||
System.out.println(label + " length: " + LENGTH_FORMATTER.format(length)
|
||||
+ "kB (" + PERCENT_FORMATTER.format(percent) + "%)");
|
||||
assertTrue(feedback.length > 1, label + " should be there");
|
||||
|
||||
String prefix = label.replaceAll(" ", "-")
|
||||
.toLowerCase();
|
||||
File tempFile = File.createTempFile("jon-optimization-" + prefix, ".json");
|
||||
FileOutputStream fos = new FileOutputStream(tempFile);
|
||||
fos.write(feedback);
|
||||
fos.close();
|
||||
System.out.println(label + " file: " + tempFile.toString());
|
||||
|
||||
Object[] restoredOnes = mapper.readValue(feedback, customers.getClass());
|
||||
assertArrayEquals(TEST_LABEL_JACKSON_DEFAULT_OPTIONS + ": restoring from JSON should work", customers, restoredOnes);
|
||||
|
||||
return feedback;
|
||||
}
|
||||
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.lombok.builder;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
|
||||
@Builder(builderMethodName = "internalBuilder")
|
||||
@Getter
|
||||
public class RequiredFieldAnnotation {
|
||||
|
||||
@NonNull
|
||||
String name;
|
||||
String description;
|
||||
|
||||
public static RequiredFieldAnnotationBuilder builder(String name) {
|
||||
return internalBuilder().name(name);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.lombok.builder;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class RequiredFieldAnnotationUnitTest {
|
||||
RequiredFieldAnnotation requiredFieldTest;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
requiredFieldTest = RequiredFieldAnnotation.builder("NameField").description("Field Description").build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBuilderWithRequiredParameter_thenParameterIsPresent() {
|
||||
assertEquals("NameField", requiredFieldTest.getName());
|
||||
}
|
||||
|
||||
}
|
|
@ -11,9 +11,8 @@
|
|||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<artifactId>persistence-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -188,13 +188,6 @@
|
|||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Needed for running tests (you may also use TestNG) -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Others -->
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
|
|
|
@ -75,6 +75,11 @@
|
|||
|
||||
<properties>
|
||||
<flyway.configFiles>src/main/resources/application-${spring-boot.run.profiles}.properties</flyway.configFiles>
|
||||
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -65,6 +65,11 @@
|
|||
<properties>
|
||||
<flyway-core.version>5.2.3</flyway-core.version>
|
||||
<flyway-maven-plugin.version>5.0.2</flyway-maven-plugin.version>
|
||||
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>jpa-hibernate-cascade-type</artifactId>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>persistence-modules</artifactId>
|
||||
|
|
|
@ -92,5 +92,10 @@
|
|||
<properties>
|
||||
<!-- persistence -->
|
||||
<hibernate.version>5.2.17.Final</hibernate.version>
|
||||
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
|
|
@ -68,6 +68,11 @@
|
|||
<properties>
|
||||
<r2dbc-h2.version>0.8.1.RELEASE</r2dbc-h2.version>
|
||||
<h2.version>1.4.200</h2.version>
|
||||
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -61,6 +61,11 @@
|
|||
<redisson.version>3.13.1</redisson.version>
|
||||
<jedis.version>3.3.0</jedis.version>
|
||||
<epoll.version>4.1.50.Final</epoll.version>
|
||||
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -39,6 +39,11 @@
|
|||
|
||||
<properties>
|
||||
<mysql-connector-java.version>8.0.12</mysql-connector-java.version>
|
||||
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -17,6 +17,13 @@
|
|||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit</groupId>
|
||||
<artifactId>junit-bom</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-dependencies</artifactId>
|
||||
|
@ -36,7 +43,6 @@
|
|||
<artifactId>jdbi3-sqlobject</artifactId>
|
||||
<version>${jdbi.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
|
|
@ -46,6 +46,11 @@
|
|||
<!-- The main class to start by executing java -jar -->
|
||||
<start-class>com.baeldung.h2db.demo.server.SpringBootApp</start-class>
|
||||
<db-util.version>1.0.4</db-util.version>
|
||||
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -36,4 +36,10 @@
|
|||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
</project>
|
|
@ -76,6 +76,11 @@
|
|||
<properties>
|
||||
<mockito.version>2.23.0</mockito.version>
|
||||
<validation-api.version>2.0.1.Final</validation-api.version>
|
||||
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -53,6 +53,11 @@
|
|||
<properties>
|
||||
<spring-boot.version>2.2.6.RELEASE</spring-boot.version>
|
||||
<cassandra-unit-spring.version>3.11.2.0</cassandra-unit-spring.version>
|
||||
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -104,6 +104,11 @@
|
|||
<cassandra-unit-spring.version>2.1.9.2</cassandra-unit-spring.version>
|
||||
<cassandra-unit-shaded.version>2.1.9.2</cassandra-unit-shaded.version>
|
||||
<hector-core.version>2.0-0</hector-core.version>
|
||||
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -16,6 +16,11 @@
|
|||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
<cosmodb.version>2.3.0</cosmodb.version>
|
||||
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -182,6 +182,11 @@
|
|||
<dynamodblocal.version>1.11.86</dynamodblocal.version>
|
||||
<dynamodblocal.repository.url>https://s3-us-west-2.amazonaws.com/dynamodb-local/release</dynamodblocal.repository.url>
|
||||
<maven-dependency-plugin.version>3.1.1</maven-dependency-plugin.version>
|
||||
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -69,5 +69,10 @@
|
|||
<fastjson.version>1.2.47</fastjson.version>
|
||||
<spatial4j.version>0.7</spatial4j.version>
|
||||
<jts.version>1.15.0</jts.version>
|
||||
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
</project>
|
|
@ -26,4 +26,11 @@
|
|||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
|
|
@ -72,6 +72,11 @@
|
|||
<testcontainers.postgresql.version>1.10.6</testcontainers.postgresql.version>
|
||||
<postgresql.version>42.2.5</postgresql.version>
|
||||
<guava.version>21.0</guava.version>
|
||||
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -66,6 +66,11 @@
|
|||
<datasource-proxy.version>1.4.1</datasource-proxy.version>
|
||||
<guava.version>21.0</guava.version>
|
||||
<testcontainers.version>1.12.2</testcontainers.version>
|
||||
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -102,6 +102,11 @@
|
|||
<mapstruct.version>1.3.1.Final</mapstruct.version>
|
||||
<guava.version>21.0</guava.version>
|
||||
<testcontainers.version>1.12.2</testcontainers.version>
|
||||
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -72,6 +72,11 @@
|
|||
<testcontainers.postgresql.version>1.10.6</testcontainers.postgresql.version>
|
||||
<postgresql.version>42.2.5</postgresql.version>
|
||||
<guava.version>21.0</guava.version>
|
||||
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -30,4 +30,10 @@
|
|||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
</project>
|
|
@ -43,6 +43,11 @@
|
|||
|
||||
<properties>
|
||||
<datasource-proxy.version>1.4.1</datasource-proxy.version>
|
||||
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -43,5 +43,10 @@
|
|||
|
||||
<properties>
|
||||
<guava.version>29.0-jre</guava.version>
|
||||
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
</project>
|
|
@ -49,4 +49,10 @@
|
|||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
</project>
|
|
@ -28,4 +28,10 @@
|
|||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
</project>
|
|
@ -107,6 +107,11 @@
|
|||
<mongodb-reactivestreams.version>4.1.0</mongodb-reactivestreams.version>
|
||||
<projectreactor.version>3.2.0.RELEASE</projectreactor.version>
|
||||
<mongodb-driver.version>4.0.5</mongodb-driver.version>
|
||||
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -98,6 +98,11 @@
|
|||
<cglib.version>3.2.4</cglib.version>
|
||||
<nosqlunit.version>0.10.0</nosqlunit.version>
|
||||
<embedded-redis.version>0.6</embedded-redis.version>
|
||||
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -45,6 +45,11 @@
|
|||
|
||||
<properties>
|
||||
<spring-data-solr.version>2.0.5.RELEASE</spring-data-solr.version>
|
||||
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -156,6 +156,11 @@
|
|||
|
||||
<!-- util -->
|
||||
<guava.version>19.0</guava.version>
|
||||
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -37,5 +37,10 @@
|
|||
|
||||
<properties>
|
||||
<spring-data-jdbc.version>2.0.3.RELEASE</spring-data-jdbc.version>
|
||||
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
</project>
|
|
@ -0,0 +1,7 @@
|
|||
package com.baeldung.spring.jdbc.template.testing;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class EmployeeApplication {
|
||||
}
|
|
@ -13,6 +13,10 @@ public class EmployeeDAO {
|
|||
jdbcTemplate = new JdbcTemplate(dataSource);
|
||||
}
|
||||
|
||||
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
}
|
||||
|
||||
public int getCountOfEmployees() {
|
||||
return jdbcTemplate.queryForObject("SELECT COUNT(*) FROM EMPLOYEE", Integer.class);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung.spring.jdbc.template.testing;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.test.context.jdbc.Sql;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
@JdbcTest
|
||||
@Sql({"schema.sql", "test-data.sql"})
|
||||
class EmployeeDAOIntegrationTest {
|
||||
@Autowired
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
|
||||
@Test
|
||||
void whenInjectInMemoryDataSource_thenReturnCorrectEmployeeCount() {
|
||||
EmployeeDAO employeeDAO = new EmployeeDAO();
|
||||
employeeDAO.setJdbcTemplate(jdbcTemplate);
|
||||
|
||||
assertEquals(4, employeeDAO.getCountOfEmployees());
|
||||
}
|
||||
}
|
2
pom.xml
2
pom.xml
|
@ -651,7 +651,6 @@
|
|||
<module>spring-dispatcher-servlet</module>
|
||||
<module>spring-drools</module>
|
||||
|
||||
<module>spring-ehcache</module>
|
||||
<module>spring-ejb</module>
|
||||
<module>spring-exceptions</module>
|
||||
|
||||
|
@ -1152,7 +1151,6 @@
|
|||
<module>spring-dispatcher-servlet</module>
|
||||
<module>spring-drools</module>
|
||||
|
||||
<module>spring-ehcache</module>
|
||||
<module>spring-ejb</module>
|
||||
<module>spring-exceptions</module>
|
||||
|
||||
|
|
|
@ -78,7 +78,6 @@
|
|||
</build>
|
||||
|
||||
<properties>
|
||||
<spring-boot.version>2.2.6.RELEASE</spring-boot.version>
|
||||
<custom.property>Custom Property Value</custom.property>
|
||||
<apache-maven.version>2.7</apache-maven.version>
|
||||
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
|
||||
|
|
|
@ -5,3 +5,4 @@
|
|||
- [Cache Eviction in Spring Boot](https://www.baeldung.com/spring-boot-evict-cache)
|
||||
- [Using Multiple Cache Managers in Spring](https://www.baeldung.com/spring-multiple-cache-managers)
|
||||
- [Testing @Cacheable on Spring Data Repositories](https://www.baeldung.com/spring-data-testing-cacheable)
|
||||
- [Spring Boot Ehcache Example](https://www.baeldung.com/spring-boot-ehcache)
|
||||
|
|
|
@ -36,6 +36,10 @@
|
|||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.cache</groupId>
|
||||
<artifactId>cache-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.ehcache</groupId>
|
||||
<artifactId>ehcache</artifactId>
|
||||
|
|
|
@ -6,13 +6,15 @@ import lombok.NoArgsConstructor;
|
|||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.UUID;
|
||||
|
||||
@Data
|
||||
@Entity
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Book {
|
||||
public class Book implements Serializable {
|
||||
|
||||
@Id
|
||||
private UUID id;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
spring.datasource.url=jdbc:h2:mem:testdb
|
||||
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_ON_EXIT=FALSE
|
||||
spring.datasource.driverClassName=org.h2.Driver
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=
|
||||
|
@ -6,3 +6,6 @@ spring.datasource.password=
|
|||
# Enabling H2 Console
|
||||
spring.h2.console.enabled=true
|
||||
spring.h2.console.path=/h2
|
||||
|
||||
#ehcache
|
||||
spring.cache.jcache.config=classpath:ehcache.xml
|
||||
|
|
|
@ -27,5 +27,28 @@
|
|||
<offheap unit="MB">10</offheap>
|
||||
</resources>
|
||||
</cache>
|
||||
|
||||
<cache alias="books">
|
||||
<key-type>java.lang.String</key-type>
|
||||
<value-type>com.baeldung.springdatacaching.model.Book</value-type>
|
||||
<expiry>
|
||||
<ttl unit="seconds">30</ttl>
|
||||
</expiry>
|
||||
|
||||
<listeners>
|
||||
<listener>
|
||||
<class>com.baeldung.cachetest.config.CacheEventLogger</class>
|
||||
<event-firing-mode>ASYNCHRONOUS</event-firing-mode>
|
||||
<event-ordering-mode>UNORDERED</event-ordering-mode>
|
||||
<events-to-fire-on>CREATED</events-to-fire-on>
|
||||
<events-to-fire-on>EXPIRED</events-to-fire-on>
|
||||
</listener>
|
||||
</listeners>
|
||||
|
||||
<resources>
|
||||
<heap unit="entries">2</heap>
|
||||
<offheap unit="MB">10</offheap>
|
||||
</resources>
|
||||
</cache>
|
||||
|
||||
</config>
|
|
@ -1,13 +0,0 @@
|
|||
*.class
|
||||
|
||||
#folders#
|
||||
/target
|
||||
/neoDb*
|
||||
/data
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
|
@ -1,8 +0,0 @@
|
|||
## Spring Ehcache
|
||||
|
||||
This module contains articles about Spring with Ehcache
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [Spring Boot Ehcache Example](https://www.baeldung.com/spring-boot-ehcache)
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE module PUBLIC
|
||||
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
|
||||
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
|
||||
<module name="Checker">
|
||||
<module name="TreeWalker">
|
||||
<module name="AvoidStarImport">
|
||||
<property name="severity" value="warning" />
|
||||
</module>
|
||||
</module>
|
||||
</module>
|
|
@ -1,87 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spring-ehcache</artifactId>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
<name>spring-ehcache</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-cache</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.cache</groupId>
|
||||
<artifactId>cache-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.ehcache</groupId>
|
||||
<artifactId>ehcache</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>spring-ehcache</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-checkstyle-plugin</artifactId>
|
||||
<version>${checkstyle-maven-plugin.version}</version>
|
||||
<configuration>
|
||||
<configLocation>checkstyle.xml</configLocation>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>check</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<reporting>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-checkstyle-plugin</artifactId>
|
||||
<version>${checkstyle-maven-plugin.version}</version>
|
||||
<configuration>
|
||||
<configLocation>checkstyle.xml</configLocation>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</reporting>
|
||||
|
||||
<properties>
|
||||
<!-- Maven plugins -->
|
||||
<checkstyle-maven-plugin.version>3.0.0</checkstyle-maven-plugin.version>
|
||||
<dependency.locations.enabled>false</dependency.locations.enabled>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -1 +0,0 @@
|
|||
spring.cache.jcache.config=classpath:ehcache.xml
|
|
@ -84,6 +84,11 @@
|
|||
<artifactId>spring-context-support</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-freemarker</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- groovy template dependency -->
|
||||
<dependency>
|
||||
|
@ -173,6 +178,7 @@
|
|||
<scribejava.version>5.1.0</scribejava.version>
|
||||
<json.version>20180130</json.version>
|
||||
<javax.mail.version>1.6.1</javax.mail.version>
|
||||
<spring-boot.version>2.3.4.RELEASE</spring-boot.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -11,7 +11,7 @@ import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
|||
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
@ComponentScan(basePackages = { "com.baeldung.controller.controller", "com.baeldung.controller", "com.baeldung.controller.config" })
|
||||
@ComponentScan(basePackages = { "com.baeldung.controller", "com.baeldung.optionalpathvars" })
|
||||
public class WebConfig implements WebMvcConfigurer {
|
||||
@Override
|
||||
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
|
||||
|
|
|
@ -27,6 +27,12 @@
|
|||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>${h2.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
|
@ -50,8 +56,6 @@
|
|||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<spring-boot-version>2.1.9.RELEASE</spring-boot-version>
|
||||
<spring.boot.starter.version>2.1.9.RELEASE</spring.boot.starter.version>
|
||||
<testcontainers.version>1.12.2</testcontainers.version>
|
||||
</properties>
|
||||
</project>
|
Loading…
Reference in New Issue