From ecc662501780e7337bc0ca0cda627bb95de3e104 Mon Sep 17 00:00:00 2001 From: Ivan Date: Fri, 4 Mar 2016 14:49:55 +0100 Subject: [PATCH 01/16] Add form tag example --- .../org/baeldung/spring/ClientWebConfig.java | 8 +-- .../baeldung/spring/ClientWebConfigJava.java | 53 +++++++++++++------ .../main/webapp/WEB-INF/view/personForm.jsp | 6 +-- .../main/webapp/WEB-INF/view/personView.jsp | 15 ------ .../src/main/webapp/WEB-INF/web.xml | 2 +- 5 files changed, 42 insertions(+), 42 deletions(-) diff --git a/spring-mvc-xml/src/main/java/org/baeldung/spring/ClientWebConfig.java b/spring-mvc-xml/src/main/java/org/baeldung/spring/ClientWebConfig.java index c4d819caa5..9705d51606 100644 --- a/spring-mvc-xml/src/main/java/org/baeldung/spring/ClientWebConfig.java +++ b/spring-mvc-xml/src/main/java/org/baeldung/spring/ClientWebConfig.java @@ -8,10 +8,10 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter @Configuration public class ClientWebConfig extends WebMvcConfigurerAdapter { - public ClientWebConfig() { - super(); - } + public ClientWebConfig() { + super(); + } - // API + // API } \ No newline at end of file diff --git a/spring-mvc-xml/src/main/java/org/baeldung/spring/ClientWebConfigJava.java b/spring-mvc-xml/src/main/java/org/baeldung/spring/ClientWebConfigJava.java index d2b57da818..c82201880f 100644 --- a/spring-mvc-xml/src/main/java/org/baeldung/spring/ClientWebConfigJava.java +++ b/spring-mvc-xml/src/main/java/org/baeldung/spring/ClientWebConfigJava.java @@ -1,6 +1,12 @@ package org.baeldung.spring; +import java.util.Locale; +import java.util.ResourceBundle; + +import org.springframework.context.MessageSource; import org.springframework.context.annotation.Bean; +import org.springframework.context.support.MessageSourceResourceBundle; +import org.springframework.context.support.ResourceBundleMessageSource; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @@ -11,27 +17,40 @@ import org.springframework.web.servlet.view.JstlView; //@Configuration public class ClientWebConfigJava extends WebMvcConfigurerAdapter { - public ClientWebConfigJava() { - super(); - } + public ClientWebConfigJava() { + super(); + } - // API + @Bean + public MessageSource messageSource() { - @Override - public void addViewControllers(final ViewControllerRegistry registry) { - super.addViewControllers(registry); + final ResourceBundleMessageSource ms = new ResourceBundleMessageSource(); + ms.setBasenames("messages"); + return ms; + } - registry.addViewController("/sample.html"); - } + @Bean + public ResourceBundle getBeanResourceBundle() { - @Bean - public ViewResolver viewResolver() { - final InternalResourceViewResolver bean = new InternalResourceViewResolver(); + final Locale locale = Locale.getDefault(); + return new MessageSourceResourceBundle(messageSource(), locale); + } - bean.setViewClass(JstlView.class); - bean.setPrefix("/WEB-INF/view/"); - bean.setSuffix(".jsp"); + @Override + public void addViewControllers(final ViewControllerRegistry registry) { + super.addViewControllers(registry); - return bean; - } + registry.addViewController("/sample.html"); + } + + @Bean + public ViewResolver viewResolver() { + final InternalResourceViewResolver bean = new InternalResourceViewResolver(); + + bean.setViewClass(JstlView.class); + bean.setPrefix("/WEB-INF/view/"); + bean.setSuffix(".jsp"); + + return bean; + } } \ No newline at end of file diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp index 79bda7c17d..63d18a987c 100644 --- a/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp @@ -24,7 +24,7 @@

Welcome, Enter The Person Details

- + @@ -104,10 +104,6 @@ Notes - - Select a file to upload - - diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/view/personView.jsp b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personView.jsp index 8893314d20..bd14474dfe 100644 --- a/spring-mvc-xml/src/main/webapp/WEB-INF/view/personView.jsp +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personView.jsp @@ -61,20 +61,5 @@ ${person.notes} - -

Submitted File

- - - - - - - - - - - - -
OriginalFileName :${person.file.originalFilename}
Type :${person.file.contentType}
\ No newline at end of file diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml b/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml index a39c4fdd3a..4e38b2fae6 100644 --- a/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml @@ -29,7 +29,7 @@ org.springframework.web.servlet.DispatcherServlet 1 - /tmp + C:/Users/ivan/Desktop/tmp From 934c0dda3740ae8142fadef664efcc26f7537d0f Mon Sep 17 00:00:00 2001 From: Ivan Date: Fri, 4 Mar 2016 14:51:16 +0100 Subject: [PATCH 02/16] Add form tag example --- spring-mvc-xml/src/main/webapp/WEB-INF/web.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml b/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml index 4e38b2fae6..a39c4fdd3a 100644 --- a/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml @@ -29,7 +29,7 @@ org.springframework.web.servlet.DispatcherServlet 1 - C:/Users/ivan/Desktop/tmp + /tmp From 925f174c54672c5436f3d83d1ad43e77a9bd49ae Mon Sep 17 00:00:00 2001 From: Ivan Date: Fri, 4 Mar 2016 14:53:39 +0100 Subject: [PATCH 03/16] Add form tag example --- spring-mvc-xml/src/main/resources/webMvcConfig.xml | 7 ------- spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp | 2 +- spring-mvc-xml/src/main/webapp/WEB-INF/web.xml | 3 --- 3 files changed, 1 insertion(+), 11 deletions(-) diff --git a/spring-mvc-xml/src/main/resources/webMvcConfig.xml b/spring-mvc-xml/src/main/resources/webMvcConfig.xml index f3297fdbf6..f127cfd637 100644 --- a/spring-mvc-xml/src/main/resources/webMvcConfig.xml +++ b/spring-mvc-xml/src/main/resources/webMvcConfig.xml @@ -25,11 +25,4 @@ - - - - - \ No newline at end of file diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp index 63d18a987c..2836c11668 100644 --- a/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp @@ -22,7 +22,7 @@ -

Welcome, Enter The Person Details

+

Welcome, Enter the Person Details

diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml b/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml index a39c4fdd3a..5275efdf24 100644 --- a/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml @@ -28,9 +28,6 @@ mvc org.springframework.web.servlet.DispatcherServlet 1 - - /tmp - mvc From 8d398b28529fd16316f38496617894fde2bf2d05 Mon Sep 17 00:00:00 2001 From: Ivan Date: Wed, 23 Mar 2016 13:12:12 +0100 Subject: [PATCH 04/16] Add file upload example --- spring-mvc-java/.classpath | 74 ++++++------ spring-mvc-java/pom.xml | 7 ++ .../spring/web/config/ClientWebConfig.java | 112 +++++++++--------- .../web/config/MainWebAppInitializer.java | 50 ++++---- .../baeldung/spring/web/config/WebConfig.java | 15 +++ .../web/controller/FileUploadController.java | 80 +++++++++++++ .../webapp/WEB-INF/view/fileUploadForm.jsp | 47 ++++++++ .../webapp/WEB-INF/view/fileUploadView.jsp | 20 ++++ 8 files changed, 293 insertions(+), 112 deletions(-) create mode 100644 spring-mvc-java/src/main/java/org/baeldung/web/controller/FileUploadController.java create mode 100644 spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp create mode 100644 spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadView.jsp diff --git a/spring-mvc-java/.classpath b/spring-mvc-java/.classpath index 6b533711d3..a642d37ceb 100644 --- a/spring-mvc-java/.classpath +++ b/spring-mvc-java/.classpath @@ -1,37 +1,37 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-mvc-java/pom.xml b/spring-mvc-java/pom.xml index d9a578cb8c..4358e7939f 100644 --- a/spring-mvc-java/pom.xml +++ b/spring-mvc-java/pom.xml @@ -57,6 +57,13 @@ slf4j-log4j12 ${org.slf4j.version} + + + commons-fileupload + commons-fileupload + 1.3.1 + + junit diff --git a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ClientWebConfig.java b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ClientWebConfig.java index db57b4716b..3f451f4259 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ClientWebConfig.java +++ b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ClientWebConfig.java @@ -25,69 +25,71 @@ import org.thymeleaf.templateresolver.ServletContextTemplateResolver; @Configuration public class ClientWebConfig extends WebMvcConfigurerAdapter { - public ClientWebConfig() { - super(); - } + public ClientWebConfig() { + super(); + } - // API + // API - @Override - public void addViewControllers(final ViewControllerRegistry registry) { - super.addViewControllers(registry); + @Override + public void addViewControllers(final ViewControllerRegistry registry) { + super.addViewControllers(registry); - registry.addViewController("/sample.html"); - } + registry.addViewController("/sample.html"); + registry.addViewController("/fileUpload.html"); + registry.addViewController("/fileUploadForm.html"); + } - @Bean - public ViewResolver thymeleafViewResolver() { - final ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); - viewResolver.setTemplateEngine(templateEngine()); - viewResolver.setOrder(1); - return viewResolver; - } + @Bean + public ViewResolver thymeleafViewResolver() { + final ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); + viewResolver.setTemplateEngine(templateEngine()); + viewResolver.setOrder(1); + return viewResolver; + } - @Bean - public ViewResolver viewResolver() { - final InternalResourceViewResolver bean = new InternalResourceViewResolver(); - bean.setViewClass(JstlView.class); - bean.setPrefix("/WEB-INF/view/"); - bean.setSuffix(".jsp"); - bean.setOrder(0); - return bean; - } + @Bean + public ViewResolver viewResolver() { + final InternalResourceViewResolver bean = new InternalResourceViewResolver(); + bean.setViewClass(JstlView.class); + bean.setPrefix("/WEB-INF/view/"); + bean.setSuffix(".jsp"); + bean.setOrder(0); + return bean; + } - @Bean - @Description("Thymeleaf template resolver serving HTML 5") - public ServletContextTemplateResolver templateResolver() { - final ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(); - templateResolver.setPrefix("/WEB-INF/templates/"); - templateResolver.setSuffix(".html"); - templateResolver.setTemplateMode("HTML5"); - return templateResolver; - } + @Bean + @Description("Thymeleaf template resolver serving HTML 5") + public ServletContextTemplateResolver templateResolver() { + final ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(); + templateResolver.setPrefix("/WEB-INF/templates/"); + templateResolver.setSuffix(".html"); + templateResolver.setTemplateMode("HTML5"); + return templateResolver; + } - @Bean - @Description("Thymeleaf template engine with Spring integration") - public SpringTemplateEngine templateEngine() { - final SpringTemplateEngine templateEngine = new SpringTemplateEngine(); - templateEngine.setTemplateResolver(templateResolver()); - final Set dialects = new HashSet<>(); - dialects.add(new CustomDialect()); - templateEngine.setAdditionalDialects(dialects); - return templateEngine; - } + @Bean + @Description("Thymeleaf template engine with Spring integration") + public SpringTemplateEngine templateEngine() { + final SpringTemplateEngine templateEngine = new SpringTemplateEngine(); + templateEngine.setTemplateResolver(templateResolver()); + final Set dialects = new HashSet<>(); + dialects.add(new CustomDialect()); + templateEngine.setAdditionalDialects(dialects); + return templateEngine; + } - @Bean - @Description("Spring message resolver") - public MessageSource messageSource() { - final ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); - messageSource.setBasename("messages"); - return messageSource; - } + @Bean + @Description("Spring message resolver") + public MessageSource messageSource() { + final ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); + messageSource.setBasename("messages"); + return messageSource; + } - @Override - public void addResourceHandlers(final ResourceHandlerRegistry registry) { - registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); - } + @Override + public void addResourceHandlers(final ResourceHandlerRegistry registry) { + registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); + } } \ No newline at end of file diff --git a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/MainWebAppInitializer.java b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/MainWebAppInitializer.java index 87502e2088..9dd9ae98b0 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/MainWebAppInitializer.java +++ b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/MainWebAppInitializer.java @@ -14,28 +14,38 @@ import org.springframework.web.servlet.DispatcherServlet; public class MainWebAppInitializer implements WebApplicationInitializer { - /** - * Register and configure all Servlet container components necessary to power the web application. - */ - @Override - public void onStartup(final ServletContext sc) throws ServletException { - System.out.println("MainWebAppInitializer.onStartup()"); + private static final String TMP_FOLDER = "C:/Users/ivan/Desktop/tmp"; // 5 + // MB + private static final int MAX_UPLOAD_SIZE = 5 * 1024 * 1024; // 5 MB - // Create the 'root' Spring application context - final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext(); - root.scan("org.baeldung.spring.web.config"); - // root.getEnvironment().setDefaultProfiles("embedded"); + /** + * Register and configure all Servlet container components necessary to power the web application. + */ + @Override + public void onStartup(final ServletContext sc) throws ServletException { - // Manages the lifecycle of the root application context - sc.addListener(new ContextLoaderListener(root)); + // Create the 'root' Spring application context + final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext(); + root.scan("org.baeldung.spring.web.config"); + // root.getEnvironment().setDefaultProfiles("embedded"); - // Handles requests into the application - final ServletRegistration.Dynamic appServlet = sc.addServlet("mvc", new DispatcherServlet(new GenericWebApplicationContext())); - appServlet.setLoadOnStartup(1); - final Set mappingConflicts = appServlet.addMapping("/"); - if (!mappingConflicts.isEmpty()) { - throw new IllegalStateException("'appServlet' could not be mapped to '/' due " + "to an existing mapping. This is a known issue under Tomcat versions " + "<= 7.0.14; see https://issues.apache.org/bugzilla/show_bug.cgi?id=51278"); - } - } + // Manages the lifecycle of the root application context + sc.addListener(new ContextLoaderListener(root)); + + // Handles requests into the application + final ServletRegistration.Dynamic appServlet = sc.addServlet("mvc", new DispatcherServlet(new GenericWebApplicationContext())); + appServlet.setLoadOnStartup(1); + + // final MultipartConfigElement multipartConfigElement = new + // MultipartConfigElement(TMP_FOLDER, MAX_UPLOAD_SIZE, + // MAX_UPLOAD_SIZE * 2, MAX_UPLOAD_SIZE / 2); + // + // appServlet.setMultipartConfig(multipartConfigElement); + + final Set mappingConflicts = appServlet.addMapping("/"); + if (!mappingConflicts.isEmpty()) { + throw new IllegalStateException("'appServlet' could not be mapped to '/' due " + "to an existing mapping. This is a known issue under Tomcat versions " + "<= 7.0.14; see https://issues.apache.org/bugzilla/show_bug.cgi?id=51278"); + } + } } diff --git a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java index 58438d2976..b7d87dffbc 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java +++ b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java @@ -22,6 +22,21 @@ public class WebConfig extends WebMvcConfigurerAdapter { super(); } + // @Bean + // public StandardServletMultipartResolver multipartResolver() { + // return new StandardServletMultipartResolver(); + // } + + // @Bean(name = "multipartResolver") + // public CommonsMultipartResolver multipartResolver() { + // + // final CommonsMultipartResolver multipartResolver = new + // CommonsMultipartResolver(); + // multipartResolver.setMaxUploadSize(100000); + // + // return multipartResolver; + // } + @Override public void addViewControllers(final ViewControllerRegistry registry) { diff --git a/spring-mvc-java/src/main/java/org/baeldung/web/controller/FileUploadController.java b/spring-mvc-java/src/main/java/org/baeldung/web/controller/FileUploadController.java new file mode 100644 index 0000000000..5c9d3cbef3 --- /dev/null +++ b/spring-mvc-java/src/main/java/org/baeldung/web/controller/FileUploadController.java @@ -0,0 +1,80 @@ +package org.baeldung.web.controller; + +import java.io.File; +import java.io.IOException; +import java.util.Iterator; +import java.util.List; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.fileupload.FileItem; +import org.apache.commons.fileupload.FileUploadException; +import org.apache.commons.fileupload.disk.DiskFileItemFactory; +import org.apache.commons.fileupload.servlet.ServletFileUpload; +import org.apache.commons.io.FileUtils; +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.multipart.MultipartFile; + +@Controller +public class FileUploadController { + + @RequestMapping(value = "/addFile1", method = RequestMethod.POST) + public String submit(@RequestParam("file") final MultipartFile file, final ModelMap modelMap) { + + modelMap.addAttribute("fileName", file.getOriginalFilename()); + modelMap.addAttribute("fileType", file.getContentType()); + + return "fileUploadView"; + } + + @RequestMapping(value = "/addFile2", method = RequestMethod.POST) + public String submit(final HttpServletRequest request, final HttpServletResponse response, + final ModelMap modelMap) { + + final String TEMP_PATH = "C:\\Users\\ivan\\Desktop\\tmp\\"; + + try { + + final DiskFileItemFactory factory = new DiskFileItemFactory(); + + // Configure a repository (to ensure a secure temp location is used) + final File repository = new File(TEMP_PATH); + factory.setRepository(repository); + + // Create a new file upload handler + final ServletFileUpload upload = new ServletFileUpload(factory); + + // Parse the request + final List items = upload.parseRequest(request); + + final Iterator iter = items.iterator(); + + while (iter.hasNext()) { + + final FileItem item = iter.next(); + + if (!item.isFormField()) { + + final File targetFile = new File(TEMP_PATH + item.getName()); + FileUtils.copyInputStreamToFile(item.getInputStream(), targetFile); + + modelMap.addAttribute("fileName", item.getName()); + modelMap.addAttribute("fileType", item.getContentType()); + } + } + + } catch (final FileUploadException e) { + e.printStackTrace(); + + } catch (final IOException e) { + e.printStackTrace(); + } + + return "fileUploadView"; + } +} diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp b/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp new file mode 100644 index 0000000000..05a7e25a02 --- /dev/null +++ b/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp @@ -0,0 +1,47 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> +<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> + + + + File Upload Example + + + + +

Enter The File to Upload (MultipartFile handling)

+ + + + + + + + + + + +
Select a file to upload
+ +
+ +
+ +

Enter The File to Upload (HttpServletRequest handling)

+ + + + + + + + + + + +
Select a file to upload
+ +
+ + + + \ No newline at end of file diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadView.jsp b/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadView.jsp new file mode 100644 index 0000000000..779c26a597 --- /dev/null +++ b/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadView.jsp @@ -0,0 +1,20 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> + + + + Spring MVC File Upload + + +

Submitted File

+ + + + + + + + + +
OriginalFileName :${fileName}
Type :${fileType}
+ + \ No newline at end of file From 09a1ebfa07b9d697fc4973aa20d87700bc8aa370 Mon Sep 17 00:00:00 2001 From: Ivan Date: Wed, 23 Mar 2016 13:16:10 +0100 Subject: [PATCH 05/16] Add file upload example --- .../java/org/baeldung/web/controller/FileUploadController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-mvc-java/src/main/java/org/baeldung/web/controller/FileUploadController.java b/spring-mvc-java/src/main/java/org/baeldung/web/controller/FileUploadController.java index 5c9d3cbef3..a25a620c8b 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/web/controller/FileUploadController.java +++ b/spring-mvc-java/src/main/java/org/baeldung/web/controller/FileUploadController.java @@ -36,7 +36,7 @@ public class FileUploadController { public String submit(final HttpServletRequest request, final HttpServletResponse response, final ModelMap modelMap) { - final String TEMP_PATH = "C:\\Users\\ivan\\Desktop\\tmp\\"; + final String TEMP_PATH = "/tmp/"; try { From b7d0b2947a0bffd2c89639425dd5a7b6bd91d4cb Mon Sep 17 00:00:00 2001 From: Ivan Date: Wed, 30 Mar 2016 15:06:37 +0200 Subject: [PATCH 06/16] Add file upload example --- .../spring/web/config/ClientWebConfig.java | 2 - .../web/config/MainWebAppInitializer.java | 3 +- .../baeldung/spring/web/config/WebConfig.java | 18 ++--- .../web/controller/FileUploadController.java | 70 +++---------------- .../webapp/WEB-INF/view/fileUploadForm.jsp | 22 ++++-- .../webapp/WEB-INF/view/fileUploadView.jsp | 22 +++++- 6 files changed, 55 insertions(+), 82 deletions(-) diff --git a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ClientWebConfig.java b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ClientWebConfig.java index 3f451f4259..6084943ddd 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ClientWebConfig.java +++ b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ClientWebConfig.java @@ -36,8 +36,6 @@ public class ClientWebConfig extends WebMvcConfigurerAdapter { super.addViewControllers(registry); registry.addViewController("/sample.html"); - registry.addViewController("/fileUpload.html"); - registry.addViewController("/fileUploadForm.html"); } @Bean diff --git a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/MainWebAppInitializer.java b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/MainWebAppInitializer.java index 9dd9ae98b0..ad37bbec5e 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/MainWebAppInitializer.java +++ b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/MainWebAppInitializer.java @@ -14,8 +14,7 @@ import org.springframework.web.servlet.DispatcherServlet; public class MainWebAppInitializer implements WebApplicationInitializer { - private static final String TMP_FOLDER = "C:/Users/ivan/Desktop/tmp"; // 5 - // MB + private static final String TMP_FOLDER = "C:/Users/ivan/Desktop/tmp"; private static final int MAX_UPLOAD_SIZE = 5 * 1024 * 1024; // 5 MB /** diff --git a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java index b7d87dffbc..cd9ae582d8 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java +++ b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java @@ -4,6 +4,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; +import org.springframework.web.multipart.commons.CommonsMultipartResolver; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; @@ -27,15 +28,14 @@ public class WebConfig extends WebMvcConfigurerAdapter { // return new StandardServletMultipartResolver(); // } - // @Bean(name = "multipartResolver") - // public CommonsMultipartResolver multipartResolver() { - // - // final CommonsMultipartResolver multipartResolver = new - // CommonsMultipartResolver(); - // multipartResolver.setMaxUploadSize(100000); - // - // return multipartResolver; - // } + @Bean(name = "multipartResolver") + public CommonsMultipartResolver multipartResolver() { + + final CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(); + multipartResolver.setMaxUploadSize(100000); + + return multipartResolver; + } @Override public void addViewControllers(final ViewControllerRegistry registry) { diff --git a/spring-mvc-java/src/main/java/org/baeldung/web/controller/FileUploadController.java b/spring-mvc-java/src/main/java/org/baeldung/web/controller/FileUploadController.java index a25a620c8b..6f557adf0f 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/web/controller/FileUploadController.java +++ b/spring-mvc-java/src/main/java/org/baeldung/web/controller/FileUploadController.java @@ -1,18 +1,5 @@ package org.baeldung.web.controller; -import java.io.File; -import java.io.IOException; -import java.util.Iterator; -import java.util.List; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.apache.commons.fileupload.FileItem; -import org.apache.commons.fileupload.FileUploadException; -import org.apache.commons.fileupload.disk.DiskFileItemFactory; -import org.apache.commons.fileupload.servlet.ServletFileUpload; -import org.apache.commons.io.FileUtils; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; @@ -23,58 +10,23 @@ import org.springframework.web.multipart.MultipartFile; @Controller public class FileUploadController { - @RequestMapping(value = "/addFile1", method = RequestMethod.POST) + @RequestMapping(value = "/fileUpload", method = RequestMethod.GET) + public String displayForm() { + + return "fileUploadForm"; + } + + @RequestMapping(value = "/uploadFile", method = RequestMethod.POST) public String submit(@RequestParam("file") final MultipartFile file, final ModelMap modelMap) { - modelMap.addAttribute("fileName", file.getOriginalFilename()); - modelMap.addAttribute("fileType", file.getContentType()); - + modelMap.addAttribute("file", file); return "fileUploadView"; } - @RequestMapping(value = "/addFile2", method = RequestMethod.POST) - public String submit(final HttpServletRequest request, final HttpServletResponse response, - final ModelMap modelMap) { - - final String TEMP_PATH = "/tmp/"; - - try { - - final DiskFileItemFactory factory = new DiskFileItemFactory(); - - // Configure a repository (to ensure a secure temp location is used) - final File repository = new File(TEMP_PATH); - factory.setRepository(repository); - - // Create a new file upload handler - final ServletFileUpload upload = new ServletFileUpload(factory); - - // Parse the request - final List items = upload.parseRequest(request); - - final Iterator iter = items.iterator(); - - while (iter.hasNext()) { - - final FileItem item = iter.next(); - - if (!item.isFormField()) { - - final File targetFile = new File(TEMP_PATH + item.getName()); - FileUtils.copyInputStreamToFile(item.getInputStream(), targetFile); - - modelMap.addAttribute("fileName", item.getName()); - modelMap.addAttribute("fileType", item.getContentType()); - } - } - - } catch (final FileUploadException e) { - e.printStackTrace(); - - } catch (final IOException e) { - e.printStackTrace(); - } + @RequestMapping(value = "/uploadMultiFile", method = RequestMethod.POST) + public String submit(@RequestParam("files") final MultipartFile[] files, final ModelMap modelMap) { + modelMap.addAttribute("files", files); return "fileUploadView"; } } diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp b/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp index 05a7e25a02..371acdc0ed 100644 --- a/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp +++ b/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp @@ -8,13 +8,13 @@ -

Enter The File to Upload (MultipartFile handling)

+

Enter The File to Upload

- + - + @@ -24,16 +24,24 @@ -
+
-

Enter The File to Upload (HttpServletRequest handling)

+

Enter The Files to Upload (Multiple files)

- +
Select a file to uploadSelect a file to upload (Single file)
- + + + + + + + + + diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadView.jsp b/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadView.jsp index 779c26a597..d6f748c6af 100644 --- a/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadView.jsp +++ b/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadView.jsp @@ -5,16 +5,32 @@ Spring MVC File Upload -

Submitted File

+ +

Submitted File (Single)

Select a file to upload
Select a file to upload
Select a file to upload
- + - +
OriginalFileName :${fileName}${file.originalFilename}
Type :${fileType}${file.contentType}
+
+ +

Submitted Files (Multiple)

+ + + + + + + + + + + +
OriginalFileName :${file.originalFilename}
Type :${file.contentType}
\ No newline at end of file From f7594ee89cd7f919ab96bac7456238e94cc40c2f Mon Sep 17 00:00:00 2001 From: ankur-singhal Date: Thu, 31 Mar 2016 17:50:47 +0530 Subject: [PATCH 07/16] XStream-XML to Object XStream-XML to Object --- xstream-xmlToObject/.project | 23 ++++++++ .../data-file-alias-field-complex.xml | 15 +++++ xstream-xmlToObject/data-file-alias-field.xml | 5 ++ .../data-file-alias-implicit-collection.xml | 13 ++++ xstream-xmlToObject/data-file-alias.xml | 5 ++ .../data-file-ignore-field.xml | 6 ++ xstream-xmlToObject/data-file.xml | 5 ++ xstream-xmlToObject/pom.xml | 36 +++++++++++ .../baeldung/annotation/pojo/Customer.java | 46 +++++++++++++++ .../annotation/pojo/CustomerOmitField.java | 50 ++++++++++++++++ .../baeldung/complex/pojo/ContactDetails.java | 46 +++++++++++++++ .../com/baeldung/complex/pojo/Customer.java | 57 ++++++++++++++++++ .../collection/pojo/ContactDetails.java | 46 +++++++++++++++ .../implicit/collection/pojo/Customer.java | 59 +++++++++++++++++++ .../initializer/SimpleXstreamInitializer.java | 19 ++++++ .../main/java/com/baeldung/pojo/Customer.java | 42 +++++++++++++ .../utility/SimpleDataGeneration.java | 19 ++++++ .../src/main/resources/log4j.properties | 16 +++++ .../ComplexXmlToObjectAnnotationTest.java | 38 ++++++++++++ ...lexXmlToObjectAttributeCollectionTest.java | 42 +++++++++++++ .../ComplexXmlToObjectCollectionTest.java | 39 ++++++++++++ .../pojo/test/XmlToObjectAliasTest.java | 37 ++++++++++++ .../pojo/test/XmlToObjectAnnotationTest.java | 38 ++++++++++++ .../pojo/test/XmlToObjectFieldAliasTest.java | 39 ++++++++++++ .../test/XmlToObjectIgnoreFieldsTest.java | 38 ++++++++++++ .../baeldung/pojo/test/XmlToObjectTest.java | 46 +++++++++++++++ 26 files changed, 825 insertions(+) create mode 100644 xstream-xmlToObject/.project create mode 100644 xstream-xmlToObject/data-file-alias-field-complex.xml create mode 100644 xstream-xmlToObject/data-file-alias-field.xml create mode 100644 xstream-xmlToObject/data-file-alias-implicit-collection.xml create mode 100644 xstream-xmlToObject/data-file-alias.xml create mode 100644 xstream-xmlToObject/data-file-ignore-field.xml create mode 100644 xstream-xmlToObject/data-file.xml create mode 100644 xstream-xmlToObject/pom.xml create mode 100644 xstream-xmlToObject/src/main/java/com/baeldung/annotation/pojo/Customer.java create mode 100644 xstream-xmlToObject/src/main/java/com/baeldung/annotation/pojo/CustomerOmitField.java create mode 100644 xstream-xmlToObject/src/main/java/com/baeldung/complex/pojo/ContactDetails.java create mode 100644 xstream-xmlToObject/src/main/java/com/baeldung/complex/pojo/Customer.java create mode 100644 xstream-xmlToObject/src/main/java/com/baeldung/implicit/collection/pojo/ContactDetails.java create mode 100644 xstream-xmlToObject/src/main/java/com/baeldung/implicit/collection/pojo/Customer.java create mode 100644 xstream-xmlToObject/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java create mode 100644 xstream-xmlToObject/src/main/java/com/baeldung/pojo/Customer.java create mode 100644 xstream-xmlToObject/src/main/java/com/baeldung/utility/SimpleDataGeneration.java create mode 100644 xstream-xmlToObject/src/main/resources/log4j.properties create mode 100644 xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java create mode 100644 xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java create mode 100644 xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java create mode 100644 xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java create mode 100644 xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java create mode 100644 xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java create mode 100644 xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java create mode 100644 xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java diff --git a/xstream-xmlToObject/.project b/xstream-xmlToObject/.project new file mode 100644 index 0000000000..133b92d690 --- /dev/null +++ b/xstream-xmlToObject/.project @@ -0,0 +1,23 @@ + + + xstream-xmlToObject + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/xstream-xmlToObject/data-file-alias-field-complex.xml b/xstream-xmlToObject/data-file-alias-field-complex.xml new file mode 100644 index 0000000000..2235aa7044 --- /dev/null +++ b/xstream-xmlToObject/data-file-alias-field-complex.xml @@ -0,0 +1,15 @@ + + XStream + Java + 1986-02-14 04:14:05.874 UTC + + + 6673543265 + 0124-2460311 + + + 4676543565 + 0120-223312 + + + \ No newline at end of file diff --git a/xstream-xmlToObject/data-file-alias-field.xml b/xstream-xmlToObject/data-file-alias-field.xml new file mode 100644 index 0000000000..9bc1d0990a --- /dev/null +++ b/xstream-xmlToObject/data-file-alias-field.xml @@ -0,0 +1,5 @@ + + XStream + Java + 1986-02-14 03:46:16.381 UTC + \ No newline at end of file diff --git a/xstream-xmlToObject/data-file-alias-implicit-collection.xml b/xstream-xmlToObject/data-file-alias-implicit-collection.xml new file mode 100644 index 0000000000..d8731900b9 --- /dev/null +++ b/xstream-xmlToObject/data-file-alias-implicit-collection.xml @@ -0,0 +1,13 @@ + + XStream + Java + 1986-02-14 04:14:20.541 UTC + + 6673543265 + 0124-2460311 + + + 4676543565 + 0120-223312 + + \ No newline at end of file diff --git a/xstream-xmlToObject/data-file-alias.xml b/xstream-xmlToObject/data-file-alias.xml new file mode 100644 index 0000000000..964157f1dd --- /dev/null +++ b/xstream-xmlToObject/data-file-alias.xml @@ -0,0 +1,5 @@ + + XStream + Java + 1986-02-14 03:46:16.381 UTC + \ No newline at end of file diff --git a/xstream-xmlToObject/data-file-ignore-field.xml b/xstream-xmlToObject/data-file-ignore-field.xml new file mode 100644 index 0000000000..865e93d4b4 --- /dev/null +++ b/xstream-xmlToObject/data-file-ignore-field.xml @@ -0,0 +1,6 @@ + + XStream + Java + 1986-02-14 04:14:20.541 UTC + XStream Java + \ No newline at end of file diff --git a/xstream-xmlToObject/data-file.xml b/xstream-xmlToObject/data-file.xml new file mode 100644 index 0000000000..59f2ea1cca --- /dev/null +++ b/xstream-xmlToObject/data-file.xml @@ -0,0 +1,5 @@ + + XStream + Java + 1986-02-14 03:46:16.381 UTC + \ No newline at end of file diff --git a/xstream-xmlToObject/pom.xml b/xstream-xmlToObject/pom.xml new file mode 100644 index 0000000000..4828ccb569 --- /dev/null +++ b/xstream-xmlToObject/pom.xml @@ -0,0 +1,36 @@ + + 4.0.0 + + com.baeldung + xstream-xmlToObject + 0.0.1-SNAPSHOT + jar + + xstream-xmlToObject + http://maven.apache.org + + + UTF-8 + + + + + com.thoughtworks.xstream + xstream + 1.4.5 + + + + junit + junit + 4.12 + + + + log4j + log4j + 1.2.17 + + + diff --git a/xstream-xmlToObject/src/main/java/com/baeldung/annotation/pojo/Customer.java b/xstream-xmlToObject/src/main/java/com/baeldung/annotation/pojo/Customer.java new file mode 100644 index 0000000000..42d1d039b0 --- /dev/null +++ b/xstream-xmlToObject/src/main/java/com/baeldung/annotation/pojo/Customer.java @@ -0,0 +1,46 @@ +package com.baeldung.annotation.pojo; + +import java.util.Date; + +import com.thoughtworks.xstream.annotations.XStreamAlias; + +@XStreamAlias("customer") +public class Customer { + + @XStreamAlias("fn") + private String firstName; + + private String lastName; + + private Date dob; + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public Date getDob() { + return dob; + } + + public void setDob(Date dob) { + this.dob = dob; + } + + @Override + public String toString() { + return "Customer [firstName=" + firstName + ", lastName=" + lastName + + ", dob=" + dob + "]"; + } +} diff --git a/xstream-xmlToObject/src/main/java/com/baeldung/annotation/pojo/CustomerOmitField.java b/xstream-xmlToObject/src/main/java/com/baeldung/annotation/pojo/CustomerOmitField.java new file mode 100644 index 0000000000..881d7a5fc7 --- /dev/null +++ b/xstream-xmlToObject/src/main/java/com/baeldung/annotation/pojo/CustomerOmitField.java @@ -0,0 +1,50 @@ +package com.baeldung.annotation.pojo; + +import java.util.Date; + +import com.thoughtworks.xstream.annotations.XStreamAlias; +import com.thoughtworks.xstream.annotations.XStreamOmitField; + + +@XStreamAlias("customer") +public class CustomerOmitField { + + @XStreamOmitField + private String firstName; + + private String lastName; + + private Date dob; + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public Date getDob() { + return dob; + } + + public void setDob(Date dob) { + this.dob = dob; + } + + @Override + public String toString() { + return "CustomerOmitAnnotation [firstName=" + firstName + ", lastName=" + + lastName + ", dob=" + dob + "]"; + } + + +} diff --git a/xstream-xmlToObject/src/main/java/com/baeldung/complex/pojo/ContactDetails.java b/xstream-xmlToObject/src/main/java/com/baeldung/complex/pojo/ContactDetails.java new file mode 100644 index 0000000000..88e721a84b --- /dev/null +++ b/xstream-xmlToObject/src/main/java/com/baeldung/complex/pojo/ContactDetails.java @@ -0,0 +1,46 @@ +package com.baeldung.complex.pojo; + +import com.thoughtworks.xstream.annotations.XStreamAlias; +import com.thoughtworks.xstream.annotations.XStreamAsAttribute; + +@XStreamAlias("ContactDetails") +public class ContactDetails { + + private String mobile; + + private String landline; + + @XStreamAsAttribute + private String contactType; + + public String getMobile() { + return mobile; + } + + public void setMobile(String mobile) { + this.mobile = mobile; + } + + public String getLandline() { + return landline; + } + + public void setLandline(String landline) { + this.landline = landline; + } + + public String getContactType() { + return contactType; + } + + public void setContactType(String contactType) { + this.contactType = contactType; + } + + @Override + public String toString() { + return "ContactDetails [mobile=" + mobile + ", landline=" + landline + + ", contactType=" + contactType + "]"; + } + +} diff --git a/xstream-xmlToObject/src/main/java/com/baeldung/complex/pojo/Customer.java b/xstream-xmlToObject/src/main/java/com/baeldung/complex/pojo/Customer.java new file mode 100644 index 0000000000..0cc3160c03 --- /dev/null +++ b/xstream-xmlToObject/src/main/java/com/baeldung/complex/pojo/Customer.java @@ -0,0 +1,57 @@ +package com.baeldung.complex.pojo; + +import java.util.Date; +import java.util.List; + +import com.thoughtworks.xstream.annotations.XStreamAlias; + +@XStreamAlias("customer") +public class Customer { + + private String firstName; + + private String lastName; + + private Date dob; + + private List contactDetailsList; + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public Date getDob() { + return dob; + } + + public void setDob(Date dob) { + this.dob = dob; + } + + public List getContactDetailsList() { + return contactDetailsList; + } + + public void setContactDetailsList(List contactDetailsList) { + this.contactDetailsList = contactDetailsList; + } + + @Override + public String toString() { + return "Customer [firstName=" + firstName + ", lastName=" + lastName + + ", dob=" + dob + ", contactDetailsList=" + contactDetailsList + + "]"; + } +} diff --git a/xstream-xmlToObject/src/main/java/com/baeldung/implicit/collection/pojo/ContactDetails.java b/xstream-xmlToObject/src/main/java/com/baeldung/implicit/collection/pojo/ContactDetails.java new file mode 100644 index 0000000000..6d6a63bd7a --- /dev/null +++ b/xstream-xmlToObject/src/main/java/com/baeldung/implicit/collection/pojo/ContactDetails.java @@ -0,0 +1,46 @@ +package com.baeldung.implicit.collection.pojo; + +import com.thoughtworks.xstream.annotations.XStreamAlias; +import com.thoughtworks.xstream.annotations.XStreamAsAttribute; + +@XStreamAlias("ContactDetails") +public class ContactDetails { + + private String mobile; + + private String landline; + + @XStreamAsAttribute + private String contactType; + + public String getMobile() { + return mobile; + } + + public void setMobile(String mobile) { + this.mobile = mobile; + } + + public String getLandline() { + return landline; + } + + public void setLandline(String landline) { + this.landline = landline; + } + + public String getContactType() { + return contactType; + } + + public void setContactType(String contactType) { + this.contactType = contactType; + } + + @Override + public String toString() { + return "ContactDetails [mobile=" + mobile + ", landline=" + landline + + ", contactType=" + contactType + "]"; + } + +} diff --git a/xstream-xmlToObject/src/main/java/com/baeldung/implicit/collection/pojo/Customer.java b/xstream-xmlToObject/src/main/java/com/baeldung/implicit/collection/pojo/Customer.java new file mode 100644 index 0000000000..acbeff165e --- /dev/null +++ b/xstream-xmlToObject/src/main/java/com/baeldung/implicit/collection/pojo/Customer.java @@ -0,0 +1,59 @@ +package com.baeldung.implicit.collection.pojo; + +import java.util.Date; +import java.util.List; + +import com.thoughtworks.xstream.annotations.XStreamAlias; +import com.thoughtworks.xstream.annotations.XStreamImplicit; + +@XStreamAlias("customer") +public class Customer { + + private String firstName; + + private String lastName; + + private Date dob; + + @XStreamImplicit + private List contactDetailsList; + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public Date getDob() { + return dob; + } + + public void setDob(Date dob) { + this.dob = dob; + } + + public List getContactDetailsList() { + return contactDetailsList; + } + + public void setContactDetailsList(List contactDetailsList) { + this.contactDetailsList = contactDetailsList; + } + + @Override + public String toString() { + return "Customer [firstName=" + firstName + ", lastName=" + lastName + + ", dob=" + dob + ", contactDetailsList=" + contactDetailsList + + "]"; + } +} diff --git a/xstream-xmlToObject/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java b/xstream-xmlToObject/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java new file mode 100644 index 0000000000..8281678bab --- /dev/null +++ b/xstream-xmlToObject/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java @@ -0,0 +1,19 @@ +package com.baeldung.initializer; + +import com.thoughtworks.xstream.XStream; + +public class SimpleXstreamInitializer { + + private static XStream xtreamInstance; + + public static XStream getXstreamInstance() { + if (xtreamInstance == null) { + synchronized (SimpleXstreamInitializer.class) { + if (xtreamInstance == null) { + xtreamInstance = new XStream(); + } + } + } + return xtreamInstance; + } +} \ No newline at end of file diff --git a/xstream-xmlToObject/src/main/java/com/baeldung/pojo/Customer.java b/xstream-xmlToObject/src/main/java/com/baeldung/pojo/Customer.java new file mode 100644 index 0000000000..affed65fff --- /dev/null +++ b/xstream-xmlToObject/src/main/java/com/baeldung/pojo/Customer.java @@ -0,0 +1,42 @@ +package com.baeldung.pojo; + +import java.util.Date; + +public class Customer { + + private String firstName; + + private String lastName; + + private Date dob; + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public Date getDob() { + return dob; + } + + public void setDob(Date dob) { + this.dob = dob; + } + + @Override + public String toString() { + return "Customer [firstName=" + firstName + ", lastName=" + lastName + + ", dob=" + dob + "]"; + } +} diff --git a/xstream-xmlToObject/src/main/java/com/baeldung/utility/SimpleDataGeneration.java b/xstream-xmlToObject/src/main/java/com/baeldung/utility/SimpleDataGeneration.java new file mode 100644 index 0000000000..14ca25952f --- /dev/null +++ b/xstream-xmlToObject/src/main/java/com/baeldung/utility/SimpleDataGeneration.java @@ -0,0 +1,19 @@ +package com.baeldung.utility; + +import java.util.Calendar; + +import com.baeldung.pojo.Customer; + +public class SimpleDataGeneration { + + public static Customer generateData() { + Customer customer = new Customer(); + Calendar cal = Calendar.getInstance(); + cal.set(1986 , 01 , 14); + customer.setDob(cal.getTime()); + customer.setFirstName("Xstream"); + customer.setLastName("Java"); + + return customer; + } +} diff --git a/xstream-xmlToObject/src/main/resources/log4j.properties b/xstream-xmlToObject/src/main/resources/log4j.properties new file mode 100644 index 0000000000..9cdafc6bdb --- /dev/null +++ b/xstream-xmlToObject/src/main/resources/log4j.properties @@ -0,0 +1,16 @@ +# Root logger option +log4j.rootLogger=DEBUG, file + +# Redirect log messages to console +# log4j.appender.stdout=org.apache.log4j.ConsoleAppender +# log4j.appender.stdout.Target=System.out +# log4j.appender.stdout.layout=org.apache.log4j.PatternLayout +# log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n + +# Redirect log messages to a log file, support file rolling. +log4j.appender.file=org.apache.log4j.RollingFileAppender +log4j.appender.file.File=D:\\Test\\xstream-application.log +log4j.appender.file.MaxFileSize=5MB +log4j.appender.file.MaxBackupIndex=10 +log4j.appender.file.layout=org.apache.log4j.PatternLayout +log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n \ No newline at end of file diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java new file mode 100644 index 0000000000..dff671b6a6 --- /dev/null +++ b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java @@ -0,0 +1,38 @@ +package com.baeldung.pojo.test; + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.baeldung.complex.pojo.Customer; +import com.baeldung.initializer.SimpleXstreamInitializer; +import com.thoughtworks.xstream.XStream; + +public class ComplexXmlToObjectAnnotationTest { + + private XStream xstream = null; + + @Before + public void dataSetup() { + xstream = SimpleXstreamInitializer.getXstreamInstance(); + xstream.processAnnotations(Customer.class); + } + + @Test + public void convertXmlToObjectFromFile() { + try { + FileReader reader = new FileReader(new File("data-file-alias-field-complex.xml")); + Customer customer = (Customer) xstream.fromXML(reader); + Assert.assertNotNull(customer); + Assert.assertNotNull(customer.getContactDetailsList()); + + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java new file mode 100644 index 0000000000..4494cc833d --- /dev/null +++ b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java @@ -0,0 +1,42 @@ +package com.baeldung.pojo.test; + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.baeldung.implicit.collection.pojo.ContactDetails; +import com.baeldung.implicit.collection.pojo.Customer; +import com.baeldung.initializer.SimpleXstreamInitializer; +import com.thoughtworks.xstream.XStream; + +public class ComplexXmlToObjectAttributeCollectionTest { + + private XStream xstream = null; + + @Before + public void dataSetup() { + xstream = SimpleXstreamInitializer.getXstreamInstance(); + xstream.processAnnotations(Customer.class); + } + + @Test + public void convertXmlToObjectFromFile() { + try { + FileReader reader = new FileReader(new File("data-file-alias-implicit-collection.xml")); + Customer customer = (Customer) xstream.fromXML(reader); + Assert.assertNotNull(customer); + Assert.assertNotNull(customer.getContactDetailsList()); + for(ContactDetails contactDetails : customer.getContactDetailsList()){ + Assert.assertNotNull(contactDetails.getContactType()); + } + + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java new file mode 100644 index 0000000000..b72ba44eb5 --- /dev/null +++ b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java @@ -0,0 +1,39 @@ +package com.baeldung.pojo.test; + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.baeldung.implicit.collection.pojo.Customer; +import com.baeldung.initializer.SimpleXstreamInitializer; +import com.thoughtworks.xstream.XStream; + +public class ComplexXmlToObjectCollectionTest { + + private XStream xstream = null; + + @Before + public void dataSetup() { + xstream = SimpleXstreamInitializer.getXstreamInstance(); + xstream.processAnnotations(Customer.class); + } + + @Test + public void convertXmlToObjectFromFile() { + try { + FileReader reader = new FileReader(new File("data-file-alias-implicit-collection.xml")); + Customer customer = (Customer) xstream.fromXML(reader); + Assert.assertNotNull(customer); + Assert.assertNotNull(customer.getContactDetailsList()); + System.out.println(customer); + + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java new file mode 100644 index 0000000000..884071542e --- /dev/null +++ b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java @@ -0,0 +1,37 @@ +package com.baeldung.pojo.test; + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.baeldung.initializer.SimpleXstreamInitializer; +import com.baeldung.pojo.Customer; +import com.thoughtworks.xstream.XStream; + +public class XmlToObjectAliasTest { + + private XStream xstream = null; + + @Before + public void dataSetup() { + xstream = SimpleXstreamInitializer.getXstreamInstance(); + xstream.alias("customer" , Customer.class); + } + + @Test + public void convertXmlToObjectFromFile() { + try { + FileReader reader = new FileReader(new File("data-file-alias.xml")); + Customer customer = (Customer) xstream.fromXML(reader); + Assert.assertNotNull(customer); + + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java new file mode 100644 index 0000000000..09bed7d24f --- /dev/null +++ b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java @@ -0,0 +1,38 @@ +package com.baeldung.pojo.test; + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.baeldung.annotation.pojo.Customer; +import com.baeldung.initializer.SimpleXstreamInitializer; +import com.thoughtworks.xstream.XStream; + +public class XmlToObjectAnnotationTest { + + private XStream xstream = null; + + @Before + public void dataSetup() { + xstream = SimpleXstreamInitializer.getXstreamInstance(); + xstream.processAnnotations(Customer.class); + } + + @Test + public void convertXmlToObjectFromFile() { + try { + FileReader reader = new FileReader(new File("data-file-alias-field.xml")); + Customer customer = (Customer) xstream.fromXML(reader); + Assert.assertNotNull(customer); + Assert.assertNotNull(customer.getFirstName()); + + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java new file mode 100644 index 0000000000..c613a6517c --- /dev/null +++ b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java @@ -0,0 +1,39 @@ +package com.baeldung.pojo.test; + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.baeldung.initializer.SimpleXstreamInitializer; +import com.baeldung.pojo.Customer; +import com.thoughtworks.xstream.XStream; + +public class XmlToObjectFieldAliasTest { + + private XStream xstream = null; + + @Before + public void dataSetup() { + xstream = SimpleXstreamInitializer.getXstreamInstance(); + xstream.alias("customer" , Customer.class); + xstream.aliasField("fn", Customer.class, "firstName"); + } + + @Test + public void convertXmlToObjectFromFile() { + try { + FileReader reader = new FileReader(new File("data-file-alias-field.xml")); + Customer customer = (Customer) xstream.fromXML(reader); + Assert.assertNotNull(customer); + Assert.assertNotNull(customer.getFirstName()); + + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java new file mode 100644 index 0000000000..aa88918402 --- /dev/null +++ b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java @@ -0,0 +1,38 @@ +package com.baeldung.pojo.test; + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.baeldung.initializer.SimpleXstreamInitializer; +import com.baeldung.pojo.Customer; +import com.thoughtworks.xstream.XStream; + +public class XmlToObjectIgnoreFieldsTest { + + private XStream xstream = null; + + @Before + public void dataSetup() { + xstream = SimpleXstreamInitializer.getXstreamInstance(); + xstream.alias("customer" , Customer.class); + xstream.ignoreUnknownElements(); + } + + @Test + public void convertXmlToObjectFromFile() { + try { + FileReader reader = new FileReader(new File("data-file-ignore-field.xml")); + Customer customer = (Customer) xstream.fromXML(reader); + Assert.assertNotNull(customer); + System.out.println(customer); + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java new file mode 100644 index 0000000000..cb7fc49b33 --- /dev/null +++ b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java @@ -0,0 +1,46 @@ +package com.baeldung.pojo.test; + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.baeldung.initializer.SimpleXstreamInitializer; +import com.baeldung.pojo.Customer; +import com.baeldung.utility.SimpleDataGeneration; +import com.thoughtworks.xstream.XStream; + +public class XmlToObjectTest { + + private XStream xstream = null; + + @Before + public void dataSetup() { + xstream = SimpleXstreamInitializer.getXstreamInstance(); + } + + @Test + public void convertXmlToObjectFromFile() { + try { + FileReader reader = new FileReader(new File("data-file.xml")); + Customer customer = (Customer) xstream.fromXML(reader); + Assert.assertNotNull(customer); + + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test + public void convertXmlToObjectFromString() { + Customer customer = SimpleDataGeneration.generateData(); + String dataXml = xstream.toXML(customer); + Customer convertedCustomer = (Customer) xstream.fromXML(dataXml); + Assert.assertNotNull(convertedCustomer); + } + + +} From f214e0a285ee153f04ba84867550a43b0afea3d4 Mon Sep 17 00:00:00 2001 From: ankur-singhal Date: Fri, 1 Apr 2016 17:43:37 +0530 Subject: [PATCH 08/16] Xstream - XML to Object xml moved to resources folder, test cases fixed --- xstream-xmlToObject/.project | 33 +++++++------------ .../initializer/SimpleXstreamInitializer.java | 4 +-- .../ComplexXmlToObjectAnnotationTest.java | 7 ++-- ...lexXmlToObjectAttributeCollectionTest.java | 11 ++++--- .../ComplexXmlToObjectCollectionTest.java | 7 ++-- .../pojo/test/XmlToObjectAliasTest.java | 7 ++-- .../pojo/test/XmlToObjectAnnotationTest.java | 7 ++-- .../pojo/test/XmlToObjectFieldAliasTest.java | 7 ++-- .../test/XmlToObjectIgnoreFieldsTest.java | 7 ++-- .../baeldung/pojo/test/XmlToObjectTest.java | 7 ++-- .../data-file-alias-field-complex.xml | 15 +++++++++ .../test/resources/data-file-alias-field.xml | 5 +++ .../data-file-alias-implicit-collection.xml | 13 ++++++++ .../src/test/resources/data-file-alias.xml | 5 +++ .../test/resources/data-file-ignore-field.xml | 6 ++++ .../src/test/resources/data-file.xml | 5 +++ 16 files changed, 97 insertions(+), 49 deletions(-) create mode 100644 xstream-xmlToObject/src/test/resources/data-file-alias-field-complex.xml create mode 100644 xstream-xmlToObject/src/test/resources/data-file-alias-field.xml create mode 100644 xstream-xmlToObject/src/test/resources/data-file-alias-implicit-collection.xml create mode 100644 xstream-xmlToObject/src/test/resources/data-file-alias.xml create mode 100644 xstream-xmlToObject/src/test/resources/data-file-ignore-field.xml create mode 100644 xstream-xmlToObject/src/test/resources/data-file.xml diff --git a/xstream-xmlToObject/.project b/xstream-xmlToObject/.project index 133b92d690..4e1a3f6e92 100644 --- a/xstream-xmlToObject/.project +++ b/xstream-xmlToObject/.project @@ -1,23 +1,14 @@ - xstream-xmlToObject - - - - - - org.eclipse.jdt.core.javabuilder - - - - - org.eclipse.m2e.core.maven2Builder - - - - - - org.eclipse.jdt.core.javanature - org.eclipse.m2e.core.maven2Nature - - + xstream-xmlToObject + NO_M2ECLIPSE_SUPPORT: Project files created with the maven-eclipse-plugin are not supported in M2Eclipse. + + + + org.eclipse.jdt.core.javabuilder + + + + org.eclipse.jdt.core.javanature + + \ No newline at end of file diff --git a/xstream-xmlToObject/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java b/xstream-xmlToObject/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java index 8281678bab..d7dc3f7a81 100644 --- a/xstream-xmlToObject/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java +++ b/xstream-xmlToObject/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java @@ -4,9 +4,9 @@ import com.thoughtworks.xstream.XStream; public class SimpleXstreamInitializer { - private static XStream xtreamInstance; + private XStream xtreamInstance; - public static XStream getXstreamInstance() { + public XStream getXstreamInstance() { if (xtreamInstance == null) { synchronized (SimpleXstreamInitializer.class) { if (xtreamInstance == null) { diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java index dff671b6a6..10f59e843b 100644 --- a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java +++ b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java @@ -1,6 +1,5 @@ package com.baeldung.pojo.test; -import java.io.File; import java.io.FileReader; import java.io.IOException; @@ -18,14 +17,16 @@ public class ComplexXmlToObjectAnnotationTest { @Before public void dataSetup() { - xstream = SimpleXstreamInitializer.getXstreamInstance(); + SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); + xstream = simpleXstreamInitializer.getXstreamInstance(); xstream.processAnnotations(Customer.class); } @Test public void convertXmlToObjectFromFile() { 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); Assert.assertNotNull(customer); Assert.assertNotNull(customer.getContactDetailsList()); diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java index 4494cc833d..876a407f76 100644 --- a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java +++ b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java @@ -1,6 +1,5 @@ package com.baeldung.pojo.test; -import java.io.File; import java.io.FileReader; import java.io.IOException; @@ -8,8 +7,8 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import com.baeldung.implicit.collection.pojo.ContactDetails; -import com.baeldung.implicit.collection.pojo.Customer; +import com.baeldung.complex.pojo.ContactDetails; +import com.baeldung.complex.pojo.Customer; import com.baeldung.initializer.SimpleXstreamInitializer; import com.thoughtworks.xstream.XStream; @@ -19,14 +18,16 @@ public class ComplexXmlToObjectAttributeCollectionTest { @Before public void dataSetup() { - xstream = SimpleXstreamInitializer.getXstreamInstance(); + SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); + xstream = simpleXstreamInitializer.getXstreamInstance(); xstream.processAnnotations(Customer.class); } @Test public void convertXmlToObjectFromFile() { 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); Assert.assertNotNull(customer); Assert.assertNotNull(customer.getContactDetailsList()); diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java index b72ba44eb5..1656d2f230 100644 --- a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java +++ b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java @@ -1,6 +1,5 @@ package com.baeldung.pojo.test; -import java.io.File; import java.io.FileReader; import java.io.IOException; @@ -18,14 +17,16 @@ public class ComplexXmlToObjectCollectionTest { @Before public void dataSetup() { - xstream = SimpleXstreamInitializer.getXstreamInstance(); + SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); + xstream = simpleXstreamInitializer.getXstreamInstance(); xstream.processAnnotations(Customer.class); } @Test public void convertXmlToObjectFromFile() { 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); Assert.assertNotNull(customer); Assert.assertNotNull(customer.getContactDetailsList()); diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java index 884071542e..cc6d945254 100644 --- a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java +++ b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java @@ -1,6 +1,5 @@ package com.baeldung.pojo.test; -import java.io.File; import java.io.FileReader; import java.io.IOException; @@ -18,14 +17,16 @@ public class XmlToObjectAliasTest { @Before public void dataSetup() { - xstream = SimpleXstreamInitializer.getXstreamInstance(); + SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); + xstream = simpleXstreamInitializer.getXstreamInstance(); xstream.alias("customer" , Customer.class); } @Test public void convertXmlToObjectFromFile() { 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); Assert.assertNotNull(customer); diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java index 09bed7d24f..f2012e2b12 100644 --- a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java +++ b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java @@ -1,6 +1,5 @@ package com.baeldung.pojo.test; -import java.io.File; import java.io.FileReader; import java.io.IOException; @@ -18,14 +17,16 @@ public class XmlToObjectAnnotationTest { @Before public void dataSetup() { - xstream = SimpleXstreamInitializer.getXstreamInstance(); + SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); + xstream = simpleXstreamInitializer.getXstreamInstance(); xstream.processAnnotations(Customer.class); } @Test public void convertXmlToObjectFromFile() { 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); Assert.assertNotNull(customer); Assert.assertNotNull(customer.getFirstName()); diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java index c613a6517c..c4bba86265 100644 --- a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java +++ b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java @@ -1,6 +1,5 @@ package com.baeldung.pojo.test; -import java.io.File; import java.io.FileReader; import java.io.IOException; @@ -18,7 +17,8 @@ public class XmlToObjectFieldAliasTest { @Before public void dataSetup() { - xstream = SimpleXstreamInitializer.getXstreamInstance(); + SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); + xstream = simpleXstreamInitializer.getXstreamInstance(); xstream.alias("customer" , Customer.class); xstream.aliasField("fn", Customer.class, "firstName"); } @@ -26,7 +26,8 @@ public class XmlToObjectFieldAliasTest { @Test public void convertXmlToObjectFromFile() { 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); Assert.assertNotNull(customer); Assert.assertNotNull(customer.getFirstName()); diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java index aa88918402..ca31a22b94 100644 --- a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java +++ b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java @@ -1,6 +1,5 @@ package com.baeldung.pojo.test; -import java.io.File; import java.io.FileReader; import java.io.IOException; @@ -18,7 +17,8 @@ public class XmlToObjectIgnoreFieldsTest { @Before public void dataSetup() { - xstream = SimpleXstreamInitializer.getXstreamInstance(); + SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); + xstream = simpleXstreamInitializer.getXstreamInstance(); xstream.alias("customer" , Customer.class); xstream.ignoreUnknownElements(); } @@ -26,7 +26,8 @@ public class XmlToObjectIgnoreFieldsTest { @Test public void convertXmlToObjectFromFile() { 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); Assert.assertNotNull(customer); System.out.println(customer); diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java index cb7fc49b33..e24e54aea4 100644 --- a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java +++ b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java @@ -1,6 +1,5 @@ package com.baeldung.pojo.test; -import java.io.File; import java.io.FileReader; import java.io.IOException; @@ -19,13 +18,15 @@ public class XmlToObjectTest { @Before public void dataSetup() { - xstream = SimpleXstreamInitializer.getXstreamInstance(); + SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); + xstream = simpleXstreamInitializer.getXstreamInstance(); } @Test public void convertXmlToObjectFromFile() { 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); Assert.assertNotNull(customer); diff --git a/xstream-xmlToObject/src/test/resources/data-file-alias-field-complex.xml b/xstream-xmlToObject/src/test/resources/data-file-alias-field-complex.xml new file mode 100644 index 0000000000..2235aa7044 --- /dev/null +++ b/xstream-xmlToObject/src/test/resources/data-file-alias-field-complex.xml @@ -0,0 +1,15 @@ + + XStream + Java + 1986-02-14 04:14:05.874 UTC + + + 6673543265 + 0124-2460311 + + + 4676543565 + 0120-223312 + + + \ No newline at end of file diff --git a/xstream-xmlToObject/src/test/resources/data-file-alias-field.xml b/xstream-xmlToObject/src/test/resources/data-file-alias-field.xml new file mode 100644 index 0000000000..9bc1d0990a --- /dev/null +++ b/xstream-xmlToObject/src/test/resources/data-file-alias-field.xml @@ -0,0 +1,5 @@ + + XStream + Java + 1986-02-14 03:46:16.381 UTC + \ No newline at end of file diff --git a/xstream-xmlToObject/src/test/resources/data-file-alias-implicit-collection.xml b/xstream-xmlToObject/src/test/resources/data-file-alias-implicit-collection.xml new file mode 100644 index 0000000000..d8731900b9 --- /dev/null +++ b/xstream-xmlToObject/src/test/resources/data-file-alias-implicit-collection.xml @@ -0,0 +1,13 @@ + + XStream + Java + 1986-02-14 04:14:20.541 UTC + + 6673543265 + 0124-2460311 + + + 4676543565 + 0120-223312 + + \ No newline at end of file diff --git a/xstream-xmlToObject/src/test/resources/data-file-alias.xml b/xstream-xmlToObject/src/test/resources/data-file-alias.xml new file mode 100644 index 0000000000..964157f1dd --- /dev/null +++ b/xstream-xmlToObject/src/test/resources/data-file-alias.xml @@ -0,0 +1,5 @@ + + XStream + Java + 1986-02-14 03:46:16.381 UTC + \ No newline at end of file diff --git a/xstream-xmlToObject/src/test/resources/data-file-ignore-field.xml b/xstream-xmlToObject/src/test/resources/data-file-ignore-field.xml new file mode 100644 index 0000000000..865e93d4b4 --- /dev/null +++ b/xstream-xmlToObject/src/test/resources/data-file-ignore-field.xml @@ -0,0 +1,6 @@ + + XStream + Java + 1986-02-14 04:14:20.541 UTC + XStream Java + \ No newline at end of file diff --git a/xstream-xmlToObject/src/test/resources/data-file.xml b/xstream-xmlToObject/src/test/resources/data-file.xml new file mode 100644 index 0000000000..59f2ea1cca --- /dev/null +++ b/xstream-xmlToObject/src/test/resources/data-file.xml @@ -0,0 +1,5 @@ + + XStream + Java + 1986-02-14 03:46:16.381 UTC + \ No newline at end of file From cbc3d119a5882d9aa5dd4b09faa214078869858b Mon Sep 17 00:00:00 2001 From: ankur-singhal Date: Fri, 1 Apr 2016 17:56:04 +0530 Subject: [PATCH 09/16] Xstream - xml to object removing input xml from root folders --- .../data-file-alias-field-complex.xml | 15 --------------- xstream-xmlToObject/data-file-alias-field.xml | 5 ----- .../data-file-alias-implicit-collection.xml | 13 ------------- xstream-xmlToObject/data-file-alias.xml | 5 ----- xstream-xmlToObject/data-file-ignore-field.xml | 6 ------ xstream-xmlToObject/data-file.xml | 5 ----- 6 files changed, 49 deletions(-) delete mode 100644 xstream-xmlToObject/data-file-alias-field-complex.xml delete mode 100644 xstream-xmlToObject/data-file-alias-field.xml delete mode 100644 xstream-xmlToObject/data-file-alias-implicit-collection.xml delete mode 100644 xstream-xmlToObject/data-file-alias.xml delete mode 100644 xstream-xmlToObject/data-file-ignore-field.xml delete mode 100644 xstream-xmlToObject/data-file.xml diff --git a/xstream-xmlToObject/data-file-alias-field-complex.xml b/xstream-xmlToObject/data-file-alias-field-complex.xml deleted file mode 100644 index 2235aa7044..0000000000 --- a/xstream-xmlToObject/data-file-alias-field-complex.xml +++ /dev/null @@ -1,15 +0,0 @@ - - XStream - Java - 1986-02-14 04:14:05.874 UTC - - - 6673543265 - 0124-2460311 - - - 4676543565 - 0120-223312 - - - \ No newline at end of file diff --git a/xstream-xmlToObject/data-file-alias-field.xml b/xstream-xmlToObject/data-file-alias-field.xml deleted file mode 100644 index 9bc1d0990a..0000000000 --- a/xstream-xmlToObject/data-file-alias-field.xml +++ /dev/null @@ -1,5 +0,0 @@ - - XStream - Java - 1986-02-14 03:46:16.381 UTC - \ No newline at end of file diff --git a/xstream-xmlToObject/data-file-alias-implicit-collection.xml b/xstream-xmlToObject/data-file-alias-implicit-collection.xml deleted file mode 100644 index d8731900b9..0000000000 --- a/xstream-xmlToObject/data-file-alias-implicit-collection.xml +++ /dev/null @@ -1,13 +0,0 @@ - - XStream - Java - 1986-02-14 04:14:20.541 UTC - - 6673543265 - 0124-2460311 - - - 4676543565 - 0120-223312 - - \ No newline at end of file diff --git a/xstream-xmlToObject/data-file-alias.xml b/xstream-xmlToObject/data-file-alias.xml deleted file mode 100644 index 964157f1dd..0000000000 --- a/xstream-xmlToObject/data-file-alias.xml +++ /dev/null @@ -1,5 +0,0 @@ - - XStream - Java - 1986-02-14 03:46:16.381 UTC - \ No newline at end of file diff --git a/xstream-xmlToObject/data-file-ignore-field.xml b/xstream-xmlToObject/data-file-ignore-field.xml deleted file mode 100644 index 865e93d4b4..0000000000 --- a/xstream-xmlToObject/data-file-ignore-field.xml +++ /dev/null @@ -1,6 +0,0 @@ - - XStream - Java - 1986-02-14 04:14:20.541 UTC - XStream Java - \ No newline at end of file diff --git a/xstream-xmlToObject/data-file.xml b/xstream-xmlToObject/data-file.xml deleted file mode 100644 index 59f2ea1cca..0000000000 --- a/xstream-xmlToObject/data-file.xml +++ /dev/null @@ -1,5 +0,0 @@ - - XStream - Java - 1986-02-14 03:46:16.381 UTC - \ No newline at end of file From dd61036a31a8ebac3087fe94fd58c0bd2bef5df4 Mon Sep 17 00:00:00 2001 From: ankur-singhal Date: Mon, 4 Apr 2016 11:27:07 +0530 Subject: [PATCH 10/16] XStream-Introduction Merging changes for Article XML - to Object --- .../baeldung/annotation/pojo/Customer.java | 0 .../annotation/pojo/CustomerOmitField.java | 0 .../baeldung/complex/pojo/ContactDetails.java | 0 .../com/baeldung/complex/pojo/Customer.java | 0 .../collection/pojo/ContactDetails.java | 0 .../implicit/collection/pojo/Customer.java | 0 .../initializer/SimpleXstreamInitializer.java | 12 +++--- .../main/java/com/baeldung/pojo/Customer.java | 6 ++- .../ComplexXmlToObjectAnnotationTest.java | 0 ...lexXmlToObjectAttributeCollectionTest.java | 0 .../ComplexXmlToObjectCollectionTest.java | 2 +- .../pojo/test/XmlToObjectAliasTest.java | 0 .../pojo/test/XmlToObjectAnnotationTest.java | 0 .../pojo/test/XmlToObjectFieldAliasTest.java | 0 .../test/XmlToObjectIgnoreFieldsTest.java | 2 +- .../baeldung/pojo/test/XmlToObjectTest.java | 0 .../utility/XStreamSimpleXmlTest.java | 5 +-- .../data-file-alias-field-complex.xml | 0 .../test/resources/data-file-alias-field.xml | 0 .../data-file-alias-implicit-collection.xml | 0 .../src/test/resources/data-file-alias.xml | 0 .../test/resources/data-file-ignore-field.xml | 0 .../src/test/resources/data-file.xml | 0 xstream-xmlToObject/.project | 14 ------- xstream-xmlToObject/pom.xml | 36 ---------------- .../initializer/SimpleXstreamInitializer.java | 19 --------- .../main/java/com/baeldung/pojo/Customer.java | 42 ------------------- .../utility/SimpleDataGeneration.java | 19 --------- .../src/main/resources/log4j.properties | 16 ------- 29 files changed, 14 insertions(+), 159 deletions(-) rename {xstream-xmlToObject => xstream-introduction}/src/main/java/com/baeldung/annotation/pojo/Customer.java (100%) rename {xstream-xmlToObject => xstream-introduction}/src/main/java/com/baeldung/annotation/pojo/CustomerOmitField.java (100%) rename {xstream-xmlToObject => xstream-introduction}/src/main/java/com/baeldung/complex/pojo/ContactDetails.java (100%) rename {xstream-xmlToObject => xstream-introduction}/src/main/java/com/baeldung/complex/pojo/Customer.java (100%) rename {xstream-xmlToObject => xstream-introduction}/src/main/java/com/baeldung/implicit/collection/pojo/ContactDetails.java (100%) rename {xstream-xmlToObject => xstream-introduction}/src/main/java/com/baeldung/implicit/collection/pojo/Customer.java (100%) rename {xstream-xmlToObject => xstream-introduction}/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java (100%) rename {xstream-xmlToObject => xstream-introduction}/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java (100%) rename {xstream-xmlToObject => xstream-introduction}/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java (96%) rename {xstream-xmlToObject => xstream-introduction}/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java (100%) rename {xstream-xmlToObject => xstream-introduction}/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java (100%) rename {xstream-xmlToObject => xstream-introduction}/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java (100%) rename {xstream-xmlToObject => xstream-introduction}/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java (96%) rename {xstream-xmlToObject => xstream-introduction}/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java (100%) rename {xstream-xmlToObject => xstream-introduction}/src/test/resources/data-file-alias-field-complex.xml (100%) rename {xstream-xmlToObject => xstream-introduction}/src/test/resources/data-file-alias-field.xml (100%) rename {xstream-xmlToObject => xstream-introduction}/src/test/resources/data-file-alias-implicit-collection.xml (100%) rename {xstream-xmlToObject => xstream-introduction}/src/test/resources/data-file-alias.xml (100%) rename {xstream-xmlToObject => xstream-introduction}/src/test/resources/data-file-ignore-field.xml (100%) rename {xstream-xmlToObject => xstream-introduction}/src/test/resources/data-file.xml (100%) delete mode 100644 xstream-xmlToObject/.project delete mode 100644 xstream-xmlToObject/pom.xml delete mode 100644 xstream-xmlToObject/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java delete mode 100644 xstream-xmlToObject/src/main/java/com/baeldung/pojo/Customer.java delete mode 100644 xstream-xmlToObject/src/main/java/com/baeldung/utility/SimpleDataGeneration.java delete mode 100644 xstream-xmlToObject/src/main/resources/log4j.properties diff --git a/xstream-xmlToObject/src/main/java/com/baeldung/annotation/pojo/Customer.java b/xstream-introduction/src/main/java/com/baeldung/annotation/pojo/Customer.java similarity index 100% rename from xstream-xmlToObject/src/main/java/com/baeldung/annotation/pojo/Customer.java rename to xstream-introduction/src/main/java/com/baeldung/annotation/pojo/Customer.java diff --git a/xstream-xmlToObject/src/main/java/com/baeldung/annotation/pojo/CustomerOmitField.java b/xstream-introduction/src/main/java/com/baeldung/annotation/pojo/CustomerOmitField.java similarity index 100% rename from xstream-xmlToObject/src/main/java/com/baeldung/annotation/pojo/CustomerOmitField.java rename to xstream-introduction/src/main/java/com/baeldung/annotation/pojo/CustomerOmitField.java diff --git a/xstream-xmlToObject/src/main/java/com/baeldung/complex/pojo/ContactDetails.java b/xstream-introduction/src/main/java/com/baeldung/complex/pojo/ContactDetails.java similarity index 100% rename from xstream-xmlToObject/src/main/java/com/baeldung/complex/pojo/ContactDetails.java rename to xstream-introduction/src/main/java/com/baeldung/complex/pojo/ContactDetails.java diff --git a/xstream-xmlToObject/src/main/java/com/baeldung/complex/pojo/Customer.java b/xstream-introduction/src/main/java/com/baeldung/complex/pojo/Customer.java similarity index 100% rename from xstream-xmlToObject/src/main/java/com/baeldung/complex/pojo/Customer.java rename to xstream-introduction/src/main/java/com/baeldung/complex/pojo/Customer.java diff --git a/xstream-xmlToObject/src/main/java/com/baeldung/implicit/collection/pojo/ContactDetails.java b/xstream-introduction/src/main/java/com/baeldung/implicit/collection/pojo/ContactDetails.java similarity index 100% rename from xstream-xmlToObject/src/main/java/com/baeldung/implicit/collection/pojo/ContactDetails.java rename to xstream-introduction/src/main/java/com/baeldung/implicit/collection/pojo/ContactDetails.java diff --git a/xstream-xmlToObject/src/main/java/com/baeldung/implicit/collection/pojo/Customer.java b/xstream-introduction/src/main/java/com/baeldung/implicit/collection/pojo/Customer.java similarity index 100% rename from xstream-xmlToObject/src/main/java/com/baeldung/implicit/collection/pojo/Customer.java rename to xstream-introduction/src/main/java/com/baeldung/implicit/collection/pojo/Customer.java diff --git a/xstream-introduction/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java b/xstream-introduction/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java index 618df877b9..d7dc3f7a81 100644 --- a/xstream-introduction/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java +++ b/xstream-introduction/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java @@ -4,16 +4,16 @@ import com.thoughtworks.xstream.XStream; public class SimpleXstreamInitializer { - private static XStream xstreamInstance; + private XStream xtreamInstance; - public static XStream getXstreamInstance() { - if (xstreamInstance == null) { + public XStream getXstreamInstance() { + if (xtreamInstance == null) { synchronized (SimpleXstreamInitializer.class) { - if (xstreamInstance == null) { - xstreamInstance = new XStream(); + if (xtreamInstance == null) { + xtreamInstance = new XStream(); } } } - return xstreamInstance; + return xtreamInstance; } } \ No newline at end of file diff --git a/xstream-introduction/src/main/java/com/baeldung/pojo/Customer.java b/xstream-introduction/src/main/java/com/baeldung/pojo/Customer.java index 2ed11dcdab..9bed3394ef 100644 --- a/xstream-introduction/src/main/java/com/baeldung/pojo/Customer.java +++ b/xstream-introduction/src/main/java/com/baeldung/pojo/Customer.java @@ -5,12 +5,10 @@ import java.util.List; import com.thoughtworks.xstream.annotations.XStreamAlias; import com.thoughtworks.xstream.annotations.XStreamImplicit; -import com.thoughtworks.xstream.annotations.XStreamOmitField; @XStreamAlias("customer") public class Customer { - //@XStreamOmitField private String firstName; private String lastName; @@ -52,4 +50,8 @@ public class Customer { this.contactDetailsList = contactDetailsList; } + @Override + public String toString() { + return "Customer [firstName=" + firstName + ", lastName=" + lastName + ", dob=" + dob + "]"; + } } diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java b/xstream-introduction/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java similarity index 100% rename from xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java rename to xstream-introduction/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java b/xstream-introduction/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java similarity index 100% rename from xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java rename to xstream-introduction/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java b/xstream-introduction/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java similarity index 96% rename from xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java rename to xstream-introduction/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java index 1656d2f230..d701fc153b 100644 --- a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java +++ b/xstream-introduction/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java @@ -30,7 +30,7 @@ public class ComplexXmlToObjectCollectionTest { Customer customer = (Customer) xstream.fromXML(reader); Assert.assertNotNull(customer); Assert.assertNotNull(customer.getContactDetailsList()); - System.out.println(customer); + //System.out.println(customer); } catch (IOException e) { e.printStackTrace(); diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java b/xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java similarity index 100% rename from xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java rename to xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java b/xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java similarity index 100% rename from xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java rename to xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java b/xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java similarity index 100% rename from xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java rename to xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java b/xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java similarity index 96% rename from xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java rename to xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java index ca31a22b94..cb13bb9570 100644 --- a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java +++ b/xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java @@ -30,7 +30,7 @@ public class XmlToObjectIgnoreFieldsTest { FileReader reader = new FileReader(classLoader.getResource("data-file-ignore-field.xml").getFile()); Customer customer = (Customer) xstream.fromXML(reader); Assert.assertNotNull(customer); - System.out.println(customer); + //System.out.println(customer); } catch (IOException e) { e.printStackTrace(); } diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java b/xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java similarity index 100% rename from xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java rename to xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java diff --git a/xstream-introduction/src/test/java/com/baeldung/utility/XStreamSimpleXmlTest.java b/xstream-introduction/src/test/java/com/baeldung/utility/XStreamSimpleXmlTest.java index 50d02528bd..ea60e68743 100644 --- a/xstream-introduction/src/test/java/com/baeldung/utility/XStreamSimpleXmlTest.java +++ b/xstream-introduction/src/test/java/com/baeldung/utility/XStreamSimpleXmlTest.java @@ -22,7 +22,8 @@ public class XStreamSimpleXmlTest { @Before public void dataSetup() { customer = SimpleDataGeneration.generateData(); - xstream = SimpleXstreamInitializer.getXstreamInstance(); + SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); + xstream = simpleXstreamInitializer.getXstreamInstance(); xstream.processAnnotations(Customer.class); xstream.processAnnotations(AddressDetails.class); xstream.processAnnotations(ContactDetails.class); @@ -30,9 +31,7 @@ public class XStreamSimpleXmlTest { xstream.registerConverter(new MyDateConverter()); // xstream.registerConverter(new MySingleValueConverter()); xstream.aliasField("fn" , Customer.class , "firstName"); - dataXml = xstream.toXML(customer); - System.out.println(dataXml); } @Test diff --git a/xstream-xmlToObject/src/test/resources/data-file-alias-field-complex.xml b/xstream-introduction/src/test/resources/data-file-alias-field-complex.xml similarity index 100% rename from xstream-xmlToObject/src/test/resources/data-file-alias-field-complex.xml rename to xstream-introduction/src/test/resources/data-file-alias-field-complex.xml diff --git a/xstream-xmlToObject/src/test/resources/data-file-alias-field.xml b/xstream-introduction/src/test/resources/data-file-alias-field.xml similarity index 100% rename from xstream-xmlToObject/src/test/resources/data-file-alias-field.xml rename to xstream-introduction/src/test/resources/data-file-alias-field.xml diff --git a/xstream-xmlToObject/src/test/resources/data-file-alias-implicit-collection.xml b/xstream-introduction/src/test/resources/data-file-alias-implicit-collection.xml similarity index 100% rename from xstream-xmlToObject/src/test/resources/data-file-alias-implicit-collection.xml rename to xstream-introduction/src/test/resources/data-file-alias-implicit-collection.xml diff --git a/xstream-xmlToObject/src/test/resources/data-file-alias.xml b/xstream-introduction/src/test/resources/data-file-alias.xml similarity index 100% rename from xstream-xmlToObject/src/test/resources/data-file-alias.xml rename to xstream-introduction/src/test/resources/data-file-alias.xml diff --git a/xstream-xmlToObject/src/test/resources/data-file-ignore-field.xml b/xstream-introduction/src/test/resources/data-file-ignore-field.xml similarity index 100% rename from xstream-xmlToObject/src/test/resources/data-file-ignore-field.xml rename to xstream-introduction/src/test/resources/data-file-ignore-field.xml diff --git a/xstream-xmlToObject/src/test/resources/data-file.xml b/xstream-introduction/src/test/resources/data-file.xml similarity index 100% rename from xstream-xmlToObject/src/test/resources/data-file.xml rename to xstream-introduction/src/test/resources/data-file.xml diff --git a/xstream-xmlToObject/.project b/xstream-xmlToObject/.project deleted file mode 100644 index 4e1a3f6e92..0000000000 --- a/xstream-xmlToObject/.project +++ /dev/null @@ -1,14 +0,0 @@ - - - xstream-xmlToObject - NO_M2ECLIPSE_SUPPORT: Project files created with the maven-eclipse-plugin are not supported in M2Eclipse. - - - - org.eclipse.jdt.core.javabuilder - - - - org.eclipse.jdt.core.javanature - - \ No newline at end of file diff --git a/xstream-xmlToObject/pom.xml b/xstream-xmlToObject/pom.xml deleted file mode 100644 index 4828ccb569..0000000000 --- a/xstream-xmlToObject/pom.xml +++ /dev/null @@ -1,36 +0,0 @@ - - 4.0.0 - - com.baeldung - xstream-xmlToObject - 0.0.1-SNAPSHOT - jar - - xstream-xmlToObject - http://maven.apache.org - - - UTF-8 - - - - - com.thoughtworks.xstream - xstream - 1.4.5 - - - - junit - junit - 4.12 - - - - log4j - log4j - 1.2.17 - - - diff --git a/xstream-xmlToObject/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java b/xstream-xmlToObject/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java deleted file mode 100644 index d7dc3f7a81..0000000000 --- a/xstream-xmlToObject/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.baeldung.initializer; - -import com.thoughtworks.xstream.XStream; - -public class SimpleXstreamInitializer { - - private XStream xtreamInstance; - - public XStream getXstreamInstance() { - if (xtreamInstance == null) { - synchronized (SimpleXstreamInitializer.class) { - if (xtreamInstance == null) { - xtreamInstance = new XStream(); - } - } - } - return xtreamInstance; - } -} \ No newline at end of file diff --git a/xstream-xmlToObject/src/main/java/com/baeldung/pojo/Customer.java b/xstream-xmlToObject/src/main/java/com/baeldung/pojo/Customer.java deleted file mode 100644 index affed65fff..0000000000 --- a/xstream-xmlToObject/src/main/java/com/baeldung/pojo/Customer.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.baeldung.pojo; - -import java.util.Date; - -public class Customer { - - private String firstName; - - private String lastName; - - private Date dob; - - public String getFirstName() { - return firstName; - } - - public void setFirstName(String firstName) { - this.firstName = firstName; - } - - public String getLastName() { - return lastName; - } - - public void setLastName(String lastName) { - this.lastName = lastName; - } - - public Date getDob() { - return dob; - } - - public void setDob(Date dob) { - this.dob = dob; - } - - @Override - public String toString() { - return "Customer [firstName=" + firstName + ", lastName=" + lastName - + ", dob=" + dob + "]"; - } -} diff --git a/xstream-xmlToObject/src/main/java/com/baeldung/utility/SimpleDataGeneration.java b/xstream-xmlToObject/src/main/java/com/baeldung/utility/SimpleDataGeneration.java deleted file mode 100644 index 14ca25952f..0000000000 --- a/xstream-xmlToObject/src/main/java/com/baeldung/utility/SimpleDataGeneration.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.baeldung.utility; - -import java.util.Calendar; - -import com.baeldung.pojo.Customer; - -public class SimpleDataGeneration { - - public static Customer generateData() { - Customer customer = new Customer(); - Calendar cal = Calendar.getInstance(); - cal.set(1986 , 01 , 14); - customer.setDob(cal.getTime()); - customer.setFirstName("Xstream"); - customer.setLastName("Java"); - - return customer; - } -} diff --git a/xstream-xmlToObject/src/main/resources/log4j.properties b/xstream-xmlToObject/src/main/resources/log4j.properties deleted file mode 100644 index 9cdafc6bdb..0000000000 --- a/xstream-xmlToObject/src/main/resources/log4j.properties +++ /dev/null @@ -1,16 +0,0 @@ -# Root logger option -log4j.rootLogger=DEBUG, file - -# Redirect log messages to console -# log4j.appender.stdout=org.apache.log4j.ConsoleAppender -# log4j.appender.stdout.Target=System.out -# log4j.appender.stdout.layout=org.apache.log4j.PatternLayout -# log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n - -# Redirect log messages to a log file, support file rolling. -log4j.appender.file=org.apache.log4j.RollingFileAppender -log4j.appender.file.File=D:\\Test\\xstream-application.log -log4j.appender.file.MaxFileSize=5MB -log4j.appender.file.MaxBackupIndex=10 -log4j.appender.file.layout=org.apache.log4j.PatternLayout -log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n \ No newline at end of file From ed629e7a49f36ad0ec7dd40b22396f59e06efb39 Mon Sep 17 00:00:00 2001 From: David Morley Date: Mon, 4 Apr 2016 05:18:35 -0500 Subject: [PATCH 11/16] Clean up XStream examples --- xstream-introduction/.project | 14 ----- .../baeldung/annotation/pojo/Customer.java | 46 -------------- .../annotation/pojo/CustomerOmitField.java | 50 --------------- .../baeldung/complex/pojo/ContactDetails.java | 46 -------------- .../com/baeldung/complex/pojo/Customer.java | 57 ----------------- .../collection/pojo/ContactDetails.java | 46 -------------- .../implicit/collection/pojo/Customer.java | 59 ------------------ .../initializer/SimpleXstreamInitializer.java | 19 ------ .../com/baeldung/pojo/AddressDetails.java | 40 ------------ .../com/baeldung/pojo/ContactDetails.java | 28 --------- .../main/java/com/baeldung/pojo/Customer.java | 57 ----------------- .../baeldung/pojo/CustomerAddressDetails.java | 50 --------------- .../com/baeldung/pojo/CustomerPortfolio.java | 20 ------ .../com/baeldung/utility/MyDateConverter.java | 40 ------------ .../utility/MySingleValueConverter.java | 28 --------- .../utility/SimpleDataGeneration.java | 37 ----------- .../ComplexXmlToObjectAnnotationTest.java | 39 ------------ ...lexXmlToObjectAttributeCollectionTest.java | 43 ------------- .../ComplexXmlToObjectCollectionTest.java | 40 ------------ .../pojo/test/XmlToObjectAliasTest.java | 38 ------------ .../pojo/test/XmlToObjectAnnotationTest.java | 39 ------------ .../pojo/test/XmlToObjectFieldAliasTest.java | 40 ------------ .../test/XmlToObjectIgnoreFieldsTest.java | 39 ------------ .../baeldung/pojo/test/XmlToObjectTest.java | 47 -------------- .../utility/XStreamSimpleXmlTest.java | 61 ------------------- .../data-file-alias-field-complex.xml | 15 ----- .../test/resources/data-file-alias-field.xml | 5 -- .../data-file-alias-implicit-collection.xml | 13 ---- .../src/test/resources/data-file-alias.xml | 5 -- .../test/resources/data-file-ignore-field.xml | 6 -- .../src/test/resources/data-file.xml | 5 -- {xstream-introduction => xstream}/pom.xml | 14 +++++ .../baeldung/annotation/pojo/Customer.java | 46 ++++++++++++++ .../annotation/pojo/CustomerOmitField.java | 50 +++++++++++++++ .../baeldung/complex/pojo/ContactDetails.java | 46 ++++++++++++++ .../com/baeldung/complex/pojo/Customer.java | 57 +++++++++++++++++ .../collection/pojo/ContactDetails.java | 46 ++++++++++++++ .../implicit/collection/pojo/Customer.java | 59 ++++++++++++++++++ .../initializer/SimpleXstreamInitializer.java | 19 ++++++ .../com/baeldung/pojo/AddressDetails.java | 40 ++++++++++++ .../com/baeldung/pojo/ContactDetails.java | 28 +++++++++ .../main/java/com/baeldung/pojo/Customer.java | 57 +++++++++++++++++ .../baeldung/pojo/CustomerAddressDetails.java | 50 +++++++++++++++ .../com/baeldung/pojo/CustomerPortfolio.java | 20 ++++++ .../com/baeldung/utility/MyDateConverter.java | 40 ++++++++++++ .../utility/MySingleValueConverter.java | 28 +++++++++ .../utility/SimpleDataGeneration.java | 37 +++++++++++ .../src/main/resources/log4j.properties | 0 .../ComplexXmlToObjectAnnotationTest.java | 38 ++++++++++++ ...lexXmlToObjectAttributeCollectionTest.java | 42 +++++++++++++ .../ComplexXmlToObjectCollectionTest.java | 39 ++++++++++++ .../pojo/test/XmlToObjectAliasTest.java | 37 +++++++++++ .../pojo/test/XmlToObjectAnnotationTest.java | 38 ++++++++++++ .../pojo/test/XmlToObjectFieldAliasTest.java | 39 ++++++++++++ .../test/XmlToObjectIgnoreFieldsTest.java | 38 ++++++++++++ .../baeldung/pojo/test/XmlToObjectTest.java | 46 ++++++++++++++ .../utility/XStreamSimpleXmlTest.java | 57 +++++++++++++++++ .../data-file-alias-field-complex.xml | 15 +++++ .../test/resources/data-file-alias-field.xml | 5 ++ .../data-file-alias-implicit-collection.xml | 13 ++++ .../src/test/resources/data-file-alias.xml | 5 ++ .../test/resources/data-file-ignore-field.xml | 6 ++ xstream/src/test/resources/data-file.xml | 5 ++ 63 files changed, 1060 insertions(+), 1072 deletions(-) delete mode 100644 xstream-introduction/.project delete mode 100644 xstream-introduction/src/main/java/com/baeldung/annotation/pojo/Customer.java delete mode 100644 xstream-introduction/src/main/java/com/baeldung/annotation/pojo/CustomerOmitField.java delete mode 100644 xstream-introduction/src/main/java/com/baeldung/complex/pojo/ContactDetails.java delete mode 100644 xstream-introduction/src/main/java/com/baeldung/complex/pojo/Customer.java delete mode 100644 xstream-introduction/src/main/java/com/baeldung/implicit/collection/pojo/ContactDetails.java delete mode 100644 xstream-introduction/src/main/java/com/baeldung/implicit/collection/pojo/Customer.java delete mode 100644 xstream-introduction/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java delete mode 100644 xstream-introduction/src/main/java/com/baeldung/pojo/AddressDetails.java delete mode 100644 xstream-introduction/src/main/java/com/baeldung/pojo/ContactDetails.java delete mode 100644 xstream-introduction/src/main/java/com/baeldung/pojo/Customer.java delete mode 100644 xstream-introduction/src/main/java/com/baeldung/pojo/CustomerAddressDetails.java delete mode 100644 xstream-introduction/src/main/java/com/baeldung/pojo/CustomerPortfolio.java delete mode 100644 xstream-introduction/src/main/java/com/baeldung/utility/MyDateConverter.java delete mode 100644 xstream-introduction/src/main/java/com/baeldung/utility/MySingleValueConverter.java delete mode 100644 xstream-introduction/src/main/java/com/baeldung/utility/SimpleDataGeneration.java delete mode 100644 xstream-introduction/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java delete mode 100644 xstream-introduction/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java delete mode 100644 xstream-introduction/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java delete mode 100644 xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java delete mode 100644 xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java delete mode 100644 xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java delete mode 100644 xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java delete mode 100644 xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java delete mode 100644 xstream-introduction/src/test/java/com/baeldung/utility/XStreamSimpleXmlTest.java delete mode 100644 xstream-introduction/src/test/resources/data-file-alias-field-complex.xml delete mode 100644 xstream-introduction/src/test/resources/data-file-alias-field.xml delete mode 100644 xstream-introduction/src/test/resources/data-file-alias-implicit-collection.xml delete mode 100644 xstream-introduction/src/test/resources/data-file-alias.xml delete mode 100644 xstream-introduction/src/test/resources/data-file-ignore-field.xml delete mode 100644 xstream-introduction/src/test/resources/data-file.xml rename {xstream-introduction => xstream}/pom.xml (75%) create mode 100644 xstream/src/main/java/com/baeldung/annotation/pojo/Customer.java create mode 100644 xstream/src/main/java/com/baeldung/annotation/pojo/CustomerOmitField.java create mode 100644 xstream/src/main/java/com/baeldung/complex/pojo/ContactDetails.java create mode 100644 xstream/src/main/java/com/baeldung/complex/pojo/Customer.java create mode 100644 xstream/src/main/java/com/baeldung/implicit/collection/pojo/ContactDetails.java create mode 100644 xstream/src/main/java/com/baeldung/implicit/collection/pojo/Customer.java create mode 100644 xstream/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java create mode 100644 xstream/src/main/java/com/baeldung/pojo/AddressDetails.java create mode 100644 xstream/src/main/java/com/baeldung/pojo/ContactDetails.java create mode 100644 xstream/src/main/java/com/baeldung/pojo/Customer.java create mode 100644 xstream/src/main/java/com/baeldung/pojo/CustomerAddressDetails.java create mode 100644 xstream/src/main/java/com/baeldung/pojo/CustomerPortfolio.java create mode 100644 xstream/src/main/java/com/baeldung/utility/MyDateConverter.java create mode 100644 xstream/src/main/java/com/baeldung/utility/MySingleValueConverter.java create mode 100644 xstream/src/main/java/com/baeldung/utility/SimpleDataGeneration.java rename {xstream-introduction => xstream}/src/main/resources/log4j.properties (100%) create mode 100644 xstream/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java create mode 100644 xstream/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java create mode 100644 xstream/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java create mode 100644 xstream/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java create mode 100644 xstream/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java create mode 100644 xstream/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java create mode 100644 xstream/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java create mode 100644 xstream/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java create mode 100644 xstream/src/test/java/com/baeldung/utility/XStreamSimpleXmlTest.java create mode 100644 xstream/src/test/resources/data-file-alias-field-complex.xml create mode 100644 xstream/src/test/resources/data-file-alias-field.xml create mode 100644 xstream/src/test/resources/data-file-alias-implicit-collection.xml create mode 100644 xstream/src/test/resources/data-file-alias.xml create mode 100644 xstream/src/test/resources/data-file-ignore-field.xml create mode 100644 xstream/src/test/resources/data-file.xml diff --git a/xstream-introduction/.project b/xstream-introduction/.project deleted file mode 100644 index cebf3c9cfa..0000000000 --- a/xstream-introduction/.project +++ /dev/null @@ -1,14 +0,0 @@ - - - xstream-introduction - An Introduction To XStream. NO_M2ECLIPSE_SUPPORT: Project files created with the maven-eclipse-plugin are not supported in M2Eclipse. - - - - org.eclipse.jdt.core.javabuilder - - - - org.eclipse.jdt.core.javanature - - \ No newline at end of file diff --git a/xstream-introduction/src/main/java/com/baeldung/annotation/pojo/Customer.java b/xstream-introduction/src/main/java/com/baeldung/annotation/pojo/Customer.java deleted file mode 100644 index 42d1d039b0..0000000000 --- a/xstream-introduction/src/main/java/com/baeldung/annotation/pojo/Customer.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.baeldung.annotation.pojo; - -import java.util.Date; - -import com.thoughtworks.xstream.annotations.XStreamAlias; - -@XStreamAlias("customer") -public class Customer { - - @XStreamAlias("fn") - private String firstName; - - private String lastName; - - private Date dob; - - public String getFirstName() { - return firstName; - } - - public void setFirstName(String firstName) { - this.firstName = firstName; - } - - public String getLastName() { - return lastName; - } - - public void setLastName(String lastName) { - this.lastName = lastName; - } - - public Date getDob() { - return dob; - } - - public void setDob(Date dob) { - this.dob = dob; - } - - @Override - public String toString() { - return "Customer [firstName=" + firstName + ", lastName=" + lastName - + ", dob=" + dob + "]"; - } -} diff --git a/xstream-introduction/src/main/java/com/baeldung/annotation/pojo/CustomerOmitField.java b/xstream-introduction/src/main/java/com/baeldung/annotation/pojo/CustomerOmitField.java deleted file mode 100644 index 881d7a5fc7..0000000000 --- a/xstream-introduction/src/main/java/com/baeldung/annotation/pojo/CustomerOmitField.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.baeldung.annotation.pojo; - -import java.util.Date; - -import com.thoughtworks.xstream.annotations.XStreamAlias; -import com.thoughtworks.xstream.annotations.XStreamOmitField; - - -@XStreamAlias("customer") -public class CustomerOmitField { - - @XStreamOmitField - private String firstName; - - private String lastName; - - private Date dob; - - public String getFirstName() { - return firstName; - } - - public void setFirstName(String firstName) { - this.firstName = firstName; - } - - public String getLastName() { - return lastName; - } - - public void setLastName(String lastName) { - this.lastName = lastName; - } - - public Date getDob() { - return dob; - } - - public void setDob(Date dob) { - this.dob = dob; - } - - @Override - public String toString() { - return "CustomerOmitAnnotation [firstName=" + firstName + ", lastName=" - + lastName + ", dob=" + dob + "]"; - } - - -} diff --git a/xstream-introduction/src/main/java/com/baeldung/complex/pojo/ContactDetails.java b/xstream-introduction/src/main/java/com/baeldung/complex/pojo/ContactDetails.java deleted file mode 100644 index 88e721a84b..0000000000 --- a/xstream-introduction/src/main/java/com/baeldung/complex/pojo/ContactDetails.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.baeldung.complex.pojo; - -import com.thoughtworks.xstream.annotations.XStreamAlias; -import com.thoughtworks.xstream.annotations.XStreamAsAttribute; - -@XStreamAlias("ContactDetails") -public class ContactDetails { - - private String mobile; - - private String landline; - - @XStreamAsAttribute - private String contactType; - - public String getMobile() { - return mobile; - } - - public void setMobile(String mobile) { - this.mobile = mobile; - } - - public String getLandline() { - return landline; - } - - public void setLandline(String landline) { - this.landline = landline; - } - - public String getContactType() { - return contactType; - } - - public void setContactType(String contactType) { - this.contactType = contactType; - } - - @Override - public String toString() { - return "ContactDetails [mobile=" + mobile + ", landline=" + landline - + ", contactType=" + contactType + "]"; - } - -} diff --git a/xstream-introduction/src/main/java/com/baeldung/complex/pojo/Customer.java b/xstream-introduction/src/main/java/com/baeldung/complex/pojo/Customer.java deleted file mode 100644 index 0cc3160c03..0000000000 --- a/xstream-introduction/src/main/java/com/baeldung/complex/pojo/Customer.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.baeldung.complex.pojo; - -import java.util.Date; -import java.util.List; - -import com.thoughtworks.xstream.annotations.XStreamAlias; - -@XStreamAlias("customer") -public class Customer { - - private String firstName; - - private String lastName; - - private Date dob; - - private List contactDetailsList; - - public String getFirstName() { - return firstName; - } - - public void setFirstName(String firstName) { - this.firstName = firstName; - } - - public String getLastName() { - return lastName; - } - - public void setLastName(String lastName) { - this.lastName = lastName; - } - - public Date getDob() { - return dob; - } - - public void setDob(Date dob) { - this.dob = dob; - } - - public List getContactDetailsList() { - return contactDetailsList; - } - - public void setContactDetailsList(List contactDetailsList) { - this.contactDetailsList = contactDetailsList; - } - - @Override - public String toString() { - return "Customer [firstName=" + firstName + ", lastName=" + lastName - + ", dob=" + dob + ", contactDetailsList=" + contactDetailsList - + "]"; - } -} diff --git a/xstream-introduction/src/main/java/com/baeldung/implicit/collection/pojo/ContactDetails.java b/xstream-introduction/src/main/java/com/baeldung/implicit/collection/pojo/ContactDetails.java deleted file mode 100644 index 6d6a63bd7a..0000000000 --- a/xstream-introduction/src/main/java/com/baeldung/implicit/collection/pojo/ContactDetails.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.baeldung.implicit.collection.pojo; - -import com.thoughtworks.xstream.annotations.XStreamAlias; -import com.thoughtworks.xstream.annotations.XStreamAsAttribute; - -@XStreamAlias("ContactDetails") -public class ContactDetails { - - private String mobile; - - private String landline; - - @XStreamAsAttribute - private String contactType; - - public String getMobile() { - return mobile; - } - - public void setMobile(String mobile) { - this.mobile = mobile; - } - - public String getLandline() { - return landline; - } - - public void setLandline(String landline) { - this.landline = landline; - } - - public String getContactType() { - return contactType; - } - - public void setContactType(String contactType) { - this.contactType = contactType; - } - - @Override - public String toString() { - return "ContactDetails [mobile=" + mobile + ", landline=" + landline - + ", contactType=" + contactType + "]"; - } - -} diff --git a/xstream-introduction/src/main/java/com/baeldung/implicit/collection/pojo/Customer.java b/xstream-introduction/src/main/java/com/baeldung/implicit/collection/pojo/Customer.java deleted file mode 100644 index acbeff165e..0000000000 --- a/xstream-introduction/src/main/java/com/baeldung/implicit/collection/pojo/Customer.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.baeldung.implicit.collection.pojo; - -import java.util.Date; -import java.util.List; - -import com.thoughtworks.xstream.annotations.XStreamAlias; -import com.thoughtworks.xstream.annotations.XStreamImplicit; - -@XStreamAlias("customer") -public class Customer { - - private String firstName; - - private String lastName; - - private Date dob; - - @XStreamImplicit - private List contactDetailsList; - - public String getFirstName() { - return firstName; - } - - public void setFirstName(String firstName) { - this.firstName = firstName; - } - - public String getLastName() { - return lastName; - } - - public void setLastName(String lastName) { - this.lastName = lastName; - } - - public Date getDob() { - return dob; - } - - public void setDob(Date dob) { - this.dob = dob; - } - - public List getContactDetailsList() { - return contactDetailsList; - } - - public void setContactDetailsList(List contactDetailsList) { - this.contactDetailsList = contactDetailsList; - } - - @Override - public String toString() { - return "Customer [firstName=" + firstName + ", lastName=" + lastName - + ", dob=" + dob + ", contactDetailsList=" + contactDetailsList - + "]"; - } -} diff --git a/xstream-introduction/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java b/xstream-introduction/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java deleted file mode 100644 index d7dc3f7a81..0000000000 --- a/xstream-introduction/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.baeldung.initializer; - -import com.thoughtworks.xstream.XStream; - -public class SimpleXstreamInitializer { - - private XStream xtreamInstance; - - public XStream getXstreamInstance() { - if (xtreamInstance == null) { - synchronized (SimpleXstreamInitializer.class) { - if (xtreamInstance == null) { - xtreamInstance = new XStream(); - } - } - } - return xtreamInstance; - } -} \ No newline at end of file diff --git a/xstream-introduction/src/main/java/com/baeldung/pojo/AddressDetails.java b/xstream-introduction/src/main/java/com/baeldung/pojo/AddressDetails.java deleted file mode 100644 index e9e30bf5fc..0000000000 --- a/xstream-introduction/src/main/java/com/baeldung/pojo/AddressDetails.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.baeldung.pojo; - -import java.util.List; - -import com.thoughtworks.xstream.annotations.XStreamAlias; - -@XStreamAlias("AddressDetails") -public class AddressDetails { - - private String address; - - private String zipcode; - - private List contactDetails; - - public String getZipcode() { - return zipcode; - } - - public void setZipcode(String zipcode) { - this.zipcode = zipcode; - } - - public List getContactDetails() { - return contactDetails; - } - - public void setContactDetails(List contactDetails) { - this.contactDetails = contactDetails; - } - - public String getAddress() { - return address; - } - - public void setAddress(String address) { - this.address = address; - } - -} diff --git a/xstream-introduction/src/main/java/com/baeldung/pojo/ContactDetails.java b/xstream-introduction/src/main/java/com/baeldung/pojo/ContactDetails.java deleted file mode 100644 index 66475b9d8e..0000000000 --- a/xstream-introduction/src/main/java/com/baeldung/pojo/ContactDetails.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.baeldung.pojo; - -import com.thoughtworks.xstream.annotations.XStreamAlias; - -@XStreamAlias("ContactDetails") -public class ContactDetails { - - private String mobile; - - private String landline; - - public String getMobile() { - return mobile; - } - - public void setMobile(String mobile) { - this.mobile = mobile; - } - - public String getLandline() { - return landline; - } - - public void setLandline(String landline) { - this.landline = landline; - } - -} diff --git a/xstream-introduction/src/main/java/com/baeldung/pojo/Customer.java b/xstream-introduction/src/main/java/com/baeldung/pojo/Customer.java deleted file mode 100644 index 9bed3394ef..0000000000 --- a/xstream-introduction/src/main/java/com/baeldung/pojo/Customer.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.baeldung.pojo; - -import java.util.Date; -import java.util.List; - -import com.thoughtworks.xstream.annotations.XStreamAlias; -import com.thoughtworks.xstream.annotations.XStreamImplicit; - -@XStreamAlias("customer") -public class Customer { - - private String firstName; - - private String lastName; - - private Date dob; - - @XStreamImplicit - private List contactDetailsList; - - public String getFirstName() { - return firstName; - } - - public void setFirstName(String firstName) { - this.firstName = firstName; - } - - public String getLastName() { - return lastName; - } - - public void setLastName(String lastName) { - this.lastName = lastName; - } - - public Date getDob() { - return dob; - } - - public void setDob(Date dob) { - this.dob = dob; - } - - public List getContactDetailsList() { - return contactDetailsList; - } - - public void setContactDetailsList(List contactDetailsList) { - this.contactDetailsList = contactDetailsList; - } - - @Override - public String toString() { - return "Customer [firstName=" + firstName + ", lastName=" + lastName + ", dob=" + dob + "]"; - } -} diff --git a/xstream-introduction/src/main/java/com/baeldung/pojo/CustomerAddressDetails.java b/xstream-introduction/src/main/java/com/baeldung/pojo/CustomerAddressDetails.java deleted file mode 100644 index 30fda1b92c..0000000000 --- a/xstream-introduction/src/main/java/com/baeldung/pojo/CustomerAddressDetails.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.baeldung.pojo; - -import java.util.List; - -import com.thoughtworks.xstream.annotations.XStreamAlias; - -@XStreamAlias("CustomerAddressDetails") -public class CustomerAddressDetails { - - private List addressDetails; - - private String firstName; - - private String lastName; - - private int age; - - public String getFirstName() { - return firstName; - } - - public void setFirstName(String firstName) { - this.firstName = firstName; - } - - public String getLastName() { - return lastName; - } - - public void setLastName(String lastName) { - this.lastName = lastName; - } - - public int getAge() { - return age; - } - - public void setAge(int age) { - this.age = age; - } - - - public List getAddressDetails() { - return addressDetails; - } - - public void setAddressDetails(List addressDetails) { - this.addressDetails = addressDetails; - } -} diff --git a/xstream-introduction/src/main/java/com/baeldung/pojo/CustomerPortfolio.java b/xstream-introduction/src/main/java/com/baeldung/pojo/CustomerPortfolio.java deleted file mode 100644 index 6f1ce4b651..0000000000 --- a/xstream-introduction/src/main/java/com/baeldung/pojo/CustomerPortfolio.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.baeldung.pojo; - -import java.util.List; - -import com.thoughtworks.xstream.annotations.XStreamAlias; - -@XStreamAlias("CustomerPortfolio") -public class CustomerPortfolio { - - private List customerAddressDetailsList; - - public List getCustomerAddressDetailsList() { - return customerAddressDetailsList; - } - - public void setCustomerAddressDetailsList(List customerAddressDetailsList) { - this.customerAddressDetailsList = customerAddressDetailsList; - } - -} diff --git a/xstream-introduction/src/main/java/com/baeldung/utility/MyDateConverter.java b/xstream-introduction/src/main/java/com/baeldung/utility/MyDateConverter.java deleted file mode 100644 index 564a28d1c5..0000000000 --- a/xstream-introduction/src/main/java/com/baeldung/utility/MyDateConverter.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.baeldung.utility; - -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.GregorianCalendar; - -import com.thoughtworks.xstream.converters.ConversionException; -import com.thoughtworks.xstream.converters.Converter; -import com.thoughtworks.xstream.converters.MarshallingContext; -import com.thoughtworks.xstream.converters.UnmarshallingContext; -import com.thoughtworks.xstream.io.HierarchicalStreamReader; -import com.thoughtworks.xstream.io.HierarchicalStreamWriter; - -public class MyDateConverter implements Converter { - - private SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); - - @Override - public boolean canConvert(Class clazz) { - return Date.class.isAssignableFrom(clazz); - } - - @Override - public void marshal(Object value , HierarchicalStreamWriter writer , MarshallingContext arg2) { - Date date = (Date) value; - writer.setValue(formatter.format(date)); - } - - @Override - public Object unmarshal(HierarchicalStreamReader reader , UnmarshallingContext arg1) { - GregorianCalendar calendar = new GregorianCalendar(); - try { - calendar.setTime(formatter.parse(reader.getValue())); - } catch (ParseException e) { - throw new ConversionException(e.getMessage() , e); - } - return calendar; - } -} diff --git a/xstream-introduction/src/main/java/com/baeldung/utility/MySingleValueConverter.java b/xstream-introduction/src/main/java/com/baeldung/utility/MySingleValueConverter.java deleted file mode 100644 index 358d647835..0000000000 --- a/xstream-introduction/src/main/java/com/baeldung/utility/MySingleValueConverter.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.baeldung.utility; - -import java.text.SimpleDateFormat; -import java.util.Date; - -import com.baeldung.pojo.Customer; -import com.thoughtworks.xstream.converters.SingleValueConverter; - -public class MySingleValueConverter implements SingleValueConverter { - - @Override - public boolean canConvert(Class clazz) { - return Customer.class.isAssignableFrom(clazz); - } - - @Override - public Object fromString(String arg0) { - return null; - } - - @Override - public String toString(Object obj) { - SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); - Date date = ((Customer) obj).getDob(); - return ((Customer) obj).getFirstName() + "," + ((Customer) obj).getLastName() + "," + formatter.format(date); - } - -} diff --git a/xstream-introduction/src/main/java/com/baeldung/utility/SimpleDataGeneration.java b/xstream-introduction/src/main/java/com/baeldung/utility/SimpleDataGeneration.java deleted file mode 100644 index 22d0f0ce77..0000000000 --- a/xstream-introduction/src/main/java/com/baeldung/utility/SimpleDataGeneration.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.baeldung.utility; - -import java.util.ArrayList; -import java.util.Calendar; -import java.util.List; - -import com.baeldung.pojo.ContactDetails; -import com.baeldung.pojo.Customer; - -public class SimpleDataGeneration { - - public static Customer generateData() { - Customer customer = new Customer(); - Calendar cal = Calendar.getInstance(); - cal.set(1986 , 01 , 14); - customer.setDob(cal.getTime()); - customer.setFirstName("XStream"); - customer.setLastName("Java"); - - List contactDetailsList = new ArrayList(); - - ContactDetails contactDetails1 = new ContactDetails(); - contactDetails1.setLandline("0124-2460311"); - contactDetails1.setMobile("6673543265"); - - ContactDetails contactDetails2 = new ContactDetails(); - contactDetails2.setLandline("0120-223312"); - contactDetails2.setMobile("4676543565"); - - contactDetailsList.add(contactDetails1); - contactDetailsList.add(contactDetails2); - - customer.setContactDetailsList(contactDetailsList); - return customer; - } - -} diff --git a/xstream-introduction/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java b/xstream-introduction/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java deleted file mode 100644 index 10f59e843b..0000000000 --- a/xstream-introduction/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.baeldung.pojo.test; - -import java.io.FileReader; -import java.io.IOException; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import com.baeldung.complex.pojo.Customer; -import com.baeldung.initializer.SimpleXstreamInitializer; -import com.thoughtworks.xstream.XStream; - -public class ComplexXmlToObjectAnnotationTest { - - private XStream xstream = null; - - @Before - public void dataSetup() { - SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); - xstream = simpleXstreamInitializer.getXstreamInstance(); - xstream.processAnnotations(Customer.class); - } - - @Test - public void convertXmlToObjectFromFile() { - try { - ClassLoader classLoader = getClass().getClassLoader(); - FileReader reader = new FileReader(classLoader.getResource("data-file-alias-field-complex.xml").getFile()); - Customer customer = (Customer) xstream.fromXML(reader); - Assert.assertNotNull(customer); - Assert.assertNotNull(customer.getContactDetailsList()); - - } catch (IOException e) { - e.printStackTrace(); - } - } - -} diff --git a/xstream-introduction/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java b/xstream-introduction/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java deleted file mode 100644 index 876a407f76..0000000000 --- a/xstream-introduction/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.baeldung.pojo.test; - -import java.io.FileReader; -import java.io.IOException; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import com.baeldung.complex.pojo.ContactDetails; -import com.baeldung.complex.pojo.Customer; -import com.baeldung.initializer.SimpleXstreamInitializer; -import com.thoughtworks.xstream.XStream; - -public class ComplexXmlToObjectAttributeCollectionTest { - - private XStream xstream = null; - - @Before - public void dataSetup() { - SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); - xstream = simpleXstreamInitializer.getXstreamInstance(); - xstream.processAnnotations(Customer.class); - } - - @Test - public void convertXmlToObjectFromFile() { - try { - ClassLoader classLoader = getClass().getClassLoader(); - FileReader reader = new FileReader(classLoader.getResource("data-file-alias-field-complex.xml").getFile()); - Customer customer = (Customer) xstream.fromXML(reader); - Assert.assertNotNull(customer); - Assert.assertNotNull(customer.getContactDetailsList()); - for(ContactDetails contactDetails : customer.getContactDetailsList()){ - Assert.assertNotNull(contactDetails.getContactType()); - } - - } catch (IOException e) { - e.printStackTrace(); - } - } - -} diff --git a/xstream-introduction/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java b/xstream-introduction/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java deleted file mode 100644 index d701fc153b..0000000000 --- a/xstream-introduction/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.baeldung.pojo.test; - -import java.io.FileReader; -import java.io.IOException; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import com.baeldung.implicit.collection.pojo.Customer; -import com.baeldung.initializer.SimpleXstreamInitializer; -import com.thoughtworks.xstream.XStream; - -public class ComplexXmlToObjectCollectionTest { - - private XStream xstream = null; - - @Before - public void dataSetup() { - SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); - xstream = simpleXstreamInitializer.getXstreamInstance(); - xstream.processAnnotations(Customer.class); - } - - @Test - public void convertXmlToObjectFromFile() { - try { - ClassLoader classLoader = getClass().getClassLoader(); - FileReader reader = new FileReader(classLoader.getResource("data-file-alias-implicit-collection.xml").getFile()); - Customer customer = (Customer) xstream.fromXML(reader); - Assert.assertNotNull(customer); - Assert.assertNotNull(customer.getContactDetailsList()); - //System.out.println(customer); - - } catch (IOException e) { - e.printStackTrace(); - } - } - -} diff --git a/xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java b/xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java deleted file mode 100644 index cc6d945254..0000000000 --- a/xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.baeldung.pojo.test; - -import java.io.FileReader; -import java.io.IOException; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import com.baeldung.initializer.SimpleXstreamInitializer; -import com.baeldung.pojo.Customer; -import com.thoughtworks.xstream.XStream; - -public class XmlToObjectAliasTest { - - private XStream xstream = null; - - @Before - public void dataSetup() { - SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); - xstream = simpleXstreamInitializer.getXstreamInstance(); - xstream.alias("customer" , Customer.class); - } - - @Test - public void convertXmlToObjectFromFile() { - try { - ClassLoader classLoader = getClass().getClassLoader(); - FileReader reader = new FileReader(classLoader.getResource("data-file-alias.xml").getFile()); - Customer customer = (Customer) xstream.fromXML(reader); - Assert.assertNotNull(customer); - - } catch (IOException e) { - e.printStackTrace(); - } - } - -} diff --git a/xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java b/xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java deleted file mode 100644 index f2012e2b12..0000000000 --- a/xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.baeldung.pojo.test; - -import java.io.FileReader; -import java.io.IOException; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import com.baeldung.annotation.pojo.Customer; -import com.baeldung.initializer.SimpleXstreamInitializer; -import com.thoughtworks.xstream.XStream; - -public class XmlToObjectAnnotationTest { - - private XStream xstream = null; - - @Before - public void dataSetup() { - SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); - xstream = simpleXstreamInitializer.getXstreamInstance(); - xstream.processAnnotations(Customer.class); - } - - @Test - public void convertXmlToObjectFromFile() { - try { - ClassLoader classLoader = getClass().getClassLoader(); - FileReader reader = new FileReader(classLoader.getResource("data-file-alias-field.xml").getFile()); - Customer customer = (Customer) xstream.fromXML(reader); - Assert.assertNotNull(customer); - Assert.assertNotNull(customer.getFirstName()); - - } catch (IOException e) { - e.printStackTrace(); - } - } - -} diff --git a/xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java b/xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java deleted file mode 100644 index c4bba86265..0000000000 --- a/xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.baeldung.pojo.test; - -import java.io.FileReader; -import java.io.IOException; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import com.baeldung.initializer.SimpleXstreamInitializer; -import com.baeldung.pojo.Customer; -import com.thoughtworks.xstream.XStream; - -public class XmlToObjectFieldAliasTest { - - private XStream xstream = null; - - @Before - public void dataSetup() { - SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); - xstream = simpleXstreamInitializer.getXstreamInstance(); - xstream.alias("customer" , Customer.class); - xstream.aliasField("fn", Customer.class, "firstName"); - } - - @Test - public void convertXmlToObjectFromFile() { - try { - ClassLoader classLoader = getClass().getClassLoader(); - FileReader reader = new FileReader(classLoader.getResource("data-file-alias-field.xml").getFile()); - Customer customer = (Customer) xstream.fromXML(reader); - Assert.assertNotNull(customer); - Assert.assertNotNull(customer.getFirstName()); - - } catch (IOException e) { - e.printStackTrace(); - } - } - -} diff --git a/xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java b/xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java deleted file mode 100644 index cb13bb9570..0000000000 --- a/xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.baeldung.pojo.test; - -import java.io.FileReader; -import java.io.IOException; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import com.baeldung.initializer.SimpleXstreamInitializer; -import com.baeldung.pojo.Customer; -import com.thoughtworks.xstream.XStream; - -public class XmlToObjectIgnoreFieldsTest { - - private XStream xstream = null; - - @Before - public void dataSetup() { - SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); - xstream = simpleXstreamInitializer.getXstreamInstance(); - xstream.alias("customer" , Customer.class); - xstream.ignoreUnknownElements(); - } - - @Test - public void convertXmlToObjectFromFile() { - try { - ClassLoader classLoader = getClass().getClassLoader(); - FileReader reader = new FileReader(classLoader.getResource("data-file-ignore-field.xml").getFile()); - Customer customer = (Customer) xstream.fromXML(reader); - Assert.assertNotNull(customer); - //System.out.println(customer); - } catch (IOException e) { - e.printStackTrace(); - } - } - -} diff --git a/xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java b/xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java deleted file mode 100644 index e24e54aea4..0000000000 --- a/xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.baeldung.pojo.test; - -import java.io.FileReader; -import java.io.IOException; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import com.baeldung.initializer.SimpleXstreamInitializer; -import com.baeldung.pojo.Customer; -import com.baeldung.utility.SimpleDataGeneration; -import com.thoughtworks.xstream.XStream; - -public class XmlToObjectTest { - - private XStream xstream = null; - - @Before - public void dataSetup() { - SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); - xstream = simpleXstreamInitializer.getXstreamInstance(); - } - - @Test - public void convertXmlToObjectFromFile() { - try { - ClassLoader classLoader = getClass().getClassLoader(); - FileReader reader = new FileReader(classLoader.getResource("data-file.xml").getFile()); - Customer customer = (Customer) xstream.fromXML(reader); - Assert.assertNotNull(customer); - - } catch (IOException e) { - e.printStackTrace(); - } - } - - @Test - public void convertXmlToObjectFromString() { - Customer customer = SimpleDataGeneration.generateData(); - String dataXml = xstream.toXML(customer); - Customer convertedCustomer = (Customer) xstream.fromXML(dataXml); - Assert.assertNotNull(convertedCustomer); - } - - -} diff --git a/xstream-introduction/src/test/java/com/baeldung/utility/XStreamSimpleXmlTest.java b/xstream-introduction/src/test/java/com/baeldung/utility/XStreamSimpleXmlTest.java deleted file mode 100644 index ea60e68743..0000000000 --- a/xstream-introduction/src/test/java/com/baeldung/utility/XStreamSimpleXmlTest.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.baeldung.utility; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import com.baeldung.initializer.SimpleXstreamInitializer; -import com.baeldung.pojo.AddressDetails; -import com.baeldung.pojo.ContactDetails; -import com.baeldung.pojo.Customer; -import com.baeldung.utility.SimpleDataGeneration; -import com.thoughtworks.xstream.XStream; - -public class XStreamSimpleXmlTest { - - private Customer customer = null; - - private String dataXml = null; - - private XStream xstream = null; - - @Before - public void dataSetup() { - customer = SimpleDataGeneration.generateData(); - SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); - xstream = simpleXstreamInitializer.getXstreamInstance(); - xstream.processAnnotations(Customer.class); - xstream.processAnnotations(AddressDetails.class); - xstream.processAnnotations(ContactDetails.class); - xstream.omitField(Customer.class , "lastName"); - xstream.registerConverter(new MyDateConverter()); - // xstream.registerConverter(new MySingleValueConverter()); - xstream.aliasField("fn" , Customer.class , "firstName"); - dataXml = xstream.toXML(customer); - } - - @Test - public void testClassAliasedAnnotation() { - Assert.assertNotEquals(-1 , dataXml.indexOf("")); - } - - @Test - public void testFieldAliasedAnnotation() { - Assert.assertNotEquals(-1 , dataXml.indexOf("")); - } - - @Test - public void testImplicitCollection() { - Assert.assertEquals(-1 , dataXml.indexOf("contactDetailsList")); - } - - @Test - public void testDateFieldFormating() { - Assert.assertEquals("14-02-1986" , dataXml.substring(dataXml.indexOf("") + 5 , dataXml.indexOf(""))); - } - - @Test - public void testOmitField() { - Assert.assertEquals(-1 , dataXml.indexOf("lastName")); - } -} diff --git a/xstream-introduction/src/test/resources/data-file-alias-field-complex.xml b/xstream-introduction/src/test/resources/data-file-alias-field-complex.xml deleted file mode 100644 index 2235aa7044..0000000000 --- a/xstream-introduction/src/test/resources/data-file-alias-field-complex.xml +++ /dev/null @@ -1,15 +0,0 @@ - - XStream - Java - 1986-02-14 04:14:05.874 UTC - - - 6673543265 - 0124-2460311 - - - 4676543565 - 0120-223312 - - - \ No newline at end of file diff --git a/xstream-introduction/src/test/resources/data-file-alias-field.xml b/xstream-introduction/src/test/resources/data-file-alias-field.xml deleted file mode 100644 index 9bc1d0990a..0000000000 --- a/xstream-introduction/src/test/resources/data-file-alias-field.xml +++ /dev/null @@ -1,5 +0,0 @@ - - XStream - Java - 1986-02-14 03:46:16.381 UTC - \ No newline at end of file diff --git a/xstream-introduction/src/test/resources/data-file-alias-implicit-collection.xml b/xstream-introduction/src/test/resources/data-file-alias-implicit-collection.xml deleted file mode 100644 index d8731900b9..0000000000 --- a/xstream-introduction/src/test/resources/data-file-alias-implicit-collection.xml +++ /dev/null @@ -1,13 +0,0 @@ - - XStream - Java - 1986-02-14 04:14:20.541 UTC - - 6673543265 - 0124-2460311 - - - 4676543565 - 0120-223312 - - \ No newline at end of file diff --git a/xstream-introduction/src/test/resources/data-file-alias.xml b/xstream-introduction/src/test/resources/data-file-alias.xml deleted file mode 100644 index 964157f1dd..0000000000 --- a/xstream-introduction/src/test/resources/data-file-alias.xml +++ /dev/null @@ -1,5 +0,0 @@ - - XStream - Java - 1986-02-14 03:46:16.381 UTC - \ No newline at end of file diff --git a/xstream-introduction/src/test/resources/data-file-ignore-field.xml b/xstream-introduction/src/test/resources/data-file-ignore-field.xml deleted file mode 100644 index 865e93d4b4..0000000000 --- a/xstream-introduction/src/test/resources/data-file-ignore-field.xml +++ /dev/null @@ -1,6 +0,0 @@ - - XStream - Java - 1986-02-14 04:14:20.541 UTC - XStream Java - \ No newline at end of file diff --git a/xstream-introduction/src/test/resources/data-file.xml b/xstream-introduction/src/test/resources/data-file.xml deleted file mode 100644 index 59f2ea1cca..0000000000 --- a/xstream-introduction/src/test/resources/data-file.xml +++ /dev/null @@ -1,5 +0,0 @@ - - XStream - Java - 1986-02-14 03:46:16.381 UTC - \ No newline at end of file diff --git a/xstream-introduction/pom.xml b/xstream/pom.xml similarity index 75% rename from xstream-introduction/pom.xml rename to xstream/pom.xml index 2f0f26d6f2..8a5aec41e9 100644 --- a/xstream-introduction/pom.xml +++ b/xstream/pom.xml @@ -27,4 +27,18 @@
+ + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5.1 + + 1.8 + 1.8 + + + + + \ No newline at end of file diff --git a/xstream/src/main/java/com/baeldung/annotation/pojo/Customer.java b/xstream/src/main/java/com/baeldung/annotation/pojo/Customer.java new file mode 100644 index 0000000000..2cdb0f56c9 --- /dev/null +++ b/xstream/src/main/java/com/baeldung/annotation/pojo/Customer.java @@ -0,0 +1,46 @@ +package com.baeldung.annotation.pojo; + +import com.thoughtworks.xstream.annotations.XStreamAlias; + +import java.util.Date; + +@XStreamAlias("customer") +public class Customer { + + @XStreamAlias("fn") + private String firstName; + + private String lastName; + + private Date dob; + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public Date getDob() { + return dob; + } + + public void setDob(Date dob) { + this.dob = dob; + } + + @Override + public String toString() { + return "Customer [firstName=" + firstName + ", lastName=" + lastName + + ", dob=" + dob + "]"; + } +} diff --git a/xstream/src/main/java/com/baeldung/annotation/pojo/CustomerOmitField.java b/xstream/src/main/java/com/baeldung/annotation/pojo/CustomerOmitField.java new file mode 100644 index 0000000000..f5b98c9c1b --- /dev/null +++ b/xstream/src/main/java/com/baeldung/annotation/pojo/CustomerOmitField.java @@ -0,0 +1,50 @@ +package com.baeldung.annotation.pojo; + +import com.thoughtworks.xstream.annotations.XStreamAlias; +import com.thoughtworks.xstream.annotations.XStreamOmitField; + +import java.util.Date; + + +@XStreamAlias("customer") +public class CustomerOmitField { + + @XStreamOmitField + private String firstName; + + private String lastName; + + private Date dob; + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public Date getDob() { + return dob; + } + + public void setDob(Date dob) { + this.dob = dob; + } + + @Override + public String toString() { + return "CustomerOmitAnnotation [firstName=" + firstName + ", lastName=" + + lastName + ", dob=" + dob + "]"; + } + + +} diff --git a/xstream/src/main/java/com/baeldung/complex/pojo/ContactDetails.java b/xstream/src/main/java/com/baeldung/complex/pojo/ContactDetails.java new file mode 100644 index 0000000000..e091492a1a --- /dev/null +++ b/xstream/src/main/java/com/baeldung/complex/pojo/ContactDetails.java @@ -0,0 +1,46 @@ +package com.baeldung.complex.pojo; + +import com.thoughtworks.xstream.annotations.XStreamAlias; +import com.thoughtworks.xstream.annotations.XStreamAsAttribute; + +@XStreamAlias("ContactDetails") +public class ContactDetails { + + private String mobile; + + private String landline; + + @XStreamAsAttribute + private String contactType; + + public String getMobile() { + return mobile; + } + + public void setMobile(String mobile) { + this.mobile = mobile; + } + + public String getLandline() { + return landline; + } + + public void setLandline(String landline) { + this.landline = landline; + } + + public String getContactType() { + return contactType; + } + + public void setContactType(String contactType) { + this.contactType = contactType; + } + + @Override + public String toString() { + return "ContactDetails [mobile=" + mobile + ", landline=" + landline + + ", contactType=" + contactType + "]"; + } + +} diff --git a/xstream/src/main/java/com/baeldung/complex/pojo/Customer.java b/xstream/src/main/java/com/baeldung/complex/pojo/Customer.java new file mode 100644 index 0000000000..c6f98982f0 --- /dev/null +++ b/xstream/src/main/java/com/baeldung/complex/pojo/Customer.java @@ -0,0 +1,57 @@ +package com.baeldung.complex.pojo; + +import com.thoughtworks.xstream.annotations.XStreamAlias; + +import java.util.Date; +import java.util.List; + +@XStreamAlias("customer") +public class Customer { + + private String firstName; + + private String lastName; + + private Date dob; + + private List contactDetailsList; + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public Date getDob() { + return dob; + } + + public void setDob(Date dob) { + this.dob = dob; + } + + public List getContactDetailsList() { + return contactDetailsList; + } + + public void setContactDetailsList(List contactDetailsList) { + this.contactDetailsList = contactDetailsList; + } + + @Override + public String toString() { + return "Customer [firstName=" + firstName + ", lastName=" + lastName + + ", dob=" + dob + ", contactDetailsList=" + contactDetailsList + + "]"; + } +} diff --git a/xstream/src/main/java/com/baeldung/implicit/collection/pojo/ContactDetails.java b/xstream/src/main/java/com/baeldung/implicit/collection/pojo/ContactDetails.java new file mode 100644 index 0000000000..38ec7ff077 --- /dev/null +++ b/xstream/src/main/java/com/baeldung/implicit/collection/pojo/ContactDetails.java @@ -0,0 +1,46 @@ +package com.baeldung.implicit.collection.pojo; + +import com.thoughtworks.xstream.annotations.XStreamAlias; +import com.thoughtworks.xstream.annotations.XStreamAsAttribute; + +@XStreamAlias("ContactDetails") +public class ContactDetails { + + private String mobile; + + private String landline; + + @XStreamAsAttribute + private String contactType; + + public String getMobile() { + return mobile; + } + + public void setMobile(String mobile) { + this.mobile = mobile; + } + + public String getLandline() { + return landline; + } + + public void setLandline(String landline) { + this.landline = landline; + } + + public String getContactType() { + return contactType; + } + + public void setContactType(String contactType) { + this.contactType = contactType; + } + + @Override + public String toString() { + return "ContactDetails [mobile=" + mobile + ", landline=" + landline + + ", contactType=" + contactType + "]"; + } + +} diff --git a/xstream/src/main/java/com/baeldung/implicit/collection/pojo/Customer.java b/xstream/src/main/java/com/baeldung/implicit/collection/pojo/Customer.java new file mode 100644 index 0000000000..a50ac850dd --- /dev/null +++ b/xstream/src/main/java/com/baeldung/implicit/collection/pojo/Customer.java @@ -0,0 +1,59 @@ +package com.baeldung.implicit.collection.pojo; + +import com.thoughtworks.xstream.annotations.XStreamAlias; +import com.thoughtworks.xstream.annotations.XStreamImplicit; + +import java.util.Date; +import java.util.List; + +@XStreamAlias("customer") +public class Customer { + + private String firstName; + + private String lastName; + + private Date dob; + + @XStreamImplicit + private List contactDetailsList; + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public Date getDob() { + return dob; + } + + public void setDob(Date dob) { + this.dob = dob; + } + + public List getContactDetailsList() { + return contactDetailsList; + } + + public void setContactDetailsList(List contactDetailsList) { + this.contactDetailsList = contactDetailsList; + } + + @Override + public String toString() { + return "Customer [firstName=" + firstName + ", lastName=" + lastName + + ", dob=" + dob + ", contactDetailsList=" + contactDetailsList + + "]"; + } +} diff --git a/xstream/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java b/xstream/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java new file mode 100644 index 0000000000..5dec19d181 --- /dev/null +++ b/xstream/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java @@ -0,0 +1,19 @@ +package com.baeldung.initializer; + +import com.thoughtworks.xstream.XStream; + +public class SimpleXstreamInitializer { + + private XStream xtreamInstance; + + public XStream getXstreamInstance() { + if (xtreamInstance == null) { + synchronized (SimpleXstreamInitializer.class) { + if (xtreamInstance == null) { + xtreamInstance = new XStream(); + } + } + } + return xtreamInstance; + } +} \ No newline at end of file diff --git a/xstream/src/main/java/com/baeldung/pojo/AddressDetails.java b/xstream/src/main/java/com/baeldung/pojo/AddressDetails.java new file mode 100644 index 0000000000..53ba7e9a85 --- /dev/null +++ b/xstream/src/main/java/com/baeldung/pojo/AddressDetails.java @@ -0,0 +1,40 @@ +package com.baeldung.pojo; + +import com.thoughtworks.xstream.annotations.XStreamAlias; + +import java.util.List; + +@XStreamAlias("AddressDetails") +public class AddressDetails { + + private String address; + + private String zipcode; + + private List contactDetails; + + public String getZipcode() { + return zipcode; + } + + public void setZipcode(String zipcode) { + this.zipcode = zipcode; + } + + public List getContactDetails() { + return contactDetails; + } + + public void setContactDetails(List contactDetails) { + this.contactDetails = contactDetails; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + +} diff --git a/xstream/src/main/java/com/baeldung/pojo/ContactDetails.java b/xstream/src/main/java/com/baeldung/pojo/ContactDetails.java new file mode 100644 index 0000000000..75408bdba8 --- /dev/null +++ b/xstream/src/main/java/com/baeldung/pojo/ContactDetails.java @@ -0,0 +1,28 @@ +package com.baeldung.pojo; + +import com.thoughtworks.xstream.annotations.XStreamAlias; + +@XStreamAlias("ContactDetails") +public class ContactDetails { + + private String mobile; + + private String landline; + + public String getMobile() { + return mobile; + } + + public void setMobile(String mobile) { + this.mobile = mobile; + } + + public String getLandline() { + return landline; + } + + public void setLandline(String landline) { + this.landline = landline; + } + +} diff --git a/xstream/src/main/java/com/baeldung/pojo/Customer.java b/xstream/src/main/java/com/baeldung/pojo/Customer.java new file mode 100644 index 0000000000..728939c356 --- /dev/null +++ b/xstream/src/main/java/com/baeldung/pojo/Customer.java @@ -0,0 +1,57 @@ +package com.baeldung.pojo; + +import com.thoughtworks.xstream.annotations.XStreamAlias; +import com.thoughtworks.xstream.annotations.XStreamImplicit; + +import java.util.Date; +import java.util.List; + +@XStreamAlias("customer") +public class Customer { + + private String firstName; + + private String lastName; + + private Date dob; + + @XStreamImplicit + private List contactDetailsList; + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public Date getDob() { + return dob; + } + + public void setDob(Date dob) { + this.dob = dob; + } + + public List getContactDetailsList() { + return contactDetailsList; + } + + public void setContactDetailsList(List contactDetailsList) { + this.contactDetailsList = contactDetailsList; + } + + @Override + public String toString() { + return "Customer [firstName=" + firstName + ", lastName=" + lastName + ", dob=" + dob + "]"; + } +} diff --git a/xstream/src/main/java/com/baeldung/pojo/CustomerAddressDetails.java b/xstream/src/main/java/com/baeldung/pojo/CustomerAddressDetails.java new file mode 100644 index 0000000000..f203c9cce9 --- /dev/null +++ b/xstream/src/main/java/com/baeldung/pojo/CustomerAddressDetails.java @@ -0,0 +1,50 @@ +package com.baeldung.pojo; + +import com.thoughtworks.xstream.annotations.XStreamAlias; + +import java.util.List; + +@XStreamAlias("CustomerAddressDetails") +public class CustomerAddressDetails { + + private List addressDetails; + + private String firstName; + + private String lastName; + + private int age; + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + + public List getAddressDetails() { + return addressDetails; + } + + public void setAddressDetails(List addressDetails) { + this.addressDetails = addressDetails; + } +} diff --git a/xstream/src/main/java/com/baeldung/pojo/CustomerPortfolio.java b/xstream/src/main/java/com/baeldung/pojo/CustomerPortfolio.java new file mode 100644 index 0000000000..90722feb71 --- /dev/null +++ b/xstream/src/main/java/com/baeldung/pojo/CustomerPortfolio.java @@ -0,0 +1,20 @@ +package com.baeldung.pojo; + +import com.thoughtworks.xstream.annotations.XStreamAlias; + +import java.util.List; + +@XStreamAlias("CustomerPortfolio") +public class CustomerPortfolio { + + private List customerAddressDetailsList; + + public List getCustomerAddressDetailsList() { + return customerAddressDetailsList; + } + + public void setCustomerAddressDetailsList(List customerAddressDetailsList) { + this.customerAddressDetailsList = customerAddressDetailsList; + } + +} diff --git a/xstream/src/main/java/com/baeldung/utility/MyDateConverter.java b/xstream/src/main/java/com/baeldung/utility/MyDateConverter.java new file mode 100644 index 0000000000..af7ca19aac --- /dev/null +++ b/xstream/src/main/java/com/baeldung/utility/MyDateConverter.java @@ -0,0 +1,40 @@ +package com.baeldung.utility; + +import com.thoughtworks.xstream.converters.ConversionException; +import com.thoughtworks.xstream.converters.Converter; +import com.thoughtworks.xstream.converters.MarshallingContext; +import com.thoughtworks.xstream.converters.UnmarshallingContext; +import com.thoughtworks.xstream.io.HierarchicalStreamReader; +import com.thoughtworks.xstream.io.HierarchicalStreamWriter; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.GregorianCalendar; + +public class MyDateConverter implements Converter { + + private SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); + + @Override + public boolean canConvert(Class clazz) { + return Date.class.isAssignableFrom(clazz); + } + + @Override + public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext arg2) { + Date date = (Date) value; + writer.setValue(formatter.format(date)); + } + + @Override + public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext arg1) { + GregorianCalendar calendar = new GregorianCalendar(); + try { + calendar.setTime(formatter.parse(reader.getValue())); + } catch (ParseException e) { + throw new ConversionException(e.getMessage(), e); + } + return calendar; + } +} diff --git a/xstream/src/main/java/com/baeldung/utility/MySingleValueConverter.java b/xstream/src/main/java/com/baeldung/utility/MySingleValueConverter.java new file mode 100644 index 0000000000..9b242f1c7c --- /dev/null +++ b/xstream/src/main/java/com/baeldung/utility/MySingleValueConverter.java @@ -0,0 +1,28 @@ +package com.baeldung.utility; + +import com.baeldung.pojo.Customer; +import com.thoughtworks.xstream.converters.SingleValueConverter; + +import java.text.SimpleDateFormat; +import java.util.Date; + +public class MySingleValueConverter implements SingleValueConverter { + + @Override + public boolean canConvert(Class clazz) { + return Customer.class.isAssignableFrom(clazz); + } + + @Override + public Object fromString(String arg0) { + return null; + } + + @Override + public String toString(Object obj) { + SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); + Date date = ((Customer) obj).getDob(); + return ((Customer) obj).getFirstName() + "," + ((Customer) obj).getLastName() + "," + formatter.format(date); + } + +} diff --git a/xstream/src/main/java/com/baeldung/utility/SimpleDataGeneration.java b/xstream/src/main/java/com/baeldung/utility/SimpleDataGeneration.java new file mode 100644 index 0000000000..cf038bfd1b --- /dev/null +++ b/xstream/src/main/java/com/baeldung/utility/SimpleDataGeneration.java @@ -0,0 +1,37 @@ +package com.baeldung.utility; + +import com.baeldung.pojo.ContactDetails; +import com.baeldung.pojo.Customer; + +import java.util.ArrayList; +import java.util.Calendar; +import java.util.List; + +public class SimpleDataGeneration { + + public static Customer generateData() { + Customer customer = new Customer(); + Calendar cal = Calendar.getInstance(); + cal.set(1986, 01, 14); + customer.setDob(cal.getTime()); + customer.setFirstName("XStream"); + customer.setLastName("Java"); + + List contactDetailsList = new ArrayList(); + + ContactDetails contactDetails1 = new ContactDetails(); + contactDetails1.setLandline("0124-2460311"); + contactDetails1.setMobile("6673543265"); + + ContactDetails contactDetails2 = new ContactDetails(); + contactDetails2.setLandline("0120-223312"); + contactDetails2.setMobile("4676543565"); + + contactDetailsList.add(contactDetails1); + contactDetailsList.add(contactDetails2); + + customer.setContactDetailsList(contactDetailsList); + return customer; + } + +} diff --git a/xstream-introduction/src/main/resources/log4j.properties b/xstream/src/main/resources/log4j.properties similarity index 100% rename from xstream-introduction/src/main/resources/log4j.properties rename to xstream/src/main/resources/log4j.properties diff --git a/xstream/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java b/xstream/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java new file mode 100644 index 0000000000..479500c4a0 --- /dev/null +++ b/xstream/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java @@ -0,0 +1,38 @@ +package com.baeldung.pojo.test; + +import com.baeldung.complex.pojo.Customer; +import com.baeldung.initializer.SimpleXstreamInitializer; +import com.thoughtworks.xstream.XStream; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.io.FileReader; +import java.io.IOException; + +public class ComplexXmlToObjectAnnotationTest { + + private XStream xstream = null; + + @Before + public void dataSetup() { + SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); + xstream = simpleXstreamInitializer.getXstreamInstance(); + xstream.processAnnotations(Customer.class); + } + + @Test + public void convertXmlToObjectFromFile() { + try { + ClassLoader classLoader = getClass().getClassLoader(); + FileReader reader = new FileReader(classLoader.getResource("data-file-alias-field-complex.xml").getFile()); + Customer customer = (Customer) xstream.fromXML(reader); + Assert.assertNotNull(customer); + Assert.assertNotNull(customer.getContactDetailsList()); + + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/xstream/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java b/xstream/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java new file mode 100644 index 0000000000..8c569aa11e --- /dev/null +++ b/xstream/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java @@ -0,0 +1,42 @@ +package com.baeldung.pojo.test; + +import com.baeldung.complex.pojo.ContactDetails; +import com.baeldung.complex.pojo.Customer; +import com.baeldung.initializer.SimpleXstreamInitializer; +import com.thoughtworks.xstream.XStream; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.io.FileReader; +import java.io.IOException; + +public class ComplexXmlToObjectAttributeCollectionTest { + + private XStream xstream = null; + + @Before + public void dataSetup() { + SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); + xstream = simpleXstreamInitializer.getXstreamInstance(); + xstream.processAnnotations(Customer.class); + } + + @Test + public void convertXmlToObjectFromFile() { + try { + ClassLoader classLoader = getClass().getClassLoader(); + FileReader reader = new FileReader(classLoader.getResource("data-file-alias-field-complex.xml").getFile()); + Customer customer = (Customer) xstream.fromXML(reader); + Assert.assertNotNull(customer); + Assert.assertNotNull(customer.getContactDetailsList()); + for (ContactDetails contactDetails : customer.getContactDetailsList()) { + Assert.assertNotNull(contactDetails.getContactType()); + } + + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/xstream/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java b/xstream/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java new file mode 100644 index 0000000000..29ef7a5d64 --- /dev/null +++ b/xstream/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java @@ -0,0 +1,39 @@ +package com.baeldung.pojo.test; + +import com.baeldung.implicit.collection.pojo.Customer; +import com.baeldung.initializer.SimpleXstreamInitializer; +import com.thoughtworks.xstream.XStream; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.io.FileReader; +import java.io.IOException; + +public class ComplexXmlToObjectCollectionTest { + + private XStream xstream = null; + + @Before + public void dataSetup() { + SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); + xstream = simpleXstreamInitializer.getXstreamInstance(); + xstream.processAnnotations(Customer.class); + } + + @Test + public void convertXmlToObjectFromFile() { + try { + ClassLoader classLoader = getClass().getClassLoader(); + FileReader reader = new FileReader(classLoader.getResource("data-file-alias-implicit-collection.xml").getFile()); + Customer customer = (Customer) xstream.fromXML(reader); + Assert.assertNotNull(customer); + Assert.assertNotNull(customer.getContactDetailsList()); + //System.out.println(customer); + + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/xstream/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java b/xstream/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java new file mode 100644 index 0000000000..8a4de3b70a --- /dev/null +++ b/xstream/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java @@ -0,0 +1,37 @@ +package com.baeldung.pojo.test; + +import com.baeldung.initializer.SimpleXstreamInitializer; +import com.baeldung.pojo.Customer; +import com.thoughtworks.xstream.XStream; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.io.FileReader; +import java.io.IOException; + +public class XmlToObjectAliasTest { + + private XStream xstream = null; + + @Before + public void dataSetup() { + SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); + xstream = simpleXstreamInitializer.getXstreamInstance(); + xstream.alias("customer", Customer.class); + } + + @Test + public void convertXmlToObjectFromFile() { + try { + ClassLoader classLoader = getClass().getClassLoader(); + FileReader reader = new FileReader(classLoader.getResource("data-file-alias.xml").getFile()); + Customer customer = (Customer) xstream.fromXML(reader); + Assert.assertNotNull(customer); + + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/xstream/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java b/xstream/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java new file mode 100644 index 0000000000..4a7ff2f74a --- /dev/null +++ b/xstream/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java @@ -0,0 +1,38 @@ +package com.baeldung.pojo.test; + +import com.baeldung.annotation.pojo.Customer; +import com.baeldung.initializer.SimpleXstreamInitializer; +import com.thoughtworks.xstream.XStream; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.io.FileReader; +import java.io.IOException; + +public class XmlToObjectAnnotationTest { + + private XStream xstream = null; + + @Before + public void dataSetup() { + SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); + xstream = simpleXstreamInitializer.getXstreamInstance(); + xstream.processAnnotations(Customer.class); + } + + @Test + public void convertXmlToObjectFromFile() { + try { + ClassLoader classLoader = getClass().getClassLoader(); + FileReader reader = new FileReader(classLoader.getResource("data-file-alias-field.xml").getFile()); + Customer customer = (Customer) xstream.fromXML(reader); + Assert.assertNotNull(customer); + Assert.assertNotNull(customer.getFirstName()); + + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/xstream/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java b/xstream/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java new file mode 100644 index 0000000000..3b1b8326ab --- /dev/null +++ b/xstream/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java @@ -0,0 +1,39 @@ +package com.baeldung.pojo.test; + +import com.baeldung.initializer.SimpleXstreamInitializer; +import com.baeldung.pojo.Customer; +import com.thoughtworks.xstream.XStream; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.io.FileReader; +import java.io.IOException; + +public class XmlToObjectFieldAliasTest { + + private XStream xstream = null; + + @Before + public void dataSetup() { + SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); + xstream = simpleXstreamInitializer.getXstreamInstance(); + xstream.alias("customer", Customer.class); + xstream.aliasField("fn", Customer.class, "firstName"); + } + + @Test + public void convertXmlToObjectFromFile() { + try { + ClassLoader classLoader = getClass().getClassLoader(); + FileReader reader = new FileReader(classLoader.getResource("data-file-alias-field.xml").getFile()); + Customer customer = (Customer) xstream.fromXML(reader); + Assert.assertNotNull(customer); + Assert.assertNotNull(customer.getFirstName()); + + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/xstream/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java b/xstream/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java new file mode 100644 index 0000000000..95a034b3e7 --- /dev/null +++ b/xstream/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java @@ -0,0 +1,38 @@ +package com.baeldung.pojo.test; + +import com.baeldung.initializer.SimpleXstreamInitializer; +import com.baeldung.pojo.Customer; +import com.thoughtworks.xstream.XStream; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.io.FileReader; +import java.io.IOException; + +public class XmlToObjectIgnoreFieldsTest { + + private XStream xstream = null; + + @Before + public void dataSetup() { + SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); + xstream = simpleXstreamInitializer.getXstreamInstance(); + xstream.alias("customer", Customer.class); + xstream.ignoreUnknownElements(); + } + + @Test + public void convertXmlToObjectFromFile() { + try { + ClassLoader classLoader = getClass().getClassLoader(); + FileReader reader = new FileReader(classLoader.getResource("data-file-ignore-field.xml").getFile()); + Customer customer = (Customer) xstream.fromXML(reader); + Assert.assertNotNull(customer); + //System.out.println(customer); + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/xstream/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java b/xstream/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java new file mode 100644 index 0000000000..b6b64ce8da --- /dev/null +++ b/xstream/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java @@ -0,0 +1,46 @@ +package com.baeldung.pojo.test; + +import com.baeldung.initializer.SimpleXstreamInitializer; +import com.baeldung.pojo.Customer; +import com.baeldung.utility.SimpleDataGeneration; +import com.thoughtworks.xstream.XStream; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.io.FileReader; +import java.io.IOException; + +public class XmlToObjectTest { + + private XStream xstream = null; + + @Before + public void dataSetup() { + SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); + xstream = simpleXstreamInitializer.getXstreamInstance(); + } + + @Test + public void convertXmlToObjectFromFile() { + try { + ClassLoader classLoader = getClass().getClassLoader(); + FileReader reader = new FileReader(classLoader.getResource("data-file.xml").getFile()); + Customer customer = (Customer) xstream.fromXML(reader); + Assert.assertNotNull(customer); + + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test + public void convertXmlToObjectFromString() { + Customer customer = SimpleDataGeneration.generateData(); + String dataXml = xstream.toXML(customer); + Customer convertedCustomer = (Customer) xstream.fromXML(dataXml); + Assert.assertNotNull(convertedCustomer); + } + + +} diff --git a/xstream/src/test/java/com/baeldung/utility/XStreamSimpleXmlTest.java b/xstream/src/test/java/com/baeldung/utility/XStreamSimpleXmlTest.java new file mode 100644 index 0000000000..83a965ce1b --- /dev/null +++ b/xstream/src/test/java/com/baeldung/utility/XStreamSimpleXmlTest.java @@ -0,0 +1,57 @@ +package com.baeldung.utility; + +import com.baeldung.initializer.SimpleXstreamInitializer; +import com.baeldung.pojo.AddressDetails; +import com.baeldung.pojo.ContactDetails; +import com.baeldung.pojo.Customer; +import com.thoughtworks.xstream.XStream; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class XStreamSimpleXmlTest { + + private Customer customer; + private String dataXml; + private XStream xstream; + + @Before + public void dataSetup() { + customer = SimpleDataGeneration.generateData(); + SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); + xstream = simpleXstreamInitializer.getXstreamInstance(); + xstream.processAnnotations(Customer.class); + xstream.processAnnotations(AddressDetails.class); + xstream.processAnnotations(ContactDetails.class); + xstream.omitField(Customer.class, "lastName"); + xstream.registerConverter(new MyDateConverter()); + // xstream.registerConverter(new MySingleValueConverter()); + xstream.aliasField("fn", Customer.class, "firstName"); + dataXml = xstream.toXML(customer); + } + + @Test + public void testClassAliasedAnnotation() { + Assert.assertNotEquals(-1, dataXml.indexOf("")); + } + + @Test + public void testFieldAliasedAnnotation() { + Assert.assertNotEquals(-1, dataXml.indexOf("")); + } + + @Test + public void testImplicitCollection() { + Assert.assertEquals(-1, dataXml.indexOf("contactDetailsList")); + } + + @Test + public void testDateFieldFormating() { + Assert.assertEquals("14-02-1986", dataXml.substring(dataXml.indexOf("") + 5, dataXml.indexOf(""))); + } + + @Test + public void testOmitField() { + Assert.assertEquals(-1, dataXml.indexOf("lastName")); + } +} diff --git a/xstream/src/test/resources/data-file-alias-field-complex.xml b/xstream/src/test/resources/data-file-alias-field-complex.xml new file mode 100644 index 0000000000..06050cd1ed --- /dev/null +++ b/xstream/src/test/resources/data-file-alias-field-complex.xml @@ -0,0 +1,15 @@ + + XStream + Java + 1986-02-14 04:14:05.874 UTC + + + 6673543265 + 0124-2460311 + + + 4676543565 + 0120-223312 + + + \ No newline at end of file diff --git a/xstream/src/test/resources/data-file-alias-field.xml b/xstream/src/test/resources/data-file-alias-field.xml new file mode 100644 index 0000000000..7e71d721ca --- /dev/null +++ b/xstream/src/test/resources/data-file-alias-field.xml @@ -0,0 +1,5 @@ + + XStream + Java + 1986-02-14 03:46:16.381 UTC + \ No newline at end of file diff --git a/xstream/src/test/resources/data-file-alias-implicit-collection.xml b/xstream/src/test/resources/data-file-alias-implicit-collection.xml new file mode 100644 index 0000000000..0cb852fc04 --- /dev/null +++ b/xstream/src/test/resources/data-file-alias-implicit-collection.xml @@ -0,0 +1,13 @@ + + XStream + Java + 1986-02-14 04:14:20.541 UTC + + 6673543265 + 0124-2460311 + + + 4676543565 + 0120-223312 + + \ No newline at end of file diff --git a/xstream/src/test/resources/data-file-alias.xml b/xstream/src/test/resources/data-file-alias.xml new file mode 100644 index 0000000000..61ee9f1ac3 --- /dev/null +++ b/xstream/src/test/resources/data-file-alias.xml @@ -0,0 +1,5 @@ + + XStream + Java + 1986-02-14 03:46:16.381 UTC + \ No newline at end of file diff --git a/xstream/src/test/resources/data-file-ignore-field.xml b/xstream/src/test/resources/data-file-ignore-field.xml new file mode 100644 index 0000000000..7dc8023b96 --- /dev/null +++ b/xstream/src/test/resources/data-file-ignore-field.xml @@ -0,0 +1,6 @@ + + XStream + Java + 1986-02-14 04:14:20.541 UTC + XStream Java + \ No newline at end of file diff --git a/xstream/src/test/resources/data-file.xml b/xstream/src/test/resources/data-file.xml new file mode 100644 index 0000000000..b8dbce32c0 --- /dev/null +++ b/xstream/src/test/resources/data-file.xml @@ -0,0 +1,5 @@ + + XStream + Java + 1986-02-14 03:46:16.381 UTC + \ No newline at end of file From d64c9e47f40bf4913af1088a448b24b959baa38d Mon Sep 17 00:00:00 2001 From: DOHA Date: Mon, 4 Apr 2016 13:41:57 +0200 Subject: [PATCH 12/16] minor fix --- .../main/java/org/baeldung/spring/SecSecurityConfig.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/spring-security-mvc-session/src/main/java/org/baeldung/spring/SecSecurityConfig.java b/spring-security-mvc-session/src/main/java/org/baeldung/spring/SecSecurityConfig.java index c62b795e01..deeea78e4e 100644 --- a/spring-security-mvc-session/src/main/java/org/baeldung/spring/SecSecurityConfig.java +++ b/spring-security-mvc-session/src/main/java/org/baeldung/spring/SecSecurityConfig.java @@ -7,6 +7,7 @@ import org.springframework.security.config.annotation.authentication.builders.Au import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.config.http.SessionCreationPolicy; import org.springframework.security.web.authentication.AuthenticationSuccessHandler; import org.springframework.security.web.session.HttpSessionEventPublisher; @@ -49,7 +50,12 @@ public class SecSecurityConfig extends WebSecurityConfigurerAdapter { .and() .rememberMe().key("uniqueAndSecret").tokenValiditySeconds(86400) .and() - .sessionManagement().invalidSessionUrl("/invalidSession.html").maximumSessions(2).expiredUrl("/sessionExpired.html"); + .sessionManagement() + .sessionFixation().migrateSession() + .sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED) + .invalidSessionUrl("/invalidSession.html") + .maximumSessions(2) + .expiredUrl("/sessionExpired.html"); // @formatter:on } From 08c8d82cd49eece79a6ae02f9e533bdfdb01840d Mon Sep 17 00:00:00 2001 From: DOHA Date: Mon, 4 Apr 2016 13:42:21 +0200 Subject: [PATCH 13/16] add java zip examples --- .../java/com/baeldung/unzip/UnzipFile.java | 17 ++++--- .../java/com/baeldung/zip/ZipDirectory.java | 43 ++++++++++++++++++ .../main/java/com/baeldung/zip/ZipFile.java | 17 ++++--- .../com/baeldung/zip/ZipMultipleFiles.java | 33 ++++++++++++++ core-java-8/src/main/resources/compressed.zip | Bin 0 -> 146 bytes .../src/main/resources/dirCompressed.zip | Bin 0 -> 634 bytes .../src/main/resources/multiCompressed.zip | Bin 0 -> 273 bytes .../src/main/resources/unzipTest/test1.txt | 1 + .../src/main/resources/zipTest/test1.txt | 1 + .../src/main/resources/zipTest/test2.txt | 1 + .../resources/zipTest/testFolder/test3.txt | 1 + .../resources/zipTest/testFolder/test4.txt | 1 + 12 files changed, 97 insertions(+), 18 deletions(-) create mode 100644 core-java-8/src/main/java/com/baeldung/zip/ZipDirectory.java create mode 100644 core-java-8/src/main/java/com/baeldung/zip/ZipMultipleFiles.java create mode 100644 core-java-8/src/main/resources/compressed.zip create mode 100644 core-java-8/src/main/resources/dirCompressed.zip create mode 100644 core-java-8/src/main/resources/multiCompressed.zip create mode 100644 core-java-8/src/main/resources/unzipTest/test1.txt create mode 100644 core-java-8/src/main/resources/zipTest/test1.txt create mode 100644 core-java-8/src/main/resources/zipTest/test2.txt create mode 100644 core-java-8/src/main/resources/zipTest/testFolder/test3.txt create mode 100644 core-java-8/src/main/resources/zipTest/testFolder/test4.txt diff --git a/core-java-8/src/main/java/com/baeldung/unzip/UnzipFile.java b/core-java-8/src/main/java/com/baeldung/unzip/UnzipFile.java index d0b4274731..813d8f20f8 100644 --- a/core-java-8/src/main/java/com/baeldung/unzip/UnzipFile.java +++ b/core-java-8/src/main/java/com/baeldung/unzip/UnzipFile.java @@ -2,27 +2,26 @@ package com.baeldung.unzip; import java.io.File; import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class UnzipFile { - public static void main(String[] args) throws FileNotFoundException, IOException { - String fileZip = "/opt/zipped/cities.zip"; - byte[] buffer = new byte[1024]; - ZipInputStream zis = new ZipInputStream(new FileInputStream(fileZip)); + public static void main(final String[] args) throws IOException { + final String fileZip = "src/main/resources/compressed.zip"; + final byte[] buffer = new byte[1024]; + final ZipInputStream zis = new ZipInputStream(new FileInputStream(fileZip)); ZipEntry zipEntry = zis.getNextEntry(); while(zipEntry != null){ - String fileName = zipEntry.getName(); - File newFile = new File("/opt/unzipped/" + fileName); - FileOutputStream fos = new FileOutputStream(newFile); + final String fileName = zipEntry.getName(); + final File newFile = new File("src/main/resources/unzipTest/" + fileName); + final FileOutputStream fos = new FileOutputStream(newFile); int len; while ((len = zis.read(buffer)) > 0) { fos.write(buffer, 0, len); } - fos.close(); + fos.close(); zipEntry = zis.getNextEntry(); } zis.closeEntry(); diff --git a/core-java-8/src/main/java/com/baeldung/zip/ZipDirectory.java b/core-java-8/src/main/java/com/baeldung/zip/ZipDirectory.java new file mode 100644 index 0000000000..7da71a093d --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/zip/ZipDirectory.java @@ -0,0 +1,43 @@ +package com.baeldung.zip; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +public class ZipDirectory { + public static void main(final String[] args) throws IOException { + final String sourceFile = "src/main/resources/zipTest"; + final FileOutputStream fos = new FileOutputStream("src/main/resources/dirCompressed.zip"); + final ZipOutputStream zipOut = new ZipOutputStream(fos); + final File fileToZip = new File(sourceFile); + + zipFile(fileToZip, fileToZip.getName(), zipOut); + zipOut.close(); + fos.close(); + } + + private static void zipFile(final File fileToZip, final String fileName, final ZipOutputStream zipOut) throws IOException { + if (fileToZip.isHidden()) { + return; + } + if (fileToZip.isDirectory()) { + final File[] children = fileToZip.listFiles(); + for (final File childFile : children) { + zipFile(childFile, fileName + "/" + childFile.getName(), zipOut); + } + return; + } + final FileInputStream fis = new FileInputStream(fileToZip); + final ZipEntry zipEntry = new ZipEntry(fileName); + zipOut.putNextEntry(zipEntry); + final byte[] bytes = new byte[1024]; + int length; + while ((length = fis.read(bytes)) >= 0) { + zipOut.write(bytes, 0, length); + } + fis.close(); + } +} \ No newline at end of file diff --git a/core-java-8/src/main/java/com/baeldung/zip/ZipFile.java b/core-java-8/src/main/java/com/baeldung/zip/ZipFile.java index dccd3f2347..af80ec9609 100644 --- a/core-java-8/src/main/java/com/baeldung/zip/ZipFile.java +++ b/core-java-8/src/main/java/com/baeldung/zip/ZipFile.java @@ -2,22 +2,21 @@ package com.baeldung.zip; import java.io.File; import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class ZipFile { - public static void main(String[] args) throws FileNotFoundException, IOException { - String sourceFile = "/opt/photos/photo.png"; - FileOutputStream fos = new FileOutputStream("/opt/zipped/cities.zip"); - ZipOutputStream zipOut = new ZipOutputStream(fos); - File fileToZip = new File(sourceFile); - FileInputStream fis = new FileInputStream(fileToZip); - ZipEntry zipEntry = new ZipEntry(fileToZip.getName()); + public static void main(final String[] args) throws IOException { + final String sourceFile = "src/main/resources/zipTest/test1.txt"; + final FileOutputStream fos = new FileOutputStream("src/main/resources/compressed.zip"); + final ZipOutputStream zipOut = new ZipOutputStream(fos); + final File fileToZip = new File(sourceFile); + final FileInputStream fis = new FileInputStream(fileToZip); + final ZipEntry zipEntry = new ZipEntry(fileToZip.getName()); zipOut.putNextEntry(zipEntry); - byte[] bytes = new byte[1024]; + final byte[] bytes = new byte[1024]; int length; while((length = fis.read(bytes)) >= 0) { zipOut.write(bytes, 0, length); diff --git a/core-java-8/src/main/java/com/baeldung/zip/ZipMultipleFiles.java b/core-java-8/src/main/java/com/baeldung/zip/ZipMultipleFiles.java new file mode 100644 index 0000000000..211696195d --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/zip/ZipMultipleFiles.java @@ -0,0 +1,33 @@ +package com.baeldung.zip; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.Arrays; +import java.util.List; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +public class ZipMultipleFiles { + public static void main(final String[] args) throws IOException { + final List srcFiles = Arrays.asList("src/main/resources/zipTest/test1.txt", "src/main/resources/zipTest/test2.txt"); + final FileOutputStream fos = new FileOutputStream("src/main/resources/multiCompressed.zip"); + final ZipOutputStream zipOut = new ZipOutputStream(fos); + for (final String srcFile : srcFiles) { + final File fileToZip = new File(srcFile); + final FileInputStream fis = new FileInputStream(fileToZip); + final ZipEntry zipEntry = new ZipEntry(fileToZip.getName()); + zipOut.putNextEntry(zipEntry); + + final byte[] bytes = new byte[1024]; + int length; + while((length = fis.read(bytes)) >= 0) { + zipOut.write(bytes, 0, length); + } + fis.close(); + } + zipOut.close(); + fos.close(); + } +} \ No newline at end of file diff --git a/core-java-8/src/main/resources/compressed.zip b/core-java-8/src/main/resources/compressed.zip new file mode 100644 index 0000000000000000000000000000000000000000..03f840ae2bf13bdf8e9509168e964dea40f782f7 GIT binary patch literal 146 zcmWIWW@Zs#;Nak3Xvk^tU_b(#Kz2!LafzW`Nkz$LkFzIFhI5?PKjj(7!Vuui&aqfV pQ-%+yga?QNycwB97!bxF%YlqR1#m+Hyjj^mQj9=o4y0Yd>Hv0b7M1`2 literal 0 HcmV?d00001 diff --git a/core-java-8/src/main/resources/dirCompressed.zip b/core-java-8/src/main/resources/dirCompressed.zip new file mode 100644 index 0000000000000000000000000000000000000000..f42d3aa5c651b72d7150dd7c6b7a805c3b3421b6 GIT binary patch literal 634 zcmWIWW@Zs#;Nak3sLW~cU_b(bKz3DTK}c$GiGB%?Fw`rlDEaJh_T`r4eqd88VX9b(Qr^4~108k&uwYXi1$7B;UlRtQ!@rD@vVAjSx2uCwA zi7+6-6ghxEYES{PC%_>b;Ek#sIXqCc`vOfu)(#F+bnVEV26+tu<^q|>+R=j$-6Ui` eqMGy_w@Kh23-D%T1KG<0ghGrA3_(E5zyJVb(vt-M literal 0 HcmV?d00001 diff --git a/core-java-8/src/main/resources/multiCompressed.zip b/core-java-8/src/main/resources/multiCompressed.zip new file mode 100644 index 0000000000000000000000000000000000000000..002e70ef81cd58a1945bbdf9f96c49140bd5bd2c GIT binary patch literal 273 zcmWIWW@Zs#;Nak3Xvt~uU_b(#Kz2!LafzW`Nkz$LkFzIFhI5?PKjj(7!Vuui&aqfV zQ-%+yga?QN2pVDpHe_wc2k$e!p(k|0KAbv#f*EYilatHK1%YPpgUw-N5@A3%2iXpg ob5H@?aRJ__+K`Pz)#eH`0i+G=oB(fDHjs%-K$r)ldqErq0N^t-RR910 literal 0 HcmV?d00001 diff --git a/core-java-8/src/main/resources/unzipTest/test1.txt b/core-java-8/src/main/resources/unzipTest/test1.txt new file mode 100644 index 0000000000..c57eff55eb --- /dev/null +++ b/core-java-8/src/main/resources/unzipTest/test1.txt @@ -0,0 +1 @@ +Hello World! \ No newline at end of file diff --git a/core-java-8/src/main/resources/zipTest/test1.txt b/core-java-8/src/main/resources/zipTest/test1.txt new file mode 100644 index 0000000000..c57eff55eb --- /dev/null +++ b/core-java-8/src/main/resources/zipTest/test1.txt @@ -0,0 +1 @@ +Hello World! \ No newline at end of file diff --git a/core-java-8/src/main/resources/zipTest/test2.txt b/core-java-8/src/main/resources/zipTest/test2.txt new file mode 100644 index 0000000000..f0fb0f14d1 --- /dev/null +++ b/core-java-8/src/main/resources/zipTest/test2.txt @@ -0,0 +1 @@ +My Name is John \ No newline at end of file diff --git a/core-java-8/src/main/resources/zipTest/testFolder/test3.txt b/core-java-8/src/main/resources/zipTest/testFolder/test3.txt new file mode 100644 index 0000000000..882edb168e --- /dev/null +++ b/core-java-8/src/main/resources/zipTest/testFolder/test3.txt @@ -0,0 +1 @@ +My Name is Tom \ No newline at end of file diff --git a/core-java-8/src/main/resources/zipTest/testFolder/test4.txt b/core-java-8/src/main/resources/zipTest/testFolder/test4.txt new file mode 100644 index 0000000000..a78c3fadc8 --- /dev/null +++ b/core-java-8/src/main/resources/zipTest/testFolder/test4.txt @@ -0,0 +1 @@ +My Name is Jane \ No newline at end of file From 3061fc7c0f9b17bf517259154900e9a70a0c512b Mon Sep 17 00:00:00 2001 From: Ivan Date: Mon, 4 Apr 2016 14:03:44 +0200 Subject: [PATCH 14/16] Add file upload example --- .../src/main/webapp/WEB-INF/view/fileUploadForm.jsp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp b/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp index 371acdc0ed..1414b824ff 100644 --- a/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp +++ b/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp @@ -8,13 +8,13 @@ -

Enter The File to Upload

+

Enter The File to Upload (Single file)

- + From 9630170b032304afb7adff66b29f59a70112352c Mon Sep 17 00:00:00 2001 From: David Morley Date: Thu, 7 Apr 2016 06:17:57 -0500 Subject: [PATCH 15/16] Rename package --- .../baeldung/aop/LoggingAspect.java | 6 +- .../baeldung/aop/PerformanceAspect.java | 2 +- .../baeldung/aop/PublishingAspect.java | 4 +- .../baeldung/aop/annotations/Entity.java | 2 +- .../baeldung/aop/annotations/Loggable.java | 2 +- .../{org => com}/baeldung/dao/FooDao.java | 6 +- .../baeldung/dialect/CustomDialect.java | 4 +- .../baeldung/events/FooCreationEvent.java | 2 +- .../events/FooCreationEventListener.java | 2 +- .../{org => com}/baeldung/model/Employee.java | 2 +- .../java/{org => com}/baeldung/model/Foo.java | 4 +- .../{org => com}/baeldung/model/User.java | 2 +- .../baeldung/processor/NameProcessor.java | 2 +- .../spring/web/config/ClientWebConfig.java | 4 +- .../config/ContentManagementWebConfig.java | 2 +- .../web/config/MainWebAppInitializer.java | 4 +- .../baeldung/spring/web/config/WebConfig.java | 4 +- .../java/{org => com}/baeldung/web/BeanA.java | 2 +- .../java/{org => com}/baeldung/web/BeanB.java | 2 +- .../web/controller/EmployeeController.java | 4 +- .../web/controller/FileUploadController.java | 2 +- .../web/controller/UserController.java | 4 +- .../spring/web/config/WebConfig.java.orig | 116 ------------------ .../{org => com}/baeldung/aop/beans.xml | 4 +- .../src/main/webapp/WEB-INF/web_old.xml | 2 +- .../baeldung/aop/AopLoggingTest.java | 8 +- .../baeldung/aop/AopPerformanceTest.java | 6 +- .../baeldung/aop/AopPublishingTest.java | 10 +- .../aop/AopXmlConfigPerformanceTest.java | 6 +- .../baeldung/config/TestConfig.java | 4 +- 30 files changed, 54 insertions(+), 170 deletions(-) rename spring-mvc-java/src/main/java/{org => com}/baeldung/aop/LoggingAspect.java (90%) rename spring-mvc-java/src/main/java/{org => com}/baeldung/aop/PerformanceAspect.java (97%) rename spring-mvc-java/src/main/java/{org => com}/baeldung/aop/PublishingAspect.java (94%) rename spring-mvc-java/src/main/java/{org => com}/baeldung/aop/annotations/Entity.java (86%) rename spring-mvc-java/src/main/java/{org => com}/baeldung/aop/annotations/Loggable.java (87%) rename spring-mvc-java/src/main/java/{org => com}/baeldung/dao/FooDao.java (75%) rename spring-mvc-java/src/main/java/{org => com}/baeldung/dialect/CustomDialect.java (86%) rename spring-mvc-java/src/main/java/{org => com}/baeldung/events/FooCreationEvent.java (86%) rename spring-mvc-java/src/main/java/{org => com}/baeldung/events/FooCreationEventListener.java (94%) rename spring-mvc-java/src/main/java/{org => com}/baeldung/model/Employee.java (97%) rename spring-mvc-java/src/main/java/{org => com}/baeldung/model/Foo.java (80%) rename spring-mvc-java/src/main/java/{org => com}/baeldung/model/User.java (90%) rename spring-mvc-java/src/main/java/{org => com}/baeldung/processor/NameProcessor.java (94%) rename spring-mvc-java/src/main/java/{org => com}/baeldung/spring/web/config/ClientWebConfig.java (97%) rename spring-mvc-java/src/main/java/{org => com}/baeldung/spring/web/config/ContentManagementWebConfig.java (97%) rename spring-mvc-java/src/main/java/{org => com}/baeldung/spring/web/config/MainWebAppInitializer.java (95%) rename spring-mvc-java/src/main/java/{org => com}/baeldung/spring/web/config/WebConfig.java (96%) rename spring-mvc-java/src/main/java/{org => com}/baeldung/web/BeanA.java (89%) rename spring-mvc-java/src/main/java/{org => com}/baeldung/web/BeanB.java (83%) rename spring-mvc-java/src/main/java/{org => com}/baeldung/web/controller/EmployeeController.java (95%) rename spring-mvc-java/src/main/java/{org => com}/baeldung/web/controller/FileUploadController.java (96%) rename spring-mvc-java/src/main/java/{org => com}/baeldung/web/controller/UserController.java (90%) delete mode 100644 spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java.orig rename spring-mvc-java/src/main/resources/{org => com}/baeldung/aop/beans.xml (86%) rename spring-mvc-java/src/test/java/{org => com}/baeldung/aop/AopLoggingTest.java (95%) rename spring-mvc-java/src/test/java/{org => com}/baeldung/aop/AopPerformanceTest.java (95%) rename spring-mvc-java/src/test/java/{org => com}/baeldung/aop/AopPublishingTest.java (90%) rename spring-mvc-java/src/test/java/{org => com}/baeldung/aop/AopXmlConfigPerformanceTest.java (94%) rename spring-mvc-java/src/test/java/{org => com}/baeldung/config/TestConfig.java (67%) diff --git a/spring-mvc-java/src/main/java/org/baeldung/aop/LoggingAspect.java b/spring-mvc-java/src/main/java/com/baeldung/aop/LoggingAspect.java similarity index 90% rename from spring-mvc-java/src/main/java/org/baeldung/aop/LoggingAspect.java rename to spring-mvc-java/src/main/java/com/baeldung/aop/LoggingAspect.java index c59c4f060a..7ae37404be 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/aop/LoggingAspect.java +++ b/spring-mvc-java/src/main/java/com/baeldung/aop/LoggingAspect.java @@ -1,4 +1,4 @@ -package org.baeldung.aop; +package com.baeldung.aop; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; @@ -27,11 +27,11 @@ public class LoggingAspect { public void repositoryMethods() { } - @Pointcut("@annotation(org.baeldung.aop.annotations.Loggable)") + @Pointcut("@annotation(com.baeldung.aop.annotations.Loggable)") public void loggableMethods() { } - @Pointcut("@args(org.baeldung.aop.annotations.Entity)") + @Pointcut("@args(com.baeldung.aop.annotations.Entity)") public void methodsAcceptingEntities() { } diff --git a/spring-mvc-java/src/main/java/org/baeldung/aop/PerformanceAspect.java b/spring-mvc-java/src/main/java/com/baeldung/aop/PerformanceAspect.java similarity index 97% rename from spring-mvc-java/src/main/java/org/baeldung/aop/PerformanceAspect.java rename to spring-mvc-java/src/main/java/com/baeldung/aop/PerformanceAspect.java index 2d07e5a5f3..1f2076adff 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/aop/PerformanceAspect.java +++ b/spring-mvc-java/src/main/java/com/baeldung/aop/PerformanceAspect.java @@ -1,4 +1,4 @@ -package org.baeldung.aop; +package com.baeldung.aop; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; diff --git a/spring-mvc-java/src/main/java/org/baeldung/aop/PublishingAspect.java b/spring-mvc-java/src/main/java/com/baeldung/aop/PublishingAspect.java similarity index 94% rename from spring-mvc-java/src/main/java/org/baeldung/aop/PublishingAspect.java rename to spring-mvc-java/src/main/java/com/baeldung/aop/PublishingAspect.java index 324605dab1..7791c63e7b 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/aop/PublishingAspect.java +++ b/spring-mvc-java/src/main/java/com/baeldung/aop/PublishingAspect.java @@ -1,10 +1,10 @@ -package org.baeldung.aop; +package com.baeldung.aop; +import com.baeldung.events.FooCreationEvent; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; -import org.baeldung.events.FooCreationEvent; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationEventPublisher; import org.springframework.stereotype.Component; diff --git a/spring-mvc-java/src/main/java/org/baeldung/aop/annotations/Entity.java b/spring-mvc-java/src/main/java/com/baeldung/aop/annotations/Entity.java similarity index 86% rename from spring-mvc-java/src/main/java/org/baeldung/aop/annotations/Entity.java rename to spring-mvc-java/src/main/java/com/baeldung/aop/annotations/Entity.java index f964c3979e..61d91b0777 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/aop/annotations/Entity.java +++ b/spring-mvc-java/src/main/java/com/baeldung/aop/annotations/Entity.java @@ -1,4 +1,4 @@ -package org.baeldung.aop.annotations; +package com.baeldung.aop.annotations; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; diff --git a/spring-mvc-java/src/main/java/org/baeldung/aop/annotations/Loggable.java b/spring-mvc-java/src/main/java/com/baeldung/aop/annotations/Loggable.java similarity index 87% rename from spring-mvc-java/src/main/java/org/baeldung/aop/annotations/Loggable.java rename to spring-mvc-java/src/main/java/com/baeldung/aop/annotations/Loggable.java index ef2863957f..92aa950e58 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/aop/annotations/Loggable.java +++ b/spring-mvc-java/src/main/java/com/baeldung/aop/annotations/Loggable.java @@ -1,4 +1,4 @@ -package org.baeldung.aop.annotations; +package com.baeldung.aop.annotations; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; diff --git a/spring-mvc-java/src/main/java/org/baeldung/dao/FooDao.java b/spring-mvc-java/src/main/java/com/baeldung/dao/FooDao.java similarity index 75% rename from spring-mvc-java/src/main/java/org/baeldung/dao/FooDao.java rename to spring-mvc-java/src/main/java/com/baeldung/dao/FooDao.java index f204440b2d..1d28b082ec 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/dao/FooDao.java +++ b/spring-mvc-java/src/main/java/com/baeldung/dao/FooDao.java @@ -1,7 +1,7 @@ -package org.baeldung.dao; +package com.baeldung.dao; -import org.baeldung.aop.annotations.Loggable; -import org.baeldung.model.Foo; +import com.baeldung.aop.annotations.Loggable; +import com.baeldung.model.Foo; import org.springframework.stereotype.Repository; @Repository diff --git a/spring-mvc-java/src/main/java/org/baeldung/dialect/CustomDialect.java b/spring-mvc-java/src/main/java/com/baeldung/dialect/CustomDialect.java similarity index 86% rename from spring-mvc-java/src/main/java/org/baeldung/dialect/CustomDialect.java rename to spring-mvc-java/src/main/java/com/baeldung/dialect/CustomDialect.java index e6d1ad6b74..0c6a7c3ae0 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/dialect/CustomDialect.java +++ b/spring-mvc-java/src/main/java/com/baeldung/dialect/CustomDialect.java @@ -1,9 +1,9 @@ -package org.baeldung.dialect; +package com.baeldung.dialect; import java.util.HashSet; import java.util.Set; -import org.baeldung.processor.NameProcessor; +import com.baeldung.processor.NameProcessor; import org.thymeleaf.dialect.AbstractDialect; import org.thymeleaf.processor.IProcessor; diff --git a/spring-mvc-java/src/main/java/org/baeldung/events/FooCreationEvent.java b/spring-mvc-java/src/main/java/com/baeldung/events/FooCreationEvent.java similarity index 86% rename from spring-mvc-java/src/main/java/org/baeldung/events/FooCreationEvent.java rename to spring-mvc-java/src/main/java/com/baeldung/events/FooCreationEvent.java index af11f3a4be..5ea4b46c04 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/events/FooCreationEvent.java +++ b/spring-mvc-java/src/main/java/com/baeldung/events/FooCreationEvent.java @@ -1,4 +1,4 @@ -package org.baeldung.events; +package com.baeldung.events; import org.springframework.context.ApplicationEvent; diff --git a/spring-mvc-java/src/main/java/org/baeldung/events/FooCreationEventListener.java b/spring-mvc-java/src/main/java/com/baeldung/events/FooCreationEventListener.java similarity index 94% rename from spring-mvc-java/src/main/java/org/baeldung/events/FooCreationEventListener.java rename to spring-mvc-java/src/main/java/com/baeldung/events/FooCreationEventListener.java index 35dcfd2bc3..c0aa744bc1 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/events/FooCreationEventListener.java +++ b/spring-mvc-java/src/main/java/com/baeldung/events/FooCreationEventListener.java @@ -1,4 +1,4 @@ -package org.baeldung.events; +package com.baeldung.events; import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component; diff --git a/spring-mvc-java/src/main/java/org/baeldung/model/Employee.java b/spring-mvc-java/src/main/java/com/baeldung/model/Employee.java similarity index 97% rename from spring-mvc-java/src/main/java/org/baeldung/model/Employee.java rename to spring-mvc-java/src/main/java/com/baeldung/model/Employee.java index 5365068a89..d0f6b724eb 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/model/Employee.java +++ b/spring-mvc-java/src/main/java/com/baeldung/model/Employee.java @@ -1,4 +1,4 @@ -package org.baeldung.model; +package com.baeldung.model; import javax.xml.bind.annotation.XmlRootElement; diff --git a/spring-mvc-java/src/main/java/org/baeldung/model/Foo.java b/spring-mvc-java/src/main/java/com/baeldung/model/Foo.java similarity index 80% rename from spring-mvc-java/src/main/java/org/baeldung/model/Foo.java rename to spring-mvc-java/src/main/java/com/baeldung/model/Foo.java index 87bd7132e6..01f5f43f60 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/model/Foo.java +++ b/spring-mvc-java/src/main/java/com/baeldung/model/Foo.java @@ -1,6 +1,6 @@ -package org.baeldung.model; +package com.baeldung.model; -import org.baeldung.aop.annotations.Entity; +import com.baeldung.aop.annotations.Entity; @Entity public class Foo { diff --git a/spring-mvc-java/src/main/java/org/baeldung/model/User.java b/spring-mvc-java/src/main/java/com/baeldung/model/User.java similarity index 90% rename from spring-mvc-java/src/main/java/org/baeldung/model/User.java rename to spring-mvc-java/src/main/java/com/baeldung/model/User.java index df549cd21d..dc4480c986 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/model/User.java +++ b/spring-mvc-java/src/main/java/com/baeldung/model/User.java @@ -1,4 +1,4 @@ -package org.baeldung.model; +package com.baeldung.model; public class User { private String firstname; diff --git a/spring-mvc-java/src/main/java/org/baeldung/processor/NameProcessor.java b/spring-mvc-java/src/main/java/com/baeldung/processor/NameProcessor.java similarity index 94% rename from spring-mvc-java/src/main/java/org/baeldung/processor/NameProcessor.java rename to spring-mvc-java/src/main/java/com/baeldung/processor/NameProcessor.java index df9a4da7f0..9a7857198c 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/processor/NameProcessor.java +++ b/spring-mvc-java/src/main/java/com/baeldung/processor/NameProcessor.java @@ -1,4 +1,4 @@ -package org.baeldung.processor; +package com.baeldung.processor; import org.thymeleaf.Arguments; import org.thymeleaf.dom.Element; diff --git a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ClientWebConfig.java b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ClientWebConfig.java similarity index 97% rename from spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ClientWebConfig.java rename to spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ClientWebConfig.java index 6084943ddd..c108a450ae 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ClientWebConfig.java +++ b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ClientWebConfig.java @@ -1,9 +1,9 @@ -package org.baeldung.spring.web.config; +package com.baeldung.spring.web.config; import java.util.HashSet; import java.util.Set; -import org.baeldung.dialect.CustomDialect; +import com.baeldung.dialect.CustomDialect; import org.springframework.context.MessageSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; diff --git a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ContentManagementWebConfig.java b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ContentManagementWebConfig.java similarity index 97% rename from spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ContentManagementWebConfig.java rename to spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ContentManagementWebConfig.java index 2c5b423029..9780575678 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ContentManagementWebConfig.java +++ b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ContentManagementWebConfig.java @@ -1,4 +1,4 @@ -package org.baeldung.spring.web.config; +package com.baeldung.spring.web.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; diff --git a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/MainWebAppInitializer.java b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/MainWebAppInitializer.java similarity index 95% rename from spring-mvc-java/src/main/java/org/baeldung/spring/web/config/MainWebAppInitializer.java rename to spring-mvc-java/src/main/java/com/baeldung/spring/web/config/MainWebAppInitializer.java index ad37bbec5e..4a11ba986c 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/MainWebAppInitializer.java +++ b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/MainWebAppInitializer.java @@ -1,4 +1,4 @@ -package org.baeldung.spring.web.config; +package com.baeldung.spring.web.config; import java.util.Set; @@ -25,7 +25,7 @@ public class MainWebAppInitializer implements WebApplicationInitializer { // Create the 'root' Spring application context final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext(); - root.scan("org.baeldung.spring.web.config"); + root.scan("com.baeldung.spring.web.config"); // root.getEnvironment().setDefaultProfiles("embedded"); // Manages the lifecycle of the root application context diff --git a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebConfig.java similarity index 96% rename from spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java rename to spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebConfig.java index bd2af0886e..693fd74f74 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java +++ b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebConfig.java @@ -1,4 +1,4 @@ -package org.baeldung.spring.web.config; +package com.baeldung.spring.web.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; @@ -16,7 +16,7 @@ import org.springframework.web.servlet.view.XmlViewResolver; @Configuration @EnableWebMvc -@ComponentScan("org.baeldung.web") +@ComponentScan("com.baeldung.web") public class WebConfig extends WebMvcConfigurerAdapter { public WebConfig() { diff --git a/spring-mvc-java/src/main/java/org/baeldung/web/BeanA.java b/spring-mvc-java/src/main/java/com/baeldung/web/BeanA.java similarity index 89% rename from spring-mvc-java/src/main/java/org/baeldung/web/BeanA.java rename to spring-mvc-java/src/main/java/com/baeldung/web/BeanA.java index b6b6f49c16..79fac724f7 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/web/BeanA.java +++ b/spring-mvc-java/src/main/java/com/baeldung/web/BeanA.java @@ -1,4 +1,4 @@ -package org.baeldung.web; +package com.baeldung.web; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; diff --git a/spring-mvc-java/src/main/java/org/baeldung/web/BeanB.java b/spring-mvc-java/src/main/java/com/baeldung/web/BeanB.java similarity index 83% rename from spring-mvc-java/src/main/java/org/baeldung/web/BeanB.java rename to spring-mvc-java/src/main/java/com/baeldung/web/BeanB.java index 49e5af4ccb..05c9560a0c 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/web/BeanB.java +++ b/spring-mvc-java/src/main/java/com/baeldung/web/BeanB.java @@ -1,4 +1,4 @@ -package org.baeldung.web; +package com.baeldung.web; import org.springframework.stereotype.Component; diff --git a/spring-mvc-java/src/main/java/org/baeldung/web/controller/EmployeeController.java b/spring-mvc-java/src/main/java/com/baeldung/web/controller/EmployeeController.java similarity index 95% rename from spring-mvc-java/src/main/java/org/baeldung/web/controller/EmployeeController.java rename to spring-mvc-java/src/main/java/com/baeldung/web/controller/EmployeeController.java index e18bbdbf63..38272b23cb 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/web/controller/EmployeeController.java +++ b/spring-mvc-java/src/main/java/com/baeldung/web/controller/EmployeeController.java @@ -1,9 +1,9 @@ -package org.baeldung.web.controller; +package com.baeldung.web.controller; import java.util.HashMap; import java.util.Map; -import org.baeldung.model.Employee; +import com.baeldung.model.Employee; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.validation.BindingResult; diff --git a/spring-mvc-java/src/main/java/org/baeldung/web/controller/FileUploadController.java b/spring-mvc-java/src/main/java/com/baeldung/web/controller/FileUploadController.java similarity index 96% rename from spring-mvc-java/src/main/java/org/baeldung/web/controller/FileUploadController.java rename to spring-mvc-java/src/main/java/com/baeldung/web/controller/FileUploadController.java index 6f557adf0f..bc9cf13c34 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/web/controller/FileUploadController.java +++ b/spring-mvc-java/src/main/java/com/baeldung/web/controller/FileUploadController.java @@ -1,4 +1,4 @@ -package org.baeldung.web.controller; +package com.baeldung.web.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; diff --git a/spring-mvc-java/src/main/java/org/baeldung/web/controller/UserController.java b/spring-mvc-java/src/main/java/com/baeldung/web/controller/UserController.java similarity index 90% rename from spring-mvc-java/src/main/java/org/baeldung/web/controller/UserController.java rename to spring-mvc-java/src/main/java/com/baeldung/web/controller/UserController.java index da39a36adf..fda159f204 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/web/controller/UserController.java +++ b/spring-mvc-java/src/main/java/com/baeldung/web/controller/UserController.java @@ -1,6 +1,6 @@ -package org.baeldung.web.controller; +package com.baeldung.web.controller; -import org.baeldung.model.User; +import com.baeldung.model.User; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; diff --git a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java.orig b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java.orig deleted file mode 100644 index 78307849b4..0000000000 --- a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java.orig +++ /dev/null @@ -1,116 +0,0 @@ -package org.baeldung.spring.web.config; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; -import org.springframework.core.io.ClassPathResource; -import org.springframework.web.multipart.commons.CommonsMultipartResolver; -import org.springframework.web.servlet.ViewResolver; -import org.springframework.web.servlet.config.annotation.EnableWebMvc; -import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; -import org.springframework.web.servlet.view.InternalResourceViewResolver; -import org.springframework.web.servlet.view.JstlView; -import org.springframework.web.servlet.view.ResourceBundleViewResolver; -import org.springframework.web.servlet.view.XmlViewResolver; - -@Configuration -@EnableWebMvc -@ComponentScan("org.baeldung.web") -public class WebConfig extends WebMvcConfigurerAdapter { - -<<<<<<< HEAD - public WebConfig() { - super(); - } - - // - - @Override - public void addViewControllers(final ViewControllerRegistry registry) { - super.addViewControllers(registry); - registry.addViewController("/sample.html"); - } - - @Bean - public ViewResolver internalResourceViewResolver() { - final InternalResourceViewResolver bean = new InternalResourceViewResolver(); - bean.setViewClass(JstlView.class); - bean.setPrefix("/WEB-INF/view/"); - bean.setSuffix(".jsp"); - bean.setOrder(2); - return bean; - } - - @Bean - public ViewResolver xmlViewResolver() { - final XmlViewResolver bean = new XmlViewResolver(); - bean.setLocation(new ClassPathResource("views.xml")); - bean.setOrder(1); - return bean; - } - - @Bean - public ViewResolver resourceBundleViewResolver() { - final ResourceBundleViewResolver bean = new ResourceBundleViewResolver(); - bean.setBasename("views"); - bean.setOrder(0); - return bean; - } - -======= - public WebConfig() { - super(); - } - - // @Bean - // public StandardServletMultipartResolver multipartResolver() { - // return new StandardServletMultipartResolver(); - // } - - @Bean(name = "multipartResolver") - public CommonsMultipartResolver multipartResolver() { - - final CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(); - multipartResolver.setMaxUploadSize(100000); - - return multipartResolver; - } - - @Override - public void addViewControllers(final ViewControllerRegistry registry) { - - super.addViewControllers(registry); - registry.addViewController("/sample.html"); - } - - @Bean - public ViewResolver internalResourceViewResolver() { - - final InternalResourceViewResolver bean = new InternalResourceViewResolver(); - bean.setViewClass(JstlView.class); - bean.setPrefix("/WEB-INF/view/"); - bean.setSuffix(".jsp"); - bean.setOrder(2); - return bean; - } - - @Bean - public ViewResolver xmlViewResolver() { - - final XmlViewResolver bean = new XmlViewResolver(); - bean.setLocation(new ClassPathResource("views.xml")); - bean.setOrder(1); - return bean; - } - - @Bean - public ViewResolver resourceBundleViewResolver() { - - final ResourceBundleViewResolver bean = new ResourceBundleViewResolver(); - bean.setBasename("views"); - bean.setOrder(0); - return bean; - } ->>>>>>> 3061fc7c0f9b17bf517259154900e9a70a0c512b -} \ No newline at end of file diff --git a/spring-mvc-java/src/main/resources/org/baeldung/aop/beans.xml b/spring-mvc-java/src/main/resources/com/baeldung/aop/beans.xml similarity index 86% rename from spring-mvc-java/src/main/resources/org/baeldung/aop/beans.xml rename to spring-mvc-java/src/main/resources/com/baeldung/aop/beans.xml index 17c63e39e4..e6aa9d77c4 100644 --- a/spring-mvc-java/src/main/resources/org/baeldung/aop/beans.xml +++ b/spring-mvc-java/src/main/resources/com/baeldung/aop/beans.xml @@ -7,8 +7,8 @@ http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> - - + + diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/web_old.xml b/spring-mvc-java/src/main/webapp/WEB-INF/web_old.xml index 016369ad27..c8b38fae30 100644 --- a/spring-mvc-java/src/main/webapp/WEB-INF/web_old.xml +++ b/spring-mvc-java/src/main/webapp/WEB-INF/web_old.xml @@ -16,7 +16,7 @@ contextConfigLocation - org.baeldung.spring.web.config + com.baeldung.spring.web.config diff --git a/spring-mvc-java/src/test/java/org/baeldung/aop/AopLoggingTest.java b/spring-mvc-java/src/test/java/com/baeldung/aop/AopLoggingTest.java similarity index 95% rename from spring-mvc-java/src/test/java/org/baeldung/aop/AopLoggingTest.java rename to spring-mvc-java/src/test/java/com/baeldung/aop/AopLoggingTest.java index b1c9867e41..19bf4d0fac 100644 --- a/spring-mvc-java/src/test/java/org/baeldung/aop/AopLoggingTest.java +++ b/spring-mvc-java/src/test/java/com/baeldung/aop/AopLoggingTest.java @@ -1,8 +1,8 @@ -package org.baeldung.aop; +package com.baeldung.aop; -import org.baeldung.config.TestConfig; -import org.baeldung.dao.FooDao; -import org.baeldung.model.Foo; +import com.baeldung.config.TestConfig; +import com.baeldung.dao.FooDao; +import com.baeldung.model.Foo; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; diff --git a/spring-mvc-java/src/test/java/org/baeldung/aop/AopPerformanceTest.java b/spring-mvc-java/src/test/java/com/baeldung/aop/AopPerformanceTest.java similarity index 95% rename from spring-mvc-java/src/test/java/org/baeldung/aop/AopPerformanceTest.java rename to spring-mvc-java/src/test/java/com/baeldung/aop/AopPerformanceTest.java index 69083c60a2..4ad5a3e1a6 100644 --- a/spring-mvc-java/src/test/java/org/baeldung/aop/AopPerformanceTest.java +++ b/spring-mvc-java/src/test/java/com/baeldung/aop/AopPerformanceTest.java @@ -1,7 +1,7 @@ -package org.baeldung.aop; +package com.baeldung.aop; -import org.baeldung.config.TestConfig; -import org.baeldung.dao.FooDao; +import com.baeldung.config.TestConfig; +import com.baeldung.dao.FooDao; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; diff --git a/spring-mvc-java/src/test/java/org/baeldung/aop/AopPublishingTest.java b/spring-mvc-java/src/test/java/com/baeldung/aop/AopPublishingTest.java similarity index 90% rename from spring-mvc-java/src/test/java/org/baeldung/aop/AopPublishingTest.java rename to spring-mvc-java/src/test/java/com/baeldung/aop/AopPublishingTest.java index e691dbd32e..c075db9fc6 100644 --- a/spring-mvc-java/src/test/java/org/baeldung/aop/AopPublishingTest.java +++ b/spring-mvc-java/src/test/java/com/baeldung/aop/AopPublishingTest.java @@ -1,9 +1,9 @@ -package org.baeldung.aop; +package com.baeldung.aop; -import org.baeldung.config.TestConfig; -import org.baeldung.dao.FooDao; -import org.baeldung.events.FooCreationEventListener; -import org.baeldung.model.Foo; +import com.baeldung.config.TestConfig; +import com.baeldung.dao.FooDao; +import com.baeldung.events.FooCreationEventListener; +import com.baeldung.model.Foo; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; diff --git a/spring-mvc-java/src/test/java/org/baeldung/aop/AopXmlConfigPerformanceTest.java b/spring-mvc-java/src/test/java/com/baeldung/aop/AopXmlConfigPerformanceTest.java similarity index 94% rename from spring-mvc-java/src/test/java/org/baeldung/aop/AopXmlConfigPerformanceTest.java rename to spring-mvc-java/src/test/java/com/baeldung/aop/AopXmlConfigPerformanceTest.java index 7ef25d743c..4d2df50d18 100644 --- a/spring-mvc-java/src/test/java/org/baeldung/aop/AopXmlConfigPerformanceTest.java +++ b/spring-mvc-java/src/test/java/com/baeldung/aop/AopXmlConfigPerformanceTest.java @@ -1,6 +1,6 @@ -package org.baeldung.aop; +package com.baeldung.aop; -import org.baeldung.dao.FooDao; +import com.baeldung.dao.FooDao; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -21,7 +21,7 @@ import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration("/org/baeldung/aop/beans.xml") +@ContextConfiguration("/com/baeldung/aop/beans.xml") public class AopXmlConfigPerformanceTest { @Before diff --git a/spring-mvc-java/src/test/java/org/baeldung/config/TestConfig.java b/spring-mvc-java/src/test/java/com/baeldung/config/TestConfig.java similarity index 67% rename from spring-mvc-java/src/test/java/org/baeldung/config/TestConfig.java rename to spring-mvc-java/src/test/java/com/baeldung/config/TestConfig.java index f9573b2add..641513a24a 100644 --- a/spring-mvc-java/src/test/java/org/baeldung/config/TestConfig.java +++ b/spring-mvc-java/src/test/java/com/baeldung/config/TestConfig.java @@ -1,11 +1,11 @@ -package org.baeldung.config; +package com.baeldung.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy; @Configuration -@ComponentScan(basePackages = { "org.baeldung.dao", "org.baeldung.aop", "org.baeldung.events" }) +@ComponentScan(basePackages = { "com.baeldung.dao", "com.baeldung.aop", "com.baeldung.events" }) @EnableAspectJAutoProxy public class TestConfig { } From d575f443bc9c33d0ce39b9aea2e983c2e8951282 Mon Sep 17 00:00:00 2001 From: DOHA Date: Fri, 8 Apr 2016 12:05:59 +0200 Subject: [PATCH 16/16] minor fix --- .../java/org/baeldung/java/io/JavaXToWriterUnitTest.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/core-java/src/test/java/org/baeldung/java/io/JavaXToWriterUnitTest.java b/core-java/src/test/java/org/baeldung/java/io/JavaXToWriterUnitTest.java index 35ec15df16..eb393668bd 100644 --- a/core-java/src/test/java/org/baeldung/java/io/JavaXToWriterUnitTest.java +++ b/core-java/src/test/java/org/baeldung/java/io/JavaXToWriterUnitTest.java @@ -1,5 +1,7 @@ package org.baeldung.java.io; +import static org.junit.Assert.assertEquals; + import java.io.IOException; import java.io.StringWriter; import java.io.Writer; @@ -23,6 +25,8 @@ public class JavaXToWriterUnitTest { final Writer targetWriter = new StringWriter().append(new String(initialArray)); targetWriter.close(); + + assertEquals("With Java", targetWriter.toString()); } @Test @@ -40,6 +44,8 @@ public class JavaXToWriterUnitTest { charSink.write(buffer); stringWriter.close(); + + assertEquals("With Guava", stringWriter.toString()); } @Test @@ -48,6 +54,8 @@ public class JavaXToWriterUnitTest { final Writer targetWriter = new StringBuilderWriter(new StringBuilder(new String(initialArray))); targetWriter.close(); + + assertEquals("With Commons IO", targetWriter.toString()); } }
Select a file to upload (Single file)Select a file to upload