forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
21e996b
commit 1d692a9
Showing
8 changed files
with
211 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<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-aegis</artifactId> | ||
<parent> | ||
<groupId>com.baeldung</groupId> | ||
<artifactId>apache-cxf</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
</parent> | ||
<properties> | ||
<cxf.version>3.1.8</cxf.version> | ||
</properties> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.apache.cxf</groupId> | ||
<artifactId>cxf-rt-databinding-aegis</artifactId> | ||
<version>${cxf.version}</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
42 changes: 42 additions & 0 deletions
42
apache-cxf/cxf-aegis/src/main/java/com/baeldung/cxf/aegis/Course.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.baeldung.cxf.aegis; | ||
|
||
import java.util.Date; | ||
|
||
public class Course { | ||
private int id; | ||
private String name; | ||
private String instructor; | ||
private Date enrolmentDate; | ||
|
||
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 String getInstructor() { | ||
return instructor; | ||
} | ||
|
||
public void setInstructor(String instructor) { | ||
this.instructor = instructor; | ||
} | ||
|
||
public Date getEnrolmentDate() { | ||
return enrolmentDate; | ||
} | ||
|
||
public void setEnrolmentDate(Date enrolmentDate) { | ||
this.enrolmentDate = enrolmentDate; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
apache-cxf/cxf-aegis/src/main/java/com/baeldung/cxf/aegis/CourseRepo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.baeldung.cxf.aegis; | ||
|
||
import java.util.Map; | ||
|
||
public interface CourseRepo { | ||
String getGreeting(); | ||
void setGreeting(String greeting); | ||
Map<Integer, Course> getCourses(); | ||
void setCourses(Map<Integer, Course> courses); | ||
void addCourse(Course course); | ||
} |
34 changes: 34 additions & 0 deletions
34
apache-cxf/cxf-aegis/src/main/java/com/baeldung/cxf/aegis/CourseRepoImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.baeldung.cxf.aegis; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class CourseRepoImpl implements CourseRepo { | ||
private String greeting; | ||
private Map<Integer, Course> courses = new HashMap<>(); | ||
|
||
@Override | ||
public String getGreeting() { | ||
return greeting; | ||
} | ||
|
||
@Override | ||
public void setGreeting(String greeting) { | ||
this.greeting = greeting; | ||
} | ||
|
||
@Override | ||
public Map<Integer, Course> getCourses() { | ||
return courses; | ||
} | ||
|
||
@Override | ||
public void setCourses(Map<Integer, Course> courses) { | ||
this.courses = courses; | ||
} | ||
|
||
@Override | ||
public void addCourse(Course course) { | ||
courses.put(course.getId(), course); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
apache-cxf/cxf-aegis/src/main/resources/com/baeldung/cxf/aegis/Course.aegis.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<mappings> | ||
<mapping> | ||
<property name="instructor" ignore="true"/> | ||
</mapping> | ||
</mappings> |
5 changes: 5 additions & 0 deletions
5
apache-cxf/cxf-aegis/src/main/resources/com/baeldung/cxf/aegis/CourseRepo.aegis.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<mappings xmlns:ns="http://courserepo.baeldung.com"> | ||
<mapping name="ns:Baeldung"> | ||
<property name="greeting" style="attribute"/> | ||
</mapping> | ||
</mappings> |
93 changes: 93 additions & 0 deletions
93
apache-cxf/cxf-aegis/src/test/java/com/baeldung/cxf/aegis/BaeldungTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package com.baeldung.cxf.aegis; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNull; | ||
|
||
import org.junit.Test; | ||
|
||
import java.io.FileInputStream; | ||
import java.io.FileOutputStream; | ||
import java.lang.reflect.Type; | ||
import java.util.Date; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import javax.xml.namespace.QName; | ||
import javax.xml.stream.XMLInputFactory; | ||
import javax.xml.stream.XMLOutputFactory; | ||
import javax.xml.stream.XMLStreamReader; | ||
import javax.xml.stream.XMLStreamWriter; | ||
|
||
import org.apache.cxf.aegis.AegisContext; | ||
import org.apache.cxf.aegis.AegisReader; | ||
import org.apache.cxf.aegis.AegisWriter; | ||
import org.apache.cxf.aegis.type.AegisType; | ||
|
||
public class BaeldungTest { | ||
private AegisContext context; | ||
private String fileName = "baeldung.xml"; | ||
|
||
@Test | ||
public void whenMarshalingAndUnmarshalingCourseRepo_thenCorrect() throws Exception { | ||
initializeContext(); | ||
CourseRepo inputRepo = initCourseRepo(); | ||
marshalCourseRepo(inputRepo); | ||
CourseRepo outputRepo = unmarshalCourseRepo(); | ||
Course restCourse = outputRepo.getCourses().get(1); | ||
Course securityCourse = outputRepo.getCourses().get(2); | ||
assertEquals("Welcome to Beldung!", outputRepo.getGreeting()); | ||
assertEquals("REST with Spring", restCourse.getName()); | ||
assertEquals(new Date(1234567890000L), restCourse.getEnrolmentDate()); | ||
assertNull(restCourse.getInstructor()); | ||
assertEquals("Learn Spring Security", securityCourse.getName()); | ||
assertEquals(new Date(1456789000000L), securityCourse.getEnrolmentDate()); | ||
assertNull(securityCourse.getInstructor()); | ||
} | ||
|
||
private void initializeContext() { | ||
context = new AegisContext(); | ||
Set<Type> rootClasses = new HashSet<Type>(); | ||
rootClasses.add(CourseRepo.class); | ||
context.setRootClasses(rootClasses); | ||
Map<Class<?>, String> beanImplementationMap = new HashMap<>(); | ||
beanImplementationMap.put(CourseRepoImpl.class, "CourseRepo"); | ||
context.setBeanImplementationMap(beanImplementationMap); | ||
context.setWriteXsiTypes(true); | ||
context.initialize(); | ||
} | ||
|
||
private CourseRepoImpl initCourseRepo() { | ||
Course restCourse = new Course(); | ||
restCourse.setId(1); | ||
restCourse.setName("REST with Spring"); | ||
restCourse.setInstructor("Eugen"); | ||
restCourse.setEnrolmentDate(new Date(1234567890000L)); | ||
Course securityCourse = new Course(); | ||
securityCourse.setId(2); | ||
securityCourse.setName("Learn Spring Security"); | ||
securityCourse.setInstructor("Eugen"); | ||
securityCourse.setEnrolmentDate(new Date(1456789000000L)); | ||
CourseRepoImpl courseRepo = new CourseRepoImpl(); | ||
courseRepo.setGreeting("Welcome to Beldung!"); | ||
courseRepo.addCourse(restCourse); | ||
courseRepo.addCourse(securityCourse); | ||
return courseRepo; | ||
} | ||
|
||
private void marshalCourseRepo(CourseRepo courseRepo) throws Exception { | ||
AegisWriter<XMLStreamWriter> writer = context.createXMLStreamWriter(); | ||
AegisType aegisType = context.getTypeMapping().getType(CourseRepo.class); | ||
XMLStreamWriter xmlWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(new FileOutputStream(fileName)); | ||
writer.write(courseRepo, new QName("http://aegis.cxf.baeldung.com", "baeldung"), false, xmlWriter, aegisType); | ||
xmlWriter.close(); | ||
} | ||
|
||
private CourseRepo unmarshalCourseRepo() throws Exception { | ||
AegisReader<XMLStreamReader> reader = context.createXMLStreamReader(); | ||
XMLStreamReader xmlReader = XMLInputFactory.newInstance().createXMLStreamReader(new FileInputStream(fileName)); | ||
CourseRepo courseRepo = (CourseRepo) reader.read(xmlReader, context.getTypeMapping().getType(CourseRepo.class)); | ||
xmlReader.close(); | ||
return courseRepo; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters