Adds XmlSerializerTest Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/79813266 Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/79813266 Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/79813266
Branch: refs/heads/master Commit: 7981326612d20a0d6bd9b7d972620f5e2e0b4fc3 Parents: e15697f Author: Aled Sage <aled.s...@gmail.com> Authored: Wed Jun 22 13:24:30 2016 +0100 Committer: Aled Sage <aled.s...@gmail.com> Committed: Wed Jun 22 13:24:30 2016 +0100 ---------------------------------------------------------------------- .../util/core/xstream/XmlSerializerTest.java | 77 ++++++++++++++++++++ 1 file changed, 77 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/79813266/core/src/test/java/org/apache/brooklyn/util/core/xstream/XmlSerializerTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/util/core/xstream/XmlSerializerTest.java b/core/src/test/java/org/apache/brooklyn/util/core/xstream/XmlSerializerTest.java new file mode 100644 index 0000000..f89e770 --- /dev/null +++ b/core/src/test/java/org/apache/brooklyn/util/core/xstream/XmlSerializerTest.java @@ -0,0 +1,77 @@ +/* + * 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.brooklyn.util.core.xstream; + +import static org.testng.Assert.assertEquals; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +public class XmlSerializerTest { + + private static final Logger LOG = LoggerFactory.getLogger(XmlSerializerTest.class); + + protected XmlSerializer<Object> serializer; + + @BeforeMethod(alwaysRun=true) + private void setUp() { + serializer = new XmlSerializer<Object>(); + } + + @Test + public void testSimple() throws Exception { + assertSerializeAndDeserialize("abc"); + assertSerializeAndDeserialize(1); + assertSerializeAndDeserialize(true); + assertSerializeAndDeserialize(new StringHolder("abc")); + } + + @Test + public void testIllegalXmlCharacter() throws Exception { + String val = "abc\u001b"; + assertEquals((int)val.charAt(3), 27); // expect that to give us unicode character 27 + assertSerializeAndDeserialize(val); + assertSerializeAndDeserialize(new StringHolder(val)); + } + + protected void assertSerializeAndDeserialize(Object val) throws Exception { + String xml = serializer.toString(val); + Object result = serializer.fromString(xml); + LOG.info("val="+val+"'; xml="+xml+"; result="+result); + assertEquals(result, val); + } + + public static class StringHolder { + public String val; + + StringHolder(String val) { + this.val = val; + } + @Override + public boolean equals(Object obj) { + return (obj instanceof StringHolder) && val.equals(((StringHolder)obj).val); + } + @Override + public int hashCode() { + return val.hashCode(); + } + } +}