From 289ae780001c31d7d5d75e0d58608c13f44549a2 Mon Sep 17 00:00:00 2001 From: joehni Date: Sat, 22 Apr 2023 18:03:39 +0200 Subject: [PATCH] Add unit test to suppress system attributes. Closes #333. --- .../thoughtworks/acceptance/AliasTest.java | 69 +++++++++++++++---- 1 file changed, 57 insertions(+), 12 deletions(-) diff --git a/xstream/src/test/com/thoughtworks/acceptance/AliasTest.java b/xstream/src/test/com/thoughtworks/acceptance/AliasTest.java index 44dd006f9..c78a89cd3 100644 --- a/xstream/src/test/com/thoughtworks/acceptance/AliasTest.java +++ b/xstream/src/test/com/thoughtworks/acceptance/AliasTest.java @@ -1,6 +1,6 @@ /* * Copyright (C) 2004, 2005, 2006 Joe Walnes. - * Copyright (C) 2006, 2007, 2008, 2009, 2013, 2014, 2018, 2019, 2020 XStream Committers. + * Copyright (C) 2006, 2007, 2008, 2009, 2013, 2014, 2018, 2019, 2020, 2023 XStream Committers. * All rights reserved. * * The software in this package is published under the terms of the BSD @@ -18,6 +18,7 @@ import com.thoughtworks.acceptance.objects.Category; import com.thoughtworks.acceptance.objects.Product; import com.thoughtworks.acceptance.objects.Software; +import com.thoughtworks.acceptance.someobjects.Protocol; import com.thoughtworks.acceptance.someobjects.WithList; import com.thoughtworks.acceptance.someobjects.X; import com.thoughtworks.xstream.XStream; @@ -283,12 +284,7 @@ public void testCanOverwriteInheritedAlias() { xstream.aliasField("c", TypeC.class, "attrC"); xstream.aliasField("y", TypeC.class, "attrA"); final TypeC object = new TypeC(); - final String xml = "" - + "\n" - + " testA\n" - + " testB\n" - + " testC\n" - + ""; + final String xml = "" + "\n" + " testA\n" + " testB\n" + " testC\n" + ""; assertBothWays(object, xml); } @@ -392,10 +388,11 @@ public void testClassTakesPrecedenceOfPackage() { assertBothWays(withList, xml); } - + @SuppressWarnings("unused") - private void takingDoubles(Double d1, double d2) {} - + private void takingDoubles(final Double d1, final double d2) { + } + public void testCanCreateAliasingJavaTypeConverter() throws NoSuchFieldException, NoSuchMethodException { final Mapper mapper = new MapperWrapper(xstream.getMapper().lookupMapperOfType(ArrayMapper.class)) { @Override @@ -418,8 +415,8 @@ public Class realClass(final String elementName) { list.add(int[][][].class); list.add(Integer[][][].class); list.add(TypeA.class.getDeclaredField("attrA")); - list.add(Product.class.getConstructor(new Class[]{String.class, String.class, double.class})); - list.add(getClass().getDeclaredMethod("takingDoubles", new Class[]{Double.class, double.class})); + list.add(Product.class.getConstructor(String.class, String.class, double.class)); + list.add(getClass().getDeclaredMethod("takingDoubles", Double.class, double.class)); list.add(ArrayList.class); final String xml = "" @@ -452,4 +449,52 @@ public Class realClass(final String elementName) { assertBothWays(list, xml); } + + public static class Protocols { + static class HTTP extends Protocol { + public HTTP() { + super("http"); + } + } + + static class TCP extends Protocol { + public TCP() { + super("tcp"); + } + } + + static class UDP extends Protocol { + public UDP() { + super("udp"); + } + } + + Object http; + TCP tcp; + Protocol udp; + } + + public void testErasesSystemAttribute() { + xstream.alias("protocols", Protocols.class); + xstream.aliasSystemAttribute(null, "class"); + + final Protocols exceptions = new Protocols(); + exceptions.http = new Protocols.HTTP(); + exceptions.tcp = new Protocols.TCP(); + exceptions.udp = new Protocols.UDP(); + + final String expected = "" + + "\n" + + " \n" + + " http\n" + + " \n" + + " \n" + + " tcp\n" + + " \n" + + " \n" + + " udp\n" + + " \n" + + ""; + assertEquals(expected, xstream.toXML(exceptions)); + } }