|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.struts2.rest.handler; |
| 20 | + |
| 21 | +import com.opensymphony.xwork2.ActionContext; |
| 22 | +import com.opensymphony.xwork2.ActionSupport; |
| 23 | +import com.opensymphony.xwork2.XWorkTestCase; |
| 24 | +import com.opensymphony.xwork2.mock.MockActionInvocation; |
| 25 | +import com.thoughtworks.xstream.XStream; |
| 26 | +import com.thoughtworks.xstream.io.xml.StaxDriver; |
| 27 | +import com.thoughtworks.xstream.security.ExplicitTypePermission; |
| 28 | +import com.thoughtworks.xstream.security.TypePermission; |
| 29 | +import org.apache.struts2.rest.handler.xstream.XStreamAllowedClassNames; |
| 30 | +import org.apache.struts2.rest.handler.xstream.XStreamAllowedClasses; |
| 31 | +import org.apache.struts2.rest.handler.xstream.XStreamPermissionProvider; |
| 32 | +import org.apache.struts2.rest.handler.xstream.XStreamProvider; |
| 33 | + |
| 34 | +import java.io.Reader; |
| 35 | +import java.io.StringReader; |
| 36 | +import java.io.StringWriter; |
| 37 | +import java.io.Writer; |
| 38 | +import java.util.ArrayList; |
| 39 | +import java.util.Arrays; |
| 40 | +import java.util.Collection; |
| 41 | +import java.util.Collections; |
| 42 | +import java.util.HashMap; |
| 43 | +import java.util.HashSet; |
| 44 | +import java.util.Locale; |
| 45 | +import java.util.Set; |
| 46 | + |
| 47 | +import static org.assertj.core.api.Assertions.assertThat; |
| 48 | + |
| 49 | +public class XStreamHandlerTest extends XWorkTestCase { |
| 50 | + |
| 51 | + private XStreamHandler handler; |
| 52 | + private MockActionInvocation ai; |
| 53 | + |
| 54 | + public void setUp() throws Exception { |
| 55 | + super.setUp(); |
| 56 | + handler = new XStreamHandler(); |
| 57 | + ai = new MockActionInvocation(); |
| 58 | + ActionSupport action = new ActionSupport(); |
| 59 | + ActionContext context = ActionContext.of(new HashMap<>()).withLocale(Locale.US); |
| 60 | + ai.setInvocationContext(context); |
| 61 | + ai.setAction(action); |
| 62 | + } |
| 63 | + |
| 64 | + public void testObjectToXml() throws Exception { |
| 65 | + // given |
| 66 | + SimpleBean obj = new SimpleBean(); |
| 67 | + obj.setName("Jan"); |
| 68 | + obj.setAge(12L); |
| 69 | + obj.setParents(Arrays.asList("Adam", "Ewa")); |
| 70 | + |
| 71 | + // when |
| 72 | + Writer stream = new StringWriter(); |
| 73 | + handler.fromObject(ai, obj, null, stream); |
| 74 | + |
| 75 | + // then |
| 76 | + stream.flush(); |
| 77 | + assertThat(stream.toString()) |
| 78 | + .contains("<org.apache.struts2.rest.handler.SimpleBean>") |
| 79 | + .contains("<name>Jan</name>") |
| 80 | + .contains("<age>12</age>") |
| 81 | + .contains("<parents class=\"java.util.Arrays$ArrayList\">") |
| 82 | + .contains("<string>Adam</string>") |
| 83 | + .contains("<string>Ewa</string>") |
| 84 | + .contains("</org.apache.struts2.rest.handler.SimpleBean>"); |
| 85 | + } |
| 86 | + |
| 87 | + public void testXmlToObject() { |
| 88 | + // given |
| 89 | + String xml = "<?xml version='1.0' encoding='UTF-8'?><org.apache.struts2.rest.handler.SimpleBean><name>Jan</name><age>12</age><parents class=\"java.util.ArrayList\"><string>Adam</string><string>Ewa</string></parents></org.apache.struts2.rest.handler.SimpleBean>"; |
| 90 | + |
| 91 | + SimpleBean obj = new SimpleBean(); |
| 92 | + ai.setAction(new SimpleAction()); |
| 93 | + |
| 94 | + // when |
| 95 | + Reader in = new StringReader(xml); |
| 96 | + handler.toObject(ai, in, obj); |
| 97 | + |
| 98 | + // then |
| 99 | + assertNotNull(obj); |
| 100 | + assertEquals("Jan", obj.getName()); |
| 101 | + assertEquals(12L, obj.getAge().longValue()); |
| 102 | + assertNotNull(obj.getParents()); |
| 103 | + assertThat(obj.getParents()) |
| 104 | + .hasSize(2) |
| 105 | + .containsExactly("Adam", "Ewa"); |
| 106 | + } |
| 107 | + |
| 108 | + public void testXmlToObjectWithAliases() { |
| 109 | + // given |
| 110 | + String xml = "<?xml version='1.0' encoding='UTF-8'?><data><name>Jan</name><age>12</age><parents><string>Adam</string><string>Ewa</string></parents></data>"; |
| 111 | + |
| 112 | + SimpleBean obj = new SimpleBean(); |
| 113 | + ai.setAction(new SimpleAliasAction()); |
| 114 | + |
| 115 | + // when |
| 116 | + Reader in = new StringReader(xml); |
| 117 | + handler.toObject(ai, in, obj); |
| 118 | + |
| 119 | + // then |
| 120 | + assertNotNull(obj); |
| 121 | + assertEquals("Jan", obj.getName()); |
| 122 | + assertEquals(12L, obj.getAge().longValue()); |
| 123 | + assertNotNull(obj.getParents()); |
| 124 | + assertThat(obj.getParents()) |
| 125 | + .hasSize(2) |
| 126 | + .containsExactly("Adam", "Ewa"); |
| 127 | + } |
| 128 | + |
| 129 | + private static class SimpleAction implements XStreamAllowedClasses, XStreamAllowedClassNames, XStreamPermissionProvider { |
| 130 | + @Override |
| 131 | + public Set<Class<?>> allowedClasses() { |
| 132 | + Set<Class<?>> classes = new HashSet<>(); |
| 133 | + classes.add(SimpleBean.class); |
| 134 | + return classes; |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public Set<String> allowedClassNames() { |
| 139 | + return Collections.emptySet(); |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public Collection<TypePermission> getTypePermissions() { |
| 144 | + return Collections.emptyList(); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + private static class SimpleAliasAction extends SimpleAction implements XStreamProvider { |
| 149 | + @Override |
| 150 | + public XStream createXStream() { |
| 151 | + XStream stream = new XStream(new StaxDriver()); |
| 152 | + stream.alias("parents", ArrayList.class); |
| 153 | + stream.alias("data", SimpleBean.class); |
| 154 | + return stream; |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments