merged Alex changes
This commit is contained in:
@@ -0,0 +1 @@
|
||||
target/
|
||||
@@ -8,18 +8,42 @@
|
||||
<artifactId>apache-cxf</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
<properties>
|
||||
<cxf.version>3.1.6</cxf.version>
|
||||
<spring.version>4.3.1.RELEASE</spring.version>
|
||||
<surefire.version>2.19.1</surefire.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.cxf</groupId>
|
||||
<artifactId>cxf-rt-frontend-jaxws</artifactId>
|
||||
<version>${cxf.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.cxf</groupId>
|
||||
<artifactId>cxf-rt-transports-http-jetty</artifactId>
|
||||
<version>${cxf.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>3.1.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<version>2.6</version>
|
||||
<configuration>
|
||||
<webXml>src/main/webapp/WEB-INF/web.xml</webXml>
|
||||
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
@@ -33,6 +57,7 @@
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>integration</id>
|
||||
@@ -41,16 +66,16 @@
|
||||
<plugin>
|
||||
<groupId>org.codehaus.cargo</groupId>
|
||||
<artifactId>cargo-maven2-plugin</artifactId>
|
||||
<version>1.5.0</version>
|
||||
<version>1.4.19</version>
|
||||
<configuration>
|
||||
<container>
|
||||
<containerId>jetty9x</containerId>
|
||||
<containerId>tomcat8x</containerId>
|
||||
<type>embedded</type>
|
||||
</container>
|
||||
<configuration>
|
||||
<properties>
|
||||
<cargo.hostname>localhost</cargo.hostname>
|
||||
<cargo.servlet.port>8080</cargo.servlet.port>
|
||||
<cargo.servlet.port>8081</cargo.servlet.port>
|
||||
</properties>
|
||||
</configuration>
|
||||
</configuration>
|
||||
@@ -71,6 +96,7 @@
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${surefire.version}</version>
|
||||
@@ -91,27 +117,13 @@
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
|
||||
</profiles>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.cxf</groupId>
|
||||
<artifactId>cxf-rt-frontend-jaxws</artifactId>
|
||||
<version>${cxf.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.cxf</groupId>
|
||||
<artifactId>cxf-rt-transports-http-jetty</artifactId>
|
||||
<version>${cxf.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-web</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<cxf.version>3.1.6</cxf.version>
|
||||
<spring.version>4.3.1.RELEASE</spring.version>
|
||||
<surefire.version>2.19.1</surefire.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.cxf.spring;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletRegistration;
|
||||
|
||||
import org.apache.cxf.transport.servlet.CXFServlet;
|
||||
import org.springframework.web.WebApplicationInitializer;
|
||||
import org.springframework.web.context.ContextLoaderListener;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
|
||||
public class AppInitializer implements WebApplicationInitializer {
|
||||
@Override
|
||||
public void onStartup(ServletContext container) {
|
||||
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
|
||||
context.register(ServiceConfiguration.class);
|
||||
container.addListener(new ContextLoaderListener(context));
|
||||
|
||||
ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new CXFServlet());
|
||||
dispatcher.addMapping("/services/*");
|
||||
}
|
||||
}
|
||||
@@ -5,5 +5,6 @@ import javax.jws.WebService;
|
||||
@WebService
|
||||
public interface Baeldung {
|
||||
String hello(String name);
|
||||
|
||||
String register(Student student);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.cxf.spring;
|
||||
|
||||
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class ClientConfiguration {
|
||||
@Bean(name = "client")
|
||||
public Object generateProxy() {
|
||||
return proxyFactoryBean().create();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JaxWsProxyFactoryBean proxyFactoryBean() {
|
||||
JaxWsProxyFactoryBean proxyFactory = new JaxWsProxyFactoryBean();
|
||||
proxyFactory.setServiceClass(Baeldung.class);
|
||||
proxyFactory.setAddress("http://localhost:8081/services/baeldung");
|
||||
return proxyFactory;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.cxf.spring;
|
||||
|
||||
import javax.xml.ws.Endpoint;
|
||||
|
||||
import org.apache.cxf.Bus;
|
||||
import org.apache.cxf.bus.spring.SpringBus;
|
||||
import org.apache.cxf.jaxws.EndpointImpl;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class ServiceConfiguration {
|
||||
@Bean(name = Bus.DEFAULT_BUS_ID)
|
||||
public SpringBus springBus() {
|
||||
return new SpringBus();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Endpoint endpoint() {
|
||||
EndpointImpl endpoint = new EndpointImpl(springBus(), new BaeldungImpl());
|
||||
endpoint.publish("http://localhost:8081/services/baeldung");
|
||||
return endpoint;
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
|
||||
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">
|
||||
<bean id="client" factory-bean="clientFactory" factory-method="create" />
|
||||
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
|
||||
<property name="serviceClass" value="com.baeldung.cxf.spring.Baeldung" />
|
||||
<property name="address" value="http://localhost:8080/services/baeldung" />
|
||||
</bean>
|
||||
</beans>
|
||||
@@ -1,7 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
|
||||
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
|
||||
<jaxws:endpoint id="baeldung"
|
||||
implementor="com.baeldung.cxf.spring.BaeldungImpl" address="http://localhost:8080/services/baeldung" />
|
||||
</beans>
|
||||
@@ -1,14 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
version="3.0"
|
||||
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
|
||||
<servlet>
|
||||
<servlet-name>cxf</servlet-name>
|
||||
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
|
||||
<load-on-startup>1</load-on-startup>
|
||||
</servlet>
|
||||
<servlet-mapping>
|
||||
<servlet-name>cxf</servlet-name>
|
||||
<url-pattern>/services/*</url-pattern>
|
||||
</servlet-mapping>
|
||||
</web-app>
|
||||
@@ -3,15 +3,12 @@ package com.baeldung.cxf.spring;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
|
||||
public class StudentTest {
|
||||
private ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "client-beans.xml" });
|
||||
private Baeldung baeldungProxy;
|
||||
|
||||
{
|
||||
baeldungProxy = (Baeldung) context.getBean("client");
|
||||
}
|
||||
private ApplicationContext context = new AnnotationConfigApplicationContext(ClientConfiguration.class);
|
||||
private Baeldung baeldungProxy = (Baeldung) context.getBean("client");
|
||||
|
||||
@Test
|
||||
public void whenUsingHelloMethod_thenCorrect() {
|
||||
@@ -25,7 +22,7 @@ public class StudentTest {
|
||||
Student student2 = new Student("Eve");
|
||||
String student1Response = baeldungProxy.register(student1);
|
||||
String student2Response = baeldungProxy.register(student2);
|
||||
|
||||
|
||||
assertEquals("Adam is registered student number 1", student1Response);
|
||||
assertEquals("Eve is registered student number 2", student2Response);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
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>
|
||||
|
||||
@@ -9,7 +8,7 @@
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<name>Resource vs Inject vs Autowired</name>
|
||||
<name>dependency-injection</name>
|
||||
<description>Accompanying the demonstration of the use of the annotations related to injection mechanisms, namely
|
||||
Resource, Inject, and Autowired
|
||||
</description>
|
||||
@@ -54,6 +53,7 @@
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
@@ -71,13 +71,29 @@
|
||||
</includes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<version>${maven-war-plugin.version}</version>
|
||||
<configuration>
|
||||
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<maven-war-plugin.version>2.6</maven-war-plugin.version>
|
||||
</properties>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>java.net</id>
|
||||
<url>https://maven.java.net/content/repositories/releases/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -16,15 +16,20 @@ import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class DozerTest {
|
||||
<<<<<<< HEAD
|
||||
DozerBeanMapper mapper = new DozerBeanMapper();
|
||||
private final long GMT_DIFFERENCE=46800000;
|
||||
=======
|
||||
|
||||
private DozerBeanMapper mapper = new DozerBeanMapper();
|
||||
>>>>>>> 6f2ccdf18729969951fc37e635d24c30dd9b43d5
|
||||
|
||||
@Before
|
||||
public void before() throws Exception {
|
||||
mapper = new DozerBeanMapper();
|
||||
}
|
||||
|
||||
BeanMappingBuilder builder = new BeanMappingBuilder() {
|
||||
private BeanMappingBuilder builder = new BeanMappingBuilder() {
|
||||
|
||||
@Override
|
||||
protected void configure() {
|
||||
@@ -33,7 +38,7 @@ public class DozerTest {
|
||||
|
||||
}
|
||||
};
|
||||
BeanMappingBuilder builderMinusAge = new BeanMappingBuilder() {
|
||||
private BeanMappingBuilder builderMinusAge = new BeanMappingBuilder() {
|
||||
|
||||
@Override
|
||||
protected void configure() {
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
<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/maven-v4_0_0.xsd">
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>hystrix</artifactId>
|
||||
<version>1.0</version>
|
||||
|
||||
<name>hystrix</name>
|
||||
|
||||
<properties>
|
||||
|
||||
<!-- General -->
|
||||
<java.version>1.8</java.version>
|
||||
|
||||
<!-- Hystrix -->
|
||||
<hystrix-core.version>1.4.10</hystrix-core.version>
|
||||
<rxjava-core.version>0.20.7</rxjava-core.version>
|
||||
|
||||
<!-- Testing -->
|
||||
<hamcrest-all.version>1.3</hamcrest-all.version>
|
||||
<junit.version>4.12</junit.version>
|
||||
|
||||
<!-- maven plugins -->
|
||||
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
|
||||
<maven-war-plugin.version>2.6</maven-war-plugin.version>
|
||||
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
||||
<maven-resources-plugin.version>2.7</maven-resources-plugin.version>
|
||||
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.netflix.hystrix</groupId>
|
||||
<artifactId>hystrix-core</artifactId>
|
||||
<version>${hystrix-core.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.netflix.rxjava</groupId>
|
||||
<artifactId>rxjava-core</artifactId>
|
||||
<version>${rxjava-core.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest-all</artifactId>
|
||||
<version>${hamcrest-all.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.hystrix;
|
||||
|
||||
|
||||
public class RemoteServiceSimulator {
|
||||
|
||||
public String checkSomething(final long timeout) throws InterruptedException {
|
||||
|
||||
System.out.print(String.format("Waiting %sms. ", timeout));
|
||||
|
||||
// to simulate a real world delay in processing.
|
||||
Thread.sleep(timeout);
|
||||
|
||||
return "Done waiting.";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.hystrix;
|
||||
|
||||
import com.netflix.hystrix.HystrixCommand;
|
||||
import com.netflix.hystrix.HystrixCommandGroupKey;
|
||||
|
||||
class CommandHelloWorld extends HystrixCommand<String> {
|
||||
|
||||
private final String name;
|
||||
|
||||
CommandHelloWorld(String name) {
|
||||
super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"));
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String run() {
|
||||
return "Hello " + name + "!";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.baeldung.hystrix;
|
||||
|
||||
import com.netflix.hystrix.*;
|
||||
import com.netflix.hystrix.collapser.RequestCollapserFactory;
|
||||
import com.netflix.hystrix.exception.HystrixRuntimeException;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
public class HystrixTimeoutTest {
|
||||
|
||||
private static HystrixCommand.Setter config;
|
||||
private static HystrixCommandProperties.Setter commandProperties = HystrixCommandProperties.Setter();
|
||||
|
||||
|
||||
@Rule
|
||||
public final ExpectedException exception = ExpectedException.none();
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
config = HystrixCommand
|
||||
.Setter
|
||||
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroup1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInputBob_andDefaultSettings_thenReturnHelloBob(){
|
||||
assertThat(new CommandHelloWorld("Bob").execute(), equalTo("Hello Bob!"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTimeoutEqualTo100_andDefaultSettings_thenReturnSuccess() throws InterruptedException {
|
||||
assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(100)).execute(), equalTo("Success"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTimeoutEqualTo10000_andDefaultSettings_thenExpectHystrixRuntimeException() throws InterruptedException {
|
||||
exception.expect(HystrixRuntimeException.class);
|
||||
new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(10_000)).execute();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTimeoutEqualTo5000_andExecutionTimeoutEqualTo10000_thenReturnSuccess() throws InterruptedException {
|
||||
commandProperties.withExecutionTimeoutInMilliseconds(10_000);
|
||||
config.andCommandPropertiesDefaults(commandProperties);
|
||||
assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(5_000)).execute(), equalTo("Success"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTimeoutEqualTo15000_andExecutionTimeoutEqualTo10000_thenExpectHystrixRuntimeException() throws InterruptedException {
|
||||
exception.expect(HystrixRuntimeException.class);
|
||||
commandProperties.withExecutionTimeoutInMilliseconds(10_000);
|
||||
config.andCommandPropertiesDefaults(commandProperties);
|
||||
new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(15_000)).execute();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.hystrix;
|
||||
|
||||
import com.netflix.hystrix.HystrixCommand;
|
||||
import com.netflix.hystrix.HystrixCommandGroupKey;
|
||||
|
||||
|
||||
class RemoteServiceTestCommand extends HystrixCommand<String> {
|
||||
|
||||
private final RemoteServiceTestSimulator remoteService;
|
||||
|
||||
RemoteServiceTestCommand(Setter config, RemoteServiceTestSimulator remoteService) {
|
||||
super(config);
|
||||
this.remoteService = remoteService;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String run() throws Exception {
|
||||
return remoteService.execute();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.hystrix;
|
||||
|
||||
|
||||
class RemoteServiceTestSimulator {
|
||||
|
||||
private long wait;
|
||||
|
||||
RemoteServiceTestSimulator(long wait) throws InterruptedException {
|
||||
this.wait = wait;
|
||||
}
|
||||
|
||||
String execute() throws InterruptedException {
|
||||
Thread.sleep(wait);
|
||||
return "Success";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?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>
|
||||
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>immutables</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.immutables</groupId>
|
||||
<artifactId>value</artifactId>
|
||||
<version>2.2.10</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>3.5.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mutabilitydetector</groupId>
|
||||
<artifactId>MutabilityDetector</artifactId>
|
||||
<version>0.9.5</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.3</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.immutable;
|
||||
|
||||
import org.immutables.value.Value;
|
||||
|
||||
@Value.Immutable
|
||||
public interface Address {
|
||||
String getStreetName();
|
||||
Integer getNumber();
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.immutable;
|
||||
|
||||
import org.immutables.value.Value;
|
||||
|
||||
@Value.Immutable
|
||||
public abstract class Person {
|
||||
abstract String getName();
|
||||
abstract Integer getAge();
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.immutable.auxiliary;
|
||||
|
||||
|
||||
import org.immutables.value.Value;
|
||||
|
||||
@Value.Immutable
|
||||
public abstract class Person {
|
||||
abstract String getName();
|
||||
abstract Integer getAge();
|
||||
|
||||
@Value.Auxiliary
|
||||
abstract String getAuxiliaryField();
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.immutable.default_;
|
||||
|
||||
import org.immutables.value.Value;
|
||||
|
||||
@Value.Immutable(prehash = true)
|
||||
public abstract class Person {
|
||||
|
||||
abstract String getName();
|
||||
|
||||
@Value.Default
|
||||
Integer getAge() {
|
||||
return 42;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.immutable.parameter;
|
||||
|
||||
|
||||
import org.immutables.value.Value;
|
||||
|
||||
@Value.Immutable
|
||||
public abstract class Person {
|
||||
|
||||
@Value.Parameter
|
||||
abstract String getName();
|
||||
|
||||
@Value.Parameter
|
||||
abstract Integer getAge();
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.immutable.prehash;
|
||||
|
||||
import org.immutables.value.Value;
|
||||
|
||||
@Value.Immutable(prehash = true)
|
||||
public abstract class Person {
|
||||
abstract String getName();
|
||||
abstract Integer getAge();
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.immutable;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mutabilitydetector.unittesting.MutabilityAssert.assertImmutable;
|
||||
|
||||
public class ImmutablePersonTest {
|
||||
|
||||
@Test
|
||||
public void whenModifying_shouldCreateNewInstance() throws Exception {
|
||||
final ImmutablePerson john = ImmutablePerson.builder()
|
||||
.age(42)
|
||||
.name("John")
|
||||
.build();
|
||||
|
||||
final ImmutablePerson john43 = john.withAge(43);
|
||||
|
||||
assertThat(john)
|
||||
.isNotSameAs(john43);
|
||||
|
||||
assertThat(john.getAge())
|
||||
.isEqualTo(42);
|
||||
|
||||
assertImmutable(ImmutablePerson.class);
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.immutable.auxiliary;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ImmutablePersonAuxiliaryTest {
|
||||
|
||||
@Test
|
||||
public void whenComparing_shouldIgnore() throws Exception {
|
||||
final ImmutablePerson john1 = ImmutablePerson.builder()
|
||||
.name("John")
|
||||
.age(42)
|
||||
.auxiliaryField("Value1")
|
||||
.build();
|
||||
|
||||
final ImmutablePerson john2 = ImmutablePerson.builder()
|
||||
.name("John")
|
||||
.age(42)
|
||||
.auxiliaryField("Value2")
|
||||
.build();
|
||||
|
||||
|
||||
assertThat(john1.equals(john2))
|
||||
.isTrue();
|
||||
|
||||
assertThat(john1.toString())
|
||||
.isEqualTo(john2.toString());
|
||||
|
||||
assertThat(john1.hashCode())
|
||||
.isEqualTo(john2.hashCode());
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.immutable.default_;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ImmutablePersonDefaultTest {
|
||||
|
||||
@Test
|
||||
public void whenInstantiating_shouldUseDefaultValue() throws Exception {
|
||||
|
||||
final ImmutablePerson john = ImmutablePerson.builder().name("John").build();
|
||||
|
||||
assertThat(john.getAge()).isEqualTo(42);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -7,9 +7,19 @@ import java.util.List;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
|
||||
|
||||
public class CustomListDeserializer extends JsonDeserializer<List<ItemWithSerializer>> {
|
||||
public class CustomListDeserializer extends StdDeserializer<List<ItemWithSerializer>> {
|
||||
|
||||
private static final long serialVersionUID = 1095767961632979804L;
|
||||
|
||||
public CustomListDeserializer() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public CustomListDeserializer(final Class<?> vc) {
|
||||
super(vc);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ItemWithSerializer> deserialize(final JsonParser jsonparser, final DeserializationContext context) throws IOException, JsonProcessingException {
|
||||
|
||||
@@ -6,11 +6,20 @@ import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
||||
|
||||
public class CustomListSerializer extends JsonSerializer<List<ItemWithSerializer>> {
|
||||
public class CustomListSerializer extends StdSerializer<List<ItemWithSerializer>> {
|
||||
|
||||
private static final long serialVersionUID = 3698763098000900856L;
|
||||
|
||||
public CustomListSerializer() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public CustomListSerializer(final Class<List<ItemWithSerializer>> t) {
|
||||
super(t);
|
||||
}
|
||||
@Override
|
||||
public void serialize(final List<ItemWithSerializer> items, final JsonGenerator generator, final SerializerProvider provider) throws IOException, JsonProcessingException {
|
||||
final List<Integer> ids = new ArrayList<Integer>();
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user