Merge remote-tracking branch 'upstream/master'
This commit is contained in:
commit
5744df67a0
|
@ -0,0 +1,3 @@
|
||||||
|
[submodule "testgitrepo"]
|
||||||
|
path = testgitrepo
|
||||||
|
url = /home/prd/Development/projects/idea/tutorials/spring-boot/src/main/resources/testgitrepo/
|
|
@ -0,0 +1,50 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<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>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
<artifactId>annotations</artifactId>
|
||||||
|
<relativePath>../</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>annotation-processing</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<auto-service.version>1.0-rc2</auto-service.version>
|
||||||
|
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.auto.service</groupId>
|
||||||
|
<artifactId>auto-service</artifactId>
|
||||||
|
<version>${auto-service.version}</version>
|
||||||
|
<scope>provided</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,132 @@
|
||||||
|
package com.baeldung.annotation.processor;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import javax.annotation.processing.*;
|
||||||
|
import javax.lang.model.SourceVersion;
|
||||||
|
import javax.lang.model.element.Element;
|
||||||
|
import javax.lang.model.element.TypeElement;
|
||||||
|
import javax.lang.model.type.ExecutableType;
|
||||||
|
import javax.tools.Diagnostic;
|
||||||
|
import javax.tools.JavaFileObject;
|
||||||
|
|
||||||
|
import com.google.auto.service.AutoService;
|
||||||
|
|
||||||
|
@SupportedAnnotationTypes("com.baeldung.annotation.processor.BuilderProperty")
|
||||||
|
@SupportedSourceVersion(SourceVersion.RELEASE_8)
|
||||||
|
@AutoService(Processor.class)
|
||||||
|
public class BuilderProcessor extends AbstractProcessor {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
||||||
|
for (TypeElement annotation : annotations) {
|
||||||
|
|
||||||
|
Set<? extends Element> annotatedElements = roundEnv.getElementsAnnotatedWith(annotation);
|
||||||
|
|
||||||
|
Map<Boolean, List<Element>> annotatedMethods = annotatedElements.stream()
|
||||||
|
.collect(Collectors.partitioningBy(element ->
|
||||||
|
((ExecutableType) element.asType()).getParameterTypes().size() == 1
|
||||||
|
&& element.getSimpleName().toString().startsWith("set")));
|
||||||
|
|
||||||
|
List<Element> setters = annotatedMethods.get(true);
|
||||||
|
List<Element> otherMethods = annotatedMethods.get(false);
|
||||||
|
|
||||||
|
otherMethods.forEach(element ->
|
||||||
|
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
|
||||||
|
"@BuilderProperty must be applied to a setXxx method with a single argument", element));
|
||||||
|
|
||||||
|
if (setters.isEmpty()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
String className = ((TypeElement) setters.get(0).getEnclosingElement()).getQualifiedName().toString();
|
||||||
|
|
||||||
|
Map<String, String> setterMap = setters.stream().collect(Collectors.toMap(
|
||||||
|
setter -> setter.getSimpleName().toString(),
|
||||||
|
setter -> ((ExecutableType) setter.asType())
|
||||||
|
.getParameterTypes().get(0).toString()
|
||||||
|
));
|
||||||
|
|
||||||
|
try {
|
||||||
|
writeBuilderFile(className, setterMap);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void writeBuilderFile(String className, Map<String, String> setterMap) throws IOException {
|
||||||
|
|
||||||
|
String packageName = null;
|
||||||
|
int lastDot = className.lastIndexOf('.');
|
||||||
|
if (lastDot > 0) {
|
||||||
|
packageName = className.substring(0, lastDot);
|
||||||
|
}
|
||||||
|
|
||||||
|
String simpleClassName = className.substring(lastDot + 1);
|
||||||
|
String builderClassName = className + "Builder";
|
||||||
|
String builderSimpleClassName = builderClassName.substring(lastDot + 1);
|
||||||
|
|
||||||
|
JavaFileObject builderFile = processingEnv.getFiler().createSourceFile(builderClassName);
|
||||||
|
try (PrintWriter out = new PrintWriter(builderFile.openWriter())) {
|
||||||
|
|
||||||
|
if (packageName != null) {
|
||||||
|
out.print("package ");
|
||||||
|
out.print(packageName);
|
||||||
|
out.println(";");
|
||||||
|
out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
out.print("public class ");
|
||||||
|
out.print(builderSimpleClassName);
|
||||||
|
out.println(" {");
|
||||||
|
out.println();
|
||||||
|
|
||||||
|
out.print(" private ");
|
||||||
|
out.print(simpleClassName);
|
||||||
|
out.print(" object = new ");
|
||||||
|
out.print(simpleClassName);
|
||||||
|
out.println("();");
|
||||||
|
out.println();
|
||||||
|
|
||||||
|
out.print(" public ");
|
||||||
|
out.print(simpleClassName);
|
||||||
|
out.println(" build() {");
|
||||||
|
out.println(" return object;");
|
||||||
|
out.println(" }");
|
||||||
|
out.println();
|
||||||
|
|
||||||
|
setterMap.entrySet().forEach(setter -> {
|
||||||
|
String methodName = setter.getKey();
|
||||||
|
String argumentType = setter.getValue();
|
||||||
|
|
||||||
|
out.print(" public ");
|
||||||
|
out.print(builderSimpleClassName);
|
||||||
|
out.print(" ");
|
||||||
|
out.print(methodName);
|
||||||
|
|
||||||
|
out.print("(");
|
||||||
|
|
||||||
|
out.print(argumentType);
|
||||||
|
out.println(" value) {");
|
||||||
|
out.print(" object.");
|
||||||
|
out.print(methodName);
|
||||||
|
out.println("(value);");
|
||||||
|
out.println(" return this;");
|
||||||
|
out.println(" }");
|
||||||
|
out.println();
|
||||||
|
});
|
||||||
|
|
||||||
|
out.println("}");
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
package com.baeldung.annotation.processor;
|
||||||
|
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
|
@Target(ElementType.METHOD)
|
||||||
|
@Retention(RetentionPolicy.SOURCE)
|
||||||
|
public @interface BuilderProperty {
|
||||||
|
}
|
|
@ -0,0 +1,51 @@
|
||||||
|
<?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>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<artifactId>annotations</artifactId>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
<relativePath>../</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>annotation-user</artifactId>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>annotation-processing</artifactId>
|
||||||
|
<version>${project.parent.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.12</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
|
||||||
|
<plugins>
|
||||||
|
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.5.1</version>
|
||||||
|
<configuration>
|
||||||
|
<source>1.8</source>
|
||||||
|
<target>1.8</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
|
</plugins>
|
||||||
|
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,29 @@
|
||||||
|
package com.baeldung.annotation;
|
||||||
|
|
||||||
|
import com.baeldung.annotation.processor.BuilderProperty;
|
||||||
|
|
||||||
|
public class Person {
|
||||||
|
|
||||||
|
private int age;
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public int getAge() {
|
||||||
|
return age;
|
||||||
|
}
|
||||||
|
|
||||||
|
@BuilderProperty
|
||||||
|
public void setAge(int age) {
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@BuilderProperty
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
package com.baeldung.annotation;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
public class PersonBuilderTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenBuildPersonWithBuilder_thenObjectHasPropertyValues() {
|
||||||
|
|
||||||
|
Person person = new PersonBuilder()
|
||||||
|
.setAge(25)
|
||||||
|
.setName("John")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
assertEquals(25, person.getAge());
|
||||||
|
assertEquals("John", person.getName());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,20 @@
|
||||||
|
<?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">
|
||||||
|
<parent>
|
||||||
|
<artifactId>parent-modules</artifactId>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>annotations</artifactId>
|
||||||
|
<packaging>pom</packaging>
|
||||||
|
|
||||||
|
<modules>
|
||||||
|
<module>annotation-processing</module>
|
||||||
|
<module>annotation-user</module>
|
||||||
|
</modules>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,2 @@
|
||||||
|
### Relevant Articles:
|
||||||
|
- [Java Annotation Processing and Creating a Builder](http://www.baeldung.com/java-annotation-processing-builder)
|
|
@ -0,0 +1,2 @@
|
||||||
|
### Relevant Articles:
|
||||||
|
- [Introduction to Apache CXF](http://www.baeldung.com/introduction-to-apache-cxf)
|
|
@ -11,6 +11,7 @@
|
||||||
</parent>
|
</parent>
|
||||||
<properties>
|
<properties>
|
||||||
<cxf.version>3.1.6</cxf.version>
|
<cxf.version>3.1.6</cxf.version>
|
||||||
|
<surefire.version>2.19.1</surefire.version>
|
||||||
</properties>
|
</properties>
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
@ -26,7 +27,7 @@
|
||||||
<version>2.19.1</version>
|
<version>2.19.1</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<excludes>
|
<excludes>
|
||||||
<exclude>**/StudentTest.java</exclude>
|
<exclude>**/*LiveTest.java</exclude>
|
||||||
</excludes>
|
</excludes>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
@ -44,4 +45,5 @@
|
||||||
<version>${cxf.version}</version>
|
<version>${cxf.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -2,20 +2,16 @@ package com.baeldung.cxf.introduction;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.xml.namespace.QName;
|
import javax.xml.namespace.QName;
|
||||||
import javax.xml.ws.Service;
|
import javax.xml.ws.Service;
|
||||||
import javax.xml.ws.soap.SOAPBinding;
|
import javax.xml.ws.soap.SOAPBinding;
|
||||||
|
|
||||||
import com.baeldung.cxf.introduction.Baeldung;
|
import org.junit.Before;
|
||||||
import com.baeldung.cxf.introduction.Student;
|
import org.junit.Test;
|
||||||
import com.baeldung.cxf.introduction.StudentImpl;
|
|
||||||
|
|
||||||
public class StudentTest {
|
public class StudentLiveTest {
|
||||||
private static QName SERVICE_NAME = new QName("http://introduction.cxf.baeldung.com/", "Baeldung");
|
private static QName SERVICE_NAME = new QName("http://introduction.cxf.baeldung.com/", "Baeldung");
|
||||||
private static QName PORT_NAME = new QName("http://introduction.cxf.baeldung.com/", "BaeldungPort");
|
private static QName PORT_NAME = new QName("http://introduction.cxf.baeldung.com/", "BaeldungPort");
|
||||||
|
|
||||||
|
@ -25,7 +21,7 @@ public class StudentTest {
|
||||||
|
|
||||||
{
|
{
|
||||||
service = Service.create(SERVICE_NAME);
|
service = Service.create(SERVICE_NAME);
|
||||||
String endpointAddress = "http://localhost:8080/baeldung";
|
final String endpointAddress = "http://localhost:8080/baeldung";
|
||||||
service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);
|
service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,28 +33,28 @@ public class StudentTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenUsingHelloMethod_thenCorrect() {
|
public void whenUsingHelloMethod_thenCorrect() {
|
||||||
String endpointResponse = baeldungProxy.hello("Baeldung");
|
final String endpointResponse = baeldungProxy.hello("Baeldung");
|
||||||
String localResponse = baeldungImpl.hello("Baeldung");
|
final String localResponse = baeldungImpl.hello("Baeldung");
|
||||||
assertEquals(localResponse, endpointResponse);
|
assertEquals(localResponse, endpointResponse);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenUsingHelloStudentMethod_thenCorrect() {
|
public void whenUsingHelloStudentMethod_thenCorrect() {
|
||||||
Student student = new StudentImpl("John Doe");
|
final Student student = new StudentImpl("John Doe");
|
||||||
String endpointResponse = baeldungProxy.helloStudent(student);
|
final String endpointResponse = baeldungProxy.helloStudent(student);
|
||||||
String localResponse = baeldungImpl.helloStudent(student);
|
final String localResponse = baeldungImpl.helloStudent(student);
|
||||||
assertEquals(localResponse, endpointResponse);
|
assertEquals(localResponse, endpointResponse);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void usingGetStudentsMethod_thenCorrect() {
|
public void usingGetStudentsMethod_thenCorrect() {
|
||||||
Student student1 = new StudentImpl("Adam");
|
final Student student1 = new StudentImpl("Adam");
|
||||||
baeldungProxy.helloStudent(student1);
|
baeldungProxy.helloStudent(student1);
|
||||||
|
|
||||||
Student student2 = new StudentImpl("Eve");
|
final Student student2 = new StudentImpl("Eve");
|
||||||
baeldungProxy.helloStudent(student2);
|
baeldungProxy.helloStudent(student2);
|
||||||
|
|
||||||
Map<Integer, Student> students = baeldungProxy.getStudents();
|
final Map<Integer, Student> students = baeldungProxy.getStudents();
|
||||||
assertEquals("Adam", students.get(1).getName());
|
assertEquals("Adam", students.get(1).getName());
|
||||||
assertEquals("Eve", students.get(2).getName());
|
assertEquals("Eve", students.get(2).getName());
|
||||||
}
|
}
|
|
@ -0,0 +1,55 @@
|
||||||
|
<?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/maven-v4_0_0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>cxf-jaxrs-implementation</artifactId>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>apache-cxf</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<cxf.version>3.1.7</cxf.version>
|
||||||
|
<httpclient.version>4.5.2</httpclient.version>
|
||||||
|
<surefire.version>2.19.1</surefire.version>
|
||||||
|
</properties>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.codehaus.mojo</groupId>
|
||||||
|
<artifactId>exec-maven-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<mainClass>com.baeldung.cxf.jaxrs.implementation.RestfulServer</mainClass>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<version>2.19.1</version>
|
||||||
|
<configuration>
|
||||||
|
<excludes>
|
||||||
|
<exclude>**/*LiveTest.java</exclude>
|
||||||
|
</excludes>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.cxf</groupId>
|
||||||
|
<artifactId>cxf-rt-frontend-jaxrs</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.apache.httpcomponents</groupId>
|
||||||
|
<artifactId>httpclient</artifactId>
|
||||||
|
<version>${httpclient.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
|
@ -0,0 +1,86 @@
|
||||||
|
package com.baeldung.cxf.jaxrs.implementation;
|
||||||
|
|
||||||
|
import javax.ws.rs.*;
|
||||||
|
import javax.ws.rs.core.Response;
|
||||||
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@XmlRootElement(name = "Course")
|
||||||
|
public class Course {
|
||||||
|
private int id;
|
||||||
|
private String name;
|
||||||
|
private List<Student> students = new ArrayList<>();
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Student> getStudents() {
|
||||||
|
return students;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStudents(List<Student> students) {
|
||||||
|
this.students = students;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GET
|
||||||
|
@Path("{studentId}")
|
||||||
|
public Student getStudent(@PathParam("studentId") int studentId) {
|
||||||
|
return findById(studentId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@POST
|
||||||
|
public Response createStudent(Student student) {
|
||||||
|
for (Student element : students) {
|
||||||
|
if (element.getId() == student.getId()) {
|
||||||
|
return Response.status(Response.Status.CONFLICT).build();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
students.add(student);
|
||||||
|
return Response.ok(student).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@DELETE
|
||||||
|
@Path("{studentId}")
|
||||||
|
public Response deleteStudent(@PathParam("studentId") int studentId) {
|
||||||
|
Student student = findById(studentId);
|
||||||
|
if (student == null) {
|
||||||
|
return Response.status(Response.Status.NOT_FOUND).build();
|
||||||
|
}
|
||||||
|
students.remove(student);
|
||||||
|
return Response.ok().build();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Student findById(int id) {
|
||||||
|
for (Student student : students) {
|
||||||
|
if (student.getId() == id) {
|
||||||
|
return student;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return id + name.hashCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
return (obj instanceof Course) && (id == ((Course) obj).getId()) && (name.equals(((Course) obj).getName()));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,72 @@
|
||||||
|
package com.baeldung.cxf.jaxrs.implementation;
|
||||||
|
|
||||||
|
import javax.ws.rs.*;
|
||||||
|
import javax.ws.rs.core.Response;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Path("baeldung")
|
||||||
|
@Produces("text/xml")
|
||||||
|
public class CourseRepository {
|
||||||
|
private Map<Integer, Course> courses = new HashMap<>();
|
||||||
|
|
||||||
|
{
|
||||||
|
Student student1 = new Student();
|
||||||
|
Student student2 = new Student();
|
||||||
|
student1.setId(1);
|
||||||
|
student1.setName("Student A");
|
||||||
|
student2.setId(2);
|
||||||
|
student2.setName("Student B");
|
||||||
|
|
||||||
|
List<Student> course1Students = new ArrayList<>();
|
||||||
|
course1Students.add(student1);
|
||||||
|
course1Students.add(student2);
|
||||||
|
|
||||||
|
Course course1 = new Course();
|
||||||
|
Course course2 = new Course();
|
||||||
|
course1.setId(1);
|
||||||
|
course1.setName("REST with Spring");
|
||||||
|
course1.setStudents(course1Students);
|
||||||
|
course2.setId(2);
|
||||||
|
course2.setName("Learn Spring Security");
|
||||||
|
|
||||||
|
courses.put(1, course1);
|
||||||
|
courses.put(2, course2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GET
|
||||||
|
@Path("courses/{courseId}")
|
||||||
|
public Course getCourse(@PathParam("courseId") int courseId) {
|
||||||
|
return findById(courseId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PUT
|
||||||
|
@Path("courses/{courseId}")
|
||||||
|
public Response updateCourse(@PathParam("courseId") int courseId, Course course) {
|
||||||
|
Course existingCourse = findById(courseId);
|
||||||
|
if (existingCourse == null) {
|
||||||
|
return Response.status(Response.Status.NOT_FOUND).build();
|
||||||
|
}
|
||||||
|
if (existingCourse.equals(course)) {
|
||||||
|
return Response.notModified().build();
|
||||||
|
}
|
||||||
|
courses.put(courseId, course);
|
||||||
|
return Response.ok().build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Path("courses/{courseId}/students")
|
||||||
|
public Course pathToStudent(@PathParam("courseId") int courseId) {
|
||||||
|
return findById(courseId);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Course findById(int id) {
|
||||||
|
for (Map.Entry<Integer, Course> course : courses.entrySet()) {
|
||||||
|
if (course.getKey() == id) {
|
||||||
|
return course.getValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
package com.baeldung.cxf.jaxrs.implementation;
|
||||||
|
|
||||||
|
import org.apache.cxf.endpoint.Server;
|
||||||
|
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
|
||||||
|
import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
|
||||||
|
|
||||||
|
public class RestfulServer {
|
||||||
|
public static void main(String args[]) throws Exception {
|
||||||
|
JAXRSServerFactoryBean factoryBean = new JAXRSServerFactoryBean();
|
||||||
|
factoryBean.setResourceClasses(CourseRepository.class);
|
||||||
|
factoryBean.setResourceProvider(new SingletonResourceProvider(new CourseRepository()));
|
||||||
|
factoryBean.setAddress("http://localhost:8080/");
|
||||||
|
Server server = factoryBean.create();
|
||||||
|
|
||||||
|
System.out.println("Server ready...");
|
||||||
|
Thread.sleep(60 * 1000);
|
||||||
|
System.out.println("Server exiting");
|
||||||
|
server.destroy();
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,35 @@
|
||||||
|
package com.baeldung.cxf.jaxrs.implementation;
|
||||||
|
|
||||||
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
|
|
||||||
|
@XmlRootElement(name = "Student")
|
||||||
|
public class Student {
|
||||||
|
private int id;
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return id + name.hashCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
return (obj instanceof Student) && (id == ((Student) obj).getId()) && (name.equals(((Student) obj).getName()));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,4 @@
|
||||||
|
<Course>
|
||||||
|
<id>2</id>
|
||||||
|
<name>Apache CXF Support for RESTful</name>
|
||||||
|
</Course>
|
|
@ -0,0 +1,4 @@
|
||||||
|
<Student>
|
||||||
|
<id>2</id>
|
||||||
|
<name>Student B</name>
|
||||||
|
</Student>
|
|
@ -0,0 +1,4 @@
|
||||||
|
<Student>
|
||||||
|
<id>3</id>
|
||||||
|
<name>Student C</name>
|
||||||
|
</Student>
|
|
@ -0,0 +1,4 @@
|
||||||
|
<Course>
|
||||||
|
<id>3</id>
|
||||||
|
<name>Apache CXF Support for RESTful</name>
|
||||||
|
</Course>
|
|
@ -0,0 +1,4 @@
|
||||||
|
<Course>
|
||||||
|
<id>1</id>
|
||||||
|
<name>REST with Spring</name>
|
||||||
|
</Course>
|
|
@ -0,0 +1,130 @@
|
||||||
|
package com.baeldung.cxf.jaxrs.implementation;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.net.URL;
|
||||||
|
|
||||||
|
import javax.xml.bind.JAXB;
|
||||||
|
|
||||||
|
import org.apache.http.HttpResponse;
|
||||||
|
import org.apache.http.client.methods.HttpDelete;
|
||||||
|
import org.apache.http.client.methods.HttpPost;
|
||||||
|
import org.apache.http.client.methods.HttpPut;
|
||||||
|
import org.apache.http.entity.InputStreamEntity;
|
||||||
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
|
import org.apache.http.impl.client.HttpClients;
|
||||||
|
import org.junit.AfterClass;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class ServiceLiveTest {
|
||||||
|
private static final String BASE_URL = "http://localhost:8080/baeldung/courses/";
|
||||||
|
private static CloseableHttpClient client;
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void createClient() {
|
||||||
|
client = HttpClients.createDefault();
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterClass
|
||||||
|
public static void closeClient() throws IOException {
|
||||||
|
client.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUpdateNonExistentCourse_thenReceiveNotFoundResponse() throws IOException {
|
||||||
|
final HttpPut httpPut = new HttpPut(BASE_URL + "3");
|
||||||
|
final InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("non_existent_course.xml");
|
||||||
|
httpPut.setEntity(new InputStreamEntity(resourceStream));
|
||||||
|
httpPut.setHeader("Content-Type", "text/xml");
|
||||||
|
|
||||||
|
final HttpResponse response = client.execute(httpPut);
|
||||||
|
assertEquals(404, response.getStatusLine().getStatusCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUpdateUnchangedCourse_thenReceiveNotModifiedResponse() throws IOException {
|
||||||
|
final HttpPut httpPut = new HttpPut(BASE_URL + "1");
|
||||||
|
final InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("unchanged_course.xml");
|
||||||
|
httpPut.setEntity(new InputStreamEntity(resourceStream));
|
||||||
|
httpPut.setHeader("Content-Type", "text/xml");
|
||||||
|
|
||||||
|
final HttpResponse response = client.execute(httpPut);
|
||||||
|
assertEquals(304, response.getStatusLine().getStatusCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUpdateValidCourse_thenReceiveOKResponse() throws IOException {
|
||||||
|
final HttpPut httpPut = new HttpPut(BASE_URL + "2");
|
||||||
|
final InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("changed_course.xml");
|
||||||
|
httpPut.setEntity(new InputStreamEntity(resourceStream));
|
||||||
|
httpPut.setHeader("Content-Type", "text/xml");
|
||||||
|
|
||||||
|
final HttpResponse response = client.execute(httpPut);
|
||||||
|
assertEquals(200, response.getStatusLine().getStatusCode());
|
||||||
|
|
||||||
|
final Course course = getCourse(2);
|
||||||
|
assertEquals(2, course.getId());
|
||||||
|
assertEquals("Apache CXF Support for RESTful", course.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenCreateConflictStudent_thenReceiveConflictResponse() throws IOException {
|
||||||
|
final HttpPost httpPost = new HttpPost(BASE_URL + "1/students");
|
||||||
|
final InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("conflict_student.xml");
|
||||||
|
httpPost.setEntity(new InputStreamEntity(resourceStream));
|
||||||
|
httpPost.setHeader("Content-Type", "text/xml");
|
||||||
|
|
||||||
|
final HttpResponse response = client.execute(httpPost);
|
||||||
|
assertEquals(409, response.getStatusLine().getStatusCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenCreateValidStudent_thenReceiveOKResponse() throws IOException {
|
||||||
|
final HttpPost httpPost = new HttpPost(BASE_URL + "2/students");
|
||||||
|
final InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("created_student.xml");
|
||||||
|
httpPost.setEntity(new InputStreamEntity(resourceStream));
|
||||||
|
httpPost.setHeader("Content-Type", "text/xml");
|
||||||
|
|
||||||
|
final HttpResponse response = client.execute(httpPost);
|
||||||
|
assertEquals(200, response.getStatusLine().getStatusCode());
|
||||||
|
|
||||||
|
final Student student = getStudent(2, 3);
|
||||||
|
assertEquals(3, student.getId());
|
||||||
|
assertEquals("Student C", student.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenDeleteInvalidStudent_thenReceiveNotFoundResponse() throws IOException {
|
||||||
|
final HttpDelete httpDelete = new HttpDelete(BASE_URL + "1/students/3");
|
||||||
|
final HttpResponse response = client.execute(httpDelete);
|
||||||
|
assertEquals(404, response.getStatusLine().getStatusCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenDeleteValidStudent_thenReceiveOKResponse() throws IOException {
|
||||||
|
final HttpDelete httpDelete = new HttpDelete(BASE_URL + "1/students/1");
|
||||||
|
final HttpResponse response = client.execute(httpDelete);
|
||||||
|
assertEquals(200, response.getStatusLine().getStatusCode());
|
||||||
|
|
||||||
|
final Course course = getCourse(1);
|
||||||
|
assertEquals(1, course.getStudents().size());
|
||||||
|
assertEquals(2, course.getStudents().get(0).getId());
|
||||||
|
assertEquals("Student B", course.getStudents().get(0).getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
private Course getCourse(int courseOrder) throws IOException {
|
||||||
|
final URL url = new URL(BASE_URL + courseOrder);
|
||||||
|
final InputStream input = url.openStream();
|
||||||
|
return JAXB.unmarshal(new InputStreamReader(input), Course.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Student getStudent(int courseOrder, int studentOrder) throws IOException {
|
||||||
|
final URL url = new URL(BASE_URL + courseOrder + "/students/" + studentOrder);
|
||||||
|
final InputStream input = url.openStream();
|
||||||
|
return JAXB.unmarshal(new InputStreamReader(input), Student.class);
|
||||||
|
}
|
||||||
|
}
|
|
@ -51,7 +51,7 @@
|
||||||
<version>${surefire.version}</version>
|
<version>${surefire.version}</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<excludes>
|
<excludes>
|
||||||
<exclude>StudentTest.java</exclude>
|
<exclude>**/*LiveTest.java</exclude>
|
||||||
</excludes>
|
</excludes>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
@ -60,7 +60,7 @@
|
||||||
|
|
||||||
<profiles>
|
<profiles>
|
||||||
<profile>
|
<profile>
|
||||||
<id>integration</id>
|
<id>live</id>
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
|
|
|
@ -6,7 +6,7 @@ import org.junit.Test;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
|
|
||||||
public class StudentTest {
|
public class StudentLiveTest {
|
||||||
private ApplicationContext context = new AnnotationConfigApplicationContext(ClientConfiguration.class);
|
private ApplicationContext context = new AnnotationConfigApplicationContext(ClientConfiguration.class);
|
||||||
private Baeldung baeldungProxy = (Baeldung) context.getBean("client");
|
private Baeldung baeldungProxy = (Baeldung) context.getBean("client");
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
<modules>
|
<modules>
|
||||||
<module>cxf-introduction</module>
|
<module>cxf-introduction</module>
|
||||||
<module>cxf-spring</module>
|
<module>cxf-spring</module>
|
||||||
|
<module>cxf-jaxrs-implementation</module>
|
||||||
</modules>
|
</modules>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|
|
@ -1,7 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
|
||||||
<launchConfiguration type="org.eclipse.ant.AntBuilderLaunchConfigurationType">
|
|
||||||
<booleanAttribute key="org.eclipse.ui.externaltools.ATTR_BUILDER_ENABLED" value="false"/>
|
|
||||||
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_DISABLED_BUILDER" value="org.eclipse.wst.jsdt.core.javascriptValidator"/>
|
|
||||||
<mapAttribute key="org.eclipse.ui.externaltools.ATTR_TOOL_ARGUMENTS"/>
|
|
||||||
<booleanAttribute key="org.eclipse.ui.externaltools.ATTR_TRIGGERS_CONFIGURED" value="true"/>
|
|
||||||
</launchConfiguration>
|
|
|
@ -97,6 +97,14 @@
|
||||||
<version>8.0.2</version>
|
<version>8.0.2</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.dbdoclet</groupId>
|
||||||
|
<artifactId>herold</artifactId>
|
||||||
|
<version>6.1.0</version>
|
||||||
|
<scope>system</scope>
|
||||||
|
<systemPath>${basedir}/src/test/resources/jars/herold.jar</systemPath>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>net.sf.jtidy</groupId>
|
<groupId>net.sf.jtidy</groupId>
|
||||||
<artifactId>jtidy</artifactId>
|
<artifactId>jtidy</artifactId>
|
||||||
|
@ -133,6 +141,7 @@
|
||||||
<configuration>
|
<configuration>
|
||||||
<excludes>
|
<excludes>
|
||||||
<exclude>**/*IntegrationTest.java</exclude>
|
<exclude>**/*IntegrationTest.java</exclude>
|
||||||
|
<exclude>**/*LiveTest.java</exclude>
|
||||||
</excludes>
|
</excludes>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
@ -141,6 +150,42 @@
|
||||||
|
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
|
<profiles>
|
||||||
|
<profile>
|
||||||
|
<id>integration</id>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<phase>integration-test</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>test</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<excludes>
|
||||||
|
<exclude>**/*ManualTest.java</exclude>
|
||||||
|
</excludes>
|
||||||
|
<includes>
|
||||||
|
<include>**/*IntegrationTest.java</include>
|
||||||
|
<exclude>**/*LiveTest.java</exclude>
|
||||||
|
</includes>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
<configuration>
|
||||||
|
<systemPropertyVariables>
|
||||||
|
<test.mime>json</test.mime>
|
||||||
|
</systemPropertyVariables>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</profile>
|
||||||
|
</profiles>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<!-- persistence -->
|
<!-- persistence -->
|
||||||
<hibernate.version>4.3.11.Final</hibernate.version>
|
<hibernate.version>4.3.11.Final</hibernate.version>
|
||||||
|
|
|
@ -25,7 +25,7 @@ import org.junit.Test;
|
||||||
import org.w3c.dom.Document;
|
import org.w3c.dom.Document;
|
||||||
import org.w3c.tidy.Tidy;
|
import org.w3c.tidy.Tidy;
|
||||||
|
|
||||||
public class ApacheFOPConvertHTMLTest {
|
public class ApacheFOPConvertHTMLIntegrationTest {
|
||||||
private String inputFile = "src/test/resources/input.html";
|
private String inputFile = "src/test/resources/input.html";
|
||||||
private String style = "src/test/resources/xhtml2fo.xsl";
|
private String style = "src/test/resources/xhtml2fo.xsl";
|
||||||
private String style1 = "src/test/resources/docbook-xsl/fo/docbook.xsl";
|
private String style1 = "src/test/resources/docbook-xsl/fo/docbook.xsl";
|
|
@ -30,7 +30,7 @@ import org.dbdoclet.trafo.script.Script;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.w3c.dom.Document;
|
import org.w3c.dom.Document;
|
||||||
|
|
||||||
public class ApacheFOPHeroldTest {
|
public class ApacheFOPHeroldLiveTest {
|
||||||
private String[] inputUrls = {// @formatter:off
|
private String[] inputUrls = {// @formatter:off
|
||||||
"http://www.baeldung.com/2011/10/20/bootstraping-a-web-application-with-spring-3-1-and-java-based-configuration-part-1/",
|
"http://www.baeldung.com/2011/10/20/bootstraping-a-web-application-with-spring-3-1-and-java-based-configuration-part-1/",
|
||||||
"http://www.baeldung.com/2011/10/25/building-a-restful-web-service-with-spring-3-1-and-java-based-configuration-part-2/",
|
"http://www.baeldung.com/2011/10/25/building-a-restful-web-service-with-spring-3-1-and-java-based-configuration-part-2/",
|
|
@ -0,0 +1,3 @@
|
||||||
|
### Relevant Articles:
|
||||||
|
- [AssertJ’s Java 8 Features](http://www.baeldung.com/assertJ-java-8-features)
|
||||||
|
- [AssertJ for Guava](http://www.baeldung.com/assertJ-for-guava)
|
|
@ -1,6 +1,5 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
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">
|
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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
@ -9,11 +8,18 @@
|
||||||
<version>1.0.0-SNAPSHOT</version>
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.google.guava</groupId>
|
<groupId>com.google.guava</groupId>
|
||||||
<artifactId>guava</artifactId>
|
<artifactId>guava</artifactId>
|
||||||
<version>19.0</version>
|
<version>${guava.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.assertj</groupId>
|
||||||
|
<artifactId>assertj-guava</artifactId>
|
||||||
|
<version>3.0.0</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
<artifactId>junit</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
|
@ -26,11 +32,7 @@
|
||||||
<version>3.5.1</version>
|
<version>3.5.1</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>org.assertj</groupId>
|
|
||||||
<artifactId>assertj-guava</artifactId>
|
|
||||||
<version>3.0.0</version>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -46,4 +48,9 @@
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<guava.version>19.0</guava.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -1,36 +0,0 @@
|
||||||
<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>autovalue-tutorial</artifactId>
|
|
||||||
<version>1.0</version>
|
|
||||||
<name>AutoValue</name>
|
|
||||||
<build>
|
|
||||||
<plugins>
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
|
||||||
<version>3.3</version>
|
|
||||||
<configuration>
|
|
||||||
<source>7</source>
|
|
||||||
<target>7</target>
|
|
||||||
</configuration>
|
|
||||||
</plugin>
|
|
||||||
</plugins>
|
|
||||||
</build>
|
|
||||||
<dependencies>
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.google.auto.value</groupId>
|
|
||||||
<artifactId>auto-value</artifactId>
|
|
||||||
<version>1.2</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>junit</groupId>
|
|
||||||
<artifactId>junit</artifactId>
|
|
||||||
<version>4.3</version>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
</dependencies>
|
|
||||||
</project>
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
### Relevant Articles:
|
||||||
|
- [Introduction to AutoValue](http://www.baeldung.com/introduction-to-autovalue)
|
|
@ -0,0 +1,37 @@
|
||||||
|
<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>autovalue-tutorial</artifactId>
|
||||||
|
<version>1.0</version>
|
||||||
|
<name>AutoValue</name>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.3</version>
|
||||||
|
<configuration>
|
||||||
|
<source>7</source>
|
||||||
|
<target>7</target>
|
||||||
|
<useIncrementalCompilation>false</useIncrementalCompilation>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.auto.value</groupId>
|
||||||
|
<artifactId>auto-value</artifactId>
|
||||||
|
<version>1.2</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.3</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
|
@ -0,0 +1,2 @@
|
||||||
|
### Relevant Articles:
|
||||||
|
- [CDI Interceptor vs Spring AspectJ](http://www.baeldung.com/cdi-interceptor-vs-spring-aspectj)
|
|
@ -0,0 +1,104 @@
|
||||||
|
<?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>cdi</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-core</artifactId>
|
||||||
|
<version>${spring.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-context</artifactId>
|
||||||
|
<version>${spring.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.aspectj</groupId>
|
||||||
|
<artifactId>aspectjweaver</artifactId>
|
||||||
|
<version>1.8.9</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jboss.weld.se</groupId>
|
||||||
|
<artifactId>weld-se-core</artifactId>
|
||||||
|
<version>2.3.5.Final</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.12</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-test</artifactId>
|
||||||
|
<version>${spring.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<version>${maven-surefire-plugin.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<excludes>
|
||||||
|
<exclude>**/*IntegrationTest.java</exclude>
|
||||||
|
<exclude>**/*LiveTest.java</exclude>
|
||||||
|
</excludes>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<profiles>
|
||||||
|
<profile>
|
||||||
|
<id>integration</id>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<phase>integration-test</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>test</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<excludes>
|
||||||
|
<exclude>**/*LiveTest.java</exclude>
|
||||||
|
</excludes>
|
||||||
|
<includes>
|
||||||
|
<include>**/*IntegrationTest.java</include>
|
||||||
|
</includes>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
<configuration>
|
||||||
|
<systemPropertyVariables>
|
||||||
|
<test.mime>json</test.mime>
|
||||||
|
</systemPropertyVariables>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</profile>
|
||||||
|
</profiles>
|
||||||
|
<properties>
|
||||||
|
<spring.version>4.3.1.RELEASE</spring.version>
|
||||||
|
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,14 @@
|
||||||
|
package com.baeldung.interceptor;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
import javax.interceptor.InterceptorBinding;
|
||||||
|
|
||||||
|
@InterceptorBinding
|
||||||
|
@Target({ ElementType.METHOD, ElementType.TYPE })
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
public @interface Audited {
|
||||||
|
}
|
|
@ -0,0 +1,20 @@
|
||||||
|
package com.baeldung.interceptor;
|
||||||
|
|
||||||
|
import javax.interceptor.AroundInvoke;
|
||||||
|
import javax.interceptor.Interceptor;
|
||||||
|
import javax.interceptor.InvocationContext;
|
||||||
|
|
||||||
|
@Audited
|
||||||
|
@Interceptor
|
||||||
|
public class AuditedInterceptor {
|
||||||
|
public static boolean calledBefore = false;
|
||||||
|
public static boolean calledAfter = false;
|
||||||
|
|
||||||
|
@AroundInvoke
|
||||||
|
public Object auditMethod(InvocationContext ctx) throws Exception {
|
||||||
|
calledBefore = true;
|
||||||
|
Object result = ctx.proceed();
|
||||||
|
calledAfter = true;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
package com.baeldung.service;
|
||||||
|
|
||||||
|
import com.baeldung.interceptor.Audited;
|
||||||
|
|
||||||
|
public class SuperService {
|
||||||
|
@Audited
|
||||||
|
public String deliverService(String uid) {
|
||||||
|
return uid;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
package com.baeldung.spring.aspect;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.aspectj.lang.ProceedingJoinPoint;
|
||||||
|
import org.aspectj.lang.annotation.Around;
|
||||||
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
|
@Aspect
|
||||||
|
public class SpringTestAspect {
|
||||||
|
@Autowired
|
||||||
|
private List<String> accumulator;
|
||||||
|
|
||||||
|
@Around("execution(* com.baeldung.spring.service.SpringSuperService.*(..))")
|
||||||
|
public Object auditMethod(ProceedingJoinPoint jp) throws Throwable {
|
||||||
|
String methodName = jp.getSignature().getName();
|
||||||
|
accumulator.add("Call to " + methodName);
|
||||||
|
Object obj = jp.proceed();
|
||||||
|
accumulator.add("Method called successfully: " + methodName);
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,30 @@
|
||||||
|
package com.baeldung.spring.configuration;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.EnableAspectJAutoProxy;
|
||||||
|
|
||||||
|
import com.baeldung.spring.aspect.SpringTestAspect;
|
||||||
|
import com.baeldung.spring.service.SpringSuperService;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableAspectJAutoProxy
|
||||||
|
public class AppConfig {
|
||||||
|
@Bean
|
||||||
|
public SpringSuperService springSuperService() {
|
||||||
|
return new SpringSuperService();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public SpringTestAspect springTestAspect() {
|
||||||
|
return new SpringTestAspect();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public List<String> getAccumulator() {
|
||||||
|
return new ArrayList<String>();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
package com.baeldung.spring.service;
|
||||||
|
|
||||||
|
public class SpringSuperService {
|
||||||
|
public String getInfoFromService(String code) {
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
<beans xmlns="http://java.sun.com/xml/ns/javaee"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
|
||||||
|
http://java.sun.com/xml/ns/javaee/beans_1_2.xsd">
|
||||||
|
<interceptors>
|
||||||
|
<class>com.baeldung.interceptor.AuditedInterceptor</class>
|
||||||
|
</interceptors>
|
||||||
|
</beans>
|
|
@ -0,0 +1,38 @@
|
||||||
|
package com.baeldung.test;
|
||||||
|
|
||||||
|
import org.jboss.weld.environment.se.Weld;
|
||||||
|
import org.jboss.weld.environment.se.WeldContainer;
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.baeldung.interceptor.AuditedInterceptor;
|
||||||
|
import com.baeldung.service.SuperService;
|
||||||
|
|
||||||
|
public class InterceptorIntegrationTest {
|
||||||
|
Weld weld;
|
||||||
|
WeldContainer container;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void init() {
|
||||||
|
weld = new Weld();
|
||||||
|
container = weld.initialize();
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void shutdown() {
|
||||||
|
weld.shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTheService_whenMethodAndInterceptorExecuted_thenOK() {
|
||||||
|
SuperService superService = container.select(SuperService.class).get();
|
||||||
|
String code = "123456";
|
||||||
|
superService.deliverService(code);
|
||||||
|
|
||||||
|
Assert.assertTrue(AuditedInterceptor.calledBefore);
|
||||||
|
Assert.assertTrue(AuditedInterceptor.calledAfter);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,38 @@
|
||||||
|
package com.baeldung.test;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import com.baeldung.spring.configuration.AppConfig;
|
||||||
|
import com.baeldung.spring.service.SpringSuperService;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@ContextConfiguration(classes = { AppConfig.class })
|
||||||
|
public class SpringInterceptorIntegrationTest {
|
||||||
|
@Autowired
|
||||||
|
SpringSuperService springSuperService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private List<String> accumulator;
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenService_whenServiceAndAspectExecuted_thenOk() {
|
||||||
|
String code = "123456";
|
||||||
|
String result = springSuperService.getInfoFromService(code);
|
||||||
|
|
||||||
|
Assert.assertThat(accumulator.size(), is(2));
|
||||||
|
Assert.assertThat(accumulator.get(0), is("Call to getInfoFromService"));
|
||||||
|
Assert.assertThat(accumulator.get(1), is("Method called successfully: getInfoFromService"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,14 +0,0 @@
|
||||||
=========
|
|
||||||
|
|
||||||
## Core Java 8 Cookbooks and Examples
|
|
||||||
|
|
||||||
### Relevant Articles:
|
|
||||||
- [Java 8 – Powerful Comparison with Lambdas](http://www.baeldung.com/java-8-sort-lambda)
|
|
||||||
- [Java – Directory Size](http://www.baeldung.com/java-folder-size)
|
|
||||||
- [Java – Try with Resources](http://www.baeldung.com/java-try-with-resources)
|
|
||||||
- [A Guide to the Java ExecutorService](http://www.baeldung.com/java-executor-service-tutorial)
|
|
||||||
- [Java 8 New Features](http://www.baeldung.com/java-8-new-features)
|
|
||||||
- [Lambda Expressions and Functional Interfaces: Tips and Best Practices](http://www.baeldung.com/java-8-lambda-expressions-tips)
|
|
||||||
- [The Double Colon Operator in Java 8](http://www.baeldung.com/java-8-double-colon-operator)
|
|
||||||
- [Java 8 Streams Advanced](http://www.baeldung.com/java-8-streams)
|
|
||||||
- [Java 8 Collectors](http://www.baeldung.com/java-8-collectors)
|
|
|
@ -1,138 +0,0 @@
|
||||||
<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>core-java8</artifactId>
|
|
||||||
<version>0.1-SNAPSHOT</version>
|
|
||||||
|
|
||||||
<name>core-java8</name>
|
|
||||||
|
|
||||||
<dependencies>
|
|
||||||
|
|
||||||
<!-- utils -->
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>commons-io</groupId>
|
|
||||||
<artifactId>commons-io</artifactId>
|
|
||||||
<version>2.4</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.google.guava</groupId>
|
|
||||||
<artifactId>guava</artifactId>
|
|
||||||
<version>${guava.version}</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.commons</groupId>
|
|
||||||
<artifactId>commons-collections4</artifactId>
|
|
||||||
<version>4.0</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>commons-codec</groupId>
|
|
||||||
<artifactId>commons-codec</artifactId>
|
|
||||||
<version>1.10</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.commons</groupId>
|
|
||||||
<artifactId>commons-lang3</artifactId>
|
|
||||||
<version>3.3.2</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.slf4j</groupId>
|
|
||||||
<artifactId>slf4j-api</artifactId>
|
|
||||||
<version>${org.slf4j.version}</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- test scoped -->
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.hamcrest</groupId>
|
|
||||||
<artifactId>hamcrest-library</artifactId>
|
|
||||||
<version>${org.hamcrest.version}</version>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>junit</groupId>
|
|
||||||
<artifactId>junit</artifactId>
|
|
||||||
<version>${junit.version}</version>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.assertj</groupId>
|
|
||||||
<artifactId>assertj-core</artifactId>
|
|
||||||
<version>3.5.1</version>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.mockito</groupId>
|
|
||||||
<artifactId>mockito-core</artifactId>
|
|
||||||
<version>${mockito.version}</version>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
</dependencies>
|
|
||||||
|
|
||||||
<build>
|
|
||||||
<finalName>core-java-8</finalName>
|
|
||||||
<resources>
|
|
||||||
<resource>
|
|
||||||
<directory>src/main/resources</directory>
|
|
||||||
<filtering>true</filtering>
|
|
||||||
</resource>
|
|
||||||
</resources>
|
|
||||||
|
|
||||||
<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>
|
|
||||||
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-surefire-plugin</artifactId>
|
|
||||||
<version>${maven-surefire-plugin.version}</version>
|
|
||||||
</plugin>
|
|
||||||
|
|
||||||
</plugins>
|
|
||||||
|
|
||||||
</build>
|
|
||||||
|
|
||||||
<properties>
|
|
||||||
<!-- logging -->
|
|
||||||
<org.slf4j.version>1.7.13</org.slf4j.version>
|
|
||||||
<logback.version>1.0.13</logback.version>
|
|
||||||
|
|
||||||
<!-- various -->
|
|
||||||
<hibernate-validator.version>5.1.3.Final</hibernate-validator.version>
|
|
||||||
|
|
||||||
<!-- util -->
|
|
||||||
<guava.version>19.0</guava.version>
|
|
||||||
<commons-lang3.version>3.4</commons-lang3.version>
|
|
||||||
|
|
||||||
<!-- testing -->
|
|
||||||
<org.hamcrest.version>1.3</org.hamcrest.version>
|
|
||||||
<junit.version>4.12</junit.version>
|
|
||||||
<mockito.version>1.10.19</mockito.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>
|
|
||||||
|
|
||||||
</project>
|
|
|
@ -1,91 +0,0 @@
|
||||||
package com.baeldung.enums;
|
|
||||||
|
|
||||||
import java.util.EnumMap;
|
|
||||||
import java.util.EnumSet;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
public class Pizza {
|
|
||||||
|
|
||||||
private static EnumSet<PizzaStatusEnum> deliveredPizzaStatuses =
|
|
||||||
EnumSet.of(PizzaStatusEnum.DELIVERED);
|
|
||||||
|
|
||||||
private PizzaStatusEnum status;
|
|
||||||
|
|
||||||
public enum PizzaStatusEnum {
|
|
||||||
ORDERED(5) {
|
|
||||||
@Override
|
|
||||||
public boolean isOrdered() {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
READY(2) {
|
|
||||||
@Override
|
|
||||||
public boolean isReady() {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
DELIVERED(0) {
|
|
||||||
@Override
|
|
||||||
public boolean isDelivered() {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
private int timeToDelivery;
|
|
||||||
|
|
||||||
public boolean isOrdered() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isReady() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isDelivered() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getTimeToDelivery() {
|
|
||||||
return timeToDelivery;
|
|
||||||
}
|
|
||||||
|
|
||||||
PizzaStatusEnum(int timeToDelivery) {
|
|
||||||
this.timeToDelivery = timeToDelivery;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public PizzaStatusEnum getStatus() {
|
|
||||||
return status;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setStatus(PizzaStatusEnum status) {
|
|
||||||
this.status = status;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isDeliverable() {
|
|
||||||
return this.status.isReady();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void printTimeToDeliver() {
|
|
||||||
System.out.println("Time to delivery is " + this.getStatus().getTimeToDelivery() + " days");
|
|
||||||
}
|
|
||||||
|
|
||||||
public static List<Pizza> getAllUndeliveredPizzas(List<Pizza> input) {
|
|
||||||
return input.stream().filter((s) -> !deliveredPizzaStatuses.contains(s.getStatus())).collect(Collectors.toList());
|
|
||||||
}
|
|
||||||
|
|
||||||
public static EnumMap<PizzaStatusEnum, List<Pizza>> groupPizzaByStatus(List<Pizza> pzList) {
|
|
||||||
return pzList.stream().collect(
|
|
||||||
Collectors.groupingBy(Pizza::getStatus,
|
|
||||||
() -> new EnumMap<>(PizzaStatusEnum.class), Collectors.toList()));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void deliver() {
|
|
||||||
if (isDeliverable()) {
|
|
||||||
PizzaDeliverySystemConfiguration.getInstance().getDeliveryStrategy().deliver(this);
|
|
||||||
this.setStatus(PizzaStatusEnum.DELIVERED);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,18 +0,0 @@
|
||||||
package com.baeldung.enums;
|
|
||||||
|
|
||||||
public enum PizzaDeliveryStrategy {
|
|
||||||
EXPRESS {
|
|
||||||
@Override
|
|
||||||
public void deliver(Pizza pz) {
|
|
||||||
System.out.println("Pizza will be delivered in express mode");
|
|
||||||
}
|
|
||||||
},
|
|
||||||
NORMAL {
|
|
||||||
@Override
|
|
||||||
public void deliver(Pizza pz) {
|
|
||||||
System.out.println("Pizza will be delivered in normal mode");
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
public abstract void deliver(Pizza pz);
|
|
||||||
}
|
|
|
@ -1,22 +0,0 @@
|
||||||
package com.baeldung.enums;
|
|
||||||
|
|
||||||
|
|
||||||
public enum PizzaDeliverySystemConfiguration {
|
|
||||||
INSTANCE;
|
|
||||||
|
|
||||||
PizzaDeliverySystemConfiguration() {
|
|
||||||
// Do the configuration initialization which
|
|
||||||
// involves overriding defaults like delivery strategy
|
|
||||||
}
|
|
||||||
|
|
||||||
private PizzaDeliveryStrategy deliveryStrategy = PizzaDeliveryStrategy.NORMAL;
|
|
||||||
|
|
||||||
public static PizzaDeliverySystemConfiguration getInstance() {
|
|
||||||
return INSTANCE;
|
|
||||||
}
|
|
||||||
|
|
||||||
public PizzaDeliveryStrategy getDeliveryStrategy() {
|
|
||||||
return deliveryStrategy;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Binary file not shown.
|
@ -1 +0,0 @@
|
||||||
Hello World!
|
|
|
@ -1 +0,0 @@
|
||||||
Hello World!
|
|
|
@ -1 +0,0 @@
|
||||||
My Name is John
|
|
|
@ -1 +0,0 @@
|
||||||
My Name is Tom
|
|
|
@ -1 +0,0 @@
|
||||||
My Name is Jane
|
|
|
@ -1,55 +0,0 @@
|
||||||
package com.baeldung.datetime;
|
|
||||||
|
|
||||||
import java.time.DayOfWeek;
|
|
||||||
import java.time.LocalDate;
|
|
||||||
import java.time.LocalDateTime;
|
|
||||||
import java.time.Month;
|
|
||||||
|
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
public class UseLocalDateTest {
|
|
||||||
|
|
||||||
UseLocalDate useLocalDate = new UseLocalDate();
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenValues_whenUsingFactoryOf_thenLocalDate(){
|
|
||||||
Assert.assertEquals("2016-05-10",useLocalDate.getLocalDateUsingFactoryOfMethod(2016,5,10).toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenString_whenUsingParse_thenLocalDate(){
|
|
||||||
Assert.assertEquals("2016-05-10",useLocalDate.getLocalDateUsingParseMethod("2016-05-10").toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenUsingClock_thenLocalDate(){
|
|
||||||
Assert.assertEquals(LocalDate.now(),useLocalDate.getLocalDateFromClock());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenDate_whenUsingPlus_thenNextDay(){
|
|
||||||
Assert.assertEquals(LocalDate.now().plusDays(1),useLocalDate.getNextDay(LocalDate.now()));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenDate_whenUsingMinus_thenPreviousDay(){
|
|
||||||
Assert.assertEquals(LocalDate.now().minusDays(1),useLocalDate.getPreviousDay(LocalDate.now()));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenToday_whenUsingGetDayOfWeek_thenDayOfWeek(){
|
|
||||||
Assert.assertEquals(DayOfWeek.SUNDAY,useLocalDate.getDayOfWeek(LocalDate.parse("2016-05-22")));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenToday_whenUsingWithTemporalAdjuster_thenFirstDayOfMonth(){
|
|
||||||
Assert.assertEquals(1,useLocalDate.getFirstDayOfMonth().getDayOfMonth());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenLocalDate_whenUsingAtStartOfDay_thenReturnMidnight(){
|
|
||||||
Assert.assertEquals(LocalDateTime.parse("2016-05-22T00:00:00"),useLocalDate.getStartOfDay(LocalDate.parse("2016-05-22")));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,19 +0,0 @@
|
||||||
package com.baeldung.datetime;
|
|
||||||
|
|
||||||
import java.time.LocalDate;
|
|
||||||
import java.time.LocalTime;
|
|
||||||
import java.time.Month;
|
|
||||||
|
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
public class UseLocalDateTimeTest {
|
|
||||||
|
|
||||||
UseLocalDateTime useLocalDateTime = new UseLocalDateTime();
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenString_whenUsingParse_thenLocalDateTime(){
|
|
||||||
Assert.assertEquals(LocalDate.of(2016, Month.MAY, 10),useLocalDateTime.getLocalDateTimeUsingParseMethod("2016-05-10T06:30").toLocalDate());
|
|
||||||
Assert.assertEquals(LocalTime.of(6,30),useLocalDateTime.getLocalDateTimeUsingParseMethod("2016-05-10T06:30").toLocalTime());
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,36 +0,0 @@
|
||||||
package com.baeldung.datetime;
|
|
||||||
|
|
||||||
import java.time.LocalTime;
|
|
||||||
|
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
public class UseLocalTimeTest {
|
|
||||||
|
|
||||||
UseLocalTime useLocalTime = new UseLocalTime();
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenValues_whenUsingFactoryOf_thenLocalTime(){
|
|
||||||
Assert.assertEquals("07:07:07",useLocalTime.getLocalTimeUsingFactoryOfMethod(7,7,7).toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenString_whenUsingParse_thenLocalTime(){
|
|
||||||
Assert.assertEquals("06:30",useLocalTime.getLocalTimeUsingParseMethod("06:30").toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenTime_whenAddHour_thenLocalTime(){
|
|
||||||
Assert.assertEquals("07:30",useLocalTime.addAnHour(LocalTime.of(6,30)).toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void getHourFromLocalTime(){
|
|
||||||
Assert.assertEquals(1, useLocalTime.getHourFromLocalTime(LocalTime.of(1,1)));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void getLocalTimeWithMinuteSetToValue(){
|
|
||||||
Assert.assertEquals(LocalTime.of(10, 20), useLocalTime.getLocalTimeWithMinuteSetToValue(LocalTime.of(10,10), 20));
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,20 +0,0 @@
|
||||||
package com.baeldung.datetime;
|
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
|
||||||
import java.time.ZoneId;
|
|
||||||
import java.time.ZonedDateTime;
|
|
||||||
|
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
public class UseZonedDateTimeTest {
|
|
||||||
|
|
||||||
UseZonedDateTime zonedDateTime=new UseZonedDateTime();
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenZoneId_thenZonedDateTime(){
|
|
||||||
ZoneId zoneId=ZoneId.of("Europe/Paris");
|
|
||||||
ZonedDateTime zonedDatetime=zonedDateTime.getZonedDateTime(LocalDateTime.parse("2016-05-20T06:30"), zoneId);
|
|
||||||
Assert.assertEquals(zoneId,ZoneId.from(zonedDatetime));
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
=========
|
||||||
|
|
||||||
|
## Core Java 9 Examples
|
||||||
|
|
||||||
|
[Java 9 New Features](http://www.baeldung.com/new-java-9)
|
|
@ -0,0 +1,93 @@
|
||||||
|
<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>core-java9</artifactId>
|
||||||
|
<version>0.2-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<name>core-java9</name>
|
||||||
|
|
||||||
|
<pluginRepositories>
|
||||||
|
<pluginRepository>
|
||||||
|
<id>apache.snapshots</id>
|
||||||
|
<url>http://repository.apache.org/snapshots/</url>
|
||||||
|
</pluginRepository>
|
||||||
|
</pluginRepositories>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-api</artifactId>
|
||||||
|
<version>${org.slf4j.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hamcrest</groupId>
|
||||||
|
<artifactId>hamcrest-library</artifactId>
|
||||||
|
<version>${org.hamcrest.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>${junit.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mockito</groupId>
|
||||||
|
<artifactId>mockito-core</artifactId>
|
||||||
|
<version>${mockito.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<finalName>core-java-9</finalName>
|
||||||
|
|
||||||
|
<plugins>
|
||||||
|
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>${maven-compiler-plugin.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<source>1.9</source>
|
||||||
|
<target>1.9</target>
|
||||||
|
<verbose>true</verbose>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<version>${maven-surefire-plugin.version}</version>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
|
</plugins>
|
||||||
|
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<!-- logging -->
|
||||||
|
<org.slf4j.version>1.7.13</org.slf4j.version>
|
||||||
|
<logback.version>1.0.13</logback.version>
|
||||||
|
|
||||||
|
|
||||||
|
<!-- maven plugins -->
|
||||||
|
<maven-compiler-plugin.version>3.6-jigsaw-SNAPSHOT</maven-compiler-plugin.version>
|
||||||
|
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
||||||
|
|
||||||
|
<!-- testing -->
|
||||||
|
<org.hamcrest.version>1.3</org.hamcrest.version>
|
||||||
|
<junit.version>4.12</junit.version>
|
||||||
|
<mockito.version>1.10.19</mockito.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,23 @@
|
||||||
|
package com.baeldung.java9.language;
|
||||||
|
|
||||||
|
public interface PrivateInterface {
|
||||||
|
|
||||||
|
private static String staticPrivate() {
|
||||||
|
return "static private";
|
||||||
|
}
|
||||||
|
|
||||||
|
private String instancePrivate() {
|
||||||
|
return "instance private";
|
||||||
|
}
|
||||||
|
|
||||||
|
public default void check(){
|
||||||
|
String result = staticPrivate();
|
||||||
|
if (!result.equals("static private"))
|
||||||
|
throw new AssertionError("Incorrect result for static private interface method");
|
||||||
|
PrivateInterface pvt = new PrivateInterface() {
|
||||||
|
};
|
||||||
|
result = pvt.instancePrivate();
|
||||||
|
if (!result.equals("instance private"))
|
||||||
|
throw new AssertionError("Incorrect result for instance private interface method");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,44 @@
|
||||||
|
package com.baeldung.java9.process;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.security.SecureRandom;
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
|
||||||
|
public class ProcessUtils {
|
||||||
|
|
||||||
|
public static String getClassPath(){
|
||||||
|
String cp = System.getProperty("java.class.path");
|
||||||
|
System.out.println("ClassPath is "+cp);
|
||||||
|
return cp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static File getJavaCmd() throws IOException{
|
||||||
|
String javaHome = System.getProperty("java.home");
|
||||||
|
File javaCmd;
|
||||||
|
if(System.getProperty("os.name").startsWith("Win")){
|
||||||
|
javaCmd = new File(javaHome, "bin/java.exe");
|
||||||
|
}else{
|
||||||
|
javaCmd = new File(javaHome, "bin/java");
|
||||||
|
}
|
||||||
|
if(javaCmd.canExecute()){
|
||||||
|
return javaCmd;
|
||||||
|
}else{
|
||||||
|
throw new UnsupportedOperationException(javaCmd.getCanonicalPath() + " is not executable");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getMainClass(){
|
||||||
|
return System.getProperty("sun.java.command");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getSystemProperties(){
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
System.getProperties().forEach((s1, s2) -> sb.append(s1 +" - "+ s2) );
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
package com.baeldung.java9.process;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public class ServiceMain {
|
||||||
|
|
||||||
|
public static void main(String[] args) throws InterruptedException {
|
||||||
|
ProcessHandle thisProcess = ProcessHandle.current();
|
||||||
|
long pid = thisProcess.getPid();
|
||||||
|
|
||||||
|
|
||||||
|
Optional<String[]> opArgs = Optional.ofNullable(args);
|
||||||
|
String procName = opArgs.map(str -> str.length > 0 ? str[0] : null).orElse(System.getProperty("sun.java.command"));
|
||||||
|
|
||||||
|
for (int i = 0; i < 10000; i++) {
|
||||||
|
System.out.println("Process " + procName + " with ID " + pid + " is running!");
|
||||||
|
Thread.sleep(10000);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,71 @@
|
||||||
|
package com.baeldung.java8;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class Java9OptionalsStreamTest {
|
||||||
|
|
||||||
|
private static List<Optional<String>> listOfOptionals = Arrays.asList(Optional.empty(), Optional.of("foo"), Optional.empty(), Optional.of("bar"));
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void filterOutPresentOptionalsWithFilter() {
|
||||||
|
assertEquals(4, listOfOptionals.size());
|
||||||
|
|
||||||
|
List<String> filteredList = listOfOptionals.stream()
|
||||||
|
.filter(Optional::isPresent)
|
||||||
|
.map(Optional::get)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
assertEquals(2, filteredList.size());
|
||||||
|
assertEquals("foo", filteredList.get(0));
|
||||||
|
assertEquals("bar", filteredList.get(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void filterOutPresentOptionalsWithFlatMap() {
|
||||||
|
assertEquals(4, listOfOptionals.size());
|
||||||
|
|
||||||
|
List<String> filteredList = listOfOptionals.stream()
|
||||||
|
.flatMap(o -> o.isPresent() ? Stream.of(o.get()) : Stream.empty())
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
assertEquals(2, filteredList.size());
|
||||||
|
|
||||||
|
assertEquals("foo", filteredList.get(0));
|
||||||
|
assertEquals("bar", filteredList.get(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void filterOutPresentOptionalsWithFlatMap2() {
|
||||||
|
assertEquals(4, listOfOptionals.size());
|
||||||
|
|
||||||
|
List<String> filteredList = listOfOptionals.stream()
|
||||||
|
.flatMap(o -> o.map(Stream::of).orElseGet(Stream::empty))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
assertEquals(2, filteredList.size());
|
||||||
|
|
||||||
|
assertEquals("foo", filteredList.get(0));
|
||||||
|
assertEquals("bar", filteredList.get(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void filterOutPresentOptionalsWithJava9() {
|
||||||
|
assertEquals(4, listOfOptionals.size());
|
||||||
|
|
||||||
|
List<String> filteredList = listOfOptionals.stream()
|
||||||
|
.flatMap(Optional::stream)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
assertEquals(2, filteredList.size());
|
||||||
|
assertEquals("foo", filteredList.get(0));
|
||||||
|
assertEquals("bar", filteredList.get(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,47 @@
|
||||||
|
package com.baeldung.java9;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertSame;
|
||||||
|
|
||||||
|
import java.awt.Image;
|
||||||
|
import java.awt.image.BaseMultiResolutionImage;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.awt.image.MultiResolutionImage;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class MultiResultionImageTest {
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void baseMultiResImageTest() {
|
||||||
|
int baseIndex = 1;
|
||||||
|
int length = 4;
|
||||||
|
BufferedImage[] resolutionVariants = new BufferedImage[length];
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
resolutionVariants[i] = createImage(i);
|
||||||
|
}
|
||||||
|
MultiResolutionImage bmrImage = new BaseMultiResolutionImage(baseIndex, resolutionVariants);
|
||||||
|
List<Image> rvImageList = bmrImage.getResolutionVariants();
|
||||||
|
assertEquals("MultiResoltion Image shoudl contain the same number of resolution variants!", rvImageList.size(), length);
|
||||||
|
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
int imageSize = getSize(i);
|
||||||
|
Image testRVImage = bmrImage.getResolutionVariant(imageSize, imageSize);
|
||||||
|
assertSame("Images should be the same", testRVImage, resolutionVariants[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int getSize(int i) {
|
||||||
|
return 8 * (i + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static BufferedImage createImage(int i) {
|
||||||
|
return new BufferedImage(getSize(i), getSize(i),
|
||||||
|
BufferedImage.TYPE_INT_RGB);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
package com.baeldung.java9;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
public class OptionalToStreamTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testOptionalToStream() {
|
||||||
|
Optional<String> op = Optional.ofNullable("String value");
|
||||||
|
Stream<String> strOptionalStream = op.stream();
|
||||||
|
Stream<String> filteredStream = strOptionalStream.filter((str) -> {
|
||||||
|
return str != null && str.startsWith("String");
|
||||||
|
});
|
||||||
|
assertEquals(1, filteredStream.count());
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,2 @@
|
||||||
|
### Relevant Artiles:
|
||||||
|
- [Filtering a Stream of Optionals in Java](http://www.baeldung.com/java-filter-stream-of-optional)
|
|
@ -0,0 +1,26 @@
|
||||||
|
package com.baeldung.java9;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
public class SetExamplesTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUnmutableSet() {
|
||||||
|
Set<String> strKeySet = Set.of("key1", "key2", "key3");
|
||||||
|
try {
|
||||||
|
strKeySet.add("newKey");
|
||||||
|
} catch (UnsupportedOperationException uoe) {
|
||||||
|
}
|
||||||
|
assertEquals(strKeySet.size(), 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testArrayToSet() {
|
||||||
|
Integer[] intArray = new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
|
||||||
|
Set<Integer> intSet = Set.of(intArray);
|
||||||
|
assertEquals(intSet.size(), intArray.length);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,126 @@
|
||||||
|
package com.baeldung.java9.httpclient;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
import static java.net.HttpURLConnection.HTTP_OK;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.CookieManager;
|
||||||
|
import java.net.CookiePolicy;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
import java.net.http.HttpClient;
|
||||||
|
import java.net.http.HttpHeaders;
|
||||||
|
import java.net.http.HttpRequest;
|
||||||
|
import java.net.http.HttpResponse;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
|
import javax.net.ssl.SSLContext;
|
||||||
|
import javax.net.ssl.SSLParameters;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class SimpleHttpRequestsTest {
|
||||||
|
|
||||||
|
private URI httpURI;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void init() throws URISyntaxException {
|
||||||
|
httpURI = new URI("http://www.baeldung.com/");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void quickGet() throws IOException, InterruptedException, URISyntaxException {
|
||||||
|
HttpRequest request = HttpRequest.create( httpURI ).GET();
|
||||||
|
HttpResponse response = request.response();
|
||||||
|
int responseStatusCode = response.statusCode();
|
||||||
|
String responseBody = response.body(HttpResponse.asString());
|
||||||
|
assertTrue("Get response status code is bigger then 400", responseStatusCode < 400);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void asynchronousGet() throws URISyntaxException, IOException, InterruptedException, ExecutionException{
|
||||||
|
HttpRequest request = HttpRequest.create(httpURI).GET();
|
||||||
|
long before = System.currentTimeMillis();
|
||||||
|
CompletableFuture<HttpResponse> futureResponse = request.responseAsync();
|
||||||
|
futureResponse.thenAccept( response -> {
|
||||||
|
String responseBody = response.body(HttpResponse.asString());
|
||||||
|
});
|
||||||
|
HttpResponse resp = futureResponse.get();
|
||||||
|
HttpHeaders hs = resp.headers();
|
||||||
|
assertTrue("There should be more then 1 header.", hs.map().size() >1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void postMehtod() throws URISyntaxException, IOException, InterruptedException {
|
||||||
|
HttpRequest.Builder requestBuilder = HttpRequest.create(httpURI);
|
||||||
|
requestBuilder.body(HttpRequest.fromString("param1=foo,param2=bar")).followRedirects(HttpClient.Redirect.SECURE);
|
||||||
|
HttpRequest request = requestBuilder.POST();
|
||||||
|
HttpResponse response = request.response();
|
||||||
|
int statusCode = response.statusCode();
|
||||||
|
assertTrue("HTTP return code", statusCode == HTTP_OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void configureHttpClient() throws NoSuchAlgorithmException, URISyntaxException, IOException, InterruptedException{
|
||||||
|
CookieManager cManager = new CookieManager();
|
||||||
|
cManager.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER);
|
||||||
|
|
||||||
|
SSLParameters sslParam = new SSLParameters (new String[] { "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256" }, new String[] { "TLSv1.2" });
|
||||||
|
|
||||||
|
HttpClient.Builder hcBuilder = HttpClient.create();
|
||||||
|
hcBuilder.cookieManager(cManager).sslContext(SSLContext.getDefault()).sslParameters(sslParam);
|
||||||
|
HttpClient httpClient = hcBuilder.build();
|
||||||
|
HttpRequest.Builder reqBuilder = httpClient.request(new URI("https://www.facebook.com"));
|
||||||
|
|
||||||
|
HttpRequest request = reqBuilder.followRedirects(HttpClient.Redirect.ALWAYS).GET();
|
||||||
|
HttpResponse response = request.response();
|
||||||
|
int statusCode = response.statusCode();
|
||||||
|
assertTrue("HTTP return code", statusCode == HTTP_OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
SSLParameters getDefaultSSLParameters() throws NoSuchAlgorithmException{
|
||||||
|
SSLParameters sslP1 = SSLContext.getDefault().getSupportedSSLParameters();
|
||||||
|
String [] proto = sslP1.getApplicationProtocols();
|
||||||
|
String [] cifers = sslP1.getCipherSuites();
|
||||||
|
System.out.println(printStringArr(proto));
|
||||||
|
System.out.println(printStringArr(cifers));
|
||||||
|
return sslP1;
|
||||||
|
}
|
||||||
|
|
||||||
|
String printStringArr(String ... args ){
|
||||||
|
if(args == null){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
for (String s : args){
|
||||||
|
sb.append(s);
|
||||||
|
sb.append("\n");
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
String printHeaders(HttpHeaders h){
|
||||||
|
if(h == null){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
Map<String, List<String>> hMap = h.map();
|
||||||
|
for(String k : hMap.keySet()){
|
||||||
|
sb.append(k).append(":");
|
||||||
|
List<String> l = hMap.get(k);
|
||||||
|
if( l != null ){
|
||||||
|
l.forEach( s -> { sb.append(s).append(","); } );
|
||||||
|
}
|
||||||
|
sb.append("\n");
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,36 @@
|
||||||
|
package com.baeldung.java9.language;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class DiamondTest {
|
||||||
|
|
||||||
|
static class FooClass<X> {
|
||||||
|
FooClass(X x) {
|
||||||
|
}
|
||||||
|
|
||||||
|
<Z> FooClass(X x, Z z) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void diamondTest() {
|
||||||
|
FooClass<Integer> fc = new FooClass<>(1) {
|
||||||
|
};
|
||||||
|
FooClass<? extends Integer> fc0 = new FooClass<>(1) {
|
||||||
|
};
|
||||||
|
FooClass<?> fc1 = new FooClass<>(1) {
|
||||||
|
};
|
||||||
|
FooClass<? super Integer> fc2 = new FooClass<>(1) {
|
||||||
|
};
|
||||||
|
|
||||||
|
FooClass<Integer> fc3 = new FooClass<>(1, "") {
|
||||||
|
};
|
||||||
|
FooClass<? extends Integer> fc4 = new FooClass<>(1, "") {
|
||||||
|
};
|
||||||
|
FooClass<?> fc5 = new FooClass<>(1, "") {
|
||||||
|
};
|
||||||
|
FooClass<? super Integer> fc6 = new FooClass<>(1, "") {
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
package com.baeldung.java9.language;
|
||||||
|
|
||||||
|
import com.baeldung.java9.language.PrivateInterface;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class PrivateInterfaceTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test() {
|
||||||
|
PrivateInterface piClass = new PrivateInterface() {
|
||||||
|
};
|
||||||
|
piClass.check();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,70 @@
|
||||||
|
package com.baeldung.java9.language;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class TryWithResourcesTest {
|
||||||
|
|
||||||
|
static int closeCount = 0;
|
||||||
|
|
||||||
|
static class MyAutoCloseable implements AutoCloseable{
|
||||||
|
final FinalWrapper finalWrapper = new FinalWrapper();
|
||||||
|
|
||||||
|
public void close() {
|
||||||
|
closeCount++;
|
||||||
|
}
|
||||||
|
|
||||||
|
static class FinalWrapper {
|
||||||
|
public final AutoCloseable finalCloseable = new AutoCloseable() {
|
||||||
|
@Override
|
||||||
|
public void close() throws Exception {
|
||||||
|
closeCount++;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void tryWithResourcesTest() {
|
||||||
|
MyAutoCloseable mac = new MyAutoCloseable();
|
||||||
|
|
||||||
|
try (mac) {
|
||||||
|
assertEquals("Expected and Actual does not match", 0, closeCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
try (mac.finalWrapper.finalCloseable) {
|
||||||
|
assertEquals("Expected and Actual does not match", 1, closeCount);
|
||||||
|
} catch (Exception ex) {
|
||||||
|
}
|
||||||
|
|
||||||
|
try (new MyAutoCloseable() { }.finalWrapper.finalCloseable) {
|
||||||
|
assertEquals("Expected and Actual does not match", 2, closeCount);
|
||||||
|
} catch (Exception ex) {
|
||||||
|
}
|
||||||
|
|
||||||
|
try ((closeCount > 0 ? mac : new MyAutoCloseable()).finalWrapper.finalCloseable) {
|
||||||
|
assertEquals("Expected and Actual does not match", 3, closeCount);
|
||||||
|
} catch (Exception ex) {
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
throw new CloseableException();
|
||||||
|
} catch (CloseableException ex) {
|
||||||
|
try (ex) {
|
||||||
|
assertEquals("Expected and Actual does not match", 4, closeCount);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assertEquals("Expected and Actual does not match", 5, closeCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static class CloseableException extends Exception implements AutoCloseable {
|
||||||
|
@Override
|
||||||
|
public void close() {
|
||||||
|
closeCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,119 @@
|
||||||
|
package com.baeldung.java9.language.stream;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import static java.lang.Integer.*;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
public class StreamFeaturesTest {
|
||||||
|
|
||||||
|
public static class TakeAndDropWhileTest {
|
||||||
|
|
||||||
|
public Stream<String> getStreamAfterTakeWhileOperation() {
|
||||||
|
return Stream
|
||||||
|
.iterate("", s -> s + "s")
|
||||||
|
.takeWhile(s -> s.length() < 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Stream<String> getStreamAfterDropWhileOperation() {
|
||||||
|
return Stream
|
||||||
|
.iterate("", s -> s + "s")
|
||||||
|
.takeWhile(s -> s.length() < 10)
|
||||||
|
.dropWhile(s -> !s.contains("sssss"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testTakeWhileOperation() {
|
||||||
|
List<String> list = getStreamAfterTakeWhileOperation().collect(Collectors.toList());
|
||||||
|
|
||||||
|
assertEquals(10, list.size());
|
||||||
|
|
||||||
|
assertEquals("", list.get(0));
|
||||||
|
assertEquals("ss", list.get(2));
|
||||||
|
assertEquals("sssssssss", list.get(list.size() - 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDropWhileOperation() {
|
||||||
|
List<String> list = getStreamAfterDropWhileOperation().collect(Collectors.toList());
|
||||||
|
|
||||||
|
assertEquals(5, list.size());
|
||||||
|
|
||||||
|
assertEquals("sssss", list.get(0));
|
||||||
|
assertEquals("sssssss", list.get(2));
|
||||||
|
assertEquals("sssssssss", list.get(list.size() - 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class IterateTest {
|
||||||
|
|
||||||
|
private Stream<Integer> getStream() {
|
||||||
|
return Stream.iterate(0, i -> i < 10, i -> i + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIterateOperation() {
|
||||||
|
List<Integer> list = getStream().collect(Collectors.toList());
|
||||||
|
|
||||||
|
assertEquals(10, list.size());
|
||||||
|
|
||||||
|
assertEquals(valueOf(0), list.get(0));
|
||||||
|
assertEquals(valueOf(5), list.get(5));
|
||||||
|
assertEquals(valueOf(9), list.get(list.size() - 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class OfNullableTest {
|
||||||
|
|
||||||
|
private List<String> collection = Arrays.asList("A", "B", "C");
|
||||||
|
private Map<String, Integer> map = new HashMap<>() {{
|
||||||
|
put("A", 10);
|
||||||
|
put("C", 30);
|
||||||
|
}};
|
||||||
|
|
||||||
|
private Stream<Integer> getStreamWithOfNullable() {
|
||||||
|
return collection.stream()
|
||||||
|
.flatMap(s -> Stream.ofNullable(map.get(s)));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Stream<Integer> getStream() {
|
||||||
|
return collection.stream()
|
||||||
|
.flatMap(s -> {
|
||||||
|
Integer temp = map.get(s);
|
||||||
|
return temp != null ? Stream.of(temp) : Stream.empty();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Integer> testOfNullableFrom(Stream<Integer> stream) {
|
||||||
|
List<Integer> list = stream.collect(Collectors.toList());
|
||||||
|
|
||||||
|
assertEquals(2, list.size());
|
||||||
|
|
||||||
|
assertEquals(valueOf(10), list.get(0));
|
||||||
|
assertEquals(valueOf(30), list.get(list.size() - 1));
|
||||||
|
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testOfNullable() {
|
||||||
|
|
||||||
|
assertEquals(
|
||||||
|
testOfNullableFrom(getStream()),
|
||||||
|
testOfNullableFrom(getStreamWithOfNullable())
|
||||||
|
);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,112 @@
|
||||||
|
package com.baeldung.java9.process;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.security.SecureRandom;
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import junit.framework.Assert;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
public class ProcessApi {
|
||||||
|
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void init() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void processInfoExample()throws NoSuchAlgorithmException{
|
||||||
|
ProcessHandle self = ProcessHandle.current();
|
||||||
|
long PID = self.getPid();
|
||||||
|
ProcessHandle.Info procInfo = self.info();
|
||||||
|
Optional<String[]> args = procInfo.arguments();
|
||||||
|
Optional<String> cmd = procInfo.commandLine();
|
||||||
|
Optional<Instant> startTime = procInfo.startInstant();
|
||||||
|
Optional<Duration> cpuUsage = procInfo.totalCpuDuration();
|
||||||
|
|
||||||
|
waistCPU();
|
||||||
|
System.out.println("Args "+ args);
|
||||||
|
System.out.println("Command " +cmd.orElse("EmptyCmd"));
|
||||||
|
System.out.println("Start time: "+ startTime.get().toString());
|
||||||
|
System.out.println(cpuUsage.get().toMillis());
|
||||||
|
|
||||||
|
Stream<ProcessHandle> allProc = ProcessHandle.current().children();
|
||||||
|
allProc.forEach(p -> {
|
||||||
|
System.out.println("Proc "+ p.getPid());
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void createAndDestroyProcess() throws IOException, InterruptedException{
|
||||||
|
int numberOfChildProcesses = 5;
|
||||||
|
for(int i=0; i < numberOfChildProcesses; i++){
|
||||||
|
createNewJVM(ServiceMain.class, i).getPid();
|
||||||
|
}
|
||||||
|
|
||||||
|
Stream<ProcessHandle> childProc = ProcessHandle.current().children();
|
||||||
|
assertEquals( childProc.count(), numberOfChildProcesses);
|
||||||
|
|
||||||
|
childProc = ProcessHandle.current().children();
|
||||||
|
childProc.forEach(processHandle -> {
|
||||||
|
assertTrue("Process "+ processHandle.getPid() +" should be alive!", processHandle.isAlive());
|
||||||
|
CompletableFuture<ProcessHandle> onProcExit = processHandle.onExit();
|
||||||
|
onProcExit.thenAccept(procHandle -> {
|
||||||
|
System.out.println("Process with PID "+ procHandle.getPid() + " has stopped");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
Thread.sleep(10000);
|
||||||
|
|
||||||
|
childProc = ProcessHandle.current().children();
|
||||||
|
childProc.forEach(procHandle -> {
|
||||||
|
assertTrue("Could not kill process "+procHandle.getPid(), procHandle.destroy());
|
||||||
|
});
|
||||||
|
|
||||||
|
Thread.sleep(5000);
|
||||||
|
|
||||||
|
childProc = ProcessHandle.current().children();
|
||||||
|
childProc.forEach(procHandle -> {
|
||||||
|
assertFalse("Process "+ procHandle.getPid() +" should not be alive!", procHandle.isAlive());
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private Process createNewJVM(Class mainClass, int number) throws IOException{
|
||||||
|
ArrayList<String> cmdParams = new ArrayList<String>(5);
|
||||||
|
cmdParams.add(ProcessUtils.getJavaCmd().getAbsolutePath());
|
||||||
|
cmdParams.add("-cp");
|
||||||
|
cmdParams.add(ProcessUtils.getClassPath());
|
||||||
|
cmdParams.add(mainClass.getName());
|
||||||
|
cmdParams.add("Service "+ number);
|
||||||
|
ProcessBuilder myService = new ProcessBuilder(cmdParams);
|
||||||
|
myService.inheritIO();
|
||||||
|
return myService.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void waistCPU() throws NoSuchAlgorithmException{
|
||||||
|
ArrayList<Integer> randArr = new ArrayList<Integer>(4096);
|
||||||
|
SecureRandom sr = SecureRandom.getInstanceStrong();
|
||||||
|
Duration somecpu = Duration.ofMillis(4200L);
|
||||||
|
Instant end = Instant.now().plus(somecpu);
|
||||||
|
while (Instant.now().isBefore(end)) {
|
||||||
|
//System.out.println(sr.nextInt());
|
||||||
|
randArr.add( sr.nextInt() );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -13,3 +13,27 @@
|
||||||
- [Java – Write to File](http://www.baeldung.com/java-write-to-file)
|
- [Java – Write to File](http://www.baeldung.com/java-write-to-file)
|
||||||
- [Java Scanner](http://www.baeldung.com/java-scanner)
|
- [Java Scanner](http://www.baeldung.com/java-scanner)
|
||||||
- [Java Timer](http://www.baeldung.com/java-timer-and-timertask)
|
- [Java Timer](http://www.baeldung.com/java-timer-and-timertask)
|
||||||
|
- [Java – Byte Array to Writer](http://www.baeldung.com/java-convert-byte-array-to-writer)
|
||||||
|
- [How to Run a Shell Command in Java](http://www.baeldung.com/run-shell-command-in-java)
|
||||||
|
- [MD5 Hashing in Java](http://www.baeldung.com/java-md5)
|
||||||
|
- [Guide to the Java ArrayList](http://www.baeldung.com/java-arraylist)
|
||||||
|
- [Guide to Java Reflection](http://www.baeldung.com/java-reflection)
|
||||||
|
- [A Guide to Java Sockets](http://www.baeldung.com/a-guide-to-java-sockets)
|
||||||
|
- [Java 8 Collectors](http://www.baeldung.com/java-8-collectors)
|
||||||
|
- [Guide To CompletableFuture](http://www.baeldung.com/java-completablefuture)
|
||||||
|
- [Guide to Java 8’s Functional Interfaces](http://www.baeldung.com/java-8-functional-interfaces)
|
||||||
|
- [Convert char to String in Java](http://www.baeldung.com/java-convert-char-to-string)
|
||||||
|
- [Random List Element](http://www.baeldung.com/java-random-list-element)
|
||||||
|
- [Convert String to int or Integer in Java](http://www.baeldung.com/java-convert-string-to-int-or-integer)
|
||||||
|
- [Java 8 – Powerful Comparison with Lambdas](http://www.baeldung.com/java-8-sort-lambda)
|
||||||
|
- [Java – Directory Size](http://www.baeldung.com/java-folder-size)
|
||||||
|
- [Java – Try with Resources](http://www.baeldung.com/java-try-with-resources)
|
||||||
|
- [A Guide to the Java ExecutorService](http://www.baeldung.com/java-executor-service-tutorial)
|
||||||
|
- [Java 8 New Features](http://www.baeldung.com/java-8-new-features)
|
||||||
|
- [Lambda Expressions and Functional Interfaces: Tips and Best Practices](http://www.baeldung.com/java-8-lambda-expressions-tips)
|
||||||
|
- [The Double Colon Operator in Java 8](http://www.baeldung.com/java-8-double-colon-operator)
|
||||||
|
- [Java 8 Streams Advanced](http://www.baeldung.com/java-8-streams)
|
||||||
|
- [Introduction to Thread Pools in Java](http://www.baeldung.com/thread-pool-java-and-guava)
|
||||||
|
- [Introduction to Java 8 Streams](http://www.baeldung.com/java-8-streams-introduction)
|
||||||
|
- [Guide to the Fork/Join Framework in Java](http://www.baeldung.com/java-fork-join)
|
||||||
|
- [How to Print Screen in Java](http://www.baeldung.com/print-screen-in-java)
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
<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">
|
<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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>core-java</artifactId>
|
<artifactId>core-java</artifactId>
|
||||||
<version>0.1.0-SNAPSHOT</version>
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>core-java</name>
|
<name>core-java</name>
|
||||||
|
|
||||||
|
@ -124,6 +126,12 @@
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>commons-codec</groupId>
|
||||||
|
<artifactId>commons-codec</artifactId>
|
||||||
|
<version>1.10</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -154,21 +162,166 @@
|
||||||
<configuration>
|
<configuration>
|
||||||
<excludes>
|
<excludes>
|
||||||
<exclude>**/*IntegrationTest.java</exclude>
|
<exclude>**/*IntegrationTest.java</exclude>
|
||||||
|
<exclude>**/*LongRunningUnitTest.java</exclude>
|
||||||
|
<exclude>**/*ManualTest.java</exclude>
|
||||||
</excludes>
|
</excludes>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-dependency-plugin</artifactId>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>copy-dependencies</id>
|
||||||
|
<phase>prepare-package</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>copy-dependencies</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<outputDirectory>${project.build.directory}/libs</outputDirectory>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-jar-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<archive>
|
||||||
|
<manifest>
|
||||||
|
<addClasspath>true</addClasspath>
|
||||||
|
<classpathPrefix>libs/</classpathPrefix>
|
||||||
|
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
|
||||||
|
</manifest>
|
||||||
|
</archive>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-assembly-plugin</artifactId>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<phase>package</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>single</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<archive>
|
||||||
|
<manifest>
|
||||||
|
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
|
||||||
|
</manifest>
|
||||||
|
</archive>
|
||||||
|
<descriptorRefs>
|
||||||
|
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||||
|
</descriptorRefs>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-shade-plugin</artifactId>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<goals>
|
||||||
|
<goal>shade</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<shadedArtifactAttached>true</shadedArtifactAttached>
|
||||||
|
<transformers>
|
||||||
|
<transformer
|
||||||
|
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||||
|
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
|
||||||
|
</transformer>
|
||||||
|
</transformers>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
|
<plugin>
|
||||||
|
<groupId>com.jolira</groupId>
|
||||||
|
<artifactId>onejar-maven-plugin</artifactId>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<configuration>
|
||||||
|
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
|
||||||
|
<attachToBuild>true</attachToBuild>
|
||||||
|
<filename>${project.build.finalName}-onejar.${project.packaging}</filename>
|
||||||
|
</configuration>
|
||||||
|
<goals>
|
||||||
|
<goal>one-jar</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<goals>
|
||||||
|
<goal>repackage</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<classifier>spring-boot</classifier>
|
||||||
|
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
</plugins>
|
</plugins>
|
||||||
|
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
|
<profiles>
|
||||||
|
<profile>
|
||||||
|
<id>integration</id>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<phase>integration-test</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>test</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<excludes>
|
||||||
|
<exclude>**/*ManualTest.java</exclude>
|
||||||
|
</excludes>
|
||||||
|
<includes>
|
||||||
|
<include>**/*IntegrationTest.java</include>
|
||||||
|
</includes>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
<configuration>
|
||||||
|
<systemPropertyVariables>
|
||||||
|
<test.mime>json</test.mime>
|
||||||
|
</systemPropertyVariables>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</profile>
|
||||||
|
</profiles>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<!-- persistence -->
|
<!-- persistence -->
|
||||||
<hibernate.version>4.3.11.Final</hibernate.version>
|
<hibernate.version>4.3.11.Final</hibernate.version>
|
||||||
<mysql-connector-java.version>5.1.38</mysql-connector-java.version>
|
<mysql-connector-java.version>5.1.38</mysql-connector-java.version>
|
||||||
|
|
||||||
<!-- marshalling -->
|
<!-- marshalling -->
|
||||||
<jackson.version>2.7.2</jackson.version>
|
<jackson.version>2.7.8</jackson.version>
|
||||||
|
|
||||||
<!-- logging -->
|
<!-- logging -->
|
||||||
<org.slf4j.version>1.7.13</org.slf4j.version>
|
<org.slf4j.version>1.7.13</org.slf4j.version>
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
### Relevant Articles:
|
||||||
|
- [Introduction to the Java 8 Date/Time API](http://www.baeldung.com/java-8-date-time-intro)
|
|
@ -6,11 +6,11 @@ import java.time.Period;
|
||||||
|
|
||||||
public class UseDuration {
|
public class UseDuration {
|
||||||
|
|
||||||
public LocalTime modifyDates(LocalTime localTime,Duration duration){
|
public LocalTime modifyDates(LocalTime localTime, Duration duration) {
|
||||||
return localTime.plus(duration);
|
return localTime.plus(duration);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Duration getDifferenceBetweenDates(LocalTime localTime1,LocalTime localTime2){
|
public Duration getDifferenceBetweenDates(LocalTime localTime1, LocalTime localTime2) {
|
||||||
return Duration.between(localTime1, localTime2);
|
return Duration.between(localTime1, localTime2);
|
||||||
}
|
}
|
||||||
}
|
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue