Xstream - XML to Object
xml moved to resources folder, test cases fixed
This commit is contained in:
		
							parent
							
								
									f7594ee89c
								
							
						
					
					
						commit
						f214e0a285
					
				| @ -1,23 +1,14 @@ | |||||||
| <?xml version="1.0" encoding="UTF-8"?> | <?xml version="1.0" encoding="UTF-8"?> | ||||||
| <projectDescription> | <projectDescription> | ||||||
| 	<name>xstream-xmlToObject</name> |   <name>xstream-xmlToObject</name> | ||||||
| 	<comment></comment> |   <comment>NO_M2ECLIPSE_SUPPORT: Project files created with the maven-eclipse-plugin are not supported in M2Eclipse.</comment> | ||||||
| 	<projects> |   <projects/> | ||||||
| 	</projects> |   <buildSpec> | ||||||
| 	<buildSpec> |     <buildCommand> | ||||||
| 		<buildCommand> |       <name>org.eclipse.jdt.core.javabuilder</name> | ||||||
| 			<name>org.eclipse.jdt.core.javabuilder</name> |     </buildCommand> | ||||||
| 			<arguments> |   </buildSpec> | ||||||
| 			</arguments> |   <natures> | ||||||
| 		</buildCommand> |     <nature>org.eclipse.jdt.core.javanature</nature> | ||||||
| 		<buildCommand> |   </natures> | ||||||
| 			<name>org.eclipse.m2e.core.maven2Builder</name> |  | ||||||
| 			<arguments> |  | ||||||
| 			</arguments> |  | ||||||
| 		</buildCommand> |  | ||||||
| 	</buildSpec> |  | ||||||
| 	<natures> |  | ||||||
| 		<nature>org.eclipse.jdt.core.javanature</nature> |  | ||||||
| 		<nature>org.eclipse.m2e.core.maven2Nature</nature> |  | ||||||
| 	</natures> |  | ||||||
| </projectDescription> | </projectDescription> | ||||||
| @ -4,9 +4,9 @@ import com.thoughtworks.xstream.XStream; | |||||||
| 
 | 
 | ||||||
