From 9260720568cee9e868d2899228eceed0c3359323 Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Tue, 20 Feb 2018 06:49:50 +0100 Subject: [PATCH] Adds test to cover basic functionality --- .../rest/handler/JacksonXmlHandlerTest.java | 87 +++++++++++++++++++ .../struts2/rest/handler/SimpleBean.java | 52 +++++++++++ 2 files changed, 139 insertions(+) create mode 100644 plugins/rest/src/test/java/org/apache/struts2/rest/handler/JacksonXmlHandlerTest.java create mode 100644 plugins/rest/src/test/java/org/apache/struts2/rest/handler/SimpleBean.java diff --git a/plugins/rest/src/test/java/org/apache/struts2/rest/handler/JacksonXmlHandlerTest.java b/plugins/rest/src/test/java/org/apache/struts2/rest/handler/JacksonXmlHandlerTest.java new file mode 100644 index 000000000..e2c4eda0a --- /dev/null +++ b/plugins/rest/src/test/java/org/apache/struts2/rest/handler/JacksonXmlHandlerTest.java @@ -0,0 +1,87 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.struts2.rest.handler; + +import com.opensymphony.xwork2.ActionInvocation; +import com.opensymphony.xwork2.XWorkTestCase; +import com.opensymphony.xwork2.mock.MockActionInvocation; + +import java.io.Reader; +import java.io.StringReader; +import java.io.StringWriter; +import java.io.Writer; +import java.util.Arrays; + +import static org.fest.assertions.Assertions.assertThat; + +public class JacksonXmlHandlerTest extends XWorkTestCase { + + private String xml; + private JacksonXmlHandler handler; + private ActionInvocation ai; + + public void setUp() throws Exception { + super.setUp(); + xml = "" + + "Jan" + + "12" + + "" + + "Adam" + + "Ewa" + + "" + + ""; + handler = new JacksonXmlHandler(); + ai = new MockActionInvocation(); + } + + public void testObjectToXml() throws Exception { + // given + SimpleBean obj = new SimpleBean(); + obj.setName("Jan"); + obj.setAge(12L); + obj.setParents(Arrays.asList("Adam", "Ewa")); + + // when + Writer stream = new StringWriter(); + handler.fromObject(ai, obj, null, stream); + + // then + stream.flush(); + assertEquals(xml, stream.toString()); + } + + public void testXmlToObject() throws Exception { + // given + SimpleBean obj = new SimpleBean(); + + // when + Reader in = new StringReader(xml); + handler.toObject(ai, in, obj); + + // then + assertNotNull(obj); + assertEquals(obj.getName(), "Jan"); + assertEquals(obj.getAge().longValue(), 12L); + assertNotNull(obj.getParents()); + assertThat(obj.getParents()) + .hasSize(2) + .containsExactly("Adam", "Ewa"); + } + +} \ No newline at end of file diff --git a/plugins/rest/src/test/java/org/apache/struts2/rest/handler/SimpleBean.java b/plugins/rest/src/test/java/org/apache/struts2/rest/handler/SimpleBean.java new file mode 100644 index 000000000..ad155d7e7 --- /dev/null +++ b/plugins/rest/src/test/java/org/apache/struts2/rest/handler/SimpleBean.java @@ -0,0 +1,52 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.struts2.rest.handler; + +import java.util.List; + +public class SimpleBean { + + private String name; + private Long age; + private List parents; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Long getAge() { + return age; + } + + public void setAge(Long age) { + this.age = age; + } + + public List getParents() { + return parents; + } + + public void setParents(List parents) { + this.parents = parents; + } +}