From fa8afc9fbf575cdeb9a0ad755584e18671bbb0dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Pfeuffer?= Date: Thu, 30 Apr 2020 14:59:05 +0200 Subject: [PATCH] Add simple test util to verify serializability --- .../scm/store/SerializationTestUtil.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 scm-test/src/main/java/sonia/scm/store/SerializationTestUtil.java diff --git a/scm-test/src/main/java/sonia/scm/store/SerializationTestUtil.java b/scm-test/src/main/java/sonia/scm/store/SerializationTestUtil.java new file mode 100644 index 0000000000..84c1fe202c --- /dev/null +++ b/scm-test/src/main/java/sonia/scm/store/SerializationTestUtil.java @@ -0,0 +1,52 @@ +/* + * MIT License + * + * Copyright (c) 2020-present Cloudogu GmbH and Contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package sonia.scm.store; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +import javax.xml.bind.JAXB; +import java.io.StringReader; +import java.io.StringWriter; + +public class SerializationTestUtil { + + static T toAndFromJson(Class clazz, T input) throws JsonProcessingException { + final ObjectMapper objectMapper = new ObjectMapper(); + final String json = objectMapper.writeValueAsString(input); + return objectMapper.readValue(json, clazz); + } + + static T toAndFromXml(Class clazz, T input) { + final StringWriter xmlWriter = new StringWriter(); + JAXB.marshal(input, xmlWriter); + final StringReader xmlReader = new StringReader(xmlWriter.toString()); + return JAXB.unmarshal(xmlReader, clazz); + } + + static T toAndFromJsonAndXml(Class clazz, T input) throws JsonProcessingException { + return toAndFromXml(clazz, toAndFromJson(clazz, input)); + } +}