BAEL-3091: The Prototype Pattern in Java (changed code based on valid comments from a reader)

This commit is contained in:
Vivek Balasubramaniam
2019-10-29 22:27:15 +05:30
parent db85c8f275
commit d3d5b060e7
20517 changed files with 1642290 additions and 0 deletions
@@ -0,0 +1,52 @@
package com.baeldung.jaxb.dateunmarshalling;
import org.junit.Test;
import javax.xml.bind.JAXBException;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import static org.junit.Assert.assertEquals;
public class JaxbDateUnmarshallingUnitTest {
@Test
public void whenUnmarshalDatesIsCalled_ThenCorrectDateIsReturned() throws JAXBException, DatatypeConfigurationException {
InputStream inputStream = JaxbDateUnmarshalling.getInputStream(JaxbDateUnmarshalling.DEFAULT_DATE_UNMARSHALLING_FILE);
XMLGregorianCalendar expected = DatatypeFactory.newInstance().newXMLGregorianCalendar("1979-11-28T02:31:32");
Book book = JaxbDateUnmarshalling.unmarshalDates(inputStream);
assertEquals(expected, book.getPublished());
}
@Test
public void whenUnmarshalDatesUsingCustomXmlAdapterIsCalled_ThenCorrectDateIsReturned() throws JAXBException, ParseException {
InputStream inputStream = JaxbDateUnmarshalling.getInputStream(JaxbDateUnmarshalling.CUSTOM_DATE_UNMARSHALLING_FILE);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date expected = format.parse("1979-11-28 02:31:32");
BookDateAdapter book = JaxbDateUnmarshalling.unmarshalDatesUsingCustomXmlAdapter(inputStream);
assertEquals(expected, book.getPublished());
}
@Test
public void whenUnmarshalDatesUsingJava8IsCalled_ThenCorrectDateIsReturned() throws JAXBException {
InputStream inputStream = JaxbDateUnmarshalling.getInputStream(JaxbDateUnmarshalling.CUSTOM_DATE_UNMARSHALLING_FILE);
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime expected = LocalDateTime.parse("1979-11-28 02:31:32", dateFormat);
BookLocalDateTimeAdapter book = JaxbDateUnmarshalling.unmarshalDatesUsingJava8(inputStream);
assertEquals(expected, book.getPublished());
}
}
@@ -0,0 +1,57 @@
package com.baeldung.jaxb.test;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Date;
import java.util.TimeZone;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.apache.commons.io.FileUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import com.baeldung.jaxb.Book;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class JaxbIntegrationTest {
private Book book;
private JAXBContext context;
@Before
public void before() throws JAXBException {
book = new Book();
book.setId(1L);
book.setName("Book1");
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
book.setDate(new Date(1481909329718L));
context = JAXBContext.newInstance(Book.class);
}
@Test
public void marshal() throws JAXBException, IOException {
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(book, new File(this.getClass().getResource("/").getPath() + "/book.xml"));
File sampleBookFile = new File(this.getClass().getResource("/sample_book.xml").getFile());
File bookFile = new File(this.getClass().getResource("/book.xml").getFile());
String sampleBookXML = FileUtils.readFileToString(sampleBookFile, "UTF-8");
String marshallerBookXML = FileUtils.readFileToString(bookFile, "UTF-8");
Assert.assertEquals(sampleBookXML.replace("\r", "").replace("\n", ""), marshallerBookXML.replace("\r", "").replace("\n", ""));
}
@Test
public void unMashal() throws JAXBException, IOException {
Unmarshaller unmarshaller = context.createUnmarshaller();
String bookFile = this.getClass().getResource("/book.xml").getFile();
Book unMarshallerbook = (Book) unmarshaller.unmarshal(new FileReader(bookFile));
Assert.assertEquals(book, unMarshallerbook);
}
}
+5
View File
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<book id="1">
<title>Book1</title>
<date>2016-12-16T17:28:49.718Z</date>
</book>
+5
View File
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<book id="1">
<title>Book1</title>
<date>2016-12-16T17:28:49.718Z</date>
</book>