| public class SimpleXstreamInitializer { | public class SimpleXstreamInitializer { | ||||||
| 
 | 
 | ||||||
| 	private static XStream xtreamInstance; | 	private XStream xtreamInstance; | ||||||
| 
 | 
 | ||||||
| 	public static XStream getXstreamInstance() { | 	public XStream getXstreamInstance() { | ||||||
| 		if (xtreamInstance == null) { | 		if (xtreamInstance == null) { | ||||||
| 			synchronized (SimpleXstreamInitializer.class) { | 			synchronized (SimpleXstreamInitializer.class) { | ||||||
| 				if (xtreamInstance == null) { | 				if (xtreamInstance == null) { | ||||||
|  | |||||||
| @ -1,6 +1,5 @@ | |||||||
| package com.baeldung.pojo.test; | package com.baeldung.pojo.test; | ||||||
| 
 | 
 | ||||||
| import java.io.File; |  | ||||||
| import java.io.FileReader; | import java.io.FileReader; | ||||||
| import java.io.IOException; | import java.io.IOException; | ||||||
| 
 | 
 | ||||||
| @ -18,14 +17,16 @@ public class ComplexXmlToObjectAnnotationTest { | |||||||
| 
 | 
 | ||||||
| 	@Before | 	@Before | ||||||
| 	public void dataSetup() { | 	public void dataSetup() { | ||||||
| 		xstream = SimpleXstreamInitializer.getXstreamInstance(); | 		SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); | ||||||
|  | 		xstream = simpleXstreamInitializer.getXstreamInstance(); | ||||||
| 		xstream.processAnnotations(Customer.class); | 		xstream.processAnnotations(Customer.class); | ||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
| 	@Test | 	@Test | ||||||
| 	public void convertXmlToObjectFromFile() { | 	public void convertXmlToObjectFromFile() { | ||||||
| 		try { | 		try { | ||||||
| 			FileReader reader = new FileReader(new File("data-file-alias-field-complex.xml")); | 			ClassLoader classLoader = getClass().getClassLoader(); | ||||||
|  | 			FileReader reader = new FileReader(classLoader.getResource("data-file-alias-field-complex.xml").getFile()); | ||||||
| 			Customer customer = (Customer) xstream.fromXML(reader); | 			Customer customer = (Customer) xstream.fromXML(reader); | ||||||
| 			Assert.assertNotNull(customer); | 			Assert.assertNotNull(customer); | ||||||
| 			Assert.assertNotNull(customer.getContactDetailsList()); | 			Assert.assertNotNull(customer.getContactDetailsList()); | ||||||
|  | |||||||
| @ -1,6 +1,5 @@ | |||||||
| package com.baeldung.pojo.test; | package com.baeldung.pojo.test; | ||||||
| 
 | 
 | ||||||
| import java.io.File; |  | ||||||
| import java.io.FileReader; | import java.io.FileReader; | ||||||
| import java.io.IOException; | import java.io.IOException; | ||||||
| 
 | 
 | ||||||
| @ -8,8 +7,8 @@ import org.junit.Assert; | |||||||
| import org.junit.Before; | import org.junit.Before; | ||||||
| import org.junit.Test; | import org.junit.Test; | ||||||
| 
 | 
 | ||||||
| import com.baeldung.implicit.collection.pojo.ContactDetails; | import com.baeldung.complex.pojo.ContactDetails; | ||||||
| import com.baeldung.implicit.collection.pojo.Customer; | import com.baeldung.complex.pojo.Customer; | ||||||
| import com.baeldung.initializer.SimpleXstreamInitializer; | import com.baeldung.initializer.SimpleXstreamInitializer; | ||||||
| import com.thoughtworks.xstream.XStream; | import com.thoughtworks.xstream.XStream; | ||||||
| 
 | 
 | ||||||
| @ -19,14 +18,16 @@ public class ComplexXmlToObjectAttributeCollectionTest { | |||||||
| 
 | 
 | ||||||
| 	@Before | 	@Before | ||||||
| 	public void dataSetup() { | 	public void dataSetup() { | ||||||
| 		xstream = SimpleXstreamInitializer.getXstreamInstance(); | 		SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); | ||||||
|  | 		xstream = simpleXstreamInitializer.getXstreamInstance(); | ||||||
| 		xstream.processAnnotations(Customer.class); | 		xstream.processAnnotations(Customer.class); | ||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
| 	@Test | 	@Test | ||||||
| 	public void convertXmlToObjectFromFile() { | 	public void convertXmlToObjectFromFile() { | ||||||
| 		try { | 		try { | ||||||
| 			FileReader reader = new FileReader(new File("data-file-alias-implicit-collection.xml")); | 			ClassLoader classLoader = getClass().getClassLoader(); | ||||||
|  | 			FileReader reader = new FileReader(classLoader.getResource("data-file-alias-field-complex.xml").getFile()); | ||||||
| 			Customer customer = (Customer) xstream.fromXML(reader); | 			Customer customer = (Customer) xstream.fromXML(reader); | ||||||
| 			Assert.assertNotNull(customer); | 			Assert.assertNotNull(customer); | ||||||
| 			Assert.assertNotNull(customer.getContactDetailsList()); | 			Assert.assertNotNull(customer.getContactDetailsList()); | ||||||
|  | |||||||
| @ -1,6 +1,5 @@ | |||||||
| package com.baeldung.pojo.test; | package com.baeldung.pojo.test; | ||||||
| 
 | 
 | ||||||
| import java.io.File; |  | ||||||
| import java.io.FileReader; | import java.io.FileReader; | ||||||
| import java.io.IOException; | import java.io.IOException; | ||||||
| 
 | 
 | ||||||
| @ -18,14 +17,16 @@ public class ComplexXmlToObjectCollectionTest { | |||||||
| 
 | 
 | ||||||
| 	@Before | 	@Before | ||||||
| 	public void dataSetup() { | 	public void dataSetup() { | ||||||
| 		xstream = SimpleXstreamInitializer.getXstreamInstance(); | 		SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); | ||||||
|  | 		xstream = simpleXstreamInitializer.getXstreamInstance(); | ||||||
| 		xstream.processAnnotations(Customer.class); | 		xstream.processAnnotations(Customer.class); | ||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
| 	@Test | 	@Test | ||||||
| 	public void convertXmlToObjectFromFile() { | 	public void convertXmlToObjectFromFile() { | ||||||
| 		try { | 		try { | ||||||
| 			FileReader reader = new FileReader(new File("data-file-alias-implicit-collection.xml")); | 			ClassLoader classLoader = getClass().getClassLoader(); | ||||||
|  | 			FileReader reader = new FileReader(classLoader.getResource("data-file-alias-implicit-collection.xml").getFile()); | ||||||
| 			Customer customer = (Customer) xstream.fromXML(reader); | 			Customer customer = (Customer) xstream.fromXML(reader); | ||||||
| 			Assert.assertNotNull(customer); | 			Assert.assertNotNull(customer); | ||||||
| 			Assert.assertNotNull(customer.getContactDetailsList()); | 			Assert.assertNotNull(customer.getContactDetailsList()); | ||||||
|  | |||||||
| @ -1,6 +1,5 @@ | |||||||
| package com.baeldung.pojo.test; | package com.baeldung.pojo.test; | ||||||
| 
 | 
 | ||||||
| import java.io.File; |  | ||||||
| import java.io.FileReader; | import java.io.FileReader; | ||||||
| import java.io.IOException; | import java.io.IOException; | ||||||
| 
 | 
 | ||||||
| @ -18,14 +17,16 @@ public class XmlToObjectAliasTest { | |||||||
| 
 | 
 | ||||||
| 	@Before | 	@Before | ||||||
| 	public void dataSetup() { | 	public void dataSetup() { | ||||||
| 		xstream = SimpleXstreamInitializer.getXstreamInstance(); | 		SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); | ||||||
|  | 		xstream = simpleXstreamInitializer.getXstreamInstance(); | ||||||
| 		xstream.alias("customer" , Customer.class); | 		xstream.alias("customer" , Customer.class); | ||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
| 	@Test | 	@Test | ||||||
| 	public void convertXmlToObjectFromFile() { | 	public void convertXmlToObjectFromFile() { | ||||||
| 		try { | 		try { | ||||||
| 			FileReader reader = new FileReader(new File("data-file-alias.xml")); | 			ClassLoader classLoader = getClass().getClassLoader(); | ||||||
|  | 			FileReader reader = new FileReader(classLoader.getResource("data-file-alias.xml").getFile()); | ||||||
| 			Customer customer = (Customer) xstream.fromXML(reader); | 			Customer customer = (Customer) xstream.fromXML(reader); | ||||||
| 			Assert.assertNotNull(customer); | 			Assert.assertNotNull(customer); | ||||||
| 			 | 			 | ||||||
|  | |||||||
| @ -1,6 +1,5 @@ | |||||||
| package com.baeldung.pojo.test; | package com.baeldung.pojo.test; | ||||||
| 
 | 
 | ||||||
| import java.io.File; |  | ||||||
| import java.io.FileReader; | import java.io.FileReader; | ||||||
| import java.io.IOException; | import java.io.IOException; | ||||||
| 
 | 
 | ||||||
| @ -18,14 +17,16 @@ public class XmlToObjectAnnotationTest { | |||||||
| 
 | 
 | ||||||
| 	@Before | 	@Before | ||||||
| 	public void dataSetup() { | 	public void dataSetup() { | ||||||
| 		xstream = SimpleXstreamInitializer.getXstreamInstance(); | 		SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); | ||||||
|  | 		xstream = simpleXstreamInitializer.getXstreamInstance(); | ||||||
| 		xstream.processAnnotations(Customer.class); | 		xstream.processAnnotations(Customer.class); | ||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
| 	@Test | 	@Test | ||||||
| 	public void convertXmlToObjectFromFile() { | 	public void convertXmlToObjectFromFile() { | ||||||
| 		try { | 		try { | ||||||
| 			FileReader reader = new FileReader(new File("data-file-alias-field.xml")); | 			ClassLoader classLoader = getClass().getClassLoader(); | ||||||
|  | 			FileReader reader = new FileReader(classLoader.getResource("data-file-alias-field.xml").getFile()); | ||||||
| 			Customer customer = (Customer) xstream.fromXML(reader); | 			Customer customer = (Customer) xstream.fromXML(reader); | ||||||
| 			Assert.assertNotNull(customer); | 			Assert.assertNotNull(customer); | ||||||
| 			Assert.assertNotNull(customer.getFirstName()); | 			Assert.assertNotNull(customer.getFirstName()); | ||||||
|  | |||||||
| @ -1,6 +1,5 @@ | |||||||
| package com.baeldung.pojo.test; | package com.baeldung.pojo.test; | ||||||
| 
 | 
 | ||||||
| import java.io.File; |  | ||||||
| import java.io.FileReader; | import java.io.FileReader; | ||||||
| import java.io.IOException; | import java.io.IOException; | ||||||
| 
 | 
 | ||||||
| @ -18,7 +17,8 @@ public class XmlToObjectFieldAliasTest { | |||||||
| 
 | 
 | ||||||
| 	@Before | 	@Before | ||||||
| 	public void dataSetup() { | 	public void dataSetup() { | ||||||
| 		xstream = SimpleXstreamInitializer.getXstreamInstance(); | 		SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); | ||||||
|  | 		xstream = simpleXstreamInitializer.getXstreamInstance(); | ||||||
| 		xstream.alias("customer" , Customer.class); | 		xstream.alias("customer" , Customer.class); | ||||||
| 		xstream.aliasField("fn", Customer.class, "firstName"); | 		xstream.aliasField("fn", Customer.class, "firstName"); | ||||||
| 	} | 	} | ||||||
| @ -26,7 +26,8 @@ public class XmlToObjectFieldAliasTest { | |||||||
| 	@Test | 	@Test | ||||||
| 	public void convertXmlToObjectFromFile() { | 	public void convertXmlToObjectFromFile() { | ||||||
| 		try { | 		try { | ||||||
| 			FileReader reader = new FileReader(new File("data-file-alias-field.xml")); | 			ClassLoader classLoader = getClass().getClassLoader(); | ||||||
|  | 			FileReader reader = new FileReader(classLoader.getResource("data-file-alias-field.xml").getFile()); | ||||||
| 			Customer customer = (Customer) xstream.fromXML(reader); | 			Customer customer = (Customer) xstream.fromXML(reader); | ||||||
| 			Assert.assertNotNull(customer); | 			Assert.assertNotNull(customer); | ||||||
| 			Assert.assertNotNull(customer.getFirstName()); | 			Assert.assertNotNull(customer.getFirstName()); | ||||||
|  | |||||||
| @ -1,6 +1,5 @@ | |||||||
| package com.baeldung.pojo.test; | package com.baeldung.pojo.test; | ||||||
| 
 | 
 | ||||||
| import java.io.File; |  | ||||||
| import java.io.FileReader; | import java.io.FileReader; | ||||||
| import java.io.IOException; | import java.io.IOException; | ||||||
| 
 | 
 | ||||||
| @ -18,7 +17,8 @@ public class XmlToObjectIgnoreFieldsTest { | |||||||
| 
 | 
 | ||||||
| 	@Before | 	@Before | ||||||
| 	public void dataSetup() { | 	public void dataSetup() { | ||||||
| 		xstream = SimpleXstreamInitializer.getXstreamInstance(); | 		SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); | ||||||
|  | 		xstream = simpleXstreamInitializer.getXstreamInstance(); | ||||||
| 		xstream.alias("customer" , Customer.class); | 		xstream.alias("customer" , Customer.class); | ||||||
| 		xstream.ignoreUnknownElements(); | 		xstream.ignoreUnknownElements(); | ||||||
| 	} | 	} | ||||||
| @ -26,7 +26,8 @@ public class XmlToObjectIgnoreFieldsTest { | |||||||
| 	@Test | 	@Test | ||||||
| 	public void convertXmlToObjectFromFile() { | 	public void convertXmlToObjectFromFile() { | ||||||
| 		try { | 		try { | ||||||
| 			FileReader reader = new FileReader(new File("data-file-ignore-field.xml")); | 			ClassLoader classLoader = getClass().getClassLoader(); | ||||||
|  | 			FileReader reader = new FileReader(classLoader.getResource("data-file-ignore-field.xml").getFile()); | ||||||
| 			Customer customer = (Customer) xstream.fromXML(reader); | 			Customer customer = (Customer) xstream.fromXML(reader); | ||||||
| 			Assert.assertNotNull(customer); | 			Assert.assertNotNull(customer); | ||||||
| 			System.out.println(customer); | 			System.out.println(customer); | ||||||
|  | |||||||
| @ -1,6 +1,5 @@ | |||||||
| package com.baeldung.pojo.test; | package com.baeldung.pojo.test; | ||||||
| 
 | 
 | ||||||
| import java.io.File; |  | ||||||
| import java.io.FileReader; | import java.io.FileReader; | ||||||
| import java.io.IOException; | import java.io.IOException; | ||||||
| 
 | 
 | ||||||
| @ -19,13 +18,15 @@ public class XmlToObjectTest { | |||||||
| 
 | 
 | ||||||
| 	@Before | 	@Before | ||||||
| 	public void dataSetup() { | 	public void dataSetup() { | ||||||
| 		xstream = SimpleXstreamInitializer.getXstreamInstance(); | 		SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); | ||||||
|  | 		xstream = simpleXstreamInitializer.getXstreamInstance(); | ||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
| 	@Test | 	@Test | ||||||
| 	public void convertXmlToObjectFromFile() { | 	public void convertXmlToObjectFromFile() { | ||||||
| 		try { | 		try { | ||||||
| 			FileReader reader = new FileReader(new File("data-file.xml")); | 			ClassLoader classLoader = getClass().getClassLoader(); | ||||||
|  | 			FileReader reader = new FileReader(classLoader.getResource("data-file.xml").getFile()); | ||||||
| 			Customer customer = (Customer) xstream.fromXML(reader); | 			Customer customer = (Customer) xstream.fromXML(reader); | ||||||
| 			Assert.assertNotNull(customer); | 			Assert.assertNotNull(customer); | ||||||
| 			 | 			 | ||||||
|  | |||||||
| @ -0,0 +1,15 @@ | |||||||
|  | <customer> | ||||||
|  |   <firstName>XStream</firstName> | ||||||
|  |   <lastName>Java</lastName> | ||||||
|  |   <dob>1986-02-14 04:14:05.874 UTC</dob> | ||||||
|  |   <contactDetailsList> | ||||||
|  |     <ContactDetails contactType="Home"> | ||||||
|  |       <mobile>6673543265</mobile> | ||||||
|  |       <landline>0124-2460311</landline> | ||||||
|  |     </ContactDetails> | ||||||
|  |     <ContactDetails contactType="Office"> | ||||||
|  |       <mobile>4676543565</mobile> | ||||||
|  |       <landline>0120-223312</landline> | ||||||
|  |     </ContactDetails> | ||||||
|  |   </contactDetailsList> | ||||||
|  | </customer> | ||||||
| @ -0,0 +1,5 @@ | |||||||
|  | <customer> | ||||||
|  |   <fn>XStream</fn> | ||||||
|  |   <lastName>Java</lastName> | ||||||
|  |   <dob>1986-02-14 03:46:16.381 UTC</dob> | ||||||
|  | </customer> | ||||||
| @ -0,0 +1,13 @@ | |||||||
|  | <customer> | ||||||
|  |   <firstName>XStream</firstName> | ||||||
|  |   <lastName>Java</lastName> | ||||||
|  |   <dob>1986-02-14 04:14:20.541 UTC</dob> | ||||||
|  |   <ContactDetails contactType="Home"> | ||||||
|  |     <mobile>6673543265</mobile> | ||||||
|  |     <landline>0124-2460311</landline> | ||||||
|  |   </ContactDetails> | ||||||
|  |   <ContactDetails contactType="Office"> | ||||||
|  |     <mobile>4676543565</mobile> | ||||||
|  |     <landline>0120-223312</landline> | ||||||
|  |   </ContactDetails> | ||||||
|  | </customer> | ||||||
| @ -0,0 +1,5 @@ | |||||||
|  | <customer> | ||||||
|  |   <firstName>XStream</firstName> | ||||||
|  |   <lastName>Java</lastName> | ||||||
|  |   <dob>1986-02-14 03:46:16.381 UTC</dob> | ||||||
|  | </customer> | ||||||
| @ -0,0 +1,6 @@ | |||||||
|  | <customer> | ||||||
|  |   <firstName>XStream</firstName> | ||||||
|  |   <lastName>Java</lastName> | ||||||
|  |   <dob>1986-02-14 04:14:20.541 UTC</dob> | ||||||
|  |   <fullName>XStream Java</fullName> | ||||||
|  | </customer> | ||||||
							
								
								
									
										5
									
								
								xstream-xmlToObject/src/test/resources/data-file.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								xstream-xmlToObject/src/test/resources/data-file.xml
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,5 @@ | |||||||
|  | <com.baeldung.pojo.Customer> | ||||||
|  |   <firstName>XStream</firstName> | ||||||
|  |   <lastName>Java</lastName> | ||||||
|  |   <dob>1986-02-14 03:46:16.381 UTC</dob> | ||||||
|  | </com.baeldung.pojo.Customer> | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user