From 23f2d852495e7f479fa05c51d848a68cfb2fae4e Mon Sep 17 00:00:00 2001 From: Fabio Scotto di Santolo Date: Sat, 30 Mar 2019 00:33:26 +0100 Subject: [PATCH] Aggiunto l'esempio di JAXB e JSON-P --- .idea/codeStyles/codeStyleConfig.xml | 5 ++ .idea/compiler.xml | 24 +++++++ .idea/encodings.xml | 9 +++ .idea/misc.xml | 32 +++++++++ .idea/vcs.xml | 6 ++ beanvalidationdemo/beanvalidationdemo.iml | 39 +--------- cdidemo/cdidemo.iml | 33 +-------- jeedemo.iml | 12 +--- jpademo/jpademo.iml | 28 -------- pom.xml | 1 + xmlandjsondemo/pom.xml | 71 +++++++++++++++++++ .../java/it/plague/jeedemo/CreditCard.java | 26 +++++++ .../it/plague/jeedemo/CreditCardJSonTest.java | 37 ++++++++++ .../it/plague/jeedemo/CreditCardXmlTest.java | 46 ++++++++++++ xmlandjsondemo/xmlandjsondemo.iml | 2 + 15 files changed, 262 insertions(+), 109 deletions(-) create mode 100644 .idea/codeStyles/codeStyleConfig.xml create mode 100644 .idea/compiler.xml create mode 100644 .idea/encodings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/vcs.xml create mode 100644 xmlandjsondemo/pom.xml create mode 100644 xmlandjsondemo/src/main/java/it/plague/jeedemo/CreditCard.java create mode 100644 xmlandjsondemo/src/test/java/it/plague/jeedemo/CreditCardJSonTest.java create mode 100644 xmlandjsondemo/src/test/java/it/plague/jeedemo/CreditCardXmlTest.java create mode 100644 xmlandjsondemo/xmlandjsondemo.iml diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..b9d18bf --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..07edf43 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..b8f3ecd --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..6e79789 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + Angular + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/beanvalidationdemo/beanvalidationdemo.iml b/beanvalidationdemo/beanvalidationdemo.iml index 110df22..74f3f13 100644 --- a/beanvalidationdemo/beanvalidationdemo.iml +++ b/beanvalidationdemo/beanvalidationdemo.iml @@ -1,39 +1,2 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file + \ No newline at end of file diff --git a/cdidemo/cdidemo.iml b/cdidemo/cdidemo.iml index 9d53095..74f3f13 100644 --- a/cdidemo/cdidemo.iml +++ b/cdidemo/cdidemo.iml @@ -1,33 +1,2 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file + \ No newline at end of file diff --git a/jeedemo.iml b/jeedemo.iml index 4fd5057..74f3f13 100644 --- a/jeedemo.iml +++ b/jeedemo.iml @@ -1,12 +1,2 @@ - - - - - - - - - - - \ No newline at end of file + \ No newline at end of file diff --git a/jpademo/jpademo.iml b/jpademo/jpademo.iml index 13d6ecc..93e6728 100644 --- a/jpademo/jpademo.iml +++ b/jpademo/jpademo.iml @@ -10,32 +10,4 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/pom.xml b/pom.xml index 2ed2870..b18fc0d 100644 --- a/pom.xml +++ b/pom.xml @@ -12,6 +12,7 @@ cdidemo beanvalidationdemo jpademo + xmlandjsondemo diff --git a/xmlandjsondemo/pom.xml b/xmlandjsondemo/pom.xml new file mode 100644 index 0000000..601969e --- /dev/null +++ b/xmlandjsondemo/pom.xml @@ -0,0 +1,71 @@ + + + 4.0.0 + xmlandjsondemo + xmlandjsondemo + + + jeedemo + it.plague.jeedemo + 1.0-SNAPSHOT + + + + UTF-8 + 1.7 + 1.7 + + + + + javax.json + javax.json-api + 1.1 + + + org.glassfish + javax.json + 1.1 + + + org.projectlombok + lombok + provided + + + junit + junit + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.0 + + 1.7 + 1.7 + + + + org.apache.maven.plugins + maven-failsafe-plugin + 2.12.4 + + + integration-test + + integration-test + verify + + + + + + + diff --git a/xmlandjsondemo/src/main/java/it/plague/jeedemo/CreditCard.java b/xmlandjsondemo/src/main/java/it/plague/jeedemo/CreditCard.java new file mode 100644 index 0000000..13ab105 --- /dev/null +++ b/xmlandjsondemo/src/main/java/it/plague/jeedemo/CreditCard.java @@ -0,0 +1,26 @@ +package it.plague.jeedemo; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +@XmlRootElement +@XmlAccessorType(XmlAccessType.FIELD) +public class CreditCard { + + @XmlAttribute + private String number; + @XmlElement(name = "expiry_date") + private String expiryDate; + @XmlElement(name = "control_number") + private Integer controlNumber; + private String type; +} diff --git a/xmlandjsondemo/src/test/java/it/plague/jeedemo/CreditCardJSonTest.java b/xmlandjsondemo/src/test/java/it/plague/jeedemo/CreditCardJSonTest.java new file mode 100644 index 0000000..37c0307 --- /dev/null +++ b/xmlandjsondemo/src/test/java/it/plague/jeedemo/CreditCardJSonTest.java @@ -0,0 +1,37 @@ +package it.plague.jeedemo; + +import static org.junit.Assert.*; + +import java.io.StringWriter; +import javax.json.Json; +import javax.json.stream.JsonGenerator; +import org.junit.Test; + +public class CreditCardJSonTest { + + public static final String creditCardJSon = + "{\"creditCard\":" + + "{\"number\":\"12345678\"," + + "\"expiryDate\":\"10/14\"," + + "\"controlNumber\":566," + + "\"type\":\"Visa\"}" + + "}"; + + @Test + public void shouldGenerateACreditCard() { + CreditCard creditCard = new CreditCard("12345678", "10/14", 566, "Visa"); + StringWriter writer = new StringWriter(); + JsonGenerator generator = Json.createGenerator(writer); + generator.writeStartObject() + .writeStartObject("creditCard") + .write("number", creditCard.getNumber()) + .write("expiryDate", creditCard.getExpiryDate()) + .write("controlNumber", creditCard.getControlNumber()) + .write("type", creditCard.getType()) + .writeEnd() + .writeEnd() + .close(); + assertEquals(creditCardJSon, writer.toString().trim()); + } + +} diff --git a/xmlandjsondemo/src/test/java/it/plague/jeedemo/CreditCardXmlTest.java b/xmlandjsondemo/src/test/java/it/plague/jeedemo/CreditCardXmlTest.java new file mode 100644 index 0000000..12d8119 --- /dev/null +++ b/xmlandjsondemo/src/test/java/it/plague/jeedemo/CreditCardXmlTest.java @@ -0,0 +1,46 @@ +package it.plague.jeedemo; + +import static org.junit.Assert.assertEquals; + +import java.io.StringReader; +import java.io.StringWriter; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Marshaller; +import javax.xml.bind.Unmarshaller; +import org.junit.Test; + +public class CreditCardXmlTest { + + public static final String creditCardXml = + "" + + "" + + "10/14" + + "566" + + "Visa" + + ""; + + @Test + public void shouldMarshallACreditCard() throws JAXBException { + CreditCard creditCard = new CreditCard("12345678", "10/14", 566, "Visa"); + StringWriter writer = new StringWriter(); + JAXBContext context = JAXBContext.newInstance(CreditCard.class); + Marshaller marshaller = context.createMarshaller(); + marshaller.marshal(creditCard, writer); + System.out.println(writer); + assertEquals(creditCardXml, writer.toString().trim()); + } + + @Test + public void shouldUnmarshallACreditCard() throws JAXBException { + StringReader reader = new StringReader(creditCardXml); + JAXBContext context = JAXBContext.newInstance(CreditCard.class); + Unmarshaller unmarshaller = context.createUnmarshaller(); + CreditCard creditCard = (CreditCard) unmarshaller.unmarshal(reader); + assertEquals("12345678", creditCard.getNumber()); + assertEquals("10/14", creditCard.getExpiryDate()); + assertEquals((Object) 566, creditCard.getControlNumber()); + assertEquals("Visa", creditCard.getType()); + } + +} diff --git a/xmlandjsondemo/xmlandjsondemo.iml b/xmlandjsondemo/xmlandjsondemo.iml new file mode 100644 index 0000000..78b2cc5 --- /dev/null +++ b/xmlandjsondemo/xmlandjsondemo.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file