Merge remote-tracking branch 'upstream/master'
This commit is contained in:
@@ -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>
|
||||
+132
@@ -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("}");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+8
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
+22
@@ -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>
|
||||
<properties>
|
||||
<cxf.version>3.1.6</cxf.version>
|
||||
<surefire.version>2.19.1</surefire.version>
|
||||
</properties>
|
||||
<build>
|
||||
<plugins>
|
||||
@@ -26,7 +27,7 @@
|
||||
<version>2.19.1</version>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/StudentTest.java</exclude>
|
||||
<exclude>**/*LiveTest.java</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
@@ -44,4 +45,5 @@
|
||||
<version>${cxf.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
||||
+13
-17
@@ -2,20 +2,16 @@ package com.baeldung.cxf.introduction;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.ws.Service;
|
||||
import javax.xml.ws.soap.SOAPBinding;
|
||||
|
||||
import com.baeldung.cxf.introduction.Baeldung;
|
||||
import com.baeldung.cxf.introduction.Student;
|
||||
import com.baeldung.cxf.introduction.StudentImpl;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class StudentTest {
|
||||
public class StudentLiveTest {
|
||||
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");
|
||||
|
||||
@@ -25,7 +21,7 @@ public class StudentTest {
|
||||
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -37,28 +33,28 @@ public class StudentTest {
|
||||
|
||||
@Test
|
||||
public void whenUsingHelloMethod_thenCorrect() {
|
||||
String endpointResponse = baeldungProxy.hello("Baeldung");
|
||||
String localResponse = baeldungImpl.hello("Baeldung");
|
||||
final String endpointResponse = baeldungProxy.hello("Baeldung");
|
||||
final String localResponse = baeldungImpl.hello("Baeldung");
|
||||
assertEquals(localResponse, endpointResponse);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingHelloStudentMethod_thenCorrect() {
|
||||
Student student = new StudentImpl("John Doe");
|
||||
String endpointResponse = baeldungProxy.helloStudent(student);
|
||||
String localResponse = baeldungImpl.helloStudent(student);
|
||||
final Student student = new StudentImpl("John Doe");
|
||||
final String endpointResponse = baeldungProxy.helloStudent(student);
|
||||
final String localResponse = baeldungImpl.helloStudent(student);
|
||||
assertEquals(localResponse, endpointResponse);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void usingGetStudentsMethod_thenCorrect() {
|
||||
Student student1 = new StudentImpl("Adam");
|
||||
final Student student1 = new StudentImpl("Adam");
|
||||
baeldungProxy.helloStudent(student1);
|
||||
|
||||
Student student2 = new StudentImpl("Eve");
|
||||
final Student student2 = new StudentImpl("Eve");
|
||||
baeldungProxy.helloStudent(student2);
|
||||
|
||||
Map<Integer, Student> students = baeldungProxy.getStudents();
|
||||
|
||||
final Map<Integer, Student> students = baeldungProxy.getStudents();
|
||||
assertEquals("Adam", students.get(1).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>
|
||||
+86
@@ -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()));
|
||||
}
|
||||
}
|
||||
+72
@@ -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;
|
||||
}
|
||||
}
|
||||
+21
@@ -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);
|
||||
}
|
||||
}
|
||||
+35
@@ -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>
|
||||
+130
@@ -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>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>StudentTest.java</exclude>
|
||||
<exclude>**/*LiveTest.java</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
@@ -60,7 +60,7 @@
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>integration</id>
|
||||
<id>live</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
|
||||
+1
-1
@@ -6,7 +6,7 @@ import org.junit.Test;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
|
||||
public class StudentTest {
|
||||
public class StudentLiveTest {
|
||||
private ApplicationContext context = new AnnotationConfigApplicationContext(ClientConfiguration.class);
|
||||
private Baeldung baeldungProxy = (Baeldung) context.getBean("client");
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
<modules>
|
||||
<module>cxf-introduction</module>
|
||||
<module>cxf-spring</module>
|
||||
<module>cxf-jaxrs-implementation</module>
|
||||
</modules>
|
||||
<dependencies>
|
||||
<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>
|
||||
+46
-1
@@ -97,6 +97,14 @@
|
||||
<version>8.0.2</version>
|
||||
</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>
|
||||
<groupId>net.sf.jtidy</groupId>
|
||||
<artifactId>jtidy</artifactId>
|
||||
@@ -132,7 +140,8 @@
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*IntegrationTest.java</exclude>
|
||||
<exclude>**/*IntegrationTest.java</exclude>
|
||||
<exclude>**/*LiveTest.java</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
@@ -141,6 +150,42 @@
|
||||
|
||||
</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>
|
||||
<!-- persistence -->
|
||||
<hibernate.version>4.3.11.Final</hibernate.version>
|
||||
|
||||
+1
-1
@@ -25,7 +25,7 @@ import org.junit.Test;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.tidy.Tidy;
|
||||
|
||||
public class ApacheFOPConvertHTMLTest {
|
||||
public class ApacheFOPConvertHTMLIntegrationTest {
|
||||
private String inputFile = "src/test/resources/input.html";
|
||||
private String style = "src/test/resources/xhtml2fo.xsl";
|
||||
private String style1 = "src/test/resources/docbook-xsl/fo/docbook.xsl";
|
||||
+1
-1
@@ -30,7 +30,7 @@ import org.dbdoclet.trafo.script.Script;
|
||||
import org.junit.Test;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
public class ApacheFOPHeroldTest {
|
||||
public class ApacheFOPHeroldLiveTest {
|
||||
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/25/building-a-restful-web-service-with-spring-3-1-and-java-based-configuration-part-2/",
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user